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>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/tps6586x.h>
31 #define TPS6586X_SUPPLYENE 0x14
32 #define EXITSLREQ_BIT BIT(1)
33 #define SLEEP_MODE_BIT BIT(3)
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
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
49 #define TPS6586X_VERSIONCRC 0xcd
51 /* Maximum register */
52 #define TPS6586X_MAX_REGISTER (TPS6586X_VERSIONCRC + 1)
54 struct tps6586x_irq_data
{
59 #define TPS6586X_IRQ(_reg, _mask) \
61 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
62 .mask_mask = (_mask), \
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),
95 static struct mfd_cell tps6586x_cell
[] = {
97 .name
= "tps6586x-gpio",
100 .name
= "tps6586x-pmic",
103 .name
= "tps6586x-rtc",
106 .name
= "tps6586x-onkey",
112 struct i2c_client
*client
;
113 struct regmap
*regmap
;
115 struct irq_chip irq_chip
;
116 struct mutex irq_lock
;
122 static inline struct tps6586x
*dev_to_tps6586x(struct device
*dev
)
124 return i2c_get_clientdata(to_i2c_client(dev
));
127 int tps6586x_write(struct device
*dev
, int reg
, uint8_t val
)
129 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
131 return regmap_write(tps6586x
->regmap
, reg
, val
);
133 EXPORT_SYMBOL_GPL(tps6586x_write
);
135 int tps6586x_writes(struct device
*dev
, int reg
, int len
, uint8_t *val
)
137 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
139 return regmap_bulk_write(tps6586x
->regmap
, reg
, val
, len
);
141 EXPORT_SYMBOL_GPL(tps6586x_writes
);
143 int tps6586x_read(struct device
*dev
, int reg
, uint8_t *val
)
145 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
149 ret
= regmap_read(tps6586x
->regmap
, reg
, &rval
);
154 EXPORT_SYMBOL_GPL(tps6586x_read
);
156 int tps6586x_reads(struct device
*dev
, int reg
, int len
, uint8_t *val
)
158 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
160 return regmap_bulk_read(tps6586x
->regmap
, reg
, val
, len
);
162 EXPORT_SYMBOL_GPL(tps6586x_reads
);
164 int tps6586x_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
166 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
168 return regmap_update_bits(tps6586x
->regmap
, reg
, bit_mask
, bit_mask
);
170 EXPORT_SYMBOL_GPL(tps6586x_set_bits
);
172 int tps6586x_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
174 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
176 return regmap_update_bits(tps6586x
->regmap
, reg
, bit_mask
, 0);
178 EXPORT_SYMBOL_GPL(tps6586x_clr_bits
);
180 int tps6586x_update(struct device
*dev
, int reg
, uint8_t val
, uint8_t mask
)
182 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
184 return regmap_update_bits(tps6586x
->regmap
, reg
, mask
, val
);
186 EXPORT_SYMBOL_GPL(tps6586x_update
);
188 static int __remove_subdev(struct device
*dev
, void *unused
)
190 platform_device_unregister(to_platform_device(dev
));
194 static int tps6586x_remove_subdevs(struct tps6586x
*tps6586x
)
196 return device_for_each_child(tps6586x
->dev
, NULL
, __remove_subdev
);
199 static void tps6586x_irq_lock(struct irq_data
*data
)
201 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
203 mutex_lock(&tps6586x
->irq_lock
);
206 static void tps6586x_irq_enable(struct irq_data
*irq_data
)
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
];
212 tps6586x
->mask_reg
[data
->mask_reg
] &= ~data
->mask_mask
;
213 tps6586x
->irq_en
|= (1 << __irq
);
216 static void tps6586x_irq_disable(struct irq_data
*irq_data
)
218 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(irq_data
);
220 unsigned int __irq
= irq_data
->irq
- tps6586x
->irq_base
;
221 const struct tps6586x_irq_data
*data
= &tps6586x_irqs
[__irq
];
223 tps6586x
->mask_reg
[data
->mask_reg
] |= data
->mask_mask
;
224 tps6586x
->irq_en
&= ~(1 << __irq
);
227 static void tps6586x_irq_sync_unlock(struct irq_data
*data
)
229 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
232 for (i
= 0; i
< ARRAY_SIZE(tps6586x
->mask_reg
); i
++) {
234 ret
= tps6586x_write(tps6586x
->dev
,
235 TPS6586X_INT_MASK1
+ i
,
236 tps6586x
->mask_reg
[i
]);
240 mutex_unlock(&tps6586x
->irq_lock
);
243 static irqreturn_t
tps6586x_irq(int irq
, void *data
)
245 struct tps6586x
*tps6586x
= data
;
249 ret
= tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
,
250 sizeof(acks
), (uint8_t *)&acks
);
253 dev_err(tps6586x
->dev
, "failed to read interrupt status\n");
257 acks
= le32_to_cpu(acks
);
262 if (tps6586x
->irq_en
& (1 << i
))
263 handle_nested_irq(tps6586x
->irq_base
+ i
);
271 static int tps6586x_irq_init(struct tps6586x
*tps6586x
, int irq
,
278 dev_warn(tps6586x
->dev
, "No interrupt support on IRQ base\n");
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);
288 tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
, sizeof(tmp
), tmp
);
290 tps6586x
->irq_base
= irq_base
;
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
;
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
,
303 irq_set_nested_thread(__irq
, 1);
305 set_irq_flags(__irq
, IRQF_VALID
);
309 ret
= request_threaded_irq(irq
, NULL
, tps6586x_irq
, IRQF_ONESHOT
,
310 "tps6586x", tps6586x
);
313 device_init_wakeup(tps6586x
->dev
, 1);
314 enable_irq_wake(irq
);
320 static int tps6586x_add_subdevs(struct tps6586x
*tps6586x
,
321 struct tps6586x_platform_data
*pdata
)
323 struct tps6586x_subdev_info
*subdev
;
324 struct platform_device
*pdev
;
327 for (i
= 0; i
< pdata
->num_subdevs
; i
++) {
328 subdev
= &pdata
->subdevs
[i
];
330 pdev
= platform_device_alloc(subdev
->name
, subdev
->id
);
336 pdev
->dev
.parent
= tps6586x
->dev
;
337 pdev
->dev
.platform_data
= subdev
->platform_data
;
338 pdev
->dev
.of_node
= subdev
->of_node
;
340 ret
= platform_device_add(pdev
);
342 platform_device_put(pdev
);
349 tps6586x_remove_subdevs(tps6586x
);
354 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
356 struct device_node
*np
= client
->dev
.of_node
;
357 struct tps6586x_platform_data
*pdata
;
359 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
361 dev_err(&client
->dev
, "Memory allocation failed\n");
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");
374 static struct of_device_id tps6586x_of_match
[] = {
375 { .compatible
= "ti,tps6586x", },
379 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
385 static bool is_volatile_reg(struct device
*dev
, unsigned int reg
)
387 /* Cache all interrupt mask register */
388 if ((reg
>= TPS6586X_INT_MASK1
) && (reg
<= TPS6586X_INT_MASK5
))
394 static const struct regmap_config tps6586x_regmap_config
= {
397 .max_register
= TPS6586X_MAX_REGISTER
- 1,
398 .volatile_reg
= is_volatile_reg
,
399 .cache_type
= REGCACHE_RBTREE
,
402 static struct device
*tps6586x_dev
;
403 static void tps6586x_power_off(void)
405 if (tps6586x_clr_bits(tps6586x_dev
, TPS6586X_SUPPLYENE
, EXITSLREQ_BIT
))
408 tps6586x_set_bits(tps6586x_dev
, TPS6586X_SUPPLYENE
, SLEEP_MODE_BIT
);
411 static int tps6586x_i2c_probe(struct i2c_client
*client
,
412 const struct i2c_device_id
*id
)
414 struct tps6586x_platform_data
*pdata
= client
->dev
.platform_data
;
415 struct tps6586x
*tps6586x
;
418 if (!pdata
&& client
->dev
.of_node
)
419 pdata
= tps6586x_parse_dt(client
);
422 dev_err(&client
->dev
, "tps6586x requires platform data\n");
426 ret
= i2c_smbus_read_byte_data(client
, TPS6586X_VERSIONCRC
);
428 dev_err(&client
->dev
, "Chip ID read failed: %d\n", ret
);
432 dev_info(&client
->dev
, "VERSIONCRC is %02x\n", ret
);
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");
440 tps6586x
->client
= client
;
441 tps6586x
->dev
= &client
->dev
;
442 i2c_set_clientdata(client
, tps6586x
);
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
);
454 ret
= tps6586x_irq_init(tps6586x
, client
->irq
,
457 dev_err(&client
->dev
, "IRQ init failed: %d\n", ret
);
462 ret
= mfd_add_devices(tps6586x
->dev
, -1,
463 tps6586x_cell
, ARRAY_SIZE(tps6586x_cell
),
466 dev_err(&client
->dev
, "mfd_add_devices failed: %d\n", ret
);
470 ret
= tps6586x_add_subdevs(tps6586x
, pdata
);
472 dev_err(&client
->dev
, "add devices failed: %d\n", ret
);
476 if (pdata
->pm_off
&& !pm_power_off
) {
477 tps6586x_dev
= &client
->dev
;
478 pm_power_off
= tps6586x_power_off
;
484 mfd_remove_devices(tps6586x
->dev
);
487 free_irq(client
->irq
, tps6586x
);
491 static int tps6586x_i2c_remove(struct i2c_client
*client
)
493 struct tps6586x
*tps6586x
= i2c_get_clientdata(client
);
495 tps6586x_remove_subdevs(tps6586x
);
496 mfd_remove_devices(tps6586x
->dev
);
498 free_irq(client
->irq
, tps6586x
);
502 static const struct i2c_device_id tps6586x_id_table
[] = {
506 MODULE_DEVICE_TABLE(i2c
, tps6586x_id_table
);
508 static struct i2c_driver tps6586x_driver
= {
511 .owner
= THIS_MODULE
,
512 .of_match_table
= of_match_ptr(tps6586x_of_match
),
514 .probe
= tps6586x_i2c_probe
,
515 .remove
= tps6586x_i2c_remove
,
516 .id_table
= tps6586x_id_table
,
519 static int __init
tps6586x_init(void)
521 return i2c_add_driver(&tps6586x_driver
);
523 subsys_initcall(tps6586x_init
);
525 static void __exit
tps6586x_exit(void)
527 i2c_del_driver(&tps6586x_driver
);
529 module_exit(tps6586x_exit
);
531 MODULE_DESCRIPTION("TPS6586X core driver");
532 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
533 MODULE_LICENSE("GPL");