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 (3 /* DCDC */ + \
115 #define FIXED_ILIMSEL BIT(0)
116 #define FIXED_VOLTAGE BIT(1)
118 #define CMD_READ(reg) ((reg) << 6)
119 #define CMD_WRITE(reg) (BIT(5) | (reg) << 6)
120 #define STAT_CLK BIT(3)
121 #define STAT_WRITE BIT(2)
122 #define STAT_INVALID BIT(1)
123 #define STAT_WP BIT(0)
140 struct field enable
, voltage
, ilimsel
;
145 struct spi_device
*spi
;
147 struct regulator_desc desc
[N_REGULATORS
];
148 struct regulator_dev
*rdev
[N_REGULATORS
];
151 static int __read_reg(struct tps6524x
*hw
, int reg
)
154 u16 cmd
= CMD_READ(reg
), in
;
156 struct spi_message m
;
157 struct spi_transfer t
[3];
159 spi_message_init(&m
);
160 memset(t
, 0, sizeof(t
));
164 t
[0].bits_per_word
= 12;
165 spi_message_add_tail(&t
[0], &m
);
169 t
[1].bits_per_word
= 16;
170 spi_message_add_tail(&t
[1], &m
);
172 t
[2].rx_buf
= &status
;
174 t
[2].bits_per_word
= 4;
175 spi_message_add_tail(&t
[2], &m
);
177 error
= spi_sync(hw
->spi
, &m
);
181 dev_dbg(hw
->dev
, "read reg %d, data %x, status %x\n",
184 if (!(status
& STAT_CLK
) || (status
& STAT_WRITE
))
187 if (status
& STAT_INVALID
)
193 static int read_reg(struct tps6524x
*hw
, int reg
)
197 mutex_lock(&hw
->lock
);
198 ret
= __read_reg(hw
, reg
);
199 mutex_unlock(&hw
->lock
);
204 static int __write_reg(struct tps6524x
*hw
, int reg
, int val
)
207 u16 cmd
= CMD_WRITE(reg
), out
= val
;
209 struct spi_message m
;
210 struct spi_transfer t
[3];
212 spi_message_init(&m
);
213 memset(t
, 0, sizeof(t
));
217 t
[0].bits_per_word
= 12;
218 spi_message_add_tail(&t
[0], &m
);
222 t
[1].bits_per_word
= 16;
223 spi_message_add_tail(&t
[1], &m
);
225 t
[2].rx_buf
= &status
;
227 t
[2].bits_per_word
= 4;
228 spi_message_add_tail(&t
[2], &m
);
230 error
= spi_sync(hw
->spi
, &m
);
234 dev_dbg(hw
->dev
, "wrote reg %d, data %x, status %x\n",
237 if (!(status
& STAT_CLK
) || !(status
& STAT_WRITE
))
240 if (status
& (STAT_INVALID
| STAT_WP
))
246 static int __rmw_reg(struct tps6524x
*hw
, int reg
, int mask
, int val
)
250 ret
= __read_reg(hw
, reg
);
257 ret
= __write_reg(hw
, reg
, ret
);
259 return (ret
< 0) ? ret
: 0;
262 static int rmw_protect(struct tps6524x
*hw
, int reg
, int mask
, int val
)
266 mutex_lock(&hw
->lock
);
268 ret
= __write_reg(hw
, REG_WRITE_ENABLE
, 1);
270 dev_err(hw
->dev
, "failed to set write enable\n");
274 ret
= __rmw_reg(hw
, reg
, mask
, val
);
276 dev_err(hw
->dev
, "failed to rmw register %d\n", reg
);
278 ret
= __write_reg(hw
, REG_WRITE_ENABLE
, 0);
280 dev_err(hw
->dev
, "failed to clear write enable\n");
285 mutex_unlock(&hw
->lock
);
290 static int read_field(struct tps6524x
*hw
, const struct field
*field
)
294 tmp
= read_reg(hw
, field
->reg
);
298 return (tmp
>> field
->shift
) & field
->mask
;
301 static int write_field(struct tps6524x
*hw
, const struct field
*field
,
304 if (val
& ~field
->mask
)
307 return rmw_protect(hw
, field
->reg
,
308 field
->mask
<< field
->shift
,
309 val
<< field
->shift
);
312 static const int dcdc1_voltages
[] = {
313 800000, 825000, 850000, 875000,
314 900000, 925000, 950000, 975000,
315 1000000, 1025000, 1050000, 1075000,
316 1100000, 1125000, 1150000, 1175000,
317 1200000, 1225000, 1250000, 1275000,
318 1300000, 1325000, 1350000, 1375000,
319 1400000, 1425000, 1450000, 1475000,
320 1500000, 1525000, 1550000, 1575000,
323 static const int dcdc2_voltages
[] = {
324 1400000, 1450000, 1500000, 1550000,
325 1600000, 1650000, 1700000, 1750000,
326 1800000, 1850000, 1900000, 1950000,
327 2000000, 2050000, 2100000, 2150000,
328 2200000, 2250000, 2300000, 2350000,
329 2400000, 2450000, 2500000, 2550000,
330 2600000, 2650000, 2700000, 2750000,
331 2800000, 2850000, 2900000, 2950000,
334 static const int dcdc3_voltages
[] = {
335 2400000, 2450000, 2500000, 2550000, 2600000,
336 2650000, 2700000, 2750000, 2800000, 2850000,
337 2900000, 2950000, 3000000, 3050000, 3100000,
338 3150000, 3200000, 3250000, 3300000, 3350000,
339 3400000, 3450000, 3500000, 3550000, 3600000,
342 static const int ldo1_voltages
[] = {
343 4300000, 4350000, 4400000, 4450000,
344 4500000, 4550000, 4600000, 4650000,
345 4700000, 4750000, 4800000, 4850000,
346 4900000, 4950000, 5000000, 5050000,
349 static const int ldo2_voltages
[] = {
350 1100000, 1150000, 1200000, 1250000,
351 1300000, 1700000, 1750000, 1800000,
352 1850000, 1900000, 3150000, 3200000,
353 3250000, 3300000, 3350000, 3400000,
356 static const int ldo_ilimsel
[] = {
360 static const int usb_ilimsel
[] = {
361 200000, 400000, 800000, 1000000
364 #define __MK_FIELD(_reg, _mask, _shift) \
365 { .reg = (_reg), .mask = (_mask), .shift = (_shift), }
367 static const struct supply_info supply_info
[N_REGULATORS
] = {
370 .flags
= FIXED_ILIMSEL
,
371 .n_voltages
= ARRAY_SIZE(dcdc1_voltages
),
372 .voltages
= dcdc1_voltages
,
373 .fixed_ilimsel
= 2400000,
374 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
376 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
381 .flags
= FIXED_ILIMSEL
,
382 .n_voltages
= ARRAY_SIZE(dcdc2_voltages
),
383 .voltages
= dcdc2_voltages
,
384 .fixed_ilimsel
= 1200000,
385 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
387 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
392 .flags
= FIXED_ILIMSEL
,
393 .n_voltages
= ARRAY_SIZE(dcdc3_voltages
),
394 .voltages
= dcdc3_voltages
,
395 .fixed_ilimsel
= 1200000,
396 .enable
= __MK_FIELD(REG_DCDC_EN
, DCDCDCDC_EN_MASK
,
398 .voltage
= __MK_FIELD(REG_DCDC_SET
, DCDC_VDCDC_MASK
,
403 .n_voltages
= ARRAY_SIZE(ldo1_voltages
),
404 .voltages
= ldo1_voltages
,
405 .n_ilimsels
= ARRAY_SIZE(ldo_ilimsel
),
406 .ilimsels
= ldo_ilimsel
,
407 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
409 .voltage
= __MK_FIELD(REG_LDO_SET
, LDO_VSEL_MASK
,
411 .ilimsel
= __MK_FIELD(REG_LDO_SET
, LDO_ILIM_MASK
,
416 .n_voltages
= ARRAY_SIZE(ldo2_voltages
),
417 .voltages
= ldo2_voltages
,
418 .n_ilimsels
= ARRAY_SIZE(ldo_ilimsel
),
419 .ilimsels
= ldo_ilimsel
,
420 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
422 .voltage
= __MK_FIELD(REG_LDO_SET
, LDO_VSEL_MASK
,
424 .ilimsel
= __MK_FIELD(REG_LDO_SET
, LDO_ILIM_MASK
,
429 .flags
= FIXED_VOLTAGE
,
430 .fixed_voltage
= 5000000,
431 .n_ilimsels
= ARRAY_SIZE(usb_ilimsel
),
432 .ilimsels
= usb_ilimsel
,
433 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
435 .ilimsel
= __MK_FIELD(REG_USB
, USB_ILIM_MASK
,
440 .flags
= FIXED_VOLTAGE
| FIXED_ILIMSEL
,
441 .fixed_voltage
= 5000000,
442 .fixed_ilimsel
= 400000,
443 .enable
= __MK_FIELD(REG_BLOCK_EN
, BLOCK_MASK
,
448 static int list_voltage(struct regulator_dev
*rdev
, unsigned selector
)
450 const struct supply_info
*info
;
453 hw
= rdev_get_drvdata(rdev
);
454 info
= &supply_info
[rdev_get_id(rdev
)];
456 if (info
->flags
& FIXED_VOLTAGE
)
457 return selector
? -EINVAL
: info
->fixed_voltage
;
459 return ((selector
< info
->n_voltages
) ?
460 info
->voltages
[selector
] : -EINVAL
);
463 static int set_voltage(struct regulator_dev
*rdev
, int min_uV
, int max_uV
,
466 const struct supply_info
*info
;
470 hw
= rdev_get_drvdata(rdev
);
471 info
= &supply_info
[rdev_get_id(rdev
)];
473 if (info
->flags
& FIXED_VOLTAGE
)
476 for (i
= 0; i
< info
->n_voltages
; i
++)
477 if (min_uV
<= info
->voltages
[i
] &&
478 max_uV
>= info
->voltages
[i
])
481 if (i
>= info
->n_voltages
)
482 i
= info
->n_voltages
- 1;
484 *selector
= info
->voltages
[i
];
486 return write_field(hw
, &info
->voltage
, i
);
489 static int get_voltage(struct regulator_dev
*rdev
)
491 const struct supply_info
*info
;
495 hw
= rdev_get_drvdata(rdev
);
496 info
= &supply_info
[rdev_get_id(rdev
)];
498 if (info
->flags
& FIXED_VOLTAGE
)
499 return info
->fixed_voltage
;
501 ret
= read_field(hw
, &info
->voltage
);
504 if (WARN_ON(ret
>= info
->n_voltages
))
507 return info
->voltages
[ret
];
510 static int set_current_limit(struct regulator_dev
*rdev
, int min_uA
,
513 const struct supply_info
*info
;
517 hw
= rdev_get_drvdata(rdev
);
518 info
= &supply_info
[rdev_get_id(rdev
)];
520 if (info
->flags
& FIXED_ILIMSEL
)
523 for (i
= 0; i
< info
->n_ilimsels
; i
++)
524 if (min_uA
<= info
->ilimsels
[i
] &&
525 max_uA
>= info
->ilimsels
[i
])
528 if (i
>= info
->n_ilimsels
)
531 return write_field(hw
, &info
->ilimsel
, i
);
534 static int get_current_limit(struct regulator_dev
*rdev
)
536 const struct supply_info
*info
;
540 hw
= rdev_get_drvdata(rdev
);
541 info
= &supply_info
[rdev_get_id(rdev
)];
543 if (info
->flags
& FIXED_ILIMSEL
)
544 return info
->fixed_ilimsel
;
546 ret
= read_field(hw
, &info
->ilimsel
);
549 if (WARN_ON(ret
>= info
->n_ilimsels
))
552 return info
->ilimsels
[ret
];
555 static int enable_supply(struct regulator_dev
*rdev
)
557 const struct supply_info
*info
;
560 hw
= rdev_get_drvdata(rdev
);
561 info
= &supply_info
[rdev_get_id(rdev
)];
563 return write_field(hw
, &info
->enable
, 1);
566 static int disable_supply(struct regulator_dev
*rdev
)
568 const struct supply_info
*info
;
571 hw
= rdev_get_drvdata(rdev
);
572 info
= &supply_info
[rdev_get_id(rdev
)];
574 return write_field(hw
, &info
->enable
, 0);
577 static int is_supply_enabled(struct regulator_dev
*rdev
)
579 const struct supply_info
*info
;
582 hw
= rdev_get_drvdata(rdev
);
583 info
= &supply_info
[rdev_get_id(rdev
)];
585 return read_field(hw
, &info
->enable
);
588 static struct regulator_ops regulator_ops
= {
589 .is_enabled
= is_supply_enabled
,
590 .enable
= enable_supply
,
591 .disable
= disable_supply
,
592 .get_voltage
= get_voltage
,
593 .set_voltage
= set_voltage
,
594 .list_voltage
= list_voltage
,
595 .set_current_limit
= set_current_limit
,
596 .get_current_limit
= get_current_limit
,
599 static int __devexit
pmic_remove(struct spi_device
*spi
)
601 struct tps6524x
*hw
= spi_get_drvdata(spi
);
606 for (i
= 0; i
< N_REGULATORS
; i
++) {
608 regulator_unregister(hw
->rdev
[i
]);
611 spi_set_drvdata(spi
, NULL
);
616 static int __devinit
pmic_probe(struct spi_device
*spi
)
619 struct device
*dev
= &spi
->dev
;
620 const struct supply_info
*info
= supply_info
;
621 struct regulator_init_data
*init_data
;
624 init_data
= dev
->platform_data
;
626 dev_err(dev
, "could not find regulator platform data\n");
630 hw
= kzalloc(sizeof(struct tps6524x
), GFP_KERNEL
);
632 dev_err(dev
, "cannot allocate regulator private data\n");
635 spi_set_drvdata(spi
, hw
);
637 memset(hw
, 0, sizeof(struct tps6524x
));
639 hw
->spi
= spi_dev_get(spi
);
640 mutex_init(&hw
->lock
);
642 for (i
= 0; i
< N_REGULATORS
; i
++, info
++, init_data
++) {
643 hw
->desc
[i
].name
= info
->name
;
645 hw
->desc
[i
].n_voltages
= info
->n_voltages
;
646 hw
->desc
[i
].ops
= ®ulator_ops
;
647 hw
->desc
[i
].type
= REGULATOR_VOLTAGE
;
648 hw
->desc
[i
].owner
= THIS_MODULE
;
650 if (info
->flags
& FIXED_VOLTAGE
)
651 hw
->desc
[i
].n_voltages
= 1;
653 hw
->rdev
[i
] = regulator_register(&hw
->desc
[i
], dev
,
655 if (IS_ERR(hw
->rdev
[i
])) {
656 ret
= PTR_ERR(hw
->rdev
[i
]);
669 static struct spi_driver pmic_driver
= {
671 .remove
= __devexit_p(pmic_remove
),
674 .owner
= THIS_MODULE
,
678 static int __init
pmic_driver_init(void)
680 return spi_register_driver(&pmic_driver
);
682 module_init(pmic_driver_init
);
684 static void __exit
pmic_driver_exit(void)
686 spi_unregister_driver(&pmic_driver
);
688 module_exit(pmic_driver_exit
);
690 MODULE_DESCRIPTION("TPS6524X PMIC Driver");
691 MODULE_AUTHOR("Cyril Chemparathy");
692 MODULE_LICENSE("GPL");
693 MODULE_ALIAS("spi:tps6524x");