2 * Regulator driver for TPS6524x PMIC
4 * Copyright (C) 2010 Texas Instruments
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
11 * whether express or implied; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/spi/spi.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
25 #define REG_LDO_SET 0x0
26 #define LDO_ILIM_MASK 1 /* 0 = 400-800, 1 = 900-1500 */
27 #define LDO_VSEL_MASK 0x0f
28 #define LDO2_ILIM_SHIFT 12
29 #define LDO2_VSEL_SHIFT 4
30 #define LDO1_ILIM_SHIFT 8
31 #define LDO1_VSEL_SHIFT 0
33 #define REG_BLOCK_EN 0x1
35 #define BLOCK_LDO1_SHIFT 0
36 #define BLOCK_LDO2_SHIFT 1
37 #define BLOCK_LCD_SHIFT 2
38 #define BLOCK_USB_SHIFT 3
40 #define REG_DCDC_SET 0x2
41 #define DCDC_VDCDC_MASK 0x1f
42 #define DCDC_VDCDC1_SHIFT 0
43 #define DCDC_VDCDC2_SHIFT 5
44 #define DCDC_VDCDC3_SHIFT 10
46 #define REG_DCDC_EN 0x3
47 #define DCDCDCDC_EN_MASK 0x1
48 #define DCDCDCDC1_EN_SHIFT 0
49 #define DCDCDCDC1_PG_MSK BIT(1)
50 #define DCDCDCDC2_EN_SHIFT 2
51 #define DCDCDCDC2_PG_MSK BIT(3)
52 #define DCDCDCDC3_EN_SHIFT 4
53 #define DCDCDCDC3_PG_MSK BIT(5)
56 #define USB_ILIM_SHIFT 0
57 #define USB_ILIM_MASK 0x3
58 #define USB_TSD_SHIFT 2
59 #define USB_TSD_MASK 0x3
60 #define USB_TWARN_SHIFT 4
61 #define USB_TWARN_MASK 0x3
62 #define USB_IWARN_SD BIT(6)
63 #define USB_FAST_LOOP BIT(7)
66 #define ALARM_LDO1 BIT(0)
67 #define ALARM_DCDC1 BIT(1)
68 #define ALARM_DCDC2 BIT(2)
69 #define ALARM_DCDC3 BIT(3)
70 #define ALARM_LDO2 BIT(4)
71 #define ALARM_USB_WARN BIT(5)
72 #define ALARM_USB_ALARM BIT(6)
73 #define ALARM_LCD BIT(9)
74 #define ALARM_TEMP_WARM BIT(10)
75 #define ALARM_TEMP_HOT BIT(11)
76 #define ALARM_NRST BIT(14)
77 #define ALARM_POWERUP BIT(15)
79 #define REG_INT_ENABLE 0x6
80 #define INT_LDO1 BIT(0)
81 #define INT_DCDC1 BIT(1)
82 #define INT_DCDC2 BIT(2)
83 #define INT_DCDC3 BIT(3)
84 #define INT_LDO2 BIT(4)
85 #define INT_USB_WARN BIT(5)
86 #define INT_USB_ALARM BIT(6)
87 #define INT_LCD BIT(9)
88 #define INT_TEMP_WARM BIT(10)
89 #define INT_TEMP_HOT BIT(11)
90 #define INT_GLOBAL_EN BIT(15)
92 #define REG_INT_STATUS 0x7
93 #define STATUS_LDO1 BIT(0)
94 #define STATUS_DCDC1 BIT(1)
95 #define STATUS_DCDC2 BIT(2)
96 #define STATUS_DCDC3 BIT(3)
97 #define STATUS_LDO2 BIT(4)
98 #define STATUS_USB_WARN BIT(5)
99 #define STATUS_USB_ALARM BIT(6)
100 #define STATUS_LCD BIT(9)
101 #define STATUS_TEMP_WARM BIT(10)
102 #define STATUS_TEMP_HOT BIT(11)
104 #define REG_SOFTWARE_RESET 0xb
105 #define REG_WRITE_ENABLE 0xd
106 #define REG_REV_ID 0xf
111 #define N_REGULATORS (N_DCDC + N_LDO + N_SWITCH)
113 #define CMD_READ(reg) ((reg) << 6)
114 #define CMD_WRITE(reg) (BIT(5) | (reg) << 6)
115 #define STAT_CLK BIT(3)
116 #define STAT_WRITE BIT(2)
117 #define STAT_INVALID BIT(1)
118 #define STAT_WP BIT(0)
129 const unsigned int *voltages
;
131 const unsigned int *ilimsels
;
132 struct field enable
, voltage
, ilimsel
;
137 struct spi_device
*spi
;
139 struct regulator_desc desc
[N_REGULATORS
];
140 struct regulator_dev
*rdev
[N_REGULATORS
];
143 static int __read_reg(struct tps6524x
*hw
, int reg
)
146 u16 cmd
= CMD_READ(reg
), in
;
148 struct spi_message m
;
149 struct spi_transfer t
[3];
151 spi_message_init(&m
);
152 memset(t
, 0, sizeof(t
));
156 t
[0].bits_per_word
= 12;
157 spi_message_add_tail(&t
[0], &m
);
161 t
[1].bits_per_word
= 16;
162 spi_message_add_tail(&t
[1], &m
);
164 t
[2].rx_buf
= &status
;
166 t
[2].bits_per_word
= 4;
167 spi_message_add_tail(&t
[2], &m
);
169 error
= spi_sync(hw
->spi
, &m
);
173 dev_dbg(hw
->dev
, "read reg %d, data %x, status %x\n",
176 if (!(status
& STAT_CLK
) || (status
& STAT_WRITE
))
179 if (status
& STAT_INVALID
)
185 static int read_reg(struct tps6524x
*hw
, int reg
)
189 mutex_lock(&hw
->lock
);
190 ret
= __read_reg(hw
, reg
);
191 mutex_unlock(&hw
->lock
);
196 static int __write_reg(struct tps6524x
*hw
, int reg
, int val
)
199 u16 cmd
= CMD_WRITE(reg
), out
= val
;
201 struct spi_message m
;
202 struct spi_transfer t
[3];
204 spi_message_init(&m
);
205 memset(t
, 0, sizeof(t
));
209 t
[0].bits_per_word
= 12;
210 spi_message_add_tail(&t
[0], &m
);
214 t
[1].bits_per_word
= 16;
215 spi_message_add_tail(&t
[1], &m
);
217 t
[2].rx_buf
= &status
;
219 t
[2].bits_per_word
= 4;
220 spi_message_add_tail(&t
[2], &m
);
222 error
= spi_sync(hw
->spi
, &m
);
226 dev_dbg(hw
->dev
, "wrote reg %d, data %x, status %x\n",
229 if (!(status
& STAT_CLK
) || !(status
& STAT_WRITE
))
232 if (status
& (STAT_INVALID
| STAT_WP
))
238 static int __rmw_reg(struct tps6524x
*hw
, int reg
, int mask
, int val
)
242 ret
= __read_reg(hw
, reg
);
249 ret
= __write_reg(hw
, reg
, ret
);
251 return (ret
< 0) ? ret
: 0;
254 static int rmw_protect(struct tps6524x
*hw
, int reg
, int mask
, int val
)
258 mutex_lock(&hw
->lock
);
260 ret
= __write_reg(hw
, REG_WRITE_ENABLE
, 1);
262 dev_err(hw
->dev
, "failed to set write enable\n");
266 ret
= __rmw_reg(hw
, reg
, mask
, val
);
268 dev_err(hw
->dev
, "failed to rmw register %d\n", reg
);
270 ret
= __write_reg(hw
, REG_WRITE_ENABLE
, 0);
272 dev_err(hw
->dev
, "failed to clear write enable\n");
277 mutex_unlock(&hw
->lock
);
282 static int read_field(struct tps6524x
*hw
, const struct field
*field
)
286 tmp
= read_reg(hw
, field
->reg
);
290 return (tmp
>> field
->shift
) & field
->mask
;
293 static int write_field(struct tps6524x
*hw
, const struct field
*field
,
296 if (val
& ~field
->mask
)
299 return rmw_protect(hw
, field
->reg
,
300 field
->mask
<< field
->shift
,
301 val
<< field
->shift
);
304 static const unsigned int dcdc1_voltages
[] = {
305 800000, 825000, 850000, 875000,
306 900000, 925000, 950000, 975000,
307 1000000, 1025000, 1050000, 1075000,
308 1100000, 1125000, 1150000, 1175000,
309 1200000, 1225000, 1250000, 1275000,
310 1300000, 1325000, 1350000, 1375000,
311 1400000, 1425000, 1450000, 1475000,
312 1500000, 1525000, 1550000, 1575000,
315 static const unsigned int dcdc2_voltages
[] = {
316 1400000, 1450000, 1500000, 1550000,
317 1600000, 1650000, 1700000, 1750000,
318 1800000, 1850000, 1900000, 1950000,
319 2000000, 2050000, 2100000, 2150000,
320 2200000, 2250000, 2300000, 2350000,
321 2400000, 2450000, 2500000, 2550000,
322 2600000, 2650000, 2700000, 2750000,
323 2800000, 2850000, 2900000, 2950000,
326 static const unsigned int dcdc3_voltages
[] = {
327 2400000, 2450000, 2500000, 2550000, 2600000,
328 2650000, 2700000, 2750000, 2800000, 2850000,
329 2900000, 2950000, 3000000, 3050000, 3100000,
330 3150000, 3200000, 3250000, 3300000, 3350000,
331 3400000, 3450000, 3500000, 3550000, 3600000,
334 static const unsigned int ldo1_voltages
[] = {
335 4300000, 4350000, 4400000, 4450000,
336 4500000, 4550000, 4600000, 4650000,
337 4700000, 4750000, 4800000, 4850000,
338 4900000, 4950000, 5000000, 5050000,
341 static const unsigned int ldo2_voltages
[] = {
342 1100000, 1150000, 1200000, 1250000,
343 1300000, 1700000, 1750000, 1800000,
344 1850000, 1900000, 3150000, 3200000,
345 3250000, 3300000, 3350000, 3400000,
348 static const unsigned int fixed_5000000_voltage
[] = {
352 static const unsigned int ldo_ilimsel
[] = {
356 static const unsigned int usb_ilimsel
[] = {
357 200000, 400000, 800000, 1000000
360 static const unsigned int fixed_2400000_ilimsel
[] = {
364 static const unsigned int fixed_1200000_ilimsel
[] = {
368 static const unsigned int fixed_400000_ilimsel
[] = {
372 #define __MK_FIELD(_reg, _mask, _shift) \
373 { .reg = (_reg), .mask = (_mask), .shift = (_shift), }
375 static const struct supply_info supply_info
[N_REGULATORS
] = {
378 .n_voltages
= ARRAY_SIZE(dcdc1_voltages
),
379 .voltages
= dcdc1_voltages
,
380 .n_ilimsels
= ARRAY_SIZE(fixed_2400000_ilimsel
),
381 .ilimsels
= fixed_2400000_ilimsel
,
382 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
384 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
389 .n_voltages
= ARRAY_SIZE(dcdc2_voltages
),
390 .voltages
= dcdc2_voltages
,
391 .n_ilimsels
= ARRAY_SIZE(fixed_1200000_ilimsel
),
392 .ilimsels
= fixed_1200000_ilimsel
,
393 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
395 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
400 .n_voltages
= ARRAY_SIZE(dcdc3_voltages
),
401 .voltages
= dcdc3_voltages
,
402 .n_ilimsels
= ARRAY_SIZE(fixed_1200000_ilimsel
),
403 .ilimsels
= fixed_1200000_ilimsel
,
404 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
406 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
411 .n_voltages
= ARRAY_SIZE(ldo1_voltages
),
412 .voltages
= ldo1_voltages
,
413 .n_ilimsels
= ARRAY_SIZE(ldo_ilimsel
),
414 .ilimsels
= ldo_ilimsel
,
415 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
417 .voltage
= __MK_FIELD(REG_LDO_SET
, LDO_VSEL_MASK
,
419 .ilimsel
= __MK_FIELD(REG_LDO_SET
, LDO_ILIM_MASK
,
424 .n_voltages
= ARRAY_SIZE(ldo2_voltages
),
425 .voltages
= ldo2_voltages
,
426 .n_ilimsels
= ARRAY_SIZE(ldo_ilimsel
),
427 .ilimsels
= ldo_ilimsel
,
428 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
430 .voltage
= __MK_FIELD(REG_LDO_SET
, LDO_VSEL_MASK
,
432 .ilimsel
= __MK_FIELD(REG_LDO_SET
, LDO_ILIM_MASK
,
437 .n_voltages
= ARRAY_SIZE(fixed_5000000_voltage
),
438 .voltages
= fixed_5000000_voltage
,
439 .n_ilimsels
= ARRAY_SIZE(usb_ilimsel
),
440 .ilimsels
= usb_ilimsel
,
441 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
443 .ilimsel
= __MK_FIELD(REG_USB
, USB_ILIM_MASK
,
448 .n_voltages
= ARRAY_SIZE(fixed_5000000_voltage
),
449 .voltages
= fixed_5000000_voltage
,
450 .n_ilimsels
= ARRAY_SIZE(fixed_400000_ilimsel
),
451 .ilimsels
= fixed_400000_ilimsel
,
452 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
457 static int set_voltage_sel(struct regulator_dev
*rdev
, unsigned selector
)
459 const struct supply_info
*info
;
462 hw
= rdev_get_drvdata(rdev
);
463 info
= &supply_info
[rdev_get_id(rdev
)];
465 if (rdev
->desc
->n_voltages
== 1)
468 return write_field(hw
, &info
->voltage
, selector
);
471 static int get_voltage_sel(struct regulator_dev
*rdev
)
473 const struct supply_info
*info
;
477 hw
= rdev_get_drvdata(rdev
);
478 info
= &supply_info
[rdev_get_id(rdev
)];
480 if (rdev
->desc
->n_voltages
== 1)
483 ret
= read_field(hw
, &info
->voltage
);
486 if (WARN_ON(ret
>= info
->n_voltages
))
492 static int set_current_limit(struct regulator_dev
*rdev
, int min_uA
,
495 const struct supply_info
*info
;
499 hw
= rdev_get_drvdata(rdev
);
500 info
= &supply_info
[rdev_get_id(rdev
)];
502 if (info
->n_ilimsels
== 1)
505 for (i
= info
->n_ilimsels
- 1; i
>= 0; i
--) {
506 if (min_uA
<= info
->ilimsels
[i
] &&
507 max_uA
>= info
->ilimsels
[i
])
508 return write_field(hw
, &info
->ilimsel
, i
);
514 static int get_current_limit(struct regulator_dev
*rdev
)
516 const struct supply_info
*info
;
520 hw
= rdev_get_drvdata(rdev
);
521 info
= &supply_info
[rdev_get_id(rdev
)];
523 if (info
->n_ilimsels
== 1)
524 return info
->ilimsels
[0];
526 ret
= read_field(hw
, &info
->ilimsel
);
529 if (WARN_ON(ret
>= info
->n_ilimsels
))
532 return info
->ilimsels
[ret
];
535 static int enable_supply(struct regulator_dev
*rdev
)
537 const struct supply_info
*info
;
540 hw
= rdev_get_drvdata(rdev
);
541 info
= &supply_info
[rdev_get_id(rdev
)];
543 return write_field(hw
, &info
->enable
, 1);
546 static int disable_supply(struct regulator_dev
*rdev
)
548 const struct supply_info
*info
;
551 hw
= rdev_get_drvdata(rdev
);
552 info
= &supply_info
[rdev_get_id(rdev
)];
554 return write_field(hw
, &info
->enable
, 0);
557 static int is_supply_enabled(struct regulator_dev
*rdev
)
559 const struct supply_info
*info
;
562 hw
= rdev_get_drvdata(rdev
);
563 info
= &supply_info
[rdev_get_id(rdev
)];
565 return read_field(hw
, &info
->enable
);
568 static struct regulator_ops regulator_ops
= {
569 .is_enabled
= is_supply_enabled
,
570 .enable
= enable_supply
,
571 .disable
= disable_supply
,
572 .get_voltage_sel
= get_voltage_sel
,
573 .set_voltage_sel
= set_voltage_sel
,
574 .list_voltage
= regulator_list_voltage_table
,
575 .set_current_limit
= set_current_limit
,
576 .get_current_limit
= get_current_limit
,
579 static int pmic_remove(struct spi_device
*spi
)
581 struct tps6524x
*hw
= spi_get_drvdata(spi
);
586 for (i
= 0; i
< N_REGULATORS
; i
++) {
588 regulator_unregister(hw
->rdev
[i
]);
591 spi_set_drvdata(spi
, NULL
);
595 static int pmic_probe(struct spi_device
*spi
)
598 struct device
*dev
= &spi
->dev
;
599 const struct supply_info
*info
= supply_info
;
600 struct regulator_init_data
*init_data
;
601 struct regulator_config config
= { };
604 init_data
= dev
->platform_data
;
606 dev_err(dev
, "could not find regulator platform data\n");
610 hw
= devm_kzalloc(&spi
->dev
, sizeof(struct tps6524x
), GFP_KERNEL
);
612 dev_err(dev
, "cannot allocate regulator private data\n");
615 spi_set_drvdata(spi
, hw
);
617 memset(hw
, 0, sizeof(struct tps6524x
));
619 hw
->spi
= spi_dev_get(spi
);
620 mutex_init(&hw
->lock
);
622 for (i
= 0; i
< N_REGULATORS
; i
++, info
++, init_data
++) {
623 hw
->desc
[i
].name
= info
->name
;
625 hw
->desc
[i
].n_voltages
= info
->n_voltages
;
626 hw
->desc
[i
].volt_table
= info
->voltages
;
627 hw
->desc
[i
].ops
= ®ulator_ops
;
628 hw
->desc
[i
].type
= REGULATOR_VOLTAGE
;
629 hw
->desc
[i
].owner
= THIS_MODULE
;
632 config
.init_data
= init_data
;
633 config
.driver_data
= hw
;
635 hw
->rdev
[i
] = regulator_register(&hw
->desc
[i
], &config
);
636 if (IS_ERR(hw
->rdev
[i
])) {
637 ret
= PTR_ERR(hw
->rdev
[i
]);
650 static struct spi_driver pmic_driver
= {
652 .remove
= pmic_remove
,
655 .owner
= THIS_MODULE
,
659 module_spi_driver(pmic_driver
);
661 MODULE_DESCRIPTION("TPS6524X PMIC Driver");
662 MODULE_AUTHOR("Cyril Chemparathy");
663 MODULE_LICENSE("GPL");
664 MODULE_ALIAS("spi:tps6524x");