2 * Copyright (C) 2011-2012 Avionic Design GmbH
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.
9 #include <linux/gpio.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>
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)
25 struct i2c_client *client;
26 struct gpio_chip gpio;
27 unsigned int reg_shift;
29 struct mutex i2c_lock;
31 struct irq_domain *domain;
32 struct mutex irq_lock;
42 static inline struct adnp *to_adnp(struct gpio_chip *chip)
44 return container_of(chip, struct adnp, gpio);
47 static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value)
51 err = i2c_smbus_read_byte_data(adnp->client, offset);
53 dev_err(adnp->gpio.dev, "%s failed: %d\n",
54 "i2c_smbus_read_byte_data()", err);
62 static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value)
66 err = i2c_smbus_write_byte_data(adnp->client, offset, value);
68 dev_err(adnp->gpio.dev, "%s failed: %d\n",
69 "i2c_smbus_write_byte_data()", err);
76 static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset)
78 struct adnp *adnp = to_adnp(chip);
79 unsigned int reg = offset >> adnp->reg_shift;
80 unsigned int pos = offset & 7;
84 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value);
88 return (value & BIT(pos)) ? 1 : 0;
91 static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value)
93 unsigned int reg = offset >> adnp->reg_shift;
94 unsigned int pos = offset & 7;
98 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val);
107 adnp_write(adnp, GPIO_PLR(adnp) + reg, val);
110 static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
112 struct adnp *adnp = to_adnp(chip);
114 mutex_lock(&adnp->i2c_lock);
115 __adnp_gpio_set(adnp, offset, value);
116 mutex_unlock(&adnp->i2c_lock);
119 static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
121 struct adnp *adnp = to_adnp(chip);
122 unsigned int reg = offset >> adnp->reg_shift;
123 unsigned int pos = offset & 7;
127 mutex_lock(&adnp->i2c_lock);
129 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
135 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value);
139 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
149 mutex_unlock(&adnp->i2c_lock);
153 static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
156 struct adnp *adnp = to_adnp(chip);
157 unsigned int reg = offset >> adnp->reg_shift;
158 unsigned int pos = offset & 7;
162 mutex_lock(&adnp->i2c_lock);
164 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
170 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val);
174 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
178 if (!(val & BIT(pos))) {
183 __adnp_gpio_set(adnp, offset, value);
187 mutex_unlock(&adnp->i2c_lock);
191 static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
193 struct adnp *adnp = to_adnp(chip);
194 unsigned int num_regs = 1 << adnp->reg_shift, i, j;
197 for (i = 0; i < num_regs; i++) {
198 u8 ddr, plr, ier, isr;
200 mutex_lock(&adnp->i2c_lock);
202 err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr);
204 mutex_unlock(&adnp->i2c_lock);
208 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr);
210 mutex_unlock(&adnp->i2c_lock);
214 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
216 mutex_unlock(&adnp->i2c_lock);
220 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
222 mutex_unlock(&adnp->i2c_lock);
226 mutex_unlock(&adnp->i2c_lock);
228 for (j = 0; j < 8; j++) {
229 unsigned int bit = (i << adnp->reg_shift) + j;
230 const char *direction = "input ";
231 const char *level = "low ";
232 const char *interrupt = "disabled";
233 const char *pending = "";
236 direction = "output";
242 interrupt = "enabled ";
247 seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit,
248 direction, level, interrupt, pending);
253 static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios)
255 struct gpio_chip *chip = &adnp->gpio;
257 adnp->reg_shift = get_count_order(num_gpios) - 3;
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;
265 if (IS_ENABLED(CONFIG_DEBUG_FS))
266 chip->dbg_show = adnp_gpio_dbg_show;
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;
278 static irqreturn_t adnp_irq(int irq, void *data)
280 struct adnp *adnp = data;
281 unsigned int num_regs, i;
283 num_regs = 1 << adnp->reg_shift;
285 for (i = 0; i < num_regs; i++) {
286 unsigned int base = i << adnp->reg_shift, bit;
287 u8 changed, level, isr, ier;
288 unsigned long pending;
291 mutex_lock(&adnp->i2c_lock);
293 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level);
295 mutex_unlock(&adnp->i2c_lock);
299 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
301 mutex_unlock(&adnp->i2c_lock);
305 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
307 mutex_unlock(&adnp->i2c_lock);
311 mutex_unlock(&adnp->i2c_lock);
313 /* determine pins that changed levels */
314 changed = level ^ adnp->irq_level[i];
316 /* compute edge-triggered interrupts */
317 pending = changed & ((adnp->irq_fall[i] & ~level) |
318 (adnp->irq_rise[i] & level));
320 /* add in level-triggered interrupts */
321 pending |= (adnp->irq_high[i] & level) |
322 (adnp->irq_low[i] & ~level);
324 /* mask out non-pending and disabled interrupts */
325 pending &= isr & ier;
327 for_each_set_bit(bit, &pending, 8) {
328 unsigned int child_irq;
329 child_irq = irq_find_mapping(adnp->domain, base + bit);
330 handle_nested_irq(child_irq);
337 static int adnp_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
339 struct adnp *adnp = to_adnp(chip);
340 return irq_create_mapping(adnp->domain, offset);
343 static void adnp_irq_mask(struct irq_data *data)
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;
349 adnp->irq_enable[reg] &= ~BIT(pos);
352 static void adnp_irq_unmask(struct irq_data *data)
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;
358 adnp->irq_enable[reg] |= BIT(pos);
361 static int adnp_irq_set_type(struct irq_data *data, unsigned int type)
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;
367 if (type & IRQ_TYPE_EDGE_RISING)
368 adnp->irq_rise[reg] |= BIT(pos);
370 adnp->irq_rise[reg] &= ~BIT(pos);
372 if (type & IRQ_TYPE_EDGE_FALLING)
373 adnp->irq_fall[reg] |= BIT(pos);
375 adnp->irq_fall[reg] &= ~BIT(pos);
377 if (type & IRQ_TYPE_LEVEL_HIGH)
378 adnp->irq_high[reg] |= BIT(pos);
380 adnp->irq_high[reg] &= ~BIT(pos);
382 if (type & IRQ_TYPE_LEVEL_LOW)
383 adnp->irq_low[reg] |= BIT(pos);
385 adnp->irq_low[reg] &= ~BIT(pos);
390 static void adnp_irq_bus_lock(struct irq_data *data)
392 struct adnp *adnp = irq_data_get_irq_chip_data(data);
394 mutex_lock(&adnp->irq_lock);
397 static void adnp_irq_bus_unlock(struct irq_data *data)
399 struct adnp *adnp = irq_data_get_irq_chip_data(data);
400 unsigned int num_regs = 1 << adnp->reg_shift, i;
402 mutex_lock(&adnp->i2c_lock);
404 for (i = 0; i < num_regs; i++)
405 adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]);
407 mutex_unlock(&adnp->i2c_lock);
408 mutex_unlock(&adnp->irq_lock);
411 static int adnp_irq_reqres(struct irq_data *data)
413 struct adnp *adnp = irq_data_get_irq_chip_data(data);
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",
424 static void adnp_irq_relres(struct irq_data *data)
426 struct adnp *adnp = irq_data_get_irq_chip_data(data);
428 gpio_unlock_as_irq(&adnp->gpio, data->hwirq);
431 static struct irq_chip adnp_irq_chip = {
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,
438 .irq_request_resources = adnp_irq_reqres,
439 .irq_release_resources = adnp_irq_relres,
442 static int adnp_irq_map(struct irq_domain *domain, unsigned int irq,
443 irq_hw_number_t hwirq)
445 irq_set_chip_data(irq, domain->host_data);
446 irq_set_chip(irq, &adnp_irq_chip);
447 irq_set_nested_thread(irq, true);
450 set_irq_flags(irq, IRQF_VALID);
452 irq_set_noprobe(irq);
458 static const struct irq_domain_ops adnp_irq_domain_ops = {
460 .xlate = irq_domain_xlate_twocell,
463 static int adnp_irq_setup(struct adnp *adnp)
465 unsigned int num_regs = 1 << adnp->reg_shift, i;
466 struct gpio_chip *chip = &adnp->gpio;
469 mutex_init(&adnp->irq_lock);
472 * Allocate memory to keep track of the current level and trigger
473 * modes of the interrupts. To avoid multiple allocations, a single
474 * large buffer is allocated and pointers are setup to point at the
475 * corresponding offsets. For consistency, the layout of the buffer
476 * is chosen to match the register layout of the hardware in that
477 * each segment contains the corresponding bits for all interrupts.
479 adnp->irq_enable = devm_kzalloc(chip->dev, num_regs * 6, GFP_KERNEL);
480 if (!adnp->irq_enable)
483 adnp->irq_level = adnp->irq_enable + (num_regs * 1);
484 adnp->irq_rise = adnp->irq_enable + (num_regs * 2);
485 adnp->irq_fall = adnp->irq_enable + (num_regs * 3);
486 adnp->irq_high = adnp->irq_enable + (num_regs * 4);
487 adnp->irq_low = adnp->irq_enable + (num_regs * 5);
489 for (i = 0; i < num_regs; i++) {
491 * Read the initial level of all pins to allow the emulation
492 * of edge triggered interrupts.
494 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]);
498 /* disable all interrupts */
499 err = adnp_write(adnp, GPIO_IER(adnp) + i, 0);
503 adnp->irq_enable[i] = 0x00;
506 adnp->domain = irq_domain_add_linear(chip->of_node, chip->ngpio,
507 &adnp_irq_domain_ops, adnp);
509 err = request_threaded_irq(adnp->client->irq, NULL, adnp_irq,
510 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
511 dev_name(chip->dev), adnp);
513 dev_err(chip->dev, "can't request IRQ#%d: %d\n",
514 adnp->client->irq, err);
518 chip->to_irq = adnp_gpio_to_irq;
522 static void adnp_irq_teardown(struct adnp *adnp)
526 free_irq(adnp->client->irq, adnp);
528 for (i = 0; i < adnp->gpio.ngpio; i++) {
529 irq = irq_find_mapping(adnp->domain, i);
531 irq_dispose_mapping(irq);
534 irq_domain_remove(adnp->domain);
537 static int adnp_i2c_probe(struct i2c_client *client,
538 const struct i2c_device_id *id)
540 struct device_node *np = client->dev.of_node;
545 err = of_property_read_u32(np, "nr-gpios", &num_gpios);
549 client->irq = irq_of_parse_and_map(np, 0);
551 return -EPROBE_DEFER;
553 adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL);
557 mutex_init(&adnp->i2c_lock);
558 adnp->client = client;
560 err = adnp_gpio_setup(adnp, num_gpios);
564 if (of_find_property(np, "interrupt-controller", NULL)) {
565 err = adnp_irq_setup(adnp);
570 err = gpiochip_add(&adnp->gpio);
574 i2c_set_clientdata(client, adnp);
578 if (of_find_property(np, "interrupt-controller", NULL))
579 adnp_irq_teardown(adnp);
584 static int adnp_i2c_remove(struct i2c_client *client)
586 struct adnp *adnp = i2c_get_clientdata(client);
587 struct device_node *np = client->dev.of_node;
590 err = gpiochip_remove(&adnp->gpio);
592 dev_err(&client->dev, "%s failed: %d\n", "gpiochip_remove()",
597 if (of_find_property(np, "interrupt-controller", NULL))
598 adnp_irq_teardown(adnp);
603 static const struct i2c_device_id adnp_i2c_id[] = {
607 MODULE_DEVICE_TABLE(i2c, adnp_i2c_id);
609 static const struct of_device_id adnp_of_match[] = {
610 { .compatible = "ad,gpio-adnp", },
613 MODULE_DEVICE_TABLE(of, adnp_of_match);
615 static struct i2c_driver adnp_i2c_driver = {
618 .owner = THIS_MODULE,
619 .of_match_table = adnp_of_match,
621 .probe = adnp_i2c_probe,
622 .remove = adnp_i2c_remove,
623 .id_table = adnp_i2c_id,
625 module_i2c_driver(adnp_i2c_driver);
627 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
628 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
629 MODULE_LICENSE("GPL");