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/gpio.h>
26 #include <linux/i2c.h>
27 #include <linux/regmap.h>
28 #include <linux/regulator/of_regulator.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/tps6586x.h>
33 /* GPIO control registers */
34 #define TPS6586X_GPIOSET1 0x5d
35 #define TPS6586X_GPIOSET2 0x5e
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),
99 struct i2c_client
*client
;
100 struct regmap
*regmap
;
102 struct gpio_chip gpio
;
103 struct irq_chip irq_chip
;
104 struct mutex irq_lock
;
110 static inline struct tps6586x
*dev_to_tps6586x(struct device
*dev
)
112 return i2c_get_clientdata(to_i2c_client(dev
));
115 int tps6586x_write(struct device
*dev
, int reg
, uint8_t val
)
117 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
119 return regmap_write(tps6586x
->regmap
, reg
, val
);
121 EXPORT_SYMBOL_GPL(tps6586x_write
);
123 int tps6586x_writes(struct device
*dev
, int reg
, int len
, uint8_t *val
)
125 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
127 return regmap_bulk_write(tps6586x
->regmap
, reg
, val
, len
);
129 EXPORT_SYMBOL_GPL(tps6586x_writes
);
131 int tps6586x_read(struct device
*dev
, int reg
, uint8_t *val
)
133 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
137 ret
= regmap_read(tps6586x
->regmap
, reg
, &rval
);
142 EXPORT_SYMBOL_GPL(tps6586x_read
);
144 int tps6586x_reads(struct device
*dev
, int reg
, int len
, uint8_t *val
)
146 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
148 return regmap_bulk_read(tps6586x
->regmap
, reg
, val
, len
);
150 EXPORT_SYMBOL_GPL(tps6586x_reads
);
152 int tps6586x_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
154 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
156 return regmap_update_bits(tps6586x
->regmap
, reg
, bit_mask
, bit_mask
);
158 EXPORT_SYMBOL_GPL(tps6586x_set_bits
);
160 int tps6586x_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
162 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
164 return regmap_update_bits(tps6586x
->regmap
, reg
, bit_mask
, 0);
166 EXPORT_SYMBOL_GPL(tps6586x_clr_bits
);
168 int tps6586x_update(struct device
*dev
, int reg
, uint8_t val
, uint8_t mask
)
170 struct tps6586x
*tps6586x
= dev_to_tps6586x(dev
);
172 return regmap_update_bits(tps6586x
->regmap
, reg
, mask
, val
);
174 EXPORT_SYMBOL_GPL(tps6586x_update
);
176 static int tps6586x_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
178 struct tps6586x
*tps6586x
= container_of(gc
, struct tps6586x
, gpio
);
182 ret
= tps6586x_read(tps6586x
->dev
, TPS6586X_GPIOSET2
, &val
);
186 return !!(val
& (1 << offset
));
190 static void tps6586x_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
193 struct tps6586x
*tps6586x
= container_of(chip
, struct tps6586x
, gpio
);
195 tps6586x_update(tps6586x
->dev
, TPS6586X_GPIOSET2
,
196 value
<< offset
, 1 << offset
);
199 static int tps6586x_gpio_output(struct gpio_chip
*gc
, unsigned offset
,
202 struct tps6586x
*tps6586x
= container_of(gc
, struct tps6586x
, gpio
);
205 tps6586x_gpio_set(gc
, offset
, value
);
207 val
= 0x1 << (offset
* 2);
208 mask
= 0x3 << (offset
* 2);
210 return tps6586x_update(tps6586x
->dev
, TPS6586X_GPIOSET1
, val
, mask
);
213 static int tps6586x_gpio_init(struct tps6586x
*tps6586x
, int gpio_base
)
218 tps6586x
->gpio
.owner
= THIS_MODULE
;
219 tps6586x
->gpio
.label
= tps6586x
->client
->name
;
220 tps6586x
->gpio
.dev
= tps6586x
->dev
;
221 tps6586x
->gpio
.base
= gpio_base
;
222 tps6586x
->gpio
.ngpio
= 4;
223 tps6586x
->gpio
.can_sleep
= 1;
225 /* FIXME: add handling of GPIOs as dedicated inputs */
226 tps6586x
->gpio
.direction_output
= tps6586x_gpio_output
;
227 tps6586x
->gpio
.set
= tps6586x_gpio_set
;
228 tps6586x
->gpio
.get
= tps6586x_gpio_get
;
230 return gpiochip_add(&tps6586x
->gpio
);
233 static int __remove_subdev(struct device
*dev
, void *unused
)
235 platform_device_unregister(to_platform_device(dev
));
239 static int tps6586x_remove_subdevs(struct tps6586x
*tps6586x
)
241 return device_for_each_child(tps6586x
->dev
, NULL
, __remove_subdev
);
244 static void tps6586x_irq_lock(struct irq_data
*data
)
246 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
248 mutex_lock(&tps6586x
->irq_lock
);
251 static void tps6586x_irq_enable(struct irq_data
*irq_data
)
253 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(irq_data
);
254 unsigned int __irq
= irq_data
->irq
- tps6586x
->irq_base
;
255 const struct tps6586x_irq_data
*data
= &tps6586x_irqs
[__irq
];
257 tps6586x
->mask_reg
[data
->mask_reg
] &= ~data
->mask_mask
;
258 tps6586x
->irq_en
|= (1 << __irq
);
261 static void tps6586x_irq_disable(struct irq_data
*irq_data
)
263 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(irq_data
);
265 unsigned int __irq
= irq_data
->irq
- tps6586x
->irq_base
;
266 const struct tps6586x_irq_data
*data
= &tps6586x_irqs
[__irq
];
268 tps6586x
->mask_reg
[data
->mask_reg
] |= data
->mask_mask
;
269 tps6586x
->irq_en
&= ~(1 << __irq
);
272 static void tps6586x_irq_sync_unlock(struct irq_data
*data
)
274 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
277 for (i
= 0; i
< ARRAY_SIZE(tps6586x
->mask_reg
); i
++) {
279 ret
= tps6586x_write(tps6586x
->dev
,
280 TPS6586X_INT_MASK1
+ i
,
281 tps6586x
->mask_reg
[i
]);
285 mutex_unlock(&tps6586x
->irq_lock
);
288 static irqreturn_t
tps6586x_irq(int irq
, void *data
)
290 struct tps6586x
*tps6586x
= data
;
294 ret
= tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
,
295 sizeof(acks
), (uint8_t *)&acks
);
298 dev_err(tps6586x
->dev
, "failed to read interrupt status\n");
302 acks
= le32_to_cpu(acks
);
307 if (tps6586x
->irq_en
& (1 << i
))
308 handle_nested_irq(tps6586x
->irq_base
+ i
);
316 static int __devinit
tps6586x_irq_init(struct tps6586x
*tps6586x
, int irq
,
323 dev_warn(tps6586x
->dev
, "No interrupt support on IRQ base\n");
327 mutex_init(&tps6586x
->irq_lock
);
328 for (i
= 0; i
< 5; i
++) {
329 tps6586x
->mask_reg
[i
] = 0xff;
330 tps6586x_write(tps6586x
->dev
, TPS6586X_INT_MASK1
+ i
, 0xff);
333 tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
, sizeof(tmp
), tmp
);
335 tps6586x
->irq_base
= irq_base
;
337 tps6586x
->irq_chip
.name
= "tps6586x";
338 tps6586x
->irq_chip
.irq_enable
= tps6586x_irq_enable
;
339 tps6586x
->irq_chip
.irq_disable
= tps6586x_irq_disable
;
340 tps6586x
->irq_chip
.irq_bus_lock
= tps6586x_irq_lock
;
341 tps6586x
->irq_chip
.irq_bus_sync_unlock
= tps6586x_irq_sync_unlock
;
343 for (i
= 0; i
< ARRAY_SIZE(tps6586x_irqs
); i
++) {
344 int __irq
= i
+ tps6586x
->irq_base
;
345 irq_set_chip_data(__irq
, tps6586x
);
346 irq_set_chip_and_handler(__irq
, &tps6586x
->irq_chip
,
348 irq_set_nested_thread(__irq
, 1);
350 set_irq_flags(__irq
, IRQF_VALID
);
354 ret
= request_threaded_irq(irq
, NULL
, tps6586x_irq
, IRQF_ONESHOT
,
355 "tps6586x", tps6586x
);
358 device_init_wakeup(tps6586x
->dev
, 1);
359 enable_irq_wake(irq
);
365 static int __devinit
tps6586x_add_subdevs(struct tps6586x
*tps6586x
,
366 struct tps6586x_platform_data
*pdata
)
368 struct tps6586x_subdev_info
*subdev
;
369 struct platform_device
*pdev
;
372 for (i
= 0; i
< pdata
->num_subdevs
; i
++) {
373 subdev
= &pdata
->subdevs
[i
];
375 pdev
= platform_device_alloc(subdev
->name
, subdev
->id
);
381 pdev
->dev
.parent
= tps6586x
->dev
;
382 pdev
->dev
.platform_data
= subdev
->platform_data
;
383 pdev
->dev
.of_node
= subdev
->of_node
;
385 ret
= platform_device_add(pdev
);
387 platform_device_put(pdev
);
394 tps6586x_remove_subdevs(tps6586x
);
399 static struct of_regulator_match tps6586x_matches
[] = {
400 { .name
= "sm0", .driver_data
= (void *)TPS6586X_ID_SM_0
},
401 { .name
= "sm1", .driver_data
= (void *)TPS6586X_ID_SM_1
},
402 { .name
= "sm2", .driver_data
= (void *)TPS6586X_ID_SM_2
},
403 { .name
= "ldo0", .driver_data
= (void *)TPS6586X_ID_LDO_0
},
404 { .name
= "ldo1", .driver_data
= (void *)TPS6586X_ID_LDO_1
},
405 { .name
= "ldo2", .driver_data
= (void *)TPS6586X_ID_LDO_2
},
406 { .name
= "ldo3", .driver_data
= (void *)TPS6586X_ID_LDO_3
},
407 { .name
= "ldo4", .driver_data
= (void *)TPS6586X_ID_LDO_4
},
408 { .name
= "ldo5", .driver_data
= (void *)TPS6586X_ID_LDO_5
},
409 { .name
= "ldo6", .driver_data
= (void *)TPS6586X_ID_LDO_6
},
410 { .name
= "ldo7", .driver_data
= (void *)TPS6586X_ID_LDO_7
},
411 { .name
= "ldo8", .driver_data
= (void *)TPS6586X_ID_LDO_8
},
412 { .name
= "ldo9", .driver_data
= (void *)TPS6586X_ID_LDO_9
},
413 { .name
= "ldo_rtc", .driver_data
= (void *)TPS6586X_ID_LDO_RTC
},
416 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
418 const unsigned int num
= ARRAY_SIZE(tps6586x_matches
);
419 struct device_node
*np
= client
->dev
.of_node
;
420 struct tps6586x_platform_data
*pdata
;
421 struct tps6586x_subdev_info
*devs
;
422 struct device_node
*regs
;
427 regs
= of_find_node_by_name(np
, "regulators");
431 err
= of_regulator_match(&client
->dev
, regs
, tps6586x_matches
, num
);
440 devs
= devm_kzalloc(&client
->dev
, count
* sizeof(*devs
), GFP_KERNEL
);
444 for (i
= 0, j
= 0; i
< num
&& j
< count
; i
++) {
445 if (!tps6586x_matches
[i
].init_data
)
448 devs
[j
].name
= "tps6586x-regulator";
449 devs
[j
].platform_data
= tps6586x_matches
[i
].init_data
;
450 devs
[j
].id
= (int)tps6586x_matches
[i
].driver_data
;
451 devs
[j
].of_node
= tps6586x_matches
[i
].of_node
;
455 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
459 pdata
->num_subdevs
= count
;
460 pdata
->subdevs
= devs
;
461 pdata
->gpio_base
= -1;
462 pdata
->irq_base
= -1;
467 static struct of_device_id tps6586x_of_match
[] = {
468 { .compatible
= "ti,tps6586x", },
472 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
478 static bool is_volatile_reg(struct device
*dev
, unsigned int reg
)
480 /* Cache all interrupt mask register */
481 if ((reg
>= TPS6586X_INT_MASK1
) && (reg
<= TPS6586X_INT_MASK5
))
487 static const struct regmap_config tps6586x_regmap_config
= {
490 .max_register
= TPS6586X_MAX_REGISTER
- 1,
491 .volatile_reg
= is_volatile_reg
,
492 .cache_type
= REGCACHE_RBTREE
,
495 static int __devinit
tps6586x_i2c_probe(struct i2c_client
*client
,
496 const struct i2c_device_id
*id
)
498 struct tps6586x_platform_data
*pdata
= client
->dev
.platform_data
;
499 struct tps6586x
*tps6586x
;
502 if (!pdata
&& client
->dev
.of_node
)
503 pdata
= tps6586x_parse_dt(client
);
506 dev_err(&client
->dev
, "tps6586x requires platform data\n");
510 ret
= i2c_smbus_read_byte_data(client
, TPS6586X_VERSIONCRC
);
512 dev_err(&client
->dev
, "Chip ID read failed: %d\n", ret
);
516 dev_info(&client
->dev
, "VERSIONCRC is %02x\n", ret
);
518 tps6586x
= devm_kzalloc(&client
->dev
, sizeof(*tps6586x
), GFP_KERNEL
);
519 if (tps6586x
== NULL
) {
520 dev_err(&client
->dev
, "memory for tps6586x alloc failed\n");
524 tps6586x
->client
= client
;
525 tps6586x
->dev
= &client
->dev
;
526 i2c_set_clientdata(client
, tps6586x
);
528 tps6586x
->regmap
= devm_regmap_init_i2c(client
,
529 &tps6586x_regmap_config
);
530 if (IS_ERR(tps6586x
->regmap
)) {
531 ret
= PTR_ERR(tps6586x
->regmap
);
532 dev_err(&client
->dev
, "regmap init failed: %d\n", ret
);
538 ret
= tps6586x_irq_init(tps6586x
, client
->irq
,
541 dev_err(&client
->dev
, "IRQ init failed: %d\n", ret
);
546 ret
= tps6586x_gpio_init(tps6586x
, pdata
->gpio_base
);
548 dev_err(&client
->dev
, "GPIO registration failed: %d\n", ret
);
552 ret
= tps6586x_add_subdevs(tps6586x
, pdata
);
554 dev_err(&client
->dev
, "add devices failed: %d\n", ret
);
561 if (pdata
->gpio_base
) {
562 ret
= gpiochip_remove(&tps6586x
->gpio
);
564 dev_err(&client
->dev
, "Can't remove gpio chip: %d\n",
569 free_irq(client
->irq
, tps6586x
);
574 static int __devexit
tps6586x_i2c_remove(struct i2c_client
*client
)
576 struct tps6586x
*tps6586x
= i2c_get_clientdata(client
);
577 struct tps6586x_platform_data
*pdata
= client
->dev
.platform_data
;
581 free_irq(client
->irq
, tps6586x
);
583 if (pdata
->gpio_base
) {
584 ret
= gpiochip_remove(&tps6586x
->gpio
);
586 dev_err(&client
->dev
, "Can't remove gpio chip: %d\n",
590 tps6586x_remove_subdevs(tps6586x
);
594 static const struct i2c_device_id tps6586x_id_table
[] = {
598 MODULE_DEVICE_TABLE(i2c
, tps6586x_id_table
);
600 static struct i2c_driver tps6586x_driver
= {
603 .owner
= THIS_MODULE
,
604 .of_match_table
= of_match_ptr(tps6586x_of_match
),
606 .probe
= tps6586x_i2c_probe
,
607 .remove
= __devexit_p(tps6586x_i2c_remove
),
608 .id_table
= tps6586x_id_table
,
611 static int __init
tps6586x_init(void)
613 return i2c_add_driver(&tps6586x_driver
);
615 subsys_initcall(tps6586x_init
);
617 static void __exit
tps6586x_exit(void)
619 i2c_del_driver(&tps6586x_driver
);
621 module_exit(tps6586x_exit
);
623 MODULE_DESCRIPTION("TPS6586X core driver");
624 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
625 MODULE_LICENSE("GPL");