2 * tps65912.c -- TI tps65912
4 * Copyright 2011 Texas Instruments Inc.
6 * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This driver is based on wm8350 implementation.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/tps65912.h>
29 #define TPS65912_REG_DCDC1 0
30 #define TPS65912_REG_DCDC2 1
31 #define TPS65912_REG_DCDC3 2
32 #define TPS65912_REG_DCDC4 3
35 #define TPS65912_REG_LDO1 4
36 #define TPS65912_REG_LDO2 5
37 #define TPS65912_REG_LDO3 6
38 #define TPS65912_REG_LDO4 7
39 #define TPS65912_REG_LDO5 8
40 #define TPS65912_REG_LDO6 9
41 #define TPS65912_REG_LDO7 10
42 #define TPS65912_REG_LDO8 11
43 #define TPS65912_REG_LDO9 12
44 #define TPS65912_REG_LDO10 13
46 /* Number of step-down converters available */
47 #define TPS65912_NUM_DCDC 4
49 /* Number of LDO voltage regulators available */
50 #define TPS65912_NUM_LDO 10
52 /* Number of total regulators available */
53 #define TPS65912_NUM_REGULATOR (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
55 #define TPS65912_REG_ENABLED 0x80
56 #define OP_SELREG_MASK 0x40
57 #define OP_SELREG_SHIFT 6
63 static struct tps_info tps65912_regs
[] = {
108 struct tps65912_reg
{
109 struct regulator_desc desc
[TPS65912_NUM_REGULATOR
];
110 struct tps65912
*mfd
;
111 struct regulator_dev
*rdev
[TPS65912_NUM_REGULATOR
];
112 struct tps_info
*info
[TPS65912_NUM_REGULATOR
];
113 /* for read/write access */
114 struct mutex io_lock
;
116 int (*get_ctrl_reg
)(int);
117 int dcdc_range
[TPS65912_NUM_DCDC
];
122 static int tps65912_get_range(struct tps65912_reg
*pmic
, int id
)
124 struct tps65912
*mfd
= pmic
->mfd
;
128 case TPS65912_REG_DCDC1
:
129 range
= tps65912_reg_read(mfd
, TPS65912_DCDC1_LIMIT
);
131 case TPS65912_REG_DCDC2
:
132 range
= tps65912_reg_read(mfd
, TPS65912_DCDC2_LIMIT
);
134 case TPS65912_REG_DCDC3
:
135 range
= tps65912_reg_read(mfd
, TPS65912_DCDC3_LIMIT
);
137 case TPS65912_REG_DCDC4
:
138 range
= tps65912_reg_read(mfd
, TPS65912_DCDC4_LIMIT
);
145 range
= (range
& DCDC_LIMIT_RANGE_MASK
)
146 >> DCDC_LIMIT_RANGE_SHIFT
;
148 pmic
->dcdc_range
[id
] = range
;
152 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel
)
156 uv
= ((vsel
* 12500) + 500000);
160 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel
)
164 uv
= ((vsel
* 12500) + 700000);
168 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel
)
172 uv
= ((vsel
* 25000) + 500000);
176 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel
)
183 uv
= ((vsel
* 50000) + 500000);
188 static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel
)
190 unsigned long uv
= 0;
193 uv
= ((vsel
* 25000) + 800000);
194 else if (vsel
> 32 && vsel
<= 60)
195 uv
= (((vsel
- 32) * 50000) + 1600000);
197 uv
= (((vsel
- 60) * 100000) + 3000000);
202 static int tps65912_get_ctrl_register(int id
)
204 if (id
>= TPS65912_REG_DCDC1
&& id
<= TPS65912_REG_LDO4
)
205 return id
* 3 + TPS65912_DCDC1_AVS
;
206 else if (id
>= TPS65912_REG_LDO5
&& id
<= TPS65912_REG_LDO10
)
207 return id
- TPS65912_REG_LDO5
+ TPS65912_LDO5
;
212 static int tps65912_get_sel_register(struct tps65912_reg
*pmic
, int id
)
214 struct tps65912
*mfd
= pmic
->mfd
;
218 if (id
>= TPS65912_REG_DCDC1
&& id
<= TPS65912_REG_LDO4
) {
219 opvsel
= tps65912_reg_read(mfd
, id
* 3 + TPS65912_DCDC1_OP
);
220 if (opvsel
& OP_SELREG_MASK
)
221 reg
= id
* 3 + TPS65912_DCDC1_AVS
;
223 reg
= id
* 3 + TPS65912_DCDC1_OP
;
224 } else if (id
>= TPS65912_REG_LDO5
&& id
<= TPS65912_REG_LDO10
) {
225 reg
= id
- TPS65912_REG_LDO5
+ TPS65912_LDO5
;
233 static int tps65912_get_mode_regiters(struct tps65912_reg
*pmic
, int id
)
236 case TPS65912_REG_DCDC1
:
237 pmic
->pwm_mode_reg
= TPS65912_DCDC1_CTRL
;
238 pmic
->eco_reg
= TPS65912_DCDC1_AVS
;
240 case TPS65912_REG_DCDC2
:
241 pmic
->pwm_mode_reg
= TPS65912_DCDC2_CTRL
;
242 pmic
->eco_reg
= TPS65912_DCDC2_AVS
;
244 case TPS65912_REG_DCDC3
:
245 pmic
->pwm_mode_reg
= TPS65912_DCDC3_CTRL
;
246 pmic
->eco_reg
= TPS65912_DCDC3_AVS
;
248 case TPS65912_REG_DCDC4
:
249 pmic
->pwm_mode_reg
= TPS65912_DCDC4_CTRL
;
250 pmic
->eco_reg
= TPS65912_DCDC4_AVS
;
259 static int tps65912_reg_is_enabled(struct regulator_dev
*dev
)
261 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
262 struct tps65912
*mfd
= pmic
->mfd
;
263 int reg
, value
, id
= rdev_get_id(dev
);
265 if (id
< TPS65912_REG_DCDC1
|| id
> TPS65912_REG_LDO10
)
268 reg
= pmic
->get_ctrl_reg(id
);
272 value
= tps65912_reg_read(mfd
, reg
);
276 return value
& TPS65912_REG_ENABLED
;
279 static int tps65912_reg_enable(struct regulator_dev
*dev
)
281 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
282 struct tps65912
*mfd
= pmic
->mfd
;
283 int id
= rdev_get_id(dev
);
286 if (id
< TPS65912_REG_DCDC1
|| id
> TPS65912_REG_LDO10
)
289 reg
= pmic
->get_ctrl_reg(id
);
293 return tps65912_set_bits(mfd
, reg
, TPS65912_REG_ENABLED
);
296 static int tps65912_reg_disable(struct regulator_dev
*dev
)
298 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
299 struct tps65912
*mfd
= pmic
->mfd
;
300 int id
= rdev_get_id(dev
), reg
;
302 reg
= pmic
->get_ctrl_reg(id
);
306 return tps65912_clear_bits(mfd
, reg
, TPS65912_REG_ENABLED
);
309 static int tps65912_set_mode(struct regulator_dev
*dev
, unsigned int mode
)
311 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
312 struct tps65912
*mfd
= pmic
->mfd
;
313 int pwm_mode
, eco
, id
= rdev_get_id(dev
);
315 tps65912_get_mode_regiters(pmic
, id
);
317 pwm_mode
= tps65912_reg_read(mfd
, pmic
->pwm_mode_reg
);
318 eco
= tps65912_reg_read(mfd
, pmic
->eco_reg
);
320 pwm_mode
&= DCDCCTRL_DCDC_MODE_MASK
;
321 eco
&= DCDC_AVS_ECO_MASK
;
324 case REGULATOR_MODE_FAST
:
325 /* Verify if mode alredy set */
326 if (pwm_mode
&& !eco
)
328 tps65912_set_bits(mfd
, pmic
->pwm_mode_reg
, DCDCCTRL_DCDC_MODE_MASK
);
329 tps65912_clear_bits(mfd
, pmic
->eco_reg
, DCDC_AVS_ECO_MASK
);
331 case REGULATOR_MODE_NORMAL
:
332 case REGULATOR_MODE_IDLE
:
333 if (!pwm_mode
&& !eco
)
335 tps65912_clear_bits(mfd
, pmic
->pwm_mode_reg
, DCDCCTRL_DCDC_MODE_MASK
);
336 tps65912_clear_bits(mfd
, pmic
->eco_reg
, DCDC_AVS_ECO_MASK
);
338 case REGULATOR_MODE_STANDBY
:
339 if (!pwm_mode
&& eco
)
341 tps65912_clear_bits(mfd
, pmic
->pwm_mode_reg
, DCDCCTRL_DCDC_MODE_MASK
);
342 tps65912_set_bits(mfd
, pmic
->eco_reg
, DCDC_AVS_ECO_MASK
);
351 static unsigned int tps65912_get_mode(struct regulator_dev
*dev
)
353 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
354 struct tps65912
*mfd
= pmic
->mfd
;
355 int pwm_mode
, eco
, mode
= 0, id
= rdev_get_id(dev
);
357 tps65912_get_mode_regiters(pmic
, id
);
359 pwm_mode
= tps65912_reg_read(mfd
, pmic
->pwm_mode_reg
);
360 eco
= tps65912_reg_read(mfd
, pmic
->eco_reg
);
362 pwm_mode
&= DCDCCTRL_DCDC_MODE_MASK
;
363 eco
&= DCDC_AVS_ECO_MASK
;
365 if (pwm_mode
&& !eco
)
366 mode
= REGULATOR_MODE_FAST
;
367 else if (!pwm_mode
&& !eco
)
368 mode
= REGULATOR_MODE_NORMAL
;
369 else if (!pwm_mode
&& eco
)
370 mode
= REGULATOR_MODE_STANDBY
;
375 static int tps65912_list_voltage_dcdc(struct regulator_dev
*dev
,
378 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
379 int range
, voltage
= 0, id
= rdev_get_id(dev
);
381 if (id
> TPS65912_REG_DCDC4
)
384 range
= pmic
->dcdc_range
[id
];
388 /* 0.5 - 1.2875V in 12.5mV steps */
389 voltage
= tps65912_vsel_to_uv_range0(selector
);
392 /* 0.7 - 1.4875V in 12.5mV steps */
393 voltage
= tps65912_vsel_to_uv_range1(selector
);
396 /* 0.5 - 2.075V in 25mV steps */
397 voltage
= tps65912_vsel_to_uv_range2(selector
);
400 /* 0.5 - 3.8V in 50mV steps */
401 voltage
= tps65912_vsel_to_uv_range3(selector
);
407 static int tps65912_get_voltage_dcdc(struct regulator_dev
*dev
)
409 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
410 struct tps65912
*mfd
= pmic
->mfd
;
411 int id
= rdev_get_id(dev
);
414 reg
= tps65912_get_sel_register(pmic
, id
);
418 vsel
= tps65912_reg_read(mfd
, reg
);
421 return tps65912_list_voltage_dcdc(dev
, vsel
);
424 static int tps65912_set_voltage_sel(struct regulator_dev
*dev
,
427 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
428 struct tps65912
*mfd
= pmic
->mfd
;
429 int id
= rdev_get_id(dev
);
433 reg
= tps65912_get_sel_register(pmic
, id
);
434 value
= tps65912_reg_read(mfd
, reg
);
436 return tps65912_reg_write(mfd
, reg
, selector
| value
);
439 static int tps65912_get_voltage_ldo(struct regulator_dev
*dev
)
441 struct tps65912_reg
*pmic
= rdev_get_drvdata(dev
);
442 struct tps65912
*mfd
= pmic
->mfd
;
443 int id
= rdev_get_id(dev
);
447 reg
= tps65912_get_sel_register(pmic
, id
);
448 vsel
= tps65912_reg_read(mfd
, reg
);
451 return tps65912_vsel_to_uv_ldo(vsel
);
454 static int tps65912_list_voltage_ldo(struct regulator_dev
*dev
,
457 int ldo
= rdev_get_id(dev
);
459 if (ldo
< TPS65912_REG_LDO1
|| ldo
> TPS65912_REG_LDO10
)
462 return tps65912_vsel_to_uv_ldo(selector
);
465 /* Operations permitted on DCDCx */
466 static struct regulator_ops tps65912_ops_dcdc
= {
467 .is_enabled
= tps65912_reg_is_enabled
,
468 .enable
= tps65912_reg_enable
,
469 .disable
= tps65912_reg_disable
,
470 .set_mode
= tps65912_set_mode
,
471 .get_mode
= tps65912_get_mode
,
472 .get_voltage
= tps65912_get_voltage_dcdc
,
473 .set_voltage_sel
= tps65912_set_voltage_sel
,
474 .list_voltage
= tps65912_list_voltage_dcdc
,
477 /* Operations permitted on LDOx */
478 static struct regulator_ops tps65912_ops_ldo
= {
479 .is_enabled
= tps65912_reg_is_enabled
,
480 .enable
= tps65912_reg_enable
,
481 .disable
= tps65912_reg_disable
,
482 .get_voltage
= tps65912_get_voltage_ldo
,
483 .set_voltage_sel
= tps65912_set_voltage_sel
,
484 .list_voltage
= tps65912_list_voltage_ldo
,
487 static __devinit
int tps65912_probe(struct platform_device
*pdev
)
489 struct tps65912
*tps65912
= dev_get_drvdata(pdev
->dev
.parent
);
490 struct tps_info
*info
;
491 struct regulator_init_data
*reg_data
;
492 struct regulator_dev
*rdev
;
493 struct tps65912_reg
*pmic
;
494 struct tps65912_board
*pmic_plat_data
;
497 pmic_plat_data
= dev_get_platdata(tps65912
->dev
);
501 reg_data
= pmic_plat_data
->tps65912_pmic_init_data
;
503 pmic
= kzalloc(sizeof(*pmic
), GFP_KERNEL
);
507 mutex_init(&pmic
->io_lock
);
508 pmic
->mfd
= tps65912
;
509 platform_set_drvdata(pdev
, pmic
);
511 pmic
->get_ctrl_reg
= &tps65912_get_ctrl_register
;
512 info
= tps65912_regs
;
514 for (i
= 0; i
< TPS65912_NUM_REGULATOR
; i
++, info
++, reg_data
++) {
516 /* Register the regulators */
517 pmic
->info
[i
] = info
;
519 pmic
->desc
[i
].name
= info
->name
;
520 pmic
->desc
[i
].id
= i
;
521 pmic
->desc
[i
].n_voltages
= 64;
522 pmic
->desc
[i
].ops
= (i
> TPS65912_REG_DCDC4
?
523 &tps65912_ops_ldo
: &tps65912_ops_dcdc
);
524 pmic
->desc
[i
].type
= REGULATOR_VOLTAGE
;
525 pmic
->desc
[i
].owner
= THIS_MODULE
;
526 range
= tps65912_get_range(pmic
, i
);
527 rdev
= regulator_register(&pmic
->desc
[i
],
528 tps65912
->dev
, reg_data
, pmic
, NULL
);
530 dev_err(tps65912
->dev
,
531 "failed to register %s regulator\n",
537 /* Save regulator for cleanup */
538 pmic
->rdev
[i
] = rdev
;
544 regulator_unregister(pmic
->rdev
[i
]);
550 static int __devexit
tps65912_remove(struct platform_device
*pdev
)
552 struct tps65912_reg
*tps65912_reg
= platform_get_drvdata(pdev
);
555 for (i
= 0; i
< TPS65912_NUM_REGULATOR
; i
++)
556 regulator_unregister(tps65912_reg
->rdev
[i
]);
562 static struct platform_driver tps65912_driver
= {
564 .name
= "tps65912-pmic",
565 .owner
= THIS_MODULE
,
567 .probe
= tps65912_probe
,
568 .remove
= __devexit_p(tps65912_remove
),
571 static int __init
tps65912_init(void)
573 return platform_driver_register(&tps65912_driver
);
575 subsys_initcall(tps65912_init
);
577 static void __exit
tps65912_cleanup(void)
579 platform_driver_unregister(&tps65912_driver
);
581 module_exit(tps65912_cleanup
);
583 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
584 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
585 MODULE_LICENSE("GPL v2");
586 MODULE_ALIAS("platform:tps65912-pmic");