gpio: add interrupt handling capability to max732x
[safe/jmp/linux-2.6] / drivers / gpio / max732x.c
1 /*
2  *  max732x.c - I2C Port Expander with 8/16 I/O
3  *
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>
7  *
8  *  Derived from drivers/gpio/pca953x.c
9  *
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.
13  */
14
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>
24
25
26 /*
27  * Each port of MAX732x (including MAX7319) falls into one of the
28  * following three types:
29  *
30  *   - Push Pull Output
31  *   - Input
32  *   - Open Drain I/O
33  *
34  * designated by 'O', 'I' and 'P' individually according to MAXIM's
35  * datasheets. 'I' and 'P' ports are interrupt capables, some with
36  * a dedicated interrupt mask.
37  *
38  * There are two groups of I/O ports, each group usually includes
39  * up to 8 I/O ports, and is accessed by a specific I2C address:
40  *
41  *   - Group A : by I2C address 0b'110xxxx
42  *   - Group B : by I2C address 0b'101xxxx
43  *
44  * where 'xxxx' is decided by the connections of pin AD2/AD0.  The
45  * address used also affects the initial state of output signals.
46  *
47  * Within each group of ports, there are five known combinations of
48  * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
49  * the detailed organization of these ports. Only Goup A is interrupt
50  * capable.
51  *
52  * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
53  * and GPIOs from GROUP_A are numbered before those from GROUP_B
54  * (if there are two groups).
55  *
56  * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
57  * they are not supported by this driver.
58  */
59
60 #define PORT_NONE       0x0     /* '/' No Port */
61 #define PORT_OUTPUT     0x1     /* 'O' Push-Pull, Output Only */
62 #define PORT_INPUT      0x2     /* 'I' Input Only */
63 #define PORT_OPENDRAIN  0x3     /* 'P' Open-Drain, I/O */
64
65 #define IO_4I4O         0x5AA5  /* O7 O6 I5 I4 I3 I2 O1 O0 */
66 #define IO_4P4O         0x5FF5  /* O7 O6 P5 P4 P3 P2 O1 O0 */
67 #define IO_8I           0xAAAA  /* I7 I6 I5 I4 I3 I2 I1 I0 */
68 #define IO_8P           0xFFFF  /* P7 P6 P5 P4 P3 P2 P1 P0 */
69 #define IO_8O           0x5555  /* O7 O6 O5 O4 O3 O2 O1 O0 */
70
71 #define GROUP_A(x)      ((x) & 0xffff)  /* I2C Addr: 0b'110xxxx */
72 #define GROUP_B(x)      ((x) << 16)     /* I2C Addr: 0b'101xxxx */
73
74 #define INT_NONE        0x0     /* No interrupt capability */
75 #define INT_NO_MASK     0x1     /* Has interrupts, no mask */
76 #define INT_INDEP_MASK  0x2     /* Has interrupts, independent mask */
77 #define INT_MERGED_MASK 0x3     /* Has interrupts, merged mask */
78
79 #define INT_CAPS(x)     (((uint64_t)(x)) << 32)
80
81 enum {
82         MAX7319,
83         MAX7320,
84         MAX7321,
85         MAX7322,
86         MAX7323,
87         MAX7324,
88         MAX7325,
89         MAX7326,
90         MAX7327,
91 };
92
93 static uint64_t max732x_features[] = {
94         [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
95         [MAX7320] = GROUP_B(IO_8O),
96         [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
97         [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
98         [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
99         [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
100         [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
101         [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
102         [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
103 };
104
105 static const struct i2c_device_id max732x_id[] = {
106         { "max7319", MAX7319 },
107         { "max7320", MAX7320 },
108         { "max7321", MAX7321 },
109         { "max7322", MAX7322 },
110         { "max7323", MAX7323 },
111         { "max7324", MAX7324 },
112         { "max7325", MAX7325 },
113         { "max7326", MAX7326 },
114         { "max7327", MAX7327 },
115         { },
116 };
117 MODULE_DEVICE_TABLE(i2c, max732x_id);
118
119 struct max732x_chip {
120         struct gpio_chip gpio_chip;
121
122         struct i2c_client *client;      /* "main" client */
123         struct i2c_client *client_dummy;
124         struct i2c_client *client_group_a;
125         struct i2c_client *client_group_b;
126
127         unsigned int    mask_group_a;
128         unsigned int    dir_input;
129         unsigned int    dir_output;
130
131         struct mutex    lock;
132         uint8_t         reg_out[2];
133
134 #ifdef CONFIG_GPIO_MAX732X_IRQ
135         struct mutex    irq_lock;
136         int             irq_base;
137         uint8_t         irq_mask;
138         uint8_t         irq_mask_cur;
139         uint8_t         irq_trig_raise;
140         uint8_t         irq_trig_fall;
141         uint8_t         irq_features;
142 #endif
143 };
144
145 static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
146 {
147         struct i2c_client *client;
148         int ret;
149
150         client = group_a ? chip->client_group_a : chip->client_group_b;
151         ret = i2c_smbus_write_byte(client, val);
152         if (ret < 0) {
153                 dev_err(&client->dev, "failed writing\n");
154                 return ret;
155         }
156
157         return 0;
158 }
159
160 static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
161 {
162         struct i2c_client *client;
163         int ret;
164
165         client = group_a ? chip->client_group_a : chip->client_group_b;
166         ret = i2c_smbus_read_byte(client);
167         if (ret < 0) {
168                 dev_err(&client->dev, "failed reading\n");
169                 return ret;
170         }
171
172         *val = (uint8_t)ret;
173         return 0;
174 }
175
176 static inline int is_group_a(struct max732x_chip *chip, unsigned off)
177 {
178         return (1u << off) & chip->mask_group_a;
179 }
180
181 static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
182 {
183         struct max732x_chip *chip;
184         uint8_t reg_val;
185         int ret;
186
187         chip = container_of(gc, struct max732x_chip, gpio_chip);
188
189         ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
190         if (ret < 0)
191                 return 0;
192
193         return reg_val & (1u << (off & 0x7));
194 }
195
196 static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
197 {
198         struct max732x_chip *chip;
199         uint8_t reg_out, mask = 1u << (off & 0x7);
200         int ret;
201
202         chip = container_of(gc, struct max732x_chip, gpio_chip);
203
204         mutex_lock(&chip->lock);
205
206         reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
207         reg_out = (val) ? reg_out | mask : reg_out & ~mask;
208
209         ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
210         if (ret < 0)
211                 goto out;
212
213         /* update the shadow register then */
214         if (off > 7)
215                 chip->reg_out[1] = reg_out;
216         else
217                 chip->reg_out[0] = reg_out;
218 out:
219         mutex_unlock(&chip->lock);
220 }
221
222 static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
223 {
224         struct max732x_chip *chip;
225         unsigned int mask = 1u << off;
226
227         chip = container_of(gc, struct max732x_chip, gpio_chip);
228
229         if ((mask & chip->dir_input) == 0) {
230                 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
231                         chip->client->name, off);
232                 return -EACCES;
233         }
234
235         return 0;
236 }
237
238 static int max732x_gpio_direction_output(struct gpio_chip *gc,
239                 unsigned off, int val)
240 {
241         struct max732x_chip *chip;
242         unsigned int mask = 1u << off;
243
244         chip = container_of(gc, struct max732x_chip, gpio_chip);
245
246         if ((mask & chip->dir_output) == 0) {
247                 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
248                         chip->client->name, off);
249                 return -EACCES;
250         }
251
252         max732x_gpio_set_value(gc, off, val);
253         return 0;
254 }
255
256 #ifdef CONFIG_GPIO_MAX732X_IRQ
257 static int max732x_writew(struct max732x_chip *chip, uint16_t val)
258 {
259         int ret;
260
261         val = cpu_to_le16(val);
262
263         ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
264         if (ret < 0) {
265                 dev_err(&chip->client_group_a->dev, "failed writing\n");
266                 return ret;
267         }
268
269         return 0;
270 }
271
272 static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
273 {
274         int ret;
275
276         ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
277         if (ret < 0) {
278                 dev_err(&chip->client_group_a->dev, "failed reading\n");
279                 return ret;
280         }
281
282         *val = le16_to_cpu(*val);
283         return 0;
284 }
285
286 static void max732x_irq_update_mask(struct max732x_chip *chip)
287 {
288         uint16_t msg;
289
290         if (chip->irq_mask == chip->irq_mask_cur)
291                 return;
292
293         chip->irq_mask = chip->irq_mask_cur;
294
295         if (chip->irq_features == INT_NO_MASK)
296                 return;
297
298         mutex_lock(&chip->lock);
299
300         switch (chip->irq_features) {
301         case INT_INDEP_MASK:
302                 msg = (chip->irq_mask << 8) | chip->reg_out[0];
303                 max732x_writew(chip, msg);
304                 break;
305
306         case INT_MERGED_MASK:
307                 msg = chip->irq_mask | chip->reg_out[0];
308                 max732x_writeb(chip, 1, (uint8_t)msg);
309                 break;
310         }
311
312         mutex_unlock(&chip->lock);
313 }
314
315 static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
316 {
317         struct max732x_chip *chip;
318
319         chip = container_of(gc, struct max732x_chip, gpio_chip);
320         return chip->irq_base + off;
321 }
322
323 static void max732x_irq_mask(unsigned int irq)
324 {
325         struct max732x_chip *chip = get_irq_chip_data(irq);
326
327         chip->irq_mask_cur &= ~(1 << (irq - chip->irq_base));
328 }
329
330 static void max732x_irq_unmask(unsigned int irq)
331 {
332         struct max732x_chip *chip = get_irq_chip_data(irq);
333
334         chip->irq_mask_cur |= 1 << (irq - chip->irq_base);
335 }
336
337 static void max732x_irq_bus_lock(unsigned int irq)
338 {
339         struct max732x_chip *chip = get_irq_chip_data(irq);
340
341         mutex_lock(&chip->irq_lock);
342         chip->irq_mask_cur = chip->irq_mask;
343 }
344
345 static void max732x_irq_bus_sync_unlock(unsigned int irq)
346 {
347         struct max732x_chip *chip = get_irq_chip_data(irq);
348
349         max732x_irq_update_mask(chip);
350         mutex_unlock(&chip->irq_lock);
351 }
352
353 static int max732x_irq_set_type(unsigned int irq, unsigned int type)
354 {
355         struct max732x_chip *chip = get_irq_chip_data(irq);
356         uint16_t off = irq - chip->irq_base;
357         uint16_t mask = 1 << off;
358
359         if (!(mask & chip->dir_input)) {
360                 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
361                         chip->client->name, off);
362                 return -EACCES;
363         }
364
365         if (!(type & IRQ_TYPE_EDGE_BOTH)) {
366                 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
367                         irq, type);
368                 return -EINVAL;
369         }
370
371         if (type & IRQ_TYPE_EDGE_FALLING)
372                 chip->irq_trig_fall |= mask;
373         else
374                 chip->irq_trig_fall &= ~mask;
375
376         if (type & IRQ_TYPE_EDGE_RISING)
377                 chip->irq_trig_raise |= mask;
378         else
379                 chip->irq_trig_raise &= ~mask;
380
381         return max732x_gpio_direction_input(&chip->gpio_chip, off);
382 }
383
384 static struct irq_chip max732x_irq_chip = {
385         .name                   = "max732x",
386         .mask                   = max732x_irq_mask,
387         .unmask                 = max732x_irq_unmask,
388         .bus_lock               = max732x_irq_bus_lock,
389         .bus_sync_unlock        = max732x_irq_bus_sync_unlock,
390         .set_type               = max732x_irq_set_type,
391 };
392
393 static uint8_t max732x_irq_pending(struct max732x_chip *chip)
394 {
395         uint8_t cur_stat;
396         uint8_t old_stat;
397         uint8_t trigger;
398         uint8_t pending;
399         uint16_t status;
400         int ret;
401
402         ret = max732x_readw(chip, &status);
403         if (ret)
404                 return 0;
405
406         trigger = status >> 8;
407         trigger &= chip->irq_mask;
408
409         if (!trigger)
410                 return 0;
411
412         cur_stat = status & 0xFF;
413         cur_stat &= chip->irq_mask;
414
415         old_stat = cur_stat ^ trigger;
416
417         pending = (old_stat & chip->irq_trig_fall) |
418                   (cur_stat & chip->irq_trig_raise);
419         pending &= trigger;
420
421         return pending;
422 }
423
424 static irqreturn_t max732x_irq_handler(int irq, void *devid)
425 {
426         struct max732x_chip *chip = devid;
427         uint8_t pending;
428         uint8_t level;
429
430         pending = max732x_irq_pending(chip);
431
432         if (!pending)
433                 return IRQ_HANDLED;
434
435         do {
436                 level = __ffs(pending);
437                 handle_nested_irq(level + chip->irq_base);
438
439                 pending &= ~(1 << level);
440         } while (pending);
441
442         return IRQ_HANDLED;
443 }
444
445 static int max732x_irq_setup(struct max732x_chip *chip,
446                              const struct i2c_device_id *id)
447 {
448         struct i2c_client *client = chip->client;
449         struct max732x_platform_data *pdata = client->dev.platform_data;
450         int has_irq = max732x_features[id->driver_data] >> 32;
451         int ret;
452
453         if (pdata->irq_base && has_irq != INT_NONE) {
454                 int lvl;
455
456                 chip->irq_base = pdata->irq_base;
457                 chip->irq_features = has_irq;
458                 mutex_init(&chip->irq_lock);
459
460                 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
461                         int irq = lvl + chip->irq_base;
462
463                         if (!(chip->dir_input & (1 << lvl)))
464                                 continue;
465
466                         set_irq_chip_data(irq, chip);
467                         set_irq_chip_and_handler(irq, &max732x_irq_chip,
468                                                  handle_edge_irq);
469                         set_irq_nested_thread(irq, 1);
470 #ifdef CONFIG_ARM
471                         set_irq_flags(irq, IRQF_VALID);
472 #else
473                         set_irq_noprobe(irq);
474 #endif
475                 }
476
477                 ret = request_threaded_irq(client->irq,
478                                            NULL,
479                                            max732x_irq_handler,
480                                            IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
481                                            dev_name(&client->dev), chip);
482                 if (ret) {
483                         dev_err(&client->dev, "failed to request irq %d\n",
484                                 client->irq);
485                         goto out_failed;
486                 }
487
488                 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
489         }
490
491         return 0;
492
493 out_failed:
494         chip->irq_base = 0;
495         return ret;
496 }
497
498 static void max732x_irq_teardown(struct max732x_chip *chip)
499 {
500         if (chip->irq_base)
501                 free_irq(chip->client->irq, chip);
502 }
503 #else /* CONFIG_GPIO_MAX732X_IRQ */
504 static int max732x_irq_setup(struct max732x_chip *chip,
505                              const struct i2c_device_id *id)
506 {
507         struct i2c_client *client = chip->client;
508         struct max732x_platform_data *pdata = client->dev.platform_data;
509         int has_irq = max732x_features[id->driver_data] >> 32;
510
511         if (pdata->irq_base && has_irq != INT_NONE)
512                 dev_warn(&client->dev, "interrupt support not compiled in\n");
513
514         return 0;
515 }
516
517 static void max732x_irq_teardown(struct max732x_chip *chip)
518 {
519 }
520 #endif
521
522 static int __devinit max732x_setup_gpio(struct max732x_chip *chip,
523                                         const struct i2c_device_id *id,
524                                         unsigned gpio_start)
525 {
526         struct gpio_chip *gc = &chip->gpio_chip;
527         uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
528         int i, port = 0;
529
530         for (i = 0; i < 16; i++, id_data >>= 2) {
531                 unsigned int mask = 1 << port;
532
533                 switch (id_data & 0x3) {
534                 case PORT_OUTPUT:
535                         chip->dir_output |= mask;
536                         break;
537                 case PORT_INPUT:
538                         chip->dir_input |= mask;
539                         break;
540                 case PORT_OPENDRAIN:
541                         chip->dir_output |= mask;
542                         chip->dir_input |= mask;
543                         break;
544                 default:
545                         continue;
546                 }
547
548                 if (i < 8)
549                         chip->mask_group_a |= mask;
550                 port++;
551         }
552
553         if (chip->dir_input)
554                 gc->direction_input = max732x_gpio_direction_input;
555         if (chip->dir_output) {
556                 gc->direction_output = max732x_gpio_direction_output;
557                 gc->set = max732x_gpio_set_value;
558         }
559         gc->get = max732x_gpio_get_value;
560         gc->can_sleep = 1;
561
562         gc->base = gpio_start;
563         gc->ngpio = port;
564         gc->label = chip->client->name;
565         gc->owner = THIS_MODULE;
566
567         return port;
568 }
569
570 static int __devinit max732x_probe(struct i2c_client *client,
571                                    const struct i2c_device_id *id)
572 {
573         struct max732x_platform_data *pdata;
574         struct max732x_chip *chip;
575         struct i2c_client *c;
576         uint16_t addr_a, addr_b;
577         int ret, nr_port;
578
579         pdata = client->dev.platform_data;
580         if (pdata == NULL) {
581                 dev_dbg(&client->dev, "no platform data\n");
582                 return -EINVAL;
583         }
584
585         chip = kzalloc(sizeof(struct max732x_chip), GFP_KERNEL);
586         if (chip == NULL)
587                 return -ENOMEM;
588         chip->client = client;
589
590         nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
591
592         addr_a = (client->addr & 0x0f) | 0x60;
593         addr_b = (client->addr & 0x0f) | 0x50;
594
595         switch (client->addr & 0x70) {
596         case 0x60:
597                 chip->client_group_a = client;
598                 if (nr_port > 7) {
599                         c = i2c_new_dummy(client->adapter, addr_b);
600                         chip->client_group_b = chip->client_dummy = c;
601                 }
602                 break;
603         case 0x50:
604                 chip->client_group_b = client;
605                 if (nr_port > 7) {
606                         c = i2c_new_dummy(client->adapter, addr_a);
607                         chip->client_group_a = chip->client_dummy = c;
608                 }
609                 break;
610         default:
611                 dev_err(&client->dev, "invalid I2C address specified %02x\n",
612                                 client->addr);
613                 ret = -EINVAL;
614                 goto out_failed;
615         }
616
617         mutex_init(&chip->lock);
618
619         max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
620         if (nr_port > 7)
621                 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
622
623         ret = max732x_irq_setup(chip, id);
624         if (ret)
625                 goto out_failed;
626
627         ret = gpiochip_add(&chip->gpio_chip);
628         if (ret)
629                 goto out_failed;
630
631         if (pdata->setup) {
632                 ret = pdata->setup(client, chip->gpio_chip.base,
633                                 chip->gpio_chip.ngpio, pdata->context);
634                 if (ret < 0)
635                         dev_warn(&client->dev, "setup failed, %d\n", ret);
636         }
637
638         i2c_set_clientdata(client, chip);
639         return 0;
640
641 out_failed:
642         max732x_irq_teardown(chip);
643         kfree(chip);
644         return ret;
645 }
646
647 static int __devexit max732x_remove(struct i2c_client *client)
648 {
649         struct max732x_platform_data *pdata = client->dev.platform_data;
650         struct max732x_chip *chip = i2c_get_clientdata(client);
651         int ret;
652
653         if (pdata->teardown) {
654                 ret = pdata->teardown(client, chip->gpio_chip.base,
655                                 chip->gpio_chip.ngpio, pdata->context);
656                 if (ret < 0) {
657                         dev_err(&client->dev, "%s failed, %d\n",
658                                         "teardown", ret);
659                         return ret;
660                 }
661         }
662
663         ret = gpiochip_remove(&chip->gpio_chip);
664         if (ret) {
665                 dev_err(&client->dev, "%s failed, %d\n",
666                                 "gpiochip_remove()", ret);
667                 return ret;
668         }
669
670         max732x_irq_teardown(chip);
671
672         /* unregister any dummy i2c_client */
673         if (chip->client_dummy)
674                 i2c_unregister_device(chip->client_dummy);
675
676         kfree(chip);
677         return 0;
678 }
679
680 static struct i2c_driver max732x_driver = {
681         .driver = {
682                 .name   = "max732x",
683                 .owner  = THIS_MODULE,
684         },
685         .probe          = max732x_probe,
686         .remove         = __devexit_p(max732x_remove),
687         .id_table       = max732x_id,
688 };
689
690 static int __init max732x_init(void)
691 {
692         return i2c_add_driver(&max732x_driver);
693 }
694 /* register after i2c postcore initcall and before
695  * subsys initcalls that may rely on these GPIOs
696  */
697 subsys_initcall(max732x_init);
698
699 static void __exit max732x_exit(void)
700 {
701         i2c_del_driver(&max732x_driver);
702 }
703 module_exit(max732x_exit);
704
705 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
706 MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
707 MODULE_LICENSE("GPL");