GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / gpio / max732x.c
blob7ae00547b03063407b050dfac5c75c5b724a5769
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>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/i2c.h>
23 #include <linux/i2c/max732x.h>
27 #define PORT_NONE 0x0 /* '/' No Port */
28 #define PORT_OUTPUT 0x1 /* 'O' Push-Pull, Output Only */
29 #define PORT_INPUT 0x2 /* 'I' Input Only */
30 #define PORT_OPENDRAIN 0x3 /* 'P' Open-Drain, I/O */
32 #define IO_4I4O 0x5AA5 /* O7 O6 I5 I4 I3 I2 O1 O0 */
33 #define IO_4P4O 0x5FF5 /* O7 O6 P5 P4 P3 P2 O1 O0 */
34 #define IO_8I 0xAAAA /* I7 I6 I5 I4 I3 I2 I1 I0 */
35 #define IO_8P 0xFFFF /* P7 P6 P5 P4 P3 P2 P1 P0 */
36 #define IO_8O 0x5555 /* O7 O6 O5 O4 O3 O2 O1 O0 */
38 #define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
39 #define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
41 #define INT_NONE 0x0 /* No interrupt capability */
42 #define INT_NO_MASK 0x1 /* Has interrupts, no mask */
43 #define INT_INDEP_MASK 0x2 /* Has interrupts, independent mask */
44 #define INT_MERGED_MASK 0x3 /* Has interrupts, merged mask */
46 #define INT_CAPS(x) (((uint64_t)(x)) << 32)
48 enum {
49 MAX7319,
50 MAX7320,
51 MAX7321,
52 MAX7322,
53 MAX7323,
54 MAX7324,
55 MAX7325,
56 MAX7326,
57 MAX7327,
60 static uint64_t max732x_features[] = {
61 [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
62 [MAX7320] = GROUP_B(IO_8O),
63 [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
64 [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
65 [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
66 [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
67 [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
68 [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
69 [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
72 static const struct i2c_device_id max732x_id[] = {
73 { "max7319", MAX7319 },
74 { "max7320", MAX7320 },
75 { "max7321", MAX7321 },
76 { "max7322", MAX7322 },
77 { "max7323", MAX7323 },
78 { "max7324", MAX7324 },
79 { "max7325", MAX7325 },
80 { "max7326", MAX7326 },
81 { "max7327", MAX7327 },
82 { },
84 MODULE_DEVICE_TABLE(i2c, max732x_id);
86 struct max732x_chip {
87 struct gpio_chip gpio_chip;
89 struct i2c_client *client; /* "main" client */
90 struct i2c_client *client_dummy;
91 struct i2c_client *client_group_a;
92 struct i2c_client *client_group_b;
94 unsigned int mask_group_a;
95 unsigned int dir_input;
96 unsigned int dir_output;
98 struct mutex lock;
99 uint8_t reg_out[2];
101 #ifdef CONFIG_GPIO_MAX732X_IRQ
102 struct mutex irq_lock;
103 int irq_base;
104 uint8_t irq_mask;
105 uint8_t irq_mask_cur;
106 uint8_t irq_trig_raise;
107 uint8_t irq_trig_fall;
108 uint8_t irq_features;
109 #endif
112 static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
114 struct i2c_client *client;
115 int ret;
117 client = group_a ? chip->client_group_a : chip->client_group_b;
118 ret = i2c_smbus_write_byte(client, val);
119 if (ret < 0) {
120 dev_err(&client->dev, "failed writing\n");
121 return ret;
124 return 0;
127 static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
129 struct i2c_client *client;
130 int ret;
132 client = group_a ? chip->client_group_a : chip->client_group_b;
133 ret = i2c_smbus_read_byte(client);
134 if (ret < 0) {
135 dev_err(&client->dev, "failed reading\n");
136 return ret;
139 *val = (uint8_t)ret;
140 return 0;
143 static inline int is_group_a(struct max732x_chip *chip, unsigned off)
145 return (1u << off) & chip->mask_group_a;
148 static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
150 struct max732x_chip *chip;
151 uint8_t reg_val;
152 int ret;
154 chip = container_of(gc, struct max732x_chip, gpio_chip);
156 ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
157 if (ret < 0)
158 return 0;
160 return reg_val & (1u << (off & 0x7));
163 static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
165 struct max732x_chip *chip;
166 uint8_t reg_out, mask = 1u << (off & 0x7);
167 int ret;
169 chip = container_of(gc, struct max732x_chip, gpio_chip);
171 mutex_lock(&chip->lock);
173 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
174 reg_out = (val) ? reg_out | mask : reg_out & ~mask;
176 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
177 if (ret < 0)
178 goto out;
180 /* update the shadow register then */
181 if (off > 7)
182 chip->reg_out[1] = reg_out;
183 else
184 chip->reg_out[0] = reg_out;
185 out:
186 mutex_unlock(&chip->lock);
189 static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
191 struct max732x_chip *chip;
192 unsigned int mask = 1u << off;
194 chip = container_of(gc, struct max732x_chip, gpio_chip);
196 if ((mask & chip->dir_input) == 0) {
197 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
198 chip->client->name, off);
199 return -EACCES;
203 * Open-drain pins must be set to high impedance (which is
204 * equivalent to output-high) to be turned into an input.
206 if ((mask & chip->dir_output))
207 max732x_gpio_set_value(gc, off, 1);
209 return 0;
212 static int max732x_gpio_direction_output(struct gpio_chip *gc,
213 unsigned off, int val)
215 struct max732x_chip *chip;
216 unsigned int mask = 1u << off;
218 chip = container_of(gc, struct max732x_chip, gpio_chip);
220 if ((mask & chip->dir_output) == 0) {
221 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
222 chip->client->name, off);
223 return -EACCES;
226 max732x_gpio_set_value(gc, off, val);
227 return 0;
230 #ifdef CONFIG_GPIO_MAX732X_IRQ
231 static int max732x_writew(struct max732x_chip *chip, uint16_t val)
233 int ret;
235 val = cpu_to_le16(val);
237 ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
238 if (ret < 0) {
239 dev_err(&chip->client_group_a->dev, "failed writing\n");
240 return ret;
243 return 0;
246 static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
248 int ret;
250 ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
251 if (ret < 0) {
252 dev_err(&chip->client_group_a->dev, "failed reading\n");
253 return ret;
256 *val = le16_to_cpu(*val);
257 return 0;
260 static void max732x_irq_update_mask(struct max732x_chip *chip)
262 uint16_t msg;
264 if (chip->irq_mask == chip->irq_mask_cur)
265 return;
267 chip->irq_mask = chip->irq_mask_cur;
269 if (chip->irq_features == INT_NO_MASK)
270 return;
272 mutex_lock(&chip->lock);
274 switch (chip->irq_features) {
275 case INT_INDEP_MASK:
276 msg = (chip->irq_mask << 8) | chip->reg_out[0];
277 max732x_writew(chip, msg);
278 break;
280 case INT_MERGED_MASK:
281 msg = chip->irq_mask | chip->reg_out[0];
282 max732x_writeb(chip, 1, (uint8_t)msg);
283 break;
286 mutex_unlock(&chip->lock);
289 static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
291 struct max732x_chip *chip;
293 chip = container_of(gc, struct max732x_chip, gpio_chip);
294 return chip->irq_base + off;
297 static void max732x_irq_mask(unsigned int irq)
299 struct max732x_chip *chip = get_irq_chip_data(irq);
301 chip->irq_mask_cur &= ~(1 << (irq - chip->irq_base));
304 static void max732x_irq_unmask(unsigned int irq)
306 struct max732x_chip *chip = get_irq_chip_data(irq);
308 chip->irq_mask_cur |= 1 << (irq - chip->irq_base);
311 static void max732x_irq_bus_lock(unsigned int irq)
313 struct max732x_chip *chip = get_irq_chip_data(irq);
315 mutex_lock(&chip->irq_lock);
316 chip->irq_mask_cur = chip->irq_mask;
319 static void max732x_irq_bus_sync_unlock(unsigned int irq)
321 struct max732x_chip *chip = get_irq_chip_data(irq);
323 max732x_irq_update_mask(chip);
324 mutex_unlock(&chip->irq_lock);
327 static int max732x_irq_set_type(unsigned int irq, unsigned int type)
329 struct max732x_chip *chip = get_irq_chip_data(irq);
330 uint16_t off = irq - chip->irq_base;
331 uint16_t mask = 1 << off;
333 if (!(mask & chip->dir_input)) {
334 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
335 chip->client->name, off);
336 return -EACCES;
339 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
340 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
341 irq, type);
342 return -EINVAL;
345 if (type & IRQ_TYPE_EDGE_FALLING)
346 chip->irq_trig_fall |= mask;
347 else
348 chip->irq_trig_fall &= ~mask;
350 if (type & IRQ_TYPE_EDGE_RISING)
351 chip->irq_trig_raise |= mask;
352 else
353 chip->irq_trig_raise &= ~mask;
355 return max732x_gpio_direction_input(&chip->gpio_chip, off);
358 static struct irq_chip max732x_irq_chip = {
359 .name = "max732x",
360 .mask = max732x_irq_mask,
361 .unmask = max732x_irq_unmask,
362 .bus_lock = max732x_irq_bus_lock,
363 .bus_sync_unlock = max732x_irq_bus_sync_unlock,
364 .set_type = max732x_irq_set_type,
367 static uint8_t max732x_irq_pending(struct max732x_chip *chip)
369 uint8_t cur_stat;
370 uint8_t old_stat;
371 uint8_t trigger;
372 uint8_t pending;
373 uint16_t status;
374 int ret;
376 ret = max732x_readw(chip, &status);
377 if (ret)
378 return 0;
380 trigger = status >> 8;
381 trigger &= chip->irq_mask;
383 if (!trigger)
384 return 0;
386 cur_stat = status & 0xFF;
387 cur_stat &= chip->irq_mask;
389 old_stat = cur_stat ^ trigger;
391 pending = (old_stat & chip->irq_trig_fall) |
392 (cur_stat & chip->irq_trig_raise);
393 pending &= trigger;
395 return pending;
398 static irqreturn_t max732x_irq_handler(int irq, void *devid)
400 struct max732x_chip *chip = devid;
401 uint8_t pending;
402 uint8_t level;
404 pending = max732x_irq_pending(chip);
406 if (!pending)
407 return IRQ_HANDLED;
409 do {
410 level = __ffs(pending);
411 handle_nested_irq(level + chip->irq_base);
413 pending &= ~(1 << level);
414 } while (pending);
416 return IRQ_HANDLED;
419 static int max732x_irq_setup(struct max732x_chip *chip,
420 const struct i2c_device_id *id)
422 struct i2c_client *client = chip->client;
423 struct max732x_platform_data *pdata = client->dev.platform_data;
424 int has_irq = max732x_features[id->driver_data] >> 32;
425 int ret;
427 if (pdata->irq_base && has_irq != INT_NONE) {
428 int lvl;
430 chip->irq_base = pdata->irq_base;
431 chip->irq_features = has_irq;
432 mutex_init(&chip->irq_lock);
434 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
435 int irq = lvl + chip->irq_base;
437 if (!(chip->dir_input & (1 << lvl)))
438 continue;
440 set_irq_chip_data(irq, chip);
441 set_irq_chip_and_handler(irq, &max732x_irq_chip,
442 handle_edge_irq);
443 set_irq_nested_thread(irq, 1);
444 #ifdef CONFIG_ARM
445 set_irq_flags(irq, IRQF_VALID);
446 #else
447 set_irq_noprobe(irq);
448 #endif
451 ret = request_threaded_irq(client->irq,
452 NULL,
453 max732x_irq_handler,
454 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
455 dev_name(&client->dev), chip);
456 if (ret) {
457 dev_err(&client->dev, "failed to request irq %d\n",
458 client->irq);
459 goto out_failed;
462 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
465 return 0;
467 out_failed:
468 chip->irq_base = 0;
469 return ret;
472 static void max732x_irq_teardown(struct max732x_chip *chip)
474 if (chip->irq_base)
475 free_irq(chip->client->irq, chip);
477 #else /* CONFIG_GPIO_MAX732X_IRQ */
478 static int max732x_irq_setup(struct max732x_chip *chip,
479 const struct i2c_device_id *id)
481 struct i2c_client *client = chip->client;
482 struct max732x_platform_data *pdata = client->dev.platform_data;
483 int has_irq = max732x_features[id->driver_data] >> 32;
485 if (pdata->irq_base && has_irq != INT_NONE)
486 dev_warn(&client->dev, "interrupt support not compiled in\n");
488 return 0;
491 static void max732x_irq_teardown(struct max732x_chip *chip)
494 #endif
496 static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
497 const struct i2c_device_id *id,
498 unsigned gpio_start)
500 struct gpio_chip *gc = &chip->gpio_chip;
501 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
502 int i, port = 0;
504 for (i = 0; i < 16; i++, id_data >>= 2) {
505 unsigned int mask = 1 << port;
507 switch (id_data & 0x3) {
508 case PORT_OUTPUT:
509 chip->dir_output |= mask;
510 break;
511 case PORT_INPUT:
512 chip->dir_input |= mask;
513 break;
514 case PORT_OPENDRAIN:
515 chip->dir_output |= mask;
516 chip->dir_input |= mask;
517 break;
518 default:
519 continue;
522 if (i < 8)
523 chip->mask_group_a |= mask;
524 port++;
527 if (chip->dir_input)
528 gc->direction_input = max732x_gpio_direction_input;
529 if (chip->dir_output) {
530 gc->direction_output = max732x_gpio_direction_output;
531 gc->set = max732x_gpio_set_value;
533 gc->get = max732x_gpio_get_value;
534 gc->can_sleep = 1;
536 gc->base = gpio_start;
537 gc->ngpio = port;
538 gc->label = chip->client->name;
539 gc->owner = THIS_MODULE;
541 return port;
544 static int __devinit max732x_probe(struct i2c_client *client,
545 const struct i2c_device_id *id)
547 struct max732x_platform_data *pdata;
548 struct max732x_chip *chip;
549 struct i2c_client *c;
550 uint16_t addr_a, addr_b;
551 int ret, nr_port;
553 pdata = client->dev.platform_data;
554 if (pdata == NULL) {
555 dev_dbg(&client->dev, "no platform data\n");
556 return -EINVAL;
559 chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
560 if (chip == NULL)
561 return -ENOMEM;
562 chip->client = client;
564 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
566 addr_a = (client->addr & 0x0f) | 0x60;
567 addr_b = (client->addr & 0x0f) | 0x50;
569 switch (client->addr & 0x70) {
570 case 0x60:
571 chip->client_group_a = client;
572 if (nr_port > 8) {
573 c = i2c_new_dummy(client->adapter, addr_b);
574 chip->client_group_b = chip->client_dummy = c;
576 break;
577 case 0x50:
578 chip->client_group_b = client;
579 if (nr_port > 8) {
580 c = i2c_new_dummy(client->adapter, addr_a);
581 chip->client_group_a = chip->client_dummy = c;
583 break;
584 default:
585 dev_err(&client->dev, "invalid I2C address specified %02x\n",
586 client->addr);
587 ret = -EINVAL;
588 goto out_failed;
591 mutex_init(&chip->lock);
593 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
594 if (nr_port > 8)
595 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
597 ret = max732x_irq_setup(chip, id);
598 if (ret)
599 goto out_failed;
601 ret = gpiochip_add(&chip->gpio_chip);
602 if (ret)
603 goto out_failed;
605 if (pdata->setup) {
606 ret = pdata->setup(client, chip->gpio_chip.base,
607 chip->gpio_chip.ngpio, pdata->context);
608 if (ret < 0)
609 dev_warn(&client->dev, "setup failed, %d\n", ret);
612 i2c_set_clientdata(client, chip);
613 return 0;
615 out_failed:
616 max732x_irq_teardown(chip);
617 kfree(chip);
618 return ret;
621 static int __devexit max732x_remove(struct i2c_client *client)
623 struct max732x_platform_data *pdata = client->dev.platform_data;
624 struct max732x_chip *chip = i2c_get_clientdata(client);
625 int ret;
627 if (pdata->teardown) {
628 ret = pdata->teardown(client, chip->gpio_chip.base,
629 chip->gpio_chip.ngpio, pdata->context);
630 if (ret < 0) {
631 dev_err(&client->dev, "%s failed, %d\n",
632 "teardown", ret);
633 return ret;
637 ret = gpiochip_remove(&chip->gpio_chip);
638 if (ret) {
639 dev_err(&client->dev, "%s failed, %d\n",
640 "gpiochip_remove()", ret);
641 return ret;
644 max732x_irq_teardown(chip);
646 /* unregister any dummy i2c_client */
647 if (chip->client_dummy)
648 i2c_unregister_device(chip->client_dummy);
650 kfree(chip);
651 return 0;
654 static struct i2c_driver max732x_driver = {
655 .driver = {
656 .name = "max732x",
657 .owner = THIS_MODULE,
659 .probe = max732x_probe,
660 .remove = __devexit_p(max732x_remove),
661 .id_table = max732x_id,
664 static int __init max732x_init(void)
666 return i2c_add_driver(&max732x_driver);
668 /* register after i2c postcore initcall and before
669 * subsys initcalls that may rely on these GPIOs
671 subsys_initcall(max732x_init);
673 static void __exit max732x_exit(void)
675 i2c_del_driver(&max732x_driver);
677 module_exit(max732x_exit);
679 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
680 MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
681 MODULE_LICENSE("GPL");