gpio-max732x.c (5838d18955b52467f4b30486e62a31727b39998d) gpio-max732x.c (984f66432e357701194abc7f753dcad89a1f9de3)
1/*
2 * MAX732x 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>
1/*
2 * MAX732x 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 * Copyright (C) 2015 Linus Walleij <linus.walleij@linaro.org>
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>
8 *
9 * Derived from drivers/gpio/pca953x.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 */
15
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/string.h>
19#include <linux/gpio.h>
20#include <linux/gpio/driver.h>
20#include <linux/interrupt.h>
21#include <linux/interrupt.h>
21#include <linux/irq.h>
22#include <linux/irqdomain.h>
23#include <linux/i2c.h>
24#include <linux/i2c/max732x.h>
25#include <linux/of.h>
26
27
28/*
29 * Each port of MAX732x (including MAX7319) falls into one of the
30 * following three types:

--- 114 unchanged lines hidden (view full) ---

145 unsigned int mask_group_a;
146 unsigned int dir_input;
147 unsigned int dir_output;
148
149 struct mutex lock;
150 uint8_t reg_out[2];
151
152#ifdef CONFIG_GPIO_MAX732X_IRQ
22#include <linux/i2c.h>
23#include <linux/i2c/max732x.h>
24#include <linux/of.h>
25
26
27/*
28 * Each port of MAX732x (including MAX7319) falls into one of the
29 * following three types:

--- 114 unchanged lines hidden (view full) ---

144 unsigned int mask_group_a;
145 unsigned int dir_input;
146 unsigned int dir_output;
147
148 struct mutex lock;
149 uint8_t reg_out[2];
150
151#ifdef CONFIG_GPIO_MAX732X_IRQ
153 struct irq_domain *irq_domain;
154 struct mutex irq_lock;
152 struct mutex irq_lock;
155 int irq_base;
156 uint8_t irq_mask;
157 uint8_t irq_mask_cur;
158 uint8_t irq_trig_raise;
159 uint8_t irq_trig_fall;
160 uint8_t irq_features;
161#endif
162};
163

--- 187 unchanged lines hidden (view full) ---

351 msg = chip->irq_mask | chip->reg_out[0];
352 max732x_writeb(chip, 1, (uint8_t)msg);
353 break;
354 }
355
356 mutex_unlock(&chip->lock);
357}
358
153 uint8_t irq_mask;
154 uint8_t irq_mask_cur;
155 uint8_t irq_trig_raise;
156 uint8_t irq_trig_fall;
157 uint8_t irq_features;
158#endif
159};
160

--- 187 unchanged lines hidden (view full) ---

348 msg = chip->irq_mask | chip->reg_out[0];
349 max732x_writeb(chip, 1, (uint8_t)msg);
350 break;
351 }
352
353 mutex_unlock(&chip->lock);
354}
355
359static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
356static void max732x_irq_mask(struct irq_data *d)
360{
357{
358 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
361 struct max732x_chip *chip = to_max732x(gc);
362
359 struct max732x_chip *chip = to_max732x(gc);
360
363 if (chip->irq_domain) {
364 return irq_create_mapping(chip->irq_domain,
365 chip->irq_base + off);
366 } else {
367 return -ENXIO;
368 }
369}
370
371static void max732x_irq_mask(struct irq_data *d)
372{
373 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
374
375 chip->irq_mask_cur &= ~(1 << d->hwirq);
376}
377
378static void max732x_irq_unmask(struct irq_data *d)
379{
361 chip->irq_mask_cur &= ~(1 << d->hwirq);
362}
363
364static void max732x_irq_unmask(struct irq_data *d)
365{
380 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
366 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
367 struct max732x_chip *chip = to_max732x(gc);
381
382 chip->irq_mask_cur |= 1 << d->hwirq;
383}
384
385static void max732x_irq_bus_lock(struct irq_data *d)
386{
368
369 chip->irq_mask_cur |= 1 << d->hwirq;
370}
371
372static void max732x_irq_bus_lock(struct irq_data *d)
373{
387 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
374 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
375 struct max732x_chip *chip = to_max732x(gc);
388
389 mutex_lock(&chip->irq_lock);
390 chip->irq_mask_cur = chip->irq_mask;
391}
392
393static void max732x_irq_bus_sync_unlock(struct irq_data *d)
394{
376
377 mutex_lock(&chip->irq_lock);
378 chip->irq_mask_cur = chip->irq_mask;
379}
380
381static void max732x_irq_bus_sync_unlock(struct irq_data *d)
382{
395 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
383 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
384 struct max732x_chip *chip = to_max732x(gc);
396 uint16_t new_irqs;
397 uint16_t level;
398
399 max732x_irq_update_mask(chip);
400
401 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
402 while (new_irqs) {
403 level = __ffs(new_irqs);
404 max732x_gpio_direction_input(&chip->gpio_chip, level);
405 new_irqs &= ~(1 << level);
406 }
407
408 mutex_unlock(&chip->irq_lock);
409}
410
411static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
412{
385 uint16_t new_irqs;
386 uint16_t level;
387
388 max732x_irq_update_mask(chip);
389
390 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
391 while (new_irqs) {
392 level = __ffs(new_irqs);
393 max732x_gpio_direction_input(&chip->gpio_chip, level);
394 new_irqs &= ~(1 << level);
395 }
396
397 mutex_unlock(&chip->irq_lock);
398}
399
400static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
401{
413 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
402 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
403 struct max732x_chip *chip = to_max732x(gc);
414 uint16_t off = d->hwirq;
415 uint16_t mask = 1 << off;
416
417 if (!(mask & chip->dir_input)) {
418 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
419 chip->client->name, off);
420 return -EACCES;
421 }

--- 65 unchanged lines hidden (view full) ---

487
488 pending = max732x_irq_pending(chip);
489
490 if (!pending)
491 return IRQ_HANDLED;
492
493 do {
494 level = __ffs(pending);
404 uint16_t off = d->hwirq;
405 uint16_t mask = 1 << off;
406
407 if (!(mask & chip->dir_input)) {
408 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
409 chip->client->name, off);
410 return -EACCES;
411 }

--- 65 unchanged lines hidden (view full) ---

477
478 pending = max732x_irq_pending(chip);
479
480 if (!pending)
481 return IRQ_HANDLED;
482
483 do {
484 level = __ffs(pending);
495 handle_nested_irq(irq_find_mapping(chip->irq_domain, level));
485 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
486 level));
496
497 pending &= ~(1 << level);
498 } while (pending);
499
500 return IRQ_HANDLED;
501}
502
487
488 pending &= ~(1 << level);
489 } while (pending);
490
491 return IRQ_HANDLED;
492}
493
503static int max732x_irq_map(struct irq_domain *h, unsigned int virq,
504 irq_hw_number_t hw)
505{
506 struct max732x_chip *chip = h->host_data;
507
508 if (!(chip->dir_input & (1 << hw))) {
509 dev_err(&chip->client->dev,
510 "Attempt to map output line as IRQ line: %lu\n",
511 hw);
512 return -EPERM;
513 }
514
515 irq_set_chip_data(virq, chip);
516 irq_set_chip_and_handler(virq, &max732x_irq_chip,
517 handle_edge_irq);
518 irq_set_nested_thread(virq, 1);
519#ifdef CONFIG_ARM
520 /* ARM needs us to explicitly flag the IRQ as valid
521 * and will set them noprobe when we do so. */
522 set_irq_flags(virq, IRQF_VALID);
523#else
524 irq_set_noprobe(virq);
525#endif
526
527 return 0;
528}
529
530static struct irq_domain_ops max732x_irq_domain_ops = {
531 .map = max732x_irq_map,
532 .xlate = irq_domain_xlate_twocell,
533};
534
535static void max732x_irq_teardown(struct max732x_chip *chip)
536{
537 if (chip->client->irq && chip->irq_domain)
538 irq_domain_remove(chip->irq_domain);
539}
540
541static int max732x_irq_setup(struct max732x_chip *chip,
542 const struct i2c_device_id *id)
543{
544 struct i2c_client *client = chip->client;
545 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
546 int has_irq = max732x_features[id->driver_data] >> 32;
494static int max732x_irq_setup(struct max732x_chip *chip,
495 const struct i2c_device_id *id)
496{
497 struct i2c_client *client = chip->client;
498 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
499 int has_irq = max732x_features[id->driver_data] >> 32;
500 int irq_base = 0;
547 int ret;
548
549 if (((pdata && pdata->irq_base) || client->irq)
550 && has_irq != INT_NONE) {
551 if (pdata)
501 int ret;
502
503 if (((pdata && pdata->irq_base) || client->irq)
504 && has_irq != INT_NONE) {
505 if (pdata)
552 chip->irq_base = pdata->irq_base;
506 irq_base = pdata->irq_base;
553 chip->irq_features = has_irq;
554 mutex_init(&chip->irq_lock);
555
507 chip->irq_features = has_irq;
508 mutex_init(&chip->irq_lock);
509
556 chip->irq_domain = irq_domain_add_simple(client->dev.of_node,
557 chip->gpio_chip.ngpio, chip->irq_base,
558 &max732x_irq_domain_ops, chip);
559 if (!chip->irq_domain) {
560 dev_err(&client->dev, "Failed to create IRQ domain\n");
561 return -ENOMEM;
562 }
563
564 ret = request_threaded_irq(client->irq,
565 NULL,
566 max732x_irq_handler,
567 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
568 dev_name(&client->dev), chip);
510 ret = devm_request_threaded_irq(&client->dev,
511 client->irq,
512 NULL,
513 max732x_irq_handler,
514 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
515 dev_name(&client->dev), chip);
569 if (ret) {
570 dev_err(&client->dev, "failed to request irq %d\n",
571 client->irq);
516 if (ret) {
517 dev_err(&client->dev, "failed to request irq %d\n",
518 client->irq);
572 goto out_failed;
519 return ret;
573 }
520 }
574
575 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
521 ret = gpiochip_irqchip_add(&chip->gpio_chip,
522 &max732x_irq_chip,
523 irq_base,
524 handle_edge_irq,
525 IRQ_TYPE_NONE);
526 if (ret) {
527 dev_err(&client->dev,
528 "could not connect irqchip to gpiochip\n");
529 return ret;
530 }
531 gpiochip_set_chained_irqchip(&chip->gpio_chip,
532 &max732x_irq_chip,
533 client->irq,
534 NULL);
576 }
577
578 return 0;
535 }
536
537 return 0;
579
580out_failed:
581 max732x_irq_teardown(chip);
582 return ret;
583}
584
585#else /* CONFIG_GPIO_MAX732X_IRQ */
586static int max732x_irq_setup(struct max732x_chip *chip,
587 const struct i2c_device_id *id)
588{
589 struct i2c_client *client = chip->client;
590 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
591 int has_irq = max732x_features[id->driver_data] >> 32;
592
593 if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
594 dev_warn(&client->dev, "interrupt support not compiled in\n");
595
596 return 0;
597}
538}
539
540#else /* CONFIG_GPIO_MAX732X_IRQ */
541static int max732x_irq_setup(struct max732x_chip *chip,
542 const struct i2c_device_id *id)
543{
544 struct i2c_client *client = chip->client;
545 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
546 int has_irq = max732x_features[id->driver_data] >> 32;
547
548 if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
549 dev_warn(&client->dev, "interrupt support not compiled in\n");
550
551 return 0;
552}
598
599static void max732x_irq_teardown(struct max732x_chip *chip)
600{
601}
602#endif
603
604static int max732x_setup_gpio(struct max732x_chip *chip,
605 const struct i2c_device_id *id,
606 unsigned gpio_start)
607{
608 struct gpio_chip *gc = &chip->gpio_chip;
609 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];

--- 115 unchanged lines hidden (view full) ---

725 }
726
727 mutex_init(&chip->lock);
728
729 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
730 if (nr_port > 8)
731 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
732
553#endif
554
555static int max732x_setup_gpio(struct max732x_chip *chip,
556 const struct i2c_device_id *id,
557 unsigned gpio_start)
558{
559 struct gpio_chip *gc = &chip->gpio_chip;
560 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];

--- 115 unchanged lines hidden (view full) ---

676 }
677
678 mutex_init(&chip->lock);
679
680 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
681 if (nr_port > 8)
682 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
683
733 ret = max732x_irq_setup(chip, id);
684 ret = gpiochip_add(&chip->gpio_chip);
734 if (ret)
735 goto out_failed;
736
685 if (ret)
686 goto out_failed;
687
737 ret = gpiochip_add(&chip->gpio_chip);
738 if (ret)
688 ret = max732x_irq_setup(chip, id);
689 if (ret) {
690 gpiochip_remove(&chip->gpio_chip);
739 goto out_failed;
691 goto out_failed;
692 }
740
741 if (pdata && pdata->setup) {
742 ret = pdata->setup(client, chip->gpio_chip.base,
743 chip->gpio_chip.ngpio, pdata->context);
744 if (ret < 0)
745 dev_warn(&client->dev, "setup failed, %d\n", ret);
746 }
747
748 i2c_set_clientdata(client, chip);
749 return 0;
750
751out_failed:
752 if (chip->client_dummy)
753 i2c_unregister_device(chip->client_dummy);
693
694 if (pdata && pdata->setup) {
695 ret = pdata->setup(client, chip->gpio_chip.base,
696 chip->gpio_chip.ngpio, pdata->context);
697 if (ret < 0)
698 dev_warn(&client->dev, "setup failed, %d\n", ret);
699 }
700
701 i2c_set_clientdata(client, chip);
702 return 0;
703
704out_failed:
705 if (chip->client_dummy)
706 i2c_unregister_device(chip->client_dummy);
754 max732x_irq_teardown(chip);
755 return ret;
756}
757
758static int max732x_remove(struct i2c_client *client)
759{
760 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
761 struct max732x_chip *chip = i2c_get_clientdata(client);
762

--- 6 unchanged lines hidden (view full) ---

769 dev_err(&client->dev, "%s failed, %d\n",
770 "teardown", ret);
771 return ret;
772 }
773 }
774
775 gpiochip_remove(&chip->gpio_chip);
776
707 return ret;
708}
709
710static int max732x_remove(struct i2c_client *client)
711{
712 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
713 struct max732x_chip *chip = i2c_get_clientdata(client);
714

--- 6 unchanged lines hidden (view full) ---

721 dev_err(&client->dev, "%s failed, %d\n",
722 "teardown", ret);
723 return ret;
724 }
725 }
726
727 gpiochip_remove(&chip->gpio_chip);
728
777 max732x_irq_teardown(chip);
778
779 /* unregister any dummy i2c_client */
780 if (chip->client_dummy)
781 i2c_unregister_device(chip->client_dummy);
782
783 return 0;
784}
785
786static struct i2c_driver max732x_driver = {

--- 28 unchanged lines hidden ---
729 /* unregister any dummy i2c_client */
730 if (chip->client_dummy)
731 i2c_unregister_device(chip->client_dummy);
732
733 return 0;
734}
735
736static struct i2c_driver max732x_driver = {

--- 28 unchanged lines hidden ---