blktrace: remove unnessary stop block trace in 'blk_trace_shutdown'
[linux-block.git] / drivers / gpio / gpio-adp5588.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * GPIO Chip driver for Analog Devices
4  * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
5  *
6  * Copyright 2009-2010 Analog Devices Inc.
7  */
8
9 #include <linux/gpio/driver.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18
19 #include <linux/platform_data/adp5588.h>
20
21 /*
22  * Early pre 4.0 Silicon required to delay readout by at least 25ms,
23  * since the Event Counter Register updated 25ms after the interrupt
24  * asserted.
25  */
26 #define WA_DELAYED_READOUT_REVID(rev)   ((rev) < 4)
27
28 struct adp5588_gpio {
29         struct i2c_client *client;
30         struct gpio_chip gpio_chip;
31         struct mutex lock;      /* protect cached dir, dat_out */
32         /* protect serialized access to the interrupt controller bus */
33         struct mutex irq_lock;
34         uint8_t dat_out[3];
35         uint8_t dir[3];
36         uint8_t int_lvl_low[3];
37         uint8_t int_lvl_high[3];
38         uint8_t int_en[3];
39         uint8_t irq_mask[3];
40         uint8_t int_input_en[3];
41 };
42
43 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
44 {
45         int ret = i2c_smbus_read_byte_data(client, reg);
46
47         if (ret < 0)
48                 dev_err(&client->dev, "Read Error\n");
49
50         return ret;
51 }
52
53 static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
54 {
55         int ret = i2c_smbus_write_byte_data(client, reg, val);
56
57         if (ret < 0)
58                 dev_err(&client->dev, "Write Error\n");
59
60         return ret;
61 }
62
63 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
64 {
65         struct adp5588_gpio *dev = gpiochip_get_data(chip);
66         unsigned bank = ADP5588_BANK(off);
67         unsigned bit = ADP5588_BIT(off);
68         int val;
69
70         mutex_lock(&dev->lock);
71
72         if (dev->dir[bank] & bit)
73                 val = dev->dat_out[bank];
74         else
75                 val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
76
77         mutex_unlock(&dev->lock);
78
79         return !!(val & bit);
80 }
81
82 static void adp5588_gpio_set_value(struct gpio_chip *chip,
83                                    unsigned off, int val)
84 {
85         unsigned bank, bit;
86         struct adp5588_gpio *dev = gpiochip_get_data(chip);
87
88         bank = ADP5588_BANK(off);
89         bit = ADP5588_BIT(off);
90
91         mutex_lock(&dev->lock);
92         if (val)
93                 dev->dat_out[bank] |= bit;
94         else
95                 dev->dat_out[bank] &= ~bit;
96
97         adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
98                            dev->dat_out[bank]);
99         mutex_unlock(&dev->lock);
100 }
101
102 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
103 {
104         int ret;
105         unsigned bank;
106         struct adp5588_gpio *dev = gpiochip_get_data(chip);
107
108         bank = ADP5588_BANK(off);
109
110         mutex_lock(&dev->lock);
111         dev->dir[bank] &= ~ADP5588_BIT(off);
112         ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
113         mutex_unlock(&dev->lock);
114
115         return ret;
116 }
117
118 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
119                                          unsigned off, int val)
120 {
121         int ret;
122         unsigned bank, bit;
123         struct adp5588_gpio *dev = gpiochip_get_data(chip);
124
125         bank = ADP5588_BANK(off);
126         bit = ADP5588_BIT(off);
127
128         mutex_lock(&dev->lock);
129         dev->dir[bank] |= bit;
130
131         if (val)
132                 dev->dat_out[bank] |= bit;
133         else
134                 dev->dat_out[bank] &= ~bit;
135
136         ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
137                                  dev->dat_out[bank]);
138         ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
139                                  dev->dir[bank]);
140         mutex_unlock(&dev->lock);
141
142         return ret;
143 }
144
145 #ifdef CONFIG_GPIO_ADP5588_IRQ
146
147 static void adp5588_irq_bus_lock(struct irq_data *d)
148 {
149         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
150         struct adp5588_gpio *dev = gpiochip_get_data(gc);
151
152         mutex_lock(&dev->irq_lock);
153 }
154
155  /*
156   * genirq core code can issue chip->mask/unmask from atomic context.
157   * This doesn't work for slow busses where an access needs to sleep.
158   * bus_sync_unlock() is therefore called outside the atomic context,
159   * syncs the current irq mask state with the slow external controller
160   * and unlocks the bus.
161   */
162
163 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
164 {
165         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
166         struct adp5588_gpio *dev = gpiochip_get_data(gc);
167         int i;
168
169         for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
170                 if (dev->int_input_en[i]) {
171                         mutex_lock(&dev->lock);
172                         dev->dir[i] &= ~dev->int_input_en[i];
173                         dev->int_input_en[i] = 0;
174                         adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
175                                            dev->dir[i]);
176                         mutex_unlock(&dev->lock);
177                 }
178
179                 if (dev->int_en[i] ^ dev->irq_mask[i]) {
180                         dev->int_en[i] = dev->irq_mask[i];
181                         adp5588_gpio_write(dev->client, GPI_EM1 + i,
182                                            dev->int_en[i]);
183                 }
184         }
185
186         mutex_unlock(&dev->irq_lock);
187 }
188
189 static void adp5588_irq_mask(struct irq_data *d)
190 {
191         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
192         struct adp5588_gpio *dev = gpiochip_get_data(gc);
193
194         dev->irq_mask[ADP5588_BANK(d->hwirq)] &= ~ADP5588_BIT(d->hwirq);
195 }
196
197 static void adp5588_irq_unmask(struct irq_data *d)
198 {
199         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
200         struct adp5588_gpio *dev = gpiochip_get_data(gc);
201
202         dev->irq_mask[ADP5588_BANK(d->hwirq)] |= ADP5588_BIT(d->hwirq);
203 }
204
205 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
206 {
207         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
208         struct adp5588_gpio *dev = gpiochip_get_data(gc);
209         uint16_t gpio = d->hwirq;
210         unsigned bank, bit;
211
212         bank = ADP5588_BANK(gpio);
213         bit = ADP5588_BIT(gpio);
214
215         dev->int_lvl_low[bank] &= ~bit;
216         dev->int_lvl_high[bank] &= ~bit;
217
218         if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_HIGH)
219                 dev->int_lvl_high[bank] |= bit;
220
221         if (type & IRQ_TYPE_EDGE_BOTH || type & IRQ_TYPE_LEVEL_LOW)
222                 dev->int_lvl_low[bank] |= bit;
223
224         dev->int_input_en[bank] |= bit;
225
226         return 0;
227 }
228
229 static struct irq_chip adp5588_irq_chip = {
230         .name                   = "adp5588",
231         .irq_mask               = adp5588_irq_mask,
232         .irq_unmask             = adp5588_irq_unmask,
233         .irq_bus_lock           = adp5588_irq_bus_lock,
234         .irq_bus_sync_unlock    = adp5588_irq_bus_sync_unlock,
235         .irq_set_type           = adp5588_irq_set_type,
236 };
237
238 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
239 {
240         struct adp5588_gpio *dev = devid;
241         int status = adp5588_gpio_read(dev->client, INT_STAT);
242
243         if (status & ADP5588_KE_INT) {
244                 int ev_cnt = adp5588_gpio_read(dev->client, KEY_LCK_EC_STAT);
245
246                 if (ev_cnt > 0) {
247                         int i;
248
249                         for (i = 0; i < (ev_cnt & ADP5588_KEC); i++) {
250                                 int key = adp5588_gpio_read(dev->client,
251                                                             Key_EVENTA + i);
252                                 /* GPIN events begin at 97,
253                                  * bit 7 indicates logic level
254                                  */
255                                 int gpio = (key & 0x7f) - 97;
256                                 int lvl = key & (1 << 7);
257                                 int bank = ADP5588_BANK(gpio);
258                                 int bit = ADP5588_BIT(gpio);
259
260                                 if ((lvl && dev->int_lvl_high[bank] & bit) ||
261                                     (!lvl && dev->int_lvl_low[bank] & bit))
262                                         handle_nested_irq(irq_find_mapping(
263                                               dev->gpio_chip.irq.domain, gpio));
264                         }
265                 }
266         }
267
268         adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
269
270         return IRQ_HANDLED;
271 }
272
273
274 static int adp5588_irq_init_hw(struct gpio_chip *gc)
275 {
276         struct adp5588_gpio *dev = gpiochip_get_data(gc);
277         /* Enable IRQs after registering chip */
278         adp5588_gpio_write(dev->client, CFG,
279                            ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_KE_IEN);
280
281         return 0;
282 }
283
284 static int adp5588_irq_setup(struct adp5588_gpio *dev)
285 {
286         struct i2c_client *client = dev->client;
287         int ret;
288         struct adp5588_gpio_platform_data *pdata =
289                         dev_get_platdata(&client->dev);
290         struct gpio_irq_chip *girq;
291
292         adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
293         adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
294
295         mutex_init(&dev->irq_lock);
296
297         ret = devm_request_threaded_irq(&client->dev, client->irq,
298                                         NULL, adp5588_irq_handler, IRQF_ONESHOT
299                                         | IRQF_TRIGGER_FALLING | IRQF_SHARED,
300                                         dev_name(&client->dev), dev);
301         if (ret) {
302                 dev_err(&client->dev, "failed to request irq %d\n",
303                         client->irq);
304                 return ret;
305         }
306
307         /* This will be registered in the call to devm_gpiochip_add_data() */
308         girq = &dev->gpio_chip.irq;
309         girq->chip = &adp5588_irq_chip;
310         /* This will let us handle the parent IRQ in the driver */
311         girq->parent_handler = NULL;
312         girq->num_parents = 0;
313         girq->parents = NULL;
314         girq->first = pdata ? pdata->irq_base : 0;
315         girq->default_type = IRQ_TYPE_NONE;
316         girq->handler = handle_simple_irq;
317         girq->init_hw = adp5588_irq_init_hw;
318         girq->threaded = true;
319
320         return 0;
321 }
322
323 #else
324 static int adp5588_irq_setup(struct adp5588_gpio *dev)
325 {
326         struct i2c_client *client = dev->client;
327         dev_warn(&client->dev, "interrupt support not compiled in\n");
328
329         return 0;
330 }
331
332 #endif /* CONFIG_GPIO_ADP5588_IRQ */
333
334 static int adp5588_gpio_probe(struct i2c_client *client)
335 {
336         struct adp5588_gpio_platform_data *pdata =
337                         dev_get_platdata(&client->dev);
338         struct adp5588_gpio *dev;
339         struct gpio_chip *gc;
340         int ret, i, revid;
341         unsigned int pullup_dis_mask = 0;
342
343         if (!i2c_check_functionality(client->adapter,
344                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
345                 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
346                 return -EIO;
347         }
348
349         dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
350         if (!dev)
351                 return -ENOMEM;
352
353         dev->client = client;
354
355         gc = &dev->gpio_chip;
356         gc->direction_input = adp5588_gpio_direction_input;
357         gc->direction_output = adp5588_gpio_direction_output;
358         gc->get = adp5588_gpio_get_value;
359         gc->set = adp5588_gpio_set_value;
360         gc->can_sleep = true;
361         gc->base = -1;
362         gc->parent = &client->dev;
363
364         if (pdata) {
365                 gc->base = pdata->gpio_start;
366                 gc->names = pdata->names;
367                 pullup_dis_mask = pdata->pullup_dis_mask;
368         }
369
370         gc->ngpio = ADP5588_MAXGPIO;
371         gc->label = client->name;
372         gc->owner = THIS_MODULE;
373
374         mutex_init(&dev->lock);
375
376         ret = adp5588_gpio_read(dev->client, DEV_ID);
377         if (ret < 0)
378                 return ret;
379
380         revid = ret & ADP5588_DEVICE_ID_MASK;
381
382         for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
383                 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
384                 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
385                 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
386                 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
387                                 (pullup_dis_mask >> (8 * i)) & 0xFF);
388                 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
389                 if (ret)
390                         return ret;
391         }
392
393         if (client->irq) {
394                 if (WA_DELAYED_READOUT_REVID(revid)) {
395                         dev_warn(&client->dev, "GPIO int not supported\n");
396                 } else {
397                         ret = adp5588_irq_setup(dev);
398                         if (ret)
399                                 return ret;
400                 }
401         }
402
403         ret = devm_gpiochip_add_data(&client->dev, &dev->gpio_chip, dev);
404         if (ret)
405                 return ret;
406
407         i2c_set_clientdata(client, dev);
408
409         return 0;
410 }
411
412 static void adp5588_gpio_remove(struct i2c_client *client)
413 {
414         struct adp5588_gpio *dev = i2c_get_clientdata(client);
415
416         if (dev->client->irq)
417                 free_irq(dev->client->irq, dev);
418 }
419
420 static const struct i2c_device_id adp5588_gpio_id[] = {
421         { "adp5588-gpio" },
422         {}
423 };
424 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
425
426 static const struct of_device_id adp5588_gpio_of_id[] = {
427         { .compatible = "adi,adp5588-gpio" },
428         {}
429 };
430 MODULE_DEVICE_TABLE(of, adp5588_gpio_of_id);
431
432 static struct i2c_driver adp5588_gpio_driver = {
433         .driver = {
434                 .name = "adp5588-gpio",
435                 .of_match_table = adp5588_gpio_of_id,
436         },
437         .probe_new = adp5588_gpio_probe,
438         .remove = adp5588_gpio_remove,
439         .id_table = adp5588_gpio_id,
440 };
441
442 module_i2c_driver(adp5588_gpio_driver);
443
444 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
445 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
446 MODULE_LICENSE("GPL");