2 * twl-regulator.c -- support regulators in twl4030/twl6030 family chips
4 * Copyright (C) 2008 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
19 #include <linux/of_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/i2c/twl.h>
24 #include <linux/delay.h>
27 * The TWL4030/TW5030/TPS659x0 family chips include power management, a
28 * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
29 * include an audio codec, battery charger, and more voltage regulators.
30 * These chips are often used in OMAP-based systems.
32 * This driver implements software-based resource control for various
33 * voltage regulators. This is usually augmented with state machine
38 /* start of regulator's PM_RECEIVER control register bank */
41 /* twl resource ID, for resource control state machine */
44 /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
48 /* State REMAP default configuration */
51 /* used by regulator core */
52 struct regulator_desc desc;
54 /* chip specific features */
55 unsigned long features;
57 /* data passed from board for external get/set voltage */
62 /* LDO control registers ... offset is from the base of its register bank.
63 * The first three registers of all power resource banks help hardware to
64 * manage the various resource groups.
66 /* Common offset in TWL4030/6030 */
68 /* TWL4030 register offsets */
71 #define VREG_DEDICATED 3 /* LDO control */
72 #define VREG_VOLTAGE_SMPS_4030 9
73 /* TWL6030 register offsets */
76 #define VREG_VOLTAGE 3
77 #define VREG_VOLTAGE_SMPS 4
80 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
85 status = twl_i2c_read_u8(slave_subgp,
86 &value, info->base + offset);
87 return (status < 0) ? status : value;
91 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
94 return twl_i2c_write_u8(slave_subgp,
95 value, info->base + offset);
98 /*----------------------------------------------------------------------*/
100 /* generic power resource operations, which work on all regulators */
102 static int twlreg_grp(struct regulator_dev *rdev)
104 return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
109 * Enable/disable regulators by joining/leaving the P1 (processor) group.
110 * We assume nobody else is updating the DEV_GRP registers.
112 /* definition for 4030 family */
113 #define P3_GRP_4030 BIT(7) /* "peripherals" */
114 #define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
115 #define P1_GRP_4030 BIT(5) /* CPU/Linux */
116 /* definition for 6030 family */
117 #define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
118 #define P2_GRP_6030 BIT(1) /* "peripherals" */
119 #define P1_GRP_6030 BIT(0) /* CPU/Linux */
121 static int twl4030reg_is_enabled(struct regulator_dev *rdev)
123 int state = twlreg_grp(rdev);
128 return state & P1_GRP_4030;
131 #define PB_I2C_BUSY BIT(0)
132 #define PB_I2C_BWEN BIT(1)
134 /* Wait until buffer empty/ready to send a word on power bus. */
135 static int twl4030_wait_pb_ready(void)
143 ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
144 TWL4030_PM_MASTER_PB_CFG);
148 if (!(val & PB_I2C_BUSY))
158 /* Send a word over the powerbus */
159 static int twl4030_send_pb_msg(unsigned msg)
164 /* save powerbus configuration */
165 ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
166 TWL4030_PM_MASTER_PB_CFG);
170 /* Enable i2c access to powerbus */
171 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN,
172 TWL4030_PM_MASTER_PB_CFG);
176 ret = twl4030_wait_pb_ready();
180 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8,
181 TWL4030_PM_MASTER_PB_WORD_MSB);
185 ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff,
186 TWL4030_PM_MASTER_PB_WORD_LSB);
190 ret = twl4030_wait_pb_ready();
194 /* Restore powerbus configuration */
195 return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
196 TWL4030_PM_MASTER_PB_CFG);
199 static int twl4030reg_enable(struct regulator_dev *rdev)
201 struct twlreg_info *info = rdev_get_drvdata(rdev);
205 grp = twlreg_grp(rdev);
211 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
216 static int twl4030reg_disable(struct regulator_dev *rdev)
218 struct twlreg_info *info = rdev_get_drvdata(rdev);
222 grp = twlreg_grp(rdev);
226 grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
228 ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
233 static int twl4030reg_get_status(struct regulator_dev *rdev)
235 int state = twlreg_grp(rdev);
241 /* assume state != WARM_RESET; we'd not be running... */
243 return REGULATOR_STATUS_OFF;
244 return (state & BIT(3))
245 ? REGULATOR_STATUS_NORMAL
246 : REGULATOR_STATUS_STANDBY;
249 static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
251 struct twlreg_info *info = rdev_get_drvdata(rdev);
254 /* We can only set the mode through state machine commands... */
256 case REGULATOR_MODE_NORMAL:
257 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
259 case REGULATOR_MODE_STANDBY:
260 message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
266 return twl4030_send_pb_msg(message);
269 static inline unsigned int twl4030reg_map_mode(unsigned int mode)
272 case RES_STATE_ACTIVE:
273 return REGULATOR_MODE_NORMAL;
274 case RES_STATE_SLEEP:
275 return REGULATOR_MODE_STANDBY;
281 /*----------------------------------------------------------------------*/
284 * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
285 * select field in its control register. We use tables indexed by VSEL
286 * to record voltages in milliVolts. (Accuracy is about three percent.)
288 * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
289 * currently handled by listing two slightly different VAUX2 regulators,
290 * only one of which will be configured.
292 * VSEL values documented as "TI cannot support these values" are flagged
293 * in these tables as UNSUP() values; we normally won't assign them.
295 * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
296 * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
298 #define UNSUP_MASK 0x8000
300 #define UNSUP(x) (UNSUP_MASK | (x))
301 #define IS_UNSUP(info, x) \
302 ((UNSUP_MASK & (x)) && \
303 !((info)->features & TWL4030_ALLOW_UNSUPPORTED))
304 #define LDO_MV(x) (~UNSUP_MASK & (x))
307 static const u16 VAUX1_VSEL_table[] = {
308 UNSUP(1500), UNSUP(1800), 2500, 2800,
309 3000, 3000, 3000, 3000,
311 static const u16 VAUX2_4030_VSEL_table[] = {
312 UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
313 1500, 1800, UNSUP(1850), 2500,
314 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
315 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
317 static const u16 VAUX2_VSEL_table[] = {
318 1700, 1700, 1900, 1300,
319 1500, 1800, 2000, 2500,
320 2100, 2800, 2200, 2300,
321 2400, 2400, 2400, 2400,
323 static const u16 VAUX3_VSEL_table[] = {
324 1500, 1800, 2500, 2800,
325 3000, 3000, 3000, 3000,
327 static const u16 VAUX4_VSEL_table[] = {
328 700, 1000, 1200, UNSUP(1300),
329 1500, 1800, UNSUP(1850), 2500,
330 UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
331 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
333 static const u16 VMMC1_VSEL_table[] = {
334 1850, 2850, 3000, 3150,
336 static const u16 VMMC2_VSEL_table[] = {
337 UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
338 UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
339 2600, 2800, 2850, 3000,
340 3150, 3150, 3150, 3150,
342 static const u16 VPLL1_VSEL_table[] = {
343 1000, 1200, 1300, 1800,
344 UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
346 static const u16 VPLL2_VSEL_table[] = {
347 700, 1000, 1200, 1300,
348 UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
349 UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
350 UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
352 static const u16 VSIM_VSEL_table[] = {
353 UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
354 2800, 3000, 3000, 3000,
356 static const u16 VDAC_VSEL_table[] = {
357 1200, 1300, 1800, 1800,
359 static const u16 VIO_VSEL_table[] = {
362 static const u16 VINTANA2_VSEL_table[] = {
366 static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
368 struct twlreg_info *info = rdev_get_drvdata(rdev);
369 int mV = info->table[index];
371 return IS_UNSUP(info, mV) ? 0 : (LDO_MV(mV) * 1000);
375 twl4030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
377 struct twlreg_info *info = rdev_get_drvdata(rdev);
379 return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
383 static int twl4030ldo_get_voltage_sel(struct regulator_dev *rdev)
385 struct twlreg_info *info = rdev_get_drvdata(rdev);
386 int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
391 vsel &= info->table_len - 1;
395 static struct regulator_ops twl4030ldo_ops = {
396 .list_voltage = twl4030ldo_list_voltage,
398 .set_voltage_sel = twl4030ldo_set_voltage_sel,
399 .get_voltage_sel = twl4030ldo_get_voltage_sel,
401 .enable = twl4030reg_enable,
402 .disable = twl4030reg_disable,
403 .is_enabled = twl4030reg_is_enabled,
405 .set_mode = twl4030reg_set_mode,
407 .get_status = twl4030reg_get_status,
411 twl4030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
414 struct twlreg_info *info = rdev_get_drvdata(rdev);
415 int vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
417 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS_4030, vsel);
422 static int twl4030smps_get_voltage(struct regulator_dev *rdev)
424 struct twlreg_info *info = rdev_get_drvdata(rdev);
427 vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
428 VREG_VOLTAGE_SMPS_4030);
430 return vsel * 12500 + 600000;
433 static struct regulator_ops twl4030smps_ops = {
434 .set_voltage = twl4030smps_set_voltage,
435 .get_voltage = twl4030smps_get_voltage,
438 /*----------------------------------------------------------------------*/
440 static struct regulator_ops twl4030fixed_ops = {
441 .list_voltage = regulator_list_voltage_linear,
443 .enable = twl4030reg_enable,
444 .disable = twl4030reg_disable,
445 .is_enabled = twl4030reg_is_enabled,
447 .set_mode = twl4030reg_set_mode,
449 .get_status = twl4030reg_get_status,
452 /*----------------------------------------------------------------------*/
454 #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) \
455 static const struct twlreg_info TWL4030_INFO_##label = { \
458 .table_len = ARRAY_SIZE(label##_VSEL_table), \
459 .table = label##_VSEL_table, \
460 .remap = remap_conf, \
463 .id = TWL4030_REG_##label, \
464 .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
465 .ops = &twl4030ldo_ops, \
466 .type = REGULATOR_VOLTAGE, \
467 .owner = THIS_MODULE, \
468 .enable_time = turnon_delay, \
469 .of_map_mode = twl4030reg_map_mode, \
473 #define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
474 static const struct twlreg_info TWL4030_INFO_##label = { \
477 .remap = remap_conf, \
480 .id = TWL4030_REG_##label, \
481 .ops = &twl4030smps_ops, \
482 .type = REGULATOR_VOLTAGE, \
483 .owner = THIS_MODULE, \
484 .enable_time = turnon_delay, \
485 .of_map_mode = twl4030reg_map_mode, \
489 #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
491 static const struct twlreg_info TWLFIXED_INFO_##label = { \
494 .remap = remap_conf, \
497 .id = TWL4030##_REG_##label, \
499 .ops = &twl4030fixed_ops, \
500 .type = REGULATOR_VOLTAGE, \
501 .owner = THIS_MODULE, \
502 .min_uV = mVolts * 1000, \
503 .enable_time = turnon_delay, \
504 .of_map_mode = twl4030reg_map_mode, \
509 * We list regulators here if systems need some level of
510 * software control over them after boot.
512 TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08);
513 TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08);
514 TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08);
515 TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08);
516 TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08);
517 TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08);
518 TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08);
519 TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00);
520 TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08);
521 TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00);
522 TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08);
523 TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08);
524 TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08);
525 TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08);
526 TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08);
527 /* VUSBCP is managed *only* by the USB subchip */
528 TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08);
529 TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08);
530 TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08);
531 TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08);
532 TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08);
534 #define TWL_OF_MATCH(comp, family, label) \
536 .compatible = comp, \
537 .data = &family##_INFO_##label, \
540 #define TWL4030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL4030, label)
541 #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
542 #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
543 #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
544 #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
546 static const struct of_device_id twl_of_match[] = {
547 TWL4030_OF_MATCH("ti,twl4030-vaux1", VAUX1),
548 TWL4030_OF_MATCH("ti,twl4030-vaux2", VAUX2_4030),
549 TWL4030_OF_MATCH("ti,twl5030-vaux2", VAUX2),
550 TWL4030_OF_MATCH("ti,twl4030-vaux3", VAUX3),
551 TWL4030_OF_MATCH("ti,twl4030-vaux4", VAUX4),
552 TWL4030_OF_MATCH("ti,twl4030-vmmc1", VMMC1),
553 TWL4030_OF_MATCH("ti,twl4030-vmmc2", VMMC2),
554 TWL4030_OF_MATCH("ti,twl4030-vpll1", VPLL1),
555 TWL4030_OF_MATCH("ti,twl4030-vpll2", VPLL2),
556 TWL4030_OF_MATCH("ti,twl4030-vsim", VSIM),
557 TWL4030_OF_MATCH("ti,twl4030-vdac", VDAC),
558 TWL4030_OF_MATCH("ti,twl4030-vintana2", VINTANA2),
559 TWL4030_OF_MATCH("ti,twl4030-vio", VIO),
560 TWL4030_OF_MATCH("ti,twl4030-vdd1", VDD1),
561 TWL4030_OF_MATCH("ti,twl4030-vdd2", VDD2),
562 TWLFIXED_OF_MATCH("ti,twl4030-vintana1", VINTANA1),
563 TWLFIXED_OF_MATCH("ti,twl4030-vintdig", VINTDIG),
564 TWLFIXED_OF_MATCH("ti,twl4030-vusb1v5", VUSB1V5),
565 TWLFIXED_OF_MATCH("ti,twl4030-vusb1v8", VUSB1V8),
566 TWLFIXED_OF_MATCH("ti,twl4030-vusb3v1", VUSB3V1),
569 MODULE_DEVICE_TABLE(of, twl_of_match);
571 static int twlreg_probe(struct platform_device *pdev)
574 struct twlreg_info *info;
575 const struct twlreg_info *template;
576 struct regulator_init_data *initdata;
577 struct regulation_constraints *c;
578 struct regulator_dev *rdev;
579 const struct of_device_id *match;
580 struct regulator_config config = { };
582 match = of_match_device(twl_of_match, &pdev->dev);
586 template = match->data;
590 id = template->desc.id;
591 initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
596 info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
600 /* Constrain board-specific capabilities according to what
601 * this driver and the chip itself can actually do.
603 c = &initdata->constraints;
604 c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
605 c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
606 | REGULATOR_CHANGE_MODE
607 | REGULATOR_CHANGE_STATUS;
609 case TWL4030_REG_VIO:
610 case TWL4030_REG_VDD1:
611 case TWL4030_REG_VDD2:
612 case TWL4030_REG_VPLL1:
613 case TWL4030_REG_VINTANA1:
614 case TWL4030_REG_VINTANA2:
615 case TWL4030_REG_VINTDIG:
622 config.dev = &pdev->dev;
623 config.init_data = initdata;
624 config.driver_data = info;
625 config.of_node = pdev->dev.of_node;
627 rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
629 dev_err(&pdev->dev, "can't register %s, %ld\n",
630 info->desc.name, PTR_ERR(rdev));
631 return PTR_ERR(rdev);
633 platform_set_drvdata(pdev, rdev);
635 twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP, info->remap);
637 /* NOTE: many regulators support short-circuit IRQs (presentable
638 * as REGULATOR_OVER_CURRENT notifications?) configured via:
640 * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
641 * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
648 MODULE_ALIAS("platform:twl4030_reg");
650 static struct platform_driver twlreg_driver = {
651 .probe = twlreg_probe,
652 /* NOTE: short name, to work around driver model truncation of
653 * "twl_regulator.12" (and friends) to "twl_regulator.1".
656 .name = "twl4030_reg",
657 .of_match_table = of_match_ptr(twl_of_match),
661 static int __init twlreg_init(void)
663 return platform_driver_register(&twlreg_driver);
665 subsys_initcall(twlreg_init);
667 static void __exit twlreg_exit(void)
669 platform_driver_unregister(&twlreg_driver);
671 module_exit(twlreg_exit)
673 MODULE_DESCRIPTION("TWL4030 regulator driver");
674 MODULE_LICENSE("GPL");