gpio-adnp.c (83bc90e11576f9c100f8ef4ba2bcd0b89212e3fb) | gpio-adnp.c (0752e169ba523e35f70d2fee4d06680b33e0e202) |
---|---|
1/* 2 * Copyright (C) 2011-2012 Avionic Design GmbH 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 | 1/* 2 * Copyright (C) 2011-2012 Avionic Design GmbH 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 |
9#include <linux/gpio.h> | 9#include <linux/gpio/driver.h> |
10#include <linux/i2c.h> 11#include <linux/interrupt.h> | 10#include <linux/i2c.h> 11#include <linux/interrupt.h> |
12#include <linux/irqdomain.h> | |
13#include <linux/module.h> 14#include <linux/of_irq.h> 15#include <linux/seq_file.h> 16#include <linux/slab.h> 17 18#define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift) 19#define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift) 20#define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift) 21#define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift) 22#define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift) 23 24struct adnp { 25 struct i2c_client *client; 26 struct gpio_chip gpio; 27 unsigned int reg_shift; 28 29 struct mutex i2c_lock; | 12#include <linux/module.h> 13#include <linux/of_irq.h> 14#include <linux/seq_file.h> 15#include <linux/slab.h> 16 17#define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift) 18#define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift) 19#define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift) 20#define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift) 21#define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift) 22 23struct adnp { 24 struct i2c_client *client; 25 struct gpio_chip gpio; 26 unsigned int reg_shift; 27 28 struct mutex i2c_lock; |
30 31 struct irq_domain *domain; | |
32 struct mutex irq_lock; 33 34 u8 *irq_enable; 35 u8 *irq_level; 36 u8 *irq_rise; 37 u8 *irq_fall; 38 u8 *irq_high; 39 u8 *irq_low; --- 208 unchanged lines hidden (view full) --- 248 direction, level, interrupt, pending); 249 } 250 } 251} 252 253static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios) 254{ 255 struct gpio_chip *chip = &adnp->gpio; | 29 struct mutex irq_lock; 30 31 u8 *irq_enable; 32 u8 *irq_level; 33 u8 *irq_rise; 34 u8 *irq_fall; 35 u8 *irq_high; 36 u8 *irq_low; --- 208 unchanged lines hidden (view full) --- 245 direction, level, interrupt, pending); 246 } 247 } 248} 249 250static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios) 251{ 252 struct gpio_chip *chip = &adnp->gpio; |
253 int err; |
|
256 257 adnp->reg_shift = get_count_order(num_gpios) - 3; 258 259 chip->direction_input = adnp_gpio_direction_input; 260 chip->direction_output = adnp_gpio_direction_output; 261 chip->get = adnp_gpio_get; 262 chip->set = adnp_gpio_set; 263 chip->can_sleep = true; 264 265 if (IS_ENABLED(CONFIG_DEBUG_FS)) 266 chip->dbg_show = adnp_gpio_dbg_show; 267 268 chip->base = -1; 269 chip->ngpio = num_gpios; 270 chip->label = adnp->client->name; 271 chip->dev = &adnp->client->dev; 272 chip->of_node = chip->dev->of_node; 273 chip->owner = THIS_MODULE; 274 | 254 255 adnp->reg_shift = get_count_order(num_gpios) - 3; 256 257 chip->direction_input = adnp_gpio_direction_input; 258 chip->direction_output = adnp_gpio_direction_output; 259 chip->get = adnp_gpio_get; 260 chip->set = adnp_gpio_set; 261 chip->can_sleep = true; 262 263 if (IS_ENABLED(CONFIG_DEBUG_FS)) 264 chip->dbg_show = adnp_gpio_dbg_show; 265 266 chip->base = -1; 267 chip->ngpio = num_gpios; 268 chip->label = adnp->client->name; 269 chip->dev = &adnp->client->dev; 270 chip->of_node = chip->dev->of_node; 271 chip->owner = THIS_MODULE; 272 |
273 err = gpiochip_add(chip); 274 if (err) 275 return err; 276 |
|
275 return 0; 276} 277 278static irqreturn_t adnp_irq(int irq, void *data) 279{ 280 struct adnp *adnp = data; 281 unsigned int num_regs, i; 282 --- 38 unchanged lines hidden (view full) --- 321 pending |= (adnp->irq_high[i] & level) | 322 (adnp->irq_low[i] & ~level); 323 324 /* mask out non-pending and disabled interrupts */ 325 pending &= isr & ier; 326 327 for_each_set_bit(bit, &pending, 8) { 328 unsigned int child_irq; | 277 return 0; 278} 279 280static irqreturn_t adnp_irq(int irq, void *data) 281{ 282 struct adnp *adnp = data; 283 unsigned int num_regs, i; 284 --- 38 unchanged lines hidden (view full) --- 323 pending |= (adnp->irq_high[i] & level) | 324 (adnp->irq_low[i] & ~level); 325 326 /* mask out non-pending and disabled interrupts */ 327 pending &= isr & ier; 328 329 for_each_set_bit(bit, &pending, 8) { 330 unsigned int child_irq; |
329 child_irq = irq_find_mapping(adnp->domain, base + bit); | 331 child_irq = irq_find_mapping(adnp->gpio.irqdomain, 332 base + bit); |
330 handle_nested_irq(child_irq); 331 } 332 } 333 334 return IRQ_HANDLED; 335} 336 | 333 handle_nested_irq(child_irq); 334 } 335 } 336 337 return IRQ_HANDLED; 338} 339 |
337static int adnp_gpio_to_irq(struct gpio_chip *chip, unsigned offset) | 340static void adnp_irq_mask(struct irq_data *d) |
338{ | 341{ |
339 struct adnp *adnp = to_adnp(chip); 340 return irq_create_mapping(adnp->domain, offset); 341} | 342 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 343 struct adnp *adnp = to_adnp(gc); 344 unsigned int reg = d->hwirq >> adnp->reg_shift; 345 unsigned int pos = d->hwirq & 7; |
342 | 346 |
343static void adnp_irq_mask(struct irq_data *data) 344{ 345 struct adnp *adnp = irq_data_get_irq_chip_data(data); 346 unsigned int reg = data->hwirq >> adnp->reg_shift; 347 unsigned int pos = data->hwirq & 7; 348 | |
349 adnp->irq_enable[reg] &= ~BIT(pos); 350} 351 | 347 adnp->irq_enable[reg] &= ~BIT(pos); 348} 349 |
352static void adnp_irq_unmask(struct irq_data *data) | 350static void adnp_irq_unmask(struct irq_data *d) |
353{ | 351{ |
354 struct adnp *adnp = irq_data_get_irq_chip_data(data); 355 unsigned int reg = data->hwirq >> adnp->reg_shift; 356 unsigned int pos = data->hwirq & 7; | 352 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 353 struct adnp *adnp = to_adnp(gc); 354 unsigned int reg = d->hwirq >> adnp->reg_shift; 355 unsigned int pos = d->hwirq & 7; |
357 358 adnp->irq_enable[reg] |= BIT(pos); 359} 360 | 356 357 adnp->irq_enable[reg] |= BIT(pos); 358} 359 |
361static int adnp_irq_set_type(struct irq_data *data, unsigned int type) | 360static int adnp_irq_set_type(struct irq_data *d, unsigned int type) |
362{ | 361{ |
363 struct adnp *adnp = irq_data_get_irq_chip_data(data); 364 unsigned int reg = data->hwirq >> adnp->reg_shift; 365 unsigned int pos = data->hwirq & 7; | 362 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 363 struct adnp *adnp = to_adnp(gc); 364 unsigned int reg = d->hwirq >> adnp->reg_shift; 365 unsigned int pos = d->hwirq & 7; |
366 367 if (type & IRQ_TYPE_EDGE_RISING) 368 adnp->irq_rise[reg] |= BIT(pos); 369 else 370 adnp->irq_rise[reg] &= ~BIT(pos); 371 372 if (type & IRQ_TYPE_EDGE_FALLING) 373 adnp->irq_fall[reg] |= BIT(pos); --- 8 unchanged lines hidden (view full) --- 382 if (type & IRQ_TYPE_LEVEL_LOW) 383 adnp->irq_low[reg] |= BIT(pos); 384 else 385 adnp->irq_low[reg] &= ~BIT(pos); 386 387 return 0; 388} 389 | 366 367 if (type & IRQ_TYPE_EDGE_RISING) 368 adnp->irq_rise[reg] |= BIT(pos); 369 else 370 adnp->irq_rise[reg] &= ~BIT(pos); 371 372 if (type & IRQ_TYPE_EDGE_FALLING) 373 adnp->irq_fall[reg] |= BIT(pos); --- 8 unchanged lines hidden (view full) --- 382 if (type & IRQ_TYPE_LEVEL_LOW) 383 adnp->irq_low[reg] |= BIT(pos); 384 else 385 adnp->irq_low[reg] &= ~BIT(pos); 386 387 return 0; 388} 389 |
390static void adnp_irq_bus_lock(struct irq_data *data) | 390static void adnp_irq_bus_lock(struct irq_data *d) |
391{ | 391{ |
392 struct adnp *adnp = irq_data_get_irq_chip_data(data); | 392 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 393 struct adnp *adnp = to_adnp(gc); |
393 394 mutex_lock(&adnp->irq_lock); 395} 396 | 394 395 mutex_lock(&adnp->irq_lock); 396} 397 |
397static void adnp_irq_bus_unlock(struct irq_data *data) | 398static void adnp_irq_bus_unlock(struct irq_data *d) |
398{ | 399{ |
399 struct adnp *adnp = irq_data_get_irq_chip_data(data); | 400 struct gpio_chip *gc = irq_data_get_irq_chip_data(d); 401 struct adnp *adnp = to_adnp(gc); |
400 unsigned int num_regs = 1 << adnp->reg_shift, i; 401 402 mutex_lock(&adnp->i2c_lock); 403 404 for (i = 0; i < num_regs; i++) 405 adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]); 406 407 mutex_unlock(&adnp->i2c_lock); 408 mutex_unlock(&adnp->irq_lock); 409} 410 | 402 unsigned int num_regs = 1 << adnp->reg_shift, i; 403 404 mutex_lock(&adnp->i2c_lock); 405 406 for (i = 0; i < num_regs; i++) 407 adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]); 408 409 mutex_unlock(&adnp->i2c_lock); 410 mutex_unlock(&adnp->irq_lock); 411} 412 |
411static int adnp_irq_reqres(struct irq_data *data) 412{ 413 struct adnp *adnp = irq_data_get_irq_chip_data(data); 414 415 if (gpio_lock_as_irq(&adnp->gpio, data->hwirq)) { 416 dev_err(adnp->gpio.dev, 417 "unable to lock HW IRQ %lu for IRQ\n", 418 data->hwirq); 419 return -EINVAL; 420 } 421 return 0; 422} 423 424static void adnp_irq_relres(struct irq_data *data) 425{ 426 struct adnp *adnp = irq_data_get_irq_chip_data(data); 427 428 gpio_unlock_as_irq(&adnp->gpio, data->hwirq); 429} 430 | |
431static struct irq_chip adnp_irq_chip = { 432 .name = "gpio-adnp", 433 .irq_mask = adnp_irq_mask, 434 .irq_unmask = adnp_irq_unmask, 435 .irq_set_type = adnp_irq_set_type, 436 .irq_bus_lock = adnp_irq_bus_lock, 437 .irq_bus_sync_unlock = adnp_irq_bus_unlock, | 413static struct irq_chip adnp_irq_chip = { 414 .name = "gpio-adnp", 415 .irq_mask = adnp_irq_mask, 416 .irq_unmask = adnp_irq_unmask, 417 .irq_set_type = adnp_irq_set_type, 418 .irq_bus_lock = adnp_irq_bus_lock, 419 .irq_bus_sync_unlock = adnp_irq_bus_unlock, |
438 .irq_request_resources = adnp_irq_reqres, 439 .irq_release_resources = adnp_irq_relres, | |
440}; 441 | 420}; 421 |
442static int adnp_irq_map(struct irq_domain *domain, unsigned int irq, 443 irq_hw_number_t hwirq) 444{ 445 irq_set_chip_data(irq, domain->host_data); 446 irq_set_chip(irq, &adnp_irq_chip); 447 irq_set_nested_thread(irq, true); 448 449#ifdef CONFIG_ARM 450 set_irq_flags(irq, IRQF_VALID); 451#else 452 irq_set_noprobe(irq); 453#endif 454 455 return 0; 456} 457 458static const struct irq_domain_ops adnp_irq_domain_ops = { 459 .map = adnp_irq_map, 460 .xlate = irq_domain_xlate_twocell, 461}; 462 | |
463static int adnp_irq_setup(struct adnp *adnp) 464{ 465 unsigned int num_regs = 1 << adnp->reg_shift, i; 466 struct gpio_chip *chip = &adnp->gpio; 467 int err; 468 469 mutex_init(&adnp->irq_lock); 470 --- 27 unchanged lines hidden (view full) --- 498 /* disable all interrupts */ 499 err = adnp_write(adnp, GPIO_IER(adnp) + i, 0); 500 if (err < 0) 501 return err; 502 503 adnp->irq_enable[i] = 0x00; 504 } 505 | 422static int adnp_irq_setup(struct adnp *adnp) 423{ 424 unsigned int num_regs = 1 << adnp->reg_shift, i; 425 struct gpio_chip *chip = &adnp->gpio; 426 int err; 427 428 mutex_init(&adnp->irq_lock); 429 --- 27 unchanged lines hidden (view full) --- 457 /* disable all interrupts */ 458 err = adnp_write(adnp, GPIO_IER(adnp) + i, 0); 459 if (err < 0) 460 return err; 461 462 adnp->irq_enable[i] = 0x00; 463 } 464 |
506 adnp->domain = irq_domain_add_linear(chip->of_node, chip->ngpio, 507 &adnp_irq_domain_ops, adnp); 508 509 err = request_threaded_irq(adnp->client->irq, NULL, adnp_irq, 510 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 511 dev_name(chip->dev), adnp); | 465 err = devm_request_threaded_irq(chip->dev, adnp->client->irq, 466 NULL, adnp_irq, 467 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 468 dev_name(chip->dev), adnp); |
512 if (err != 0) { 513 dev_err(chip->dev, "can't request IRQ#%d: %d\n", 514 adnp->client->irq, err); 515 return err; 516 } 517 | 469 if (err != 0) { 470 dev_err(chip->dev, "can't request IRQ#%d: %d\n", 471 adnp->client->irq, err); 472 return err; 473 } 474 |
518 chip->to_irq = adnp_gpio_to_irq; 519 return 0; 520} 521 522static void adnp_irq_teardown(struct adnp *adnp) 523{ 524 unsigned int irq, i; 525 526 free_irq(adnp->client->irq, adnp); 527 528 for (i = 0; i < adnp->gpio.ngpio; i++) { 529 irq = irq_find_mapping(adnp->domain, i); 530 if (irq > 0) 531 irq_dispose_mapping(irq); | 475 err = gpiochip_irqchip_add(chip, 476 &adnp_irq_chip, 477 0, 478 handle_simple_irq, 479 IRQ_TYPE_NONE); 480 if (err) { 481 dev_err(chip->dev, 482 "could not connect irqchip to gpiochip\n"); 483 return err; |
532 } 533 | 484 } 485 |
534 irq_domain_remove(adnp->domain); | 486 return 0; |
535} 536 537static int adnp_i2c_probe(struct i2c_client *client, 538 const struct i2c_device_id *id) 539{ 540 struct device_node *np = client->dev.of_node; 541 struct adnp *adnp; 542 u32 num_gpios; --- 10 unchanged lines hidden (view full) --- 553 adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL); 554 if (!adnp) 555 return -ENOMEM; 556 557 mutex_init(&adnp->i2c_lock); 558 adnp->client = client; 559 560 err = adnp_gpio_setup(adnp, num_gpios); | 487} 488 489static int adnp_i2c_probe(struct i2c_client *client, 490 const struct i2c_device_id *id) 491{ 492 struct device_node *np = client->dev.of_node; 493 struct adnp *adnp; 494 u32 num_gpios; --- 10 unchanged lines hidden (view full) --- 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 512 err = adnp_gpio_setup(adnp, num_gpios); |
561 if (err < 0) | 513 if (err) |
562 return err; 563 564 if (of_find_property(np, "interrupt-controller", NULL)) { 565 err = adnp_irq_setup(adnp); | 514 return err; 515 516 if (of_find_property(np, "interrupt-controller", NULL)) { 517 err = adnp_irq_setup(adnp); |
566 if (err < 0) 567 goto teardown; | 518 if (err) 519 return err; |
568 } 569 | 520 } 521 |
570 err = gpiochip_add(&adnp->gpio); 571 if (err < 0) 572 goto teardown; 573 | |
574 i2c_set_clientdata(client, adnp); | 522 i2c_set_clientdata(client, adnp); |
575 return 0; | |
576 | 523 |
577teardown: 578 if (of_find_property(np, "interrupt-controller", NULL)) 579 adnp_irq_teardown(adnp); 580 581 return err; | 524 return 0; |
582} 583 584static int adnp_i2c_remove(struct i2c_client *client) 585{ 586 struct adnp *adnp = i2c_get_clientdata(client); | 525} 526 527static int adnp_i2c_remove(struct i2c_client *client) 528{ 529 struct adnp *adnp = i2c_get_clientdata(client); |
587 struct device_node *np = client->dev.of_node; | |
588 589 gpiochip_remove(&adnp->gpio); | 530 531 gpiochip_remove(&adnp->gpio); |
590 if (of_find_property(np, "interrupt-controller", NULL)) 591 adnp_irq_teardown(adnp); 592 | |
593 return 0; 594} 595 596static const struct i2c_device_id adnp_i2c_id[] = { 597 { "gpio-adnp" }, 598 { }, 599}; 600MODULE_DEVICE_TABLE(i2c, adnp_i2c_id); --- 22 unchanged lines hidden --- | 532 return 0; 533} 534 535static const struct i2c_device_id adnp_i2c_id[] = { 536 { "gpio-adnp" }, 537 { }, 538}; 539MODULE_DEVICE_TABLE(i2c, adnp_i2c_id); --- 22 unchanged lines hidden --- |