Bluetooth: Reject incoming SCO connections without listeners
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / gpio / max732x.c
blobf7868243af899a46e69f3cb52064bb06d85bab86
1 /*
2 * max732x.c - I2C Port Expander with 8/16 I/O
4 * Copyright (C) 2007 Marvell International Ltd.
5 * Copyright (C) 2008 Jack Ren <jack.ren@marvell.com>
6 * Copyright (C) 2008 Eric Miao <eric.miao@marvell.com>
8 * Derived from drivers/gpio/pca953x.c
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/gpio.h>
21 #include <linux/i2c.h>
22 #include <linux/i2c/max732x.h>
26 * Each port of MAX732x (including MAX7319) falls into one of the
27 * following three types:
29 * - Push Pull Output
30 * - Input
31 * - Open Drain I/O
33 * designated by 'O', 'I' and 'P' individually according to MAXIM's
34 * datasheets.
36 * There are two groups of I/O ports, each group usually includes
37 * up to 8 I/O ports, and is accessed by a specific I2C address:
39 * - Group A : by I2C address 0b'110xxxx
40 * - Group B : by I2C address 0b'101xxxx
42 * where 'xxxx' is decided by the connections of pin AD2/AD0. The
43 * address used also affects the initial state of output signals.
45 * Within each group of ports, there are five known combinations of
46 * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
47 * the detailed organization of these ports.
49 * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
50 * and GPIOs from GROUP_A are numbered before those from GROUP_B
51 * (if there are two groups).
53 * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
54 * they are not supported by this driver.
57 #define PORT_NONE 0x0 /* '/' No Port */
58 #define PORT_OUTPUT 0x1 /* 'O' Push-Pull, Output Only */
59 #define PORT_INPUT 0x2 /* 'I' Input Only */
60 #define PORT_OPENDRAIN 0x3 /* 'P' Open-Drain, I/O */
62 #define IO_4I4O 0x5AA5 /* O7 O6 I5 I4 I3 I2 O1 O0 */
63 #define IO_4P4O 0x5FF5 /* O7 O6 P5 P4 P3 P2 O1 O0 */
64 #define IO_8I 0xAAAA /* I7 I6 I5 I4 I3 I2 I1 I0 */
65 #define IO_8P 0xFFFF /* P7 P6 P5 P4 P3 P2 P1 P0 */
66 #define IO_8O 0x5555 /* O7 O6 O5 O4 O3 O2 O1 O0 */
68 #define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
69 #define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
71 static const struct i2c_device_id max732x_id[] = {
72 { "max7319", GROUP_A(IO_8I) },
73 { "max7320", GROUP_B(IO_8O) },
74 { "max7321", GROUP_A(IO_8P) },
75 { "max7322", GROUP_A(IO_4I4O) },
76 { "max7323", GROUP_A(IO_4P4O) },
77 { "max7324", GROUP_A(IO_8I) | GROUP_B(IO_8O) },
78 { "max7325", GROUP_A(IO_8P) | GROUP_B(IO_8O) },
79 { "max7326", GROUP_A(IO_4I4O) | GROUP_B(IO_8O) },
80 { "max7327", GROUP_A(IO_4P4O) | GROUP_B(IO_8O) },
81 { },
83 MODULE_DEVICE_TABLE(i2c, max732x_id);
85 struct max732x_chip {
86 struct gpio_chip gpio_chip;
88 struct i2c_client *client; /* "main" client */
89 struct i2c_client *client_dummy;
90 struct i2c_client *client_group_a;
91 struct i2c_client *client_group_b;
93 unsigned int mask_group_a;
94 unsigned int dir_input;
95 unsigned int dir_output;
97 struct mutex lock;
98 uint8_t reg_out[2];
101 static int max732x_write(struct max732x_chip *chip, int group_a, uint8_t val)
103 struct i2c_client *client;
104 int ret;
106 client = group_a ? chip->client_group_a : chip->client_group_b;
107 ret = i2c_smbus_write_byte(client, val);
108 if (ret < 0) {
109 dev_err(&client->dev, "failed writing\n");
110 return ret;
113 return 0;
116 static int max732x_read(struct max732x_chip *chip, int group_a, uint8_t *val)
118 struct i2c_client *client;
119 int ret;
121 client = group_a ? chip->client_group_a : chip->client_group_b;
122 ret = i2c_smbus_read_byte(client);
123 if (ret < 0) {
124 dev_err(&client->dev, "failed reading\n");
125 return ret;
128 *val = (uint8_t)ret;
129 return 0;
132 static inline int is_group_a(struct max732x_chip *chip, unsigned off)
134 return (1u << off) & chip->mask_group_a;
137 static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
139 struct max732x_chip *chip;
140 uint8_t reg_val;
141 int ret;
143 chip = container_of(gc, struct max732x_chip, gpio_chip);
145 ret = max732x_read(chip, is_group_a(chip, off), &reg_val);
146 if (ret < 0)
147 return 0;
149 return reg_val & (1u << (off & 0x7));
152 static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
154 struct max732x_chip *chip;
155 uint8_t reg_out, mask = 1u << (off & 0x7);
156 int ret;
158 chip = container_of(gc, struct max732x_chip, gpio_chip);
160 mutex_lock(&chip->lock);
162 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
163 reg_out = (val) ? reg_out | mask : reg_out & ~mask;
165 ret = max732x_write(chip, is_group_a(chip, off), reg_out);
166 if (ret < 0)
167 goto out;
169 /* update the shadow register then */
170 if (off > 7)
171 chip->reg_out[1] = reg_out;
172 else
173 chip->reg_out[0] = reg_out;
174 out:
175 mutex_unlock(&chip->lock);
178 static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
180 struct max732x_chip *chip;
181 unsigned int mask = 1u << off;
183 chip = container_of(gc, struct max732x_chip, gpio_chip);
185 if ((mask & chip->dir_input) == 0) {
186 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
187 chip->client->name, off);
188 return -EACCES;
191 return 0;
194 static int max732x_gpio_direction_output(struct gpio_chip *gc,
195 unsigned off, int val)
197 struct max732x_chip *chip;
198 unsigned int mask = 1u << off;
200 chip = container_of(gc, struct max732x_chip, gpio_chip);
202 if ((mask & chip->dir_output) == 0) {
203 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
204 chip->client->name, off);
205 return -EACCES;
208 max732x_gpio_set_value(gc, off, val);
209 return 0;
212 static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
213 const struct i2c_device_id *id,
214 unsigned gpio_start)
216 struct gpio_chip *gc = &chip->gpio_chip;
217 uint32_t id_data = id->driver_data;
218 int i, port = 0;
220 for (i = 0; i < 16; i++, id_data >>= 2) {
221 unsigned int mask = 1 << port;
223 switch (id_data & 0x3) {
224 case PORT_OUTPUT:
225 chip->dir_output |= mask;
226 break;
227 case PORT_INPUT:
228 chip->dir_input |= mask;
229 break;
230 case PORT_OPENDRAIN:
231 chip->dir_output |= mask;
232 chip->dir_input |= mask;
233 break;
234 default:
235 continue;
238 if (i < 8)
239 chip->mask_group_a |= mask;
240 port++;
243 if (chip->dir_input)
244 gc->direction_input = max732x_gpio_direction_input;
245 if (chip->dir_output) {
246 gc->direction_output = max732x_gpio_direction_output;
247 gc->set = max732x_gpio_set_value;
249 gc->get = max732x_gpio_get_value;
250 gc->can_sleep = 1;
252 gc->base = gpio_start;
253 gc->ngpio = port;
254 gc->label = chip->client->name;
255 gc->owner = THIS_MODULE;
257 return port;
260 static int __devinit max732x_probe(struct i2c_client *client,
261 const struct i2c_device_id *id)
263 struct max732x_platform_data *pdata;
264 struct max732x_chip *chip;
265 struct i2c_client *c;
266 uint16_t addr_a, addr_b;
267 int ret, nr_port;
269 pdata = client->dev.platform_data;
270 if (pdata == NULL) {
271 dev_dbg(&client->dev, "no platform data\n");
272 return -EINVAL;
275 chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
276 if (chip == NULL)
277 return -ENOMEM;
278 chip->client = client;
280 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
282 addr_a = (client->addr & 0x0f) | 0x60;
283 addr_b = (client->addr & 0x0f) | 0x50;
285 switch (client->addr & 0x70) {
286 case 0x60:
287 chip->client_group_a = client;
288 if (nr_port > 7) {
289 c = i2c_new_dummy(client->adapter, addr_b);
290 chip->client_group_b = chip->client_dummy = c;
292 break;
293 case 0x50:
294 chip->client_group_b = client;
295 if (nr_port > 7) {
296 c = i2c_new_dummy(client->adapter, addr_a);
297 chip->client_group_a = chip->client_dummy = c;
299 break;
300 default:
301 dev_err(&client->dev, "invalid I2C address specified %02x\n",
302 client->addr);
303 ret = -EINVAL;
304 goto out_failed;
307 mutex_init(&chip->lock);
309 max732x_read(chip, is_group_a(chip, 0), &chip->reg_out[0]);
310 if (nr_port > 7)
311 max732x_read(chip, is_group_a(chip, 8), &chip->reg_out[1]);
313 ret = gpiochip_add(&chip->gpio_chip);
314 if (ret)
315 goto out_failed;
317 if (pdata->setup) {
318 ret = pdata->setup(client, chip->gpio_chip.base,
319 chip->gpio_chip.ngpio, pdata->context);
320 if (ret < 0)
321 dev_warn(&client->dev, "setup failed, %d\n", ret);
324 i2c_set_clientdata(client, chip);
325 return 0;
327 out_failed:
328 kfree(chip);
329 return ret;
332 static int __devexit max732x_remove(struct i2c_client *client)
334 struct max732x_platform_data *pdata = client->dev.platform_data;
335 struct max732x_chip *chip = i2c_get_clientdata(client);
336 int ret;
338 if (pdata->teardown) {
339 ret = pdata->teardown(client, chip->gpio_chip.base,
340 chip->gpio_chip.ngpio, pdata->context);
341 if (ret < 0) {
342 dev_err(&client->dev, "%s failed, %d\n",
343 "teardown", ret);
344 return ret;
348 ret = gpiochip_remove(&chip->gpio_chip);
349 if (ret) {
350 dev_err(&client->dev, "%s failed, %d\n",
351 "gpiochip_remove()", ret);
352 return ret;
355 /* unregister any dummy i2c_client */
356 if (chip->client_dummy)
357 i2c_unregister_device(chip->client_dummy);
359 kfree(chip);
360 return 0;
363 static struct i2c_driver max732x_driver = {
364 .driver = {
365 .name = "max732x",
366 .owner = THIS_MODULE,
368 .probe = max732x_probe,
369 .remove = __devexit_p(max732x_remove),
370 .id_table = max732x_id,
373 static int __init max732x_init(void)
375 return i2c_add_driver(&max732x_driver);
377 /* register after i2c postcore initcall and before
378 * subsys initcalls that may rely on these GPIOs
380 subsys_initcall(max732x_init);
382 static void __exit max732x_exit(void)
384 i2c_del_driver(&max732x_driver);
386 module_exit(max732x_exit);
388 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
389 MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
390 MODULE_LICENSE("GPL");