]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/mfd/tps6586x.c
Merge tag 'regulator-3.8' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[can-eth-gw-linux.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/err.h>
25 #include <linux/i2c.h>
26 #include <linux/regmap.h>
27
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/tps6586x.h>
30
31 #define TPS6586X_SUPPLYENE      0x14
32 #define EXITSLREQ_BIT           BIT(1)
33 #define SLEEP_MODE_BIT          BIT(3)
34
35 /* interrupt control registers */
36 #define TPS6586X_INT_ACK1       0xb5
37 #define TPS6586X_INT_ACK2       0xb6
38 #define TPS6586X_INT_ACK3       0xb7
39 #define TPS6586X_INT_ACK4       0xb8
40
41 /* interrupt mask registers */
42 #define TPS6586X_INT_MASK1      0xb0
43 #define TPS6586X_INT_MASK2      0xb1
44 #define TPS6586X_INT_MASK3      0xb2
45 #define TPS6586X_INT_MASK4      0xb3
46 #define TPS6586X_INT_MASK5      0xb4
47
48 /* device id */
49 #define TPS6586X_VERSIONCRC     0xcd
50
51 /* Maximum register */
52 #define TPS6586X_MAX_REGISTER   (TPS6586X_VERSIONCRC + 1)
53
54 struct tps6586x_irq_data {
55         u8      mask_reg;
56         u8      mask_mask;
57 };
58
59 #define TPS6586X_IRQ(_reg, _mask)                               \
60         {                                                       \
61                 .mask_reg = (_reg) - TPS6586X_INT_MASK1,        \
62                 .mask_mask = (_mask),                           \
63         }
64
65 static const struct tps6586x_irq_data tps6586x_irqs[] = {
66         [TPS6586X_INT_PLDO_0]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
67         [TPS6586X_INT_PLDO_1]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
68         [TPS6586X_INT_PLDO_2]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
69         [TPS6586X_INT_PLDO_3]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
70         [TPS6586X_INT_PLDO_4]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
71         [TPS6586X_INT_PLDO_5]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
72         [TPS6586X_INT_PLDO_6]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
73         [TPS6586X_INT_PLDO_7]   = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
74         [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
75         [TPS6586X_INT_ADC]      = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
76         [TPS6586X_INT_PLDO_8]   = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
77         [TPS6586X_INT_PLDO_9]   = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
78         [TPS6586X_INT_PSM_0]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
79         [TPS6586X_INT_PSM_1]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
80         [TPS6586X_INT_PSM_2]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
81         [TPS6586X_INT_PSM_3]    = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
82         [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
83         [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
84         [TPS6586X_INT_USB_DET]  = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
85         [TPS6586X_INT_AC_DET]   = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
86         [TPS6586X_INT_BAT_DET]  = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
87         [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
88         [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
89         [TPS6586X_INT_PP]       = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
90         [TPS6586X_INT_RESUME]   = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
91         [TPS6586X_INT_LOW_SYS]  = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
92         [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
93 };
94
95 static struct mfd_cell tps6586x_cell[] = {
96         {
97                 .name = "tps6586x-gpio",
98         },
99         {
100                 .name = "tps6586x-pmic",
101         },
102         {
103                 .name = "tps6586x-rtc",
104         },
105         {
106                 .name = "tps6586x-onkey",
107         },
108 };
109
110 struct tps6586x {
111         struct device           *dev;
112         struct i2c_client       *client;
113         struct regmap           *regmap;
114
115         struct irq_chip         irq_chip;
116         struct mutex            irq_lock;
117         int                     irq_base;
118         u32                     irq_en;
119         u8                      mask_reg[5];
120 };
121
122 static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
123 {
124         return i2c_get_clientdata(to_i2c_client(dev));
125 }
126
127 int tps6586x_write(struct device *dev, int reg, uint8_t val)
128 {
129         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
130
131         return regmap_write(tps6586x->regmap, reg, val);
132 }
133 EXPORT_SYMBOL_GPL(tps6586x_write);
134
135 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
136 {
137         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
138
139         return regmap_bulk_write(tps6586x->regmap, reg, val, len);
140 }
141 EXPORT_SYMBOL_GPL(tps6586x_writes);
142
143 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
144 {
145         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
146         unsigned int rval;
147         int ret;
148
149         ret = regmap_read(tps6586x->regmap, reg, &rval);
150         if (!ret)
151                 *val = rval;
152         return ret;
153 }
154 EXPORT_SYMBOL_GPL(tps6586x_read);
155
156 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
157 {
158         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
159
160         return regmap_bulk_read(tps6586x->regmap, reg, val, len);
161 }
162 EXPORT_SYMBOL_GPL(tps6586x_reads);
163
164 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
165 {
166         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
167
168         return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
169 }
170 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
171
172 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
173 {
174         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
175
176         return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
177 }
178 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
179
180 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
181 {
182         struct tps6586x *tps6586x = dev_to_tps6586x(dev);
183
184         return regmap_update_bits(tps6586x->regmap, reg, mask, val);
185 }
186 EXPORT_SYMBOL_GPL(tps6586x_update);
187
188 static int __remove_subdev(struct device *dev, void *unused)
189 {
190         platform_device_unregister(to_platform_device(dev));
191         return 0;
192 }
193
194 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
195 {
196         return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
197 }
198
199 static void tps6586x_irq_lock(struct irq_data *data)
200 {
201         struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
202
203         mutex_lock(&tps6586x->irq_lock);
204 }
205
206 static void tps6586x_irq_enable(struct irq_data *irq_data)
207 {
208         struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
209         unsigned int __irq = irq_data->irq - tps6586x->irq_base;
210         const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
211
212         tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
213         tps6586x->irq_en |= (1 << __irq);
214 }
215
216 static void tps6586x_irq_disable(struct irq_data *irq_data)
217 {
218         struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
219
220         unsigned int __irq = irq_data->irq - tps6586x->irq_base;
221         const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
222
223         tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
224         tps6586x->irq_en &= ~(1 << __irq);
225 }
226
227 static void tps6586x_irq_sync_unlock(struct irq_data *data)
228 {
229         struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
230         int i;
231
232         for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
233                 int ret;
234                 ret = tps6586x_write(tps6586x->dev,
235                                             TPS6586X_INT_MASK1 + i,
236                                             tps6586x->mask_reg[i]);
237                 WARN_ON(ret);
238         }
239
240         mutex_unlock(&tps6586x->irq_lock);
241 }
242
243 static irqreturn_t tps6586x_irq(int irq, void *data)
244 {
245         struct tps6586x *tps6586x = data;
246         u32 acks;
247         int ret = 0;
248
249         ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
250                              sizeof(acks), (uint8_t *)&acks);
251
252         if (ret < 0) {
253                 dev_err(tps6586x->dev, "failed to read interrupt status\n");
254                 return IRQ_NONE;
255         }
256
257         acks = le32_to_cpu(acks);
258
259         while (acks) {
260                 int i = __ffs(acks);
261
262                 if (tps6586x->irq_en & (1 << i))
263                         handle_nested_irq(tps6586x->irq_base + i);
264
265                 acks &= ~(1 << i);
266         }
267
268         return IRQ_HANDLED;
269 }
270
271 static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
272                                        int irq_base)
273 {
274         int i, ret;
275         u8 tmp[4];
276
277         if (!irq_base) {
278                 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
279                 return -EINVAL;
280         }
281
282         mutex_init(&tps6586x->irq_lock);
283         for (i = 0; i < 5; i++) {
284                 tps6586x->mask_reg[i] = 0xff;
285                 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
286         }
287
288         tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
289
290         tps6586x->irq_base = irq_base;
291
292         tps6586x->irq_chip.name = "tps6586x";
293         tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
294         tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
295         tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
296         tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
297
298         for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
299                 int __irq = i + tps6586x->irq_base;
300                 irq_set_chip_data(__irq, tps6586x);
301                 irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
302                                          handle_simple_irq);
303                 irq_set_nested_thread(__irq, 1);
304 #ifdef CONFIG_ARM
305                 set_irq_flags(__irq, IRQF_VALID);
306 #endif
307         }
308
309         ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
310                                    "tps6586x", tps6586x);
311
312         if (!ret) {
313                 device_init_wakeup(tps6586x->dev, 1);
314                 enable_irq_wake(irq);
315         }
316
317         return ret;
318 }
319
320 static int tps6586x_add_subdevs(struct tps6586x *tps6586x,
321                                           struct tps6586x_platform_data *pdata)
322 {
323         struct tps6586x_subdev_info *subdev;
324         struct platform_device *pdev;
325         int i, ret = 0;
326
327         for (i = 0; i < pdata->num_subdevs; i++) {
328                 subdev = &pdata->subdevs[i];
329
330                 pdev = platform_device_alloc(subdev->name, subdev->id);
331                 if (!pdev) {
332                         ret = -ENOMEM;
333                         goto failed;
334                 }
335
336                 pdev->dev.parent = tps6586x->dev;
337                 pdev->dev.platform_data = subdev->platform_data;
338                 pdev->dev.of_node = subdev->of_node;
339
340                 ret = platform_device_add(pdev);
341                 if (ret) {
342                         platform_device_put(pdev);
343                         goto failed;
344                 }
345         }
346         return 0;
347
348 failed:
349         tps6586x_remove_subdevs(tps6586x);
350         return ret;
351 }
352
353 #ifdef CONFIG_OF
354 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
355 {
356         struct device_node *np = client->dev.of_node;
357         struct tps6586x_platform_data *pdata;
358
359         pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
360         if (!pdata) {
361                 dev_err(&client->dev, "Memory allocation failed\n");
362                 return NULL;
363         }
364
365         pdata->num_subdevs = 0;
366         pdata->subdevs = NULL;
367         pdata->gpio_base = -1;
368         pdata->irq_base = -1;
369         pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
370
371         return pdata;
372 }
373
374 static struct of_device_id tps6586x_of_match[] = {
375         { .compatible = "ti,tps6586x", },
376         { },
377 };
378 #else
379 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
380 {
381         return NULL;
382 }
383 #endif
384
385 static bool is_volatile_reg(struct device *dev, unsigned int reg)
386 {
387         /* Cache all interrupt mask register */
388         if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
389                 return false;
390
391         return true;
392 }
393
394 static const struct regmap_config tps6586x_regmap_config = {
395         .reg_bits = 8,
396         .val_bits = 8,
397         .max_register = TPS6586X_MAX_REGISTER - 1,
398         .volatile_reg = is_volatile_reg,
399         .cache_type = REGCACHE_RBTREE,
400 };
401
402 static struct device *tps6586x_dev;
403 static void tps6586x_power_off(void)
404 {
405         if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
406                 return;
407
408         tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
409 }
410
411 static int tps6586x_i2c_probe(struct i2c_client *client,
412                                         const struct i2c_device_id *id)
413 {
414         struct tps6586x_platform_data *pdata = client->dev.platform_data;
415         struct tps6586x *tps6586x;
416         int ret;
417
418         if (!pdata && client->dev.of_node)
419                 pdata = tps6586x_parse_dt(client);
420
421         if (!pdata) {
422                 dev_err(&client->dev, "tps6586x requires platform data\n");
423                 return -ENOTSUPP;
424         }
425
426         ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
427         if (ret < 0) {
428                 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
429                 return -EIO;
430         }
431
432         dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
433
434         tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
435         if (tps6586x == NULL) {
436                 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
437                 return -ENOMEM;
438         }
439
440         tps6586x->client = client;
441         tps6586x->dev = &client->dev;
442         i2c_set_clientdata(client, tps6586x);
443
444         tps6586x->regmap = devm_regmap_init_i2c(client,
445                                         &tps6586x_regmap_config);
446         if (IS_ERR(tps6586x->regmap)) {
447                 ret = PTR_ERR(tps6586x->regmap);
448                 dev_err(&client->dev, "regmap init failed: %d\n", ret);
449                 return ret;
450         }
451
452
453         if (client->irq) {
454                 ret = tps6586x_irq_init(tps6586x, client->irq,
455                                         pdata->irq_base);
456                 if (ret) {
457                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
458                         return ret;
459                 }
460         }
461
462         ret = mfd_add_devices(tps6586x->dev, -1,
463                               tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
464                               NULL, 0, NULL);
465         if (ret < 0) {
466                 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
467                 goto err_mfd_add;
468         }
469
470         ret = tps6586x_add_subdevs(tps6586x, pdata);
471         if (ret) {
472                 dev_err(&client->dev, "add devices failed: %d\n", ret);
473                 goto err_add_devs;
474         }
475
476         if (pdata->pm_off && !pm_power_off) {
477                 tps6586x_dev = &client->dev;
478                 pm_power_off = tps6586x_power_off;
479         }
480
481         return 0;
482
483 err_add_devs:
484         mfd_remove_devices(tps6586x->dev);
485 err_mfd_add:
486         if (client->irq)
487                 free_irq(client->irq, tps6586x);
488         return ret;
489 }
490
491 static int tps6586x_i2c_remove(struct i2c_client *client)
492 {
493         struct tps6586x *tps6586x = i2c_get_clientdata(client);
494
495         tps6586x_remove_subdevs(tps6586x);
496         mfd_remove_devices(tps6586x->dev);
497         if (client->irq)
498                 free_irq(client->irq, tps6586x);
499         return 0;
500 }
501
502 static const struct i2c_device_id tps6586x_id_table[] = {
503         { "tps6586x", 0 },
504         { },
505 };
506 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
507
508 static struct i2c_driver tps6586x_driver = {
509         .driver = {
510                 .name   = "tps6586x",
511                 .owner  = THIS_MODULE,
512                 .of_match_table = of_match_ptr(tps6586x_of_match),
513         },
514         .probe          = tps6586x_i2c_probe,
515         .remove         = tps6586x_i2c_remove,
516         .id_table       = tps6586x_id_table,
517 };
518
519 static int __init tps6586x_init(void)
520 {
521         return i2c_add_driver(&tps6586x_driver);
522 }
523 subsys_initcall(tps6586x_init);
524
525 static void __exit tps6586x_exit(void)
526 {
527         i2c_del_driver(&tps6586x_driver);
528 }
529 module_exit(tps6586x_exit);
530
531 MODULE_DESCRIPTION("TPS6586X core driver");
532 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
533 MODULE_LICENSE("GPL");