0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/slab.h>
0016 #include <linux/string.h>
0017 #include <linux/gpio/driver.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/i2c.h>
0020 #include <linux/platform_data/max732x.h>
0021 #include <linux/of.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058 #define PORT_NONE 0x0
0059 #define PORT_OUTPUT 0x1
0060 #define PORT_INPUT 0x2
0061 #define PORT_OPENDRAIN 0x3
0062
0063 #define IO_4I4O 0x5AA5
0064 #define IO_4P4O 0x5FF5
0065 #define IO_8I 0xAAAA
0066 #define IO_8P 0xFFFF
0067 #define IO_8O 0x5555
0068
0069 #define GROUP_A(x) ((x) & 0xffff)
0070 #define GROUP_B(x) ((x) << 16)
0071
0072 #define INT_NONE 0x0
0073 #define INT_NO_MASK 0x1
0074 #define INT_INDEP_MASK 0x2
0075 #define INT_MERGED_MASK 0x3
0076
0077 #define INT_CAPS(x) (((uint64_t)(x)) << 32)
0078
0079 enum {
0080 MAX7319,
0081 MAX7320,
0082 MAX7321,
0083 MAX7322,
0084 MAX7323,
0085 MAX7324,
0086 MAX7325,
0087 MAX7326,
0088 MAX7327,
0089 };
0090
0091 static uint64_t max732x_features[] = {
0092 [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
0093 [MAX7320] = GROUP_B(IO_8O),
0094 [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
0095 [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
0096 [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
0097 [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
0098 [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
0099 [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
0100 [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
0101 };
0102
0103 static const struct i2c_device_id max732x_id[] = {
0104 { "max7319", MAX7319 },
0105 { "max7320", MAX7320 },
0106 { "max7321", MAX7321 },
0107 { "max7322", MAX7322 },
0108 { "max7323", MAX7323 },
0109 { "max7324", MAX7324 },
0110 { "max7325", MAX7325 },
0111 { "max7326", MAX7326 },
0112 { "max7327", MAX7327 },
0113 { },
0114 };
0115 MODULE_DEVICE_TABLE(i2c, max732x_id);
0116
0117 #ifdef CONFIG_OF
0118 static const struct of_device_id max732x_of_table[] = {
0119 { .compatible = "maxim,max7319" },
0120 { .compatible = "maxim,max7320" },
0121 { .compatible = "maxim,max7321" },
0122 { .compatible = "maxim,max7322" },
0123 { .compatible = "maxim,max7323" },
0124 { .compatible = "maxim,max7324" },
0125 { .compatible = "maxim,max7325" },
0126 { .compatible = "maxim,max7326" },
0127 { .compatible = "maxim,max7327" },
0128 { }
0129 };
0130 MODULE_DEVICE_TABLE(of, max732x_of_table);
0131 #endif
0132
0133 struct max732x_chip {
0134 struct gpio_chip gpio_chip;
0135
0136 struct i2c_client *client;
0137 struct i2c_client *client_dummy;
0138 struct i2c_client *client_group_a;
0139 struct i2c_client *client_group_b;
0140
0141 unsigned int mask_group_a;
0142 unsigned int dir_input;
0143 unsigned int dir_output;
0144
0145 struct mutex lock;
0146 uint8_t reg_out[2];
0147
0148 #ifdef CONFIG_GPIO_MAX732X_IRQ
0149 struct mutex irq_lock;
0150 uint8_t irq_mask;
0151 uint8_t irq_mask_cur;
0152 uint8_t irq_trig_raise;
0153 uint8_t irq_trig_fall;
0154 uint8_t irq_features;
0155 #endif
0156 };
0157
0158 static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
0159 {
0160 struct i2c_client *client;
0161 int ret;
0162
0163 client = group_a ? chip->client_group_a : chip->client_group_b;
0164 ret = i2c_smbus_write_byte(client, val);
0165 if (ret < 0) {
0166 dev_err(&client->dev, "failed writing\n");
0167 return ret;
0168 }
0169
0170 return 0;
0171 }
0172
0173 static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
0174 {
0175 struct i2c_client *client;
0176 int ret;
0177
0178 client = group_a ? chip->client_group_a : chip->client_group_b;
0179 ret = i2c_smbus_read_byte(client);
0180 if (ret < 0) {
0181 dev_err(&client->dev, "failed reading\n");
0182 return ret;
0183 }
0184
0185 *val = (uint8_t)ret;
0186 return 0;
0187 }
0188
0189 static inline int is_group_a(struct max732x_chip *chip, unsigned off)
0190 {
0191 return (1u << off) & chip->mask_group_a;
0192 }
0193
0194 static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
0195 {
0196 struct max732x_chip *chip = gpiochip_get_data(gc);
0197 uint8_t reg_val;
0198 int ret;
0199
0200 ret = max732x_readb(chip, is_group_a(chip, off), ®_val);
0201 if (ret < 0)
0202 return ret;
0203
0204 return !!(reg_val & (1u << (off & 0x7)));
0205 }
0206
0207 static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
0208 int val)
0209 {
0210 struct max732x_chip *chip = gpiochip_get_data(gc);
0211 uint8_t reg_out;
0212 int ret;
0213
0214 mutex_lock(&chip->lock);
0215
0216 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
0217 reg_out = (reg_out & ~mask) | (val & mask);
0218
0219 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
0220 if (ret < 0)
0221 goto out;
0222
0223
0224 if (off > 7)
0225 chip->reg_out[1] = reg_out;
0226 else
0227 chip->reg_out[0] = reg_out;
0228 out:
0229 mutex_unlock(&chip->lock);
0230 }
0231
0232 static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
0233 {
0234 unsigned base = off & ~0x7;
0235 uint8_t mask = 1u << (off & 0x7);
0236
0237 max732x_gpio_set_mask(gc, base, mask, val << (off & 0x7));
0238 }
0239
0240 static void max732x_gpio_set_multiple(struct gpio_chip *gc,
0241 unsigned long *mask, unsigned long *bits)
0242 {
0243 unsigned mask_lo = mask[0] & 0xff;
0244 unsigned mask_hi = (mask[0] >> 8) & 0xff;
0245
0246 if (mask_lo)
0247 max732x_gpio_set_mask(gc, 0, mask_lo, bits[0] & 0xff);
0248 if (mask_hi)
0249 max732x_gpio_set_mask(gc, 8, mask_hi, (bits[0] >> 8) & 0xff);
0250 }
0251
0252 static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
0253 {
0254 struct max732x_chip *chip = gpiochip_get_data(gc);
0255 unsigned int mask = 1u << off;
0256
0257 if ((mask & chip->dir_input) == 0) {
0258 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
0259 chip->client->name, off);
0260 return -EACCES;
0261 }
0262
0263
0264
0265
0266
0267 if ((mask & chip->dir_output))
0268 max732x_gpio_set_value(gc, off, 1);
0269
0270 return 0;
0271 }
0272
0273 static int max732x_gpio_direction_output(struct gpio_chip *gc,
0274 unsigned off, int val)
0275 {
0276 struct max732x_chip *chip = gpiochip_get_data(gc);
0277 unsigned int mask = 1u << off;
0278
0279 if ((mask & chip->dir_output) == 0) {
0280 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
0281 chip->client->name, off);
0282 return -EACCES;
0283 }
0284
0285 max732x_gpio_set_value(gc, off, val);
0286 return 0;
0287 }
0288
0289 #ifdef CONFIG_GPIO_MAX732X_IRQ
0290 static int max732x_writew(struct max732x_chip *chip, uint16_t val)
0291 {
0292 int ret;
0293
0294 val = cpu_to_le16(val);
0295
0296 ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
0297 if (ret < 0) {
0298 dev_err(&chip->client_group_a->dev, "failed writing\n");
0299 return ret;
0300 }
0301
0302 return 0;
0303 }
0304
0305 static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
0306 {
0307 int ret;
0308
0309 ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
0310 if (ret < 0) {
0311 dev_err(&chip->client_group_a->dev, "failed reading\n");
0312 return ret;
0313 }
0314
0315 *val = le16_to_cpu(*val);
0316 return 0;
0317 }
0318
0319 static void max732x_irq_update_mask(struct max732x_chip *chip)
0320 {
0321 uint16_t msg;
0322
0323 if (chip->irq_mask == chip->irq_mask_cur)
0324 return;
0325
0326 chip->irq_mask = chip->irq_mask_cur;
0327
0328 if (chip->irq_features == INT_NO_MASK)
0329 return;
0330
0331 mutex_lock(&chip->lock);
0332
0333 switch (chip->irq_features) {
0334 case INT_INDEP_MASK:
0335 msg = (chip->irq_mask << 8) | chip->reg_out[0];
0336 max732x_writew(chip, msg);
0337 break;
0338
0339 case INT_MERGED_MASK:
0340 msg = chip->irq_mask | chip->reg_out[0];
0341 max732x_writeb(chip, 1, (uint8_t)msg);
0342 break;
0343 }
0344
0345 mutex_unlock(&chip->lock);
0346 }
0347
0348 static void max732x_irq_mask(struct irq_data *d)
0349 {
0350 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0351 struct max732x_chip *chip = gpiochip_get_data(gc);
0352
0353 chip->irq_mask_cur &= ~(1 << d->hwirq);
0354 }
0355
0356 static void max732x_irq_unmask(struct irq_data *d)
0357 {
0358 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0359 struct max732x_chip *chip = gpiochip_get_data(gc);
0360
0361 chip->irq_mask_cur |= 1 << d->hwirq;
0362 }
0363
0364 static void max732x_irq_bus_lock(struct irq_data *d)
0365 {
0366 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0367 struct max732x_chip *chip = gpiochip_get_data(gc);
0368
0369 mutex_lock(&chip->irq_lock);
0370 chip->irq_mask_cur = chip->irq_mask;
0371 }
0372
0373 static void max732x_irq_bus_sync_unlock(struct irq_data *d)
0374 {
0375 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0376 struct max732x_chip *chip = gpiochip_get_data(gc);
0377 uint16_t new_irqs;
0378 uint16_t level;
0379
0380 max732x_irq_update_mask(chip);
0381
0382 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
0383 while (new_irqs) {
0384 level = __ffs(new_irqs);
0385 max732x_gpio_direction_input(&chip->gpio_chip, level);
0386 new_irqs &= ~(1 << level);
0387 }
0388
0389 mutex_unlock(&chip->irq_lock);
0390 }
0391
0392 static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
0393 {
0394 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0395 struct max732x_chip *chip = gpiochip_get_data(gc);
0396 uint16_t off = d->hwirq;
0397 uint16_t mask = 1 << off;
0398
0399 if (!(mask & chip->dir_input)) {
0400 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
0401 chip->client->name, off);
0402 return -EACCES;
0403 }
0404
0405 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
0406 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
0407 d->irq, type);
0408 return -EINVAL;
0409 }
0410
0411 if (type & IRQ_TYPE_EDGE_FALLING)
0412 chip->irq_trig_fall |= mask;
0413 else
0414 chip->irq_trig_fall &= ~mask;
0415
0416 if (type & IRQ_TYPE_EDGE_RISING)
0417 chip->irq_trig_raise |= mask;
0418 else
0419 chip->irq_trig_raise &= ~mask;
0420
0421 return 0;
0422 }
0423
0424 static int max732x_irq_set_wake(struct irq_data *data, unsigned int on)
0425 {
0426 struct max732x_chip *chip = irq_data_get_irq_chip_data(data);
0427
0428 irq_set_irq_wake(chip->client->irq, on);
0429 return 0;
0430 }
0431
0432 static struct irq_chip max732x_irq_chip = {
0433 .name = "max732x",
0434 .irq_mask = max732x_irq_mask,
0435 .irq_unmask = max732x_irq_unmask,
0436 .irq_bus_lock = max732x_irq_bus_lock,
0437 .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
0438 .irq_set_type = max732x_irq_set_type,
0439 .irq_set_wake = max732x_irq_set_wake,
0440 };
0441
0442 static uint8_t max732x_irq_pending(struct max732x_chip *chip)
0443 {
0444 uint8_t cur_stat;
0445 uint8_t old_stat;
0446 uint8_t trigger;
0447 uint8_t pending;
0448 uint16_t status;
0449 int ret;
0450
0451 ret = max732x_readw(chip, &status);
0452 if (ret)
0453 return 0;
0454
0455 trigger = status >> 8;
0456 trigger &= chip->irq_mask;
0457
0458 if (!trigger)
0459 return 0;
0460
0461 cur_stat = status & 0xFF;
0462 cur_stat &= chip->irq_mask;
0463
0464 old_stat = cur_stat ^ trigger;
0465
0466 pending = (old_stat & chip->irq_trig_fall) |
0467 (cur_stat & chip->irq_trig_raise);
0468 pending &= trigger;
0469
0470 return pending;
0471 }
0472
0473 static irqreturn_t max732x_irq_handler(int irq, void *devid)
0474 {
0475 struct max732x_chip *chip = devid;
0476 uint8_t pending;
0477 uint8_t level;
0478
0479 pending = max732x_irq_pending(chip);
0480
0481 if (!pending)
0482 return IRQ_HANDLED;
0483
0484 do {
0485 level = __ffs(pending);
0486 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
0487 level));
0488
0489 pending &= ~(1 << level);
0490 } while (pending);
0491
0492 return IRQ_HANDLED;
0493 }
0494
0495 static int max732x_irq_setup(struct max732x_chip *chip,
0496 const struct i2c_device_id *id)
0497 {
0498 struct i2c_client *client = chip->client;
0499 int has_irq = max732x_features[id->driver_data] >> 32;
0500 int irq_base = 0;
0501 int ret;
0502
0503 if (client->irq && has_irq != INT_NONE) {
0504 struct gpio_irq_chip *girq;
0505
0506 chip->irq_features = has_irq;
0507 mutex_init(&chip->irq_lock);
0508
0509 ret = devm_request_threaded_irq(&client->dev, client->irq,
0510 NULL, max732x_irq_handler, IRQF_ONESHOT |
0511 IRQF_TRIGGER_FALLING | IRQF_SHARED,
0512 dev_name(&client->dev), chip);
0513 if (ret) {
0514 dev_err(&client->dev, "failed to request irq %d\n",
0515 client->irq);
0516 return ret;
0517 }
0518
0519 girq = &chip->gpio_chip.irq;
0520 girq->chip = &max732x_irq_chip;
0521
0522 girq->parent_handler = NULL;
0523 girq->num_parents = 0;
0524 girq->parents = NULL;
0525 girq->default_type = IRQ_TYPE_NONE;
0526 girq->handler = handle_simple_irq;
0527 girq->threaded = true;
0528 girq->first = irq_base;
0529 }
0530
0531 return 0;
0532 }
0533
0534 #else
0535 static int max732x_irq_setup(struct max732x_chip *chip,
0536 const struct i2c_device_id *id)
0537 {
0538 struct i2c_client *client = chip->client;
0539 int has_irq = max732x_features[id->driver_data] >> 32;
0540
0541 if (client->irq && has_irq != INT_NONE)
0542 dev_warn(&client->dev, "interrupt support not compiled in\n");
0543
0544 return 0;
0545 }
0546 #endif
0547
0548 static int max732x_setup_gpio(struct max732x_chip *chip,
0549 const struct i2c_device_id *id,
0550 unsigned gpio_start)
0551 {
0552 struct gpio_chip *gc = &chip->gpio_chip;
0553 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
0554 int i, port = 0;
0555
0556 for (i = 0; i < 16; i++, id_data >>= 2) {
0557 unsigned int mask = 1 << port;
0558
0559 switch (id_data & 0x3) {
0560 case PORT_OUTPUT:
0561 chip->dir_output |= mask;
0562 break;
0563 case PORT_INPUT:
0564 chip->dir_input |= mask;
0565 break;
0566 case PORT_OPENDRAIN:
0567 chip->dir_output |= mask;
0568 chip->dir_input |= mask;
0569 break;
0570 default:
0571 continue;
0572 }
0573
0574 if (i < 8)
0575 chip->mask_group_a |= mask;
0576 port++;
0577 }
0578
0579 if (chip->dir_input)
0580 gc->direction_input = max732x_gpio_direction_input;
0581 if (chip->dir_output) {
0582 gc->direction_output = max732x_gpio_direction_output;
0583 gc->set = max732x_gpio_set_value;
0584 gc->set_multiple = max732x_gpio_set_multiple;
0585 }
0586 gc->get = max732x_gpio_get_value;
0587 gc->can_sleep = true;
0588
0589 gc->base = gpio_start;
0590 gc->ngpio = port;
0591 gc->label = chip->client->name;
0592 gc->parent = &chip->client->dev;
0593 gc->owner = THIS_MODULE;
0594
0595 return port;
0596 }
0597
0598 static struct max732x_platform_data *of_gpio_max732x(struct device *dev)
0599 {
0600 struct max732x_platform_data *pdata;
0601
0602 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0603 if (!pdata)
0604 return NULL;
0605
0606 pdata->gpio_base = -1;
0607
0608 return pdata;
0609 }
0610
0611 static int max732x_probe(struct i2c_client *client,
0612 const struct i2c_device_id *id)
0613 {
0614 struct max732x_platform_data *pdata;
0615 struct device_node *node;
0616 struct max732x_chip *chip;
0617 struct i2c_client *c;
0618 uint16_t addr_a, addr_b;
0619 int ret, nr_port;
0620
0621 pdata = dev_get_platdata(&client->dev);
0622 node = client->dev.of_node;
0623
0624 if (!pdata && node)
0625 pdata = of_gpio_max732x(&client->dev);
0626
0627 if (!pdata) {
0628 dev_dbg(&client->dev, "no platform data\n");
0629 return -EINVAL;
0630 }
0631
0632 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
0633 if (chip == NULL)
0634 return -ENOMEM;
0635 chip->client = client;
0636
0637 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
0638 chip->gpio_chip.parent = &client->dev;
0639
0640 addr_a = (client->addr & 0x0f) | 0x60;
0641 addr_b = (client->addr & 0x0f) | 0x50;
0642
0643 switch (client->addr & 0x70) {
0644 case 0x60:
0645 chip->client_group_a = client;
0646 if (nr_port > 8) {
0647 c = devm_i2c_new_dummy_device(&client->dev,
0648 client->adapter, addr_b);
0649 if (IS_ERR(c)) {
0650 dev_err(&client->dev,
0651 "Failed to allocate I2C device\n");
0652 return PTR_ERR(c);
0653 }
0654 chip->client_group_b = chip->client_dummy = c;
0655 }
0656 break;
0657 case 0x50:
0658 chip->client_group_b = client;
0659 if (nr_port > 8) {
0660 c = devm_i2c_new_dummy_device(&client->dev,
0661 client->adapter, addr_a);
0662 if (IS_ERR(c)) {
0663 dev_err(&client->dev,
0664 "Failed to allocate I2C device\n");
0665 return PTR_ERR(c);
0666 }
0667 chip->client_group_a = chip->client_dummy = c;
0668 }
0669 break;
0670 default:
0671 dev_err(&client->dev, "invalid I2C address specified %02x\n",
0672 client->addr);
0673 return -EINVAL;
0674 }
0675
0676 if (nr_port > 8 && !chip->client_dummy) {
0677 dev_err(&client->dev,
0678 "Failed to allocate second group I2C device\n");
0679 return -ENODEV;
0680 }
0681
0682 mutex_init(&chip->lock);
0683
0684 ret = max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
0685 if (ret)
0686 return ret;
0687 if (nr_port > 8) {
0688 ret = max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
0689 if (ret)
0690 return ret;
0691 }
0692
0693 ret = max732x_irq_setup(chip, id);
0694 if (ret)
0695 return ret;
0696
0697 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
0698 if (ret)
0699 return ret;
0700
0701 i2c_set_clientdata(client, chip);
0702 return 0;
0703 }
0704
0705 static struct i2c_driver max732x_driver = {
0706 .driver = {
0707 .name = "max732x",
0708 .of_match_table = of_match_ptr(max732x_of_table),
0709 },
0710 .probe = max732x_probe,
0711 .id_table = max732x_id,
0712 };
0713
0714 static int __init max732x_init(void)
0715 {
0716 return i2c_add_driver(&max732x_driver);
0717 }
0718
0719
0720
0721 subsys_initcall(max732x_init);
0722
0723 static void __exit max732x_exit(void)
0724 {
0725 i2c_del_driver(&max732x_driver);
0726 }
0727 module_exit(max732x_exit);
0728
0729 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
0730 MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
0731 MODULE_LICENSE("GPL");