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