gpio: pca953x.c: add interrupt handling capability

Most of the GPIO expanders controlled by the pca953x driver are able to
report changes on the input pins through an *INT pin.

This patch implements the irq_chip functionality (edge detection only).

The driver has been tested on an Arcom Zeus.

[akpm@linux-foundation.org: the compiler does inlining for us nowadays]
Signed-off-by: Marc Zyngier <maz@misterjones.org>
Cc: Eric Miao <eric.y.miao@gmail.com>
Cc: Haojian Zhuang <haojian.zhuang@gmail.com>
Cc: David Brownell <david-b@pacbell.net>
Cc: Nate Case <ncase@xes-inc.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Marc Zyngier and committed by Linus Torvalds 89ea8bbe 8c35c89a

+247 -12
+7
drivers/gpio/Kconfig
··· 134 134 This driver can also be built as a module. If so, the module 135 135 will be called pca953x. 136 136 137 + config GPIO_PCA953X_IRQ 138 + bool "Interrupt controller support for PCA953x" 139 + depends on GPIO_PCA953X=y 140 + help 141 + Say yes here to enable the pca953x to be used as an interrupt 142 + controller. It requires the driver to be built in the kernel. 143 + 137 144 config GPIO_PCF857X 138 145 tristate "PCF857x, PCA{85,96}7x, and MAX732[89] I2C GPIO expanders" 139 146 depends on I2C
+237 -12
drivers/gpio/pca953x.c
··· 14 14 #include <linux/module.h> 15 15 #include <linux/init.h> 16 16 #include <linux/gpio.h> 17 + #include <linux/interrupt.h> 18 + #include <linux/irq.h> 17 19 #include <linux/i2c.h> 18 20 #include <linux/i2c/pca953x.h> 19 21 #ifdef CONFIG_OF_GPIO ··· 28 26 #define PCA953X_INVERT 2 29 27 #define PCA953X_DIRECTION 3 30 28 29 + #define PCA953X_GPIOS 0x00FF 30 + #define PCA953X_INT 0x0100 31 + 31 32 static const struct i2c_device_id pca953x_id[] = { 32 - { "pca9534", 8, }, 33 - { "pca9535", 16, }, 33 + { "pca9534", 8 | PCA953X_INT, }, 34 + { "pca9535", 16 | PCA953X_INT, }, 34 35 { "pca9536", 4, }, 35 - { "pca9537", 4, }, 36 - { "pca9538", 8, }, 37 - { "pca9539", 16, }, 38 - { "pca9554", 8, }, 39 - { "pca9555", 16, }, 36 + { "pca9537", 4 | PCA953X_INT, }, 37 + { "pca9538", 8 | PCA953X_INT, }, 38 + { "pca9539", 16 | PCA953X_INT, }, 39 + { "pca9554", 8 | PCA953X_INT, }, 40 + { "pca9555", 16 | PCA953X_INT, }, 40 41 { "pca9556", 8, }, 41 42 { "pca9557", 8, }, 42 43 43 44 { "max7310", 8, }, 44 - { "max7315", 8, }, 45 - { "pca6107", 8, }, 46 - { "tca6408", 8, }, 47 - { "tca6416", 16, }, 45 + { "max7312", 16 | PCA953X_INT, }, 46 + { "max7313", 16 | PCA953X_INT, }, 47 + { "max7315", 8 | PCA953X_INT, }, 48 + { "pca6107", 8 | PCA953X_INT, }, 49 + { "tca6408", 8 | PCA953X_INT, }, 50 + { "tca6416", 16 | PCA953X_INT, }, 48 51 /* NYET: { "tca6424", 24, }, */ 49 52 { } 50 53 }; ··· 59 52 unsigned gpio_start; 60 53 uint16_t reg_output; 61 54 uint16_t reg_direction; 55 + 56 + #ifdef CONFIG_GPIO_PCA953X_IRQ 57 + struct mutex irq_lock; 58 + uint16_t irq_mask; 59 + uint16_t irq_stat; 60 + uint16_t irq_trig_raise; 61 + uint16_t irq_trig_fall; 62 + int irq_base; 63 + #endif 62 64 63 65 struct i2c_client *client; 64 66 struct pca953x_platform_data *dyn_pdata; ··· 218 202 gc->names = chip->names; 219 203 } 220 204 205 + #ifdef CONFIG_GPIO_PCA953X_IRQ 206 + static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off) 207 + { 208 + struct pca953x_chip *chip; 209 + 210 + chip = container_of(gc, struct pca953x_chip, gpio_chip); 211 + return chip->irq_base + off; 212 + } 213 + 214 + static void pca953x_irq_mask(unsigned int irq) 215 + { 216 + struct pca953x_chip *chip = get_irq_chip_data(irq); 217 + 218 + chip->irq_mask &= ~(1 << (irq - chip->irq_base)); 219 + } 220 + 221 + static void pca953x_irq_unmask(unsigned int irq) 222 + { 223 + struct pca953x_chip *chip = get_irq_chip_data(irq); 224 + 225 + chip->irq_mask |= 1 << (irq - chip->irq_base); 226 + } 227 + 228 + static void pca953x_irq_bus_lock(unsigned int irq) 229 + { 230 + struct pca953x_chip *chip = get_irq_chip_data(irq); 231 + 232 + mutex_lock(&chip->irq_lock); 233 + } 234 + 235 + static void pca953x_irq_bus_sync_unlock(unsigned int irq) 236 + { 237 + struct pca953x_chip *chip = get_irq_chip_data(irq); 238 + 239 + mutex_unlock(&chip->irq_lock); 240 + } 241 + 242 + static int pca953x_irq_set_type(unsigned int irq, unsigned int type) 243 + { 244 + struct pca953x_chip *chip = get_irq_chip_data(irq); 245 + uint16_t level = irq - chip->irq_base; 246 + uint16_t mask = 1 << level; 247 + 248 + if (!(type & IRQ_TYPE_EDGE_BOTH)) { 249 + dev_err(&chip->client->dev, "irq %d: unsupported type %d\n", 250 + irq, type); 251 + return -EINVAL; 252 + } 253 + 254 + if (type & IRQ_TYPE_EDGE_FALLING) 255 + chip->irq_trig_fall |= mask; 256 + else 257 + chip->irq_trig_fall &= ~mask; 258 + 259 + if (type & IRQ_TYPE_EDGE_RISING) 260 + chip->irq_trig_raise |= mask; 261 + else 262 + chip->irq_trig_raise &= ~mask; 263 + 264 + return pca953x_gpio_direction_input(&chip->gpio_chip, level); 265 + } 266 + 267 + static struct irq_chip pca953x_irq_chip = { 268 + .name = "pca953x", 269 + .mask = pca953x_irq_mask, 270 + .unmask = pca953x_irq_unmask, 271 + .bus_lock = pca953x_irq_bus_lock, 272 + .bus_sync_unlock = pca953x_irq_bus_sync_unlock, 273 + .set_type = pca953x_irq_set_type, 274 + }; 275 + 276 + static uint16_t pca953x_irq_pending(struct pca953x_chip *chip) 277 + { 278 + uint16_t cur_stat; 279 + uint16_t old_stat; 280 + uint16_t pending; 281 + uint16_t trigger; 282 + int ret; 283 + 284 + ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat); 285 + if (ret) 286 + return 0; 287 + 288 + /* Remove output pins from the equation */ 289 + cur_stat &= chip->reg_direction; 290 + 291 + old_stat = chip->irq_stat; 292 + trigger = (cur_stat ^ old_stat) & chip->irq_mask; 293 + 294 + if (!trigger) 295 + return 0; 296 + 297 + chip->irq_stat = cur_stat; 298 + 299 + pending = (old_stat & chip->irq_trig_fall) | 300 + (cur_stat & chip->irq_trig_raise); 301 + pending &= trigger; 302 + 303 + return pending; 304 + } 305 + 306 + static irqreturn_t pca953x_irq_handler(int irq, void *devid) 307 + { 308 + struct pca953x_chip *chip = devid; 309 + uint16_t pending; 310 + uint16_t level; 311 + 312 + pending = pca953x_irq_pending(chip); 313 + 314 + if (!pending) 315 + return IRQ_HANDLED; 316 + 317 + do { 318 + level = __ffs(pending); 319 + handle_nested_irq(level + chip->irq_base); 320 + 321 + pending &= ~(1 << level); 322 + } while (pending); 323 + 324 + return IRQ_HANDLED; 325 + } 326 + 327 + static int pca953x_irq_setup(struct pca953x_chip *chip, 328 + const struct i2c_device_id *id) 329 + { 330 + struct i2c_client *client = chip->client; 331 + struct pca953x_platform_data *pdata = client->dev.platform_data; 332 + int ret; 333 + 334 + if (pdata->irq_base && (id->driver_data & PCA953X_INT)) { 335 + int lvl; 336 + 337 + ret = pca953x_read_reg(chip, PCA953X_INPUT, 338 + &chip->irq_stat); 339 + if (ret) 340 + goto out_failed; 341 + 342 + /* 343 + * There is no way to know which GPIO line generated the 344 + * interrupt. We have to rely on the previous read for 345 + * this purpose. 346 + */ 347 + chip->irq_stat &= chip->reg_direction; 348 + chip->irq_base = pdata->irq_base; 349 + mutex_init(&chip->irq_lock); 350 + 351 + for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) { 352 + int irq = lvl + chip->irq_base; 353 + 354 + set_irq_chip_data(irq, chip); 355 + set_irq_chip_and_handler(irq, &pca953x_irq_chip, 356 + handle_edge_irq); 357 + set_irq_nested_thread(irq, 1); 358 + #ifdef CONFIG_ARM 359 + set_irq_flags(irq, IRQF_VALID); 360 + #else 361 + set_irq_noprobe(irq); 362 + #endif 363 + } 364 + 365 + ret = request_threaded_irq(client->irq, 366 + NULL, 367 + pca953x_irq_handler, 368 + IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 369 + dev_name(&client->dev), chip); 370 + if (ret) { 371 + dev_err(&client->dev, "failed to request irq %d\n", 372 + client->irq); 373 + goto out_failed; 374 + } 375 + 376 + chip->gpio_chip.to_irq = pca953x_gpio_to_irq; 377 + } 378 + 379 + return 0; 380 + 381 + out_failed: 382 + chip->irq_base = 0; 383 + return ret; 384 + } 385 + 386 + static void pca953x_irq_teardown(struct pca953x_chip *chip) 387 + { 388 + if (chip->irq_base) 389 + free_irq(chip->client->irq, chip); 390 + } 391 + #else /* CONFIG_GPIO_PCA953X_IRQ */ 392 + static int pca953x_irq_setup(struct pca953x_chip *chip, 393 + const struct i2c_device_id *id) 394 + { 395 + struct i2c_client *client = chip->client; 396 + struct pca953x_platform_data *pdata = client->dev.platform_data; 397 + 398 + if (pdata->irq_base && (id->driver_data & PCA953X_INT)) 399 + dev_warn(&client->dev, "interrupt support not compiled in\n"); 400 + 401 + return 0; 402 + } 403 + 404 + static void pca953x_irq_teardown(struct pca953x_chip *chip) 405 + { 406 + } 407 + #endif 408 + 221 409 /* 222 410 * Handlers for alternative sources of platform_data 223 411 */ ··· 506 286 /* initialize cached registers from their original values. 507 287 * we can't share this chip with another i2c master. 508 288 */ 509 - pca953x_setup_gpio(chip, id->driver_data); 289 + pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS); 510 290 511 291 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output); 512 292 if (ret) ··· 521 301 if (ret) 522 302 goto out_failed; 523 303 304 + ret = pca953x_irq_setup(chip, id); 305 + if (ret) 306 + goto out_failed; 524 307 525 308 ret = gpiochip_add(&chip->gpio_chip); 526 309 if (ret) ··· 540 317 return 0; 541 318 542 319 out_failed: 320 + pca953x_irq_teardown(chip); 543 321 kfree(chip->dyn_pdata); 544 322 kfree(chip); 545 323 return ret; ··· 569 345 return ret; 570 346 } 571 347 348 + pca953x_irq_teardown(chip); 572 349 kfree(chip->dyn_pdata); 573 350 kfree(chip); 574 351 return 0;
+3
include/linux/i2c/pca953x.h
··· 13 13 /* initial polarity inversion setting */ 14 14 uint16_t invert; 15 15 16 + /* interrupt base */ 17 + int irq_base; 18 + 16 19 void *context; /* param to setup/teardown */ 17 20 18 21 int (*setup)(struct i2c_client *client,