mfd: Add basic tps6586x interrupt support
[linux-2.6.git] / drivers / mfd / tps6586x.c
1 /*
2  * Core driver for TI TPS6586x PMIC family
3  *
4  * Copyright (c) 2010 CompuLab Ltd.
5  * Mike Rapoport <mike@compulab.co.il>
6  *
7  * Based on da903x.c.
8  * Copyright (C) 2008 Compulab, Ltd.
9  * Mike Rapoport <mike@compulab.co.il>
10  * Copyright (C) 2006-2008 Marvell International Ltd.
11  * Eric Miao <eric.miao@marvell.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/i2c.h>
26
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/tps6586x.h>
29
30 /* GPIO control registers */
31 #define TPS6586X_GPIOSET1       0x5d
32 #define TPS6586X_GPIOSET2       0x5e
33
34 /* interrupt control registers */
35 #define TPS6586X_INT_ACK1       0xb5
36 #define TPS6586X_INT_ACK2       0xb6
37 #define TPS6586X_INT_ACK3       0xb7
38 #define TPS6586X_INT_ACK4       0xb8
39
40 /* interrupt mask registers */
41 #define TPS6586X_INT_MASK1      0xb0
42 #define TPS6586X_INT_MASK2      0xb1
43 #define TPS6586X_INT_MASK3      0xb2
44 #define TPS6586X_INT_MASK4      0xb3
45 #define TPS6586X_INT_MASK5      0xb4
46
47 /* device id */
48 #define TPS6586X_VERSIONCRC     0xcd
49 #define TPS658621A_VERSIONCRC   0x15
50
51 struct tps6586x_irq_data {
52         u8      mask_reg;
53         u8      mask_mask;
54 };
55
56 #define TPS6586X_IRQ(_reg, _mask)                               \
57         {                                                       \
58                 .mask_reg = (_reg) - TPS6586X_INT_MASK1,        \
59                 .mask_mask = (_mask),                           \
60         }
61
62 static const struct tps6586x_irq_data tps6586x_irqs[] = {
63         [TPS6586X_INT_PLDO_0]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
64         [TPS6586X_INT_PLDO_1]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
65         [TPS6586X_INT_PLDO_2]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
66         [TPS6586X_INT_PLDO_3]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
67         [TPS6586X_INT_PLDO_4]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
68         [TPS6586X_INT_PLDO_5]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
69         [TPS6586X_INT_PLDO_6]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
70         [TPS6586X_INT_PLDO_7]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
71         [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
72         [TPS6586X_INT_ADC]      = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
73         [TPS6586X_INT_PLDO_8]   = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
74         [TPS6586X_INT_PLDO_9]   = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
75         [TPS6586X_INT_PSM_0]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
76         [TPS6586X_INT_PSM_1]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
77         [TPS6586X_INT_PSM_2]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
78         [TPS6586X_INT_PSM_3]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
79         [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
80         [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
81         [TPS6586X_INT_USB_DET]  = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
82         [TPS6586X_INT_AC_DET]   = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
83         [TPS6586X_INT_BAT_DET]  = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
84         [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
85         [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
86         [TPS6586X_INT_PP]       = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
87         [TPS6586X_INT_RESUME]   = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
88         [TPS6586X_INT_LOW_SYS]  = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
89         [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
90 };
91
92 struct tps6586x {
93         struct mutex            lock;
94         struct device           *dev;
95         struct i2c_client       *client;
96
97         struct gpio_chip        gpio;
98         struct irq_chip         irq_chip;
99         struct mutex            irq_lock;
100         int                     irq_base;
101         u32                     irq_en;
102         u8                      mask_cache[5];
103         u8                      mask_reg[5];
104 };
105
106 static inline int __tps6586x_read(struct i2c_client *client,
107                                   int reg, uint8_t *val)
108 {
109         int ret;
110
111         ret = i2c_smbus_read_byte_data(client, reg);
112         if (ret < 0) {
113                 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
114                 return ret;
115         }
116
117         *val = (uint8_t)ret;
118
119         return 0;
120 }
121
122 static inline int __tps6586x_reads(struct i2c_client *client, int reg,
123                                    int len, uint8_t *val)
124 {
125         int ret;
126
127         ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
128         if (ret < 0) {
129                 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
130                 return ret;
131         }
132
133         return 0;
134 }
135
136 static inline int __tps6586x_write(struct i2c_client *client,
137                                  int reg, uint8_t val)
138 {
139         int ret;
140
141         ret = i2c_smbus_write_byte_data(client, reg, val);
142         if (ret < 0) {
143                 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
144                                 val, reg);
145                 return ret;
146         }
147
148         return 0;
149 }
150
151 static inline int __tps6586x_writes(struct i2c_client *client, int reg,
152                                   int len, uint8_t *val)
153 {
154         int ret;
155
156         ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
157         if (ret < 0) {
158                 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
159                 return ret;
160         }
161
162         return 0;
163 }
164
165 int tps6586x_write(struct device *dev, int reg, uint8_t val)
166 {
167         return __tps6586x_write(to_i2c_client(dev), reg, val);
168 }
169 EXPORT_SYMBOL_GPL(tps6586x_write);
170
171 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
172 {
173         return __tps6586x_writes(to_i2c_client(dev), reg, len, val);
174 }
175 EXPORT_SYMBOL_GPL(tps6586x_writes);
176
177 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
178 {
179         return __tps6586x_read(to_i2c_client(dev), reg, val);
180 }
181 EXPORT_SYMBOL_GPL(tps6586x_read);
182
183 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
184 {
185         return __tps6586x_reads(to_i2c_client(dev), reg, len, val);
186 }
187 EXPORT_SYMBOL_GPL(tps6586x_reads);
188
189 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
190 {
191         struct tps6586x *tps6586x = dev_get_drvdata(dev);
192         uint8_t reg_val;
193         int ret = 0;
194
195         mutex_lock(&tps6586x->lock);
196
197         ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
198         if (ret)
199                 goto out;
200
201         if ((reg_val & bit_mask) == 0) {
202                 reg_val |= bit_mask;
203                 ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
204         }
205 out:
206         mutex_unlock(&tps6586x->lock);
207         return ret;
208 }
209 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
210
211 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
212 {
213         struct tps6586x *tps6586x = dev_get_drvdata(dev);
214         uint8_t reg_val;
215         int ret = 0;
216
217         mutex_lock(&tps6586x->lock);
218
219         ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
220         if (ret)
221                 goto out;
222
223         if (reg_val & bit_mask) {
224                 reg_val &= ~bit_mask;
225                 ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
226         }
227 out:
228         mutex_unlock(&tps6586x->lock);
229         return ret;
230 }
231 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
232
233 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
234 {
235         struct tps6586x *tps6586x = dev_get_drvdata(dev);
236         uint8_t reg_val;
237         int ret = 0;
238
239         mutex_lock(&tps6586x->lock);
240
241         ret = __tps6586x_read(tps6586x->client, reg, &reg_val);
242         if (ret)
243                 goto out;
244
245         if ((reg_val & mask) != val) {
246                 reg_val = (reg_val & ~mask) | val;
247                 ret = __tps6586x_write(tps6586x->client, reg, reg_val);
248         }
249 out:
250         mutex_unlock(&tps6586x->lock);
251         return ret;
252 }
253 EXPORT_SYMBOL_GPL(tps6586x_update);
254
255 static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
256 {
257         struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
258         uint8_t val;
259         int ret;
260
261         ret = __tps6586x_read(tps6586x->client, TPS6586X_GPIOSET2, &val);
262         if (ret)
263                 return ret;
264
265         return !!(val & (1 << offset));
266 }
267
268
269 static void tps6586x_gpio_set(struct gpio_chip *chip, unsigned offset,
270                               int value)
271 {
272         struct tps6586x *tps6586x = container_of(chip, struct tps6586x, gpio);
273
274         __tps6586x_write(tps6586x->client, TPS6586X_GPIOSET2,
275                          value << offset);
276 }
277
278 static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
279                                 int value)
280 {
281         struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
282         uint8_t val, mask;
283
284         tps6586x_gpio_set(gc, offset, value);
285
286         val = 0x1 << (offset * 2);
287         mask = 0x3 << (offset * 2);
288
289         return tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET1, val, mask);
290 }
291
292 static void tps6586x_gpio_init(struct tps6586x *tps6586x, int gpio_base)
293 {
294         int ret;
295
296         if (!gpio_base)
297                 return;
298
299         tps6586x->gpio.owner            = THIS_MODULE;
300         tps6586x->gpio.label            = tps6586x->client->name;
301         tps6586x->gpio.dev              = tps6586x->dev;
302         tps6586x->gpio.base             = gpio_base;
303         tps6586x->gpio.ngpio            = 4;
304         tps6586x->gpio.can_sleep        = 1;
305
306         /* FIXME: add handling of GPIOs as dedicated inputs */
307         tps6586x->gpio.direction_output = tps6586x_gpio_output;
308         tps6586x->gpio.set              = tps6586x_gpio_set;
309         tps6586x->gpio.get              = tps6586x_gpio_get;
310
311         ret = gpiochip_add(&tps6586x->gpio);
312         if (ret)
313                 dev_warn(tps6586x->dev, "GPIO registration failed: %d\n", ret);
314 }
315
316 static int __remove_subdev(struct device *dev, void *unused)
317 {
318         platform_device_unregister(to_platform_device(dev));
319         return 0;
320 }
321
322 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
323 {
324         return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
325 }
326
327 static void tps6586x_irq_lock(unsigned int irq)
328 {
329         struct tps6586x *tps6586x = get_irq_chip_data(irq);
330
331         mutex_lock(&tps6586x->irq_lock);
332 }
333
334 static void tps6586x_irq_enable(unsigned int irq)
335 {
336         struct tps6586x *tps6586x = get_irq_chip_data(irq);
337         unsigned int __irq = irq - tps6586x->irq_base;
338         const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
339
340         tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
341         tps6586x->irq_en |= (1 << __irq);
342 }
343
344 static void tps6586x_irq_disable(unsigned int irq)
345 {
346         struct tps6586x *tps6586x = get_irq_chip_data(irq);
347
348         unsigned int __irq = irq - tps6586x->irq_base;
349         const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
350
351         tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
352         tps6586x->irq_en &= ~(1 << __irq);
353 }
354
355 static void tps6586x_irq_sync_unlock(unsigned int irq)
356 {
357         struct tps6586x *tps6586x = get_irq_chip_data(irq);
358         int i;
359
360         for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
361                 if (tps6586x->mask_reg[i] != tps6586x->mask_cache[i]) {
362                         if (!WARN_ON(tps6586x_write(tps6586x->dev,
363                                                     TPS6586X_INT_MASK1 + i,
364                                                     tps6586x->mask_reg[i])))
365                                 tps6586x->mask_cache[i] = tps6586x->mask_reg[i];
366                 }
367         }
368
369         mutex_unlock(&tps6586x->irq_lock);
370 }
371
372 static irqreturn_t tps6586x_irq(int irq, void *data)
373 {
374         struct tps6586x *tps6586x = data;
375         u32 acks;
376         int ret = 0;
377
378         ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
379                              sizeof(acks), (uint8_t *)&acks);
380
381         if (ret < 0) {
382                 dev_err(tps6586x->dev, "failed to read interrupt status\n");
383                 return IRQ_NONE;
384         }
385
386         acks = le32_to_cpu(acks);
387
388         while (acks) {
389                 int i = __ffs(acks);
390
391                 if (tps6586x->irq_en & (1 << i))
392                         handle_nested_irq(tps6586x->irq_base + i);
393
394                 acks &= ~(1 << i);
395         }
396
397         return IRQ_HANDLED;
398 }
399
400 static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
401                                        int irq_base)
402 {
403         int i, ret;
404         u8 tmp[4];
405
406         if (!irq_base) {
407                 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
408                 return -EINVAL;
409         }
410
411         mutex_init(&tps6586x->irq_lock);
412         for (i = 0; i < 5; i++) {
413                 tps6586x->mask_cache[i] = 0xff;
414                 tps6586x->mask_reg[i] = 0xff;
415                 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
416         }
417
418         tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
419
420         tps6586x->irq_base = irq_base;
421
422         tps6586x->irq_chip.name = "tps6586x";
423         tps6586x->irq_chip.enable = tps6586x_irq_enable;
424         tps6586x->irq_chip.disable = tps6586x_irq_disable;
425         tps6586x->irq_chip.bus_lock = tps6586x_irq_lock;
426         tps6586x->irq_chip.bus_sync_unlock = tps6586x_irq_sync_unlock;
427
428         for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
429                 int __irq = i + tps6586x->irq_base;
430                 set_irq_chip_data(__irq, tps6586x);
431                 set_irq_chip_and_handler(__irq, &tps6586x->irq_chip,
432                                          handle_simple_irq);
433                 set_irq_nested_thread(__irq, 1);
434 #ifdef CONFIG_ARM
435                 set_irq_flags(__irq, IRQF_VALID);
436 #endif
437         }
438
439         ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
440                                    "tps6586x", tps6586x);
441
442         if (!ret) {
443                 device_init_wakeup(tps6586x->dev, 1);
444                 enable_irq_wake(irq);
445         }
446
447         return ret;
448 }
449
450 static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
451                                           struct tps6586x_platform_data *pdata)
452 {
453         struct tps6586x_subdev_info *subdev;
454         struct platform_device *pdev;
455         int i, ret = 0;
456
457         for (i = 0; i < pdata->num_subdevs; i++) {
458                 subdev = &pdata->subdevs[i];
459
460                 pdev = platform_device_alloc(subdev->name, subdev->id);
461                 if (!pdev) {
462                         ret = -ENOMEM;
463                         goto failed;
464                 }
465
466                 pdev->dev.parent = tps6586x->dev;
467                 pdev->dev.platform_data = subdev->platform_data;
468
469                 ret = platform_device_add(pdev);
470                 if (ret) {
471                         platform_device_put(pdev);
472                         goto failed;
473                 }
474         }
475         return 0;
476
477 failed:
478         tps6586x_remove_subdevs(tps6586x);
479         return ret;
480 }
481
482 static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
483                                         const struct i2c_device_id *id)
484 {
485         struct tps6586x_platform_data *pdata = client->dev.platform_data;
486         struct tps6586x *tps6586x;
487         int ret;
488
489         if (!pdata) {
490                 dev_err(&client->dev, "tps6586x requires platform data\n");
491                 return -ENOTSUPP;
492         }
493
494         ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
495         if (ret < 0) {
496                 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
497                 return -EIO;
498         }
499
500         if (ret != TPS658621A_VERSIONCRC) {
501                 dev_err(&client->dev, "Unsupported chip ID: %x\n", ret);
502                 return -ENODEV;
503         }
504
505         tps6586x = kzalloc(sizeof(struct tps6586x), GFP_KERNEL);
506         if (tps6586x == NULL)
507                 return -ENOMEM;
508
509         tps6586x->client = client;
510         tps6586x->dev = &client->dev;
511         i2c_set_clientdata(client, tps6586x);
512
513         mutex_init(&tps6586x->lock);
514
515         if (client->irq) {
516                 ret = tps6586x_irq_init(tps6586x, client->irq,
517                                         pdata->irq_base);
518                 if (ret) {
519                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
520                         goto err_irq_init;
521                 }
522         }
523
524         ret = tps6586x_add_subdevs(tps6586x, pdata);
525         if (ret) {
526                 dev_err(&client->dev, "add devices failed: %d\n", ret);
527                 goto err_add_devs;
528         }
529
530         tps6586x_gpio_init(tps6586x, pdata->gpio_base);
531
532         return 0;
533
534 err_add_devs:
535         if (client->irq)
536                 free_irq(client->irq, tps6586x);
537 err_irq_init:
538         kfree(tps6586x);
539         return ret;
540 }
541
542 static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
543 {
544         struct tps6586x *tps6586x = i2c_get_clientdata(client);
545         struct tps6586x_platform_data *pdata = client->dev.platform_data;
546         int ret;
547
548         if (client->irq)
549                 free_irq(client->irq, tps6586x);
550
551         if (pdata->gpio_base) {
552                 ret = gpiochip_remove(&tps6586x->gpio);
553                 if (ret)
554                         dev_err(&client->dev, "Can't remove gpio chip: %d\n",
555                                 ret);
556         }
557
558         tps6586x_remove_subdevs(tps6586x);
559         kfree(tps6586x);
560         return 0;
561 }
562
563 static const struct i2c_device_id tps6586x_id_table[] = {
564         { "tps6586x", 0 },
565         { },
566 };
567 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
568
569 static struct i2c_driver tps6586x_driver = {
570         .driver = {
571                 .name   = "tps6586x",
572                 .owner  = THIS_MODULE,
573         },
574         .probe          = tps6586x_i2c_probe,
575         .remove         = __devexit_p(tps6586x_i2c_remove),
576         .id_table       = tps6586x_id_table,
577 };
578
579 static int __init tps6586x_init(void)
580 {
581         return i2c_add_driver(&tps6586x_driver);
582 }
583 subsys_initcall(tps6586x_init);
584
585 static void __exit tps6586x_exit(void)
586 {
587         i2c_del_driver(&tps6586x_driver);
588 }
589 module_exit(tps6586x_exit);
590
591 MODULE_DESCRIPTION("TPS6586X core driver");
592 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
593 MODULE_LICENSE("GPL");
594