2 * Core driver for TI TPS6586x PMIC family
4 * Copyright (c) 2010 CompuLab Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
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>
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.
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 #include <linux/regulator/of_regulator.h>
29 #include <linux/mfd/core.h>
30 #include <linux/mfd/tps6586x.h>
32 /* interrupt control registers */
33 #define TPS6586X_INT_ACK1 0xb5
34 #define TPS6586X_INT_ACK2 0xb6
35 #define TPS6586X_INT_ACK3 0xb7
36 #define TPS6586X_INT_ACK4 0xb8
38 /* interrupt mask registers */
39 #define TPS6586X_INT_MASK1 0xb0
40 #define TPS6586X_INT_MASK2 0xb1
41 #define TPS6586X_INT_MASK3 0xb2
42 #define TPS6586X_INT_MASK4 0xb3
43 #define TPS6586X_INT_MASK5 0xb4
46 #define TPS6586X_VERSIONCRC 0xcd
48 /* Maximum register */
49 #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
51 struct tps6586x_irq_data {
56 #define TPS6586X_IRQ(_reg, _mask) \
58 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
59 .mask_mask = (_mask), \
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),
92 static struct mfd_cell tps6586x_cell[] = {
94 .name = "tps6586x-gpio",
97 .name = "tps6586x-rtc",
100 .name = "tps6586x-onkey",
106 struct i2c_client *client;
107 struct regmap *regmap;
109 struct irq_chip irq_chip;
110 struct mutex irq_lock;
116 static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
118 return i2c_get_clientdata(to_i2c_client(dev));
121 int tps6586x_write(struct device *dev, int reg, uint8_t val)
123 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
125 return regmap_write(tps6586x->regmap, reg, val);
127 EXPORT_SYMBOL_GPL(tps6586x_write);
129 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
131 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
133 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
135 EXPORT_SYMBOL_GPL(tps6586x_writes);
137 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
139 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
143 ret = regmap_read(tps6586x->regmap, reg, &rval);
148 EXPORT_SYMBOL_GPL(tps6586x_read);
150 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
152 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
154 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
156 EXPORT_SYMBOL_GPL(tps6586x_reads);
158 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
160 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
162 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
164 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
166 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
168 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
170 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
172 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
174 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
176 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
178 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
180 EXPORT_SYMBOL_GPL(tps6586x_update);
182 static int __remove_subdev(struct device *dev, void *unused)
184 platform_device_unregister(to_platform_device(dev));
188 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
190 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
193 static void tps6586x_irq_lock(struct irq_data *data)
195 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
197 mutex_lock(&tps6586x->irq_lock);
200 static void tps6586x_irq_enable(struct irq_data *irq_data)
202 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
203 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
204 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
206 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
207 tps6586x->irq_en |= (1 << __irq);
210 static void tps6586x_irq_disable(struct irq_data *irq_data)
212 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
214 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
215 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
217 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
218 tps6586x->irq_en &= ~(1 << __irq);
221 static void tps6586x_irq_sync_unlock(struct irq_data *data)
223 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
226 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
228 ret = tps6586x_write(tps6586x->dev,
229 TPS6586X_INT_MASK1 + i,
230 tps6586x->mask_reg[i]);
234 mutex_unlock(&tps6586x->irq_lock);
237 static irqreturn_t tps6586x_irq(int irq, void *data)
239 struct tps6586x *tps6586x = data;
243 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
244 sizeof(acks), (uint8_t *)&acks);
247 dev_err(tps6586x->dev, "failed to read interrupt status\n");
251 acks = le32_to_cpu(acks);
256 if (tps6586x->irq_en & (1 << i))
257 handle_nested_irq(tps6586x->irq_base + i);
265 static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
272 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
276 mutex_init(&tps6586x->irq_lock);
277 for (i = 0; i < 5; i++) {
278 tps6586x->mask_reg[i] = 0xff;
279 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
282 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
284 tps6586x->irq_base = irq_base;
286 tps6586x->irq_chip.name = "tps6586x";
287 tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
288 tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
289 tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
290 tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
292 for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
293 int __irq = i + tps6586x->irq_base;
294 irq_set_chip_data(__irq, tps6586x);
295 irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
297 irq_set_nested_thread(__irq, 1);
299 set_irq_flags(__irq, IRQF_VALID);
303 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
304 "tps6586x", tps6586x);
307 device_init_wakeup(tps6586x->dev, 1);
308 enable_irq_wake(irq);
314 static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
315 struct tps6586x_platform_data *pdata)
317 struct tps6586x_subdev_info *subdev;
318 struct platform_device *pdev;
321 for (i = 0; i < pdata->num_subdevs; i++) {
322 subdev = &pdata->subdevs[i];
324 pdev = platform_device_alloc(subdev->name, subdev->id);
330 pdev->dev.parent = tps6586x->dev;
331 pdev->dev.platform_data = subdev->platform_data;
332 pdev->dev.of_node = subdev->of_node;
334 ret = platform_device_add(pdev);
336 platform_device_put(pdev);
343 tps6586x_remove_subdevs(tps6586x);
348 static struct of_regulator_match tps6586x_matches[] = {
349 { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 },
350 { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 },
351 { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 },
352 { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 },
353 { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 },
354 { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 },
355 { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 },
356 { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 },
357 { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 },
358 { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 },
359 { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 },
360 { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 },
361 { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 },
362 { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
365 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
367 const unsigned int num = ARRAY_SIZE(tps6586x_matches);
368 struct device_node *np = client->dev.of_node;
369 struct tps6586x_platform_data *pdata;
370 struct tps6586x_subdev_info *devs;
371 struct device_node *regs;
376 regs = of_find_node_by_name(np, "regulators");
380 err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
389 devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
393 for (i = 0, j = 0; i < num && j < count; i++) {
394 if (!tps6586x_matches[i].init_data)
397 devs[j].name = "tps6586x-regulator";
398 devs[j].platform_data = tps6586x_matches[i].init_data;
399 devs[j].id = (int)tps6586x_matches[i].driver_data;
400 devs[j].of_node = tps6586x_matches[i].of_node;
404 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
408 pdata->num_subdevs = count;
409 pdata->subdevs = devs;
410 pdata->gpio_base = -1;
411 pdata->irq_base = -1;
416 static struct of_device_id tps6586x_of_match[] = {
417 { .compatible = "ti,tps6586x", },
421 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
427 static bool is_volatile_reg(struct device *dev, unsigned int reg)
429 /* Cache all interrupt mask register */
430 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
436 static const struct regmap_config tps6586x_regmap_config = {
439 .max_register = TPS6586X_MAX_REGISTER - 1,
440 .volatile_reg = is_volatile_reg,
441 .cache_type = REGCACHE_RBTREE,
444 static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
445 const struct i2c_device_id *id)
447 struct tps6586x_platform_data *pdata = client->dev.platform_data;
448 struct tps6586x *tps6586x;
451 if (!pdata && client->dev.of_node)
452 pdata = tps6586x_parse_dt(client);
455 dev_err(&client->dev, "tps6586x requires platform data\n");
459 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
461 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
465 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
467 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
468 if (tps6586x == NULL) {
469 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
473 tps6586x->client = client;
474 tps6586x->dev = &client->dev;
475 i2c_set_clientdata(client, tps6586x);
477 tps6586x->regmap = devm_regmap_init_i2c(client,
478 &tps6586x_regmap_config);
479 if (IS_ERR(tps6586x->regmap)) {
480 ret = PTR_ERR(tps6586x->regmap);
481 dev_err(&client->dev, "regmap init failed: %d\n", ret);
487 ret = tps6586x_irq_init(tps6586x, client->irq,
490 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
495 ret = mfd_add_devices(tps6586x->dev, -1,
496 tps6586x_cell, ARRAY_SIZE(tps6586x_cell), NULL, 0);
498 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
502 ret = tps6586x_add_subdevs(tps6586x, pdata);
504 dev_err(&client->dev, "add devices failed: %d\n", ret);
511 mfd_remove_devices(tps6586x->dev);
514 free_irq(client->irq, tps6586x);
518 static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
520 struct tps6586x *tps6586x = i2c_get_clientdata(client);
522 tps6586x_remove_subdevs(tps6586x);
523 mfd_remove_devices(tps6586x->dev);
525 free_irq(client->irq, tps6586x);
529 static const struct i2c_device_id tps6586x_id_table[] = {
533 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
535 static struct i2c_driver tps6586x_driver = {
538 .owner = THIS_MODULE,
539 .of_match_table = of_match_ptr(tps6586x_of_match),
541 .probe = tps6586x_i2c_probe,
542 .remove = __devexit_p(tps6586x_i2c_remove),
543 .id_table = tps6586x_id_table,
546 static int __init tps6586x_init(void)
548 return i2c_add_driver(&tps6586x_driver);
550 subsys_initcall(tps6586x_init);
552 static void __exit tps6586x_exit(void)
554 i2c_del_driver(&tps6586x_driver);
556 module_exit(tps6586x_exit);
558 MODULE_DESCRIPTION("TPS6586X core driver");
559 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
560 MODULE_LICENSE("GPL");