16 #include <linux/module.h>
22 #include <linux/device.h>
86 #define GPIO_INDEX(bank, gpio) (gpio % bank->width)
87 #define GPIO_BIT(bank, gpio) (1 << GPIO_INDEX(bank, gpio))
88 #define GPIO_MOD_CTRL_BIT BIT(0)
95 static void _set_gpio_direction(
struct gpio_bank *bank,
int gpio,
int is_input)
100 reg += bank->
regs->direction;
112 static void _set_gpio_dataout_reg(
struct gpio_bank *bank,
int gpio,
int enable)
118 reg += bank->
regs->set_dataout;
121 reg += bank->
regs->clr_dataout;
129 static void _set_gpio_dataout_mask(
struct gpio_bank *bank,
int gpio,
int enable)
151 static int _get_gpio_dataout(
struct gpio_bank *bank,
int offset)
170 static inline void _gpio_dbck_enable(
struct gpio_bank *bank)
177 bank->
base + bank->
regs->debounce_en);
181 static inline void _gpio_dbck_disable(
struct gpio_bank *bank)
205 static void _set_gpio_debounce(
struct gpio_bank *bank,
unsigned gpio,
217 else if (debounce > 7936)
220 debounce = (debounce / 0x1f) - 1;
225 reg = bank->
base + bank->
regs->debounce;
228 reg = bank->
base + bank->
regs->debounce_en;
247 _gpio_dbck_enable(bank);
264 static void _clear_gpio_debounce(
struct gpio_bank *bank,
unsigned gpio)
275 bank->
context.debounce_en &= ~gpio_bit;
277 bank->
base + bank->
regs->debounce_en);
282 bank->
regs->debounce);
288 static inline void set_gpio_trigger(
struct gpio_bank *bank,
int gpio,
294 _gpio_rmw(base, bank->
regs->leveldetect0, gpio_bit,
296 _gpio_rmw(base, bank->
regs->leveldetect1, gpio_bit,
298 _gpio_rmw(base, bank->
regs->risingdetect, gpio_bit,
300 _gpio_rmw(base, bank->
regs->fallingdetect, gpio_bit,
313 _gpio_rmw(base, bank->
regs->wkup_en, gpio_bit, trigger != 0);
319 if (!bank->
regs->irqctrl) {
344 #ifdef CONFIG_ARCH_OMAP1
349 static void _toggle_gpio_edge_triggering(
struct gpio_bank *bank,
int gpio)
354 if (!bank->
regs->irqctrl)
357 reg += bank->
regs->irqctrl;
368 static void _toggle_gpio_edge_triggering(
struct gpio_bank *bank,
int gpio) {}
371 static int _set_gpio_triggering(
struct gpio_bank *bank,
int gpio,
378 if (bank->
regs->leveldetect0 && bank->
regs->wkup_en) {
379 set_gpio_trigger(bank, gpio, trigger);
380 }
else if (bank->
regs->irqctrl) {
381 reg += bank->
regs->irqctrl;
394 }
else if (bank->
regs->edgectrl1) {
396 reg += bank->
regs->edgectrl2;
398 reg += bank->
regs->edgectrl1;
402 l &= ~(3 << (gpio << 1));
403 if (trigger & IRQ_TYPE_EDGE_RISING)
404 l |= 2 << (gpio << 1);
406 l |= 1 << (gpio << 1);
409 _gpio_rmw(base, bank->
regs->wkup_en, 1 << gpio, trigger);
419 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
424 #ifdef CONFIG_ARCH_OMAP1
432 if (type & ~IRQ_TYPE_SENSE_MASK)
435 if (!bank->
regs->leveldetect0 &&
440 retval = _set_gpio_triggering(bank,
GPIO_INDEX(bank, gpio), type);
441 spin_unlock_irqrestore(&bank->
lock, flags);
451 static void _clear_gpio_irqbank(
struct gpio_bank *bank,
int gpio_mask)
455 reg += bank->
regs->irqstatus;
459 if (bank->
regs->irqstatus2) {
460 reg = bank->
base + bank->
regs->irqstatus2;
468 static inline void _clear_gpio_irqstatus(
struct gpio_bank *bank,
int gpio)
470 _clear_gpio_irqbank(bank,
GPIO_BIT(bank, gpio));
473 static u32 _get_gpio_irqbank_mask(
struct gpio_bank *bank)
479 reg += bank->
regs->irqenable;
481 if (bank->
regs->irqenable_inv)
487 static void _enable_gpio_irqbank(
struct gpio_bank *bank,
int gpio_mask)
492 if (bank->
regs->set_irqenable) {
493 reg += bank->
regs->set_irqenable;
495 bank->
context.irqenable1 |= gpio_mask;
497 reg += bank->
regs->irqenable;
499 if (bank->
regs->irqenable_inv)
509 static void _disable_gpio_irqbank(
struct gpio_bank *bank,
int gpio_mask)
514 if (bank->
regs->clr_irqenable) {
515 reg += bank->
regs->clr_irqenable;
517 bank->
context.irqenable1 &= ~gpio_mask;
519 reg += bank->
regs->irqenable;
521 if (bank->
regs->irqenable_inv)
531 static inline void _set_gpio_irqenable(
struct gpio_bank *bank,
int gpio,
int enable)
534 _enable_gpio_irqbank(bank,
GPIO_BIT(bank, gpio));
536 _disable_gpio_irqbank(bank,
GPIO_BIT(bank, gpio));
547 static int _set_gpio_wakeup(
struct gpio_bank *bank,
int gpio,
int enable)
554 "Unable to modify wakeup on non-wakeup GPIO%d\n", gpio);
562 bank->
context.wake_en &= ~gpio_bit;
565 spin_unlock_irqrestore(&bank->
lock, flags);
570 static void _reset_gpio(
struct gpio_bank *bank,
int gpio)
572 _set_gpio_direction(bank,
GPIO_INDEX(bank, gpio), 1);
573 _set_gpio_irqenable(bank, gpio, 0);
574 _clear_gpio_irqstatus(bank, gpio);
576 _clear_gpio_debounce(bank, gpio);
580 static int gpio_wake_enable(
struct irq_data *d,
unsigned int enable)
582 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
585 return _set_gpio_wakeup(bank, gpio, enable);
588 static int omap_gpio_request(
struct gpio_chip *
chip,
unsigned offset)
598 pm_runtime_get_sync(bank->
dev);
606 if (bank->
regs->pinctrl) {
626 spin_unlock_irqrestore(&bank->
lock, flags);
631 static void omap_gpio_free(
struct gpio_chip *chip,
unsigned offset)
639 if (bank->
regs->wkup_en) {
641 _gpio_rmw(base, bank->
regs->wkup_en, 1 << offset, 0);
659 _reset_gpio(bank, bank->
chip.base + offset);
660 spin_unlock_irqrestore(&bank->
lock, flags);
667 pm_runtime_put(bank->
dev);
683 unsigned int gpio_irq, gpio_index;
686 struct irq_chip *chip = irq_desc_get_chip(desc);
688 chained_irq_enter(chip, desc);
690 bank = irq_get_handler_data(irq);
691 isr_reg = bank->
base + bank->
regs->irqstatus;
692 pm_runtime_get_sync(bank->
dev);
698 u32 isr_saved, level_mask = 0;
701 enabled = _get_gpio_irqbank_mask(bank);
710 _disable_gpio_irqbank(bank, isr_saved & ~level_mask);
711 _clear_gpio_irqbank(bank, isr_saved & ~level_mask);
712 _enable_gpio_irqbank(bank, isr_saved & ~level_mask);
716 if (!level_mask && !unmasked) {
718 chained_irq_exit(chip, desc);
725 for (; isr != 0; isr >>= 1, gpio_irq++) {
741 _toggle_gpio_edge_triggering(bank, gpio_index);
752 chained_irq_exit(chip, desc);
753 pm_runtime_put(bank->
dev);
756 static void gpio_irq_shutdown(
struct irq_data *d)
758 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
763 _reset_gpio(bank, gpio);
764 spin_unlock_irqrestore(&bank->
lock, flags);
767 static void gpio_ack_irq(
struct irq_data *d)
769 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
772 _clear_gpio_irqstatus(bank, gpio);
775 static void gpio_mask_irq(
struct irq_data *d)
777 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
782 _set_gpio_irqenable(bank, gpio, 0);
784 spin_unlock_irqrestore(&bank->
lock, flags);
787 static void gpio_unmask_irq(
struct irq_data *d)
789 struct gpio_bank *bank = irq_data_get_irq_chip_data(d);
791 unsigned int irq_mask =
GPIO_BIT(bank, gpio);
792 u32 trigger = irqd_get_trigger_type(d);
797 _set_gpio_triggering(bank,
GPIO_INDEX(bank, gpio), trigger);
802 _set_gpio_irqenable(bank, gpio, 0);
803 _clear_gpio_irqstatus(bank, gpio);
806 _set_gpio_irqenable(bank, gpio, 1);
807 spin_unlock_irqrestore(&bank->
lock, flags);
810 static struct irq_chip gpio_irq_chip = {
812 .irq_shutdown = gpio_irq_shutdown,
813 .irq_ack = gpio_ack_irq,
814 .irq_mask = gpio_mask_irq,
815 .irq_unmask = gpio_unmask_irq,
816 .irq_set_type = gpio_irq_type,
817 .irq_set_wake = gpio_wake_enable,
822 static int omap_mpuio_suspend_noirq(
struct device *
dev)
825 struct gpio_bank *bank = platform_get_drvdata(pdev);
832 spin_unlock_irqrestore(&bank->
lock, flags);
837 static int omap_mpuio_resume_noirq(
struct device *
dev)
840 struct gpio_bank *bank = platform_get_drvdata(pdev);
847 spin_unlock_irqrestore(&bank->
lock, flags);
852 static const struct dev_pm_ops omap_mpuio_dev_pm_ops = {
853 .suspend_noirq = omap_mpuio_suspend_noirq,
854 .resume_noirq = omap_mpuio_resume_noirq,
861 .pm = &omap_mpuio_dev_pm_ops,
869 .driver = &omap_mpuio_driver.
driver,
874 static inline void mpuio_init(
struct gpio_bank *bank)
876 platform_set_drvdata(&omap_mpuio_device, bank);
884 static int gpio_input(
struct gpio_chip *chip,
unsigned offset)
891 _set_gpio_direction(bank, offset, 1);
892 spin_unlock_irqrestore(&bank->
lock, flags);
896 static int gpio_is_input(
struct gpio_bank *bank,
int mask)
903 static int gpio_get(
struct gpio_chip *chip,
unsigned offset)
911 if (gpio_is_input(bank, mask))
912 return _get_gpio_datain(bank, offset);
914 return _get_gpio_dataout(bank, offset);
917 static int gpio_output(
struct gpio_chip *chip,
unsigned offset,
int value)
925 _set_gpio_direction(bank, offset, 0);
926 spin_unlock_irqrestore(&bank->
lock, flags);
930 static int gpio_debounce(
struct gpio_chip *chip,
unsigned offset,
939 _set_gpio_debounce(bank, offset, debounce);
940 spin_unlock_irqrestore(&bank->
lock, flags);
945 static void gpio_set(
struct gpio_chip *chip,
unsigned offset,
int value)
953 spin_unlock_irqrestore(&bank->
lock, flags);
956 static int gpio_2irq(
struct gpio_chip *chip,
unsigned offset)
975 pr_info(
"OMAP GPIO hardware version %d.%d\n",
976 (rev >> 4) & 0x0f, rev & 0x0f);
986 static void omap_gpio_mod_init(
struct gpio_bank *bank)
991 if (bank->
width == 16)
999 _gpio_rmw(base, bank->
regs->irqenable, l, bank->
regs->irqenable_inv);
1000 _gpio_rmw(base, bank->
regs->irqstatus, l, !bank->
regs->irqenable_inv);
1001 if (bank->
regs->debounce_en)
1007 if (bank->
regs->ctrl)
1011 if (IS_ERR(bank->
dbck))
1012 dev_err(bank->
dev,
"Could not get gpio dbck\n");
1016 omap_mpuio_alloc_gc(
struct gpio_bank *bank,
unsigned int irq_start,
1019 struct irq_chip_generic *
gc;
1020 struct irq_chip_type *
ct;
1025 dev_err(bank->
dev,
"Memory alloc failed for gc\n");
1029 ct = gc->chip_types;
1034 ct->chip.irq_set_type = gpio_irq_type;
1036 if (bank->
regs->wkup_en)
1037 ct->chip.irq_set_wake = gpio_wake_enable,
1053 bank->
chip.request = omap_gpio_request;
1054 bank->
chip.free = omap_gpio_free;
1055 bank->
chip.direction_input = gpio_input;
1056 bank->
chip.get = gpio_get;
1057 bank->
chip.direction_output = gpio_output;
1058 bank->
chip.set_debounce = gpio_debounce;
1059 bank->
chip.set = gpio_set;
1060 bank->
chip.to_irq = gpio_2irq;
1062 bank->
chip.label =
"mpuio";
1063 if (bank->
regs->wkup_en)
1064 bank->
chip.dev = &omap_mpuio_device.
dev;
1067 bank->
chip.label =
"gpio";
1069 gpio += bank->
width;
1075 for (j = bank->
irq_base; j < bank->irq_base + bank->
width; j++) {
1076 irq_set_lockdep_class(j, &gpio_lock_class);
1079 omap_mpuio_alloc_gc(bank, j, bank->
width);
1086 irq_set_chained_handler(bank->
irq, gpio_irq_handler);
1110 dev_err(dev,
"Memory alloc failed\n");
1116 dev_err(dev,
"Invalid IRQ resource\n");
1129 #ifdef CONFIG_OF_GPIO
1130 bank->
chip.of_node = of_node_get(node);
1135 dev_err(dev,
"Couldn't allocate IRQ numbers\n");
1142 if (bank->
regs->set_dataout && bank->
regs->clr_dataout)
1152 dev_err(dev,
"Invalid mem resource\n");
1158 dev_err(dev,
"Region already claimed\n");
1164 dev_err(dev,
"Could not ioremap\n");
1168 platform_set_drvdata(pdev, bank);
1172 pm_runtime_get_sync(bank->
dev);
1177 omap_gpio_mod_init(bank);
1178 omap_gpio_chip_init(bank);
1179 omap_gpio_show_rev(bank);
1184 pm_runtime_put(bank->
dev);
1191 #ifdef CONFIG_ARCH_OMAP2PLUS
1193 #if defined(CONFIG_PM_RUNTIME)
1194 static void omap_gpio_restore_context(
struct gpio_bank *bank);
1199 struct gpio_bank *bank = platform_get_drvdata(pdev);
1201 unsigned long flags;
1202 u32 wake_low, wake_hi;
1220 bank->
base + bank->
regs->fallingdetect);
1224 bank->
base + bank->
regs->risingdetect);
1227 goto update_gpio_context_count;
1231 goto update_gpio_context_count;
1239 bank->
regs->datain);
1240 l1 = bank->
context.fallingdetect;
1251 update_gpio_context_count:
1256 _gpio_dbck_disable(bank);
1257 spin_unlock_irqrestore(&bank->
lock, flags);
1265 struct gpio_bank *bank = platform_get_drvdata(pdev);
1266 int context_lost_cnt_after;
1267 u32 l = 0,
gen, gen0, gen1;
1268 unsigned long flags;
1271 _gpio_dbck_enable(bank);
1280 bank->
base + bank->
regs->fallingdetect);
1282 bank->
base + bank->
regs->risingdetect);
1285 context_lost_cnt_after =
1288 omap_gpio_restore_context(bank);
1290 spin_unlock_irqrestore(&bank->
lock, flags);
1296 spin_unlock_irqrestore(&bank->
lock, flags);
1301 bank->
base + bank->
regs->fallingdetect);
1303 bank->
base + bank->
regs->risingdetect);
1319 gen0 = l & bank->
context.fallingdetect;
1322 gen1 = l & bank->
context.risingdetect;
1327 ~(bank->
context.risingdetect));
1337 if (!bank->
regs->irqstatus_raw0) {
1339 bank->
regs->leveldetect0);
1341 bank->
regs->leveldetect1);
1344 if (bank->
regs->irqstatus_raw0) {
1346 bank->
regs->leveldetect0);
1348 bank->
regs->leveldetect1);
1355 spin_unlock_irqrestore(&bank->
lock, flags);
1371 pm_runtime_put_sync_suspend(bank->
dev);
1383 pm_runtime_get_sync(bank->
dev);
1387 #if defined(CONFIG_PM_RUNTIME)
1388 static void omap_gpio_restore_context(
struct gpio_bank *bank)
1394 bank->
base + bank->
regs->leveldetect0);
1396 bank->
base + bank->
regs->leveldetect1);
1398 bank->
base + bank->
regs->risingdetect);
1400 bank->
base + bank->
regs->fallingdetect);
1401 if (bank->
regs->set_dataout && bank->
regs->clr_dataout)
1403 bank->
base + bank->
regs->set_dataout);
1411 bank->
regs->debounce);
1413 bank->
base + bank->
regs->debounce_en);
1417 bank->
base + bank->
regs->irqenable);
1419 bank->
base + bank->
regs->irqenable2);
1423 #define omap_gpio_runtime_suspend NULL
1424 #define omap_gpio_runtime_resume NULL
1427 static const struct dev_pm_ops gpio_pm_ops = {
1432 #if defined(CONFIG_OF)
1480 .
regs = &omap2_gpio_regs,
1486 .
regs = &omap2_gpio_regs,
1492 .
regs = &omap4_gpio_regs,
1499 .compatible =
"ti,omap4-gpio",
1500 .data = &omap4_pdata,
1503 .compatible =
"ti,omap3-gpio",
1504 .data = &omap3_pdata,
1507 .compatible =
"ti,omap2-gpio",
1508 .data = &omap2_pdata,
1516 .probe = omap_gpio_probe,
1518 .name =
"omap_gpio",
1529 static int __init omap_gpio_drv_reg(
void)