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)
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
},
84 MODULE_DEVICE_TABLE(i2c
, max732x_id
);
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
;
101 #ifdef CONFIG_GPIO_MAX732X_IRQ
102 struct mutex irq_lock
;
105 uint8_t irq_mask_cur
;
106 uint8_t irq_trig_raise
;
107 uint8_t irq_trig_fall
;
108 uint8_t irq_features
;
112 static int max732x_writeb(struct max732x_chip
*chip
, int group_a
, uint8_t val
)
114 struct i2c_client
*client
;
117 client
= group_a
? chip
->client_group_a
: chip
->client_group_b
;
118 ret
= i2c_smbus_write_byte(client
, val
);
120 dev_err(&client
->dev
, "failed writing\n");
127 static int max732x_readb(struct max732x_chip
*chip
, int group_a
, uint8_t *val
)
129 struct i2c_client
*client
;
132 client
= group_a
? chip
->client_group_a
: chip
->client_group_b
;
133 ret
= i2c_smbus_read_byte(client
);
135 dev_err(&client
->dev
, "failed reading\n");
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
;
154 chip
= container_of(gc
, struct max732x_chip
, gpio_chip
);
156 ret
= max732x_readb(chip
, is_group_a(chip
, off
), ®_val
);
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);
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
);
180 /* update the shadow register then */
182 chip
->reg_out
[1] = reg_out
;
184 chip
->reg_out
[0] = reg_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
);
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);
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
);
226 max732x_gpio_set_value(gc
, off
, val
);
230 #ifdef CONFIG_GPIO_MAX732X_IRQ
231 static int max732x_writew(struct max732x_chip
*chip
, uint16_t val
)
235 val
= cpu_to_le16(val
);
237 ret
= i2c_master_send(chip
->client_group_a
, (char *)&val
, 2);
239 dev_err(&chip
->client_group_a
->dev
, "failed writing\n");
246 static int max732x_readw(struct max732x_chip
*chip
, uint16_t *val
)
250 ret
= i2c_master_recv(chip
->client_group_a
, (char *)val
, 2);
252 dev_err(&chip
->client_group_a
->dev
, "failed reading\n");
256 *val
= le16_to_cpu(*val
);
260 static void max732x_irq_update_mask(struct max732x_chip
*chip
)
264 if (chip
->irq_mask
== chip
->irq_mask_cur
)
267 chip
->irq_mask
= chip
->irq_mask_cur
;
269 if (chip
->irq_features
== INT_NO_MASK
)
272 mutex_lock(&chip
->lock
);
274 switch (chip
->irq_features
) {
276 msg
= (chip
->irq_mask
<< 8) | chip
->reg_out
[0];
277 max732x_writew(chip
, msg
);
280 case INT_MERGED_MASK
:
281 msg
= chip
->irq_mask
| chip
->reg_out
[0];
282 max732x_writeb(chip
, 1, (uint8_t)msg
);
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
);
339 if (!(type
& IRQ_TYPE_EDGE_BOTH
)) {
340 dev_err(&chip
->client
->dev
, "irq %d: unsupported type %d\n",
345 if (type
& IRQ_TYPE_EDGE_FALLING
)
346 chip
->irq_trig_fall
|= mask
;
348 chip
->irq_trig_fall
&= ~mask
;
350 if (type
& IRQ_TYPE_EDGE_RISING
)
351 chip
->irq_trig_raise
|= mask
;
353 chip
->irq_trig_raise
&= ~mask
;
355 return max732x_gpio_direction_input(&chip
->gpio_chip
, off
);
358 static struct irq_chip max732x_irq_chip
= {
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
)
376 ret
= max732x_readw(chip
, &status
);
380 trigger
= status
>> 8;
381 trigger
&= chip
->irq_mask
;
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
);
398 static irqreturn_t
max732x_irq_handler(int irq
, void *devid
)
400 struct max732x_chip
*chip
= devid
;
404 pending
= max732x_irq_pending(chip
);
410 level
= __ffs(pending
);
411 handle_nested_irq(level
+ chip
->irq_base
);
413 pending
&= ~(1 << level
);
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;
427 if (pdata
->irq_base
&& has_irq
!= INT_NONE
) {
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
)))
440 set_irq_chip_data(irq
, chip
);
441 set_irq_chip_and_handler(irq
, &max732x_irq_chip
,
443 set_irq_nested_thread(irq
, 1);
445 set_irq_flags(irq
, IRQF_VALID
);
447 set_irq_noprobe(irq
);
451 ret
= request_threaded_irq(client
->irq
,
454 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
455 dev_name(&client
->dev
), chip
);
457 dev_err(&client
->dev
, "failed to request irq %d\n",
462 chip
->gpio_chip
.to_irq
= max732x_gpio_to_irq
;
472 static void max732x_irq_teardown(struct max732x_chip
*chip
)
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");
491 static void max732x_irq_teardown(struct max732x_chip
*chip
)
496 static int __devinit
max732x_setup_gpio(struct max732x_chip
*chip
,
497 const struct i2c_device_id
*id
,
500 struct gpio_chip
*gc
= &chip
->gpio_chip
;
501 uint32_t id_data
= (uint32_t)max732x_features
[id
->driver_data
];
504 for (i
= 0; i
< 16; i
++, id_data
>>= 2) {
505 unsigned int mask
= 1 << port
;
507 switch (id_data
& 0x3) {
509 chip
->dir_output
|= mask
;
512 chip
->dir_input
|= mask
;
515 chip
->dir_output
|= mask
;
516 chip
->dir_input
|= mask
;
523 chip
->mask_group_a
|= mask
;
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
;
536 gc
->base
= gpio_start
;
538 gc
->label
= chip
->client
->name
;
539 gc
->owner
= THIS_MODULE
;
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
;
553 pdata
= client
->dev
.platform_data
;
555 dev_dbg(&client
->dev
, "no platform data\n");
559 chip
= kzalloc(sizeof(struct max732x_chip
), GFP_KERNEL
);
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) {
571 chip
->client_group_a
= client
;
573 c
= i2c_new_dummy(client
->adapter
, addr_b
);
574 chip
->client_group_b
= chip
->client_dummy
= c
;
578 chip
->client_group_b
= client
;
580 c
= i2c_new_dummy(client
->adapter
, addr_a
);
581 chip
->client_group_a
= chip
->client_dummy
= c
;
585 dev_err(&client
->dev
, "invalid I2C address specified %02x\n",
591 mutex_init(&chip
->lock
);
593 max732x_readb(chip
, is_group_a(chip
, 0), &chip
->reg_out
[0]);
595 max732x_readb(chip
, is_group_a(chip
, 8), &chip
->reg_out
[1]);
597 ret
= max732x_irq_setup(chip
, id
);
601 ret
= gpiochip_add(&chip
->gpio_chip
);
606 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
607 chip
->gpio_chip
.ngpio
, pdata
->context
);
609 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
612 i2c_set_clientdata(client
, chip
);
616 max732x_irq_teardown(chip
);
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
);
627 if (pdata
->teardown
) {
628 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
629 chip
->gpio_chip
.ngpio
, pdata
->context
);
631 dev_err(&client
->dev
, "%s failed, %d\n",
637 ret
= gpiochip_remove(&chip
->gpio_chip
);
639 dev_err(&client
->dev
, "%s failed, %d\n",
640 "gpiochip_remove()", ret
);
644 max732x_irq_teardown(chip
);
646 /* unregister any dummy i2c_client */
647 if (chip
->client_dummy
)
648 i2c_unregister_device(chip
->client_dummy
);
654 static struct i2c_driver max732x_driver
= {
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");