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/gpio.h>
25 #include <linux/i2c.h>
26 #include <linux/regulator/of_regulator.h>
28 #include <linux/mfd/core.h>
29 #include <linux/mfd/tps6586x.h>
31 /* GPIO control registers */
32 #define TPS6586X_GPIOSET1 0x5d
33 #define TPS6586X_GPIOSET2 0x5e
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 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),
95 struct i2c_client
*client
;
97 struct gpio_chip gpio
;
98 struct irq_chip irq_chip
;
99 struct mutex irq_lock
;
106 static inline int __tps6586x_read(struct i2c_client
*client
,
107 int reg
, uint8_t *val
)
111 ret
= i2c_smbus_read_byte_data(client
, reg
);
113 dev_err(&client
->dev
, "failed reading at 0x%02x\n", reg
);
122 static inline int __tps6586x_reads(struct i2c_client
*client
, int reg
,
123 int len
, uint8_t *val
)
127 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, len
, val
);
129 dev_err(&client
->dev
, "failed reading from 0x%02x\n", reg
);
136 static inline int __tps6586x_write(struct i2c_client
*client
,
137 int reg
, uint8_t val
)
141 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
143 dev_err(&client
->dev
, "failed writing 0x%02x to 0x%02x\n",
151 static inline int __tps6586x_writes(struct i2c_client
*client
, int reg
,
152 int len
, uint8_t *val
)
156 for (i
= 0; i
< len
; i
++) {
157 ret
= __tps6586x_write(client
, reg
+ i
, *(val
+ i
));
165 int tps6586x_write(struct device
*dev
, int reg
, uint8_t val
)
167 return __tps6586x_write(to_i2c_client(dev
), reg
, val
);
169 EXPORT_SYMBOL_GPL(tps6586x_write
);
171 int tps6586x_writes(struct device
*dev
, int reg
, int len
, uint8_t *val
)
173 return __tps6586x_writes(to_i2c_client(dev
), reg
, len
, val
);
175 EXPORT_SYMBOL_GPL(tps6586x_writes
);
177 int tps6586x_read(struct device
*dev
, int reg
, uint8_t *val
)
179 return __tps6586x_read(to_i2c_client(dev
), reg
, val
);
181 EXPORT_SYMBOL_GPL(tps6586x_read
);
183 int tps6586x_reads(struct device
*dev
, int reg
, int len
, uint8_t *val
)
185 return __tps6586x_reads(to_i2c_client(dev
), reg
, len
, val
);
187 EXPORT_SYMBOL_GPL(tps6586x_reads
);
189 int tps6586x_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
191 struct tps6586x
*tps6586x
= dev_get_drvdata(dev
);
195 mutex_lock(&tps6586x
->lock
);
197 ret
= __tps6586x_read(to_i2c_client(dev
), reg
, ®_val
);
201 if ((reg_val
& bit_mask
) != bit_mask
) {
203 ret
= __tps6586x_write(to_i2c_client(dev
), reg
, reg_val
);
206 mutex_unlock(&tps6586x
->lock
);
209 EXPORT_SYMBOL_GPL(tps6586x_set_bits
);
211 int tps6586x_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
213 struct tps6586x
*tps6586x
= dev_get_drvdata(dev
);
217 mutex_lock(&tps6586x
->lock
);
219 ret
= __tps6586x_read(to_i2c_client(dev
), reg
, ®_val
);
223 if (reg_val
& bit_mask
) {
224 reg_val
&= ~bit_mask
;
225 ret
= __tps6586x_write(to_i2c_client(dev
), reg
, reg_val
);
228 mutex_unlock(&tps6586x
->lock
);
231 EXPORT_SYMBOL_GPL(tps6586x_clr_bits
);
233 int tps6586x_update(struct device
*dev
, int reg
, uint8_t val
, uint8_t mask
)
235 struct tps6586x
*tps6586x
= dev_get_drvdata(dev
);
239 mutex_lock(&tps6586x
->lock
);
241 ret
= __tps6586x_read(tps6586x
->client
, reg
, ®_val
);
245 if ((reg_val
& mask
) != val
) {
246 reg_val
= (reg_val
& ~mask
) | val
;
247 ret
= __tps6586x_write(tps6586x
->client
, reg
, reg_val
);
250 mutex_unlock(&tps6586x
->lock
);
253 EXPORT_SYMBOL_GPL(tps6586x_update
);
255 static int tps6586x_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
257 struct tps6586x
*tps6586x
= container_of(gc
, struct tps6586x
, gpio
);
261 ret
= __tps6586x_read(tps6586x
->client
, TPS6586X_GPIOSET2
, &val
);
265 return !!(val
& (1 << offset
));
269 static void tps6586x_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
272 struct tps6586x
*tps6586x
= container_of(chip
, struct tps6586x
, gpio
);
274 tps6586x_update(tps6586x
->dev
, TPS6586X_GPIOSET2
,
275 value
<< offset
, 1 << offset
);
278 static int tps6586x_gpio_output(struct gpio_chip
*gc
, unsigned offset
,
281 struct tps6586x
*tps6586x
= container_of(gc
, struct tps6586x
, gpio
);
284 tps6586x_gpio_set(gc
, offset
, value
);
286 val
= 0x1 << (offset
* 2);
287 mask
= 0x3 << (offset
* 2);
289 return tps6586x_update(tps6586x
->dev
, TPS6586X_GPIOSET1
, val
, mask
);
292 static int tps6586x_gpio_init(struct tps6586x
*tps6586x
, int gpio_base
)
297 tps6586x
->gpio
.owner
= THIS_MODULE
;
298 tps6586x
->gpio
.label
= tps6586x
->client
->name
;
299 tps6586x
->gpio
.dev
= tps6586x
->dev
;
300 tps6586x
->gpio
.base
= gpio_base
;
301 tps6586x
->gpio
.ngpio
= 4;
302 tps6586x
->gpio
.can_sleep
= 1;
304 /* FIXME: add handling of GPIOs as dedicated inputs */
305 tps6586x
->gpio
.direction_output
= tps6586x_gpio_output
;
306 tps6586x
->gpio
.set
= tps6586x_gpio_set
;
307 tps6586x
->gpio
.get
= tps6586x_gpio_get
;
309 return gpiochip_add(&tps6586x
->gpio
);
312 static int __remove_subdev(struct device
*dev
, void *unused
)
314 platform_device_unregister(to_platform_device(dev
));
318 static int tps6586x_remove_subdevs(struct tps6586x
*tps6586x
)
320 return device_for_each_child(tps6586x
->dev
, NULL
, __remove_subdev
);
323 static void tps6586x_irq_lock(struct irq_data
*data
)
325 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
327 mutex_lock(&tps6586x
->irq_lock
);
330 static void tps6586x_irq_enable(struct irq_data
*irq_data
)
332 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(irq_data
);
333 unsigned int __irq
= irq_data
->irq
- tps6586x
->irq_base
;
334 const struct tps6586x_irq_data
*data
= &tps6586x_irqs
[__irq
];
336 tps6586x
->mask_reg
[data
->mask_reg
] &= ~data
->mask_mask
;
337 tps6586x
->irq_en
|= (1 << __irq
);
340 static void tps6586x_irq_disable(struct irq_data
*irq_data
)
342 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(irq_data
);
344 unsigned int __irq
= irq_data
->irq
- tps6586x
->irq_base
;
345 const struct tps6586x_irq_data
*data
= &tps6586x_irqs
[__irq
];
347 tps6586x
->mask_reg
[data
->mask_reg
] |= data
->mask_mask
;
348 tps6586x
->irq_en
&= ~(1 << __irq
);
351 static void tps6586x_irq_sync_unlock(struct irq_data
*data
)
353 struct tps6586x
*tps6586x
= irq_data_get_irq_chip_data(data
);
356 for (i
= 0; i
< ARRAY_SIZE(tps6586x
->mask_reg
); i
++) {
357 if (tps6586x
->mask_reg
[i
] != tps6586x
->mask_cache
[i
]) {
358 if (!WARN_ON(tps6586x_write(tps6586x
->dev
,
359 TPS6586X_INT_MASK1
+ i
,
360 tps6586x
->mask_reg
[i
])))
361 tps6586x
->mask_cache
[i
] = tps6586x
->mask_reg
[i
];
365 mutex_unlock(&tps6586x
->irq_lock
);
368 static irqreturn_t
tps6586x_irq(int irq
, void *data
)
370 struct tps6586x
*tps6586x
= data
;
374 ret
= tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
,
375 sizeof(acks
), (uint8_t *)&acks
);
378 dev_err(tps6586x
->dev
, "failed to read interrupt status\n");
382 acks
= le32_to_cpu(acks
);
387 if (tps6586x
->irq_en
& (1 << i
))
388 handle_nested_irq(tps6586x
->irq_base
+ i
);
396 static int __devinit
tps6586x_irq_init(struct tps6586x
*tps6586x
, int irq
,
403 dev_warn(tps6586x
->dev
, "No interrupt support on IRQ base\n");
407 mutex_init(&tps6586x
->irq_lock
);
408 for (i
= 0; i
< 5; i
++) {
409 tps6586x
->mask_cache
[i
] = 0xff;
410 tps6586x
->mask_reg
[i
] = 0xff;
411 tps6586x_write(tps6586x
->dev
, TPS6586X_INT_MASK1
+ i
, 0xff);
414 tps6586x_reads(tps6586x
->dev
, TPS6586X_INT_ACK1
, sizeof(tmp
), tmp
);
416 tps6586x
->irq_base
= irq_base
;
418 tps6586x
->irq_chip
.name
= "tps6586x";
419 tps6586x
->irq_chip
.irq_enable
= tps6586x_irq_enable
;
420 tps6586x
->irq_chip
.irq_disable
= tps6586x_irq_disable
;
421 tps6586x
->irq_chip
.irq_bus_lock
= tps6586x_irq_lock
;
422 tps6586x
->irq_chip
.irq_bus_sync_unlock
= tps6586x_irq_sync_unlock
;
424 for (i
= 0; i
< ARRAY_SIZE(tps6586x_irqs
); i
++) {
425 int __irq
= i
+ tps6586x
->irq_base
;
426 irq_set_chip_data(__irq
, tps6586x
);
427 irq_set_chip_and_handler(__irq
, &tps6586x
->irq_chip
,
429 irq_set_nested_thread(__irq
, 1);
431 set_irq_flags(__irq
, IRQF_VALID
);
435 ret
= request_threaded_irq(irq
, NULL
, tps6586x_irq
, IRQF_ONESHOT
,
436 "tps6586x", tps6586x
);
439 device_init_wakeup(tps6586x
->dev
, 1);
440 enable_irq_wake(irq
);
446 static int __devinit
tps6586x_add_subdevs(struct tps6586x
*tps6586x
,
447 struct tps6586x_platform_data
*pdata
)
449 struct tps6586x_subdev_info
*subdev
;
450 struct platform_device
*pdev
;
453 for (i
= 0; i
< pdata
->num_subdevs
; i
++) {
454 subdev
= &pdata
->subdevs
[i
];
456 pdev
= platform_device_alloc(subdev
->name
, subdev
->id
);
462 pdev
->dev
.parent
= tps6586x
->dev
;
463 pdev
->dev
.platform_data
= subdev
->platform_data
;
464 pdev
->dev
.of_node
= subdev
->of_node
;
466 ret
= platform_device_add(pdev
);
468 platform_device_put(pdev
);
475 tps6586x_remove_subdevs(tps6586x
);
480 static struct of_regulator_match tps6586x_matches
[] = {
481 { .name
= "sm0", .driver_data
= (void *)TPS6586X_ID_SM_0
},
482 { .name
= "sm1", .driver_data
= (void *)TPS6586X_ID_SM_1
},
483 { .name
= "sm2", .driver_data
= (void *)TPS6586X_ID_SM_2
},
484 { .name
= "ldo0", .driver_data
= (void *)TPS6586X_ID_LDO_0
},
485 { .name
= "ldo1", .driver_data
= (void *)TPS6586X_ID_LDO_1
},
486 { .name
= "ldo2", .driver_data
= (void *)TPS6586X_ID_LDO_2
},
487 { .name
= "ldo3", .driver_data
= (void *)TPS6586X_ID_LDO_3
},
488 { .name
= "ldo4", .driver_data
= (void *)TPS6586X_ID_LDO_4
},
489 { .name
= "ldo5", .driver_data
= (void *)TPS6586X_ID_LDO_5
},
490 { .name
= "ldo6", .driver_data
= (void *)TPS6586X_ID_LDO_6
},
491 { .name
= "ldo7", .driver_data
= (void *)TPS6586X_ID_LDO_7
},
492 { .name
= "ldo8", .driver_data
= (void *)TPS6586X_ID_LDO_8
},
493 { .name
= "ldo9", .driver_data
= (void *)TPS6586X_ID_LDO_9
},
494 { .name
= "ldo_rtc", .driver_data
= (void *)TPS6586X_ID_LDO_RTC
},
497 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
499 const unsigned int num
= ARRAY_SIZE(tps6586x_matches
);
500 struct device_node
*np
= client
->dev
.of_node
;
501 struct tps6586x_platform_data
*pdata
;
502 struct tps6586x_subdev_info
*devs
;
503 struct device_node
*regs
;
508 regs
= of_find_node_by_name(np
, "regulators");
512 err
= of_regulator_match(&client
->dev
, regs
, tps6586x_matches
, num
);
521 devs
= devm_kzalloc(&client
->dev
, count
* sizeof(*devs
), GFP_KERNEL
);
525 for (i
= 0, j
= 0; i
< num
&& j
< count
; i
++) {
526 if (!tps6586x_matches
[i
].init_data
)
529 devs
[j
].name
= "tps6586x-regulator";
530 devs
[j
].platform_data
= tps6586x_matches
[i
].init_data
;
531 devs
[j
].id
= (int)tps6586x_matches
[i
].driver_data
;
532 devs
[j
].of_node
= tps6586x_matches
[i
].of_node
;
536 pdata
= devm_kzalloc(&client
->dev
, sizeof(*pdata
), GFP_KERNEL
);
540 pdata
->num_subdevs
= count
;
541 pdata
->subdevs
= devs
;
542 pdata
->gpio_base
= -1;
543 pdata
->irq_base
= -1;
548 static struct of_device_id tps6586x_of_match
[] = {
549 { .compatible
= "ti,tps6586x", },
553 static struct tps6586x_platform_data
*tps6586x_parse_dt(struct i2c_client
*client
)
559 static int __devinit
tps6586x_i2c_probe(struct i2c_client
*client
,
560 const struct i2c_device_id
*id
)
562 struct tps6586x_platform_data
*pdata
= client
->dev
.platform_data
;
563 struct tps6586x
*tps6586x
;
566 if (!pdata
&& client
->dev
.of_node
)
567 pdata
= tps6586x_parse_dt(client
);
570 dev_err(&client
->dev
, "tps6586x requires platform data\n");
574 ret
= i2c_smbus_read_byte_data(client
, TPS6586X_VERSIONCRC
);
576 dev_err(&client
->dev
, "Chip ID read failed: %d\n", ret
);
580 dev_info(&client
->dev
, "VERSIONCRC is %02x\n", ret
);
582 tps6586x
= kzalloc(sizeof(struct tps6586x
), GFP_KERNEL
);
583 if (tps6586x
== NULL
)
586 tps6586x
->client
= client
;
587 tps6586x
->dev
= &client
->dev
;
588 i2c_set_clientdata(client
, tps6586x
);
590 mutex_init(&tps6586x
->lock
);
593 ret
= tps6586x_irq_init(tps6586x
, client
->irq
,
596 dev_err(&client
->dev
, "IRQ init failed: %d\n", ret
);
601 ret
= tps6586x_gpio_init(tps6586x
, pdata
->gpio_base
);
603 dev_err(&client
->dev
, "GPIO registration failed: %d\n", ret
);
607 ret
= tps6586x_add_subdevs(tps6586x
, pdata
);
609 dev_err(&client
->dev
, "add devices failed: %d\n", ret
);
616 if (pdata
->gpio_base
) {
617 ret
= gpiochip_remove(&tps6586x
->gpio
);
619 dev_err(&client
->dev
, "Can't remove gpio chip: %d\n",
624 free_irq(client
->irq
, tps6586x
);
630 static int __devexit
tps6586x_i2c_remove(struct i2c_client
*client
)
632 struct tps6586x
*tps6586x
= i2c_get_clientdata(client
);
633 struct tps6586x_platform_data
*pdata
= client
->dev
.platform_data
;
637 free_irq(client
->irq
, tps6586x
);
639 if (pdata
->gpio_base
) {
640 ret
= gpiochip_remove(&tps6586x
->gpio
);
642 dev_err(&client
->dev
, "Can't remove gpio chip: %d\n",
646 tps6586x_remove_subdevs(tps6586x
);
651 static const struct i2c_device_id tps6586x_id_table
[] = {
655 MODULE_DEVICE_TABLE(i2c
, tps6586x_id_table
);
657 static struct i2c_driver tps6586x_driver
= {
660 .owner
= THIS_MODULE
,
661 .of_match_table
= of_match_ptr(tps6586x_of_match
),
663 .probe
= tps6586x_i2c_probe
,
664 .remove
= __devexit_p(tps6586x_i2c_remove
),
665 .id_table
= tps6586x_id_table
,
668 static int __init
tps6586x_init(void)
670 return i2c_add_driver(&tps6586x_driver
);
672 subsys_initcall(tps6586x_init
);
674 static void __exit
tps6586x_exit(void)
676 i2c_del_driver(&tps6586x_driver
);
678 module_exit(tps6586x_exit
);
680 MODULE_DESCRIPTION("TPS6586X core driver");
681 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
682 MODULE_LICENSE("GPL");