2 * Driver for Regulator part of Palmas PMIC Chips
4 * Copyright 2011-2013 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Ian Lartey <ian@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
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/slab.h>
24 #include <linux/regmap.h>
25 #include <linux/mfd/palmas.h>
27 #include <linux/of_platform.h>
28 #include <linux/regulator/of_regulator.h>
39 static const struct regs_info palmas_regs_info
[] = {
43 .vsel_addr
= PALMAS_SMPS12_VOLTAGE
,
44 .ctrl_addr
= PALMAS_SMPS12_CTRL
,
45 .tstep_addr
= PALMAS_SMPS12_TSTEP
,
46 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS12
,
51 .vsel_addr
= PALMAS_SMPS12_VOLTAGE
,
52 .ctrl_addr
= PALMAS_SMPS12_CTRL
,
53 .tstep_addr
= PALMAS_SMPS12_TSTEP
,
54 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS12
,
59 .vsel_addr
= PALMAS_SMPS3_VOLTAGE
,
60 .ctrl_addr
= PALMAS_SMPS3_CTRL
,
61 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS3
,
66 .vsel_addr
= PALMAS_SMPS45_VOLTAGE
,
67 .ctrl_addr
= PALMAS_SMPS45_CTRL
,
68 .tstep_addr
= PALMAS_SMPS45_TSTEP
,
69 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS45
,
74 .vsel_addr
= PALMAS_SMPS45_VOLTAGE
,
75 .ctrl_addr
= PALMAS_SMPS45_CTRL
,
76 .tstep_addr
= PALMAS_SMPS45_TSTEP
,
77 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS45
,
82 .vsel_addr
= PALMAS_SMPS6_VOLTAGE
,
83 .ctrl_addr
= PALMAS_SMPS6_CTRL
,
84 .tstep_addr
= PALMAS_SMPS6_TSTEP
,
85 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS6
,
90 .vsel_addr
= PALMAS_SMPS7_VOLTAGE
,
91 .ctrl_addr
= PALMAS_SMPS7_CTRL
,
92 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS7
,
97 .vsel_addr
= PALMAS_SMPS8_VOLTAGE
,
98 .ctrl_addr
= PALMAS_SMPS8_CTRL
,
99 .tstep_addr
= PALMAS_SMPS8_TSTEP
,
100 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS8
,
105 .vsel_addr
= PALMAS_SMPS9_VOLTAGE
,
106 .ctrl_addr
= PALMAS_SMPS9_CTRL
,
107 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS9
,
110 .name
= "SMPS10_OUT2",
111 .sname
= "smps10-in",
112 .ctrl_addr
= PALMAS_SMPS10_CTRL
,
113 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS10
,
116 .name
= "SMPS10_OUT1",
117 .sname
= "smps10-out2",
118 .ctrl_addr
= PALMAS_SMPS10_CTRL
,
119 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SMPS10
,
124 .vsel_addr
= PALMAS_LDO1_VOLTAGE
,
125 .ctrl_addr
= PALMAS_LDO1_CTRL
,
126 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO1
,
131 .vsel_addr
= PALMAS_LDO2_VOLTAGE
,
132 .ctrl_addr
= PALMAS_LDO2_CTRL
,
133 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO2
,
138 .vsel_addr
= PALMAS_LDO3_VOLTAGE
,
139 .ctrl_addr
= PALMAS_LDO3_CTRL
,
140 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO3
,
145 .vsel_addr
= PALMAS_LDO4_VOLTAGE
,
146 .ctrl_addr
= PALMAS_LDO4_CTRL
,
147 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO4
,
152 .vsel_addr
= PALMAS_LDO5_VOLTAGE
,
153 .ctrl_addr
= PALMAS_LDO5_CTRL
,
154 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO5
,
159 .vsel_addr
= PALMAS_LDO6_VOLTAGE
,
160 .ctrl_addr
= PALMAS_LDO6_CTRL
,
161 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO6
,
166 .vsel_addr
= PALMAS_LDO7_VOLTAGE
,
167 .ctrl_addr
= PALMAS_LDO7_CTRL
,
168 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO7
,
173 .vsel_addr
= PALMAS_LDO8_VOLTAGE
,
174 .ctrl_addr
= PALMAS_LDO8_CTRL
,
175 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO8
,
180 .vsel_addr
= PALMAS_LDO9_VOLTAGE
,
181 .ctrl_addr
= PALMAS_LDO9_CTRL
,
182 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDO9
,
187 .vsel_addr
= PALMAS_LDOLN_VOLTAGE
,
188 .ctrl_addr
= PALMAS_LDOLN_CTRL
,
189 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDOLN
,
193 .sname
= "ldousb-in",
194 .vsel_addr
= PALMAS_LDOUSB_VOLTAGE
,
195 .ctrl_addr
= PALMAS_LDOUSB_CTRL
,
196 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_LDOUSB
,
200 .ctrl_addr
= PALMAS_REGEN1_CTRL
,
201 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_REGEN1
,
205 .ctrl_addr
= PALMAS_REGEN2_CTRL
,
206 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_REGEN2
,
210 .ctrl_addr
= PALMAS_REGEN3_CTRL
,
211 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_REGEN3
,
215 .ctrl_addr
= PALMAS_SYSEN1_CTRL
,
216 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SYSEN1
,
220 .ctrl_addr
= PALMAS_SYSEN2_CTRL
,
221 .sleep_id
= PALMAS_EXTERNAL_REQSTR_ID_SYSEN2
,
225 static unsigned int palmas_smps_ramp_delay
[4] = {0, 10000, 5000, 2500};
227 #define SMPS_CTRL_MODE_OFF 0x00
228 #define SMPS_CTRL_MODE_ON 0x01
229 #define SMPS_CTRL_MODE_ECO 0x02
230 #define SMPS_CTRL_MODE_PWM 0x03
232 #define PALMAS_SMPS_NUM_VOLTAGES 122
233 #define PALMAS_SMPS10_NUM_VOLTAGES 2
234 #define PALMAS_LDO_NUM_VOLTAGES 50
236 #define SMPS10_VSEL (1<<3)
237 #define SMPS10_BOOST_EN (1<<2)
238 #define SMPS10_BYPASS_EN (1<<1)
239 #define SMPS10_SWITCH_EN (1<<0)
241 #define REGULATOR_SLAVE 0
243 static int palmas_smps_read(struct palmas
*palmas
, unsigned int reg
,
248 addr
= PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
, reg
);
250 return regmap_read(palmas
->regmap
[REGULATOR_SLAVE
], addr
, dest
);
253 static int palmas_smps_write(struct palmas
*palmas
, unsigned int reg
,
258 addr
= PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
, reg
);
260 return regmap_write(palmas
->regmap
[REGULATOR_SLAVE
], addr
, value
);
263 static int palmas_ldo_read(struct palmas
*palmas
, unsigned int reg
,
268 addr
= PALMAS_BASE_TO_REG(PALMAS_LDO_BASE
, reg
);
270 return regmap_read(palmas
->regmap
[REGULATOR_SLAVE
], addr
, dest
);
273 static int palmas_ldo_write(struct palmas
*palmas
, unsigned int reg
,
278 addr
= PALMAS_BASE_TO_REG(PALMAS_LDO_BASE
, reg
);
280 return regmap_write(palmas
->regmap
[REGULATOR_SLAVE
], addr
, value
);
283 static int palmas_is_enabled_smps(struct regulator_dev
*dev
)
285 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
286 int id
= rdev_get_id(dev
);
289 palmas_smps_read(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, ®
);
291 reg
&= PALMAS_SMPS12_CTRL_STATUS_MASK
;
292 reg
>>= PALMAS_SMPS12_CTRL_STATUS_SHIFT
;
297 static int palmas_enable_smps(struct regulator_dev
*dev
)
299 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
300 int id
= rdev_get_id(dev
);
303 palmas_smps_read(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, ®
);
305 reg
&= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
306 if (pmic
->current_reg_mode
[id
])
307 reg
|= pmic
->current_reg_mode
[id
];
309 reg
|= SMPS_CTRL_MODE_ON
;
311 palmas_smps_write(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, reg
);
316 static int palmas_disable_smps(struct regulator_dev
*dev
)
318 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
319 int id
= rdev_get_id(dev
);
322 palmas_smps_read(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, ®
);
324 reg
&= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
326 palmas_smps_write(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, reg
);
331 static int palmas_set_mode_smps(struct regulator_dev
*dev
, unsigned int mode
)
333 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
334 int id
= rdev_get_id(dev
);
336 bool rail_enable
= true;
338 palmas_smps_read(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, ®
);
339 reg
&= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
341 if (reg
== SMPS_CTRL_MODE_OFF
)
345 case REGULATOR_MODE_NORMAL
:
346 reg
|= SMPS_CTRL_MODE_ON
;
348 case REGULATOR_MODE_IDLE
:
349 reg
|= SMPS_CTRL_MODE_ECO
;
351 case REGULATOR_MODE_FAST
:
352 reg
|= SMPS_CTRL_MODE_PWM
;
358 pmic
->current_reg_mode
[id
] = reg
& PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
360 palmas_smps_write(pmic
->palmas
,
361 palmas_regs_info
[id
].ctrl_addr
, reg
);
365 static unsigned int palmas_get_mode_smps(struct regulator_dev
*dev
)
367 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
368 int id
= rdev_get_id(dev
);
371 reg
= pmic
->current_reg_mode
[id
] & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
374 case SMPS_CTRL_MODE_ON
:
375 return REGULATOR_MODE_NORMAL
;
376 case SMPS_CTRL_MODE_ECO
:
377 return REGULATOR_MODE_IDLE
;
378 case SMPS_CTRL_MODE_PWM
:
379 return REGULATOR_MODE_FAST
;
385 static int palmas_list_voltage_smps(struct regulator_dev
*dev
,
388 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
389 int id
= rdev_get_id(dev
);
392 /* Read the multiplier set in VSEL register to return
393 * the correct voltage.
400 else if (selector
< 6)
401 return 500000 * mult
;
403 /* Voltage is linear mapping starting from selector 6,
404 * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
405 * RANGE is either x1 or x2
407 return (490000 + ((selector
- 5) * 10000)) * mult
;
410 static int palmas_map_voltage_smps(struct regulator_dev
*rdev
,
411 int min_uV
, int max_uV
)
413 struct palmas_pmic
*pmic
= rdev_get_drvdata(rdev
);
414 int id
= rdev_get_id(rdev
);
420 if (pmic
->range
[id
]) { /* RANGE is x2 */
421 if (min_uV
< 1000000)
423 ret
= DIV_ROUND_UP(min_uV
- 1000000, 20000) + 6;
424 } else { /* RANGE is x1 */
427 ret
= DIV_ROUND_UP(min_uV
- 500000, 10000) + 6;
430 /* Map back into a voltage to verify we're still in bounds */
431 voltage
= palmas_list_voltage_smps(rdev
, ret
);
432 if (voltage
< min_uV
|| voltage
> max_uV
)
438 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev
*rdev
,
439 unsigned int old_selector
, unsigned int new_selector
)
441 struct palmas_pmic
*pmic
= rdev_get_drvdata(rdev
);
442 int id
= rdev_get_id(rdev
);
444 unsigned int ramp_delay
= pmic
->ramp_delay
[id
];
449 old_uv
= palmas_list_voltage_smps(rdev
, old_selector
);
453 new_uv
= palmas_list_voltage_smps(rdev
, new_selector
);
457 return DIV_ROUND_UP(abs(old_uv
- new_uv
), ramp_delay
);
460 static int palmas_smps_set_ramp_delay(struct regulator_dev
*rdev
,
463 struct palmas_pmic
*pmic
= rdev_get_drvdata(rdev
);
464 int id
= rdev_get_id(rdev
);
465 unsigned int reg
= 0;
466 unsigned int addr
= palmas_regs_info
[id
].tstep_addr
;
469 /* SMPS3 and SMPS7 do not have tstep_addr setting */
471 case PALMAS_REG_SMPS3
:
472 case PALMAS_REG_SMPS7
:
478 else if (ramp_delay
<= 2500)
480 else if (ramp_delay
<= 5000)
485 ret
= palmas_smps_write(pmic
->palmas
, addr
, reg
);
487 dev_err(pmic
->palmas
->dev
, "TSTEP write failed: %d\n", ret
);
491 pmic
->ramp_delay
[id
] = palmas_smps_ramp_delay
[reg
];
495 static struct regulator_ops palmas_ops_smps
= {
496 .is_enabled
= palmas_is_enabled_smps
,
497 .enable
= palmas_enable_smps
,
498 .disable
= palmas_disable_smps
,
499 .set_mode
= palmas_set_mode_smps
,
500 .get_mode
= palmas_get_mode_smps
,
501 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
502 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
503 .list_voltage
= palmas_list_voltage_smps
,
504 .map_voltage
= palmas_map_voltage_smps
,
505 .set_voltage_time_sel
= palma_smps_set_voltage_smps_time_sel
,
506 .set_ramp_delay
= palmas_smps_set_ramp_delay
,
509 static struct regulator_ops palmas_ops_ext_control_smps
= {
510 .set_mode
= palmas_set_mode_smps
,
511 .get_mode
= palmas_get_mode_smps
,
512 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
513 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
514 .list_voltage
= palmas_list_voltage_smps
,
515 .map_voltage
= palmas_map_voltage_smps
,
516 .set_voltage_time_sel
= palma_smps_set_voltage_smps_time_sel
,
517 .set_ramp_delay
= palmas_smps_set_ramp_delay
,
520 static struct regulator_ops palmas_ops_smps10
= {
521 .is_enabled
= regulator_is_enabled_regmap
,
522 .enable
= regulator_enable_regmap
,
523 .disable
= regulator_disable_regmap
,
524 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
525 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
526 .list_voltage
= regulator_list_voltage_linear
,
527 .map_voltage
= regulator_map_voltage_linear
,
528 .set_bypass
= regulator_set_bypass_regmap
,
529 .get_bypass
= regulator_get_bypass_regmap
,
532 static int palmas_is_enabled_ldo(struct regulator_dev
*dev
)
534 struct palmas_pmic
*pmic
= rdev_get_drvdata(dev
);
535 int id
= rdev_get_id(dev
);
538 palmas_ldo_read(pmic
->palmas
, palmas_regs_info
[id
].ctrl_addr
, ®
);
540 reg
&= PALMAS_LDO1_CTRL_STATUS
;
545 static struct regulator_ops palmas_ops_ldo
= {
546 .is_enabled
= palmas_is_enabled_ldo
,
547 .enable
= regulator_enable_regmap
,
548 .disable
= regulator_disable_regmap
,
549 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
550 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
551 .list_voltage
= regulator_list_voltage_linear
,
552 .map_voltage
= regulator_map_voltage_linear
,
555 static struct regulator_ops palmas_ops_ext_control_ldo
= {
556 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
557 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
558 .list_voltage
= regulator_list_voltage_linear
,
559 .map_voltage
= regulator_map_voltage_linear
,
562 static struct regulator_ops palmas_ops_extreg
= {
563 .is_enabled
= regulator_is_enabled_regmap
,
564 .enable
= regulator_enable_regmap
,
565 .disable
= regulator_disable_regmap
,
568 static struct regulator_ops palmas_ops_ext_control_extreg
= {
571 static int palmas_regulator_config_external(struct palmas
*palmas
, int id
,
572 struct palmas_reg_init
*reg_init
)
574 int sleep_id
= palmas_regs_info
[id
].sleep_id
;
577 ret
= palmas_ext_control_req_config(palmas
, sleep_id
,
578 reg_init
->roof_floor
, true);
581 "Ext control config for regulator %d failed %d\n",
587 * setup the hardware based sleep configuration of the SMPS/LDO regulators
588 * from the platform data. This is different to the software based control
589 * supported by the regulator framework as it is controlled by toggling
590 * pins on the PMIC such as PREQ, SYSEN, ...
592 static int palmas_smps_init(struct palmas
*palmas
, int id
,
593 struct palmas_reg_init
*reg_init
)
599 addr
= palmas_regs_info
[id
].ctrl_addr
;
601 ret
= palmas_smps_read(palmas
, addr
, ®
);
606 case PALMAS_REG_SMPS10_OUT1
:
607 case PALMAS_REG_SMPS10_OUT2
:
608 reg
&= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK
;
609 if (reg_init
->mode_sleep
)
610 reg
|= reg_init
->mode_sleep
<<
611 PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT
;
614 if (reg_init
->warm_reset
)
615 reg
|= PALMAS_SMPS12_CTRL_WR_S
;
617 reg
&= ~PALMAS_SMPS12_CTRL_WR_S
;
619 if (reg_init
->roof_floor
)
620 reg
|= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN
;
622 reg
&= ~PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN
;
624 reg
&= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK
;
625 if (reg_init
->mode_sleep
)
626 reg
|= reg_init
->mode_sleep
<<
627 PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT
;
630 ret
= palmas_smps_write(palmas
, addr
, reg
);
634 if (palmas_regs_info
[id
].vsel_addr
&& reg_init
->vsel
) {
635 addr
= palmas_regs_info
[id
].vsel_addr
;
637 reg
= reg_init
->vsel
;
639 ret
= palmas_smps_write(palmas
, addr
, reg
);
644 if (reg_init
->roof_floor
&& (id
!= PALMAS_REG_SMPS10_OUT1
) &&
645 (id
!= PALMAS_REG_SMPS10_OUT2
)) {
646 /* Enable externally controlled regulator */
647 addr
= palmas_regs_info
[id
].ctrl_addr
;
648 ret
= palmas_smps_read(palmas
, addr
, ®
);
652 if (!(reg
& PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
)) {
653 reg
|= SMPS_CTRL_MODE_ON
;
654 ret
= palmas_smps_write(palmas
, addr
, reg
);
658 return palmas_regulator_config_external(palmas
, id
, reg_init
);
663 static int palmas_ldo_init(struct palmas
*palmas
, int id
,
664 struct palmas_reg_init
*reg_init
)
670 addr
= palmas_regs_info
[id
].ctrl_addr
;
672 ret
= palmas_ldo_read(palmas
, addr
, ®
);
676 if (reg_init
->warm_reset
)
677 reg
|= PALMAS_LDO1_CTRL_WR_S
;
679 reg
&= ~PALMAS_LDO1_CTRL_WR_S
;
681 if (reg_init
->mode_sleep
)
682 reg
|= PALMAS_LDO1_CTRL_MODE_SLEEP
;
684 reg
&= ~PALMAS_LDO1_CTRL_MODE_SLEEP
;
686 ret
= palmas_ldo_write(palmas
, addr
, reg
);
690 if (reg_init
->roof_floor
) {
691 /* Enable externally controlled regulator */
692 addr
= palmas_regs_info
[id
].ctrl_addr
;
693 ret
= palmas_update_bits(palmas
, PALMAS_LDO_BASE
,
694 addr
, PALMAS_LDO1_CTRL_MODE_ACTIVE
,
695 PALMAS_LDO1_CTRL_MODE_ACTIVE
);
698 "LDO Register 0x%02x update failed %d\n",
702 return palmas_regulator_config_external(palmas
, id
, reg_init
);
707 static int palmas_extreg_init(struct palmas
*palmas
, int id
,
708 struct palmas_reg_init
*reg_init
)
712 unsigned int val
= 0;
714 addr
= palmas_regs_info
[id
].ctrl_addr
;
716 if (reg_init
->mode_sleep
)
717 val
= PALMAS_REGEN1_CTRL_MODE_SLEEP
;
719 ret
= palmas_update_bits(palmas
, PALMAS_RESOURCE_BASE
,
720 addr
, PALMAS_REGEN1_CTRL_MODE_SLEEP
, val
);
722 dev_err(palmas
->dev
, "Resource reg 0x%02x update failed %d\n",
727 if (reg_init
->roof_floor
) {
728 /* Enable externally controlled regulator */
729 addr
= palmas_regs_info
[id
].ctrl_addr
;
730 ret
= palmas_update_bits(palmas
, PALMAS_RESOURCE_BASE
,
731 addr
, PALMAS_REGEN1_CTRL_MODE_ACTIVE
,
732 PALMAS_REGEN1_CTRL_MODE_ACTIVE
);
735 "Resource Register 0x%02x update failed %d\n",
739 return palmas_regulator_config_external(palmas
, id
, reg_init
);
744 static void palmas_enable_ldo8_track(struct palmas
*palmas
)
750 addr
= palmas_regs_info
[PALMAS_REG_LDO8
].ctrl_addr
;
752 ret
= palmas_ldo_read(palmas
, addr
, ®
);
754 dev_err(palmas
->dev
, "Error in reading ldo8 control reg\n");
758 reg
|= PALMAS_LDO8_CTRL_LDO_TRACKING_EN
;
759 ret
= palmas_ldo_write(palmas
, addr
, reg
);
761 dev_err(palmas
->dev
, "Error in enabling tracking mode\n");
765 * When SMPS45 is set to off and LDO8 tracking is enabled, the LDO8
766 * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
767 * and can be set from 0.45 to 1.65 V.
769 addr
= palmas_regs_info
[PALMAS_REG_LDO8
].vsel_addr
;
770 ret
= palmas_ldo_read(palmas
, addr
, ®
);
772 dev_err(palmas
->dev
, "Error in reading ldo8 voltage reg\n");
776 reg
= (reg
<< 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK
;
777 ret
= palmas_ldo_write(palmas
, addr
, reg
);
779 dev_err(palmas
->dev
, "Error in setting ldo8 voltage reg\n");
784 static struct of_regulator_match palmas_matches
[] = {
785 { .name
= "smps12", },
786 { .name
= "smps123", },
787 { .name
= "smps3", },
788 { .name
= "smps45", },
789 { .name
= "smps457", },
790 { .name
= "smps6", },
791 { .name
= "smps7", },
792 { .name
= "smps8", },
793 { .name
= "smps9", },
794 { .name
= "smps10_out2", },
795 { .name
= "smps10_out1", },
805 { .name
= "ldoln", },
806 { .name
= "ldousb", },
807 { .name
= "regen1", },
808 { .name
= "regen2", },
809 { .name
= "regen3", },
810 { .name
= "sysen1", },
811 { .name
= "sysen2", },
814 static void palmas_dt_to_pdata(struct device
*dev
,
815 struct device_node
*node
,
816 struct palmas_pmic_platform_data
*pdata
)
818 struct device_node
*regulators
;
822 node
= of_node_get(node
);
823 regulators
= of_get_child_by_name(node
, "regulators");
825 dev_info(dev
, "regulator node not found\n");
829 ret
= of_regulator_match(dev
, regulators
, palmas_matches
,
831 of_node_put(regulators
);
833 dev_err(dev
, "Error parsing regulator init data: %d\n", ret
);
837 for (idx
= 0; idx
< PALMAS_NUM_REGS
; idx
++) {
838 if (!palmas_matches
[idx
].init_data
||
839 !palmas_matches
[idx
].of_node
)
842 pdata
->reg_data
[idx
] = palmas_matches
[idx
].init_data
;
844 pdata
->reg_init
[idx
] = devm_kzalloc(dev
,
845 sizeof(struct palmas_reg_init
), GFP_KERNEL
);
847 pdata
->reg_init
[idx
]->warm_reset
=
848 of_property_read_bool(palmas_matches
[idx
].of_node
,
851 ret
= of_property_read_u32(palmas_matches
[idx
].of_node
,
852 "ti,roof-floor", &prop
);
853 /* EINVAL: Property not found */
854 if (ret
!= -EINVAL
) {
857 /* use default value, when no value is specified */
858 econtrol
= PALMAS_EXT_CONTROL_NSLEEP
;
862 econtrol
= PALMAS_EXT_CONTROL_ENABLE1
;
865 econtrol
= PALMAS_EXT_CONTROL_ENABLE2
;
868 econtrol
= PALMAS_EXT_CONTROL_NSLEEP
;
873 "%s: Invalid roof-floor option: %u\n",
874 palmas_matches
[idx
].name
, prop
);
878 pdata
->reg_init
[idx
]->roof_floor
= econtrol
;
881 ret
= of_property_read_u32(palmas_matches
[idx
].of_node
,
882 "ti,mode-sleep", &prop
);
884 pdata
->reg_init
[idx
]->mode_sleep
= prop
;
886 ret
= of_property_read_bool(palmas_matches
[idx
].of_node
,
889 pdata
->reg_init
[idx
]->vsel
=
890 PALMAS_SMPS12_VOLTAGE_RANGE
;
892 if (idx
== PALMAS_REG_LDO8
)
893 pdata
->enable_ldo8_tracking
= of_property_read_bool(
894 palmas_matches
[idx
].of_node
,
895 "ti,enable-ldo8-tracking");
898 pdata
->ldo6_vibrator
= of_property_read_bool(node
, "ti,ldo6-vibrator");
902 static int palmas_regulators_probe(struct platform_device
*pdev
)
904 struct palmas
*palmas
= dev_get_drvdata(pdev
->dev
.parent
);
905 struct palmas_pmic_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
906 struct device_node
*node
= pdev
->dev
.of_node
;
907 struct regulator_dev
*rdev
;
908 struct regulator_config config
= { };
909 struct palmas_pmic
*pmic
;
910 struct palmas_reg_init
*reg_init
;
912 unsigned int addr
, reg
;
914 if (node
&& !pdata
) {
915 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
920 palmas_dt_to_pdata(&pdev
->dev
, node
, pdata
);
923 pmic
= devm_kzalloc(&pdev
->dev
, sizeof(*pmic
), GFP_KERNEL
);
927 pmic
->dev
= &pdev
->dev
;
928 pmic
->palmas
= palmas
;
930 platform_set_drvdata(pdev
, pmic
);
932 ret
= palmas_smps_read(palmas
, PALMAS_SMPS_CTRL
, ®
);
936 if (reg
& PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN
)
939 if (reg
& PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN
)
942 config
.regmap
= palmas
->regmap
[REGULATOR_SLAVE
];
943 config
.dev
= &pdev
->dev
;
944 config
.driver_data
= pmic
;
946 for (id
= 0; id
< PALMAS_REG_LDO1
; id
++) {
947 bool ramp_delay_support
= false;
950 * Miss out regulators which are not available due
951 * to slaving configurations.
954 case PALMAS_REG_SMPS12
:
955 case PALMAS_REG_SMPS3
:
958 if (id
== PALMAS_REG_SMPS12
)
959 ramp_delay_support
= true;
961 case PALMAS_REG_SMPS123
:
964 ramp_delay_support
= true;
966 case PALMAS_REG_SMPS45
:
967 case PALMAS_REG_SMPS7
:
970 if (id
== PALMAS_REG_SMPS45
)
971 ramp_delay_support
= true;
973 case PALMAS_REG_SMPS457
:
976 ramp_delay_support
= true;
978 case PALMAS_REG_SMPS10_OUT1
:
979 case PALMAS_REG_SMPS10_OUT2
:
980 if (!PALMAS_PMIC_HAS(palmas
, SMPS10_BOOST
))
984 if ((id
== PALMAS_REG_SMPS6
) || (id
== PALMAS_REG_SMPS8
))
985 ramp_delay_support
= true;
987 if (ramp_delay_support
) {
988 addr
= palmas_regs_info
[id
].tstep_addr
;
989 ret
= palmas_smps_read(pmic
->palmas
, addr
, ®
);
992 "reading TSTEP reg failed: %d\n", ret
);
995 pmic
->desc
[id
].ramp_delay
=
996 palmas_smps_ramp_delay
[reg
& 0x3];
997 pmic
->ramp_delay
[id
] = pmic
->desc
[id
].ramp_delay
;
1000 /* Initialise sleep/init values from platform data */
1001 if (pdata
&& pdata
->reg_init
[id
]) {
1002 reg_init
= pdata
->reg_init
[id
];
1003 ret
= palmas_smps_init(palmas
, id
, reg_init
);
1010 /* Register the regulators */
1011 pmic
->desc
[id
].name
= palmas_regs_info
[id
].name
;
1012 pmic
->desc
[id
].id
= id
;
1015 case PALMAS_REG_SMPS10_OUT1
:
1016 case PALMAS_REG_SMPS10_OUT2
:
1017 pmic
->desc
[id
].n_voltages
= PALMAS_SMPS10_NUM_VOLTAGES
;
1018 pmic
->desc
[id
].ops
= &palmas_ops_smps10
;
1019 pmic
->desc
[id
].vsel_reg
=
1020 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
,
1021 PALMAS_SMPS10_CTRL
);
1022 pmic
->desc
[id
].vsel_mask
= SMPS10_VSEL
;
1023 pmic
->desc
[id
].enable_reg
=
1024 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
,
1025 PALMAS_SMPS10_CTRL
);
1026 if (id
== PALMAS_REG_SMPS10_OUT1
)
1027 pmic
->desc
[id
].enable_mask
= SMPS10_SWITCH_EN
;
1029 pmic
->desc
[id
].enable_mask
= SMPS10_BOOST_EN
;
1030 pmic
->desc
[id
].bypass_reg
=
1031 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
,
1032 PALMAS_SMPS10_CTRL
);
1033 pmic
->desc
[id
].bypass_mask
= SMPS10_BYPASS_EN
;
1034 pmic
->desc
[id
].min_uV
= 3750000;
1035 pmic
->desc
[id
].uV_step
= 1250000;
1039 * Read and store the RANGE bit for later use
1040 * This must be done before regulator is probed,
1041 * otherwise we error in probe with unsupportable
1042 * ranges. Read the current smps mode for later use.
1044 addr
= palmas_regs_info
[id
].vsel_addr
;
1046 ret
= palmas_smps_read(pmic
->palmas
, addr
, ®
);
1049 if (reg
& PALMAS_SMPS12_VOLTAGE_RANGE
)
1050 pmic
->range
[id
] = 1;
1052 if (reg_init
&& reg_init
->roof_floor
)
1053 pmic
->desc
[id
].ops
=
1054 &palmas_ops_ext_control_smps
;
1056 pmic
->desc
[id
].ops
= &palmas_ops_smps
;
1057 pmic
->desc
[id
].n_voltages
= PALMAS_SMPS_NUM_VOLTAGES
;
1058 pmic
->desc
[id
].vsel_reg
=
1059 PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE
,
1060 palmas_regs_info
[id
].vsel_addr
);
1061 pmic
->desc
[id
].vsel_mask
=
1062 PALMAS_SMPS12_VOLTAGE_VSEL_MASK
;
1064 /* Read the smps mode for later use. */
1065 addr
= palmas_regs_info
[id
].ctrl_addr
;
1066 ret
= palmas_smps_read(pmic
->palmas
, addr
, ®
);
1069 pmic
->current_reg_mode
[id
] = reg
&
1070 PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK
;
1073 pmic
->desc
[id
].type
= REGULATOR_VOLTAGE
;
1074 pmic
->desc
[id
].owner
= THIS_MODULE
;
1077 config
.init_data
= pdata
->reg_data
[id
];
1079 config
.init_data
= NULL
;
1081 pmic
->desc
[id
].supply_name
= palmas_regs_info
[id
].sname
;
1082 config
.of_node
= palmas_matches
[id
].of_node
;
1084 rdev
= devm_regulator_register(&pdev
->dev
, &pmic
->desc
[id
],
1088 "failed to register %s regulator\n",
1090 return PTR_ERR(rdev
);
1093 /* Save regulator for cleanup */
1094 pmic
->rdev
[id
] = rdev
;
1097 /* Start this loop from the id left from previous loop */
1098 for (; id
< PALMAS_NUM_REGS
; id
++) {
1099 if (pdata
&& pdata
->reg_init
[id
])
1100 reg_init
= pdata
->reg_init
[id
];
1104 /* Miss out regulators which are not available due
1105 * to alternate functions.
1108 /* Register the regulators */
1109 pmic
->desc
[id
].name
= palmas_regs_info
[id
].name
;
1110 pmic
->desc
[id
].id
= id
;
1111 pmic
->desc
[id
].type
= REGULATOR_VOLTAGE
;
1112 pmic
->desc
[id
].owner
= THIS_MODULE
;
1114 if (id
< PALMAS_REG_REGEN1
) {
1115 pmic
->desc
[id
].n_voltages
= PALMAS_LDO_NUM_VOLTAGES
;
1116 if (reg_init
&& reg_init
->roof_floor
)
1117 pmic
->desc
[id
].ops
=
1118 &palmas_ops_ext_control_ldo
;
1120 pmic
->desc
[id
].ops
= &palmas_ops_ldo
;
1121 pmic
->desc
[id
].min_uV
= 900000;
1122 pmic
->desc
[id
].uV_step
= 50000;
1123 pmic
->desc
[id
].linear_min_sel
= 1;
1124 pmic
->desc
[id
].enable_time
= 500;
1125 pmic
->desc
[id
].vsel_reg
=
1126 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE
,
1127 palmas_regs_info
[id
].vsel_addr
);
1128 pmic
->desc
[id
].vsel_mask
=
1129 PALMAS_LDO1_VOLTAGE_VSEL_MASK
;
1130 pmic
->desc
[id
].enable_reg
=
1131 PALMAS_BASE_TO_REG(PALMAS_LDO_BASE
,
1132 palmas_regs_info
[id
].ctrl_addr
);
1133 pmic
->desc
[id
].enable_mask
=
1134 PALMAS_LDO1_CTRL_MODE_ACTIVE
;
1136 /* Check if LDO8 is in tracking mode or not */
1137 if (pdata
&& (id
== PALMAS_REG_LDO8
) &&
1138 pdata
->enable_ldo8_tracking
) {
1139 palmas_enable_ldo8_track(palmas
);
1140 pmic
->desc
[id
].min_uV
= 450000;
1141 pmic
->desc
[id
].uV_step
= 25000;
1144 /* LOD6 in vibrator mode will have enable time 2000us */
1145 if (pdata
&& pdata
->ldo6_vibrator
&&
1146 (id
== PALMAS_REG_LDO6
))
1147 pmic
->desc
[id
].enable_time
= 2000;
1149 pmic
->desc
[id
].n_voltages
= 1;
1150 if (reg_init
&& reg_init
->roof_floor
)
1151 pmic
->desc
[id
].ops
=
1152 &palmas_ops_ext_control_extreg
;
1154 pmic
->desc
[id
].ops
= &palmas_ops_extreg
;
1155 pmic
->desc
[id
].enable_reg
=
1156 PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE
,
1157 palmas_regs_info
[id
].ctrl_addr
);
1158 pmic
->desc
[id
].enable_mask
=
1159 PALMAS_REGEN1_CTRL_MODE_ACTIVE
;
1163 config
.init_data
= pdata
->reg_data
[id
];
1165 config
.init_data
= NULL
;
1167 pmic
->desc
[id
].supply_name
= palmas_regs_info
[id
].sname
;
1168 config
.of_node
= palmas_matches
[id
].of_node
;
1170 rdev
= devm_regulator_register(&pdev
->dev
, &pmic
->desc
[id
],
1174 "failed to register %s regulator\n",
1176 return PTR_ERR(rdev
);
1179 /* Save regulator for cleanup */
1180 pmic
->rdev
[id
] = rdev
;
1182 /* Initialise sleep/init values from platform data */
1184 reg_init
= pdata
->reg_init
[id
];
1186 if (id
< PALMAS_REG_REGEN1
)
1187 ret
= palmas_ldo_init(palmas
,
1190 ret
= palmas_extreg_init(palmas
,
1202 static struct of_device_id of_palmas_match_tbl
[] = {
1203 { .compatible
= "ti,palmas-pmic", },
1204 { .compatible
= "ti,twl6035-pmic", },
1205 { .compatible
= "ti,twl6036-pmic", },
1206 { .compatible
= "ti,twl6037-pmic", },
1207 { .compatible
= "ti,tps65913-pmic", },
1208 { .compatible
= "ti,tps65914-pmic", },
1209 { .compatible
= "ti,tps80036-pmic", },
1210 { .compatible
= "ti,tps659038-pmic", },
1214 static struct platform_driver palmas_driver
= {
1216 .name
= "palmas-pmic",
1217 .of_match_table
= of_palmas_match_tbl
,
1218 .owner
= THIS_MODULE
,
1220 .probe
= palmas_regulators_probe
,
1223 static int __init
palmas_init(void)
1225 return platform_driver_register(&palmas_driver
);
1227 subsys_initcall(palmas_init
);
1229 static void __exit
palmas_exit(void)
1231 platform_driver_unregister(&palmas_driver
);
1233 module_exit(palmas_exit
);
1235 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1236 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1237 MODULE_LICENSE("GPL");
1238 MODULE_ALIAS("platform:palmas-pmic");
1239 MODULE_DEVICE_TABLE(of
, of_palmas_match_tbl
);