OSDN Git Service

PCI: keystone: Fix link training retries initiation
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / gpio / gpio-adp5588.c
1 /*
2  * GPIO Chip driver for Analog Devices
3  * ADP5588/ADP5587 I/O Expander and QWERTY Keypad Controller
4  *
5  * Copyright 2009-2010 Analog Devices Inc.
6  *
7  * Licensed under the GPL-2 or later.
8  */
9
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/gpio.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18
19 #include <linux/i2c/adp5588.h>
20
21 #define DRV_NAME        "adp5588-gpio"
22
23 /*
24  * Early pre 4.0 Silicon required to delay readout by at least 25ms,
25  * since the Event Counter Register updated 25ms after the interrupt
26  * asserted.
27  */
28 #define WA_DELAYED_READOUT_REVID(rev)   ((rev) < 4)
29
30 struct adp5588_gpio {
31         struct i2c_client *client;
32         struct gpio_chip gpio_chip;
33         struct mutex lock;      /* protect cached dir, dat_out */
34         /* protect serialized access to the interrupt controller bus */
35         struct mutex irq_lock;
36         unsigned gpio_start;
37         unsigned irq_base;
38         uint8_t dat_out[3];
39         uint8_t dir[3];
40         uint8_t int_lvl[3];
41         uint8_t int_en[3];
42         uint8_t irq_mask[3];
43         uint8_t irq_stat[3];
44         uint8_t int_input_en[3];
45         uint8_t int_lvl_cached[3];
46 };
47
48 static int adp5588_gpio_read(struct i2c_client *client, u8 reg)
49 {
50         int ret = i2c_smbus_read_byte_data(client, reg);
51
52         if (ret < 0)
53                 dev_err(&client->dev, "Read Error\n");
54
55         return ret;
56 }
57
58 static int adp5588_gpio_write(struct i2c_client *client, u8 reg, u8 val)
59 {
60         int ret = i2c_smbus_write_byte_data(client, reg, val);
61
62         if (ret < 0)
63                 dev_err(&client->dev, "Write Error\n");
64
65         return ret;
66 }
67
68 static int adp5588_gpio_get_value(struct gpio_chip *chip, unsigned off)
69 {
70         struct adp5588_gpio *dev =
71             container_of(chip, struct adp5588_gpio, gpio_chip);
72         unsigned bank = ADP5588_BANK(off);
73         unsigned bit = ADP5588_BIT(off);
74         int val;
75
76         mutex_lock(&dev->lock);
77
78         if (dev->dir[bank] & bit)
79                 val = dev->dat_out[bank];
80         else
81                 val = adp5588_gpio_read(dev->client, GPIO_DAT_STAT1 + bank);
82
83         mutex_unlock(&dev->lock);
84
85         return !!(val & bit);
86 }
87
88 static void adp5588_gpio_set_value(struct gpio_chip *chip,
89                                    unsigned off, int val)
90 {
91         unsigned bank, bit;
92         struct adp5588_gpio *dev =
93             container_of(chip, struct adp5588_gpio, gpio_chip);
94
95         bank = ADP5588_BANK(off);
96         bit = ADP5588_BIT(off);
97
98         mutex_lock(&dev->lock);
99         if (val)
100                 dev->dat_out[bank] |= bit;
101         else
102                 dev->dat_out[bank] &= ~bit;
103
104         adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
105                            dev->dat_out[bank]);
106         mutex_unlock(&dev->lock);
107 }
108
109 static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
110 {
111         int ret;
112         unsigned bank;
113         struct adp5588_gpio *dev =
114             container_of(chip, struct adp5588_gpio, gpio_chip);
115
116         bank = ADP5588_BANK(off);
117
118         mutex_lock(&dev->lock);
119         dev->dir[bank] &= ~ADP5588_BIT(off);
120         ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
121         mutex_unlock(&dev->lock);
122
123         return ret;
124 }
125
126 static int adp5588_gpio_direction_output(struct gpio_chip *chip,
127                                          unsigned off, int val)
128 {
129         int ret;
130         unsigned bank, bit;
131         struct adp5588_gpio *dev =
132             container_of(chip, struct adp5588_gpio, gpio_chip);
133
134         bank = ADP5588_BANK(off);
135         bit = ADP5588_BIT(off);
136
137         mutex_lock(&dev->lock);
138         dev->dir[bank] |= bit;
139
140         if (val)
141                 dev->dat_out[bank] |= bit;
142         else
143                 dev->dat_out[bank] &= ~bit;
144
145         ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
146                                  dev->dat_out[bank]);
147         ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
148                                  dev->dir[bank]);
149         mutex_unlock(&dev->lock);
150
151         return ret;
152 }
153
154 #ifdef CONFIG_GPIO_ADP5588_IRQ
155 static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
156 {
157         struct adp5588_gpio *dev =
158                 container_of(chip, struct adp5588_gpio, gpio_chip);
159         return dev->irq_base + off;
160 }
161
162 static void adp5588_irq_bus_lock(struct irq_data *d)
163 {
164         struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
165
166         mutex_lock(&dev->irq_lock);
167 }
168
169  /*
170   * genirq core code can issue chip->mask/unmask from atomic context.
171   * This doesn't work for slow busses where an access needs to sleep.
172   * bus_sync_unlock() is therefore called outside the atomic context,
173   * syncs the current irq mask state with the slow external controller
174   * and unlocks the bus.
175   */
176
177 static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
178 {
179         struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
180         int i;
181
182         for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
183                 if (dev->int_input_en[i]) {
184                         mutex_lock(&dev->lock);
185                         dev->dir[i] &= ~dev->int_input_en[i];
186                         dev->int_input_en[i] = 0;
187                         adp5588_gpio_write(dev->client, GPIO_DIR1 + i,
188                                            dev->dir[i]);
189                         mutex_unlock(&dev->lock);
190                 }
191
192                 if (dev->int_lvl_cached[i] != dev->int_lvl[i]) {
193                         dev->int_lvl_cached[i] = dev->int_lvl[i];
194                         adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + i,
195                                            dev->int_lvl[i]);
196                 }
197
198                 if (dev->int_en[i] ^ dev->irq_mask[i]) {
199                         dev->int_en[i] = dev->irq_mask[i];
200                         adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
201                                            dev->int_en[i]);
202                 }
203         }
204
205         mutex_unlock(&dev->irq_lock);
206 }
207
208 static void adp5588_irq_mask(struct irq_data *d)
209 {
210         struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
211         unsigned gpio = d->irq - dev->irq_base;
212
213         dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
214 }
215
216 static void adp5588_irq_unmask(struct irq_data *d)
217 {
218         struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
219         unsigned gpio = d->irq - dev->irq_base;
220
221         dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
222 }
223
224 static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
225 {
226         struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
227         uint16_t gpio = d->irq - dev->irq_base;
228         unsigned bank, bit;
229
230         if ((type & IRQ_TYPE_EDGE_BOTH)) {
231                 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
232                         d->irq, type);
233                 return -EINVAL;
234         }
235
236         bank = ADP5588_BANK(gpio);
237         bit = ADP5588_BIT(gpio);
238
239         if (type & IRQ_TYPE_LEVEL_HIGH)
240                 dev->int_lvl[bank] |= bit;
241         else if (type & IRQ_TYPE_LEVEL_LOW)
242                 dev->int_lvl[bank] &= ~bit;
243         else
244                 return -EINVAL;
245
246         dev->int_input_en[bank] |= bit;
247
248         return 0;
249 }
250
251 static struct irq_chip adp5588_irq_chip = {
252         .name                   = "adp5588",
253         .irq_mask               = adp5588_irq_mask,
254         .irq_unmask             = adp5588_irq_unmask,
255         .irq_bus_lock           = adp5588_irq_bus_lock,
256         .irq_bus_sync_unlock    = adp5588_irq_bus_sync_unlock,
257         .irq_set_type           = adp5588_irq_set_type,
258 };
259
260 static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
261 {
262         int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
263
264         if (ret < 0)
265                 dev_err(&client->dev, "Read INT_STAT Error\n");
266
267         return ret;
268 }
269
270 static irqreturn_t adp5588_irq_handler(int irq, void *devid)
271 {
272         struct adp5588_gpio *dev = devid;
273         unsigned status, bank, bit, pending;
274         int ret;
275         status = adp5588_gpio_read(dev->client, INT_STAT);
276
277         if (status & ADP5588_GPI_INT) {
278                 ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
279                 if (ret < 0)
280                         memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
281
282                 for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
283                         bank++, bit = 0) {
284                         pending = dev->irq_stat[bank] & dev->irq_mask[bank];
285
286                         while (pending) {
287                                 if (pending & (1 << bit)) {
288                                         handle_nested_irq(dev->irq_base +
289                                                           (bank << 3) + bit);
290                                         pending &= ~(1 << bit);
291
292                                 }
293                                 bit++;
294                         }
295                 }
296         }
297
298         adp5588_gpio_write(dev->client, INT_STAT, status); /* Status is W1C */
299
300         return IRQ_HANDLED;
301 }
302
303 static int adp5588_irq_setup(struct adp5588_gpio *dev)
304 {
305         struct i2c_client *client = dev->client;
306         struct adp5588_gpio_platform_data *pdata =
307                         dev_get_platdata(&client->dev);
308         unsigned gpio;
309         int ret;
310
311         adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
312         adp5588_gpio_write(client, INT_STAT, -1); /* status is W1C */
313         adp5588_gpio_read_intstat(client, dev->irq_stat); /* read to clear */
314
315         dev->irq_base = pdata->irq_base;
316         mutex_init(&dev->irq_lock);
317
318         for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
319                 int irq = gpio + dev->irq_base;
320                 irq_set_chip_data(irq, dev);
321                 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
322                                          handle_level_irq);
323                 irq_set_nested_thread(irq, 1);
324                 irq_modify_status(irq, IRQ_NOREQUEST, IRQ_NOPROBE);
325         }
326
327         ret = request_threaded_irq(client->irq,
328                                    NULL,
329                                    adp5588_irq_handler,
330                                    IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
331                                    dev_name(&client->dev), dev);
332         if (ret) {
333                 dev_err(&client->dev, "failed to request irq %d\n",
334                         client->irq);
335                 goto out;
336         }
337
338         dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
339         adp5588_gpio_write(client, CFG,
340                 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
341
342         return 0;
343
344 out:
345         dev->irq_base = 0;
346         return ret;
347 }
348
349 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
350 {
351         if (dev->irq_base)
352                 free_irq(dev->client->irq, dev);
353 }
354
355 #else
356 static int adp5588_irq_setup(struct adp5588_gpio *dev)
357 {
358         struct i2c_client *client = dev->client;
359         dev_warn(&client->dev, "interrupt support not compiled in\n");
360
361         return 0;
362 }
363
364 static void adp5588_irq_teardown(struct adp5588_gpio *dev)
365 {
366 }
367 #endif /* CONFIG_GPIO_ADP5588_IRQ */
368
369 static int adp5588_gpio_probe(struct i2c_client *client,
370                                         const struct i2c_device_id *id)
371 {
372         struct adp5588_gpio_platform_data *pdata =
373                         dev_get_platdata(&client->dev);
374         struct adp5588_gpio *dev;
375         struct gpio_chip *gc;
376         int ret, i, revid;
377
378         if (!pdata) {
379                 dev_err(&client->dev, "missing platform data\n");
380                 return -ENODEV;
381         }
382
383         if (!i2c_check_functionality(client->adapter,
384                                         I2C_FUNC_SMBUS_BYTE_DATA)) {
385                 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
386                 return -EIO;
387         }
388
389         dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
390         if (!dev)
391                 return -ENOMEM;
392
393         dev->client = client;
394
395         gc = &dev->gpio_chip;
396         gc->direction_input = adp5588_gpio_direction_input;
397         gc->direction_output = adp5588_gpio_direction_output;
398         gc->get = adp5588_gpio_get_value;
399         gc->set = adp5588_gpio_set_value;
400         gc->can_sleep = true;
401
402         gc->base = pdata->gpio_start;
403         gc->ngpio = ADP5588_MAXGPIO;
404         gc->label = client->name;
405         gc->owner = THIS_MODULE;
406         gc->names = pdata->names;
407
408         mutex_init(&dev->lock);
409
410         ret = adp5588_gpio_read(dev->client, DEV_ID);
411         if (ret < 0)
412                 goto err;
413
414         revid = ret & ADP5588_DEVICE_ID_MASK;
415
416         for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
417                 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
418                 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
419                 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
420                 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
421                                 (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
422                 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
423                 if (ret)
424                         goto err;
425         }
426
427         if (pdata->irq_base) {
428                 if (WA_DELAYED_READOUT_REVID(revid)) {
429                         dev_warn(&client->dev, "GPIO int not supported\n");
430                 } else {
431                         ret = adp5588_irq_setup(dev);
432                         if (ret)
433                                 goto err;
434                 }
435         }
436
437         ret = gpiochip_add(&dev->gpio_chip);
438         if (ret)
439                 goto err_irq;
440
441         dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
442                         pdata->irq_base, revid);
443
444         if (pdata->setup) {
445                 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
446                 if (ret < 0)
447                         dev_warn(&client->dev, "setup failed, %d\n", ret);
448         }
449
450         i2c_set_clientdata(client, dev);
451
452         return 0;
453
454 err_irq:
455         adp5588_irq_teardown(dev);
456 err:
457         return ret;
458 }
459
460 static int adp5588_gpio_remove(struct i2c_client *client)
461 {
462         struct adp5588_gpio_platform_data *pdata =
463                         dev_get_platdata(&client->dev);
464         struct adp5588_gpio *dev = i2c_get_clientdata(client);
465         int ret;
466
467         if (pdata->teardown) {
468                 ret = pdata->teardown(client,
469                                       dev->gpio_chip.base, dev->gpio_chip.ngpio,
470                                       pdata->context);
471                 if (ret < 0) {
472                         dev_err(&client->dev, "teardown failed %d\n", ret);
473                         return ret;
474                 }
475         }
476
477         if (dev->irq_base)
478                 free_irq(dev->client->irq, dev);
479
480         gpiochip_remove(&dev->gpio_chip);
481
482         return 0;
483 }
484
485 static const struct i2c_device_id adp5588_gpio_id[] = {
486         {DRV_NAME, 0},
487         {}
488 };
489
490 MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
491
492 static struct i2c_driver adp5588_gpio_driver = {
493         .driver = {
494                    .name = DRV_NAME,
495                    },
496         .probe = adp5588_gpio_probe,
497         .remove = adp5588_gpio_remove,
498         .id_table = adp5588_gpio_id,
499 };
500
501 module_i2c_driver(adp5588_gpio_driver);
502
503 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
504 MODULE_DESCRIPTION("GPIO ADP5588 Driver");
505 MODULE_LICENSE("GPL");