Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR71XX/AR724X/AR913X GPIO API support |
| 3 | * |
Alban Bedel | 28be55d | 2016-01-28 20:44:33 +0100 | [diff] [blame] | 4 | * Copyright (C) 2015 Alban Bedel <albeu@free.fr> |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 5 | * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com> |
| 6 | * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 7 | * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License version 2 as published |
| 11 | * by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 15 | #include <linux/platform_data/gpio-ath79.h> |
| 16 | #include <linux/of_device.h> |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Paul Gortmaker | 2034b9d | 2016-09-12 18:16:28 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 19 | #include <linux/irq.h> |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 20 | |
Alban Bedel | 409d878 | 2016-01-28 20:44:30 +0100 | [diff] [blame] | 21 | #define AR71XX_GPIO_REG_OE 0x00 |
| 22 | #define AR71XX_GPIO_REG_IN 0x04 |
| 23 | #define AR71XX_GPIO_REG_SET 0x0c |
| 24 | #define AR71XX_GPIO_REG_CLEAR 0x10 |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 25 | |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 26 | #define AR71XX_GPIO_REG_INT_ENABLE 0x14 |
| 27 | #define AR71XX_GPIO_REG_INT_TYPE 0x18 |
| 28 | #define AR71XX_GPIO_REG_INT_POLARITY 0x1c |
| 29 | #define AR71XX_GPIO_REG_INT_PENDING 0x20 |
| 30 | #define AR71XX_GPIO_REG_INT_MASK 0x24 |
| 31 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 32 | struct ath79_gpio_ctrl { |
Alban Bedel | ab32770 | 2016-01-28 20:44:29 +0100 | [diff] [blame] | 33 | struct gpio_chip gc; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 34 | void __iomem *base; |
| 35 | spinlock_t lock; |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 36 | unsigned long both_edges; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 37 | }; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 38 | |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 39 | static struct ath79_gpio_ctrl *irq_data_to_ath79_gpio(struct irq_data *data) |
| 40 | { |
| 41 | struct gpio_chip *gc = irq_data_get_irq_chip_data(data); |
| 42 | |
| 43 | return container_of(gc, struct ath79_gpio_ctrl, gc); |
| 44 | } |
| 45 | |
| 46 | static u32 ath79_gpio_read(struct ath79_gpio_ctrl *ctrl, unsigned reg) |
| 47 | { |
| 48 | return readl(ctrl->base + reg); |
| 49 | } |
| 50 | |
| 51 | static void ath79_gpio_write(struct ath79_gpio_ctrl *ctrl, |
| 52 | unsigned reg, u32 val) |
| 53 | { |
| 54 | return writel(val, ctrl->base + reg); |
| 55 | } |
| 56 | |
| 57 | static bool ath79_gpio_update_bits( |
| 58 | struct ath79_gpio_ctrl *ctrl, unsigned reg, u32 mask, u32 bits) |
| 59 | { |
| 60 | u32 old_val, new_val; |
| 61 | |
| 62 | old_val = ath79_gpio_read(ctrl, reg); |
| 63 | new_val = (old_val & ~mask) | (bits & mask); |
| 64 | |
| 65 | if (new_val != old_val) |
| 66 | ath79_gpio_write(ctrl, reg, new_val); |
| 67 | |
| 68 | return new_val != old_val; |
| 69 | } |
| 70 | |
| 71 | static void ath79_gpio_irq_unmask(struct irq_data *data) |
| 72 | { |
| 73 | struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data); |
| 74 | u32 mask = BIT(irqd_to_hwirq(data)); |
| 75 | unsigned long flags; |
| 76 | |
| 77 | spin_lock_irqsave(&ctrl->lock, flags); |
| 78 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask); |
| 79 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 80 | } |
| 81 | |
| 82 | static void ath79_gpio_irq_mask(struct irq_data *data) |
| 83 | { |
| 84 | struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data); |
| 85 | u32 mask = BIT(irqd_to_hwirq(data)); |
| 86 | unsigned long flags; |
| 87 | |
| 88 | spin_lock_irqsave(&ctrl->lock, flags); |
| 89 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0); |
| 90 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 91 | } |
| 92 | |
| 93 | static void ath79_gpio_irq_enable(struct irq_data *data) |
| 94 | { |
| 95 | struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data); |
| 96 | u32 mask = BIT(irqd_to_hwirq(data)); |
| 97 | unsigned long flags; |
| 98 | |
| 99 | spin_lock_irqsave(&ctrl->lock, flags); |
| 100 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask); |
| 101 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, mask); |
| 102 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 103 | } |
| 104 | |
| 105 | static void ath79_gpio_irq_disable(struct irq_data *data) |
| 106 | { |
| 107 | struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data); |
| 108 | u32 mask = BIT(irqd_to_hwirq(data)); |
| 109 | unsigned long flags; |
| 110 | |
| 111 | spin_lock_irqsave(&ctrl->lock, flags); |
| 112 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_MASK, mask, 0); |
| 113 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0); |
| 114 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 115 | } |
| 116 | |
| 117 | static int ath79_gpio_irq_set_type(struct irq_data *data, |
| 118 | unsigned int flow_type) |
| 119 | { |
| 120 | struct ath79_gpio_ctrl *ctrl = irq_data_to_ath79_gpio(data); |
| 121 | u32 mask = BIT(irqd_to_hwirq(data)); |
| 122 | u32 type = 0, polarity = 0; |
| 123 | unsigned long flags; |
| 124 | bool disabled; |
| 125 | |
| 126 | switch (flow_type) { |
| 127 | case IRQ_TYPE_EDGE_RISING: |
| 128 | polarity |= mask; |
| 129 | case IRQ_TYPE_EDGE_FALLING: |
| 130 | case IRQ_TYPE_EDGE_BOTH: |
| 131 | break; |
| 132 | |
| 133 | case IRQ_TYPE_LEVEL_HIGH: |
| 134 | polarity |= mask; |
| 135 | case IRQ_TYPE_LEVEL_LOW: |
| 136 | type |= mask; |
| 137 | break; |
| 138 | |
| 139 | default: |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | spin_lock_irqsave(&ctrl->lock, flags); |
| 144 | |
| 145 | if (flow_type == IRQ_TYPE_EDGE_BOTH) { |
| 146 | ctrl->both_edges |= mask; |
| 147 | polarity = ~ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN); |
| 148 | } else { |
| 149 | ctrl->both_edges &= ~mask; |
| 150 | } |
| 151 | |
| 152 | /* As the IRQ configuration can't be loaded atomically we |
| 153 | * have to disable the interrupt while the configuration state |
| 154 | * is invalid. |
| 155 | */ |
| 156 | disabled = ath79_gpio_update_bits( |
| 157 | ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, 0); |
| 158 | |
| 159 | ath79_gpio_update_bits( |
| 160 | ctrl, AR71XX_GPIO_REG_INT_TYPE, mask, type); |
| 161 | ath79_gpio_update_bits( |
| 162 | ctrl, AR71XX_GPIO_REG_INT_POLARITY, mask, polarity); |
| 163 | |
| 164 | if (disabled) |
| 165 | ath79_gpio_update_bits( |
| 166 | ctrl, AR71XX_GPIO_REG_INT_ENABLE, mask, mask); |
| 167 | |
| 168 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static struct irq_chip ath79_gpio_irqchip = { |
| 174 | .name = "gpio-ath79", |
| 175 | .irq_enable = ath79_gpio_irq_enable, |
| 176 | .irq_disable = ath79_gpio_irq_disable, |
| 177 | .irq_mask = ath79_gpio_irq_mask, |
| 178 | .irq_unmask = ath79_gpio_irq_unmask, |
| 179 | .irq_set_type = ath79_gpio_irq_set_type, |
| 180 | }; |
| 181 | |
| 182 | static void ath79_gpio_irq_handler(struct irq_desc *desc) |
| 183 | { |
| 184 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
| 185 | struct irq_chip *irqchip = irq_desc_get_chip(desc); |
| 186 | struct ath79_gpio_ctrl *ctrl = |
| 187 | container_of(gc, struct ath79_gpio_ctrl, gc); |
| 188 | unsigned long flags, pending; |
| 189 | u32 both_edges, state; |
| 190 | int irq; |
| 191 | |
| 192 | chained_irq_enter(irqchip, desc); |
| 193 | |
| 194 | spin_lock_irqsave(&ctrl->lock, flags); |
| 195 | |
| 196 | pending = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_INT_PENDING); |
| 197 | |
| 198 | /* Update the polarity of the both edges irqs */ |
| 199 | both_edges = ctrl->both_edges & pending; |
| 200 | if (both_edges) { |
| 201 | state = ath79_gpio_read(ctrl, AR71XX_GPIO_REG_IN); |
| 202 | ath79_gpio_update_bits(ctrl, AR71XX_GPIO_REG_INT_POLARITY, |
| 203 | both_edges, ~state); |
| 204 | } |
| 205 | |
| 206 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 207 | |
| 208 | if (pending) { |
| 209 | for_each_set_bit(irq, &pending, gc->ngpio) |
| 210 | generic_handle_irq( |
| 211 | irq_linear_revmap(gc->irqdomain, irq)); |
| 212 | } |
| 213 | |
| 214 | chained_irq_exit(irqchip, desc); |
| 215 | } |
| 216 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 217 | static const struct of_device_id ath79_gpio_of_match[] = { |
| 218 | { .compatible = "qca,ar7100-gpio" }, |
| 219 | { .compatible = "qca,ar9340-gpio" }, |
| 220 | {}, |
| 221 | }; |
| 222 | |
| 223 | static int ath79_gpio_probe(struct platform_device *pdev) |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 224 | { |
Nizam Haider | ab128af | 2015-11-23 20:53:18 +0530 | [diff] [blame] | 225 | struct ath79_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 226 | struct device_node *np = pdev->dev.of_node; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 227 | struct ath79_gpio_ctrl *ctrl; |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 228 | struct resource *res; |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 229 | u32 ath79_gpio_count; |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 230 | bool oe_inverted; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 231 | int err; |
| 232 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 233 | ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL); |
| 234 | if (!ctrl) |
| 235 | return -ENOMEM; |
Alban Bedel | 2f890cf | 2016-01-28 20:44:31 +0100 | [diff] [blame] | 236 | platform_set_drvdata(pdev, ctrl); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 237 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 238 | if (np) { |
| 239 | err = of_property_read_u32(np, "ngpios", &ath79_gpio_count); |
| 240 | if (err) { |
| 241 | dev_err(&pdev->dev, "ngpios property is not valid\n"); |
| 242 | return err; |
| 243 | } |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 244 | oe_inverted = of_device_is_compatible(np, "qca,ar9340-gpio"); |
| 245 | } else if (pdata) { |
| 246 | ath79_gpio_count = pdata->ngpios; |
| 247 | oe_inverted = pdata->oe_inverted; |
| 248 | } else { |
| 249 | dev_err(&pdev->dev, "No DT node or platform data found\n"); |
| 250 | return -EINVAL; |
| 251 | } |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 252 | |
Axel Lin | f0d3c72 | 2016-02-20 09:48:07 +0800 | [diff] [blame] | 253 | if (ath79_gpio_count >= 32) { |
| 254 | dev_err(&pdev->dev, "ngpios must be less than 32\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 258 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 259 | ctrl->base = devm_ioremap_nocache( |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 260 | &pdev->dev, res->start, resource_size(res)); |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 261 | if (!ctrl->base) |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 262 | return -ENOMEM; |
| 263 | |
Alban Bedel | 49a5bd8 | 2015-09-01 11:38:02 +0200 | [diff] [blame] | 264 | spin_lock_init(&ctrl->lock); |
Alban Bedel | ab32770 | 2016-01-28 20:44:29 +0100 | [diff] [blame] | 265 | err = bgpio_init(&ctrl->gc, &pdev->dev, 4, |
| 266 | ctrl->base + AR71XX_GPIO_REG_IN, |
| 267 | ctrl->base + AR71XX_GPIO_REG_SET, |
| 268 | ctrl->base + AR71XX_GPIO_REG_CLEAR, |
| 269 | oe_inverted ? NULL : ctrl->base + AR71XX_GPIO_REG_OE, |
| 270 | oe_inverted ? ctrl->base + AR71XX_GPIO_REG_OE : NULL, |
| 271 | 0); |
| 272 | if (err) { |
| 273 | dev_err(&pdev->dev, "bgpio_init failed\n"); |
| 274 | return err; |
Gabor Juhos | 5b5b544 | 2012-03-14 10:45:23 +0100 | [diff] [blame] | 275 | } |
Alban Bedel | ab32770 | 2016-01-28 20:44:29 +0100 | [diff] [blame] | 276 | /* Use base 0 to stay compatible with legacy platforms */ |
| 277 | ctrl->gc.base = 0; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 278 | |
Alban Bedel | ab32770 | 2016-01-28 20:44:29 +0100 | [diff] [blame] | 279 | err = gpiochip_add_data(&ctrl->gc, ctrl); |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 280 | if (err) { |
| 281 | dev_err(&pdev->dev, |
| 282 | "cannot add AR71xx GPIO chip, error=%d", err); |
| 283 | return err; |
| 284 | } |
| 285 | |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 286 | if (np && !of_property_read_bool(np, "interrupt-controller")) |
| 287 | return 0; |
| 288 | |
| 289 | err = gpiochip_irqchip_add(&ctrl->gc, &ath79_gpio_irqchip, 0, |
| 290 | handle_simple_irq, IRQ_TYPE_NONE); |
| 291 | if (err) { |
| 292 | dev_err(&pdev->dev, "failed to add gpiochip_irqchip\n"); |
| 293 | goto gpiochip_remove; |
| 294 | } |
| 295 | |
| 296 | gpiochip_set_chained_irqchip(&ctrl->gc, &ath79_gpio_irqchip, |
| 297 | platform_get_irq(pdev, 0), |
| 298 | ath79_gpio_irq_handler); |
| 299 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 300 | return 0; |
Alban Bedel | 2b8f89e | 2016-01-28 20:44:32 +0100 | [diff] [blame] | 301 | |
| 302 | gpiochip_remove: |
| 303 | gpiochip_remove(&ctrl->gc); |
| 304 | return err; |
Gabor Juhos | 6eae43c | 2011-01-04 21:28:15 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Alban Bedel | 2f890cf | 2016-01-28 20:44:31 +0100 | [diff] [blame] | 307 | static int ath79_gpio_remove(struct platform_device *pdev) |
| 308 | { |
| 309 | struct ath79_gpio_ctrl *ctrl = platform_get_drvdata(pdev); |
| 310 | |
| 311 | gpiochip_remove(&ctrl->gc); |
| 312 | return 0; |
| 313 | } |
| 314 | |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 315 | static struct platform_driver ath79_gpio_driver = { |
| 316 | .driver = { |
| 317 | .name = "ath79-gpio", |
| 318 | .of_match_table = ath79_gpio_of_match, |
| 319 | }, |
| 320 | .probe = ath79_gpio_probe, |
Alban Bedel | 2f890cf | 2016-01-28 20:44:31 +0100 | [diff] [blame] | 321 | .remove = ath79_gpio_remove, |
Alban Bedel | 2ddf3a7 | 2015-05-31 02:18:24 +0200 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | module_platform_driver(ath79_gpio_driver); |