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>
28 #include <linux/regulator/machine.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/tps6586x.h>
33 #define TPS6586X_SUPPLYENE 0x14
34 #define EXITSLREQ_BIT BIT(1)
35 #define SLEEP_MODE_BIT BIT(3)
37 /* interrupt control registers */
38 #define TPS6586X_INT_ACK1 0xb5
39 #define TPS6586X_INT_ACK2 0xb6
40 #define TPS6586X_INT_ACK3 0xb7
41 #define TPS6586X_INT_ACK4 0xb8
43 /* interrupt mask registers */
44 #define TPS6586X_INT_MASK1 0xb0
45 #define TPS6586X_INT_MASK2 0xb1
46 #define TPS6586X_INT_MASK3 0xb2
47 #define TPS6586X_INT_MASK4 0xb3
48 #define TPS6586X_INT_MASK5 0xb4
51 #define TPS6586X_VERSIONCRC 0xcd
53 /* Maximum register */
54 #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
56 struct tps6586x_irq_data {
61 #define TPS6586X_IRQ(_reg, _mask) \
63 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
64 .mask_mask = (_mask), \
67 static const struct tps6586x_irq_data tps6586x_irqs[] = {
68 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
69 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
70 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
71 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
72 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
73 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
74 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
75 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
76 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
77 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
78 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
79 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
80 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
81 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
82 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
83 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
84 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
85 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
86 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
87 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
88 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
89 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
90 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
91 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
92 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
93 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
94 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
97 static struct mfd_cell tps6586x_cell[] = {
99 .name = "tps6586x-gpio",
102 .name = "tps6586x-rtc",
105 .name = "tps6586x-onkey",
111 struct i2c_client *client;
112 struct regmap *regmap;
114 struct irq_chip irq_chip;
115 struct mutex irq_lock;
121 static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
123 return i2c_get_clientdata(to_i2c_client(dev));
126 int tps6586x_write(struct device *dev, int reg, uint8_t val)
128 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
130 return regmap_write(tps6586x->regmap, reg, val);
132 EXPORT_SYMBOL_GPL(tps6586x_write);
134 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
136 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
138 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
140 EXPORT_SYMBOL_GPL(tps6586x_writes);
142 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
144 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
148 ret = regmap_read(tps6586x->regmap, reg, &rval);
153 EXPORT_SYMBOL_GPL(tps6586x_read);
155 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
157 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
159 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
161 EXPORT_SYMBOL_GPL(tps6586x_reads);
163 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
165 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
167 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
169 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
171 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
173 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
175 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
177 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
179 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
181 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
183 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
185 EXPORT_SYMBOL_GPL(tps6586x_update);
187 static int __remove_subdev(struct device *dev, void *unused)
189 platform_device_unregister(to_platform_device(dev));
193 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
195 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
198 static void tps6586x_irq_lock(struct irq_data *data)
200 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
202 mutex_lock(&tps6586x->irq_lock);
205 static void tps6586x_irq_enable(struct irq_data *irq_data)
207 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
208 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
209 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
211 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
212 tps6586x->irq_en |= (1 << __irq);
215 static void tps6586x_irq_disable(struct irq_data *irq_data)
217 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
219 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
220 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
222 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
223 tps6586x->irq_en &= ~(1 << __irq);
226 static void tps6586x_irq_sync_unlock(struct irq_data *data)
228 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
231 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
233 ret = tps6586x_write(tps6586x->dev,
234 TPS6586X_INT_MASK1 + i,
235 tps6586x->mask_reg[i]);
239 mutex_unlock(&tps6586x->irq_lock);
242 static irqreturn_t tps6586x_irq(int irq, void *data)
244 struct tps6586x *tps6586x = data;
248 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
249 sizeof(acks), (uint8_t *)&acks);
252 dev_err(tps6586x->dev, "failed to read interrupt status\n");
256 acks = le32_to_cpu(acks);
261 if (tps6586x->irq_en & (1 << i))
262 handle_nested_irq(tps6586x->irq_base + i);
270 static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
277 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
281 mutex_init(&tps6586x->irq_lock);
282 for (i = 0; i < 5; i++) {
283 tps6586x->mask_reg[i] = 0xff;
284 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
287 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
289 tps6586x->irq_base = irq_base;
291 tps6586x->irq_chip.name = "tps6586x";
292 tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
293 tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
294 tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
295 tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
297 for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
298 int __irq = i + tps6586x->irq_base;
299 irq_set_chip_data(__irq, tps6586x);
300 irq_set_chip_and_handler(__irq, &tps6586x->irq_chip,
302 irq_set_nested_thread(__irq, 1);
304 set_irq_flags(__irq, IRQF_VALID);
308 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
309 "tps6586x", tps6586x);
312 device_init_wakeup(tps6586x->dev, 1);
313 enable_irq_wake(irq);
319 static int tps6586x_add_subdevs(struct tps6586x *tps6586x,
320 struct tps6586x_platform_data *pdata)
322 struct tps6586x_subdev_info *subdev;
323 struct platform_device *pdev;
326 for (i = 0; i < pdata->num_subdevs; i++) {
327 subdev = &pdata->subdevs[i];
329 pdev = platform_device_alloc(subdev->name, subdev->id);
335 pdev->dev.parent = tps6586x->dev;
336 pdev->dev.platform_data = subdev->platform_data;
337 pdev->dev.of_node = subdev->of_node;
339 ret = platform_device_add(pdev);
341 platform_device_put(pdev);
348 tps6586x_remove_subdevs(tps6586x);
353 static struct of_regulator_match tps6586x_matches[] = {
354 { .name = "sys", .driver_data = (void *)TPS6586X_ID_SYS },
355 { .name = "sm0", .driver_data = (void *)TPS6586X_ID_SM_0 },
356 { .name = "sm1", .driver_data = (void *)TPS6586X_ID_SM_1 },
357 { .name = "sm2", .driver_data = (void *)TPS6586X_ID_SM_2 },
358 { .name = "ldo0", .driver_data = (void *)TPS6586X_ID_LDO_0 },
359 { .name = "ldo1", .driver_data = (void *)TPS6586X_ID_LDO_1 },
360 { .name = "ldo2", .driver_data = (void *)TPS6586X_ID_LDO_2 },
361 { .name = "ldo3", .driver_data = (void *)TPS6586X_ID_LDO_3 },
362 { .name = "ldo4", .driver_data = (void *)TPS6586X_ID_LDO_4 },
363 { .name = "ldo5", .driver_data = (void *)TPS6586X_ID_LDO_5 },
364 { .name = "ldo6", .driver_data = (void *)TPS6586X_ID_LDO_6 },
365 { .name = "ldo7", .driver_data = (void *)TPS6586X_ID_LDO_7 },
366 { .name = "ldo8", .driver_data = (void *)TPS6586X_ID_LDO_8 },
367 { .name = "ldo9", .driver_data = (void *)TPS6586X_ID_LDO_9 },
368 { .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
371 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
373 const unsigned int num = ARRAY_SIZE(tps6586x_matches);
374 struct device_node *np = client->dev.of_node;
375 struct tps6586x_platform_data *pdata;
376 struct tps6586x_subdev_info *devs;
377 struct device_node *regs;
378 const char *sys_rail_name = NULL;
383 regs = of_find_node_by_name(np, "regulators");
387 err = of_regulator_match(&client->dev, regs, tps6586x_matches, num);
396 devs = devm_kzalloc(&client->dev, count * sizeof(*devs), GFP_KERNEL);
400 for (i = 0, j = 0; i < num && j < count; i++) {
401 struct regulator_init_data *reg_idata;
403 if (!tps6586x_matches[i].init_data)
406 reg_idata = tps6586x_matches[i].init_data;
407 devs[j].name = "tps6586x-regulator";
408 devs[j].platform_data = tps6586x_matches[i].init_data;
409 devs[j].id = (int)tps6586x_matches[i].driver_data;
410 if (devs[j].id == TPS6586X_ID_SYS)
411 sys_rail_name = reg_idata->constraints.name;
413 if ((devs[j].id == TPS6586X_ID_LDO_5) ||
414 (devs[j].id == TPS6586X_ID_LDO_RTC))
415 reg_idata->supply_regulator = sys_rail_name;
417 devs[j].of_node = tps6586x_matches[i].of_node;
421 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
425 pdata->num_subdevs = count;
426 pdata->subdevs = devs;
427 pdata->gpio_base = -1;
428 pdata->irq_base = -1;
429 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
434 static struct of_device_id tps6586x_of_match[] = {
435 { .compatible = "ti,tps6586x", },
439 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
445 static bool is_volatile_reg(struct device *dev, unsigned int reg)
447 /* Cache all interrupt mask register */
448 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
454 static const struct regmap_config tps6586x_regmap_config = {
457 .max_register = TPS6586X_MAX_REGISTER - 1,
458 .volatile_reg = is_volatile_reg,
459 .cache_type = REGCACHE_RBTREE,
462 static struct device *tps6586x_dev;
463 static void tps6586x_power_off(void)
465 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
468 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
471 static int tps6586x_i2c_probe(struct i2c_client *client,
472 const struct i2c_device_id *id)
474 struct tps6586x_platform_data *pdata = client->dev.platform_data;
475 struct tps6586x *tps6586x;
478 if (!pdata && client->dev.of_node)
479 pdata = tps6586x_parse_dt(client);
482 dev_err(&client->dev, "tps6586x requires platform data\n");
486 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
488 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
492 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
494 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
495 if (tps6586x == NULL) {
496 dev_err(&client->dev, "memory for tps6586x alloc failed\n");
500 tps6586x->client = client;
501 tps6586x->dev = &client->dev;
502 i2c_set_clientdata(client, tps6586x);
504 tps6586x->regmap = devm_regmap_init_i2c(client,
505 &tps6586x_regmap_config);
506 if (IS_ERR(tps6586x->regmap)) {
507 ret = PTR_ERR(tps6586x->regmap);
508 dev_err(&client->dev, "regmap init failed: %d\n", ret);
514 ret = tps6586x_irq_init(tps6586x, client->irq,
517 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
522 ret = mfd_add_devices(tps6586x->dev, -1,
523 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
526 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
530 ret = tps6586x_add_subdevs(tps6586x, pdata);
532 dev_err(&client->dev, "add devices failed: %d\n", ret);
536 if (pdata->pm_off && !pm_power_off) {
537 tps6586x_dev = &client->dev;
538 pm_power_off = tps6586x_power_off;
544 mfd_remove_devices(tps6586x->dev);
547 free_irq(client->irq, tps6586x);
551 static int tps6586x_i2c_remove(struct i2c_client *client)
553 struct tps6586x *tps6586x = i2c_get_clientdata(client);
555 tps6586x_remove_subdevs(tps6586x);
556 mfd_remove_devices(tps6586x->dev);
558 free_irq(client->irq, tps6586x);
562 static const struct i2c_device_id tps6586x_id_table[] = {
566 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
568 static struct i2c_driver tps6586x_driver = {
571 .owner = THIS_MODULE,
572 .of_match_table = of_match_ptr(tps6586x_of_match),
574 .probe = tps6586x_i2c_probe,
575 .remove = tps6586x_i2c_remove,
576 .id_table = tps6586x_id_table,
579 static int __init tps6586x_init(void)
581 return i2c_add_driver(&tps6586x_driver);
583 subsys_initcall(tps6586x_init);
585 static void __exit tps6586x_exit(void)
587 i2c_del_driver(&tps6586x_driver);
589 module_exit(tps6586x_exit);
591 MODULE_DESCRIPTION("TPS6586X core driver");
592 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
593 MODULE_LICENSE("GPL");