15 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
22 #include <linux/i2c.h>
61 #define PORT_OUTPUT 0x1
62 #define PORT_INPUT 0x2
63 #define PORT_OPENDRAIN 0x3
65 #define IO_4I4O 0x5AA5
66 #define IO_4P4O 0x5FF5
71 #define GROUP_A(x) ((x) & 0xffff)
72 #define GROUP_B(x) ((x) << 16)
75 #define INT_NO_MASK 0x1
76 #define INT_INDEP_MASK 0x2
77 #define INT_MERGED_MASK 0x3
79 #define INT_CAPS(x) (((uint64_t)(x)) << 32)
93 static uint64_t max732x_features[] = {
134 #ifdef CONFIG_GPIO_MAX732X_IRQ
176 static inline int is_group_a(
struct max732x_chip *chip,
unsigned off)
181 static int max732x_gpio_get_value(
struct gpio_chip *
gc,
unsigned off)
189 ret = max732x_readb(chip, is_group_a(chip, off), ®_val);
193 return reg_val & (1
u << (off & 0x7));
196 static void max732x_gpio_set_value(
struct gpio_chip *gc,
unsigned off,
int val)
207 reg_out = (val) ? reg_out | mask : reg_out & ~mask;
209 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
222 static int max732x_gpio_direction_input(
struct gpio_chip *gc,
unsigned off)
225 unsigned int mask = 1
u << off;
240 max732x_gpio_set_value(gc, off, 1);
245 static int max732x_gpio_direction_output(
struct gpio_chip *gc,
246 unsigned off,
int val)
249 unsigned int mask = 1
u << off;
259 max732x_gpio_set_value(gc, off, val);
263 #ifdef CONFIG_GPIO_MAX732X_IRQ
293 static void max732x_irq_update_mask(
struct max732x_chip *chip)
297 if (chip->irq_mask == chip->irq_mask_cur)
300 chip->irq_mask = chip->irq_mask_cur;
307 switch (chip->irq_features) {
309 msg = (chip->irq_mask << 8) | chip->
reg_out[0];
310 max732x_writew(chip, msg);
314 msg = chip->irq_mask | chip->
reg_out[0];
315 max732x_writeb(chip, 1, (
uint8_t)msg);
322 static int max732x_gpio_to_irq(
struct gpio_chip *gc,
unsigned off)
327 return chip->irq_base + off;
330 static void max732x_irq_mask(
struct irq_data *
d)
332 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
334 chip->irq_mask_cur &= ~(1 << (d->
irq - chip->irq_base));
337 static void max732x_irq_unmask(
struct irq_data *
d)
339 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
341 chip->irq_mask_cur |= 1 << (d->
irq - chip->irq_base);
344 static void max732x_irq_bus_lock(
struct irq_data *
d)
346 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
349 chip->irq_mask_cur = chip->irq_mask;
352 static void max732x_irq_bus_sync_unlock(
struct irq_data *
d)
354 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
356 max732x_irq_update_mask(chip);
360 static int max732x_irq_set_type(
struct irq_data *
d,
unsigned int type)
362 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
379 chip->irq_trig_fall |=
mask;
381 chip->irq_trig_fall &= ~mask;
384 chip->irq_trig_raise |=
mask;
386 chip->irq_trig_raise &= ~mask;
388 return max732x_gpio_direction_input(&chip->
gpio_chip, off);
391 static struct irq_chip max732x_irq_chip = {
393 .irq_mask = max732x_irq_mask,
394 .irq_unmask = max732x_irq_unmask,
395 .irq_bus_lock = max732x_irq_bus_lock,
396 .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
397 .irq_set_type = max732x_irq_set_type,
409 ret = max732x_readw(chip, &status);
413 trigger = status >> 8;
414 trigger &= chip->irq_mask;
419 cur_stat = status & 0xFF;
420 cur_stat &= chip->irq_mask;
424 pending = (old_stat & chip->irq_trig_fall) |
425 (cur_stat & chip->irq_trig_raise);
437 pending = max732x_irq_pending(chip);
443 level =
__ffs(pending);
446 pending &= ~(1 <<
level);
457 int has_irq = max732x_features[
id->driver_data] >> 32;
464 chip->irq_features = has_irq;
467 for (lvl = 0; lvl < chip->
gpio_chip.ngpio; lvl++) {
468 int irq = lvl + chip->irq_base;
474 irq_set_chip_and_handler(irq, &max732x_irq_chip,
476 irq_set_nested_thread(irq, 1);
480 irq_set_noprobe(irq);
488 dev_name(&client->
dev), chip);
490 dev_err(&client->
dev,
"failed to request irq %d\n",
495 chip->
gpio_chip.to_irq = max732x_gpio_to_irq;
505 static void max732x_irq_teardown(
struct max732x_chip *chip)
516 int has_irq = max732x_features[
id->driver_data] >> 32;
519 dev_warn(&client->
dev,
"interrupt support not compiled in\n");
524 static void max732x_irq_teardown(
struct max732x_chip *chip)
537 for (i = 0; i < 16; i++, id_data >>= 2) {
538 unsigned int mask = 1 <<
port;
540 switch (id_data & 0x3) {
561 gc->direction_input = max732x_gpio_direction_input;
563 gc->direction_output = max732x_gpio_direction_output;
564 gc->set = max732x_gpio_set_value;
566 gc->get = max732x_gpio_get_value;
569 gc->base = gpio_start;
571 gc->label = chip->
client->name;
586 pdata = client->
dev.platform_data;
597 nr_port = max732x_setup_gpio(chip,
id, pdata->
gpio_base);
599 addr_a = (client->
addr & 0x0f) | 0x60;
600 addr_b = (client->
addr & 0x0f) | 0x50;
602 switch (client->
addr & 0x70) {
618 dev_err(&client->
dev,
"invalid I2C address specified %02x\n",
626 max732x_readb(chip, is_group_a(chip, 0), &chip->
reg_out[0]);
628 max732x_readb(chip, is_group_a(chip, 8), &chip->
reg_out[1]);
630 ret = max732x_irq_setup(chip,
id);
645 i2c_set_clientdata(client, chip);
649 max732x_irq_teardown(chip);
673 "gpiochip_remove()", ret);
677 max732x_irq_teardown(chip);
692 .probe = max732x_probe,
694 .id_table = max732x_id,
697 static int __init max732x_init(
void)
699 return i2c_add_driver(&max732x_driver);
706 static void __exit max732x_exit(
void)