Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1 | /* |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 2 | * Support functions for OMAP GPIO |
| 3 | * |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 4 | * Copyright (C) 2003-2005 Nokia Corporation |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 5 | * Written by Juha Yrjölä <juha.yrjola@nokia.com> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 6 | * |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 7 | * Copyright (C) 2009 Texas Instruments |
| 8 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
| 9 | * |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Rafael J. Wysocki | 3c437ff | 2011-04-22 22:02:46 +0200 | [diff] [blame] | 18 | #include <linux/syscore_ops.h> |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 19 | #include <linux/err.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 20 | #include <linux/clk.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 22 | #include <linux/cpu_pm.h> |
Benoit Cousson | 96751fc | 2012-02-01 16:01:39 +0100 | [diff] [blame] | 23 | #include <linux/device.h> |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Tarun Kanti DebBarma | 55b93c3 | 2011-09-29 07:23:22 +0530 | [diff] [blame] | 25 | #include <linux/pm.h> |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_device.h> |
Linus Walleij | b7351b0 | 2018-05-24 14:24:00 +0200 | [diff] [blame] | 28 | #include <linux/gpio/driver.h> |
Yegor Yefremov | 9370084 | 2014-04-24 08:57:39 +0200 | [diff] [blame] | 29 | #include <linux/bitops.h> |
Tony Lindgren | 4b25408 | 2012-08-30 15:37:24 -0700 | [diff] [blame] | 30 | #include <linux/platform_data/gpio-omap.h> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 31 | |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 32 | #define OMAP4_GPIO_DEBOUNCINGTIME_MASK 0xFF |
Tarun Kanti DebBarma | 2dc983c | 2011-11-24 02:44:29 +0530 | [diff] [blame] | 33 | |
Charulatha V | 6d62e21 | 2011-04-18 15:06:51 +0000 | [diff] [blame] | 34 | struct gpio_regs { |
| 35 | u32 irqenable1; |
| 36 | u32 irqenable2; |
| 37 | u32 wake_en; |
| 38 | u32 ctrl; |
| 39 | u32 oe; |
| 40 | u32 leveldetect0; |
| 41 | u32 leveldetect1; |
| 42 | u32 risingdetect; |
| 43 | u32 fallingdetect; |
| 44 | u32 dataout; |
Nishanth Menon | ae54735 | 2011-09-09 19:08:58 +0530 | [diff] [blame] | 45 | u32 debounce; |
| 46 | u32 debounce_en; |
Charulatha V | 6d62e21 | 2011-04-18 15:06:51 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 49 | struct gpio_bank { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 50 | void __iomem *base; |
Grygorii Strashko | 30cefea | 2015-09-25 12:06:02 -0700 | [diff] [blame] | 51 | int irq; |
Juha Yrjola | 3ac4fa9 | 2006-12-06 17:13:52 -0800 | [diff] [blame] | 52 | u32 non_wakeup_gpios; |
| 53 | u32 enabled_non_wakeup_gpios; |
Charulatha V | 6d62e21 | 2011-04-18 15:06:51 +0000 | [diff] [blame] | 54 | struct gpio_regs context; |
Juha Yrjola | 3ac4fa9 | 2006-12-06 17:13:52 -0800 | [diff] [blame] | 55 | u32 saved_datain; |
Kevin Hilman | b144ff6 | 2008-01-16 21:56:15 -0800 | [diff] [blame] | 56 | u32 level_mask; |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 57 | u32 toggle_mask; |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 58 | raw_spinlock_t lock; |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 59 | raw_spinlock_t wa_lock; |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 60 | struct gpio_chip chip; |
Jouni Hogander | 89db948 | 2008-12-10 17:35:24 -0800 | [diff] [blame] | 61 | struct clk *dbck; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 62 | struct notifier_block nb; |
| 63 | unsigned int is_suspended:1; |
Charulatha V | 058af1e | 2009-11-22 10:11:25 -0800 | [diff] [blame] | 64 | u32 mod_usage; |
Javier Martinez Canillas | fa365e4 | 2013-09-25 02:36:52 +0200 | [diff] [blame] | 65 | u32 irq_usage; |
Kevin Hilman | 8865b9b | 2009-01-27 11:15:34 -0800 | [diff] [blame] | 66 | u32 dbck_enable_mask; |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 67 | bool dbck_enabled; |
Charulatha V | d0d665a | 2011-08-31 00:02:21 +0530 | [diff] [blame] | 68 | bool is_mpuio; |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 69 | bool dbck_flag; |
Charulatha V | 0cde8d0 | 2011-05-05 20:15:16 +0530 | [diff] [blame] | 70 | bool loses_context; |
Jon Hunter | 352a2d5 | 2013-04-15 13:06:54 -0500 | [diff] [blame] | 71 | bool context_valid; |
Tony Lindgren | 5de62b8 | 2010-12-07 16:26:58 -0800 | [diff] [blame] | 72 | int stride; |
Kevin Hilman | d5f4624 | 2011-04-21 09:23:00 -0700 | [diff] [blame] | 73 | u32 width; |
Tarun Kanti DebBarma | 60a3437 | 2011-09-29 04:47:25 +0530 | [diff] [blame] | 74 | int context_loss_count; |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 75 | |
Grygorii Strashko | 04ebcbd | 2015-03-23 14:18:24 +0200 | [diff] [blame] | 76 | void (*set_dataout)(struct gpio_bank *bank, unsigned gpio, int enable); |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 77 | void (*set_dataout_multiple)(struct gpio_bank *bank, |
| 78 | unsigned long *mask, unsigned long *bits); |
Tarun Kanti DebBarma | 60a3437 | 2011-09-29 04:47:25 +0530 | [diff] [blame] | 79 | int (*get_context_loss_count)(struct device *dev); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 80 | |
| 81 | struct omap_gpio_reg_offs *regs; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Charulatha V | c8eef65 | 2011-05-02 15:21:42 +0530 | [diff] [blame] | 84 | #define GPIO_MOD_CTRL_BIT BIT(0) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 85 | |
Javier Martinez Canillas | fa365e4 | 2013-09-25 02:36:52 +0200 | [diff] [blame] | 86 | #define BANK_USED(bank) (bank->mod_usage || bank->irq_usage) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 87 | #define LINE_USED(line, offset) (line & (BIT(offset))) |
Javier Martinez Canillas | fa365e4 | 2013-09-25 02:36:52 +0200 | [diff] [blame] | 88 | |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 89 | static void omap_gpio_unmask_irq(struct irq_data *d); |
| 90 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 91 | static inline struct gpio_bank *omap_irq_data_get_bank(struct irq_data *d) |
Jon Hunter | ede4d7a | 2013-03-01 11:22:47 -0600 | [diff] [blame] | 92 | { |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 93 | struct gpio_chip *chip = irq_data_get_irq_chip_data(d); |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 94 | return gpiochip_get_data(chip); |
Benoit Cousson | 25db711 | 2012-02-23 21:50:10 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 97 | static void omap_set_gpio_direction(struct gpio_bank *bank, int gpio, |
| 98 | int is_input) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 99 | { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 100 | void __iomem *reg = bank->base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 101 | u32 l; |
| 102 | |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 103 | reg += bank->regs->direction; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 104 | l = readl_relaxed(reg); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 105 | if (is_input) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 106 | l |= BIT(gpio); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 107 | else |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 108 | l &= ~(BIT(gpio)); |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 109 | writel_relaxed(l, reg); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 110 | bank->context.oe = l; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 113 | |
| 114 | /* set data out value using dedicate set/clear register */ |
Grygorii Strashko | 04ebcbd | 2015-03-23 14:18:24 +0200 | [diff] [blame] | 115 | static void omap_set_gpio_dataout_reg(struct gpio_bank *bank, unsigned offset, |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 116 | int enable) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 117 | { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 118 | void __iomem *reg = bank->base; |
Grygorii Strashko | 04ebcbd | 2015-03-23 14:18:24 +0200 | [diff] [blame] | 119 | u32 l = BIT(offset); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 120 | |
Tarun Kanti DebBarma | 2c836f7 | 2012-03-02 12:52:52 +0530 | [diff] [blame] | 121 | if (enable) { |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 122 | reg += bank->regs->set_dataout; |
Tarun Kanti DebBarma | 2c836f7 | 2012-03-02 12:52:52 +0530 | [diff] [blame] | 123 | bank->context.dataout |= l; |
| 124 | } else { |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 125 | reg += bank->regs->clr_dataout; |
Tarun Kanti DebBarma | 2c836f7 | 2012-03-02 12:52:52 +0530 | [diff] [blame] | 126 | bank->context.dataout &= ~l; |
| 127 | } |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 128 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 129 | writel_relaxed(l, reg); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* set data out value using mask register */ |
Grygorii Strashko | 04ebcbd | 2015-03-23 14:18:24 +0200 | [diff] [blame] | 133 | static void omap_set_gpio_dataout_mask(struct gpio_bank *bank, unsigned offset, |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 134 | int enable) |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 135 | { |
| 136 | void __iomem *reg = bank->base + bank->regs->dataout; |
Grygorii Strashko | 04ebcbd | 2015-03-23 14:18:24 +0200 | [diff] [blame] | 137 | u32 gpio_bit = BIT(offset); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 138 | u32 l; |
| 139 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 140 | l = readl_relaxed(reg); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 141 | if (enable) |
| 142 | l |= gpio_bit; |
| 143 | else |
| 144 | l &= ~gpio_bit; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 145 | writel_relaxed(l, reg); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 146 | bank->context.dataout = l; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 149 | static int omap_get_gpio_datain(struct gpio_bank *bank, int offset) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 150 | { |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 151 | void __iomem *reg = bank->base + bank->regs->datain; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 152 | |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 153 | return (readl_relaxed(reg) & (BIT(offset))) != 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 156 | static int omap_get_gpio_dataout(struct gpio_bank *bank, int offset) |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 157 | { |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 158 | void __iomem *reg = bank->base + bank->regs->dataout; |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 159 | |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 160 | return (readl_relaxed(reg) & (BIT(offset))) != 0; |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 161 | } |
| 162 | |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 163 | /* set multiple data out values using dedicate set/clear register */ |
| 164 | static void omap_set_gpio_dataout_reg_multiple(struct gpio_bank *bank, |
| 165 | unsigned long *mask, |
| 166 | unsigned long *bits) |
| 167 | { |
| 168 | void __iomem *reg = bank->base; |
| 169 | u32 l; |
| 170 | |
| 171 | l = *bits & *mask; |
| 172 | writel_relaxed(l, reg + bank->regs->set_dataout); |
| 173 | bank->context.dataout |= l; |
| 174 | |
| 175 | l = ~*bits & *mask; |
| 176 | writel_relaxed(l, reg + bank->regs->clr_dataout); |
| 177 | bank->context.dataout &= ~l; |
| 178 | } |
| 179 | |
| 180 | /* set multiple data out values using mask register */ |
| 181 | static void omap_set_gpio_dataout_mask_multiple(struct gpio_bank *bank, |
| 182 | unsigned long *mask, |
| 183 | unsigned long *bits) |
| 184 | { |
| 185 | void __iomem *reg = bank->base + bank->regs->dataout; |
| 186 | u32 l = (readl_relaxed(reg) & ~*mask) | (*bits & *mask); |
| 187 | |
| 188 | writel_relaxed(l, reg); |
| 189 | bank->context.dataout = l; |
| 190 | } |
| 191 | |
| 192 | static unsigned long omap_get_gpio_datain_multiple(struct gpio_bank *bank, |
| 193 | unsigned long *mask) |
| 194 | { |
| 195 | void __iomem *reg = bank->base + bank->regs->datain; |
| 196 | |
| 197 | return readl_relaxed(reg) & *mask; |
| 198 | } |
| 199 | |
| 200 | static unsigned long omap_get_gpio_dataout_multiple(struct gpio_bank *bank, |
| 201 | unsigned long *mask) |
| 202 | { |
| 203 | void __iomem *reg = bank->base + bank->regs->dataout; |
| 204 | |
| 205 | return readl_relaxed(reg) & *mask; |
| 206 | } |
| 207 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 208 | static inline void omap_gpio_rmw(void __iomem *base, u32 reg, u32 mask, bool set) |
Kevin Hilman | ece9528 | 2011-07-12 08:18:15 -0700 | [diff] [blame] | 209 | { |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 210 | int l = readl_relaxed(base + reg); |
Kevin Hilman | ece9528 | 2011-07-12 08:18:15 -0700 | [diff] [blame] | 211 | |
Benoit Cousson | 862ff64 | 2012-02-01 15:58:56 +0100 | [diff] [blame] | 212 | if (set) |
Kevin Hilman | ece9528 | 2011-07-12 08:18:15 -0700 | [diff] [blame] | 213 | l |= mask; |
| 214 | else |
| 215 | l &= ~mask; |
| 216 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 217 | writel_relaxed(l, base + reg); |
Kevin Hilman | ece9528 | 2011-07-12 08:18:15 -0700 | [diff] [blame] | 218 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 219 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 220 | static inline void omap_gpio_dbck_enable(struct gpio_bank *bank) |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 221 | { |
| 222 | if (bank->dbck_enable_mask && !bank->dbck_enabled) { |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 223 | clk_enable(bank->dbck); |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 224 | bank->dbck_enabled = true; |
Grazvydas Ignotas | 9e303f2 | 2012-06-16 22:01:25 +0300 | [diff] [blame] | 225 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 226 | writel_relaxed(bank->dbck_enable_mask, |
Grazvydas Ignotas | 9e303f2 | 2012-06-16 22:01:25 +0300 | [diff] [blame] | 227 | bank->base + bank->regs->debounce_en); |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 231 | static inline void omap_gpio_dbck_disable(struct gpio_bank *bank) |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 232 | { |
| 233 | if (bank->dbck_enable_mask && bank->dbck_enabled) { |
Grazvydas Ignotas | 9e303f2 | 2012-06-16 22:01:25 +0300 | [diff] [blame] | 234 | /* |
| 235 | * Disable debounce before cutting it's clock. If debounce is |
| 236 | * enabled but the clock is not, GPIO module seems to be unable |
| 237 | * to detect events and generate interrupts at least on OMAP3. |
| 238 | */ |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 239 | writel_relaxed(0, bank->base + bank->regs->debounce_en); |
Grazvydas Ignotas | 9e303f2 | 2012-06-16 22:01:25 +0300 | [diff] [blame] | 240 | |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 241 | clk_disable(bank->dbck); |
Tarun Kanti DebBarma | 72f83af9 | 2011-11-24 03:03:28 +0530 | [diff] [blame] | 242 | bank->dbck_enabled = false; |
| 243 | } |
| 244 | } |
| 245 | |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 246 | /** |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 247 | * omap2_set_gpio_debounce - low level gpio debounce time |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 248 | * @bank: the gpio bank we're acting upon |
Grygorii Strashko | 4a58d22 | 2015-03-23 14:18:25 +0200 | [diff] [blame] | 249 | * @offset: the gpio number on this @bank |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 250 | * @debounce: debounce time to use |
| 251 | * |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 252 | * OMAP's debounce time is in 31us steps |
| 253 | * <debounce time> = (GPIO_DEBOUNCINGTIME[7:0].DEBOUNCETIME + 1) x 31 |
| 254 | * so we need to convert and round up to the closest unit. |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 255 | * |
| 256 | * Return: 0 on success, negative error otherwise. |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 257 | */ |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 258 | static int omap2_set_gpio_debounce(struct gpio_bank *bank, unsigned offset, |
| 259 | unsigned debounce) |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 260 | { |
Kevin Hilman | 9942da0 | 2011-04-22 12:02:05 -0700 | [diff] [blame] | 261 | void __iomem *reg; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 262 | u32 val; |
| 263 | u32 l; |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 264 | bool enable = !!debounce; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 265 | |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 266 | if (!bank->dbck_flag) |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 267 | return -ENOTSUPP; |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 268 | |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 269 | if (enable) { |
| 270 | debounce = DIV_ROUND_UP(debounce, 31) - 1; |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 271 | if ((debounce & OMAP4_GPIO_DEBOUNCINGTIME_MASK) != debounce) |
| 272 | return -EINVAL; |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 273 | } |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 274 | |
Grygorii Strashko | 4a58d22 | 2015-03-23 14:18:25 +0200 | [diff] [blame] | 275 | l = BIT(offset); |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 276 | |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 277 | clk_enable(bank->dbck); |
Kevin Hilman | 9942da0 | 2011-04-22 12:02:05 -0700 | [diff] [blame] | 278 | reg = bank->base + bank->regs->debounce; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 279 | writel_relaxed(debounce, reg); |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 280 | |
Kevin Hilman | 9942da0 | 2011-04-22 12:02:05 -0700 | [diff] [blame] | 281 | reg = bank->base + bank->regs->debounce_en; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 282 | val = readl_relaxed(reg); |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 283 | |
Grygorii Strashko | e85ec6c | 2015-08-18 14:10:54 +0300 | [diff] [blame] | 284 | if (enable) |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 285 | val |= l; |
Tarun Kanti DebBarma | 6fd9c42 | 2011-11-24 03:58:54 +0530 | [diff] [blame] | 286 | else |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 287 | val &= ~l; |
Kevin Hilman | f7ec0b0 | 2010-06-09 13:53:07 +0300 | [diff] [blame] | 288 | bank->dbck_enable_mask = val; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 289 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 290 | writel_relaxed(val, reg); |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 291 | clk_disable(bank->dbck); |
Tarun Kanti DebBarma | 6fd9c42 | 2011-11-24 03:58:54 +0530 | [diff] [blame] | 292 | /* |
| 293 | * Enable debounce clock per module. |
| 294 | * This call is mandatory because in omap_gpio_request() when |
| 295 | * *_runtime_get_sync() is called, _gpio_dbck_enable() within |
| 296 | * runtime callbck fails to turn on dbck because dbck_enable_mask |
| 297 | * used within _gpio_dbck_enable() is still not initialized at |
| 298 | * that point. Therefore we have to enable dbck here. |
| 299 | */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 300 | omap_gpio_dbck_enable(bank); |
Nishanth Menon | ae54735 | 2011-09-09 19:08:58 +0530 | [diff] [blame] | 301 | if (bank->dbck_enable_mask) { |
| 302 | bank->context.debounce = debounce; |
| 303 | bank->context.debounce_en = val; |
| 304 | } |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 305 | |
| 306 | return 0; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 309 | /** |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 310 | * omap_clear_gpio_debounce - clear debounce settings for a gpio |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 311 | * @bank: the gpio bank we're acting upon |
Grygorii Strashko | 4a58d22 | 2015-03-23 14:18:25 +0200 | [diff] [blame] | 312 | * @offset: the gpio number on this @bank |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 313 | * |
| 314 | * If a gpio is using debounce, then clear the debounce enable bit and if |
| 315 | * this is the only gpio in this bank using debounce, then clear the debounce |
| 316 | * time too. The debounce clock will also be disabled when calling this function |
| 317 | * if this is the only gpio in the bank using debounce. |
| 318 | */ |
Grygorii Strashko | 4a58d22 | 2015-03-23 14:18:25 +0200 | [diff] [blame] | 319 | static void omap_clear_gpio_debounce(struct gpio_bank *bank, unsigned offset) |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 320 | { |
Grygorii Strashko | 4a58d22 | 2015-03-23 14:18:25 +0200 | [diff] [blame] | 321 | u32 gpio_bit = BIT(offset); |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 322 | |
| 323 | if (!bank->dbck_flag) |
| 324 | return; |
| 325 | |
| 326 | if (!(bank->dbck_enable_mask & gpio_bit)) |
| 327 | return; |
| 328 | |
| 329 | bank->dbck_enable_mask &= ~gpio_bit; |
| 330 | bank->context.debounce_en &= ~gpio_bit; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 331 | writel_relaxed(bank->context.debounce_en, |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 332 | bank->base + bank->regs->debounce_en); |
| 333 | |
| 334 | if (!bank->dbck_enable_mask) { |
| 335 | bank->context.debounce = 0; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 336 | writel_relaxed(bank->context.debounce, bank->base + |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 337 | bank->regs->debounce); |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 338 | clk_disable(bank->dbck); |
Jon Hunter | c9c55d9 | 2012-10-26 14:26:04 -0500 | [diff] [blame] | 339 | bank->dbck_enabled = false; |
| 340 | } |
| 341 | } |
| 342 | |
Tony Lindgren | da38ef3 | 2019-03-25 15:43:18 -0700 | [diff] [blame] | 343 | /* |
| 344 | * Off mode wake-up capable GPIOs in bank(s) that are in the wakeup domain. |
| 345 | * See TRM section for GPIO for "Wake-Up Generation" for the list of GPIOs |
| 346 | * in wakeup domain. If bank->non_wakeup_gpios is not configured, assume none |
| 347 | * are capable waking up the system from off mode. |
| 348 | */ |
| 349 | static bool omap_gpio_is_off_wakeup_capable(struct gpio_bank *bank, u32 gpio_mask) |
| 350 | { |
| 351 | u32 no_wake = bank->non_wakeup_gpios; |
| 352 | |
| 353 | if (no_wake) |
| 354 | return !!(~no_wake & gpio_mask); |
| 355 | |
| 356 | return false; |
| 357 | } |
| 358 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 359 | static inline void omap_set_gpio_trigger(struct gpio_bank *bank, int gpio, |
Tarun Kanti DebBarma | 00ece7e | 2011-11-25 15:41:06 +0530 | [diff] [blame] | 360 | unsigned trigger) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 361 | { |
Juha Yrjola | 3ac4fa9 | 2006-12-06 17:13:52 -0800 | [diff] [blame] | 362 | void __iomem *base = bank->base; |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 363 | u32 gpio_bit = BIT(gpio); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 364 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 365 | omap_gpio_rmw(base, bank->regs->leveldetect0, gpio_bit, |
| 366 | trigger & IRQ_TYPE_LEVEL_LOW); |
| 367 | omap_gpio_rmw(base, bank->regs->leveldetect1, gpio_bit, |
| 368 | trigger & IRQ_TYPE_LEVEL_HIGH); |
Russell King | e6818d2 | 2019-04-08 12:46:53 -0700 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * We need the edge detection enabled for to allow the GPIO block |
| 372 | * to be woken from idle state. Set the appropriate edge detection |
| 373 | * in addition to the level detection. |
| 374 | */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 375 | omap_gpio_rmw(base, bank->regs->risingdetect, gpio_bit, |
Russell King | e6818d2 | 2019-04-08 12:46:53 -0700 | [diff] [blame] | 376 | trigger & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH)); |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 377 | omap_gpio_rmw(base, bank->regs->fallingdetect, gpio_bit, |
Russell King | e6818d2 | 2019-04-08 12:46:53 -0700 | [diff] [blame] | 378 | trigger & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)); |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 379 | |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 380 | bank->context.leveldetect0 = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 381 | readl_relaxed(bank->base + bank->regs->leveldetect0); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 382 | bank->context.leveldetect1 = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 383 | readl_relaxed(bank->base + bank->regs->leveldetect1); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 384 | bank->context.risingdetect = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 385 | readl_relaxed(bank->base + bank->regs->risingdetect); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 386 | bank->context.fallingdetect = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 387 | readl_relaxed(bank->base + bank->regs->fallingdetect); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 388 | |
| 389 | if (likely(!(bank->non_wakeup_gpios & gpio_bit))) { |
Tony Lindgren | 00ded24 | 2018-12-07 11:08:29 -0800 | [diff] [blame] | 390 | omap_gpio_rmw(base, bank->regs->wkup_en, gpio_bit, trigger != 0); |
| 391 | bank->context.wake_en = |
| 392 | readl_relaxed(bank->base + bank->regs->wkup_en); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 393 | } |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 394 | |
Ambresh K | 55b220c | 2011-06-15 13:40:45 -0700 | [diff] [blame] | 395 | /* This part needs to be executed always for OMAP{34xx, 44xx} */ |
Tony Lindgren | da38ef3 | 2019-03-25 15:43:18 -0700 | [diff] [blame] | 396 | if (!bank->regs->irqctrl && !omap_gpio_is_off_wakeup_capable(bank, gpio)) { |
Chunqiu Wang | 699117a6 | 2009-06-24 17:13:39 +0000 | [diff] [blame] | 397 | /* |
| 398 | * Log the edge gpio and manually trigger the IRQ |
| 399 | * after resume if the input level changes |
| 400 | * to avoid irq lost during PER RET/OFF mode |
| 401 | * Applies for omap2 non-wakeup gpio and all omap3 gpios |
| 402 | */ |
| 403 | if (trigger & IRQ_TYPE_EDGE_BOTH) |
Juha Yrjola | 3ac4fa9 | 2006-12-06 17:13:52 -0800 | [diff] [blame] | 404 | bank->enabled_non_wakeup_gpios |= gpio_bit; |
| 405 | else |
| 406 | bank->enabled_non_wakeup_gpios &= ~gpio_bit; |
| 407 | } |
Kevin Hilman | 5eb3bb9 | 2007-05-05 11:40:29 -0700 | [diff] [blame] | 408 | |
Tarun Kanti DebBarma | 9ea14d8 | 2011-08-30 15:05:44 +0530 | [diff] [blame] | 409 | bank->level_mask = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 410 | readl_relaxed(bank->base + bank->regs->leveldetect0) | |
| 411 | readl_relaxed(bank->base + bank->regs->leveldetect1); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Uwe Kleine-König | 9198bcd | 2010-01-29 14:20:05 -0800 | [diff] [blame] | 414 | #ifdef CONFIG_ARCH_OMAP1 |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 415 | /* |
| 416 | * This only applies to chips that can't do both rising and falling edge |
| 417 | * detection at once. For all other chips, this function is a noop. |
| 418 | */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 419 | static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 420 | { |
| 421 | void __iomem *reg = bank->base; |
| 422 | u32 l = 0; |
| 423 | |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 424 | if (!bank->regs->irqctrl) |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 425 | return; |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 426 | |
| 427 | reg += bank->regs->irqctrl; |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 428 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 429 | l = readl_relaxed(reg); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 430 | if ((l >> gpio) & 1) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 431 | l &= ~(BIT(gpio)); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 432 | else |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 433 | l |= BIT(gpio); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 434 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 435 | writel_relaxed(l, reg); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 436 | } |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 437 | #else |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 438 | static void omap_toggle_gpio_edge_triggering(struct gpio_bank *bank, int gpio) {} |
Uwe Kleine-König | 9198bcd | 2010-01-29 14:20:05 -0800 | [diff] [blame] | 439 | #endif |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 440 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 441 | static int omap_set_gpio_triggering(struct gpio_bank *bank, int gpio, |
| 442 | unsigned trigger) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 443 | { |
| 444 | void __iomem *reg = bank->base; |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 445 | void __iomem *base = bank->base; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 446 | u32 l = 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 447 | |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 448 | if (bank->regs->leveldetect0 && bank->regs->wkup_en) { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 449 | omap_set_gpio_trigger(bank, gpio, trigger); |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 450 | } else if (bank->regs->irqctrl) { |
| 451 | reg += bank->regs->irqctrl; |
| 452 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 453 | l = readl_relaxed(reg); |
Janusz Krzysztofik | 2950157 | 2010-04-05 11:38:06 +0000 | [diff] [blame] | 454 | if ((trigger & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 455 | bank->toggle_mask |= BIT(gpio); |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 456 | if (trigger & IRQ_TYPE_EDGE_RISING) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 457 | l |= BIT(gpio); |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 458 | else if (trigger & IRQ_TYPE_EDGE_FALLING) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 459 | l &= ~(BIT(gpio)); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 460 | else |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 461 | return -EINVAL; |
| 462 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 463 | writel_relaxed(l, reg); |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 464 | } else if (bank->regs->edgectrl1) { |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 465 | if (gpio & 0x08) |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 466 | reg += bank->regs->edgectrl2; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 467 | else |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 468 | reg += bank->regs->edgectrl1; |
| 469 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 470 | gpio &= 0x07; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 471 | l = readl_relaxed(reg); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 472 | l &= ~(3 << (gpio << 1)); |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 473 | if (trigger & IRQ_TYPE_EDGE_RISING) |
Tony Lindgren | 6e60e79 | 2006-04-02 17:46:23 +0100 | [diff] [blame] | 474 | l |= 2 << (gpio << 1); |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 475 | if (trigger & IRQ_TYPE_EDGE_FALLING) |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 476 | l |= BIT(gpio << 1); |
Tarun Kanti DebBarma | 5e571f3 | 2011-09-13 15:02:14 +0530 | [diff] [blame] | 477 | |
| 478 | /* Enable wake-up during idle for dynamic tick */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 479 | omap_gpio_rmw(base, bank->regs->wkup_en, BIT(gpio), trigger); |
Tarun Kanti DebBarma | 41d87cb | 2011-11-15 12:52:38 +0530 | [diff] [blame] | 480 | bank->context.wake_en = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 481 | readl_relaxed(bank->base + bank->regs->wkup_en); |
| 482 | writel_relaxed(l, reg); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 483 | } |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 484 | return 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 487 | static void omap_enable_gpio_module(struct gpio_bank *bank, unsigned offset) |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 488 | { |
| 489 | if (bank->regs->pinctrl) { |
| 490 | void __iomem *reg = bank->base + bank->regs->pinctrl; |
| 491 | |
| 492 | /* Claim the pin for MPU */ |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 493 | writel_relaxed(readl_relaxed(reg) | (BIT(offset)), reg); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (bank->regs->ctrl && !BANK_USED(bank)) { |
| 497 | void __iomem *reg = bank->base + bank->regs->ctrl; |
| 498 | u32 ctrl; |
| 499 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 500 | ctrl = readl_relaxed(reg); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 501 | /* Module is enabled, clocks are not gated */ |
| 502 | ctrl &= ~GPIO_MOD_CTRL_BIT; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 503 | writel_relaxed(ctrl, reg); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 504 | bank->context.ctrl = ctrl; |
| 505 | } |
| 506 | } |
| 507 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 508 | static void omap_disable_gpio_module(struct gpio_bank *bank, unsigned offset) |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 509 | { |
| 510 | void __iomem *base = bank->base; |
| 511 | |
| 512 | if (bank->regs->wkup_en && |
| 513 | !LINE_USED(bank->mod_usage, offset) && |
| 514 | !LINE_USED(bank->irq_usage, offset)) { |
| 515 | /* Disable wake-up during idle for dynamic tick */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 516 | omap_gpio_rmw(base, bank->regs->wkup_en, BIT(offset), 0); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 517 | bank->context.wake_en = |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 518 | readl_relaxed(bank->base + bank->regs->wkup_en); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | if (bank->regs->ctrl && !BANK_USED(bank)) { |
| 522 | void __iomem *reg = bank->base + bank->regs->ctrl; |
| 523 | u32 ctrl; |
| 524 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 525 | ctrl = readl_relaxed(reg); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 526 | /* Module is disabled, clocks are gated */ |
| 527 | ctrl |= GPIO_MOD_CTRL_BIT; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 528 | writel_relaxed(ctrl, reg); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 529 | bank->context.ctrl = ctrl; |
| 530 | } |
| 531 | } |
| 532 | |
Grygorii Strashko | b2b2004 | 2015-03-23 14:18:23 +0200 | [diff] [blame] | 533 | static int omap_gpio_is_input(struct gpio_bank *bank, unsigned offset) |
Javier Martinez Canillas | fa365e4 | 2013-09-25 02:36:52 +0200 | [diff] [blame] | 534 | { |
| 535 | void __iomem *reg = bank->base + bank->regs->direction; |
| 536 | |
Grygorii Strashko | b2b2004 | 2015-03-23 14:18:23 +0200 | [diff] [blame] | 537 | return readl_relaxed(reg) & BIT(offset); |
Javier Martinez Canillas | fa365e4 | 2013-09-25 02:36:52 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Grygorii Strashko | 37e14ec | 2015-03-23 14:18:26 +0200 | [diff] [blame] | 540 | static void omap_gpio_init_irq(struct gpio_bank *bank, unsigned offset) |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 541 | { |
| 542 | if (!LINE_USED(bank->mod_usage, offset)) { |
| 543 | omap_enable_gpio_module(bank, offset); |
| 544 | omap_set_gpio_direction(bank, offset, 1); |
| 545 | } |
Grygorii Strashko | 37e14ec | 2015-03-23 14:18:26 +0200 | [diff] [blame] | 546 | bank->irq_usage |= BIT(offset); |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 547 | } |
| 548 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 549 | static int omap_gpio_irq_type(struct irq_data *d, unsigned type) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 550 | { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 551 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 552 | int retval; |
David Brownell | a647253 | 2008-03-03 04:33:30 -0800 | [diff] [blame] | 553 | unsigned long flags; |
Grygorii Strashko | ea5fbe8 | 2015-03-23 14:18:29 +0200 | [diff] [blame] | 554 | unsigned offset = d->hwirq; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 555 | |
David Brownell | e5c56ed | 2006-12-06 17:13:59 -0800 | [diff] [blame] | 556 | if (type & ~IRQ_TYPE_SENSE_MASK) |
Tony Lindgren | 6e60e79 | 2006-04-02 17:46:23 +0100 | [diff] [blame] | 557 | return -EINVAL; |
David Brownell | e5c56ed | 2006-12-06 17:13:59 -0800 | [diff] [blame] | 558 | |
Tarun Kanti DebBarma | 9ea14d8 | 2011-08-30 15:05:44 +0530 | [diff] [blame] | 559 | if (!bank->regs->leveldetect0 && |
| 560 | (type & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 561 | return -EINVAL; |
| 562 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 563 | raw_spin_lock_irqsave(&bank->lock, flags); |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 564 | retval = omap_set_gpio_triggering(bank, offset, type); |
Grygorii Strashko | 977bd8a | 2015-06-24 17:54:17 +0300 | [diff] [blame] | 565 | if (retval) { |
Axel Lin | 627c89b | 2015-08-05 22:37:41 +0800 | [diff] [blame] | 566 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Grygorii Strashko | 1562e46 | 2015-05-22 17:35:49 +0300 | [diff] [blame] | 567 | goto error; |
Grygorii Strashko | 977bd8a | 2015-06-24 17:54:17 +0300 | [diff] [blame] | 568 | } |
Grygorii Strashko | 37e14ec | 2015-03-23 14:18:26 +0200 | [diff] [blame] | 569 | omap_gpio_init_irq(bank, offset); |
Grygorii Strashko | b2b2004 | 2015-03-23 14:18:23 +0200 | [diff] [blame] | 570 | if (!omap_gpio_is_input(bank, offset)) { |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 571 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Grygorii Strashko | 1562e46 | 2015-05-22 17:35:49 +0300 | [diff] [blame] | 572 | retval = -EINVAL; |
| 573 | goto error; |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 574 | } |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 575 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Kevin Hilman | 672e302 | 2008-01-16 21:56:16 -0800 | [diff] [blame] | 576 | |
| 577 | if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH)) |
Thomas Gleixner | 43ec2e4 | 2015-06-23 15:52:39 +0200 | [diff] [blame] | 578 | irq_set_handler_locked(d, handle_level_irq); |
Kevin Hilman | 672e302 | 2008-01-16 21:56:16 -0800 | [diff] [blame] | 579 | else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)) |
Grygorii Strashko | 80ac93c | 2017-10-03 11:17:05 -0500 | [diff] [blame] | 580 | /* |
| 581 | * Edge IRQs are already cleared/acked in irq_handler and |
| 582 | * not need to be masked, as result handle_edge_irq() |
| 583 | * logic is excessed here and may cause lose of interrupts. |
| 584 | * So just use handle_simple_irq. |
| 585 | */ |
| 586 | irq_set_handler_locked(d, handle_simple_irq); |
Kevin Hilman | 672e302 | 2008-01-16 21:56:16 -0800 | [diff] [blame] | 587 | |
Grygorii Strashko | 1562e46 | 2015-05-22 17:35:49 +0300 | [diff] [blame] | 588 | return 0; |
| 589 | |
| 590 | error: |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 591 | return retval; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 594 | static void omap_clear_gpio_irqbank(struct gpio_bank *bank, int gpio_mask) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 595 | { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 596 | void __iomem *reg = bank->base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 597 | |
Kevin Hilman | eef4bec | 2011-04-21 09:17:35 -0700 | [diff] [blame] | 598 | reg += bank->regs->irqstatus; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 599 | writel_relaxed(gpio_mask, reg); |
Hiroshi DOYU | bee7930 | 2006-09-25 12:41:46 +0300 | [diff] [blame] | 600 | |
| 601 | /* Workaround for clearing DSP GPIO interrupts to allow retention */ |
Kevin Hilman | eef4bec | 2011-04-21 09:17:35 -0700 | [diff] [blame] | 602 | if (bank->regs->irqstatus2) { |
| 603 | reg = bank->base + bank->regs->irqstatus2; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 604 | writel_relaxed(gpio_mask, reg); |
Kevin Hilman | eef4bec | 2011-04-21 09:17:35 -0700 | [diff] [blame] | 605 | } |
Roger Quadros | bedfd15 | 2009-04-23 11:10:50 -0700 | [diff] [blame] | 606 | |
| 607 | /* Flush posted write for the irq status to avoid spurious interrupts */ |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 608 | readl_relaxed(reg); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 611 | static inline void omap_clear_gpio_irqstatus(struct gpio_bank *bank, |
| 612 | unsigned offset) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 613 | { |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 614 | omap_clear_gpio_irqbank(bank, BIT(offset)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 617 | static u32 omap_get_gpio_irqbank_mask(struct gpio_bank *bank) |
Imre Deak | ea6dedd | 2006-06-26 16:16:00 -0700 | [diff] [blame] | 618 | { |
| 619 | void __iomem *reg = bank->base; |
Imre Deak | 99c4770 | 2006-06-26 16:16:07 -0700 | [diff] [blame] | 620 | u32 l; |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 621 | u32 mask = (BIT(bank->width)) - 1; |
Imre Deak | ea6dedd | 2006-06-26 16:16:00 -0700 | [diff] [blame] | 622 | |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 623 | reg += bank->regs->irqenable; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 624 | l = readl_relaxed(reg); |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 625 | if (bank->regs->irqenable_inv) |
Imre Deak | 99c4770 | 2006-06-26 16:16:07 -0700 | [diff] [blame] | 626 | l = ~l; |
| 627 | l &= mask; |
| 628 | return l; |
Imre Deak | ea6dedd | 2006-06-26 16:16:00 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 631 | static void omap_enable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 632 | { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 633 | void __iomem *reg = bank->base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 634 | u32 l; |
| 635 | |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 636 | if (bank->regs->set_irqenable) { |
| 637 | reg += bank->regs->set_irqenable; |
| 638 | l = gpio_mask; |
Tarun Kanti DebBarma | 2a900eb | 2012-03-06 12:08:16 +0530 | [diff] [blame] | 639 | bank->context.irqenable1 |= gpio_mask; |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 640 | } else { |
| 641 | reg += bank->regs->irqenable; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 642 | l = readl_relaxed(reg); |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 643 | if (bank->regs->irqenable_inv) |
| 644 | l &= ~gpio_mask; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 645 | else |
| 646 | l |= gpio_mask; |
Tarun Kanti DebBarma | 2a900eb | 2012-03-06 12:08:16 +0530 | [diff] [blame] | 647 | bank->context.irqenable1 = l; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 648 | } |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 649 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 650 | writel_relaxed(l, reg); |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 653 | static void omap_disable_gpio_irqbank(struct gpio_bank *bank, int gpio_mask) |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 654 | { |
| 655 | void __iomem *reg = bank->base; |
| 656 | u32 l; |
| 657 | |
| 658 | if (bank->regs->clr_irqenable) { |
| 659 | reg += bank->regs->clr_irqenable; |
| 660 | l = gpio_mask; |
Tarun Kanti DebBarma | 2a900eb | 2012-03-06 12:08:16 +0530 | [diff] [blame] | 661 | bank->context.irqenable1 &= ~gpio_mask; |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 662 | } else { |
| 663 | reg += bank->regs->irqenable; |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 664 | l = readl_relaxed(reg); |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 665 | if (bank->regs->irqenable_inv) |
| 666 | l |= gpio_mask; |
| 667 | else |
| 668 | l &= ~gpio_mask; |
Tarun Kanti DebBarma | 2a900eb | 2012-03-06 12:08:16 +0530 | [diff] [blame] | 669 | bank->context.irqenable1 = l; |
Kevin Hilman | 28f3b5a | 2011-04-21 09:53:06 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 672 | writel_relaxed(l, reg); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 675 | static inline void omap_set_gpio_irqenable(struct gpio_bank *bank, |
| 676 | unsigned offset, int enable) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 677 | { |
Tarun Kanti DebBarma | 8276536c | 2011-11-25 15:27:37 +0530 | [diff] [blame] | 678 | if (enable) |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 679 | omap_enable_gpio_irqbank(bank, BIT(offset)); |
Tarun Kanti DebBarma | 8276536c | 2011-11-25 15:27:37 +0530 | [diff] [blame] | 680 | else |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 681 | omap_disable_gpio_irqbank(bank, BIT(offset)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 684 | /* Use disable_irq_wake() and enable_irq_wake() functions from drivers */ |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 685 | static int omap_gpio_wake_enable(struct irq_data *d, unsigned int enable) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 686 | { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 687 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 688 | |
Grygorii Strashko | 0c0451e | 2016-04-12 13:52:31 +0300 | [diff] [blame] | 689 | return irq_set_irq_wake(bank->irq, enable); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 692 | /* |
| 693 | * We need to unmask the GPIO bank interrupt as soon as possible to |
| 694 | * avoid missing GPIO interrupts for other lines in the bank. |
| 695 | * Then we need to mask-read-clear-unmask the triggered GPIO lines |
| 696 | * in the bank to avoid missing nested interrupts for a GPIO line. |
| 697 | * If we wait to unmask individual GPIO lines in the bank after the |
| 698 | * line's interrupt handler has been run, we may miss some nested |
| 699 | * interrupts. |
| 700 | */ |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 701 | static irqreturn_t omap_gpio_irq_handler(int irq, void *gpiobank) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 702 | { |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 703 | void __iomem *isr_reg = NULL; |
Russell King | 395373c | 2019-06-10 20:10:47 +0300 | [diff] [blame] | 704 | u32 enabled, isr, edge; |
Jon Hunter | 3513cde | 2013-04-04 15:16:14 -0500 | [diff] [blame] | 705 | unsigned int bit; |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 706 | struct gpio_bank *bank = gpiobank; |
| 707 | unsigned long wa_lock_flags; |
Grygorii Strashko | 235f1eb | 2015-08-18 14:10:55 +0300 | [diff] [blame] | 708 | unsigned long lock_flags; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 709 | |
Kevin Hilman | eef4bec | 2011-04-21 09:17:35 -0700 | [diff] [blame] | 710 | isr_reg = bank->base + bank->regs->irqstatus; |
Evgeny Kuznetsov | b1cc4c5 | 2010-12-07 16:25:40 -0800 | [diff] [blame] | 711 | if (WARN_ON(!isr_reg)) |
| 712 | goto exit; |
| 713 | |
Tony Lindgren | 5284521 | 2018-09-20 12:35:32 -0700 | [diff] [blame] | 714 | if (WARN_ONCE(!pm_runtime_active(bank->chip.parent), |
| 715 | "gpio irq%i while runtime suspended?\n", irq)) |
| 716 | return IRQ_NONE; |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 717 | |
Laurent Navet | e83507b | 2013-03-20 13:15:57 +0100 | [diff] [blame] | 718 | while (1) { |
Grygorii Strashko | 235f1eb | 2015-08-18 14:10:55 +0300 | [diff] [blame] | 719 | raw_spin_lock_irqsave(&bank->lock, lock_flags); |
| 720 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 721 | enabled = omap_get_gpio_irqbank_mask(bank); |
Grygorii Strashko | 80ac93c | 2017-10-03 11:17:05 -0500 | [diff] [blame] | 722 | isr = readl_relaxed(isr_reg) & enabled; |
Tony Lindgren | 6e60e79 | 2006-04-02 17:46:23 +0100 | [diff] [blame] | 723 | |
Russell King | 395373c | 2019-06-10 20:10:47 +0300 | [diff] [blame] | 724 | /* |
| 725 | * Clear edge sensitive interrupts before calling handler(s) |
| 726 | * so subsequent edge transitions are not missed while the |
| 727 | * handlers are running. |
| 728 | */ |
| 729 | edge = isr & ~bank->level_mask; |
| 730 | if (edge) |
| 731 | omap_clear_gpio_irqbank(bank, edge); |
Tony Lindgren | 6e60e79 | 2006-04-02 17:46:23 +0100 | [diff] [blame] | 732 | |
Grygorii Strashko | 235f1eb | 2015-08-18 14:10:55 +0300 | [diff] [blame] | 733 | raw_spin_unlock_irqrestore(&bank->lock, lock_flags); |
| 734 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 735 | if (!isr) |
| 736 | break; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 737 | |
Jon Hunter | 3513cde | 2013-04-04 15:16:14 -0500 | [diff] [blame] | 738 | while (isr) { |
| 739 | bit = __ffs(isr); |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 740 | isr &= ~(BIT(bit)); |
Benoit Cousson | 25db711 | 2012-02-23 21:50:10 +0100 | [diff] [blame] | 741 | |
Grygorii Strashko | 235f1eb | 2015-08-18 14:10:55 +0300 | [diff] [blame] | 742 | raw_spin_lock_irqsave(&bank->lock, lock_flags); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 743 | /* |
| 744 | * Some chips can't respond to both rising and falling |
| 745 | * at the same time. If this irq was requested with |
| 746 | * both flags, we need to flip the ICR data for the IRQ |
| 747 | * to respond to the IRQ for the opposite direction. |
| 748 | * This will be indicated in the bank toggle_mask. |
| 749 | */ |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 750 | if (bank->toggle_mask & (BIT(bit))) |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 751 | omap_toggle_gpio_edge_triggering(bank, bit); |
Cory Maccarrone | 4318f36 | 2010-01-08 10:29:04 -0800 | [diff] [blame] | 752 | |
Grygorii Strashko | 235f1eb | 2015-08-18 14:10:55 +0300 | [diff] [blame] | 753 | raw_spin_unlock_irqrestore(&bank->lock, lock_flags); |
| 754 | |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 755 | raw_spin_lock_irqsave(&bank->wa_lock, wa_lock_flags); |
| 756 | |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 757 | generic_handle_irq(irq_find_mapping(bank->chip.irq.domain, |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 758 | bit)); |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 759 | |
| 760 | raw_spin_unlock_irqrestore(&bank->wa_lock, |
| 761 | wa_lock_flags); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 762 | } |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 763 | } |
Evgeny Kuznetsov | b1cc4c5 | 2010-12-07 16:25:40 -0800 | [diff] [blame] | 764 | exit: |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 765 | return IRQ_HANDLED; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 768 | static unsigned int omap_gpio_irq_startup(struct irq_data *d) |
| 769 | { |
| 770 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 771 | unsigned long flags; |
Grygorii Strashko | 37e14ec | 2015-03-23 14:18:26 +0200 | [diff] [blame] | 772 | unsigned offset = d->hwirq; |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 773 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 774 | raw_spin_lock_irqsave(&bank->lock, flags); |
Grygorii Strashko | 121dcb7 | 2015-05-22 17:35:52 +0300 | [diff] [blame] | 775 | |
| 776 | if (!LINE_USED(bank->mod_usage, offset)) |
| 777 | omap_set_gpio_direction(bank, offset, 1); |
| 778 | else if (!omap_gpio_is_input(bank, offset)) |
| 779 | goto err; |
| 780 | omap_enable_gpio_module(bank, offset); |
| 781 | bank->irq_usage |= BIT(offset); |
| 782 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 783 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 784 | omap_gpio_unmask_irq(d); |
| 785 | |
| 786 | return 0; |
Grygorii Strashko | 121dcb7 | 2015-05-22 17:35:52 +0300 | [diff] [blame] | 787 | err: |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 788 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Grygorii Strashko | 121dcb7 | 2015-05-22 17:35:52 +0300 | [diff] [blame] | 789 | return -EINVAL; |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 790 | } |
| 791 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 792 | static void omap_gpio_irq_shutdown(struct irq_data *d) |
Tony Lindgren | 4196dd6 | 2006-09-25 12:41:38 +0300 | [diff] [blame] | 793 | { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 794 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Colin Cross | 85ec7b9 | 2011-06-06 13:38:18 -0700 | [diff] [blame] | 795 | unsigned long flags; |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 796 | unsigned offset = d->hwirq; |
Tony Lindgren | 4196dd6 | 2006-09-25 12:41:38 +0300 | [diff] [blame] | 797 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 798 | raw_spin_lock_irqsave(&bank->lock, flags); |
Javier Martinez Canillas | b1e9fec | 2014-04-27 02:00:49 +0200 | [diff] [blame] | 799 | bank->irq_usage &= ~(BIT(offset)); |
Grygorii Strashko | 6e96c1b | 2015-05-22 17:35:50 +0300 | [diff] [blame] | 800 | omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE); |
Russell King | c859e0d | 2019-06-10 20:10:44 +0300 | [diff] [blame] | 801 | omap_clear_gpio_irqstatus(bank, offset); |
| 802 | omap_set_gpio_irqenable(bank, offset, 0); |
Grygorii Strashko | 6e96c1b | 2015-05-22 17:35:50 +0300 | [diff] [blame] | 803 | if (!LINE_USED(bank->mod_usage, offset)) |
| 804 | omap_clear_gpio_debounce(bank, offset); |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 805 | omap_disable_gpio_module(bank, offset); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 806 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Grygorii Strashko | aca82d1 | 2015-09-25 12:28:02 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static void omap_gpio_irq_bus_lock(struct irq_data *data) |
| 810 | { |
| 811 | struct gpio_bank *bank = omap_irq_data_get_bank(data); |
| 812 | |
Grygorii Strashko | 4674807 | 2018-09-28 16:39:50 -0500 | [diff] [blame] | 813 | pm_runtime_get_sync(bank->chip.parent); |
Grygorii Strashko | aca82d1 | 2015-09-25 12:28:02 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void gpio_irq_bus_sync_unlock(struct irq_data *data) |
| 817 | { |
| 818 | struct gpio_bank *bank = omap_irq_data_get_bank(data); |
Javier Martinez Canillas | fac7fa1 | 2013-09-25 02:36:54 +0200 | [diff] [blame] | 819 | |
Grygorii Strashko | 4674807 | 2018-09-28 16:39:50 -0500 | [diff] [blame] | 820 | pm_runtime_put(bank->chip.parent); |
Tony Lindgren | 4196dd6 | 2006-09-25 12:41:38 +0300 | [diff] [blame] | 821 | } |
| 822 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 823 | static void omap_gpio_mask_irq(struct irq_data *d) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 824 | { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 825 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 826 | unsigned offset = d->hwirq; |
Colin Cross | 85ec7b9 | 2011-06-06 13:38:18 -0700 | [diff] [blame] | 827 | unsigned long flags; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 828 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 829 | raw_spin_lock_irqsave(&bank->lock, flags); |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 830 | omap_set_gpio_triggering(bank, offset, IRQ_TYPE_NONE); |
Russell King | c859e0d | 2019-06-10 20:10:44 +0300 | [diff] [blame] | 831 | omap_set_gpio_irqenable(bank, offset, 0); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 832 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 833 | } |
| 834 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 835 | static void omap_gpio_unmask_irq(struct irq_data *d) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 836 | { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 837 | struct gpio_bank *bank = omap_irq_data_get_bank(d); |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 838 | unsigned offset = d->hwirq; |
Thomas Gleixner | 8c04a17 | 2011-03-24 12:40:15 +0100 | [diff] [blame] | 839 | u32 trigger = irqd_get_trigger_type(d); |
Colin Cross | 85ec7b9 | 2011-06-06 13:38:18 -0700 | [diff] [blame] | 840 | unsigned long flags; |
Kevin Hilman | 55b6019 | 2009-06-04 15:57:10 -0700 | [diff] [blame] | 841 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 842 | raw_spin_lock_irqsave(&bank->lock, flags); |
Grygorii Strashko | 9943f26 | 2015-03-23 14:18:27 +0200 | [diff] [blame] | 843 | omap_set_gpio_irqenable(bank, offset, 1); |
Russell King | d01849f | 2019-03-01 11:02:52 -0800 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * For level-triggered GPIOs, clearing must be done after the source |
| 847 | * is cleared, thus after the handler has run. OMAP4 needs this done |
| 848 | * after enabing the interrupt to clear the wakeup status. |
| 849 | */ |
Russell King | c859e0d | 2019-06-10 20:10:44 +0300 | [diff] [blame] | 850 | if (bank->regs->leveldetect0 && bank->regs->wkup_en && |
| 851 | trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) |
Russell King | d01849f | 2019-03-01 11:02:52 -0800 | [diff] [blame] | 852 | omap_clear_gpio_irqstatus(bank, offset); |
| 853 | |
Russell King | c859e0d | 2019-06-10 20:10:44 +0300 | [diff] [blame] | 854 | if (trigger) |
| 855 | omap_set_gpio_triggering(bank, offset, trigger); |
| 856 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 857 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 858 | } |
| 859 | |
David Brownell | e5c56ed | 2006-12-06 17:13:59 -0800 | [diff] [blame] | 860 | /*---------------------------------------------------------------------*/ |
| 861 | |
Magnus Damm | 79ee031 | 2009-07-08 13:22:04 +0200 | [diff] [blame] | 862 | static int omap_mpuio_suspend_noirq(struct device *dev) |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 863 | { |
Wolfram Sang | a3f4f72 | 2018-10-21 21:59:59 +0200 | [diff] [blame] | 864 | struct gpio_bank *bank = dev_get_drvdata(dev); |
Tony Lindgren | 5de62b8 | 2010-12-07 16:26:58 -0800 | [diff] [blame] | 865 | void __iomem *mask_reg = bank->base + |
| 866 | OMAP_MPUIO_GPIO_MASKIT / bank->stride; |
David Brownell | a647253 | 2008-03-03 04:33:30 -0800 | [diff] [blame] | 867 | unsigned long flags; |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 868 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 869 | raw_spin_lock_irqsave(&bank->lock, flags); |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 870 | writel_relaxed(0xffff & ~bank->context.wake_en, mask_reg); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 871 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 872 | |
| 873 | return 0; |
| 874 | } |
| 875 | |
Magnus Damm | 79ee031 | 2009-07-08 13:22:04 +0200 | [diff] [blame] | 876 | static int omap_mpuio_resume_noirq(struct device *dev) |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 877 | { |
Wolfram Sang | a3f4f72 | 2018-10-21 21:59:59 +0200 | [diff] [blame] | 878 | struct gpio_bank *bank = dev_get_drvdata(dev); |
Tony Lindgren | 5de62b8 | 2010-12-07 16:26:58 -0800 | [diff] [blame] | 879 | void __iomem *mask_reg = bank->base + |
| 880 | OMAP_MPUIO_GPIO_MASKIT / bank->stride; |
David Brownell | a647253 | 2008-03-03 04:33:30 -0800 | [diff] [blame] | 881 | unsigned long flags; |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 882 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 883 | raw_spin_lock_irqsave(&bank->lock, flags); |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 884 | writel_relaxed(bank->context.wake_en, mask_reg); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 885 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 890 | static const struct dev_pm_ops omap_mpuio_dev_pm_ops = { |
Magnus Damm | 79ee031 | 2009-07-08 13:22:04 +0200 | [diff] [blame] | 891 | .suspend_noirq = omap_mpuio_suspend_noirq, |
| 892 | .resume_noirq = omap_mpuio_resume_noirq, |
| 893 | }; |
| 894 | |
Rafael J. Wysocki | 3c437ff | 2011-04-22 22:02:46 +0200 | [diff] [blame] | 895 | /* use platform_driver for this. */ |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 896 | static struct platform_driver omap_mpuio_driver = { |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 897 | .driver = { |
| 898 | .name = "mpuio", |
Magnus Damm | 79ee031 | 2009-07-08 13:22:04 +0200 | [diff] [blame] | 899 | .pm = &omap_mpuio_dev_pm_ops, |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 900 | }, |
| 901 | }; |
| 902 | |
| 903 | static struct platform_device omap_mpuio_device = { |
| 904 | .name = "mpuio", |
| 905 | .id = -1, |
| 906 | .dev = { |
| 907 | .driver = &omap_mpuio_driver.driver, |
| 908 | } |
| 909 | /* could list the /proc/iomem resources */ |
| 910 | }; |
| 911 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 912 | static inline void omap_mpuio_init(struct gpio_bank *bank) |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 913 | { |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 914 | platform_set_drvdata(&omap_mpuio_device, bank); |
David Brownell | fcf126d | 2007-04-02 12:46:47 -0700 | [diff] [blame] | 915 | |
David Brownell | 11a78b7 | 2006-12-06 17:14:11 -0800 | [diff] [blame] | 916 | if (platform_driver_register(&omap_mpuio_driver) == 0) |
| 917 | (void) platform_device_register(&omap_mpuio_device); |
| 918 | } |
| 919 | |
David Brownell | e5c56ed | 2006-12-06 17:13:59 -0800 | [diff] [blame] | 920 | /*---------------------------------------------------------------------*/ |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 921 | |
Russell King | dfbc6c7 | 2019-06-10 20:10:49 +0300 | [diff] [blame^] | 922 | static int omap_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 923 | { |
| 924 | struct gpio_bank *bank = gpiochip_get_data(chip); |
| 925 | unsigned long flags; |
| 926 | |
| 927 | pm_runtime_get_sync(chip->parent); |
| 928 | |
| 929 | raw_spin_lock_irqsave(&bank->lock, flags); |
| 930 | omap_enable_gpio_module(bank, offset); |
| 931 | bank->mod_usage |= BIT(offset); |
| 932 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static void omap_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 938 | { |
| 939 | struct gpio_bank *bank = gpiochip_get_data(chip); |
| 940 | unsigned long flags; |
| 941 | |
| 942 | raw_spin_lock_irqsave(&bank->lock, flags); |
| 943 | bank->mod_usage &= ~(BIT(offset)); |
| 944 | if (!LINE_USED(bank->irq_usage, offset)) { |
| 945 | omap_set_gpio_direction(bank, offset, 1); |
| 946 | omap_clear_gpio_debounce(bank, offset); |
| 947 | } |
| 948 | omap_disable_gpio_module(bank, offset); |
| 949 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 950 | |
| 951 | pm_runtime_put(chip->parent); |
| 952 | } |
| 953 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 954 | static int omap_gpio_get_direction(struct gpio_chip *chip, unsigned offset) |
Yegor Yefremov | 9370084 | 2014-04-24 08:57:39 +0200 | [diff] [blame] | 955 | { |
| 956 | struct gpio_bank *bank; |
| 957 | unsigned long flags; |
| 958 | void __iomem *reg; |
| 959 | int dir; |
| 960 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 961 | bank = gpiochip_get_data(chip); |
Yegor Yefremov | 9370084 | 2014-04-24 08:57:39 +0200 | [diff] [blame] | 962 | reg = bank->base + bank->regs->direction; |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 963 | raw_spin_lock_irqsave(&bank->lock, flags); |
Yegor Yefremov | 9370084 | 2014-04-24 08:57:39 +0200 | [diff] [blame] | 964 | dir = !!(readl_relaxed(reg) & BIT(offset)); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 965 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Yegor Yefremov | 9370084 | 2014-04-24 08:57:39 +0200 | [diff] [blame] | 966 | return dir; |
| 967 | } |
| 968 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 969 | static int omap_gpio_input(struct gpio_chip *chip, unsigned offset) |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 970 | { |
| 971 | struct gpio_bank *bank; |
| 972 | unsigned long flags; |
| 973 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 974 | bank = gpiochip_get_data(chip); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 975 | raw_spin_lock_irqsave(&bank->lock, flags); |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 976 | omap_set_gpio_direction(bank, offset, 1); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 977 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 981 | static int omap_gpio_get(struct gpio_chip *chip, unsigned offset) |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 982 | { |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 983 | struct gpio_bank *bank; |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 984 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 985 | bank = gpiochip_get_data(chip); |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 986 | |
Grygorii Strashko | b2b2004 | 2015-03-23 14:18:23 +0200 | [diff] [blame] | 987 | if (omap_gpio_is_input(bank, offset)) |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 988 | return omap_get_gpio_datain(bank, offset); |
Roger Quadros | b37c45b | 2009-08-05 16:53:24 +0300 | [diff] [blame] | 989 | else |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 990 | return omap_get_gpio_dataout(bank, offset); |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 993 | static int omap_gpio_output(struct gpio_chip *chip, unsigned offset, int value) |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 994 | { |
| 995 | struct gpio_bank *bank; |
| 996 | unsigned long flags; |
| 997 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 998 | bank = gpiochip_get_data(chip); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 999 | raw_spin_lock_irqsave(&bank->lock, flags); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 1000 | bank->set_dataout(bank, offset, value); |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1001 | omap_set_gpio_direction(bank, offset, 0); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1002 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Javier Martinez Canillas | 2f56e0a | 2013-10-16 02:47:30 +0200 | [diff] [blame] | 1003 | return 0; |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1006 | static int omap_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask, |
| 1007 | unsigned long *bits) |
| 1008 | { |
| 1009 | struct gpio_bank *bank = gpiochip_get_data(chip); |
| 1010 | void __iomem *reg = bank->base + bank->regs->direction; |
| 1011 | unsigned long in = readl_relaxed(reg), l; |
| 1012 | |
| 1013 | *bits = 0; |
| 1014 | |
| 1015 | l = in & *mask; |
| 1016 | if (l) |
| 1017 | *bits |= omap_get_gpio_datain_multiple(bank, &l); |
| 1018 | |
| 1019 | l = ~in & *mask; |
| 1020 | if (l) |
| 1021 | *bits |= omap_get_gpio_dataout_multiple(bank, &l); |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1026 | static int omap_gpio_debounce(struct gpio_chip *chip, unsigned offset, |
| 1027 | unsigned debounce) |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1028 | { |
| 1029 | struct gpio_bank *bank; |
| 1030 | unsigned long flags; |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 1031 | int ret; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1032 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 1033 | bank = gpiochip_get_data(chip); |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1034 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1035 | raw_spin_lock_irqsave(&bank->lock, flags); |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 1036 | ret = omap2_set_gpio_debounce(bank, offset, debounce); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1037 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1038 | |
David Rivshin | 8397744 | 2017-04-24 18:56:50 -0400 | [diff] [blame] | 1039 | if (ret) |
| 1040 | dev_info(chip->parent, |
| 1041 | "Could not set line %u debounce to %u microseconds (%d)", |
| 1042 | offset, debounce, ret); |
| 1043 | |
| 1044 | return ret; |
Felipe Balbi | 168ef3d | 2010-05-26 14:42:23 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 1047 | static int omap_gpio_set_config(struct gpio_chip *chip, unsigned offset, |
| 1048 | unsigned long config) |
| 1049 | { |
| 1050 | u32 debounce; |
| 1051 | |
| 1052 | if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE) |
| 1053 | return -ENOTSUPP; |
| 1054 | |
| 1055 | debounce = pinconf_to_config_argument(config); |
| 1056 | return omap_gpio_debounce(chip, offset, debounce); |
| 1057 | } |
| 1058 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1059 | static void omap_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 1060 | { |
| 1061 | struct gpio_bank *bank; |
| 1062 | unsigned long flags; |
| 1063 | |
Linus Walleij | d99f7ae | 2015-12-07 11:16:00 +0100 | [diff] [blame] | 1064 | bank = gpiochip_get_data(chip); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1065 | raw_spin_lock_irqsave(&bank->lock, flags); |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 1066 | bank->set_dataout(bank, offset, value); |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1067 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1070 | static void omap_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask, |
| 1071 | unsigned long *bits) |
| 1072 | { |
| 1073 | struct gpio_bank *bank = gpiochip_get_data(chip); |
| 1074 | unsigned long flags; |
| 1075 | |
| 1076 | raw_spin_lock_irqsave(&bank->lock, flags); |
| 1077 | bank->set_dataout_multiple(bank, mask, bits); |
| 1078 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 1079 | } |
| 1080 | |
David Brownell | 52e3134 | 2008-03-03 12:43:23 -0800 | [diff] [blame] | 1081 | /*---------------------------------------------------------------------*/ |
| 1082 | |
Arnd Bergmann | e4b2ae7 | 2017-09-16 22:42:21 +0200 | [diff] [blame] | 1083 | static void omap_gpio_show_rev(struct gpio_bank *bank) |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1084 | { |
Kevin Hilman | e5ff444 | 2011-04-22 14:37:16 -0700 | [diff] [blame] | 1085 | static bool called; |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1086 | u32 rev; |
| 1087 | |
Kevin Hilman | e5ff444 | 2011-04-22 14:37:16 -0700 | [diff] [blame] | 1088 | if (called || bank->regs->revision == USHRT_MAX) |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1089 | return; |
| 1090 | |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 1091 | rev = readw_relaxed(bank->base + bank->regs->revision); |
Kevin Hilman | e5ff444 | 2011-04-22 14:37:16 -0700 | [diff] [blame] | 1092 | pr_info("OMAP GPIO hardware version %d.%d\n", |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1093 | (rev >> 4) & 0x0f, rev & 0x0f); |
Kevin Hilman | e5ff444 | 2011-04-22 14:37:16 -0700 | [diff] [blame] | 1094 | |
| 1095 | called = true; |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Charulatha V | 03e128c | 2011-05-05 19:58:01 +0530 | [diff] [blame] | 1098 | static void omap_gpio_mod_init(struct gpio_bank *bank) |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1099 | { |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1100 | void __iomem *base = bank->base; |
| 1101 | u32 l = 0xffffffff; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1102 | |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1103 | if (bank->width == 16) |
| 1104 | l = 0xffff; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1105 | |
Charulatha V | d0d665a | 2011-08-31 00:02:21 +0530 | [diff] [blame] | 1106 | if (bank->is_mpuio) { |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 1107 | writel_relaxed(l, bank->base + bank->regs->irqenable); |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1108 | return; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1109 | } |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1110 | |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1111 | omap_gpio_rmw(base, bank->regs->irqenable, l, |
| 1112 | bank->regs->irqenable_inv); |
| 1113 | omap_gpio_rmw(base, bank->regs->irqstatus, l, |
| 1114 | !bank->regs->irqenable_inv); |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1115 | if (bank->regs->debounce_en) |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 1116 | writel_relaxed(0, base + bank->regs->debounce_en); |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1117 | |
Tarun Kanti DebBarma | 2dc983c | 2011-11-24 02:44:29 +0530 | [diff] [blame] | 1118 | /* Save OE default value (0xffffffff) in the context */ |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 1119 | bank->context.oe = readl_relaxed(bank->base + bank->regs->direction); |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1120 | /* Initialize interface clk ungated, module enabled */ |
| 1121 | if (bank->regs->ctrl) |
Victor Kamensky | 661553b | 2013-11-16 02:01:04 +0200 | [diff] [blame] | 1122 | writel_relaxed(0, base + bank->regs->ctrl); |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1125 | static int omap_gpio_chip_init(struct gpio_bank *bank, struct irq_chip *irqc) |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1126 | { |
Grygorii Strashko | 8193032 | 2017-11-15 12:36:33 -0600 | [diff] [blame] | 1127 | struct gpio_irq_chip *irq; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1128 | static int gpio; |
Linus Walleij | 088413b | 2017-12-29 13:22:58 +0100 | [diff] [blame] | 1129 | const char *label; |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1130 | int irq_base = 0; |
Javier Martinez Canillas | 6ef7f38 | 2014-04-06 16:58:14 +0200 | [diff] [blame] | 1131 | int ret; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1132 | |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1133 | /* |
| 1134 | * REVISIT eventually switch from OMAP-specific gpio structs |
| 1135 | * over to the generic ones |
| 1136 | */ |
| 1137 | bank->chip.request = omap_gpio_request; |
| 1138 | bank->chip.free = omap_gpio_free; |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1139 | bank->chip.get_direction = omap_gpio_get_direction; |
| 1140 | bank->chip.direction_input = omap_gpio_input; |
| 1141 | bank->chip.get = omap_gpio_get; |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1142 | bank->chip.get_multiple = omap_gpio_get_multiple; |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1143 | bank->chip.direction_output = omap_gpio_output; |
Mika Westerberg | 2956b5d | 2017-01-23 15:34:34 +0300 | [diff] [blame] | 1144 | bank->chip.set_config = omap_gpio_set_config; |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1145 | bank->chip.set = omap_gpio_set; |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1146 | bank->chip.set_multiple = omap_gpio_set_multiple; |
Charulatha V | d0d665a | 2011-08-31 00:02:21 +0530 | [diff] [blame] | 1147 | if (bank->is_mpuio) { |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1148 | bank->chip.label = "mpuio"; |
Tarun Kanti DebBarma | 6ed87c5 | 2011-09-13 14:41:44 +0530 | [diff] [blame] | 1149 | if (bank->regs->wkup_en) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1150 | bank->chip.parent = &omap_mpuio_device.dev; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1151 | bank->chip.base = OMAP_MPUIO(0); |
| 1152 | } else { |
Linus Walleij | 088413b | 2017-12-29 13:22:58 +0100 | [diff] [blame] | 1153 | label = devm_kasprintf(bank->chip.parent, GFP_KERNEL, "gpio-%d-%d", |
| 1154 | gpio, gpio + bank->width - 1); |
| 1155 | if (!label) |
| 1156 | return -ENOMEM; |
| 1157 | bank->chip.label = label; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1158 | bank->chip.base = gpio; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1159 | } |
Kevin Hilman | d5f4624 | 2011-04-21 09:23:00 -0700 | [diff] [blame] | 1160 | bank->chip.ngpio = bank->width; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1161 | |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1162 | #ifdef CONFIG_ARCH_OMAP1 |
| 1163 | /* |
| 1164 | * REVISIT: Once we have OMAP1 supporting SPARSE_IRQ, we can drop |
| 1165 | * irq_alloc_descs() since a base IRQ offset will no longer be needed. |
| 1166 | */ |
Bartosz Golaszewski | 2ed36f3 | 2017-03-04 17:23:31 +0100 | [diff] [blame] | 1167 | irq_base = devm_irq_alloc_descs(bank->chip.parent, |
| 1168 | -1, 0, bank->width, 0); |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1169 | if (irq_base < 0) { |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1170 | dev_err(bank->chip.parent, "Couldn't allocate IRQ numbers\n"); |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1171 | return -ENODEV; |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
Tony Lindgren | d2d05c6 | 2015-04-23 16:54:17 -0700 | [diff] [blame] | 1175 | /* MPUIO is a bit different, reading IRQ status clears it */ |
Russell King | 693de83 | 2019-06-10 20:10:48 +0300 | [diff] [blame] | 1176 | if (bank->is_mpuio && !bank->regs->wkup_en) |
| 1177 | irqc->irq_set_wake = NULL; |
Tony Lindgren | d2d05c6 | 2015-04-23 16:54:17 -0700 | [diff] [blame] | 1178 | |
Grygorii Strashko | 8193032 | 2017-11-15 12:36:33 -0600 | [diff] [blame] | 1179 | irq = &bank->chip.irq; |
| 1180 | irq->chip = irqc; |
| 1181 | irq->handler = handle_bad_irq; |
| 1182 | irq->default_type = IRQ_TYPE_NONE; |
| 1183 | irq->num_parents = 1; |
| 1184 | irq->parents = &bank->irq; |
| 1185 | irq->first = irq_base; |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1186 | |
Grygorii Strashko | 8193032 | 2017-11-15 12:36:33 -0600 | [diff] [blame] | 1187 | ret = gpiochip_add_data(&bank->chip, bank); |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1188 | if (ret) { |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1189 | dev_err(bank->chip.parent, |
Grygorii Strashko | 8193032 | 2017-11-15 12:36:33 -0600 | [diff] [blame] | 1190 | "Could not register gpio chip %d\n", ret); |
| 1191 | return ret; |
Javier Martinez Canillas | fb655f5 | 2014-04-06 16:58:16 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1194 | ret = devm_request_irq(bank->chip.parent, bank->irq, |
| 1195 | omap_gpio_irq_handler, |
| 1196 | 0, dev_name(bank->chip.parent), bank); |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 1197 | if (ret) |
| 1198 | gpiochip_remove(&bank->chip); |
| 1199 | |
Grygorii Strashko | 8193032 | 2017-11-15 12:36:33 -0600 | [diff] [blame] | 1200 | if (!bank->is_mpuio) |
| 1201 | gpio += bank->width; |
| 1202 | |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 1203 | return ret; |
Varadarajan, Charulatha | 2fae7fb | 2010-12-07 16:26:55 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1206 | static void omap_gpio_init_context(struct gpio_bank *p) |
| 1207 | { |
| 1208 | struct omap_gpio_reg_offs *regs = p->regs; |
| 1209 | void __iomem *base = p->base; |
| 1210 | |
| 1211 | p->context.ctrl = readl_relaxed(base + regs->ctrl); |
| 1212 | p->context.oe = readl_relaxed(base + regs->direction); |
| 1213 | p->context.wake_en = readl_relaxed(base + regs->wkup_en); |
| 1214 | p->context.leveldetect0 = readl_relaxed(base + regs->leveldetect0); |
| 1215 | p->context.leveldetect1 = readl_relaxed(base + regs->leveldetect1); |
| 1216 | p->context.risingdetect = readl_relaxed(base + regs->risingdetect); |
| 1217 | p->context.fallingdetect = readl_relaxed(base + regs->fallingdetect); |
| 1218 | p->context.irqenable1 = readl_relaxed(base + regs->irqenable); |
| 1219 | p->context.irqenable2 = readl_relaxed(base + regs->irqenable2); |
| 1220 | |
| 1221 | if (regs->set_dataout && p->regs->clr_dataout) |
| 1222 | p->context.dataout = readl_relaxed(base + regs->set_dataout); |
| 1223 | else |
| 1224 | p->context.dataout = readl_relaxed(base + regs->dataout); |
| 1225 | |
| 1226 | p->context_valid = true; |
| 1227 | } |
| 1228 | |
| 1229 | static void omap_gpio_restore_context(struct gpio_bank *bank) |
| 1230 | { |
| 1231 | writel_relaxed(bank->context.wake_en, |
| 1232 | bank->base + bank->regs->wkup_en); |
| 1233 | writel_relaxed(bank->context.ctrl, bank->base + bank->regs->ctrl); |
| 1234 | writel_relaxed(bank->context.leveldetect0, |
| 1235 | bank->base + bank->regs->leveldetect0); |
| 1236 | writel_relaxed(bank->context.leveldetect1, |
| 1237 | bank->base + bank->regs->leveldetect1); |
| 1238 | writel_relaxed(bank->context.risingdetect, |
| 1239 | bank->base + bank->regs->risingdetect); |
| 1240 | writel_relaxed(bank->context.fallingdetect, |
| 1241 | bank->base + bank->regs->fallingdetect); |
| 1242 | if (bank->regs->set_dataout && bank->regs->clr_dataout) |
| 1243 | writel_relaxed(bank->context.dataout, |
| 1244 | bank->base + bank->regs->set_dataout); |
| 1245 | else |
| 1246 | writel_relaxed(bank->context.dataout, |
| 1247 | bank->base + bank->regs->dataout); |
| 1248 | writel_relaxed(bank->context.oe, bank->base + bank->regs->direction); |
| 1249 | |
| 1250 | if (bank->dbck_enable_mask) { |
| 1251 | writel_relaxed(bank->context.debounce, bank->base + |
| 1252 | bank->regs->debounce); |
| 1253 | writel_relaxed(bank->context.debounce_en, |
| 1254 | bank->base + bank->regs->debounce_en); |
| 1255 | } |
| 1256 | |
| 1257 | writel_relaxed(bank->context.irqenable1, |
| 1258 | bank->base + bank->regs->irqenable); |
| 1259 | writel_relaxed(bank->context.irqenable2, |
| 1260 | bank->base + bank->regs->irqenable2); |
| 1261 | } |
| 1262 | |
| 1263 | static void omap_gpio_idle(struct gpio_bank *bank, bool may_lose_context) |
| 1264 | { |
| 1265 | struct device *dev = bank->chip.parent; |
Tony Lindgren | 21e2118 | 2019-03-25 15:43:16 -0700 | [diff] [blame] | 1266 | void __iomem *base = bank->base; |
| 1267 | u32 nowake; |
| 1268 | |
| 1269 | bank->saved_datain = readl_relaxed(base + bank->regs->datain); |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1270 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1271 | if (!bank->enabled_non_wakeup_gpios) |
| 1272 | goto update_gpio_context_count; |
| 1273 | |
| 1274 | if (!may_lose_context) |
| 1275 | goto update_gpio_context_count; |
| 1276 | |
| 1277 | /* |
Tony Lindgren | 21e2118 | 2019-03-25 15:43:16 -0700 | [diff] [blame] | 1278 | * If going to OFF, remove triggering for all wkup domain |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1279 | * non-wakeup GPIOs. Otherwise spurious IRQs will be |
| 1280 | * generated. See OMAP2420 Errata item 1.101. |
| 1281 | */ |
Tony Lindgren | 21e2118 | 2019-03-25 15:43:16 -0700 | [diff] [blame] | 1282 | if (!bank->loses_context && bank->enabled_non_wakeup_gpios) { |
| 1283 | nowake = bank->enabled_non_wakeup_gpios; |
| 1284 | omap_gpio_rmw(base, bank->regs->fallingdetect, nowake, ~nowake); |
| 1285 | omap_gpio_rmw(base, bank->regs->risingdetect, nowake, ~nowake); |
| 1286 | } |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1287 | |
| 1288 | update_gpio_context_count: |
| 1289 | if (bank->get_context_loss_count) |
| 1290 | bank->context_loss_count = |
| 1291 | bank->get_context_loss_count(dev); |
| 1292 | |
| 1293 | omap_gpio_dbck_disable(bank); |
| 1294 | } |
| 1295 | |
| 1296 | static void omap_gpio_unidle(struct gpio_bank *bank) |
| 1297 | { |
| 1298 | struct device *dev = bank->chip.parent; |
| 1299 | u32 l = 0, gen, gen0, gen1; |
| 1300 | int c; |
| 1301 | |
| 1302 | /* |
| 1303 | * On the first resume during the probe, the context has not |
| 1304 | * been initialised and so initialise it now. Also initialise |
| 1305 | * the context loss count. |
| 1306 | */ |
| 1307 | if (bank->loses_context && !bank->context_valid) { |
| 1308 | omap_gpio_init_context(bank); |
| 1309 | |
| 1310 | if (bank->get_context_loss_count) |
| 1311 | bank->context_loss_count = |
| 1312 | bank->get_context_loss_count(dev); |
| 1313 | } |
| 1314 | |
| 1315 | omap_gpio_dbck_enable(bank); |
| 1316 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1317 | if (bank->loses_context) { |
| 1318 | if (!bank->get_context_loss_count) { |
| 1319 | omap_gpio_restore_context(bank); |
| 1320 | } else { |
| 1321 | c = bank->get_context_loss_count(dev); |
| 1322 | if (c != bank->context_loss_count) { |
| 1323 | omap_gpio_restore_context(bank); |
| 1324 | } else { |
| 1325 | return; |
| 1326 | } |
| 1327 | } |
Tony Lindgren | 21e2118 | 2019-03-25 15:43:16 -0700 | [diff] [blame] | 1328 | } else { |
| 1329 | /* Restore changes done for OMAP2420 errata 1.101 */ |
| 1330 | writel_relaxed(bank->context.fallingdetect, |
| 1331 | bank->base + bank->regs->fallingdetect); |
| 1332 | writel_relaxed(bank->context.risingdetect, |
| 1333 | bank->base + bank->regs->risingdetect); |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1336 | l = readl_relaxed(bank->base + bank->regs->datain); |
| 1337 | |
| 1338 | /* |
| 1339 | * Check if any of the non-wakeup interrupt GPIOs have changed |
| 1340 | * state. If so, generate an IRQ by software. This is |
| 1341 | * horribly racy, but it's the best we can do to work around |
| 1342 | * this silicon bug. |
| 1343 | */ |
| 1344 | l ^= bank->saved_datain; |
| 1345 | l &= bank->enabled_non_wakeup_gpios; |
| 1346 | |
| 1347 | /* |
| 1348 | * No need to generate IRQs for the rising edge for gpio IRQs |
| 1349 | * configured with falling edge only; and vice versa. |
| 1350 | */ |
| 1351 | gen0 = l & bank->context.fallingdetect; |
| 1352 | gen0 &= bank->saved_datain; |
| 1353 | |
| 1354 | gen1 = l & bank->context.risingdetect; |
| 1355 | gen1 &= ~(bank->saved_datain); |
| 1356 | |
| 1357 | /* FIXME: Consider GPIO IRQs with level detections properly! */ |
| 1358 | gen = l & (~(bank->context.fallingdetect) & |
| 1359 | ~(bank->context.risingdetect)); |
| 1360 | /* Consider all GPIO IRQs needed to be updated */ |
| 1361 | gen |= gen0 | gen1; |
| 1362 | |
| 1363 | if (gen) { |
| 1364 | u32 old0, old1; |
| 1365 | |
| 1366 | old0 = readl_relaxed(bank->base + bank->regs->leveldetect0); |
| 1367 | old1 = readl_relaxed(bank->base + bank->regs->leveldetect1); |
| 1368 | |
| 1369 | if (!bank->regs->irqstatus_raw0) { |
| 1370 | writel_relaxed(old0 | gen, bank->base + |
| 1371 | bank->regs->leveldetect0); |
| 1372 | writel_relaxed(old1 | gen, bank->base + |
| 1373 | bank->regs->leveldetect1); |
| 1374 | } |
| 1375 | |
| 1376 | if (bank->regs->irqstatus_raw0) { |
| 1377 | writel_relaxed(old0 | l, bank->base + |
| 1378 | bank->regs->leveldetect0); |
| 1379 | writel_relaxed(old1 | l, bank->base + |
| 1380 | bank->regs->leveldetect1); |
| 1381 | } |
| 1382 | writel_relaxed(old0, bank->base + bank->regs->leveldetect0); |
| 1383 | writel_relaxed(old1, bank->base + bank->regs->leveldetect1); |
| 1384 | } |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1385 | } |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1386 | |
| 1387 | static int gpio_omap_cpu_notifier(struct notifier_block *nb, |
| 1388 | unsigned long cmd, void *v) |
| 1389 | { |
| 1390 | struct gpio_bank *bank; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1391 | unsigned long flags; |
| 1392 | |
| 1393 | bank = container_of(nb, struct gpio_bank, nb); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1394 | |
| 1395 | raw_spin_lock_irqsave(&bank->lock, flags); |
| 1396 | switch (cmd) { |
| 1397 | case CPU_CLUSTER_PM_ENTER: |
| 1398 | if (bank->is_suspended) |
| 1399 | break; |
| 1400 | omap_gpio_idle(bank, true); |
| 1401 | break; |
| 1402 | case CPU_CLUSTER_PM_ENTER_FAILED: |
| 1403 | case CPU_CLUSTER_PM_EXIT: |
| 1404 | if (bank->is_suspended) |
| 1405 | break; |
| 1406 | omap_gpio_unidle(bank); |
| 1407 | break; |
| 1408 | } |
| 1409 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 1410 | |
| 1411 | return NOTIFY_OK; |
| 1412 | } |
| 1413 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1414 | static struct omap_gpio_reg_offs omap2_gpio_regs = { |
| 1415 | .revision = OMAP24XX_GPIO_REVISION, |
| 1416 | .direction = OMAP24XX_GPIO_OE, |
| 1417 | .datain = OMAP24XX_GPIO_DATAIN, |
| 1418 | .dataout = OMAP24XX_GPIO_DATAOUT, |
| 1419 | .set_dataout = OMAP24XX_GPIO_SETDATAOUT, |
| 1420 | .clr_dataout = OMAP24XX_GPIO_CLEARDATAOUT, |
| 1421 | .irqstatus = OMAP24XX_GPIO_IRQSTATUS1, |
| 1422 | .irqstatus2 = OMAP24XX_GPIO_IRQSTATUS2, |
| 1423 | .irqenable = OMAP24XX_GPIO_IRQENABLE1, |
| 1424 | .irqenable2 = OMAP24XX_GPIO_IRQENABLE2, |
| 1425 | .set_irqenable = OMAP24XX_GPIO_SETIRQENABLE1, |
| 1426 | .clr_irqenable = OMAP24XX_GPIO_CLEARIRQENABLE1, |
| 1427 | .debounce = OMAP24XX_GPIO_DEBOUNCE_VAL, |
| 1428 | .debounce_en = OMAP24XX_GPIO_DEBOUNCE_EN, |
| 1429 | .ctrl = OMAP24XX_GPIO_CTRL, |
| 1430 | .wkup_en = OMAP24XX_GPIO_WAKE_EN, |
| 1431 | .leveldetect0 = OMAP24XX_GPIO_LEVELDETECT0, |
| 1432 | .leveldetect1 = OMAP24XX_GPIO_LEVELDETECT1, |
| 1433 | .risingdetect = OMAP24XX_GPIO_RISINGDETECT, |
| 1434 | .fallingdetect = OMAP24XX_GPIO_FALLINGDETECT, |
| 1435 | }; |
| 1436 | |
| 1437 | static struct omap_gpio_reg_offs omap4_gpio_regs = { |
| 1438 | .revision = OMAP4_GPIO_REVISION, |
| 1439 | .direction = OMAP4_GPIO_OE, |
| 1440 | .datain = OMAP4_GPIO_DATAIN, |
| 1441 | .dataout = OMAP4_GPIO_DATAOUT, |
| 1442 | .set_dataout = OMAP4_GPIO_SETDATAOUT, |
| 1443 | .clr_dataout = OMAP4_GPIO_CLEARDATAOUT, |
| 1444 | .irqstatus = OMAP4_GPIO_IRQSTATUS0, |
| 1445 | .irqstatus2 = OMAP4_GPIO_IRQSTATUS1, |
Russell King | 64ea3e9 | 2019-06-10 20:10:45 +0300 | [diff] [blame] | 1446 | .irqstatus_raw0 = OMAP4_GPIO_IRQSTATUSRAW0, |
| 1447 | .irqstatus_raw1 = OMAP4_GPIO_IRQSTATUSRAW1, |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1448 | .irqenable = OMAP4_GPIO_IRQSTATUSSET0, |
| 1449 | .irqenable2 = OMAP4_GPIO_IRQSTATUSSET1, |
| 1450 | .set_irqenable = OMAP4_GPIO_IRQSTATUSSET0, |
| 1451 | .clr_irqenable = OMAP4_GPIO_IRQSTATUSCLR0, |
| 1452 | .debounce = OMAP4_GPIO_DEBOUNCINGTIME, |
| 1453 | .debounce_en = OMAP4_GPIO_DEBOUNCENABLE, |
| 1454 | .ctrl = OMAP4_GPIO_CTRL, |
| 1455 | .wkup_en = OMAP4_GPIO_IRQWAKEN0, |
| 1456 | .leveldetect0 = OMAP4_GPIO_LEVELDETECT0, |
| 1457 | .leveldetect1 = OMAP4_GPIO_LEVELDETECT1, |
| 1458 | .risingdetect = OMAP4_GPIO_RISINGDETECT, |
| 1459 | .fallingdetect = OMAP4_GPIO_FALLINGDETECT, |
| 1460 | }; |
| 1461 | |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1462 | static const struct omap_gpio_platform_data omap2_pdata = { |
| 1463 | .regs = &omap2_gpio_regs, |
| 1464 | .bank_width = 32, |
| 1465 | .dbck_flag = false, |
| 1466 | }; |
| 1467 | |
| 1468 | static const struct omap_gpio_platform_data omap3_pdata = { |
| 1469 | .regs = &omap2_gpio_regs, |
| 1470 | .bank_width = 32, |
| 1471 | .dbck_flag = true, |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1472 | }; |
| 1473 | |
| 1474 | static const struct omap_gpio_platform_data omap4_pdata = { |
| 1475 | .regs = &omap4_gpio_regs, |
| 1476 | .bank_width = 32, |
| 1477 | .dbck_flag = true, |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1478 | }; |
| 1479 | |
| 1480 | static const struct of_device_id omap_gpio_match[] = { |
| 1481 | { |
| 1482 | .compatible = "ti,omap4-gpio", |
| 1483 | .data = &omap4_pdata, |
| 1484 | }, |
| 1485 | { |
| 1486 | .compatible = "ti,omap3-gpio", |
| 1487 | .data = &omap3_pdata, |
| 1488 | }, |
| 1489 | { |
| 1490 | .compatible = "ti,omap2-gpio", |
| 1491 | .data = &omap2_pdata, |
| 1492 | }, |
| 1493 | { }, |
| 1494 | }; |
| 1495 | MODULE_DEVICE_TABLE(of, omap_gpio_match); |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1496 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 1497 | static int omap_gpio_probe(struct platform_device *pdev) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1498 | { |
Benoit Cousson | 862ff64 | 2012-02-01 15:58:56 +0100 | [diff] [blame] | 1499 | struct device *dev = &pdev->dev; |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1500 | struct device_node *node = dev->of_node; |
| 1501 | const struct of_device_id *match; |
Uwe Kleine-König | f6817a2 | 2012-05-21 21:57:39 +0200 | [diff] [blame] | 1502 | const struct omap_gpio_platform_data *pdata; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1503 | struct gpio_bank *bank; |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1504 | struct irq_chip *irqc; |
Javier Martinez Canillas | 6ef7f38 | 2014-04-06 16:58:14 +0200 | [diff] [blame] | 1505 | int ret; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1506 | |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1507 | match = of_match_device(of_match_ptr(omap_gpio_match), dev); |
| 1508 | |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 1509 | pdata = match ? match->data : dev_get_platdata(dev); |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1510 | if (!pdata) |
Benoit Cousson | 96751fc | 2012-02-01 16:01:39 +0100 | [diff] [blame] | 1511 | return -EINVAL; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1512 | |
Markus Elfring | f97364c | 2018-02-10 21:49:22 +0100 | [diff] [blame] | 1513 | bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL); |
Markus Elfring | 9117d40 | 2018-02-10 21:46:30 +0100 | [diff] [blame] | 1514 | if (!bank) |
Benoit Cousson | 96751fc | 2012-02-01 16:01:39 +0100 | [diff] [blame] | 1515 | return -ENOMEM; |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1516 | |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1517 | irqc = devm_kzalloc(dev, sizeof(*irqc), GFP_KERNEL); |
| 1518 | if (!irqc) |
| 1519 | return -ENOMEM; |
| 1520 | |
Tony Lindgren | 3d009c8 | 2015-01-16 14:50:50 -0800 | [diff] [blame] | 1521 | irqc->irq_startup = omap_gpio_irq_startup, |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1522 | irqc->irq_shutdown = omap_gpio_irq_shutdown, |
Russell King | 693de83 | 2019-06-10 20:10:48 +0300 | [diff] [blame] | 1523 | irqc->irq_ack = dummy_irq_chip.irq_ack, |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1524 | irqc->irq_mask = omap_gpio_mask_irq, |
| 1525 | irqc->irq_unmask = omap_gpio_unmask_irq, |
| 1526 | irqc->irq_set_type = omap_gpio_irq_type, |
| 1527 | irqc->irq_set_wake = omap_gpio_wake_enable, |
Grygorii Strashko | aca82d1 | 2015-09-25 12:28:02 -0700 | [diff] [blame] | 1528 | irqc->irq_bus_lock = omap_gpio_irq_bus_lock, |
| 1529 | irqc->irq_bus_sync_unlock = gpio_irq_bus_sync_unlock, |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1530 | irqc->name = dev_name(&pdev->dev); |
Grygorii Strashko | 0c0451e | 2016-04-12 13:52:31 +0300 | [diff] [blame] | 1531 | irqc->flags = IRQCHIP_MASK_ON_SUSPEND; |
Grygorii Strashko | 4674807 | 2018-09-28 16:39:50 -0500 | [diff] [blame] | 1532 | irqc->parent_device = dev; |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1533 | |
Grygorii Strashko | 89d18e3 | 2015-08-18 14:10:53 +0300 | [diff] [blame] | 1534 | bank->irq = platform_get_irq(pdev, 0); |
| 1535 | if (bank->irq <= 0) { |
| 1536 | if (!bank->irq) |
| 1537 | bank->irq = -ENXIO; |
| 1538 | if (bank->irq != -EPROBE_DEFER) |
| 1539 | dev_err(dev, |
| 1540 | "can't get irq resource ret=%d\n", bank->irq); |
| 1541 | return bank->irq; |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 1544 | bank->chip.parent = dev; |
Grygorii Strashko | c23837c | 2015-06-25 18:13:33 +0300 | [diff] [blame] | 1545 | bank->chip.owner = THIS_MODULE; |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1546 | bank->dbck_flag = pdata->dbck_flag; |
Tony Lindgren | 5de62b8 | 2010-12-07 16:26:58 -0800 | [diff] [blame] | 1547 | bank->stride = pdata->bank_stride; |
Kevin Hilman | d5f4624 | 2011-04-21 09:23:00 -0700 | [diff] [blame] | 1548 | bank->width = pdata->bank_width; |
Charulatha V | d0d665a | 2011-08-31 00:02:21 +0530 | [diff] [blame] | 1549 | bank->is_mpuio = pdata->is_mpuio; |
Charulatha V | 803a243 | 2011-05-05 17:04:12 +0530 | [diff] [blame] | 1550 | bank->non_wakeup_gpios = pdata->non_wakeup_gpios; |
Kevin Hilman | fa87931a | 2011-04-20 16:31:23 -0700 | [diff] [blame] | 1551 | bank->regs = pdata->regs; |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1552 | #ifdef CONFIG_OF_GPIO |
| 1553 | bank->chip.of_node = of_node_get(node); |
| 1554 | #endif |
Tony Lindgren | ec0daae | 2018-09-20 12:35:30 -0700 | [diff] [blame] | 1555 | |
Jon Hunter | a2797be | 2013-04-04 15:16:15 -0500 | [diff] [blame] | 1556 | if (node) { |
| 1557 | if (!of_property_read_bool(node, "ti,gpio-always-on")) |
| 1558 | bank->loses_context = true; |
| 1559 | } else { |
| 1560 | bank->loses_context = pdata->loses_context; |
Jon Hunter | 352a2d5 | 2013-04-15 13:06:54 -0500 | [diff] [blame] | 1561 | |
| 1562 | if (bank->loses_context) |
| 1563 | bank->get_context_loss_count = |
| 1564 | pdata->get_context_loss_count; |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1567 | if (bank->regs->set_dataout && bank->regs->clr_dataout) { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1568 | bank->set_dataout = omap_set_gpio_dataout_reg; |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1569 | bank->set_dataout_multiple = omap_set_gpio_dataout_reg_multiple; |
| 1570 | } else { |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1571 | bank->set_dataout = omap_set_gpio_dataout_mask; |
Janusz Krzysztofik | 442af14 | 2018-07-19 01:57:08 +0200 | [diff] [blame] | 1572 | bank->set_dataout_multiple = |
| 1573 | omap_set_gpio_dataout_mask_multiple; |
| 1574 | } |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1575 | |
Sebastian Andrzej Siewior | 4dbada2 | 2015-07-21 18:26:51 +0200 | [diff] [blame] | 1576 | raw_spin_lock_init(&bank->lock); |
Grygorii Strashko | 450fa54 | 2015-09-25 12:28:03 -0700 | [diff] [blame] | 1577 | raw_spin_lock_init(&bank->wa_lock); |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1578 | |
| 1579 | /* Static mapping, never released */ |
Enrico Weigelt, metux IT consult | 58f57f8 | 2019-03-11 20:50:05 +0100 | [diff] [blame] | 1580 | bank->base = devm_platform_ioremap_resource(pdev, 0); |
Jingoo Han | 717f70e | 2014-02-12 11:51:38 +0900 | [diff] [blame] | 1581 | if (IS_ERR(bank->base)) { |
Jingoo Han | 717f70e | 2014-02-12 11:51:38 +0900 | [diff] [blame] | 1582 | return PTR_ERR(bank->base); |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 1585 | if (bank->dbck_flag) { |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1586 | bank->dbck = devm_clk_get(dev, "dbclk"); |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 1587 | if (IS_ERR(bank->dbck)) { |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1588 | dev_err(dev, |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 1589 | "Could not get gpio dbck. Disable debounce\n"); |
| 1590 | bank->dbck_flag = false; |
| 1591 | } else { |
| 1592 | clk_prepare(bank->dbck); |
| 1593 | } |
| 1594 | } |
| 1595 | |
Tarun Kanti DebBarma | 065cd79 | 2011-11-24 01:48:52 +0530 | [diff] [blame] | 1596 | platform_set_drvdata(pdev, bank); |
| 1597 | |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1598 | pm_runtime_enable(dev); |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1599 | pm_runtime_get_sync(dev); |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1600 | |
Charulatha V | d0d665a | 2011-08-31 00:02:21 +0530 | [diff] [blame] | 1601 | if (bank->is_mpuio) |
Javier Martinez Canillas | a0e827c | 2014-06-27 22:17:37 +0200 | [diff] [blame] | 1602 | omap_mpuio_init(bank); |
Tarun Kanti DebBarma | ab985f0 | 2011-09-13 15:12:05 +0530 | [diff] [blame] | 1603 | |
Charulatha V | 03e128c | 2011-05-05 19:58:01 +0530 | [diff] [blame] | 1604 | omap_gpio_mod_init(bank); |
Javier Martinez Canillas | 6ef7f38 | 2014-04-06 16:58:14 +0200 | [diff] [blame] | 1605 | |
Nishanth Menon | 46824e22 | 2014-09-05 14:52:55 -0500 | [diff] [blame] | 1606 | ret = omap_gpio_chip_init(bank, irqc); |
Tony Lindgren | 5e606ab | 2015-08-28 11:44:49 -0700 | [diff] [blame] | 1607 | if (ret) { |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1608 | pm_runtime_put_sync(dev); |
| 1609 | pm_runtime_disable(dev); |
Arvind Yadav | e2c3c19 | 2017-08-01 12:14:31 +0530 | [diff] [blame] | 1610 | if (bank->dbck_flag) |
| 1611 | clk_unprepare(bank->dbck); |
Javier Martinez Canillas | 6ef7f38 | 2014-04-06 16:58:14 +0200 | [diff] [blame] | 1612 | return ret; |
Tony Lindgren | 5e606ab | 2015-08-28 11:44:49 -0700 | [diff] [blame] | 1613 | } |
Javier Martinez Canillas | 6ef7f38 | 2014-04-06 16:58:14 +0200 | [diff] [blame] | 1614 | |
Tony Lindgren | 9a74805 | 2010-12-07 16:26:56 -0800 | [diff] [blame] | 1615 | omap_gpio_show_rev(bank); |
Tony Lindgren | 9f7065d | 2009-10-19 15:25:20 -0700 | [diff] [blame] | 1616 | |
Russell King | e6818d2 | 2019-04-08 12:46:53 -0700 | [diff] [blame] | 1617 | bank->nb.notifier_call = gpio_omap_cpu_notifier; |
| 1618 | cpu_pm_register_notifier(&bank->nb); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1619 | |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1620 | pm_runtime_put(dev); |
Tarun Kanti DebBarma | 55b93c3 | 2011-09-29 07:23:22 +0530 | [diff] [blame] | 1621 | |
Jon Hunter | 879fe32 | 2013-04-04 15:16:12 -0500 | [diff] [blame] | 1622 | return 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Tony Lindgren | cac089f | 2015-04-23 16:56:22 -0700 | [diff] [blame] | 1625 | static int omap_gpio_remove(struct platform_device *pdev) |
| 1626 | { |
| 1627 | struct gpio_bank *bank = platform_get_drvdata(pdev); |
| 1628 | |
Russell King | e6818d2 | 2019-04-08 12:46:53 -0700 | [diff] [blame] | 1629 | cpu_pm_unregister_notifier(&bank->nb); |
Tony Lindgren | cac089f | 2015-04-23 16:56:22 -0700 | [diff] [blame] | 1630 | gpiochip_remove(&bank->chip); |
Grygorii Strashko | 7b1e5dc | 2016-03-04 17:25:35 +0200 | [diff] [blame] | 1631 | pm_runtime_disable(&pdev->dev); |
Grygorii Strashko | 5d9452e | 2015-08-18 14:10:56 +0300 | [diff] [blame] | 1632 | if (bank->dbck_flag) |
| 1633 | clk_unprepare(bank->dbck); |
Tony Lindgren | cac089f | 2015-04-23 16:56:22 -0700 | [diff] [blame] | 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1638 | static int __maybe_unused omap_gpio_runtime_suspend(struct device *dev) |
| 1639 | { |
Wolfram Sang | a3f4f72 | 2018-10-21 21:59:59 +0200 | [diff] [blame] | 1640 | struct gpio_bank *bank = dev_get_drvdata(dev); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1641 | unsigned long flags; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1642 | |
| 1643 | raw_spin_lock_irqsave(&bank->lock, flags); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1644 | omap_gpio_idle(bank, true); |
| 1645 | bank->is_suspended = true; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1646 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 1647 | |
Russell King | 044e499 | 2019-04-10 12:51:13 -0700 | [diff] [blame] | 1648 | return 0; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static int __maybe_unused omap_gpio_runtime_resume(struct device *dev) |
| 1652 | { |
Wolfram Sang | a3f4f72 | 2018-10-21 21:59:59 +0200 | [diff] [blame] | 1653 | struct gpio_bank *bank = dev_get_drvdata(dev); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1654 | unsigned long flags; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1655 | |
| 1656 | raw_spin_lock_irqsave(&bank->lock, flags); |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1657 | omap_gpio_unidle(bank); |
| 1658 | bank->is_suspended = false; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1659 | raw_spin_unlock_irqrestore(&bank->lock, flags); |
| 1660 | |
Russell King | 044e499 | 2019-04-10 12:51:13 -0700 | [diff] [blame] | 1661 | return 0; |
Tony Lindgren | b764a58 | 2018-09-20 12:35:31 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Tarun Kanti DebBarma | 55b93c3 | 2011-09-29 07:23:22 +0530 | [diff] [blame] | 1664 | static const struct dev_pm_ops gpio_pm_ops = { |
Tarun Kanti DebBarma | 2dc983c | 2011-11-24 02:44:29 +0530 | [diff] [blame] | 1665 | SET_RUNTIME_PM_OPS(omap_gpio_runtime_suspend, omap_gpio_runtime_resume, |
| 1666 | NULL) |
Tarun Kanti DebBarma | 55b93c3 | 2011-09-29 07:23:22 +0530 | [diff] [blame] | 1667 | }; |
Benoit Cousson | 384ebe1 | 2011-08-16 11:53:02 +0200 | [diff] [blame] | 1668 | |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1669 | static struct platform_driver omap_gpio_driver = { |
| 1670 | .probe = omap_gpio_probe, |
Tony Lindgren | cac089f | 2015-04-23 16:56:22 -0700 | [diff] [blame] | 1671 | .remove = omap_gpio_remove, |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1672 | .driver = { |
| 1673 | .name = "omap_gpio", |
Tarun Kanti DebBarma | 55b93c3 | 2011-09-29 07:23:22 +0530 | [diff] [blame] | 1674 | .pm = &gpio_pm_ops, |
Arnd Bergmann | 7c68571 | 2019-03-07 11:33:32 +0100 | [diff] [blame] | 1675 | .of_match_table = omap_gpio_match, |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1676 | }, |
| 1677 | }; |
| 1678 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1679 | /* |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1680 | * gpio driver register needs to be done before |
| 1681 | * machine_init functions access gpio APIs. |
| 1682 | * Hence omap_gpio_drv_reg() is a postcore_initcall. |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1683 | */ |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1684 | static int __init omap_gpio_drv_reg(void) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1685 | { |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1686 | return platform_driver_register(&omap_gpio_driver); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1687 | } |
Varadarajan, Charulatha | 77640aa | 2010-12-07 16:26:57 -0800 | [diff] [blame] | 1688 | postcore_initcall(omap_gpio_drv_reg); |
Tony Lindgren | cac089f | 2015-04-23 16:56:22 -0700 | [diff] [blame] | 1689 | |
| 1690 | static void __exit omap_gpio_exit(void) |
| 1691 | { |
| 1692 | platform_driver_unregister(&omap_gpio_driver); |
| 1693 | } |
| 1694 | module_exit(omap_gpio_exit); |
| 1695 | |
| 1696 | MODULE_DESCRIPTION("omap gpio driver"); |
| 1697 | MODULE_ALIAS("platform:gpio-omap"); |
| 1698 | MODULE_LICENSE("GPL v2"); |