1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011-2012 Avionic Design GmbH
6 #include <linux/gpio/driver.h>
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/of_irq.h>
11 #include <linux/seq_file.h>
12 #include <linux/slab.h>
14 #define GPIO_DDR(gpio) (0x00 << (gpio)->reg_shift)
15 #define GPIO_PLR(gpio) (0x01 << (gpio)->reg_shift)
16 #define GPIO_IER(gpio) (0x02 << (gpio)->reg_shift)
17 #define GPIO_ISR(gpio) (0x03 << (gpio)->reg_shift)
18 #define GPIO_PTR(gpio) (0x04 << (gpio)->reg_shift)
21 struct i2c_client *client;
22 struct gpio_chip gpio;
23 unsigned int reg_shift;
25 struct mutex i2c_lock;
26 struct mutex irq_lock;
36 static int adnp_read(struct adnp *adnp, unsigned offset, uint8_t *value)
40 err = i2c_smbus_read_byte_data(adnp->client, offset);
42 dev_err(adnp->gpio.parent, "%s failed: %d\n",
43 "i2c_smbus_read_byte_data()", err);
51 static int adnp_write(struct adnp *adnp, unsigned offset, uint8_t value)
55 err = i2c_smbus_write_byte_data(adnp->client, offset, value);
57 dev_err(adnp->gpio.parent, "%s failed: %d\n",
58 "i2c_smbus_write_byte_data()", err);
65 static int adnp_gpio_get(struct gpio_chip *chip, unsigned offset)
67 struct adnp *adnp = gpiochip_get_data(chip);
68 unsigned int reg = offset >> adnp->reg_shift;
69 unsigned int pos = offset & 7;
73 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &value);
77 return (value & BIT(pos)) ? 1 : 0;
80 static void __adnp_gpio_set(struct adnp *adnp, unsigned offset, int value)
82 unsigned int reg = offset >> adnp->reg_shift;
83 unsigned int pos = offset & 7;
87 err = adnp_read(adnp, GPIO_PLR(adnp) + reg, &val);
96 adnp_write(adnp, GPIO_PLR(adnp) + reg, val);
99 static void adnp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
101 struct adnp *adnp = gpiochip_get_data(chip);
103 mutex_lock(&adnp->i2c_lock);
104 __adnp_gpio_set(adnp, offset, value);
105 mutex_unlock(&adnp->i2c_lock);
108 static int adnp_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
110 struct adnp *adnp = gpiochip_get_data(chip);
111 unsigned int reg = offset >> adnp->reg_shift;
112 unsigned int pos = offset & 7;
116 mutex_lock(&adnp->i2c_lock);
118 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
124 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, value);
128 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &value);
132 if (value & BIT(pos)) {
140 mutex_unlock(&adnp->i2c_lock);
144 static int adnp_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
147 struct adnp *adnp = gpiochip_get_data(chip);
148 unsigned int reg = offset >> adnp->reg_shift;
149 unsigned int pos = offset & 7;
153 mutex_lock(&adnp->i2c_lock);
155 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
161 err = adnp_write(adnp, GPIO_DDR(adnp) + reg, val);
165 err = adnp_read(adnp, GPIO_DDR(adnp) + reg, &val);
169 if (!(val & BIT(pos))) {
174 __adnp_gpio_set(adnp, offset, value);
178 mutex_unlock(&adnp->i2c_lock);
182 static void adnp_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
184 struct adnp *adnp = gpiochip_get_data(chip);
185 unsigned int num_regs = 1 << adnp->reg_shift, i, j;
188 for (i = 0; i < num_regs; i++) {
189 u8 ddr, plr, ier, isr;
191 mutex_lock(&adnp->i2c_lock);
193 err = adnp_read(adnp, GPIO_DDR(adnp) + i, &ddr);
197 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &plr);
201 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
205 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
209 mutex_unlock(&adnp->i2c_lock);
211 for (j = 0; j < 8; j++) {
212 unsigned int bit = (i << adnp->reg_shift) + j;
213 const char *direction = "input ";
214 const char *level = "low ";
215 const char *interrupt = "disabled";
216 const char *pending = "";
219 direction = "output";
225 interrupt = "enabled ";
230 seq_printf(s, "%2u: %s %s IRQ %s %s\n", bit,
231 direction, level, interrupt, pending);
238 mutex_unlock(&adnp->i2c_lock);
241 static irqreturn_t adnp_irq(int irq, void *data)
243 struct adnp *adnp = data;
244 unsigned int num_regs, i;
246 num_regs = 1 << adnp->reg_shift;
248 for (i = 0; i < num_regs; i++) {
249 unsigned int base = i << adnp->reg_shift, bit;
250 u8 changed, level, isr, ier;
251 unsigned long pending;
254 mutex_lock(&adnp->i2c_lock);
256 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &level);
258 mutex_unlock(&adnp->i2c_lock);
262 err = adnp_read(adnp, GPIO_ISR(adnp) + i, &isr);
264 mutex_unlock(&adnp->i2c_lock);
268 err = adnp_read(adnp, GPIO_IER(adnp) + i, &ier);
270 mutex_unlock(&adnp->i2c_lock);
274 mutex_unlock(&adnp->i2c_lock);
276 /* determine pins that changed levels */
277 changed = level ^ adnp->irq_level[i];
279 /* compute edge-triggered interrupts */
280 pending = changed & ((adnp->irq_fall[i] & ~level) |
281 (adnp->irq_rise[i] & level));
283 /* add in level-triggered interrupts */
284 pending |= (adnp->irq_high[i] & level) |
285 (adnp->irq_low[i] & ~level);
287 /* mask out non-pending and disabled interrupts */
288 pending &= isr & ier;
290 for_each_set_bit(bit, &pending, 8) {
291 unsigned int child_irq;
292 child_irq = irq_find_mapping(adnp->gpio.irq.domain,
294 handle_nested_irq(child_irq);
301 static void adnp_irq_mask(struct irq_data *d)
303 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
304 struct adnp *adnp = gpiochip_get_data(gc);
305 unsigned int reg = d->hwirq >> adnp->reg_shift;
306 unsigned int pos = d->hwirq & 7;
308 adnp->irq_enable[reg] &= ~BIT(pos);
311 static void adnp_irq_unmask(struct irq_data *d)
313 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
314 struct adnp *adnp = gpiochip_get_data(gc);
315 unsigned int reg = d->hwirq >> adnp->reg_shift;
316 unsigned int pos = d->hwirq & 7;
318 adnp->irq_enable[reg] |= BIT(pos);
321 static int adnp_irq_set_type(struct irq_data *d, unsigned int type)
323 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
324 struct adnp *adnp = gpiochip_get_data(gc);
325 unsigned int reg = d->hwirq >> adnp->reg_shift;
326 unsigned int pos = d->hwirq & 7;
328 if (type & IRQ_TYPE_EDGE_RISING)
329 adnp->irq_rise[reg] |= BIT(pos);
331 adnp->irq_rise[reg] &= ~BIT(pos);
333 if (type & IRQ_TYPE_EDGE_FALLING)
334 adnp->irq_fall[reg] |= BIT(pos);
336 adnp->irq_fall[reg] &= ~BIT(pos);
338 if (type & IRQ_TYPE_LEVEL_HIGH)
339 adnp->irq_high[reg] |= BIT(pos);
341 adnp->irq_high[reg] &= ~BIT(pos);
343 if (type & IRQ_TYPE_LEVEL_LOW)
344 adnp->irq_low[reg] |= BIT(pos);
346 adnp->irq_low[reg] &= ~BIT(pos);
351 static void adnp_irq_bus_lock(struct irq_data *d)
353 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
354 struct adnp *adnp = gpiochip_get_data(gc);
356 mutex_lock(&adnp->irq_lock);
359 static void adnp_irq_bus_unlock(struct irq_data *d)
361 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
362 struct adnp *adnp = gpiochip_get_data(gc);
363 unsigned int num_regs = 1 << adnp->reg_shift, i;
365 mutex_lock(&adnp->i2c_lock);
367 for (i = 0; i < num_regs; i++)
368 adnp_write(adnp, GPIO_IER(adnp) + i, adnp->irq_enable[i]);
370 mutex_unlock(&adnp->i2c_lock);
371 mutex_unlock(&adnp->irq_lock);
374 static struct irq_chip adnp_irq_chip = {
376 .irq_mask = adnp_irq_mask,
377 .irq_unmask = adnp_irq_unmask,
378 .irq_set_type = adnp_irq_set_type,
379 .irq_bus_lock = adnp_irq_bus_lock,
380 .irq_bus_sync_unlock = adnp_irq_bus_unlock,
383 static int adnp_irq_setup(struct adnp *adnp)
385 unsigned int num_regs = 1 << adnp->reg_shift, i;
386 struct gpio_chip *chip = &adnp->gpio;
389 mutex_init(&adnp->irq_lock);
392 * Allocate memory to keep track of the current level and trigger
393 * modes of the interrupts. To avoid multiple allocations, a single
394 * large buffer is allocated and pointers are setup to point at the
395 * corresponding offsets. For consistency, the layout of the buffer
396 * is chosen to match the register layout of the hardware in that
397 * each segment contains the corresponding bits for all interrupts.
399 adnp->irq_enable = devm_kcalloc(chip->parent, num_regs, 6,
401 if (!adnp->irq_enable)
404 adnp->irq_level = adnp->irq_enable + (num_regs * 1);
405 adnp->irq_rise = adnp->irq_enable + (num_regs * 2);
406 adnp->irq_fall = adnp->irq_enable + (num_regs * 3);
407 adnp->irq_high = adnp->irq_enable + (num_regs * 4);
408 adnp->irq_low = adnp->irq_enable + (num_regs * 5);
410 for (i = 0; i < num_regs; i++) {
412 * Read the initial level of all pins to allow the emulation
413 * of edge triggered interrupts.
415 err = adnp_read(adnp, GPIO_PLR(adnp) + i, &adnp->irq_level[i]);
419 /* disable all interrupts */
420 err = adnp_write(adnp, GPIO_IER(adnp) + i, 0);
424 adnp->irq_enable[i] = 0x00;
427 err = devm_request_threaded_irq(chip->parent, adnp->client->irq,
429 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
430 dev_name(chip->parent), adnp);
432 dev_err(chip->parent, "can't request IRQ#%d: %d\n",
433 adnp->client->irq, err);
440 static int adnp_gpio_setup(struct adnp *adnp, unsigned int num_gpios,
441 bool is_irq_controller)
443 struct gpio_chip *chip = &adnp->gpio;
446 adnp->reg_shift = get_count_order(num_gpios) - 3;
448 chip->direction_input = adnp_gpio_direction_input;
449 chip->direction_output = adnp_gpio_direction_output;
450 chip->get = adnp_gpio_get;
451 chip->set = adnp_gpio_set;
452 chip->can_sleep = true;
454 if (IS_ENABLED(CONFIG_DEBUG_FS))
455 chip->dbg_show = adnp_gpio_dbg_show;
458 chip->ngpio = num_gpios;
459 chip->label = adnp->client->name;
460 chip->parent = &adnp->client->dev;
461 chip->of_node = chip->parent->of_node;
462 chip->owner = THIS_MODULE;
464 if (is_irq_controller) {
465 struct gpio_irq_chip *girq;
467 err = adnp_irq_setup(adnp);
472 girq->chip = &adnp_irq_chip;
473 /* This will let us handle the parent IRQ in the driver */
474 girq->parent_handler = NULL;
475 girq->num_parents = 0;
476 girq->parents = NULL;
477 girq->default_type = IRQ_TYPE_NONE;
478 girq->handler = handle_simple_irq;
479 girq->threaded = true;
482 err = devm_gpiochip_add_data(&adnp->client->dev, chip, adnp);
489 static int adnp_i2c_probe(struct i2c_client *client,
490 const struct i2c_device_id *id)
492 struct device_node *np = client->dev.of_node;
497 err = of_property_read_u32(np, "nr-gpios", &num_gpios);
501 client->irq = irq_of_parse_and_map(np, 0);
503 return -EPROBE_DEFER;
505 adnp = devm_kzalloc(&client->dev, sizeof(*adnp), GFP_KERNEL);
509 mutex_init(&adnp->i2c_lock);
510 adnp->client = client;
512 err = adnp_gpio_setup(adnp, num_gpios,
513 of_property_read_bool(np, "interrupt-controller"));
517 i2c_set_clientdata(client, adnp);
522 static const struct i2c_device_id adnp_i2c_id[] = {
526 MODULE_DEVICE_TABLE(i2c, adnp_i2c_id);
528 static const struct of_device_id adnp_of_match[] = {
529 { .compatible = "ad,gpio-adnp", },
532 MODULE_DEVICE_TABLE(of, adnp_of_match);
534 static struct i2c_driver adnp_i2c_driver = {
537 .of_match_table = adnp_of_match,
539 .probe = adnp_i2c_probe,
540 .id_table = adnp_i2c_id,
542 module_i2c_driver(adnp_i2c_driver);
544 MODULE_DESCRIPTION("Avionic Design N-bit GPIO expander");
545 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
546 MODULE_LICENSE("GPL");