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