blob: 66337e12719b40c684d614ccee4c921e71175d06 [file] [log] [blame]
Laxman Dewanganbc407332013-09-20 18:00:13 +05301/*
2 * Voltage regulator support for AMS AS3722 PMIC
3 *
4 * Copyright (C) 2013 ams
5 *
6 * Author: Florian Lobmaier <florian.lobmaier@ams.com>
7 * Author: Laxman Dewangan <ldewangan@nvidia.com>
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25#include <linux/err.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/mfd/as3722.h>
29#include <linux/of.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>
36
37/* Regulator IDs */
38enum 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,
58};
59
60struct as3722_register_mapping {
61 u8 regulator_id;
62 const char *name;
63 const char *sname;
64 u8 vsel_reg;
65 u8 vsel_mask;
66 int n_voltages;
67 u32 enable_reg;
68 u8 enable_mask;
69 u32 control_reg;
70 u8 mode_mask;
71 u32 sleep_ctrl_reg;
72 u8 sleep_ctrl_mask;
73};
74
75struct as3722_regulator_config_data {
76 struct regulator_init_data *reg_init;
77 bool enable_tracking;
78 int ext_control;
79};
80
81struct as3722_regulators {
82 struct device *dev;
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];
88};
89
90static const struct as3722_register_mapping as3722_reg_lookup[] = {
91 {
92 .regulator_id = AS3722_REGULATOR_ID_SD0,
93 .name = "as3722-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,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530102 },
103 {
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,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530114 },
115 {
116 .regulator_id = AS3722_REGULATOR_ID_SD2,
117 .name = "as3722-sd2",
118 .sname = "vsup-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,
Axel Lin5e965702013-10-02 20:49:29 +0800127 .n_voltages = AS3722_SD2_VSEL_MAX + 1,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530128 },
129 {
130 .regulator_id = AS3722_REGULATOR_ID_SD3,
131 .name = "as3722-sd3",
132 .sname = "vsup-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,
Axel Lin5e965702013-10-02 20:49:29 +0800141 .n_voltages = AS3722_SD2_VSEL_MAX + 1,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530142 },
143 {
144 .regulator_id = AS3722_REGULATOR_ID_SD4,
145 .name = "as3722-sd4",
146 .sname = "vsup-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,
Axel Lin5e965702013-10-02 20:49:29 +0800155 .n_voltages = AS3722_SD2_VSEL_MAX + 1,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530156 },
157 {
158 .regulator_id = AS3722_REGULATOR_ID_SD5,
159 .name = "as3722-sd5",
160 .sname = "vsup-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,
Axel Lin5e965702013-10-02 20:49:29 +0800169 .n_voltages = AS3722_SD2_VSEL_MAX + 1,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530170 },
171 {
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,
Laxman Dewanganbc407332013-09-20 18:00:13 +0530182 },
183 {
184 .regulator_id = AS3722_REGULATOR_ID_LDO0,
185 .name = "as3722-ldo0",
186 .sname = "vin-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,
194 },
195 {
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,
206 },
207 {
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,
218 },
219 {
220 .regulator_id = AS3722_REGULATOR_ID_LDO3,
221 .name = "as3722-ldo3",
Sachin Kamata181c1c2014-06-24 13:54:43 +0530222 .sname = "vin-ldo3-4",
Laxman Dewanganbc407332013-09-20 18:00:13 +0530223 .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,
230 },
231 {
232 .regulator_id = AS3722_REGULATOR_ID_LDO4,
233 .name = "as3722-ldo4",
Sachin Kamata181c1c2014-06-24 13:54:43 +0530234 .sname = "vin-ldo3-4",
Laxman Dewanganbc407332013-09-20 18:00:13 +0530235 .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,
242 },
243 {
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,
254 },
255 {
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,
266 },
267 {
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,
278 },
279 {
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,
290 },
291 {
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,
302 },
303 {
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,
314 },
315};
316
317
318static const int as3722_ldo_current[] = { 150000, 300000 };
319static const int as3722_sd016_current[] = { 2500000, 3000000, 3500000 };
320
321static int as3722_current_to_index(int min_uA, int max_uA,
322 const int *curr_table, int n_currents)
323{
324 int i;
325
326 for (i = n_currents - 1; i >= 0; i--) {
327 if ((min_uA <= curr_table[i]) && (curr_table[i] <= max_uA))
328 return i;
329 }
330 return -EINVAL;
331}
332
333static int as3722_ldo_get_current_limit(struct regulator_dev *rdev)
334{
335 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
336 struct as3722 *as3722 = as3722_regs->as3722;
337 int id = rdev_get_id(rdev);
338 u32 val;
339 int ret;
340
341 ret = as3722_read(as3722, as3722_reg_lookup[id].vsel_reg, &val);
342 if (ret < 0) {
343 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
344 as3722_reg_lookup[id].vsel_reg, ret);
345 return ret;
346 }
347 if (val & AS3722_LDO_ILIMIT_MASK)
348 return 300000;
349 return 150000;
350}
351
352static int as3722_ldo_set_current_limit(struct regulator_dev *rdev,
353 int min_uA, int max_uA)
354{
355 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
356 struct as3722 *as3722 = as3722_regs->as3722;
357 int id = rdev_get_id(rdev);
358 int ret;
359 u32 reg = 0;
360
361 ret = as3722_current_to_index(min_uA, max_uA, as3722_ldo_current,
362 ARRAY_SIZE(as3722_ldo_current));
363 if (ret < 0) {
364 dev_err(as3722_regs->dev,
365 "Current range min:max = %d:%d does not support\n",
366 min_uA, max_uA);
367 return ret;
368 }
369 if (ret)
370 reg = AS3722_LDO_ILIMIT_BIT;
371 return as3722_update_bits(as3722, as3722_reg_lookup[id].vsel_reg,
372 AS3722_LDO_ILIMIT_MASK, reg);
373}
374
Thierry Reding162c5a32016-04-07 16:22:39 +0200375static const struct regulator_ops as3722_ldo0_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530376 .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,
384};
385
Thierry Reding162c5a32016-04-07 16:22:39 +0200386static const struct regulator_ops as3722_ldo0_extcntrl_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530387 .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,
392};
393
394static int as3722_ldo3_set_tracking_mode(struct as3722_regulators *as3722_reg,
395 int id, u8 mode)
396{
397 struct as3722 *as3722 = as3722_reg->as3722;
398
399 switch (mode) {
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);
407
408 default:
409 return -EINVAL;
410 }
411}
412
413static int as3722_ldo3_get_current_limit(struct regulator_dev *rdev)
414{
415 return 150000;
416}
417
Thierry Reding162c5a32016-04-07 16:22:39 +0200418static const struct regulator_ops as3722_ldo3_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530419 .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,
426};
427
Thierry Reding162c5a32016-04-07 16:22:39 +0200428static const struct regulator_ops as3722_ldo3_extcntrl_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530429 .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,
433};
434
Thierry Reding162c5a32016-04-07 16:22:39 +0200435static const struct regulator_ops as3722_ldo6_ops = {
Jon Hunter40a86552016-04-07 16:22:38 +0200436 .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,
447};
448
Thierry Reding162c5a32016-04-07 16:22:39 +0200449static const struct regulator_ops as3722_ldo6_extcntrl_ops = {
Jon Hunter40a86552016-04-07 16:22:38 +0200450 .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,
458};
459
Laxman Dewanganbc407332013-09-20 18:00:13 +0530460static const struct regulator_linear_range as3722_ldo_ranges[] = {
Thierry Reding2050afd2014-06-03 17:17:32 +0200461 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
Axel Lin8828bae2013-10-11 09:32:18 +0800462 REGULATOR_LINEAR_RANGE(825000, 0x01, 0x24, 25000),
463 REGULATOR_LINEAR_RANGE(1725000, 0x40, 0x7F, 25000),
Laxman Dewanganbc407332013-09-20 18:00:13 +0530464};
465
Thierry Reding162c5a32016-04-07 16:22:39 +0200466static const struct regulator_ops as3722_ldo_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530467 .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,
476};
477
Thierry Reding162c5a32016-04-07 16:22:39 +0200478static const struct regulator_ops as3722_ldo_extcntrl_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530479 .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,
485};
486
487static unsigned int as3722_sd_get_mode(struct regulator_dev *rdev)
488{
489 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
490 struct as3722 *as3722 = as3722_regs->as3722;
491 int id = rdev_get_id(rdev);
492 u32 val;
493 int ret;
494
495 if (!as3722_reg_lookup[id].control_reg)
496 return -ENOTSUPP;
497
498 ret = as3722_read(as3722, as3722_reg_lookup[id].control_reg, &val);
499 if (ret < 0) {
500 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
501 as3722_reg_lookup[id].control_reg, ret);
502 return ret;
503 }
504
505 if (val & as3722_reg_lookup[id].mode_mask)
506 return REGULATOR_MODE_FAST;
507 else
508 return REGULATOR_MODE_NORMAL;
509}
510
511static int as3722_sd_set_mode(struct regulator_dev *rdev,
512 unsigned int mode)
513{
514 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
515 struct as3722 *as3722 = as3722_regs->as3722;
516 u8 id = rdev_get_id(rdev);
517 u8 val = 0;
518 int ret;
519
520 if (!as3722_reg_lookup[id].control_reg)
521 return -ERANGE;
522
523 switch (mode) {
524 case REGULATOR_MODE_FAST:
525 val = as3722_reg_lookup[id].mode_mask;
526 case REGULATOR_MODE_NORMAL: /* fall down */
527 break;
528 default:
529 return -EINVAL;
530 }
531
532 ret = as3722_update_bits(as3722, as3722_reg_lookup[id].control_reg,
533 as3722_reg_lookup[id].mode_mask, val);
534 if (ret < 0) {
535 dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
536 as3722_reg_lookup[id].control_reg, ret);
537 return ret;
538 }
539 return ret;
540}
541
542static int as3722_sd016_get_current_limit(struct regulator_dev *rdev)
543{
544 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
545 struct as3722 *as3722 = as3722_regs->as3722;
546 int id = rdev_get_id(rdev);
547 u32 val, reg;
548 int mask;
549 int ret;
550
551 switch (id) {
552 case AS3722_REGULATOR_ID_SD0:
553 reg = AS3722_OVCURRENT_REG;
554 mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
555 break;
556 case AS3722_REGULATOR_ID_SD1:
557 reg = AS3722_OVCURRENT_REG;
558 mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
559 break;
560 case AS3722_REGULATOR_ID_SD6:
561 reg = AS3722_OVCURRENT_DEB_REG;
562 mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
563 break;
564 default:
565 return -EINVAL;
566 }
567 ret = as3722_read(as3722, reg, &val);
568 if (ret < 0) {
569 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
570 reg, ret);
571 return ret;
572 }
573 val &= mask;
574 val >>= ffs(mask) - 1;
575 if (val == 3)
576 return -EINVAL;
577 return as3722_sd016_current[val];
578}
579
580static int as3722_sd016_set_current_limit(struct regulator_dev *rdev,
581 int min_uA, int max_uA)
582{
583 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
584 struct as3722 *as3722 = as3722_regs->as3722;
585 int id = rdev_get_id(rdev);
586 int ret;
587 int val;
588 int mask;
589 u32 reg;
590
591 ret = as3722_current_to_index(min_uA, max_uA, as3722_sd016_current,
592 ARRAY_SIZE(as3722_sd016_current));
593 if (ret < 0) {
594 dev_err(as3722_regs->dev,
595 "Current range min:max = %d:%d does not support\n",
596 min_uA, max_uA);
597 return ret;
598 }
599
600 switch (id) {
601 case AS3722_REGULATOR_ID_SD0:
602 reg = AS3722_OVCURRENT_REG;
603 mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
604 break;
605 case AS3722_REGULATOR_ID_SD1:
606 reg = AS3722_OVCURRENT_REG;
607 mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
608 break;
609 case AS3722_REGULATOR_ID_SD6:
610 reg = AS3722_OVCURRENT_DEB_REG;
611 mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
612 break;
613 default:
614 return -EINVAL;
615 }
Vince Hsu63890752013-11-28 19:10:42 +0800616 ret <<= ffs(mask) - 1;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530617 val = ret & mask;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530618 return as3722_update_bits(as3722, reg, mask, val);
619}
620
Andrew Bresticker762a8ee2013-12-26 13:48:46 -0800621static bool as3722_sd0_is_low_voltage(struct as3722_regulators *as3722_regs)
622{
623 int err;
624 unsigned val;
625
626 err = as3722_read(as3722_regs->as3722, AS3722_FUSE7_REG, &val);
627 if (err < 0) {
628 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
629 AS3722_FUSE7_REG, err);
630 return false;
631 }
632 if (val & AS3722_FUSE7_SD0_LOW_VOLTAGE)
633 return true;
634 return false;
635}
636
Laxman Dewanganbc407332013-09-20 18:00:13 +0530637static const struct regulator_linear_range as3722_sd2345_ranges[] = {
Thierry Reding2050afd2014-06-03 17:17:32 +0200638 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
Axel Lin8828bae2013-10-11 09:32:18 +0800639 REGULATOR_LINEAR_RANGE(612500, 0x01, 0x40, 12500),
640 REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
641 REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7F, 50000),
Laxman Dewanganbc407332013-09-20 18:00:13 +0530642};
643
Thierry Reding162c5a32016-04-07 16:22:39 +0200644static const struct regulator_ops as3722_sd016_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530645 .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,
656};
657
Thierry Reding162c5a32016-04-07 16:22:39 +0200658static const struct regulator_ops as3722_sd016_extcntrl_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530659 .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,
667};
668
Thierry Reding162c5a32016-04-07 16:22:39 +0200669static const struct regulator_ops as3722_sd2345_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530670 .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,
679};
680
Thierry Reding162c5a32016-04-07 16:22:39 +0200681static const struct regulator_ops as3722_sd2345_extcntrl_ops = {
Laxman Dewanganbc407332013-09-20 18:00:13 +0530682 .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,
688};
689
690static int as3722_extreg_init(struct as3722_regulators *as3722_regs, int id,
691 int ext_pwr_ctrl)
692{
693 int ret;
694 unsigned int val;
695
696 if ((ext_pwr_ctrl < AS3722_EXT_CONTROL_ENABLE1) ||
697 (ext_pwr_ctrl > AS3722_EXT_CONTROL_ENABLE3))
698 return -EINVAL;
699
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);
704 if (ret < 0)
705 dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
706 as3722_reg_lookup[id].sleep_ctrl_reg, ret);
707 return ret;
708}
709
710static struct of_regulator_match as3722_regulator_matches[] = {
711 { .name = "sd0", },
712 { .name = "sd1", },
713 { .name = "sd2", },
714 { .name = "sd3", },
715 { .name = "sd4", },
716 { .name = "sd5", },
717 { .name = "sd6", },
718 { .name = "ldo0", },
719 { .name = "ldo1", },
720 { .name = "ldo2", },
721 { .name = "ldo3", },
722 { .name = "ldo4", },
723 { .name = "ldo5", },
724 { .name = "ldo6", },
725 { .name = "ldo7", },
726 { .name = "ldo9", },
727 { .name = "ldo10", },
728 { .name = "ldo11", },
729};
730
731static int as3722_get_regulator_dt_data(struct platform_device *pdev,
732 struct as3722_regulators *as3722_regs)
733{
734 struct device_node *np;
735 struct as3722_regulator_config_data *reg_config;
736 u32 prop;
737 int id;
738 int ret;
739
740 np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
741 if (!np) {
742 dev_err(&pdev->dev, "Device is not having regulators node\n");
743 return -ENODEV;
744 }
745 pdev->dev.of_node = np;
746
747 ret = of_regulator_match(&pdev->dev, np, as3722_regulator_matches,
748 ARRAY_SIZE(as3722_regulator_matches));
Sachin Kamatba40e552014-02-17 14:33:30 +0530749 of_node_put(np);
Laxman Dewanganbc407332013-09-20 18:00:13 +0530750 if (ret < 0) {
751 dev_err(&pdev->dev, "Parsing of regulator node failed: %d\n",
752 ret);
753 return ret;
754 }
755
756 for (id = 0; id < ARRAY_SIZE(as3722_regulator_matches); ++id) {
757 struct device_node *reg_node;
758
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;
762
763 if (!reg_config->reg_init || !reg_node)
764 continue;
765
766 ret = of_property_read_u32(reg_node, "ams,ext-control", &prop);
767 if (!ret) {
768 if (prop < 3)
769 reg_config->ext_control = prop;
770 else
771 dev_warn(&pdev->dev,
772 "ext-control have invalid option: %u\n",
773 prop);
774 }
775 reg_config->enable_tracking =
776 of_property_read_bool(reg_node, "ams,enable-tracking");
777 }
778 return 0;
779}
780
781static int as3722_regulator_probe(struct platform_device *pdev)
782{
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 = { };
Thierry Reding162c5a32016-04-07 16:22:39 +0200788 const struct regulator_ops *ops;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530789 int id;
790 int ret;
791
792 as3722_regs = devm_kzalloc(&pdev->dev, sizeof(*as3722_regs),
793 GFP_KERNEL);
794 if (!as3722_regs)
795 return -ENOMEM;
796
797 as3722_regs->dev = &pdev->dev;
798 as3722_regs->as3722 = as3722;
799 platform_set_drvdata(pdev, as3722_regs);
800
801 ret = as3722_get_regulator_dt_data(pdev, as3722_regs);
802 if (ret < 0)
803 return ret;
804
805 config.dev = &pdev->dev;
806 config.driver_data = as3722_regs;
807 config.regmap = as3722->regmap;
808
809 for (id = 0; id < AS3722_REGULATOR_ID_MAX; id++) {
810 reg_config = &as3722_regs->reg_config_data[id];
811
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;
826 switch (id) {
827 case AS3722_REGULATOR_ID_LDO0:
828 if (reg_config->ext_control)
829 ops = &as3722_ldo0_extcntrl_ops;
830 else
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;
836 break;
837 case AS3722_REGULATOR_ID_LDO3:
838 if (reg_config->ext_control)
839 ops = &as3722_ldo3_extcntrl_ops;
840 else
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);
849 if (ret < 0) {
850 dev_err(&pdev->dev,
851 "LDO3 tracking failed: %d\n",
852 ret);
853 return ret;
854 }
855 }
856 break;
Jon Hunter40a86552016-04-07 16:22:38 +0200857 case AS3722_REGULATOR_ID_LDO6:
858 if (reg_config->ext_control)
859 ops = &as3722_ldo6_extcntrl_ops;
860 else
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);
874 break;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530875 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;
880 else
881 ops = &as3722_sd016_ops;
Andrew Bresticker762a8ee2013-12-26 13:48:46 -0800882 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;
887 } else {
888 as3722_regs->desc[id].n_voltages =
889 AS3722_SD0_VSEL_MAX + 1,
890 as3722_regs->desc[id].min_uV = 610000;
891 }
Laxman Dewanganbc407332013-09-20 18:00:13 +0530892 as3722_regs->desc[id].uV_step = 10000;
893 as3722_regs->desc[id].linear_min_sel = 1;
Vince Hsufb04f1b2013-12-26 13:48:47 -0800894 as3722_regs->desc[id].enable_time = 600;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530895 break;
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;
902 else
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);
908 break;
909 default:
910 if (reg_config->ext_control)
911 ops = &as3722_ldo_extcntrl_ops;
912 else
913 ops = &as3722_ldo_ops;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530914 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);
918 break;
919 }
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);
925 if (IS_ERR(rdev)) {
926 ret = PTR_ERR(rdev);
927 dev_err(&pdev->dev, "regulator %d register failed %d\n",
928 id, ret);
929 return ret;
930 }
931
932 as3722_regs->rdevs[id] = rdev;
933 if (reg_config->ext_control) {
934 ret = regulator_enable_regmap(rdev);
935 if (ret < 0) {
936 dev_err(&pdev->dev,
937 "Regulator %d enable failed: %d\n",
938 id, ret);
939 return ret;
940 }
941 ret = as3722_extreg_init(as3722_regs, id,
942 reg_config->ext_control);
943 if (ret < 0) {
944 dev_err(&pdev->dev,
945 "AS3722 ext control failed: %d", ret);
946 return ret;
947 }
948 }
949 }
950 return 0;
951}
952
953static const struct of_device_id of_as3722_regulator_match[] = {
954 { .compatible = "ams,as3722-regulator", },
955 {},
956};
957MODULE_DEVICE_TABLE(of, of_as3722_regulator_match);
958
959static struct platform_driver as3722_regulator_driver = {
960 .driver = {
961 .name = "as3722-regulator",
Laxman Dewanganbc407332013-09-20 18:00:13 +0530962 .of_match_table = of_as3722_regulator_match,
963 },
964 .probe = as3722_regulator_probe,
965};
966
967module_platform_driver(as3722_regulator_driver);
968
969MODULE_ALIAS("platform:as3722-regulator");
970MODULE_DESCRIPTION("AS3722 regulator driver");
971MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
972MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
973MODULE_LICENSE("GPL");