blob: 8b046eec6ae051b5600ee4d05a13511a20ae3db6 [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
375static 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,
384};
385
386static 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,
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
418static 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,
426};
427
428static 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,
433};
434
Laxman Dewanganbc407332013-09-20 18:00:13 +0530435static const struct regulator_linear_range as3722_ldo_ranges[] = {
Thierry Reding2050afd2014-06-03 17:17:32 +0200436 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
Axel Lin8828bae2013-10-11 09:32:18 +0800437 REGULATOR_LINEAR_RANGE(825000, 0x01, 0x24, 25000),
438 REGULATOR_LINEAR_RANGE(1725000, 0x40, 0x7F, 25000),
Laxman Dewanganbc407332013-09-20 18:00:13 +0530439};
440
441static struct regulator_ops as3722_ldo_ops = {
442 .is_enabled = regulator_is_enabled_regmap,
443 .enable = regulator_enable_regmap,
444 .disable = regulator_disable_regmap,
445 .map_voltage = regulator_map_voltage_linear_range,
446 .set_voltage_sel = regulator_set_voltage_sel_regmap,
447 .get_voltage_sel = regulator_get_voltage_sel_regmap,
448 .list_voltage = regulator_list_voltage_linear_range,
449 .get_current_limit = as3722_ldo_get_current_limit,
450 .set_current_limit = as3722_ldo_set_current_limit,
451};
452
453static struct regulator_ops as3722_ldo_extcntrl_ops = {
454 .map_voltage = regulator_map_voltage_linear_range,
455 .set_voltage_sel = regulator_set_voltage_sel_regmap,
456 .get_voltage_sel = regulator_get_voltage_sel_regmap,
457 .list_voltage = regulator_list_voltage_linear_range,
458 .get_current_limit = as3722_ldo_get_current_limit,
459 .set_current_limit = as3722_ldo_set_current_limit,
460};
461
462static unsigned int as3722_sd_get_mode(struct regulator_dev *rdev)
463{
464 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
465 struct as3722 *as3722 = as3722_regs->as3722;
466 int id = rdev_get_id(rdev);
467 u32 val;
468 int ret;
469
470 if (!as3722_reg_lookup[id].control_reg)
471 return -ENOTSUPP;
472
473 ret = as3722_read(as3722, as3722_reg_lookup[id].control_reg, &val);
474 if (ret < 0) {
475 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
476 as3722_reg_lookup[id].control_reg, ret);
477 return ret;
478 }
479
480 if (val & as3722_reg_lookup[id].mode_mask)
481 return REGULATOR_MODE_FAST;
482 else
483 return REGULATOR_MODE_NORMAL;
484}
485
486static int as3722_sd_set_mode(struct regulator_dev *rdev,
487 unsigned int mode)
488{
489 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
490 struct as3722 *as3722 = as3722_regs->as3722;
491 u8 id = rdev_get_id(rdev);
492 u8 val = 0;
493 int ret;
494
495 if (!as3722_reg_lookup[id].control_reg)
496 return -ERANGE;
497
498 switch (mode) {
499 case REGULATOR_MODE_FAST:
500 val = as3722_reg_lookup[id].mode_mask;
501 case REGULATOR_MODE_NORMAL: /* fall down */
502 break;
503 default:
504 return -EINVAL;
505 }
506
507 ret = as3722_update_bits(as3722, as3722_reg_lookup[id].control_reg,
508 as3722_reg_lookup[id].mode_mask, val);
509 if (ret < 0) {
510 dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
511 as3722_reg_lookup[id].control_reg, ret);
512 return ret;
513 }
514 return ret;
515}
516
517static int as3722_sd016_get_current_limit(struct regulator_dev *rdev)
518{
519 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
520 struct as3722 *as3722 = as3722_regs->as3722;
521 int id = rdev_get_id(rdev);
522 u32 val, reg;
523 int mask;
524 int ret;
525
526 switch (id) {
527 case AS3722_REGULATOR_ID_SD0:
528 reg = AS3722_OVCURRENT_REG;
529 mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
530 break;
531 case AS3722_REGULATOR_ID_SD1:
532 reg = AS3722_OVCURRENT_REG;
533 mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
534 break;
535 case AS3722_REGULATOR_ID_SD6:
536 reg = AS3722_OVCURRENT_DEB_REG;
537 mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
538 break;
539 default:
540 return -EINVAL;
541 }
542 ret = as3722_read(as3722, reg, &val);
543 if (ret < 0) {
544 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
545 reg, ret);
546 return ret;
547 }
548 val &= mask;
549 val >>= ffs(mask) - 1;
550 if (val == 3)
551 return -EINVAL;
552 return as3722_sd016_current[val];
553}
554
555static int as3722_sd016_set_current_limit(struct regulator_dev *rdev,
556 int min_uA, int max_uA)
557{
558 struct as3722_regulators *as3722_regs = rdev_get_drvdata(rdev);
559 struct as3722 *as3722 = as3722_regs->as3722;
560 int id = rdev_get_id(rdev);
561 int ret;
562 int val;
563 int mask;
564 u32 reg;
565
566 ret = as3722_current_to_index(min_uA, max_uA, as3722_sd016_current,
567 ARRAY_SIZE(as3722_sd016_current));
568 if (ret < 0) {
569 dev_err(as3722_regs->dev,
570 "Current range min:max = %d:%d does not support\n",
571 min_uA, max_uA);
572 return ret;
573 }
574
575 switch (id) {
576 case AS3722_REGULATOR_ID_SD0:
577 reg = AS3722_OVCURRENT_REG;
578 mask = AS3722_OVCURRENT_SD0_TRIP_MASK;
579 break;
580 case AS3722_REGULATOR_ID_SD1:
581 reg = AS3722_OVCURRENT_REG;
582 mask = AS3722_OVCURRENT_SD1_TRIP_MASK;
583 break;
584 case AS3722_REGULATOR_ID_SD6:
585 reg = AS3722_OVCURRENT_DEB_REG;
586 mask = AS3722_OVCURRENT_SD6_TRIP_MASK;
587 break;
588 default:
589 return -EINVAL;
590 }
Vince Hsu63890752013-11-28 19:10:42 +0800591 ret <<= ffs(mask) - 1;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530592 val = ret & mask;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530593 return as3722_update_bits(as3722, reg, mask, val);
594}
595
Andrew Bresticker762a8ee2013-12-26 13:48:46 -0800596static bool as3722_sd0_is_low_voltage(struct as3722_regulators *as3722_regs)
597{
598 int err;
599 unsigned val;
600
601 err = as3722_read(as3722_regs->as3722, AS3722_FUSE7_REG, &val);
602 if (err < 0) {
603 dev_err(as3722_regs->dev, "Reg 0x%02x read failed: %d\n",
604 AS3722_FUSE7_REG, err);
605 return false;
606 }
607 if (val & AS3722_FUSE7_SD0_LOW_VOLTAGE)
608 return true;
609 return false;
610}
611
Laxman Dewanganbc407332013-09-20 18:00:13 +0530612static const struct regulator_linear_range as3722_sd2345_ranges[] = {
Thierry Reding2050afd2014-06-03 17:17:32 +0200613 REGULATOR_LINEAR_RANGE(0, 0x00, 0x00, 0),
Axel Lin8828bae2013-10-11 09:32:18 +0800614 REGULATOR_LINEAR_RANGE(612500, 0x01, 0x40, 12500),
615 REGULATOR_LINEAR_RANGE(1425000, 0x41, 0x70, 25000),
616 REGULATOR_LINEAR_RANGE(2650000, 0x71, 0x7F, 50000),
Laxman Dewanganbc407332013-09-20 18:00:13 +0530617};
618
619static struct regulator_ops as3722_sd016_ops = {
620 .is_enabled = regulator_is_enabled_regmap,
621 .enable = regulator_enable_regmap,
622 .disable = regulator_disable_regmap,
623 .list_voltage = regulator_list_voltage_linear,
624 .map_voltage = regulator_map_voltage_linear,
625 .get_voltage_sel = regulator_get_voltage_sel_regmap,
626 .set_voltage_sel = regulator_set_voltage_sel_regmap,
627 .get_current_limit = as3722_sd016_get_current_limit,
628 .set_current_limit = as3722_sd016_set_current_limit,
629 .get_mode = as3722_sd_get_mode,
630 .set_mode = as3722_sd_set_mode,
631};
632
633static struct regulator_ops as3722_sd016_extcntrl_ops = {
634 .list_voltage = regulator_list_voltage_linear,
635 .map_voltage = regulator_map_voltage_linear,
636 .get_voltage_sel = regulator_get_voltage_sel_regmap,
637 .set_voltage_sel = regulator_set_voltage_sel_regmap,
638 .get_current_limit = as3722_sd016_get_current_limit,
639 .set_current_limit = as3722_sd016_set_current_limit,
640 .get_mode = as3722_sd_get_mode,
641 .set_mode = as3722_sd_set_mode,
642};
643
644static struct regulator_ops as3722_sd2345_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_range,
649 .map_voltage = regulator_map_voltage_linear_range,
650 .set_voltage_sel = regulator_set_voltage_sel_regmap,
651 .get_voltage_sel = regulator_get_voltage_sel_regmap,
652 .get_mode = as3722_sd_get_mode,
653 .set_mode = as3722_sd_set_mode,
654};
655
656static struct regulator_ops as3722_sd2345_extcntrl_ops = {
657 .list_voltage = regulator_list_voltage_linear_range,
658 .map_voltage = regulator_map_voltage_linear_range,
659 .set_voltage_sel = regulator_set_voltage_sel_regmap,
660 .get_voltage_sel = regulator_get_voltage_sel_regmap,
661 .get_mode = as3722_sd_get_mode,
662 .set_mode = as3722_sd_set_mode,
663};
664
665static int as3722_extreg_init(struct as3722_regulators *as3722_regs, int id,
666 int ext_pwr_ctrl)
667{
668 int ret;
669 unsigned int val;
670
671 if ((ext_pwr_ctrl < AS3722_EXT_CONTROL_ENABLE1) ||
672 (ext_pwr_ctrl > AS3722_EXT_CONTROL_ENABLE3))
673 return -EINVAL;
674
675 val = ext_pwr_ctrl << (ffs(as3722_reg_lookup[id].sleep_ctrl_mask) - 1);
676 ret = as3722_update_bits(as3722_regs->as3722,
677 as3722_reg_lookup[id].sleep_ctrl_reg,
678 as3722_reg_lookup[id].sleep_ctrl_mask, val);
679 if (ret < 0)
680 dev_err(as3722_regs->dev, "Reg 0x%02x update failed: %d\n",
681 as3722_reg_lookup[id].sleep_ctrl_reg, ret);
682 return ret;
683}
684
685static struct of_regulator_match as3722_regulator_matches[] = {
686 { .name = "sd0", },
687 { .name = "sd1", },
688 { .name = "sd2", },
689 { .name = "sd3", },
690 { .name = "sd4", },
691 { .name = "sd5", },
692 { .name = "sd6", },
693 { .name = "ldo0", },
694 { .name = "ldo1", },
695 { .name = "ldo2", },
696 { .name = "ldo3", },
697 { .name = "ldo4", },
698 { .name = "ldo5", },
699 { .name = "ldo6", },
700 { .name = "ldo7", },
701 { .name = "ldo9", },
702 { .name = "ldo10", },
703 { .name = "ldo11", },
704};
705
706static int as3722_get_regulator_dt_data(struct platform_device *pdev,
707 struct as3722_regulators *as3722_regs)
708{
709 struct device_node *np;
710 struct as3722_regulator_config_data *reg_config;
711 u32 prop;
712 int id;
713 int ret;
714
715 np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
716 if (!np) {
717 dev_err(&pdev->dev, "Device is not having regulators node\n");
718 return -ENODEV;
719 }
720 pdev->dev.of_node = np;
721
722 ret = of_regulator_match(&pdev->dev, np, as3722_regulator_matches,
723 ARRAY_SIZE(as3722_regulator_matches));
Sachin Kamatba40e552014-02-17 14:33:30 +0530724 of_node_put(np);
Laxman Dewanganbc407332013-09-20 18:00:13 +0530725 if (ret < 0) {
726 dev_err(&pdev->dev, "Parsing of regulator node failed: %d\n",
727 ret);
728 return ret;
729 }
730
731 for (id = 0; id < ARRAY_SIZE(as3722_regulator_matches); ++id) {
732 struct device_node *reg_node;
733
734 reg_config = &as3722_regs->reg_config_data[id];
735 reg_config->reg_init = as3722_regulator_matches[id].init_data;
736 reg_node = as3722_regulator_matches[id].of_node;
737
738 if (!reg_config->reg_init || !reg_node)
739 continue;
740
741 ret = of_property_read_u32(reg_node, "ams,ext-control", &prop);
742 if (!ret) {
743 if (prop < 3)
744 reg_config->ext_control = prop;
745 else
746 dev_warn(&pdev->dev,
747 "ext-control have invalid option: %u\n",
748 prop);
749 }
750 reg_config->enable_tracking =
751 of_property_read_bool(reg_node, "ams,enable-tracking");
752 }
753 return 0;
754}
755
756static int as3722_regulator_probe(struct platform_device *pdev)
757{
758 struct as3722 *as3722 = dev_get_drvdata(pdev->dev.parent);
759 struct as3722_regulators *as3722_regs;
760 struct as3722_regulator_config_data *reg_config;
761 struct regulator_dev *rdev;
762 struct regulator_config config = { };
763 struct regulator_ops *ops;
764 int id;
765 int ret;
766
767 as3722_regs = devm_kzalloc(&pdev->dev, sizeof(*as3722_regs),
768 GFP_KERNEL);
769 if (!as3722_regs)
770 return -ENOMEM;
771
772 as3722_regs->dev = &pdev->dev;
773 as3722_regs->as3722 = as3722;
774 platform_set_drvdata(pdev, as3722_regs);
775
776 ret = as3722_get_regulator_dt_data(pdev, as3722_regs);
777 if (ret < 0)
778 return ret;
779
780 config.dev = &pdev->dev;
781 config.driver_data = as3722_regs;
782 config.regmap = as3722->regmap;
783
784 for (id = 0; id < AS3722_REGULATOR_ID_MAX; id++) {
785 reg_config = &as3722_regs->reg_config_data[id];
786
787 as3722_regs->desc[id].name = as3722_reg_lookup[id].name;
788 as3722_regs->desc[id].supply_name = as3722_reg_lookup[id].sname;
789 as3722_regs->desc[id].id = as3722_reg_lookup[id].regulator_id;
790 as3722_regs->desc[id].n_voltages =
791 as3722_reg_lookup[id].n_voltages;
792 as3722_regs->desc[id].type = REGULATOR_VOLTAGE;
793 as3722_regs->desc[id].owner = THIS_MODULE;
794 as3722_regs->desc[id].enable_reg =
795 as3722_reg_lookup[id].enable_reg;
796 as3722_regs->desc[id].enable_mask =
797 as3722_reg_lookup[id].enable_mask;
798 as3722_regs->desc[id].vsel_reg = as3722_reg_lookup[id].vsel_reg;
799 as3722_regs->desc[id].vsel_mask =
800 as3722_reg_lookup[id].vsel_mask;
801 switch (id) {
802 case AS3722_REGULATOR_ID_LDO0:
803 if (reg_config->ext_control)
804 ops = &as3722_ldo0_extcntrl_ops;
805 else
806 ops = &as3722_ldo0_ops;
807 as3722_regs->desc[id].min_uV = 825000;
808 as3722_regs->desc[id].uV_step = 25000;
809 as3722_regs->desc[id].linear_min_sel = 1;
810 as3722_regs->desc[id].enable_time = 500;
811 break;
812 case AS3722_REGULATOR_ID_LDO3:
813 if (reg_config->ext_control)
814 ops = &as3722_ldo3_extcntrl_ops;
815 else
816 ops = &as3722_ldo3_ops;
817 as3722_regs->desc[id].min_uV = 620000;
818 as3722_regs->desc[id].uV_step = 20000;
819 as3722_regs->desc[id].linear_min_sel = 1;
820 as3722_regs->desc[id].enable_time = 500;
821 if (reg_config->enable_tracking) {
822 ret = as3722_ldo3_set_tracking_mode(as3722_regs,
823 id, AS3722_LDO3_MODE_PMOS_TRACKING);
824 if (ret < 0) {
825 dev_err(&pdev->dev,
826 "LDO3 tracking failed: %d\n",
827 ret);
828 return ret;
829 }
830 }
831 break;
832 case AS3722_REGULATOR_ID_SD0:
833 case AS3722_REGULATOR_ID_SD1:
834 case AS3722_REGULATOR_ID_SD6:
835 if (reg_config->ext_control)
836 ops = &as3722_sd016_extcntrl_ops;
837 else
838 ops = &as3722_sd016_ops;
Andrew Bresticker762a8ee2013-12-26 13:48:46 -0800839 if (id == AS3722_REGULATOR_ID_SD0 &&
840 as3722_sd0_is_low_voltage(as3722_regs)) {
841 as3722_regs->desc[id].n_voltages =
842 AS3722_SD0_VSEL_LOW_VOL_MAX + 1;
843 as3722_regs->desc[id].min_uV = 410000;
844 } else {
845 as3722_regs->desc[id].n_voltages =
846 AS3722_SD0_VSEL_MAX + 1,
847 as3722_regs->desc[id].min_uV = 610000;
848 }
Laxman Dewanganbc407332013-09-20 18:00:13 +0530849 as3722_regs->desc[id].uV_step = 10000;
850 as3722_regs->desc[id].linear_min_sel = 1;
Vince Hsufb04f1b02013-12-26 13:48:47 -0800851 as3722_regs->desc[id].enable_time = 600;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530852 break;
853 case AS3722_REGULATOR_ID_SD2:
854 case AS3722_REGULATOR_ID_SD3:
855 case AS3722_REGULATOR_ID_SD4:
856 case AS3722_REGULATOR_ID_SD5:
857 if (reg_config->ext_control)
858 ops = &as3722_sd2345_extcntrl_ops;
859 else
860 ops = &as3722_sd2345_ops;
861 as3722_regs->desc[id].linear_ranges =
862 as3722_sd2345_ranges;
863 as3722_regs->desc[id].n_linear_ranges =
864 ARRAY_SIZE(as3722_sd2345_ranges);
865 break;
866 default:
867 if (reg_config->ext_control)
868 ops = &as3722_ldo_extcntrl_ops;
869 else
870 ops = &as3722_ldo_ops;
Laxman Dewanganbc407332013-09-20 18:00:13 +0530871 as3722_regs->desc[id].enable_time = 500;
872 as3722_regs->desc[id].linear_ranges = as3722_ldo_ranges;
873 as3722_regs->desc[id].n_linear_ranges =
874 ARRAY_SIZE(as3722_ldo_ranges);
875 break;
876 }
877 as3722_regs->desc[id].ops = ops;
878 config.init_data = reg_config->reg_init;
879 config.of_node = as3722_regulator_matches[id].of_node;
880 rdev = devm_regulator_register(&pdev->dev,
881 &as3722_regs->desc[id], &config);
882 if (IS_ERR(rdev)) {
883 ret = PTR_ERR(rdev);
884 dev_err(&pdev->dev, "regulator %d register failed %d\n",
885 id, ret);
886 return ret;
887 }
888
889 as3722_regs->rdevs[id] = rdev;
890 if (reg_config->ext_control) {
891 ret = regulator_enable_regmap(rdev);
892 if (ret < 0) {
893 dev_err(&pdev->dev,
894 "Regulator %d enable failed: %d\n",
895 id, ret);
896 return ret;
897 }
898 ret = as3722_extreg_init(as3722_regs, id,
899 reg_config->ext_control);
900 if (ret < 0) {
901 dev_err(&pdev->dev,
902 "AS3722 ext control failed: %d", ret);
903 return ret;
904 }
905 }
906 }
907 return 0;
908}
909
910static const struct of_device_id of_as3722_regulator_match[] = {
911 { .compatible = "ams,as3722-regulator", },
912 {},
913};
914MODULE_DEVICE_TABLE(of, of_as3722_regulator_match);
915
916static struct platform_driver as3722_regulator_driver = {
917 .driver = {
918 .name = "as3722-regulator",
Laxman Dewanganbc407332013-09-20 18:00:13 +0530919 .of_match_table = of_as3722_regulator_match,
920 },
921 .probe = as3722_regulator_probe,
922};
923
924module_platform_driver(as3722_regulator_driver);
925
926MODULE_ALIAS("platform:as3722-regulator");
927MODULE_DESCRIPTION("AS3722 regulator driver");
928MODULE_AUTHOR("Florian Lobmaier <florian.lobmaier@ams.com>");
929MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
930MODULE_LICENSE("GPL");