2 * Voltage regulator support for AMS AS3722 PMIC
4 * Copyright (C) 2013 ams
6 * Author: Florian Lobmaier <florian.lobmaier@ams.com>
7 * Author: Laxman Dewangan <ldewangan@nvidia.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/err.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/mfd/as3722.h>
30 #include <linux/of_platform.h>
31 #include <linux/platform_device.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/regulator/of_regulator.h>
35 #include <linux/slab.h>
38 enum as3722_regulators_id
{
39 AS3722_REGULATOR_ID_SD0
,
40 AS3722_REGULATOR_ID_SD1
,
41 AS3722_REGULATOR_ID_SD2
,
42 AS3722_REGULATOR_ID_SD3
,
43 AS3722_REGULATOR_ID_SD4
,
44 AS3722_REGULATOR_ID_SD5
,
45 AS3722_REGULATOR_ID_SD6
,
46 AS3722_REGULATOR_ID_LDO0
,
47 AS3722_REGULATOR_ID_LDO1
,
48 AS3722_REGULATOR_ID_LDO2
,
49 AS3722_REGULATOR_ID_LDO3
,
50 AS3722_REGULATOR_ID_LDO4
,
51 AS3722_REGULATOR_ID_LDO5
,
52 AS3722_REGULATOR_ID_LDO6
,
53 AS3722_REGULATOR_ID_LDO7
,
54 AS3722_REGULATOR_ID_LDO9
,
55 AS3722_REGULATOR_ID_LDO10
,
56 AS3722_REGULATOR_ID_LDO11
,
57 AS3722_REGULATOR_ID_MAX
,
60 struct as3722_register_mapping
{
75 struct as3722_regulator_config_data
{
76 struct regulator_init_data
*reg_init
;
81 struct as3722_regulators
{
83 struct as3722
*as3722
;
84 struct regulator_dev
*rdevs
[AS3722_REGULATOR_ID_MAX
];
85 struct regulator_desc desc
[AS3722_REGULATOR_ID_MAX
];
86 struct as3722_regulator_config_data
87 reg_config_data
[AS3722_REGULATOR_ID_MAX
];
90 static const struct as3722_register_mapping as3722_reg_lookup
[] = {
92 .regulator_id
= AS3722_REGULATOR_ID_SD0
,
94 .vsel_reg
= AS3722_SD0_VOLTAGE_REG
,
95 .vsel_mask
= AS3722_SD_VSEL_MASK
,
96 .enable_reg
= AS3722_SD_CONTROL_REG
,
97 .enable_mask
= AS3722_SDn_CTRL(0),
98 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL1_REG
,
99 .sleep_ctrl_mask
= AS3722_SD0_EXT_ENABLE_MASK
,
100 .control_reg
= AS3722_SD0_CONTROL_REG
,
101 .mode_mask
= AS3722_SD0_MODE_FAST
,
104 .regulator_id
= AS3722_REGULATOR_ID_SD1
,
105 .name
= "as3722-sd1",
106 .vsel_reg
= AS3722_SD1_VOLTAGE_REG
,
107 .vsel_mask
= AS3722_SD_VSEL_MASK
,
108 .enable_reg
= AS3722_SD_CONTROL_REG
,
109 .enable_mask
= AS3722_SDn_CTRL(1),
110 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL1_REG
,
111 .sleep_ctrl_mask
= AS3722_SD1_EXT_ENABLE_MASK
,
112 .control_reg
= AS3722_SD1_CONTROL_REG
,
113 .mode_mask
= AS3722_SD1_MODE_FAST
,
116 .regulator_id
= AS3722_REGULATOR_ID_SD2
,
117 .name
= "as3722-sd2",
119 .vsel_reg
= AS3722_SD2_VOLTAGE_REG
,
120 .vsel_mask
= AS3722_SD_VSEL_MASK
,
121 .enable_reg
= AS3722_SD_CONTROL_REG
,
122 .enable_mask
= AS3722_SDn_CTRL(2),
123 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL1_REG
,
124 .sleep_ctrl_mask
= AS3722_SD2_EXT_ENABLE_MASK
,
125 .control_reg
= AS3722_SD23_CONTROL_REG
,
126 .mode_mask
= AS3722_SD2_MODE_FAST
,
127 .n_voltages
= AS3722_SD2_VSEL_MAX
+ 1,
130 .regulator_id
= AS3722_REGULATOR_ID_SD3
,
131 .name
= "as3722-sd3",
133 .vsel_reg
= AS3722_SD3_VOLTAGE_REG
,
134 .vsel_mask
= AS3722_SD_VSEL_MASK
,
135 .enable_reg
= AS3722_SD_CONTROL_REG
,
136 .enable_mask
= AS3722_SDn_CTRL(3),
137 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL1_REG
,
138 .sleep_ctrl_mask
= AS3722_SD3_EXT_ENABLE_MASK
,
139 .control_reg
= AS3722_SD23_CONTROL_REG
,
140 .mode_mask
= AS3722_SD3_MODE_FAST
,
141 .n_voltages
= AS3722_SD2_VSEL_MAX
+ 1,
144 .regulator_id
= AS3722_REGULATOR_ID_SD4
,
145 .name
= "as3722-sd4",
147 .vsel_reg
= AS3722_SD4_VOLTAGE_REG
,
148 .vsel_mask
= AS3722_SD_VSEL_MASK
,
149 .enable_reg
= AS3722_SD_CONTROL_REG
,
150 .enable_mask
= AS3722_SDn_CTRL(4),
151 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL2_REG
,
152 .sleep_ctrl_mask
= AS3722_SD4_EXT_ENABLE_MASK
,
153 .control_reg
= AS3722_SD4_CONTROL_REG
,
154 .mode_mask
= AS3722_SD4_MODE_FAST
,
155 .n_voltages
= AS3722_SD2_VSEL_MAX
+ 1,
158 .regulator_id
= AS3722_REGULATOR_ID_SD5
,
159 .name
= "as3722-sd5",
161 .vsel_reg
= AS3722_SD5_VOLTAGE_REG
,
162 .vsel_mask
= AS3722_SD_VSEL_MASK
,
163 .enable_reg
= AS3722_SD_CONTROL_REG
,
164 .enable_mask
= AS3722_SDn_CTRL(5),
165 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL2_REG
,
166 .sleep_ctrl_mask
= AS3722_SD5_EXT_ENABLE_MASK
,
167 .control_reg
= AS3722_SD5_CONTROL_REG
,
168 .mode_mask
= AS3722_SD5_MODE_FAST
,
169 .n_voltages
= AS3722_SD2_VSEL_MAX
+ 1,
172 .regulator_id
= AS3722_REGULATOR_ID_SD6
,
173 .name
= "as3722-sd6",
174 .vsel_reg
= AS3722_SD6_VOLTAGE_REG
,
175 .vsel_mask
= AS3722_SD_VSEL_MASK
,
176 .enable_reg
= AS3722_SD_CONTROL_REG
,
177 .enable_mask
= AS3722_SDn_CTRL(6),
178 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL2_REG
,
179 .sleep_ctrl_mask
= AS3722_SD6_EXT_ENABLE_MASK
,
180 .control_reg
= AS3722_SD6_CONTROL_REG
,
181 .mode_mask
= AS3722_SD6_MODE_FAST
,
184 .regulator_id
= AS3722_REGULATOR_ID_LDO0
,
185 .name
= "as3722-ldo0",
187 .vsel_reg
= AS3722_LDO0_VOLTAGE_REG
,
188 .vsel_mask
= AS3722_LDO0_VSEL_MASK
,
189 .enable_reg
= AS3722_LDOCONTROL0_REG
,
190 .enable_mask
= AS3722_LDO0_CTRL
,
191 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL3_REG
,
192 .sleep_ctrl_mask
= AS3722_LDO0_EXT_ENABLE_MASK
,
193 .n_voltages
= AS3722_LDO0_NUM_VOLT
,
196 .regulator_id
= AS3722_REGULATOR_ID_LDO1
,
197 .name
= "as3722-ldo1",
198 .sname
= "vin-ldo1-6",
199 .vsel_reg
= AS3722_LDO1_VOLTAGE_REG
,
200 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
201 .enable_reg
= AS3722_LDOCONTROL0_REG
,
202 .enable_mask
= AS3722_LDO1_CTRL
,
203 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL3_REG
,
204 .sleep_ctrl_mask
= AS3722_LDO1_EXT_ENABLE_MASK
,
205 .n_voltages
= AS3722_LDO_NUM_VOLT
,
208 .regulator_id
= AS3722_REGULATOR_ID_LDO2
,
209 .name
= "as3722-ldo2",
210 .sname
= "vin-ldo2-5-7",
211 .vsel_reg
= AS3722_LDO2_VOLTAGE_REG
,
212 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
213 .enable_reg
= AS3722_LDOCONTROL0_REG
,
214 .enable_mask
= AS3722_LDO2_CTRL
,
215 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL3_REG
,
216 .sleep_ctrl_mask
= AS3722_LDO2_EXT_ENABLE_MASK
,
217 .n_voltages
= AS3722_LDO_NUM_VOLT
,
220 .regulator_id
= AS3722_REGULATOR_ID_LDO3
,
221 .name
= "as3722-ldo3",
222 .sname
= "vin-ldo3-4",
223 .vsel_reg
= AS3722_LDO3_VOLTAGE_REG
,
224 .vsel_mask
= AS3722_LDO3_VSEL_MASK
,
225 .enable_reg
= AS3722_LDOCONTROL0_REG
,
226 .enable_mask
= AS3722_LDO3_CTRL
,
227 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL3_REG
,
228 .sleep_ctrl_mask
= AS3722_LDO3_EXT_ENABLE_MASK
,
229 .n_voltages
= AS3722_LDO3_NUM_VOLT
,
232 .regulator_id
= AS3722_REGULATOR_ID_LDO4
,
233 .name
= "as3722-ldo4",
234 .sname
= "vin-ldo3-4",
235 .vsel_reg
= AS3722_LDO4_VOLTAGE_REG
,
236 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
237 .enable_reg
= AS3722_LDOCONTROL0_REG
,
238 .enable_mask
= AS3722_LDO4_CTRL
,
239 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL4_REG
,
240 .sleep_ctrl_mask
= AS3722_LDO4_EXT_ENABLE_MASK
,
241 .n_voltages
= AS3722_LDO_NUM_VOLT
,
244 .regulator_id
= AS3722_REGULATOR_ID_LDO5
,
245 .name
= "as3722-ldo5",
246 .sname
= "vin-ldo2-5-7",
247 .vsel_reg
= AS3722_LDO5_VOLTAGE_REG
,
248 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
249 .enable_reg
= AS3722_LDOCONTROL0_REG
,
250 .enable_mask
= AS3722_LDO5_CTRL
,
251 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL4_REG
,
252 .sleep_ctrl_mask
= AS3722_LDO5_EXT_ENABLE_MASK
,
253 .n_voltages
= AS3722_LDO_NUM_VOLT
,
256 .regulator_id
= AS3722_REGULATOR_ID_LDO6
,
257 .name
= "as3722-ldo6",
258 .sname
= "vin-ldo1-6",
259 .vsel_reg
= AS3722_LDO6_VOLTAGE_REG
,
260 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
261 .enable_reg
= AS3722_LDOCONTROL0_REG
,
262 .enable_mask
= AS3722_LDO6_CTRL
,
263 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL4_REG
,
264 .sleep_ctrl_mask
= AS3722_LDO6_EXT_ENABLE_MASK
,
265 .n_voltages
= AS3722_LDO_NUM_VOLT
,
268 .regulator_id
= AS3722_REGULATOR_ID_LDO7
,
269 .name
= "as3722-ldo7",
270 .sname
= "vin-ldo2-5-7",
271 .vsel_reg
= AS3722_LDO7_VOLTAGE_REG
,
272 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
273 .enable_reg
= AS3722_LDOCONTROL0_REG
,
274 .enable_mask
= AS3722_LDO7_CTRL
,
275 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL4_REG
,
276 .sleep_ctrl_mask
= AS3722_LDO7_EXT_ENABLE_MASK
,
277 .n_voltages
= AS3722_LDO_NUM_VOLT
,
280 .regulator_id
= AS3722_REGULATOR_ID_LDO9
,
281 .name
= "as3722-ldo9",
282 .sname
= "vin-ldo9-10",
283 .vsel_reg
= AS3722_LDO9_VOLTAGE_REG
,
284 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
285 .enable_reg
= AS3722_LDOCONTROL1_REG
,
286 .enable_mask
= AS3722_LDO9_CTRL
,
287 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL5_REG
,
288 .sleep_ctrl_mask
= AS3722_LDO9_EXT_ENABLE_MASK
,
289 .n_voltages
= AS3722_LDO_NUM_VOLT
,
292 .regulator_id
= AS3722_REGULATOR_ID_LDO10
,
293 .name
= "as3722-ldo10",
294 .sname
= "vin-ldo9-10",
295 .vsel_reg
= AS3722_LDO10_VOLTAGE_REG
,
296 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
297 .enable_reg
= AS3722_LDOCONTROL1_REG
,
298 .enable_mask
= AS3722_LDO10_CTRL
,
299 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL5_REG
,
300 .sleep_ctrl_mask
= AS3722_LDO10_EXT_ENABLE_MASK
,
301 .n_voltages
= AS3722_LDO_NUM_VOLT
,
304 .regulator_id
= AS3722_REGULATOR_ID_LDO11
,
305 .name
= "as3722-ldo11",
306 .sname
= "vin-ldo11",
307 .vsel_reg
= AS3722_LDO11_VOLTAGE_REG
,
308 .vsel_mask
= AS3722_LDO_VSEL_MASK
,
309 .enable_reg
= AS3722_LDOCONTROL1_REG
,
310 .enable_mask
= AS3722_LDO11_CTRL
,
311 .sleep_ctrl_reg
= AS3722_ENABLE_CTRL5_REG
,
312 .sleep_ctrl_mask
= AS3722_LDO11_EXT_ENABLE_MASK
,
313 .n_voltages
= AS3722_LDO_NUM_VOLT
,
318 static const int as3722_ldo_current
[] = { 150000, 300000 };
319 static const int as3722_sd016_current
[] = { 2500000, 3000000, 3500000 };
321 static int as3722_current_to_index(int min_uA
, int max_uA
,
322 const int *curr_table
, int n_currents
)
326 for (i
= n_currents
- 1; i
>= 0; i
--) {
327 if ((min_uA
<= curr_table
[i
]) && (curr_table
[i
] <= max_uA
))
333 static int as3722_ldo_get_current_limit(struct regulator_dev
*rdev
)
335 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
336 struct as3722
*as3722
= as3722_regs
->as3722
;
337 int id
= rdev_get_id(rdev
);
341 ret
= as3722_read(as3722
, as3722_reg_lookup
[id
].vsel_reg
, &val
);
343 dev_err(as3722_regs
->dev
, "Reg 0x%02x read failed: %d\n",
344 as3722_reg_lookup
[id
].vsel_reg
, ret
);
347 if (val
& AS3722_LDO_ILIMIT_MASK
)
352 static int as3722_ldo_set_current_limit(struct regulator_dev
*rdev
,
353 int min_uA
, int max_uA
)
355 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
356 struct as3722
*as3722
= as3722_regs
->as3722
;
357 int id
= rdev_get_id(rdev
);
361 ret
= as3722_current_to_index(min_uA
, max_uA
, as3722_ldo_current
,
362 ARRAY_SIZE(as3722_ldo_current
));
364 dev_err(as3722_regs
->dev
,
365 "Current range min:max = %d:%d does not support\n",
370 reg
= AS3722_LDO_ILIMIT_BIT
;
371 return as3722_update_bits(as3722
, as3722_reg_lookup
[id
].vsel_reg
,
372 AS3722_LDO_ILIMIT_MASK
, reg
);
375 static const struct regulator_ops as3722_ldo0_ops
= {
376 .is_enabled
= regulator_is_enabled_regmap
,
377 .enable
= regulator_enable_regmap
,
378 .disable
= regulator_disable_regmap
,
379 .list_voltage
= regulator_list_voltage_linear
,
380 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
381 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
382 .get_current_limit
= as3722_ldo_get_current_limit
,
383 .set_current_limit
= as3722_ldo_set_current_limit
,
386 static const struct regulator_ops as3722_ldo0_extcntrl_ops
= {
387 .list_voltage
= regulator_list_voltage_linear
,
388 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
389 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
390 .get_current_limit
= as3722_ldo_get_current_limit
,
391 .set_current_limit
= as3722_ldo_set_current_limit
,
394 static int as3722_ldo3_set_tracking_mode(struct as3722_regulators
*as3722_reg
,
397 struct as3722
*as3722
= as3722_reg
->as3722
;
400 case AS3722_LDO3_MODE_PMOS
:
401 case AS3722_LDO3_MODE_PMOS_TRACKING
:
402 case AS3722_LDO3_MODE_NMOS
:
403 case AS3722_LDO3_MODE_SWITCH
:
404 return as3722_update_bits(as3722
,
405 as3722_reg_lookup
[id
].vsel_reg
,
406 AS3722_LDO3_MODE_MASK
, mode
);
413 static int as3722_ldo3_get_current_limit(struct regulator_dev
*rdev
)
418 static const struct regulator_ops as3722_ldo3_ops
= {
419 .is_enabled
= regulator_is_enabled_regmap
,
420 .enable
= regulator_enable_regmap
,
421 .disable
= regulator_disable_regmap
,
422 .list_voltage
= regulator_list_voltage_linear
,
423 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
424 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
425 .get_current_limit
= as3722_ldo3_get_current_limit
,
428 static const struct regulator_ops as3722_ldo3_extcntrl_ops
= {
429 .list_voltage
= regulator_list_voltage_linear
,
430 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
431 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
432 .get_current_limit
= as3722_ldo3_get_current_limit
,
435 static const struct regulator_ops as3722_ldo6_ops
= {
436 .is_enabled
= regulator_is_enabled_regmap
,
437 .enable
= regulator_enable_regmap
,
438 .disable
= regulator_disable_regmap
,
439 .map_voltage
= regulator_map_voltage_linear_range
,
440 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
441 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
442 .list_voltage
= regulator_list_voltage_linear_range
,
443 .get_current_limit
= as3722_ldo_get_current_limit
,
444 .set_current_limit
= as3722_ldo_set_current_limit
,
445 .get_bypass
= regulator_get_bypass_regmap
,
446 .set_bypass
= regulator_set_bypass_regmap
,
449 static const struct regulator_ops as3722_ldo6_extcntrl_ops
= {
450 .map_voltage
= regulator_map_voltage_linear_range
,
451 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
452 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
453 .list_voltage
= regulator_list_voltage_linear_range
,
454 .get_current_limit
= as3722_ldo_get_current_limit
,
455 .set_current_limit
= as3722_ldo_set_current_limit
,
456 .get_bypass
= regulator_get_bypass_regmap
,
457 .set_bypass
= regulator_set_bypass_regmap
,
460 static const struct regulator_linear_range as3722_ldo_ranges
[] = {
461 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
462 REGULATOR_LINEAR_RANGE(825000, 0x01, 0x24, 25000),
463 REGULATOR_LINEAR_RANGE(1725000, 0x40, 0x7F, 25000),
466 static const struct regulator_ops as3722_ldo_ops
= {
467 .is_enabled
= regulator_is_enabled_regmap
,
468 .enable
= regulator_enable_regmap
,
469 .disable
= regulator_disable_regmap
,
470 .map_voltage
= regulator_map_voltage_linear_range
,
471 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
472 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
473 .list_voltage
= regulator_list_voltage_linear_range
,
474 .get_current_limit
= as3722_ldo_get_current_limit
,
475 .set_current_limit
= as3722_ldo_set_current_limit
,
478 static const struct regulator_ops as3722_ldo_extcntrl_ops
= {
479 .map_voltage
= regulator_map_voltage_linear_range
,
480 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
481 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
482 .list_voltage
= regulator_list_voltage_linear_range
,
483 .get_current_limit
= as3722_ldo_get_current_limit
,
484 .set_current_limit
= as3722_ldo_set_current_limit
,
487 static unsigned int as3722_sd_get_mode(struct regulator_dev
*rdev
)
489 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
490 struct as3722
*as3722
= as3722_regs
->as3722
;
491 int id
= rdev_get_id(rdev
);
495 if (!as3722_reg_lookup
[id
].control_reg
)
498 ret
= as3722_read(as3722
, as3722_reg_lookup
[id
].control_reg
, &val
);
500 dev_err(as3722_regs
->dev
, "Reg 0x%02x read failed: %d\n",
501 as3722_reg_lookup
[id
].control_reg
, ret
);
505 if (val
& as3722_reg_lookup
[id
].mode_mask
)
506 return REGULATOR_MODE_FAST
;
508 return REGULATOR_MODE_NORMAL
;
511 static int as3722_sd_set_mode(struct regulator_dev
*rdev
,
514 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
515 struct as3722
*as3722
= as3722_regs
->as3722
;
516 u8 id
= rdev_get_id(rdev
);
520 if (!as3722_reg_lookup
[id
].control_reg
)
524 case REGULATOR_MODE_FAST
:
525 val
= as3722_reg_lookup
[id
].mode_mask
;
526 case REGULATOR_MODE_NORMAL
: /* fall down */
532 ret
= as3722_update_bits(as3722
, as3722_reg_lookup
[id
].control_reg
,
533 as3722_reg_lookup
[id
].mode_mask
, val
);
535 dev_err(as3722_regs
->dev
, "Reg 0x%02x update failed: %d\n",
536 as3722_reg_lookup
[id
].control_reg
, ret
);
542 static int as3722_sd016_get_current_limit(struct regulator_dev
*rdev
)
544 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
545 struct as3722
*as3722
= as3722_regs
->as3722
;
546 int id
= rdev_get_id(rdev
);
552 case AS3722_REGULATOR_ID_SD0
:
553 reg
= AS3722_OVCURRENT_REG
;
554 mask
= AS3722_OVCURRENT_SD0_TRIP_MASK
;
556 case AS3722_REGULATOR_ID_SD1
:
557 reg
= AS3722_OVCURRENT_REG
;
558 mask
= AS3722_OVCURRENT_SD1_TRIP_MASK
;
560 case AS3722_REGULATOR_ID_SD6
:
561 reg
= AS3722_OVCURRENT_DEB_REG
;
562 mask
= AS3722_OVCURRENT_SD6_TRIP_MASK
;
567 ret
= as3722_read(as3722
, reg
, &val
);
569 dev_err(as3722_regs
->dev
, "Reg 0x%02x read failed: %d\n",
574 val
>>= ffs(mask
) - 1;
577 return as3722_sd016_current
[val
];
580 static int as3722_sd016_set_current_limit(struct regulator_dev
*rdev
,
581 int min_uA
, int max_uA
)
583 struct as3722_regulators
*as3722_regs
= rdev_get_drvdata(rdev
);
584 struct as3722
*as3722
= as3722_regs
->as3722
;
585 int id
= rdev_get_id(rdev
);
591 ret
= as3722_current_to_index(min_uA
, max_uA
, as3722_sd016_current
,
592 ARRAY_SIZE(as3722_sd016_current
));
594 dev_err(as3722_regs
->dev
,
595 "Current range min:max = %d:%d does not support\n",
601 case AS3722_REGULATOR_ID_SD0
:
602 reg
= AS3722_OVCURRENT_REG
;
603 mask
= AS3722_OVCURRENT_SD0_TRIP_MASK
;
605 case AS3722_REGULATOR_ID_SD1
:
606 reg
= AS3722_OVCURRENT_REG
;
607 mask
= AS3722_OVCURRENT_SD1_TRIP_MASK
;
609 case AS3722_REGULATOR_ID_SD6
:
610 reg
= AS3722_OVCURRENT_DEB_REG
;
611 mask
= AS3722_OVCURRENT_SD6_TRIP_MASK
;
616 ret
<<= ffs(mask
) - 1;
618 return as3722_update_bits(as3722
, reg
, mask
, val
);
621 static bool as3722_sd0_is_low_voltage(struct as3722_regulators
*as3722_regs
)
626 err
= as3722_read(as3722_regs
->as3722
, AS3722_FUSE7_REG
, &val
);
628 dev_err(as3722_regs
->dev
, "Reg 0x%02x read failed: %d\n",
629 AS3722_FUSE7_REG
, err
);
632 if (val
& AS3722_FUSE7_SD0_LOW_VOLTAGE
)
637 static const struct regulator_linear_range as3722_sd2345_ranges
[] = {
638 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
639 REGULATOR_LINEAR_RANGE(612500, 0x01, 0x40, 12500),
640 REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
641 REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7F, 50000),
644 static const struct regulator_ops as3722_sd016_ops
= {
645 .is_enabled
= regulator_is_enabled_regmap
,
646 .enable
= regulator_enable_regmap
,
647 .disable
= regulator_disable_regmap
,
648 .list_voltage
= regulator_list_voltage_linear
,
649 .map_voltage
= regulator_map_voltage_linear
,
650 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
651 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
652 .get_current_limit
= as3722_sd016_get_current_limit
,
653 .set_current_limit
= as3722_sd016_set_current_limit
,
654 .get_mode
= as3722_sd_get_mode
,
655 .set_mode
= as3722_sd_set_mode
,
658 static const struct regulator_ops as3722_sd016_extcntrl_ops
= {
659 .list_voltage
= regulator_list_voltage_linear
,
660 .map_voltage
= regulator_map_voltage_linear
,
661 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
662 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
663 .get_current_limit
= as3722_sd016_get_current_limit
,
664 .set_current_limit
= as3722_sd016_set_current_limit
,
665 .get_mode
= as3722_sd_get_mode
,
666 .set_mode
= as3722_sd_set_mode
,
669 static const struct regulator_ops as3722_sd2345_ops
= {
670 .is_enabled
= regulator_is_enabled_regmap
,
671 .enable
= regulator_enable_regmap
,
672 .disable
= regulator_disable_regmap
,
673 .list_voltage
= regulator_list_voltage_linear_range
,
674 .map_voltage
= regulator_map_voltage_linear_range
,
675 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
676 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
677 .get_mode
= as3722_sd_get_mode
,
678 .set_mode
= as3722_sd_set_mode
,
681 static const struct regulator_ops as3722_sd2345_extcntrl_ops
= {
682 .list_voltage
= regulator_list_voltage_linear_range
,
683 .map_voltage
= regulator_map_voltage_linear_range
,
684 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
685 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
686 .get_mode
= as3722_sd_get_mode
,
687 .set_mode
= as3722_sd_set_mode
,
690 static int as3722_extreg_init(struct as3722_regulators
*as3722_regs
, int id
,
696 if ((ext_pwr_ctrl
< AS3722_EXT_CONTROL_ENABLE1
) ||
697 (ext_pwr_ctrl
> AS3722_EXT_CONTROL_ENABLE3
))
700 val
= ext_pwr_ctrl
<< (ffs(as3722_reg_lookup
[id
].sleep_ctrl_mask
) - 1);
701 ret
= as3722_update_bits(as3722_regs
->as3722
,
702 as3722_reg_lookup
[id
].sleep_ctrl_reg
,
703 as3722_reg_lookup
[id
].sleep_ctrl_mask
, val
);
705 dev_err(as3722_regs
->dev
, "Reg 0x%02x update failed: %d\n",
706 as3722_reg_lookup
[id
].sleep_ctrl_reg
, ret
);
710 static struct of_regulator_match as3722_regulator_matches
[] = {
727 { .name
= "ldo10", },
728 { .name
= "ldo11", },
731 static int as3722_get_regulator_dt_data(struct platform_device
*pdev
,
732 struct as3722_regulators
*as3722_regs
)
734 struct device_node
*np
;
735 struct as3722_regulator_config_data
*reg_config
;
740 np
= of_get_child_by_name(pdev
->dev
.parent
->of_node
, "regulators");
742 dev_err(&pdev
->dev
, "Device is not having regulators node\n");
745 pdev
->dev
.of_node
= np
;
747 ret
= of_regulator_match(&pdev
->dev
, np
, as3722_regulator_matches
,
748 ARRAY_SIZE(as3722_regulator_matches
));
751 dev_err(&pdev
->dev
, "Parsing of regulator node failed: %d\n",
756 for (id
= 0; id
< ARRAY_SIZE(as3722_regulator_matches
); ++id
) {
757 struct device_node
*reg_node
;
759 reg_config
= &as3722_regs
->reg_config_data
[id
];
760 reg_config
->reg_init
= as3722_regulator_matches
[id
].init_data
;
761 reg_node
= as3722_regulator_matches
[id
].of_node
;
763 if (!reg_config
->reg_init
|| !reg_node
)
766 ret
= of_property_read_u32(reg_node
, "ams,ext-control", &prop
);
769 reg_config
->ext_control
= prop
;
772 "ext-control have invalid option: %u\n",
775 reg_config
->enable_tracking
=
776 of_property_read_bool(reg_node
, "ams,enable-tracking");
781 static int as3722_regulator_probe(struct platform_device
*pdev
)
783 struct as3722
*as3722
= dev_get_drvdata(pdev
->dev
.parent
);
784 struct as3722_regulators
*as3722_regs
;
785 struct as3722_regulator_config_data
*reg_config
;
786 struct regulator_dev
*rdev
;
787 struct regulator_config config
= { };
788 const struct regulator_ops
*ops
;
792 as3722_regs
= devm_kzalloc(&pdev
->dev
, sizeof(*as3722_regs
),
797 as3722_regs
->dev
= &pdev
->dev
;
798 as3722_regs
->as3722
= as3722
;
799 platform_set_drvdata(pdev
, as3722_regs
);
801 ret
= as3722_get_regulator_dt_data(pdev
, as3722_regs
);
805 config
.dev
= &pdev
->dev
;
806 config
.driver_data
= as3722_regs
;
807 config
.regmap
= as3722
->regmap
;
809 for (id
= 0; id
< AS3722_REGULATOR_ID_MAX
; id
++) {
810 reg_config
= &as3722_regs
->reg_config_data
[id
];
812 as3722_regs
->desc
[id
].name
= as3722_reg_lookup
[id
].name
;
813 as3722_regs
->desc
[id
].supply_name
= as3722_reg_lookup
[id
].sname
;
814 as3722_regs
->desc
[id
].id
= as3722_reg_lookup
[id
].regulator_id
;
815 as3722_regs
->desc
[id
].n_voltages
=
816 as3722_reg_lookup
[id
].n_voltages
;
817 as3722_regs
->desc
[id
].type
= REGULATOR_VOLTAGE
;
818 as3722_regs
->desc
[id
].owner
= THIS_MODULE
;
819 as3722_regs
->desc
[id
].enable_reg
=
820 as3722_reg_lookup
[id
].enable_reg
;
821 as3722_regs
->desc
[id
].enable_mask
=
822 as3722_reg_lookup
[id
].enable_mask
;
823 as3722_regs
->desc
[id
].vsel_reg
= as3722_reg_lookup
[id
].vsel_reg
;
824 as3722_regs
->desc
[id
].vsel_mask
=
825 as3722_reg_lookup
[id
].vsel_mask
;
827 case AS3722_REGULATOR_ID_LDO0
:
828 if (reg_config
->ext_control
)
829 ops
= &as3722_ldo0_extcntrl_ops
;
831 ops
= &as3722_ldo0_ops
;
832 as3722_regs
->desc
[id
].min_uV
= 825000;
833 as3722_regs
->desc
[id
].uV_step
= 25000;
834 as3722_regs
->desc
[id
].linear_min_sel
= 1;
835 as3722_regs
->desc
[id
].enable_time
= 500;
837 case AS3722_REGULATOR_ID_LDO3
:
838 if (reg_config
->ext_control
)
839 ops
= &as3722_ldo3_extcntrl_ops
;
841 ops
= &as3722_ldo3_ops
;
842 as3722_regs
->desc
[id
].min_uV
= 620000;
843 as3722_regs
->desc
[id
].uV_step
= 20000;
844 as3722_regs
->desc
[id
].linear_min_sel
= 1;
845 as3722_regs
->desc
[id
].enable_time
= 500;
846 if (reg_config
->enable_tracking
) {
847 ret
= as3722_ldo3_set_tracking_mode(as3722_regs
,
848 id
, AS3722_LDO3_MODE_PMOS_TRACKING
);
851 "LDO3 tracking failed: %d\n",
857 case AS3722_REGULATOR_ID_LDO6
:
858 if (reg_config
->ext_control
)
859 ops
= &as3722_ldo6_extcntrl_ops
;
861 ops
= &as3722_ldo6_ops
;
862 as3722_regs
->desc
[id
].enable_time
= 500;
863 as3722_regs
->desc
[id
].bypass_reg
=
864 AS3722_LDO6_VOLTAGE_REG
;
865 as3722_regs
->desc
[id
].bypass_mask
=
866 AS3722_LDO_VSEL_MASK
;
867 as3722_regs
->desc
[id
].bypass_val_on
=
868 AS3722_LDO6_VSEL_BYPASS
;
869 as3722_regs
->desc
[id
].bypass_val_off
=
870 AS3722_LDO6_VSEL_BYPASS
;
871 as3722_regs
->desc
[id
].linear_ranges
= as3722_ldo_ranges
;
872 as3722_regs
->desc
[id
].n_linear_ranges
=
873 ARRAY_SIZE(as3722_ldo_ranges
);
875 case AS3722_REGULATOR_ID_SD0
:
876 case AS3722_REGULATOR_ID_SD1
:
877 case AS3722_REGULATOR_ID_SD6
:
878 if (reg_config
->ext_control
)
879 ops
= &as3722_sd016_extcntrl_ops
;
881 ops
= &as3722_sd016_ops
;
882 if (id
== AS3722_REGULATOR_ID_SD0
&&
883 as3722_sd0_is_low_voltage(as3722_regs
)) {
884 as3722_regs
->desc
[id
].n_voltages
=
885 AS3722_SD0_VSEL_LOW_VOL_MAX
+ 1;
886 as3722_regs
->desc
[id
].min_uV
= 410000;
888 as3722_regs
->desc
[id
].n_voltages
=
889 AS3722_SD0_VSEL_MAX
+ 1,
890 as3722_regs
->desc
[id
].min_uV
= 610000;
892 as3722_regs
->desc
[id
].uV_step
= 10000;
893 as3722_regs
->desc
[id
].linear_min_sel
= 1;
894 as3722_regs
->desc
[id
].enable_time
= 600;
896 case AS3722_REGULATOR_ID_SD2
:
897 case AS3722_REGULATOR_ID_SD3
:
898 case AS3722_REGULATOR_ID_SD4
:
899 case AS3722_REGULATOR_ID_SD5
:
900 if (reg_config
->ext_control
)
901 ops
= &as3722_sd2345_extcntrl_ops
;
903 ops
= &as3722_sd2345_ops
;
904 as3722_regs
->desc
[id
].linear_ranges
=
905 as3722_sd2345_ranges
;
906 as3722_regs
->desc
[id
].n_linear_ranges
=
907 ARRAY_SIZE(as3722_sd2345_ranges
);
910 if (reg_config
->ext_control
)
911 ops
= &as3722_ldo_extcntrl_ops
;
913 ops
= &as3722_ldo_ops
;
914 as3722_regs
->desc
[id
].enable_time
= 500;
915 as3722_regs
->desc
[id
].linear_ranges
= as3722_ldo_ranges
;
916 as3722_regs
->desc
[id
].n_linear_ranges
=
917 ARRAY_SIZE(as3722_ldo_ranges
);
920 as3722_regs
->desc
[id
].ops
= ops
;
921 config
.init_data
= reg_config
->reg_init
;
922 config
.of_node
= as3722_regulator_matches
[id
].of_node
;
923 rdev
= devm_regulator_register(&pdev
->dev
,
924 &as3722_regs
->desc
[id
], &config
);
927 dev_err(&pdev
->dev
, "regulator %d register failed %d\n",
932 as3722_regs
->rdevs
[id
] = rdev
;
933 if (reg_config
->ext_control
) {
934 ret
= regulator_enable_regmap(rdev
);
937 "Regulator %d enable failed: %d\n",
941 ret
= as3722_extreg_init(as3722_regs
, id
,
942 reg_config
->ext_control
);
945 "AS3722 ext control failed: %d", ret
);
953 static const struct of_device_id of_as3722_regulator_match
[] = {
954 { .compatible
= "ams,as3722-regulator", },
957 MODULE_DEVICE_TABLE(of
, of_as3722_regulator_match
);
959 static struct platform_driver as3722_regulator_driver
= {
961 .name
= "as3722-regulator",
962 .of_match_table
= of_as3722_regulator_match
,
964 .probe
= as3722_regulator_probe
,
967 module_platform_driver(as3722_regulator_driver
);
969 MODULE_ALIAS("platform:as3722-regulator");
970 MODULE_DESCRIPTION("AS3722 regulator driver");
971 MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
972 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
973 MODULE_LICENSE("GPL");