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