1
2
3
4
5
6
7
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
25
26
27
28#define WA_DELAYED_READOUT_REVID(rev) ((rev) < 4)
29
30struct adp5588_gpio {
31 struct i2c_client *client;
32 struct gpio_chip gpio_chip;
33 struct mutex lock;
34
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};
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 =
69 container_of(chip, struct adp5588_gpio, gpio_chip);
70
71 return !!(adp5588_gpio_read(dev->client,
72 GPIO_DAT_STAT1 + ADP5588_BANK(off)) & ADP5588_BIT(off));
73}
74
75static void adp5588_gpio_set_value(struct gpio_chip *chip,
76 unsigned off, int val)
77{
78 unsigned bank, bit;
79 struct adp5588_gpio *dev =
80 container_of(chip, struct adp5588_gpio, gpio_chip);
81
82 bank = ADP5588_BANK(off);
83 bit = ADP5588_BIT(off);
84
85 mutex_lock(&dev->lock);
86 if (val)
87 dev->dat_out[bank] |= bit;
88 else
89 dev->dat_out[bank] &= ~bit;
90
91 adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
92 dev->dat_out[bank]);
93 mutex_unlock(&dev->lock);
94}
95
96static int adp5588_gpio_direction_input(struct gpio_chip *chip, unsigned off)
97{
98 int ret;
99 unsigned bank;
100 struct adp5588_gpio *dev =
101 container_of(chip, struct adp5588_gpio, gpio_chip);
102
103 bank = ADP5588_BANK(off);
104
105 mutex_lock(&dev->lock);
106 dev->dir[bank] &= ~ADP5588_BIT(off);
107 ret = adp5588_gpio_write(dev->client, GPIO_DIR1 + bank, dev->dir[bank]);
108 mutex_unlock(&dev->lock);
109
110 return ret;
111}
112
113static int adp5588_gpio_direction_output(struct gpio_chip *chip,
114 unsigned off, int val)
115{
116 int ret;
117 unsigned bank, bit;
118 struct adp5588_gpio *dev =
119 container_of(chip, struct adp5588_gpio, gpio_chip);
120
121 bank = ADP5588_BANK(off);
122 bit = ADP5588_BIT(off);
123
124 mutex_lock(&dev->lock);
125 dev->dir[bank] |= bit;
126
127 if (val)
128 dev->dat_out[bank] |= bit;
129 else
130 dev->dat_out[bank] &= ~bit;
131
132 ret = adp5588_gpio_write(dev->client, GPIO_DAT_OUT1 + bank,
133 dev->dat_out[bank]);
134 ret |= adp5588_gpio_write(dev->client, GPIO_DIR1 + bank,
135 dev->dir[bank]);
136 mutex_unlock(&dev->lock);
137
138 return ret;
139}
140
141#ifdef CONFIG_GPIO_ADP5588_IRQ
142static int adp5588_gpio_to_irq(struct gpio_chip *chip, unsigned off)
143{
144 struct adp5588_gpio *dev =
145 container_of(chip, struct adp5588_gpio, gpio_chip);
146 return dev->irq_base + off;
147}
148
149static void adp5588_irq_bus_lock(struct irq_data *d)
150{
151 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
152
153 mutex_lock(&dev->irq_lock);
154}
155
156
157
158
159
160
161
162
163
164static void adp5588_irq_bus_sync_unlock(struct irq_data *d)
165{
166 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
167 int i;
168
169 for (i = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++)
170 if (dev->int_en[i] ^ dev->irq_mask[i]) {
171 dev->int_en[i] = dev->irq_mask[i];
172 adp5588_gpio_write(dev->client, GPIO_INT_EN1 + i,
173 dev->int_en[i]);
174 }
175
176 mutex_unlock(&dev->irq_lock);
177}
178
179static void adp5588_irq_mask(struct irq_data *d)
180{
181 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
182 unsigned gpio = d->irq - dev->irq_base;
183
184 dev->irq_mask[ADP5588_BANK(gpio)] &= ~ADP5588_BIT(gpio);
185}
186
187static void adp5588_irq_unmask(struct irq_data *d)
188{
189 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
190 unsigned gpio = d->irq - dev->irq_base;
191
192 dev->irq_mask[ADP5588_BANK(gpio)] |= ADP5588_BIT(gpio);
193}
194
195static int adp5588_irq_set_type(struct irq_data *d, unsigned int type)
196{
197 struct adp5588_gpio *dev = irq_data_get_irq_chip_data(d);
198 uint16_t gpio = d->irq - dev->irq_base;
199 unsigned bank, bit;
200
201 if ((type & IRQ_TYPE_EDGE_BOTH)) {
202 dev_err(&dev->client->dev, "irq %d: unsupported type %d\n",
203 d->irq, type);
204 return -EINVAL;
205 }
206
207 bank = ADP5588_BANK(gpio);
208 bit = ADP5588_BIT(gpio);
209
210 if (type & IRQ_TYPE_LEVEL_HIGH)
211 dev->int_lvl[bank] |= bit;
212 else if (type & IRQ_TYPE_LEVEL_LOW)
213 dev->int_lvl[bank] &= ~bit;
214 else
215 return -EINVAL;
216
217 adp5588_gpio_direction_input(&dev->gpio_chip, gpio);
218 adp5588_gpio_write(dev->client, GPIO_INT_LVL1 + bank,
219 dev->int_lvl[bank]);
220
221 return 0;
222}
223
224static struct irq_chip adp5588_irq_chip = {
225 .name = "adp5588",
226 .irq_mask = adp5588_irq_mask,
227 .irq_unmask = adp5588_irq_unmask,
228 .irq_bus_lock = adp5588_irq_bus_lock,
229 .irq_bus_sync_unlock = adp5588_irq_bus_sync_unlock,
230 .irq_set_type = adp5588_irq_set_type,
231};
232
233static int adp5588_gpio_read_intstat(struct i2c_client *client, u8 *buf)
234{
235 int ret = i2c_smbus_read_i2c_block_data(client, GPIO_INT_STAT1, 3, buf);
236
237 if (ret < 0)
238 dev_err(&client->dev, "Read INT_STAT Error\n");
239
240 return ret;
241}
242
243static irqreturn_t adp5588_irq_handler(int irq, void *devid)
244{
245 struct adp5588_gpio *dev = devid;
246 unsigned status, bank, bit, pending;
247 int ret;
248 status = adp5588_gpio_read(dev->client, INT_STAT);
249
250 if (status & ADP5588_GPI_INT) {
251 ret = adp5588_gpio_read_intstat(dev->client, dev->irq_stat);
252 if (ret < 0)
253 memset(dev->irq_stat, 0, ARRAY_SIZE(dev->irq_stat));
254
255 for (bank = 0, bit = 0; bank <= ADP5588_BANK(ADP5588_MAXGPIO);
256 bank++, bit = 0) {
257 pending = dev->irq_stat[bank] & dev->irq_mask[bank];
258
259 while (pending) {
260 if (pending & (1 << bit)) {
261 handle_nested_irq(dev->irq_base +
262 (bank << 3) + bit);
263 pending &= ~(1 << bit);
264
265 }
266 bit++;
267 }
268 }
269 }
270
271 adp5588_gpio_write(dev->client, INT_STAT, status);
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 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
280 unsigned gpio;
281 int ret;
282
283 adp5588_gpio_write(client, CFG, ADP5588_AUTO_INC);
284 adp5588_gpio_write(client, INT_STAT, -1);
285 adp5588_gpio_read_intstat(client, dev->irq_stat);
286
287 dev->irq_base = pdata->irq_base;
288 mutex_init(&dev->irq_lock);
289
290 for (gpio = 0; gpio < dev->gpio_chip.ngpio; gpio++) {
291 int irq = gpio + dev->irq_base;
292 irq_set_chip_data(irq, dev);
293 irq_set_chip_and_handler(irq, &adp5588_irq_chip,
294 handle_level_irq);
295 irq_set_nested_thread(irq, 1);
296#ifdef CONFIG_ARM
297
298
299
300
301 set_irq_flags(irq, IRQF_VALID);
302#else
303 irq_set_noprobe(irq);
304#endif
305 }
306
307 ret = request_threaded_irq(client->irq,
308 NULL,
309 adp5588_irq_handler,
310 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
311 dev_name(&client->dev), dev);
312 if (ret) {
313 dev_err(&client->dev, "failed to request irq %d\n",
314 client->irq);
315 goto out;
316 }
317
318 dev->gpio_chip.to_irq = adp5588_gpio_to_irq;
319 adp5588_gpio_write(client, CFG,
320 ADP5588_AUTO_INC | ADP5588_INT_CFG | ADP5588_GPI_INT);
321
322 return 0;
323
324out:
325 dev->irq_base = 0;
326 return ret;
327}
328
329static void adp5588_irq_teardown(struct adp5588_gpio *dev)
330{
331 if (dev->irq_base)
332 free_irq(dev->client->irq, dev);
333}
334
335#else
336static int adp5588_irq_setup(struct adp5588_gpio *dev)
337{
338 struct i2c_client *client = dev->client;
339 dev_warn(&client->dev, "interrupt support not compiled in\n");
340
341 return 0;
342}
343
344static void adp5588_irq_teardown(struct adp5588_gpio *dev)
345{
346}
347#endif
348
349static int adp5588_gpio_probe(struct i2c_client *client,
350 const struct i2c_device_id *id)
351{
352 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
353 struct adp5588_gpio *dev;
354 struct gpio_chip *gc;
355 int ret, i, revid;
356
357 if (pdata == NULL) {
358 dev_err(&client->dev, "missing platform data\n");
359 return -ENODEV;
360 }
361
362 if (!i2c_check_functionality(client->adapter,
363 I2C_FUNC_SMBUS_BYTE_DATA)) {
364 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
365 return -EIO;
366 }
367
368 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
369 if (dev == NULL) {
370 dev_err(&client->dev, "failed to alloc memory\n");
371 return -ENOMEM;
372 }
373
374 dev->client = client;
375
376 gc = &dev->gpio_chip;
377 gc->direction_input = adp5588_gpio_direction_input;
378 gc->direction_output = adp5588_gpio_direction_output;
379 gc->get = adp5588_gpio_get_value;
380 gc->set = adp5588_gpio_set_value;
381 gc->can_sleep = 1;
382
383 gc->base = pdata->gpio_start;
384 gc->ngpio = ADP5588_MAXGPIO;
385 gc->label = client->name;
386 gc->owner = THIS_MODULE;
387
388 mutex_init(&dev->lock);
389
390 ret = adp5588_gpio_read(dev->client, DEV_ID);
391 if (ret < 0)
392 goto err;
393
394 revid = ret & ADP5588_DEVICE_ID_MASK;
395
396 for (i = 0, ret = 0; i <= ADP5588_BANK(ADP5588_MAXGPIO); i++) {
397 dev->dat_out[i] = adp5588_gpio_read(client, GPIO_DAT_OUT1 + i);
398 dev->dir[i] = adp5588_gpio_read(client, GPIO_DIR1 + i);
399 ret |= adp5588_gpio_write(client, KP_GPIO1 + i, 0);
400 ret |= adp5588_gpio_write(client, GPIO_PULL1 + i,
401 (pdata->pullup_dis_mask >> (8 * i)) & 0xFF);
402 ret |= adp5588_gpio_write(client, GPIO_INT_EN1 + i, 0);
403 if (ret)
404 goto err;
405 }
406
407 if (pdata->irq_base) {
408 if (WA_DELAYED_READOUT_REVID(revid)) {
409 dev_warn(&client->dev, "GPIO int not supported\n");
410 } else {
411 ret = adp5588_irq_setup(dev);
412 if (ret)
413 goto err;
414 }
415 }
416
417 ret = gpiochip_add(&dev->gpio_chip);
418 if (ret)
419 goto err_irq;
420
421 dev_info(&client->dev, "IRQ Base: %d Rev.: %d\n",
422 pdata->irq_base, revid);
423
424 if (pdata->setup) {
425 ret = pdata->setup(client, gc->base, gc->ngpio, pdata->context);
426 if (ret < 0)
427 dev_warn(&client->dev, "setup failed, %d\n", ret);
428 }
429
430 i2c_set_clientdata(client, dev);
431
432 return 0;
433
434err_irq:
435 adp5588_irq_teardown(dev);
436err:
437 kfree(dev);
438 return ret;
439}
440
441static int adp5588_gpio_remove(struct i2c_client *client)
442{
443 struct adp5588_gpio_platform_data *pdata = client->dev.platform_data;
444 struct adp5588_gpio *dev = i2c_get_clientdata(client);
445 int ret;
446
447 if (pdata->teardown) {
448 ret = pdata->teardown(client,
449 dev->gpio_chip.base, dev->gpio_chip.ngpio,
450 pdata->context);
451 if (ret < 0) {
452 dev_err(&client->dev, "teardown failed %d\n", ret);
453 return ret;
454 }
455 }
456
457 if (dev->irq_base)
458 free_irq(dev->client->irq, dev);
459
460 ret = gpiochip_remove(&dev->gpio_chip);
461 if (ret) {
462 dev_err(&client->dev, "gpiochip_remove failed %d\n", ret);
463 return ret;
464 }
465
466 kfree(dev);
467 return 0;
468}
469
470static const struct i2c_device_id adp5588_gpio_id[] = {
471 {DRV_NAME, 0},
472 {}
473};
474
475MODULE_DEVICE_TABLE(i2c, adp5588_gpio_id);
476
477static struct i2c_driver adp5588_gpio_driver = {
478 .driver = {
479 .name = DRV_NAME,
480 },
481 .probe = adp5588_gpio_probe,
482 .remove = adp5588_gpio_remove,
483 .id_table = adp5588_gpio_id,
484};
485
486module_i2c_driver(adp5588_gpio_driver);
487
488MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
489MODULE_DESCRIPTION("GPIO ADP5588 Driver");
490MODULE_LICENSE("GPL");
491