Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011-2012 Avionic Design GmbH |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 6 | #include <linux/gpio/driver.h> |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 7 | #include <linux/i2c.h> |
| 8 | #include <linux/interrupt.h> |
Andy Shevchenko | 8773bac | 2022-06-28 22:59:13 +0300 | [diff] [blame] | 9 | #include <linux/mod_devicetable.h> |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 10 | #include <linux/module.h> |
Andy Shevchenko | 8773bac | 2022-06-28 22:59:13 +0300 | [diff] [blame] | 11 | #include <linux/property.h> |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/slab.h> |
| 14 | |
| 15 | #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift) |
| 16 | #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift) |
| 17 | #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift) |
| 18 | #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift) |
| 19 | #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift) |
| 20 | |
| 21 | struct adnp { |
| 22 | struct i2c_client *client; |
| 23 | struct gpio_chip gpio; |
| 24 | unsigned int reg_shift; |
| 25 | |
| 26 | struct mutex i2c_lock; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 27 | struct mutex irq_lock; |
| 28 | |
| 29 | u8 *irq_enable; |
| 30 | u8 *irq_level; |
| 31 | u8 *irq_rise; |
| 32 | u8 *irq_fall; |
| 33 | u8 *irq_high; |
| 34 | u8 *irq_low; |
| 35 | }; |
| 36 | |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 37 | static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value) |
| 38 | { |
| 39 | int err; |
| 40 | |
| 41 | err = i2c_smbus_read_byte_data(adnp->client, offset); |
| 42 | if (err < 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 43 | dev_err(adnp->gpio.parent, "%s failed: %d\n", |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 44 | "i2c_smbus_read_byte_data()", err); |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | *value = err; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value) |
| 53 | { |
| 54 | int err; |
| 55 | |
| 56 | err = i2c_smbus_write_byte_data(adnp->client, offset, value); |
| 57 | if (err < 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 58 | dev_err(adnp->gpio.parent, "%s failed: %d\n", |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 59 | "i2c_smbus_write_byte_data()", err); |
| 60 | return err; |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset) |
| 67 | { |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 68 | struct adnp *adnp = gpiochip_get_data(chip); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 69 | unsigned int reg = offset >> adnp->reg_shift; |
| 70 | unsigned int pos = offset & 7; |
| 71 | u8 value; |
| 72 | int err; |
| 73 | |
| 74 | err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value); |
| 75 | if (err < 0) |
| 76 | return err; |
| 77 | |
| 78 | return (value & BIT(pos)) ? 1 : 0; |
| 79 | } |
| 80 | |
| 81 | static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value) |
| 82 | { |
| 83 | unsigned int reg = offset >> adnp->reg_shift; |
| 84 | unsigned int pos = offset & 7; |
| 85 | int err; |
| 86 | u8 val; |
| 87 | |
| 88 | err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val); |
| 89 | if (err < 0) |
| 90 | return; |
| 91 | |
| 92 | if (value) |
| 93 | val |= BIT(pos); |
| 94 | else |
| 95 | val &= ~BIT(pos); |
| 96 | |
| 97 | adnp_write(adnp, GPIO_PLR(adnp) + reg, val); |
| 98 | } |
| 99 | |
| 100 | static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value) |
| 101 | { |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 102 | struct adnp *adnp = gpiochip_get_data(chip); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 103 | |
| 104 | mutex_lock(&adnp->i2c_lock); |
| 105 | __adnp_gpio_set(adnp, offset, value); |
| 106 | mutex_unlock(&adnp->i2c_lock); |
| 107 | } |
| 108 | |
| 109 | static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset) |
| 110 | { |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 111 | struct adnp *adnp = gpiochip_get_data(chip); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 112 | unsigned int reg = offset >> adnp->reg_shift; |
| 113 | unsigned int pos = offset & 7; |
| 114 | u8 value; |
| 115 | int err; |
| 116 | |
| 117 | mutex_lock(&adnp->i2c_lock); |
| 118 | |
| 119 | err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value); |
| 120 | if (err < 0) |
| 121 | goto out; |
| 122 | |
| 123 | value &= ~BIT(pos); |
| 124 | |
| 125 | err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value); |
| 126 | if (err < 0) |
| 127 | goto out; |
| 128 | |
| 129 | err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value); |
| 130 | if (err < 0) |
| 131 | goto out; |
| 132 | |
Axel Lin | c5bc6e5 | 2019-03-11 21:29:37 +0800 | [diff] [blame] | 133 | if (value & BIT(pos)) { |
| 134 | err = -EPERM; |
| 135 | goto out; |
| 136 | } |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 137 | |
| 138 | err = 0; |
| 139 | |
| 140 | out: |
| 141 | mutex_unlock(&adnp->i2c_lock); |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset, |
| 146 | int value) |
| 147 | { |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 148 | struct adnp *adnp = gpiochip_get_data(chip); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 149 | unsigned int reg = offset >> adnp->reg_shift; |
| 150 | unsigned int pos = offset & 7; |
| 151 | int err; |
| 152 | u8 val; |
| 153 | |
| 154 | mutex_lock(&adnp->i2c_lock); |
| 155 | |
| 156 | err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val); |
| 157 | if (err < 0) |
| 158 | goto out; |
| 159 | |
| 160 | val |= BIT(pos); |
| 161 | |
| 162 | err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val); |
| 163 | if (err < 0) |
| 164 | goto out; |
| 165 | |
| 166 | err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val); |
| 167 | if (err < 0) |
| 168 | goto out; |
| 169 | |
| 170 | if (!(val & BIT(pos))) { |
| 171 | err = -EPERM; |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | __adnp_gpio_set(adnp, offset, value); |
| 176 | err = 0; |
| 177 | |
| 178 | out: |
| 179 | mutex_unlock(&adnp->i2c_lock); |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
| 184 | { |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 185 | struct adnp *adnp = gpiochip_get_data(chip); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 186 | unsigned int num_regs = 1 << adnp->reg_shift, i, j; |
| 187 | int err; |
| 188 | |
| 189 | for (i = 0; i < num_regs; i++) { |
| 190 | u8 ddr, plr, ier, isr; |
| 191 | |
| 192 | mutex_lock(&adnp->i2c_lock); |
| 193 | |
| 194 | err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr); |
Markus Elfring | 5ac9d2d | 2017-10-22 20:21:55 +0200 | [diff] [blame] | 195 | if (err < 0) |
| 196 | goto unlock; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 197 | |
| 198 | err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr); |
Markus Elfring | 5ac9d2d | 2017-10-22 20:21:55 +0200 | [diff] [blame] | 199 | if (err < 0) |
| 200 | goto unlock; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 201 | |
| 202 | err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier); |
Markus Elfring | 5ac9d2d | 2017-10-22 20:21:55 +0200 | [diff] [blame] | 203 | if (err < 0) |
| 204 | goto unlock; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 205 | |
| 206 | err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr); |
Markus Elfring | 5ac9d2d | 2017-10-22 20:21:55 +0200 | [diff] [blame] | 207 | if (err < 0) |
| 208 | goto unlock; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 209 | |
| 210 | mutex_unlock(&adnp->i2c_lock); |
| 211 | |
| 212 | for (j = 0; j < 8; j++) { |
| 213 | unsigned int bit = (i << adnp->reg_shift) + j; |
| 214 | const char *direction = "input "; |
| 215 | const char *level = "low "; |
| 216 | const char *interrupt = "disabled"; |
| 217 | const char *pending = ""; |
| 218 | |
| 219 | if (ddr & BIT(j)) |
| 220 | direction = "output"; |
| 221 | |
| 222 | if (plr & BIT(j)) |
| 223 | level = "high"; |
| 224 | |
| 225 | if (ier & BIT(j)) |
| 226 | interrupt = "enabled "; |
| 227 | |
| 228 | if (isr & BIT(j)) |
| 229 | pending = "pending"; |
| 230 | |
| 231 | seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit, |
| 232 | direction, level, interrupt, pending); |
| 233 | } |
| 234 | } |
Markus Elfring | 5ac9d2d | 2017-10-22 20:21:55 +0200 | [diff] [blame] | 235 | |
| 236 | return; |
| 237 | |
| 238 | unlock: |
| 239 | mutex_unlock(&adnp->i2c_lock); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 242 | static irqreturn_t adnp_irq(int irq, void *data) |
| 243 | { |
| 244 | struct adnp *adnp = data; |
| 245 | unsigned int num_regs, i; |
| 246 | |
| 247 | num_regs = 1 << adnp->reg_shift; |
| 248 | |
| 249 | for (i = 0; i < num_regs; i++) { |
| 250 | unsigned int base = i << adnp->reg_shift, bit; |
| 251 | u8 changed, level, isr, ier; |
| 252 | unsigned long pending; |
| 253 | int err; |
| 254 | |
| 255 | mutex_lock(&adnp->i2c_lock); |
| 256 | |
| 257 | err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level); |
| 258 | if (err < 0) { |
| 259 | mutex_unlock(&adnp->i2c_lock); |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr); |
| 264 | if (err < 0) { |
| 265 | mutex_unlock(&adnp->i2c_lock); |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier); |
| 270 | if (err < 0) { |
| 271 | mutex_unlock(&adnp->i2c_lock); |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | mutex_unlock(&adnp->i2c_lock); |
| 276 | |
| 277 | /* determine pins that changed levels */ |
| 278 | changed = level ^ adnp->irq_level[i]; |
| 279 | |
| 280 | /* compute edge-triggered interrupts */ |
| 281 | pending = changed & ((adnp->irq_fall[i] & ~level) | |
| 282 | (adnp->irq_rise[i] & level)); |
| 283 | |
| 284 | /* add in level-triggered interrupts */ |
| 285 | pending |= (adnp->irq_high[i] & level) | |
| 286 | (adnp->irq_low[i] & ~level); |
| 287 | |
| 288 | /* mask out non-pending and disabled interrupts */ |
| 289 | pending &= isr & ier; |
| 290 | |
| 291 | for_each_set_bit(bit, &pending, 8) { |
Linus Walleij | 472f95b | 2013-10-11 19:10:00 +0200 | [diff] [blame] | 292 | unsigned int child_irq; |
Thierry Reding | f0fbe7b | 2017-11-07 19:15:47 +0100 | [diff] [blame] | 293 | child_irq = irq_find_mapping(adnp->gpio.irq.domain, |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 294 | base + bit); |
Linus Walleij | 472f95b | 2013-10-11 19:10:00 +0200 | [diff] [blame] | 295 | handle_nested_irq(child_irq); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | return IRQ_HANDLED; |
| 300 | } |
| 301 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 302 | static void adnp_irq_mask(struct irq_data *d) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 303 | { |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 304 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 305 | struct adnp *adnp = gpiochip_get_data(gc); |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 306 | unsigned int reg = d->hwirq >> adnp->reg_shift; |
| 307 | unsigned int pos = d->hwirq & 7; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 308 | |
| 309 | adnp->irq_enable[reg] &= ~BIT(pos); |
Linus Walleij | d4c0cf3 | 2023-03-09 08:45:50 +0100 | [diff] [blame] | 310 | gpiochip_disable_irq(gc, irqd_to_hwirq(d)); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 313 | static void adnp_irq_unmask(struct irq_data *d) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 314 | { |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 315 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 316 | struct adnp *adnp = gpiochip_get_data(gc); |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 317 | unsigned int reg = d->hwirq >> adnp->reg_shift; |
| 318 | unsigned int pos = d->hwirq & 7; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 319 | |
Linus Walleij | d4c0cf3 | 2023-03-09 08:45:50 +0100 | [diff] [blame] | 320 | gpiochip_enable_irq(gc, irqd_to_hwirq(d)); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 321 | adnp->irq_enable[reg] |= BIT(pos); |
| 322 | } |
| 323 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 324 | static int adnp_irq_set_type(struct irq_data *d, unsigned int type) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 325 | { |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 326 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 327 | struct adnp *adnp = gpiochip_get_data(gc); |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 328 | unsigned int reg = d->hwirq >> adnp->reg_shift; |
| 329 | unsigned int pos = d->hwirq & 7; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 330 | |
| 331 | if (type & IRQ_TYPE_EDGE_RISING) |
| 332 | adnp->irq_rise[reg] |= BIT(pos); |
| 333 | else |
| 334 | adnp->irq_rise[reg] &= ~BIT(pos); |
| 335 | |
| 336 | if (type & IRQ_TYPE_EDGE_FALLING) |
| 337 | adnp->irq_fall[reg] |= BIT(pos); |
| 338 | else |
| 339 | adnp->irq_fall[reg] &= ~BIT(pos); |
| 340 | |
| 341 | if (type & IRQ_TYPE_LEVEL_HIGH) |
| 342 | adnp->irq_high[reg] |= BIT(pos); |
| 343 | else |
| 344 | adnp->irq_high[reg] &= ~BIT(pos); |
| 345 | |
| 346 | if (type & IRQ_TYPE_LEVEL_LOW) |
| 347 | adnp->irq_low[reg] |= BIT(pos); |
| 348 | else |
| 349 | adnp->irq_low[reg] &= ~BIT(pos); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 354 | static void adnp_irq_bus_lock(struct irq_data *d) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 355 | { |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 356 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 357 | struct adnp *adnp = gpiochip_get_data(gc); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 358 | |
| 359 | mutex_lock(&adnp->irq_lock); |
| 360 | } |
| 361 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 362 | static void adnp_irq_bus_unlock(struct irq_data *d) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 363 | { |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 364 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 1e69c4f | 2015-12-04 14:56:15 +0100 | [diff] [blame] | 365 | struct adnp *adnp = gpiochip_get_data(gc); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 366 | unsigned int num_regs = 1 << adnp->reg_shift, i; |
| 367 | |
| 368 | mutex_lock(&adnp->i2c_lock); |
| 369 | |
| 370 | for (i = 0; i < num_regs; i++) |
| 371 | adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]); |
| 372 | |
| 373 | mutex_unlock(&adnp->i2c_lock); |
| 374 | mutex_unlock(&adnp->irq_lock); |
| 375 | } |
| 376 | |
Linus Walleij | d4c0cf3 | 2023-03-09 08:45:50 +0100 | [diff] [blame] | 377 | static const struct irq_chip adnp_irq_chip = { |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 378 | .name = "gpio-adnp", |
| 379 | .irq_mask = adnp_irq_mask, |
| 380 | .irq_unmask = adnp_irq_unmask, |
| 381 | .irq_set_type = adnp_irq_set_type, |
| 382 | .irq_bus_lock = adnp_irq_bus_lock, |
| 383 | .irq_bus_sync_unlock = adnp_irq_bus_unlock, |
Linus Walleij | d4c0cf3 | 2023-03-09 08:45:50 +0100 | [diff] [blame] | 384 | .flags = IRQCHIP_IMMUTABLE, |
| 385 | GPIOCHIP_IRQ_RESOURCE_HELPERS, |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | static int adnp_irq_setup(struct adnp *adnp) |
| 389 | { |
| 390 | unsigned int num_regs = 1 << adnp->reg_shift, i; |
| 391 | struct gpio_chip *chip = &adnp->gpio; |
| 392 | int err; |
| 393 | |
| 394 | mutex_init(&adnp->irq_lock); |
| 395 | |
| 396 | /* |
| 397 | * Allocate memory to keep track of the current level and trigger |
| 398 | * modes of the interrupts. To avoid multiple allocations, a single |
| 399 | * large buffer is allocated and pointers are setup to point at the |
| 400 | * corresponding offsets. For consistency, the layout of the buffer |
| 401 | * is chosen to match the register layout of the hardware in that |
| 402 | * each segment contains the corresponding bits for all interrupts. |
| 403 | */ |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 404 | adnp->irq_enable = devm_kcalloc(chip->parent, num_regs, 6, |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 405 | GFP_KERNEL); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 406 | if (!adnp->irq_enable) |
| 407 | return -ENOMEM; |
| 408 | |
| 409 | adnp->irq_level = adnp->irq_enable + (num_regs * 1); |
| 410 | adnp->irq_rise = adnp->irq_enable + (num_regs * 2); |
| 411 | adnp->irq_fall = adnp->irq_enable + (num_regs * 3); |
| 412 | adnp->irq_high = adnp->irq_enable + (num_regs * 4); |
| 413 | adnp->irq_low = adnp->irq_enable + (num_regs * 5); |
| 414 | |
| 415 | for (i = 0; i < num_regs; i++) { |
| 416 | /* |
| 417 | * Read the initial level of all pins to allow the emulation |
| 418 | * of edge triggered interrupts. |
| 419 | */ |
| 420 | err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]); |
| 421 | if (err < 0) |
| 422 | return err; |
| 423 | |
| 424 | /* disable all interrupts */ |
| 425 | err = adnp_write(adnp, GPIO_IER(adnp) + i, 0); |
| 426 | if (err < 0) |
| 427 | return err; |
| 428 | |
| 429 | adnp->irq_enable[i] = 0x00; |
| 430 | } |
| 431 | |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 432 | err = devm_request_threaded_irq(chip->parent, adnp->client->irq, |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 433 | NULL, adnp_irq, |
| 434 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 435 | dev_name(chip->parent), adnp); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 436 | if (err != 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 437 | dev_err(chip->parent, "can't request IRQ#%d: %d\n", |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 438 | adnp->client->irq, err); |
Lars Poeschel | 5b21533 | 2013-08-07 17:23:58 +0200 | [diff] [blame] | 439 | return err; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Linus Walleij | 565a0e9 | 2020-07-16 14:03:18 +0200 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios, |
| 446 | bool is_irq_controller) |
| 447 | { |
| 448 | struct gpio_chip *chip = &adnp->gpio; |
| 449 | int err; |
| 450 | |
| 451 | adnp->reg_shift = get_count_order(num_gpios) - 3; |
| 452 | |
| 453 | chip->direction_input = adnp_gpio_direction_input; |
| 454 | chip->direction_output = adnp_gpio_direction_output; |
| 455 | chip->get = adnp_gpio_get; |
| 456 | chip->set = adnp_gpio_set; |
| 457 | chip->can_sleep = true; |
| 458 | |
| 459 | if (IS_ENABLED(CONFIG_DEBUG_FS)) |
| 460 | chip->dbg_show = adnp_gpio_dbg_show; |
| 461 | |
| 462 | chip->base = -1; |
| 463 | chip->ngpio = num_gpios; |
| 464 | chip->label = adnp->client->name; |
| 465 | chip->parent = &adnp->client->dev; |
Linus Walleij | 565a0e9 | 2020-07-16 14:03:18 +0200 | [diff] [blame] | 466 | chip->owner = THIS_MODULE; |
| 467 | |
| 468 | if (is_irq_controller) { |
| 469 | struct gpio_irq_chip *girq; |
| 470 | |
| 471 | err = adnp_irq_setup(adnp); |
| 472 | if (err) |
| 473 | return err; |
| 474 | |
| 475 | girq = &chip->irq; |
Linus Walleij | d4c0cf3 | 2023-03-09 08:45:50 +0100 | [diff] [blame] | 476 | gpio_irq_chip_set_chip(girq, &adnp_irq_chip); |
| 477 | |
Linus Walleij | 565a0e9 | 2020-07-16 14:03:18 +0200 | [diff] [blame] | 478 | /* This will let us handle the parent IRQ in the driver */ |
| 479 | girq->parent_handler = NULL; |
| 480 | girq->num_parents = 0; |
| 481 | girq->parents = NULL; |
| 482 | girq->default_type = IRQ_TYPE_NONE; |
| 483 | girq->handler = handle_simple_irq; |
| 484 | girq->threaded = true; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Linus Walleij | 565a0e9 | 2020-07-16 14:03:18 +0200 | [diff] [blame] | 487 | err = devm_gpiochip_add_data(&adnp->client->dev, chip, adnp); |
| 488 | if (err) |
| 489 | return err; |
Linus Walleij | 35ca3f6 | 2016-11-24 13:27:54 +0100 | [diff] [blame] | 490 | |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 491 | return 0; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Andy Shevchenko | a2a15e1 | 2022-06-28 22:59:12 +0300 | [diff] [blame] | 494 | static int adnp_i2c_probe(struct i2c_client *client) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 495 | { |
Andy Shevchenko | 8773bac | 2022-06-28 22:59:13 +0300 | [diff] [blame] | 496 | struct device *dev = &client->dev; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 497 | struct adnp *adnp; |
| 498 | u32 num_gpios; |
| 499 | int err; |
| 500 | |
Andy Shevchenko | 8773bac | 2022-06-28 22:59:13 +0300 | [diff] [blame] | 501 | err = device_property_read_u32(dev, "nr-gpios", &num_gpios); |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 502 | if (err < 0) |
| 503 | return err; |
| 504 | |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 505 | adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL); |
| 506 | if (!adnp) |
| 507 | return -ENOMEM; |
| 508 | |
| 509 | mutex_init(&adnp->i2c_lock); |
| 510 | adnp->client = client; |
| 511 | |
Andy Shevchenko | 8773bac | 2022-06-28 22:59:13 +0300 | [diff] [blame] | 512 | err = adnp_gpio_setup(adnp, num_gpios, device_property_read_bool(dev, "interrupt-controller")); |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 513 | if (err) |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 514 | return err; |
| 515 | |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 516 | i2c_set_clientdata(client, adnp); |
Linus Walleij | 0752e16 | 2014-06-02 15:17:54 +0200 | [diff] [blame] | 517 | |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 518 | return 0; |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Bill Pemberton | b5ba78d | 2012-11-19 13:25:05 -0500 | [diff] [blame] | 521 | static const struct i2c_device_id adnp_i2c_id[] = { |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 522 | { "gpio-adnp" }, |
| 523 | { }, |
| 524 | }; |
| 525 | MODULE_DEVICE_TABLE(i2c, adnp_i2c_id); |
| 526 | |
Bill Pemberton | b5ba78d | 2012-11-19 13:25:05 -0500 | [diff] [blame] | 527 | static const struct of_device_id adnp_of_match[] = { |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 528 | { .compatible = "ad,gpio-adnp", }, |
| 529 | { }, |
| 530 | }; |
| 531 | MODULE_DEVICE_TABLE(of, adnp_of_match); |
| 532 | |
| 533 | static struct i2c_driver adnp_i2c_driver = { |
| 534 | .driver = { |
| 535 | .name = "gpio-adnp", |
Sachin Kamat | 83924fb3 | 2013-09-28 17:34:07 +0530 | [diff] [blame] | 536 | .of_match_table = adnp_of_match, |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 537 | }, |
Uwe Kleine-König | b41cabb | 2023-05-20 19:47:35 +0200 | [diff] [blame] | 538 | .probe = adnp_i2c_probe, |
Thierry Reding | 5e969a4 | 2012-09-18 10:57:10 +0200 | [diff] [blame] | 539 | .id_table = adnp_i2c_id, |
| 540 | }; |
| 541 | module_i2c_driver(adnp_i2c_driver); |
| 542 | |
| 543 | MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander"); |
| 544 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
| 545 | MODULE_LICENSE("GPL"); |