blob: c584bd1ffa9c1e2654b9e3bf137393d756ad5291 [file] [log] [blame]
Krzysztof Kozlowski4f3fb282018-08-07 18:18:26 +02001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
4// http://www.samsung.com
Sangbeom Kimcb746852012-07-11 21:08:17 +09005
6#include <linux/bug.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +09007#include <linux/err.h>
8#include <linux/gpio.h>
9#include <linux/slab.h>
10#include <linux/module.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053011#include <linux/of.h>
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +053012#include <linux/regmap.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +090013#include <linux/platform_device.h>
14#include <linux/regulator/driver.h>
15#include <linux/regulator/machine.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053016#include <linux/regulator/of_regulator.h>
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +020017#include <linux/of_gpio.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +090018#include <linux/mfd/samsung/core.h>
19#include <linux/mfd/samsung/s2mps11.h>
Chanwoo Choi76b98402014-11-18 17:59:40 +090020#include <linux/mfd/samsung/s2mps13.h>
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +010021#include <linux/mfd/samsung/s2mps14.h>
Thomas Abraham51af2062015-11-20 16:07:52 +053022#include <linux/mfd/samsung/s2mps15.h>
Chanwoo Choi00e25732014-06-25 16:14:45 +090023#include <linux/mfd/samsung/s2mpu02.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053024
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +090025/* The highest number of possible regulators for supported devices. */
26#define S2MPS_REGULATOR_MAX S2MPS13_REGULATOR_MAX
Sangbeom Kimcb746852012-07-11 21:08:17 +090027struct s2mps11_info {
Sangbeom Kimcb746852012-07-11 21:08:17 +090028 int ramp_delay2;
29 int ramp_delay34;
30 int ramp_delay5;
31 int ramp_delay16;
32 int ramp_delay7810;
33 int ramp_delay9;
Chanwoo Choi00e25732014-06-25 16:14:45 +090034
35 enum sec_device_type dev_type;
36
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010037 /*
Chanwoo Choi76b98402014-11-18 17:59:40 +090038 * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether
39 * the suspend mode was enabled.
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010040 */
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +090041 DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX);
Chanwoo Choi00e25732014-06-25 16:14:45 +090042
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +090043 /*
44 * Array (size: number of regulators) with GPIO-s for external
45 * sleep control.
46 */
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +020047 int *ext_control_gpio;
Sangbeom Kimcb746852012-07-11 21:08:17 +090048};
49
50static int get_ramp_delay(int ramp_delay)
51{
52 unsigned char cnt = 0;
53
Yadwinder Singh Brar90068342013-06-24 16:50:55 +053054 ramp_delay /= 6250;
Sangbeom Kimcb746852012-07-11 21:08:17 +090055
56 while (true) {
57 ramp_delay = ramp_delay >> 1;
58 if (ramp_delay == 0)
59 break;
60 cnt++;
61 }
Axel Linf8f1d482013-08-05 14:08:37 +080062
63 if (cnt > 3)
64 cnt = 3;
65
Sangbeom Kimcb746852012-07-11 21:08:17 +090066 return cnt;
67}
68
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053069static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
70 unsigned int old_selector,
71 unsigned int new_selector)
72{
73 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
74 unsigned int ramp_delay = 0;
75 int old_volt, new_volt;
76
Thiago Farinad55efa42014-01-26 21:57:12 -020077 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053078 case S2MPS11_BUCK2:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053079 ramp_delay = s2mps11->ramp_delay2;
80 break;
81 case S2MPS11_BUCK3:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053082 case S2MPS11_BUCK4:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053083 ramp_delay = s2mps11->ramp_delay34;
84 break;
85 case S2MPS11_BUCK5:
86 ramp_delay = s2mps11->ramp_delay5;
87 break;
88 case S2MPS11_BUCK6:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053089 case S2MPS11_BUCK1:
90 ramp_delay = s2mps11->ramp_delay16;
91 break;
92 case S2MPS11_BUCK7:
93 case S2MPS11_BUCK8:
94 case S2MPS11_BUCK10:
95 ramp_delay = s2mps11->ramp_delay7810;
96 break;
97 case S2MPS11_BUCK9:
98 ramp_delay = s2mps11->ramp_delay9;
99 }
100
101 if (ramp_delay == 0)
102 ramp_delay = rdev->desc->ramp_delay;
103
104 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
105 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
106
107 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
108}
109
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530110static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
111{
112 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
113 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
114 unsigned int ramp_enable = 1, enable_shift = 0;
115 int ret;
116
Thiago Farinad55efa42014-01-26 21:57:12 -0200117 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530118 case S2MPS11_BUCK1:
119 if (ramp_delay > s2mps11->ramp_delay16)
120 s2mps11->ramp_delay16 = ramp_delay;
121 else
122 ramp_delay = s2mps11->ramp_delay16;
123
124 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
125 break;
126 case S2MPS11_BUCK2:
127 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
128 if (!ramp_delay) {
129 ramp_enable = 0;
130 break;
131 }
132
133 s2mps11->ramp_delay2 = ramp_delay;
134 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
135 ramp_reg = S2MPS11_REG_RAMP;
136 break;
137 case S2MPS11_BUCK3:
138 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
139 if (!ramp_delay) {
140 ramp_enable = 0;
141 break;
142 }
143
144 if (ramp_delay > s2mps11->ramp_delay34)
145 s2mps11->ramp_delay34 = ramp_delay;
146 else
147 ramp_delay = s2mps11->ramp_delay34;
148
149 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
150 ramp_reg = S2MPS11_REG_RAMP;
151 break;
152 case S2MPS11_BUCK4:
153 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
154 if (!ramp_delay) {
155 ramp_enable = 0;
156 break;
157 }
158
159 if (ramp_delay > s2mps11->ramp_delay34)
160 s2mps11->ramp_delay34 = ramp_delay;
161 else
162 ramp_delay = s2mps11->ramp_delay34;
163
164 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
165 ramp_reg = S2MPS11_REG_RAMP;
166 break;
167 case S2MPS11_BUCK5:
168 s2mps11->ramp_delay5 = ramp_delay;
169 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
170 break;
171 case S2MPS11_BUCK6:
172 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
173 if (!ramp_delay) {
174 ramp_enable = 0;
175 break;
176 }
177
178 if (ramp_delay > s2mps11->ramp_delay16)
179 s2mps11->ramp_delay16 = ramp_delay;
180 else
181 ramp_delay = s2mps11->ramp_delay16;
182
183 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
184 break;
185 case S2MPS11_BUCK7:
186 case S2MPS11_BUCK8:
187 case S2MPS11_BUCK10:
188 if (ramp_delay > s2mps11->ramp_delay7810)
189 s2mps11->ramp_delay7810 = ramp_delay;
190 else
191 ramp_delay = s2mps11->ramp_delay7810;
192
193 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
194 break;
195 case S2MPS11_BUCK9:
196 s2mps11->ramp_delay9 = ramp_delay;
197 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
198 break;
199 default:
200 return 0;
201 }
202
203 if (!ramp_enable)
204 goto ramp_disable;
205
Krzysztof Kozlowskib203e0d2014-05-06 08:37:36 +0200206 /* Ramp delay can be enabled/disabled only for buck[2346] */
207 if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
208 rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
209 rdev_get_id(rdev) == S2MPS11_BUCK6) {
210 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
211 1 << enable_shift, 1 << enable_shift);
212 if (ret) {
213 dev_err(&rdev->dev, "failed to enable ramp rate\n");
214 return ret;
215 }
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530216 }
217
218 ramp_val = get_ramp_delay(ramp_delay);
219
Axel Linf8f1d482013-08-05 14:08:37 +0800220 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
221 ramp_val << ramp_shift);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530222
223ramp_disable:
Axel Lin80853302013-08-03 17:10:42 +0800224 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
225 1 << enable_shift, 0);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530226}
227
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200228static const struct regulator_ops s2mps11_ldo_ops = {
Sangbeom Kimcb746852012-07-11 21:08:17 +0900229 .list_voltage = regulator_list_voltage_linear,
230 .map_voltage = regulator_map_voltage_linear,
231 .is_enabled = regulator_is_enabled_regmap,
232 .enable = regulator_enable_regmap,
233 .disable = regulator_disable_regmap,
234 .get_voltage_sel = regulator_get_voltage_sel_regmap,
235 .set_voltage_sel = regulator_set_voltage_sel_regmap,
236 .set_voltage_time_sel = regulator_set_voltage_time_sel,
237};
238
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200239static const struct regulator_ops s2mps11_buck_ops = {
Sangbeom Kimcb746852012-07-11 21:08:17 +0900240 .list_voltage = regulator_list_voltage_linear,
241 .map_voltage = regulator_map_voltage_linear,
242 .is_enabled = regulator_is_enabled_regmap,
243 .enable = regulator_enable_regmap,
244 .disable = regulator_disable_regmap,
245 .get_voltage_sel = regulator_get_voltage_sel_regmap,
246 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +0530247 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530248 .set_ramp_delay = s2mps11_set_ramp_delay,
Sangbeom Kimcb746852012-07-11 21:08:17 +0900249};
250
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530251#define regulator_desc_s2mps11_ldo(num, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900252 .name = "LDO"#num, \
253 .id = S2MPS11_LDO##num, \
254 .ops = &s2mps11_ldo_ops, \
255 .type = REGULATOR_VOLTAGE, \
256 .owner = THIS_MODULE, \
Krzysztof Kozlowski94be46b2016-04-18 15:33:10 +0200257 .ramp_delay = RAMP_DELAY_12_MVUS, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530258 .min_uV = MIN_800_MV, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530259 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900260 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
261 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
Axel Lin2693fca2012-07-12 09:35:50 +0800262 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900263 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
264 .enable_mask = S2MPS11_ENABLE_MASK \
265}
266
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100267#define regulator_desc_s2mps11_buck1_4(num) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900268 .name = "BUCK"#num, \
269 .id = S2MPS11_BUCK##num, \
270 .ops = &s2mps11_buck_ops, \
271 .type = REGULATOR_VOLTAGE, \
272 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530273 .min_uV = MIN_600_MV, \
274 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900275 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530276 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900277 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800278 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900279 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
280 .enable_mask = S2MPS11_ENABLE_MASK \
281}
282
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100283#define regulator_desc_s2mps11_buck5 { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900284 .name = "BUCK5", \
285 .id = S2MPS11_BUCK5, \
286 .ops = &s2mps11_buck_ops, \
287 .type = REGULATOR_VOLTAGE, \
288 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530289 .min_uV = MIN_600_MV, \
290 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900291 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530292 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900293 .vsel_reg = S2MPS11_REG_B5CTRL2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800294 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900295 .enable_reg = S2MPS11_REG_B5CTRL1, \
296 .enable_mask = S2MPS11_ENABLE_MASK \
297}
298
Krzysztof Kozlowski3b672622016-03-28 13:09:56 +0900299#define regulator_desc_s2mps11_buck67810(num, min, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900300 .name = "BUCK"#num, \
301 .id = S2MPS11_BUCK##num, \
302 .ops = &s2mps11_buck_ops, \
303 .type = REGULATOR_VOLTAGE, \
304 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530305 .min_uV = min, \
306 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900307 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530308 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900309 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800310 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900311 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
312 .enable_mask = S2MPS11_ENABLE_MASK \
313}
314
Krzysztof Kozlowski3b672622016-03-28 13:09:56 +0900315#define regulator_desc_s2mps11_buck9 { \
316 .name = "BUCK9", \
317 .id = S2MPS11_BUCK9, \
318 .ops = &s2mps11_buck_ops, \
319 .type = REGULATOR_VOLTAGE, \
320 .owner = THIS_MODULE, \
321 .min_uV = MIN_3000_MV, \
322 .uV_step = STEP_25_MV, \
323 .n_voltages = S2MPS11_BUCK9_N_VOLTAGES, \
324 .ramp_delay = S2MPS11_RAMP_DELAY, \
325 .vsel_reg = S2MPS11_REG_B9CTRL2, \
326 .vsel_mask = S2MPS11_BUCK9_VSEL_MASK, \
327 .enable_reg = S2MPS11_REG_B9CTRL1, \
328 .enable_mask = S2MPS11_ENABLE_MASK \
329}
330
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100331static const struct regulator_desc s2mps11_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530332 regulator_desc_s2mps11_ldo(1, STEP_25_MV),
333 regulator_desc_s2mps11_ldo(2, STEP_50_MV),
334 regulator_desc_s2mps11_ldo(3, STEP_50_MV),
335 regulator_desc_s2mps11_ldo(4, STEP_50_MV),
336 regulator_desc_s2mps11_ldo(5, STEP_50_MV),
337 regulator_desc_s2mps11_ldo(6, STEP_25_MV),
338 regulator_desc_s2mps11_ldo(7, STEP_50_MV),
339 regulator_desc_s2mps11_ldo(8, STEP_50_MV),
340 regulator_desc_s2mps11_ldo(9, STEP_50_MV),
341 regulator_desc_s2mps11_ldo(10, STEP_50_MV),
342 regulator_desc_s2mps11_ldo(11, STEP_25_MV),
343 regulator_desc_s2mps11_ldo(12, STEP_50_MV),
344 regulator_desc_s2mps11_ldo(13, STEP_50_MV),
345 regulator_desc_s2mps11_ldo(14, STEP_50_MV),
346 regulator_desc_s2mps11_ldo(15, STEP_50_MV),
347 regulator_desc_s2mps11_ldo(16, STEP_50_MV),
348 regulator_desc_s2mps11_ldo(17, STEP_50_MV),
349 regulator_desc_s2mps11_ldo(18, STEP_50_MV),
350 regulator_desc_s2mps11_ldo(19, STEP_50_MV),
351 regulator_desc_s2mps11_ldo(20, STEP_50_MV),
352 regulator_desc_s2mps11_ldo(21, STEP_50_MV),
353 regulator_desc_s2mps11_ldo(22, STEP_25_MV),
354 regulator_desc_s2mps11_ldo(23, STEP_25_MV),
355 regulator_desc_s2mps11_ldo(24, STEP_50_MV),
356 regulator_desc_s2mps11_ldo(25, STEP_50_MV),
357 regulator_desc_s2mps11_ldo(26, STEP_50_MV),
358 regulator_desc_s2mps11_ldo(27, STEP_25_MV),
359 regulator_desc_s2mps11_ldo(28, STEP_50_MV),
360 regulator_desc_s2mps11_ldo(29, STEP_50_MV),
361 regulator_desc_s2mps11_ldo(30, STEP_50_MV),
362 regulator_desc_s2mps11_ldo(31, STEP_50_MV),
363 regulator_desc_s2mps11_ldo(32, STEP_50_MV),
364 regulator_desc_s2mps11_ldo(33, STEP_50_MV),
365 regulator_desc_s2mps11_ldo(34, STEP_50_MV),
Krzysztof Kozlowski462aee42019-02-09 18:14:14 +0100366 regulator_desc_s2mps11_ldo(35, STEP_25_MV),
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530367 regulator_desc_s2mps11_ldo(36, STEP_50_MV),
368 regulator_desc_s2mps11_ldo(37, STEP_50_MV),
369 regulator_desc_s2mps11_ldo(38, STEP_50_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100370 regulator_desc_s2mps11_buck1_4(1),
371 regulator_desc_s2mps11_buck1_4(2),
372 regulator_desc_s2mps11_buck1_4(3),
373 regulator_desc_s2mps11_buck1_4(4),
374 regulator_desc_s2mps11_buck5,
Krzysztof Kozlowski3b672622016-03-28 13:09:56 +0900375 regulator_desc_s2mps11_buck67810(6, MIN_600_MV, STEP_6_25_MV),
Krzysztof Kozlowski462aee42019-02-09 18:14:14 +0100376 regulator_desc_s2mps11_buck67810(7, MIN_600_MV, STEP_12_5_MV),
377 regulator_desc_s2mps11_buck67810(8, MIN_600_MV, STEP_12_5_MV),
Krzysztof Kozlowski3b672622016-03-28 13:09:56 +0900378 regulator_desc_s2mps11_buck9,
379 regulator_desc_s2mps11_buck67810(10, MIN_750_MV, STEP_12_5_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100380};
381
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200382static const struct regulator_ops s2mps14_reg_ops;
Chanwoo Choi76b98402014-11-18 17:59:40 +0900383
384#define regulator_desc_s2mps13_ldo(num, min, step, min_sel) { \
385 .name = "LDO"#num, \
386 .id = S2MPS13_LDO##num, \
387 .ops = &s2mps14_reg_ops, \
388 .type = REGULATOR_VOLTAGE, \
389 .owner = THIS_MODULE, \
390 .min_uV = min, \
391 .uV_step = step, \
392 .linear_min_sel = min_sel, \
393 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
394 .vsel_reg = S2MPS13_REG_L1CTRL + num - 1, \
395 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
396 .enable_reg = S2MPS13_REG_L1CTRL + num - 1, \
397 .enable_mask = S2MPS14_ENABLE_MASK \
398}
399
400#define regulator_desc_s2mps13_buck(num, min, step, min_sel) { \
401 .name = "BUCK"#num, \
402 .id = S2MPS13_BUCK##num, \
403 .ops = &s2mps14_reg_ops, \
404 .type = REGULATOR_VOLTAGE, \
405 .owner = THIS_MODULE, \
406 .min_uV = min, \
407 .uV_step = step, \
408 .linear_min_sel = min_sel, \
409 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
410 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
411 .vsel_reg = S2MPS13_REG_B1OUT + (num - 1) * 2, \
412 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
413 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
414 .enable_mask = S2MPS14_ENABLE_MASK \
415}
416
Jonghwa Leead26aa62015-01-08 11:04:07 +0900417#define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \
418 .name = "BUCK"#num, \
419 .id = S2MPS13_BUCK##num, \
420 .ops = &s2mps14_reg_ops, \
421 .type = REGULATOR_VOLTAGE, \
422 .owner = THIS_MODULE, \
423 .min_uV = min, \
424 .uV_step = step, \
425 .linear_min_sel = min_sel, \
426 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
427 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
428 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
429 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
430 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
431 .enable_mask = S2MPS14_ENABLE_MASK \
432}
433
434#define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) { \
435 .name = "BUCK"#num, \
436 .id = S2MPS13_BUCK##num, \
437 .ops = &s2mps14_reg_ops, \
438 .type = REGULATOR_VOLTAGE, \
439 .owner = THIS_MODULE, \
440 .min_uV = min, \
441 .uV_step = step, \
442 .linear_min_sel = min_sel, \
443 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
444 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
445 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
446 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
447 .enable_reg = S2MPS13_REG_B1CTRL + (num) * 2 - 1, \
448 .enable_mask = S2MPS14_ENABLE_MASK \
449}
450
Chanwoo Choi76b98402014-11-18 17:59:40 +0900451static const struct regulator_desc s2mps13_regulators[] = {
452 regulator_desc_s2mps13_ldo(1, MIN_800_MV, STEP_12_5_MV, 0x00),
453 regulator_desc_s2mps13_ldo(2, MIN_1400_MV, STEP_50_MV, 0x0C),
454 regulator_desc_s2mps13_ldo(3, MIN_1000_MV, STEP_25_MV, 0x08),
455 regulator_desc_s2mps13_ldo(4, MIN_800_MV, STEP_12_5_MV, 0x00),
456 regulator_desc_s2mps13_ldo(5, MIN_800_MV, STEP_12_5_MV, 0x00),
457 regulator_desc_s2mps13_ldo(6, MIN_800_MV, STEP_12_5_MV, 0x00),
458 regulator_desc_s2mps13_ldo(7, MIN_1000_MV, STEP_25_MV, 0x08),
459 regulator_desc_s2mps13_ldo(8, MIN_1000_MV, STEP_25_MV, 0x08),
460 regulator_desc_s2mps13_ldo(9, MIN_1000_MV, STEP_25_MV, 0x08),
461 regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV, 0x0C),
462 regulator_desc_s2mps13_ldo(11, MIN_800_MV, STEP_25_MV, 0x10),
463 regulator_desc_s2mps13_ldo(12, MIN_800_MV, STEP_25_MV, 0x10),
464 regulator_desc_s2mps13_ldo(13, MIN_800_MV, STEP_25_MV, 0x10),
465 regulator_desc_s2mps13_ldo(14, MIN_800_MV, STEP_12_5_MV, 0x00),
466 regulator_desc_s2mps13_ldo(15, MIN_800_MV, STEP_12_5_MV, 0x00),
467 regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV, 0x0C),
468 regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV, 0x0C),
469 regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV, 0x08),
470 regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV, 0x08),
471 regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV, 0x0C),
472 regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV, 0x08),
473 regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV, 0x08),
474 regulator_desc_s2mps13_ldo(23, MIN_800_MV, STEP_12_5_MV, 0x00),
475 regulator_desc_s2mps13_ldo(24, MIN_800_MV, STEP_12_5_MV, 0x00),
476 regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV, 0x0C),
477 regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV, 0x0C),
478 regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV, 0x0C),
479 regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV, 0x08),
480 regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV, 0x0C),
481 regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV, 0x0C),
482 regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV, 0x08),
483 regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV, 0x08),
484 regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV, 0x0C),
485 regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV, 0x08),
486 regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV, 0x0C),
487 regulator_desc_s2mps13_ldo(36, MIN_800_MV, STEP_12_5_MV, 0x00),
488 regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV, 0x08),
489 regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV, 0x0C),
490 regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV, 0x08),
491 regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV, 0x0C),
492 regulator_desc_s2mps13_buck(1, MIN_500_MV, STEP_6_25_MV, 0x10),
493 regulator_desc_s2mps13_buck(2, MIN_500_MV, STEP_6_25_MV, 0x10),
494 regulator_desc_s2mps13_buck(3, MIN_500_MV, STEP_6_25_MV, 0x10),
495 regulator_desc_s2mps13_buck(4, MIN_500_MV, STEP_6_25_MV, 0x10),
496 regulator_desc_s2mps13_buck(5, MIN_500_MV, STEP_6_25_MV, 0x10),
497 regulator_desc_s2mps13_buck(6, MIN_500_MV, STEP_6_25_MV, 0x10),
Jonghwa Leead26aa62015-01-08 11:04:07 +0900498 regulator_desc_s2mps13_buck7(7, MIN_500_MV, STEP_6_25_MV, 0x10),
499 regulator_desc_s2mps13_buck8_10(8, MIN_1000_MV, STEP_12_5_MV, 0x20),
500 regulator_desc_s2mps13_buck8_10(9, MIN_1000_MV, STEP_12_5_MV, 0x20),
501 regulator_desc_s2mps13_buck8_10(10, MIN_500_MV, STEP_6_25_MV, 0x10),
Chanwoo Choi76b98402014-11-18 17:59:40 +0900502};
503
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100504static int s2mps14_regulator_enable(struct regulator_dev *rdev)
505{
506 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
507 unsigned int val;
508
Chanwoo Choi00e25732014-06-25 16:14:45 +0900509 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900510 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900511 case S2MPS14X:
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900512 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
Chanwoo Choi00e25732014-06-25 16:14:45 +0900513 val = S2MPS14_ENABLE_SUSPEND;
514 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
515 val = S2MPS14_ENABLE_EXT_CONTROL;
516 else
517 val = rdev->desc->enable_mask;
518 break;
519 case S2MPU02:
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900520 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
Chanwoo Choi00e25732014-06-25 16:14:45 +0900521 val = S2MPU02_ENABLE_SUSPEND;
522 else
523 val = rdev->desc->enable_mask;
524 break;
525 default:
526 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900527 }
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100528
529 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
530 rdev->desc->enable_mask, val);
531}
532
533static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
534{
535 int ret;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900536 unsigned int val, state;
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100537 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900538 int rdev_id = rdev_get_id(rdev);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100539
Chanwoo Choi00e25732014-06-25 16:14:45 +0900540 /* Below LDO should be always on or does not support suspend mode. */
541 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900542 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900543 case S2MPS14X:
544 switch (rdev_id) {
545 case S2MPS14_LDO3:
546 return 0;
547 default:
548 state = S2MPS14_ENABLE_SUSPEND;
549 break;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900550 }
Chanwoo Choi00e25732014-06-25 16:14:45 +0900551 break;
552 case S2MPU02:
553 switch (rdev_id) {
554 case S2MPU02_LDO13:
555 case S2MPU02_LDO14:
556 case S2MPU02_LDO15:
557 case S2MPU02_LDO17:
558 case S2MPU02_BUCK7:
559 state = S2MPU02_DISABLE_SUSPEND;
560 break;
561 default:
562 state = S2MPU02_ENABLE_SUSPEND;
563 break;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900564 }
Chanwoo Choi00e25732014-06-25 16:14:45 +0900565 break;
566 default:
567 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900568 }
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100569
570 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
571 if (ret < 0)
572 return ret;
573
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900574 set_bit(rdev_get_id(rdev), s2mps11->suspend_state);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100575 /*
576 * Don't enable suspend mode if regulator is already disabled because
577 * this would effectively for a short time turn on the regulator after
578 * resuming.
579 * However we still want to toggle the suspend_state bit for regulator
580 * in case if it got enabled before suspending the system.
581 */
582 if (!(val & rdev->desc->enable_mask))
583 return 0;
584
585 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Chanwoo Choi00e25732014-06-25 16:14:45 +0900586 rdev->desc->enable_mask, state);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100587}
588
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200589static const struct regulator_ops s2mps14_reg_ops = {
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100590 .list_voltage = regulator_list_voltage_linear,
591 .map_voltage = regulator_map_voltage_linear,
592 .is_enabled = regulator_is_enabled_regmap,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100593 .enable = s2mps14_regulator_enable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100594 .disable = regulator_disable_regmap,
595 .get_voltage_sel = regulator_get_voltage_sel_regmap,
596 .set_voltage_sel = regulator_set_voltage_sel_regmap,
597 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100598 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100599};
600
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530601#define regulator_desc_s2mps14_ldo(num, min, step) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100602 .name = "LDO"#num, \
603 .id = S2MPS14_LDO##num, \
604 .ops = &s2mps14_reg_ops, \
605 .type = REGULATOR_VOLTAGE, \
606 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530607 .min_uV = min, \
608 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100609 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
610 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
611 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
612 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
613 .enable_mask = S2MPS14_ENABLE_MASK \
614}
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530615
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100616#define regulator_desc_s2mps14_buck(num, min, step, min_sel) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100617 .name = "BUCK"#num, \
618 .id = S2MPS14_BUCK##num, \
619 .ops = &s2mps14_reg_ops, \
620 .type = REGULATOR_VOLTAGE, \
621 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530622 .min_uV = min, \
623 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100624 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100625 .linear_min_sel = min_sel, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100626 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
627 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
628 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
629 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
630 .enable_mask = S2MPS14_ENABLE_MASK \
631}
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100632
633static const struct regulator_desc s2mps14_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530634 regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
635 regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
636 regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
637 regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
638 regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
639 regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
640 regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
641 regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
642 regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
643 regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
644 regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
645 regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
646 regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
647 regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
648 regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
649 regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
650 regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
651 regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
652 regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
653 regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
654 regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
655 regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
656 regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
657 regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
658 regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100659 regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
660 S2MPS14_BUCK1235_START_SEL),
661 regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
662 S2MPS14_BUCK1235_START_SEL),
663 regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
664 S2MPS14_BUCK1235_START_SEL),
665 regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
666 S2MPS14_BUCK4_START_SEL),
667 regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
668 S2MPS14_BUCK1235_START_SEL),
Sangbeom Kimcb746852012-07-11 21:08:17 +0900669};
670
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200671static const struct regulator_ops s2mps15_reg_ldo_ops = {
Thomas Abraham51af2062015-11-20 16:07:52 +0530672 .list_voltage = regulator_list_voltage_linear_range,
673 .map_voltage = regulator_map_voltage_linear_range,
674 .is_enabled = regulator_is_enabled_regmap,
675 .enable = regulator_enable_regmap,
676 .disable = regulator_disable_regmap,
677 .get_voltage_sel = regulator_get_voltage_sel_regmap,
678 .set_voltage_sel = regulator_set_voltage_sel_regmap,
679};
680
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200681static const struct regulator_ops s2mps15_reg_buck_ops = {
Thomas Abraham51af2062015-11-20 16:07:52 +0530682 .list_voltage = regulator_list_voltage_linear_range,
683 .map_voltage = regulator_map_voltage_linear_range,
684 .is_enabled = regulator_is_enabled_regmap,
685 .enable = regulator_enable_regmap,
686 .disable = regulator_disable_regmap,
687 .get_voltage_sel = regulator_get_voltage_sel_regmap,
688 .set_voltage_sel = regulator_set_voltage_sel_regmap,
689 .set_voltage_time_sel = regulator_set_voltage_time_sel,
690};
691
692#define regulator_desc_s2mps15_ldo(num, range) { \
693 .name = "LDO"#num, \
694 .id = S2MPS15_LDO##num, \
695 .ops = &s2mps15_reg_ldo_ops, \
696 .type = REGULATOR_VOLTAGE, \
697 .owner = THIS_MODULE, \
698 .linear_ranges = range, \
699 .n_linear_ranges = ARRAY_SIZE(range), \
700 .n_voltages = S2MPS15_LDO_N_VOLTAGES, \
701 .vsel_reg = S2MPS15_REG_L1CTRL + num - 1, \
702 .vsel_mask = S2MPS15_LDO_VSEL_MASK, \
703 .enable_reg = S2MPS15_REG_L1CTRL + num - 1, \
704 .enable_mask = S2MPS15_ENABLE_MASK \
705}
706
707#define regulator_desc_s2mps15_buck(num, range) { \
708 .name = "BUCK"#num, \
709 .id = S2MPS15_BUCK##num, \
710 .ops = &s2mps15_reg_buck_ops, \
711 .type = REGULATOR_VOLTAGE, \
712 .owner = THIS_MODULE, \
713 .linear_ranges = range, \
714 .n_linear_ranges = ARRAY_SIZE(range), \
715 .ramp_delay = 12500, \
716 .n_voltages = S2MPS15_BUCK_N_VOLTAGES, \
717 .vsel_reg = S2MPS15_REG_B1CTRL2 + ((num - 1) * 2), \
718 .vsel_mask = S2MPS15_BUCK_VSEL_MASK, \
719 .enable_reg = S2MPS15_REG_B1CTRL1 + ((num - 1) * 2), \
720 .enable_mask = S2MPS15_ENABLE_MASK \
721}
722
723/* voltage range for s2mps15 LDO 3, 5, 15, 16, 18, 20, 23 and 27 */
724static const struct regulator_linear_range s2mps15_ldo_voltage_ranges1[] = {
725 REGULATOR_LINEAR_RANGE(1000000, 0xc, 0x38, 25000),
726};
727
728/* voltage range for s2mps15 LDO 2, 6, 14, 17, 19, 21, 24 and 25 */
729static const struct regulator_linear_range s2mps15_ldo_voltage_ranges2[] = {
730 REGULATOR_LINEAR_RANGE(1800000, 0x0, 0x3f, 25000),
731};
732
733/* voltage range for s2mps15 LDO 4, 11, 12, 13, 22 and 26 */
734static const struct regulator_linear_range s2mps15_ldo_voltage_ranges3[] = {
735 REGULATOR_LINEAR_RANGE(700000, 0x0, 0x34, 12500),
736};
737
738/* voltage range for s2mps15 LDO 7, 8, 9 and 10 */
739static const struct regulator_linear_range s2mps15_ldo_voltage_ranges4[] = {
Alim Akhtar04c16b82016-07-12 11:26:43 +0530740 REGULATOR_LINEAR_RANGE(700000, 0x10, 0x20, 25000),
Thomas Abraham51af2062015-11-20 16:07:52 +0530741};
742
743/* voltage range for s2mps15 LDO 1 */
744static const struct regulator_linear_range s2mps15_ldo_voltage_ranges5[] = {
745 REGULATOR_LINEAR_RANGE(500000, 0x0, 0x20, 12500),
746};
747
748/* voltage range for s2mps15 BUCK 1, 2, 3, 4, 5, 6 and 7 */
749static const struct regulator_linear_range s2mps15_buck_voltage_ranges1[] = {
Alim Akhtar04c16b82016-07-12 11:26:43 +0530750 REGULATOR_LINEAR_RANGE(500000, 0x20, 0xc0, 6250),
Thomas Abraham51af2062015-11-20 16:07:52 +0530751};
752
753/* voltage range for s2mps15 BUCK 8, 9 and 10 */
754static const struct regulator_linear_range s2mps15_buck_voltage_ranges2[] = {
Alim Akhtar04c16b82016-07-12 11:26:43 +0530755 REGULATOR_LINEAR_RANGE(1000000, 0x20, 0x78, 12500),
Thomas Abraham51af2062015-11-20 16:07:52 +0530756};
757
758static const struct regulator_desc s2mps15_regulators[] = {
759 regulator_desc_s2mps15_ldo(1, s2mps15_ldo_voltage_ranges5),
760 regulator_desc_s2mps15_ldo(2, s2mps15_ldo_voltage_ranges2),
761 regulator_desc_s2mps15_ldo(3, s2mps15_ldo_voltage_ranges1),
762 regulator_desc_s2mps15_ldo(4, s2mps15_ldo_voltage_ranges3),
763 regulator_desc_s2mps15_ldo(5, s2mps15_ldo_voltage_ranges1),
764 regulator_desc_s2mps15_ldo(6, s2mps15_ldo_voltage_ranges2),
765 regulator_desc_s2mps15_ldo(7, s2mps15_ldo_voltage_ranges4),
766 regulator_desc_s2mps15_ldo(8, s2mps15_ldo_voltage_ranges4),
767 regulator_desc_s2mps15_ldo(9, s2mps15_ldo_voltage_ranges4),
768 regulator_desc_s2mps15_ldo(10, s2mps15_ldo_voltage_ranges4),
769 regulator_desc_s2mps15_ldo(11, s2mps15_ldo_voltage_ranges3),
770 regulator_desc_s2mps15_ldo(12, s2mps15_ldo_voltage_ranges3),
771 regulator_desc_s2mps15_ldo(13, s2mps15_ldo_voltage_ranges3),
772 regulator_desc_s2mps15_ldo(14, s2mps15_ldo_voltage_ranges2),
773 regulator_desc_s2mps15_ldo(15, s2mps15_ldo_voltage_ranges1),
774 regulator_desc_s2mps15_ldo(16, s2mps15_ldo_voltage_ranges1),
775 regulator_desc_s2mps15_ldo(17, s2mps15_ldo_voltage_ranges2),
776 regulator_desc_s2mps15_ldo(18, s2mps15_ldo_voltage_ranges1),
777 regulator_desc_s2mps15_ldo(19, s2mps15_ldo_voltage_ranges2),
778 regulator_desc_s2mps15_ldo(20, s2mps15_ldo_voltage_ranges1),
779 regulator_desc_s2mps15_ldo(21, s2mps15_ldo_voltage_ranges2),
780 regulator_desc_s2mps15_ldo(22, s2mps15_ldo_voltage_ranges3),
781 regulator_desc_s2mps15_ldo(23, s2mps15_ldo_voltage_ranges1),
782 regulator_desc_s2mps15_ldo(24, s2mps15_ldo_voltage_ranges2),
783 regulator_desc_s2mps15_ldo(25, s2mps15_ldo_voltage_ranges2),
784 regulator_desc_s2mps15_ldo(26, s2mps15_ldo_voltage_ranges3),
785 regulator_desc_s2mps15_ldo(27, s2mps15_ldo_voltage_ranges1),
786 regulator_desc_s2mps15_buck(1, s2mps15_buck_voltage_ranges1),
787 regulator_desc_s2mps15_buck(2, s2mps15_buck_voltage_ranges1),
788 regulator_desc_s2mps15_buck(3, s2mps15_buck_voltage_ranges1),
789 regulator_desc_s2mps15_buck(4, s2mps15_buck_voltage_ranges1),
790 regulator_desc_s2mps15_buck(5, s2mps15_buck_voltage_ranges1),
791 regulator_desc_s2mps15_buck(6, s2mps15_buck_voltage_ranges1),
792 regulator_desc_s2mps15_buck(7, s2mps15_buck_voltage_ranges1),
793 regulator_desc_s2mps15_buck(8, s2mps15_buck_voltage_ranges2),
794 regulator_desc_s2mps15_buck(9, s2mps15_buck_voltage_ranges2),
795 regulator_desc_s2mps15_buck(10, s2mps15_buck_voltage_ranges2),
796};
797
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200798static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
799 struct regulator_dev *rdev)
800{
801 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
802 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
803}
804
805static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200806 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
807{
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200808 int *gpio = s2mps11->ext_control_gpio;
809 unsigned int i;
810 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
811 S2MPS14_LDO12 };
812
813 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
814 unsigned int reg = valid_regulators[i];
815
816 if (!rdata[reg].init_data || !rdata[reg].of_node)
817 continue;
818
819 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
820 "samsung,ext-control-gpios", 0);
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200821 if (gpio_is_valid(gpio[reg]))
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200822 dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
823 gpio[reg], reg, rdata[reg].name);
824 }
825}
826
827static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +0900828 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11,
829 unsigned int rdev_num)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200830{
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200831 struct device_node *reg_np;
832
833 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
834 if (!reg_np) {
835 dev_err(&pdev->dev, "could not find regulators sub-node\n");
836 return -EINVAL;
837 }
838
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +0900839 of_regulator_match(&pdev->dev, reg_np, rdata, rdev_num);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900840 if (s2mps11->dev_type == S2MPS14X)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200841 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
842
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200843 of_node_put(reg_np);
844
845 return 0;
846}
847
Chanwoo Choi00e25732014-06-25 16:14:45 +0900848static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
849{
850 unsigned int ramp_val, ramp_shift, ramp_reg;
851
852 switch (rdev_get_id(rdev)) {
853 case S2MPU02_BUCK1:
854 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
855 break;
856 case S2MPU02_BUCK2:
857 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
858 break;
859 case S2MPU02_BUCK3:
860 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
861 break;
862 case S2MPU02_BUCK4:
863 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
864 break;
865 default:
866 return 0;
867 }
868 ramp_reg = S2MPU02_REG_RAMP1;
869 ramp_val = get_ramp_delay(ramp_delay);
870
871 return regmap_update_bits(rdev->regmap, ramp_reg,
872 S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
873 ramp_val << ramp_shift);
874}
875
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200876static const struct regulator_ops s2mpu02_ldo_ops = {
Chanwoo Choi00e25732014-06-25 16:14:45 +0900877 .list_voltage = regulator_list_voltage_linear,
878 .map_voltage = regulator_map_voltage_linear,
879 .is_enabled = regulator_is_enabled_regmap,
880 .enable = s2mps14_regulator_enable,
881 .disable = regulator_disable_regmap,
882 .get_voltage_sel = regulator_get_voltage_sel_regmap,
883 .set_voltage_sel = regulator_set_voltage_sel_regmap,
884 .set_voltage_time_sel = regulator_set_voltage_time_sel,
885 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
886};
887
Krzysztof Kozlowski71b45402017-03-11 21:01:22 +0200888static const struct regulator_ops s2mpu02_buck_ops = {
Chanwoo Choi00e25732014-06-25 16:14:45 +0900889 .list_voltage = regulator_list_voltage_linear,
890 .map_voltage = regulator_map_voltage_linear,
891 .is_enabled = regulator_is_enabled_regmap,
892 .enable = s2mps14_regulator_enable,
893 .disable = regulator_disable_regmap,
894 .get_voltage_sel = regulator_get_voltage_sel_regmap,
895 .set_voltage_sel = regulator_set_voltage_sel_regmap,
896 .set_voltage_time_sel = regulator_set_voltage_time_sel,
897 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
898 .set_ramp_delay = s2mpu02_set_ramp_delay,
899};
900
901#define regulator_desc_s2mpu02_ldo1(num) { \
902 .name = "LDO"#num, \
903 .id = S2MPU02_LDO##num, \
904 .ops = &s2mpu02_ldo_ops, \
905 .type = REGULATOR_VOLTAGE, \
906 .owner = THIS_MODULE, \
907 .min_uV = S2MPU02_LDO_MIN_900MV, \
908 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
909 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
910 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
911 .vsel_reg = S2MPU02_REG_L1CTRL, \
912 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
913 .enable_reg = S2MPU02_REG_L1CTRL, \
914 .enable_mask = S2MPU02_ENABLE_MASK \
915}
916#define regulator_desc_s2mpu02_ldo2(num) { \
917 .name = "LDO"#num, \
918 .id = S2MPU02_LDO##num, \
919 .ops = &s2mpu02_ldo_ops, \
920 .type = REGULATOR_VOLTAGE, \
921 .owner = THIS_MODULE, \
922 .min_uV = S2MPU02_LDO_MIN_1050MV, \
923 .uV_step = S2MPU02_LDO_STEP_25MV, \
924 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
925 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
926 .vsel_reg = S2MPU02_REG_L2CTRL1, \
927 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
928 .enable_reg = S2MPU02_REG_L2CTRL1, \
929 .enable_mask = S2MPU02_ENABLE_MASK \
930}
931#define regulator_desc_s2mpu02_ldo3(num) { \
932 .name = "LDO"#num, \
933 .id = S2MPU02_LDO##num, \
934 .ops = &s2mpu02_ldo_ops, \
935 .type = REGULATOR_VOLTAGE, \
936 .owner = THIS_MODULE, \
937 .min_uV = S2MPU02_LDO_MIN_900MV, \
938 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
939 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
940 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
941 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
942 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
943 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
944 .enable_mask = S2MPU02_ENABLE_MASK \
945}
946#define regulator_desc_s2mpu02_ldo4(num) { \
947 .name = "LDO"#num, \
948 .id = S2MPU02_LDO##num, \
949 .ops = &s2mpu02_ldo_ops, \
950 .type = REGULATOR_VOLTAGE, \
951 .owner = THIS_MODULE, \
952 .min_uV = S2MPU02_LDO_MIN_1050MV, \
953 .uV_step = S2MPU02_LDO_STEP_25MV, \
954 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
955 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
956 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
957 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
958 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
959 .enable_mask = S2MPU02_ENABLE_MASK \
960}
961#define regulator_desc_s2mpu02_ldo5(num) { \
962 .name = "LDO"#num, \
963 .id = S2MPU02_LDO##num, \
964 .ops = &s2mpu02_ldo_ops, \
965 .type = REGULATOR_VOLTAGE, \
966 .owner = THIS_MODULE, \
967 .min_uV = S2MPU02_LDO_MIN_1600MV, \
968 .uV_step = S2MPU02_LDO_STEP_50MV, \
969 .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
970 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
971 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
972 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
973 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
974 .enable_mask = S2MPU02_ENABLE_MASK \
975}
976
977#define regulator_desc_s2mpu02_buck1234(num) { \
978 .name = "BUCK"#num, \
979 .id = S2MPU02_BUCK##num, \
980 .ops = &s2mpu02_buck_ops, \
981 .type = REGULATOR_VOLTAGE, \
982 .owner = THIS_MODULE, \
983 .min_uV = S2MPU02_BUCK1234_MIN_600MV, \
984 .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \
985 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
986 .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \
987 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
988 .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \
989 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
990 .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \
991 .enable_mask = S2MPU02_ENABLE_MASK \
992}
993#define regulator_desc_s2mpu02_buck5(num) { \
994 .name = "BUCK"#num, \
995 .id = S2MPU02_BUCK##num, \
996 .ops = &s2mpu02_ldo_ops, \
997 .type = REGULATOR_VOLTAGE, \
998 .owner = THIS_MODULE, \
999 .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \
1000 .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \
1001 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
1002 .linear_min_sel = S2MPU02_BUCK5_START_SEL, \
1003 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
1004 .vsel_reg = S2MPU02_REG_B5CTRL2, \
1005 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
1006 .enable_reg = S2MPU02_REG_B5CTRL1, \
1007 .enable_mask = S2MPU02_ENABLE_MASK \
1008}
1009#define regulator_desc_s2mpu02_buck6(num) { \
1010 .name = "BUCK"#num, \
1011 .id = S2MPU02_BUCK##num, \
1012 .ops = &s2mpu02_ldo_ops, \
1013 .type = REGULATOR_VOLTAGE, \
1014 .owner = THIS_MODULE, \
1015 .min_uV = S2MPU02_BUCK6_MIN_1700MV, \
1016 .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \
1017 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
1018 .linear_min_sel = S2MPU02_BUCK6_START_SEL, \
1019 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
1020 .vsel_reg = S2MPU02_REG_B6CTRL2, \
1021 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
1022 .enable_reg = S2MPU02_REG_B6CTRL1, \
1023 .enable_mask = S2MPU02_ENABLE_MASK \
1024}
1025#define regulator_desc_s2mpu02_buck7(num) { \
1026 .name = "BUCK"#num, \
1027 .id = S2MPU02_BUCK##num, \
1028 .ops = &s2mpu02_ldo_ops, \
1029 .type = REGULATOR_VOLTAGE, \
1030 .owner = THIS_MODULE, \
1031 .min_uV = S2MPU02_BUCK7_MIN_900MV, \
1032 .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \
1033 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
1034 .linear_min_sel = S2MPU02_BUCK7_START_SEL, \
1035 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
1036 .vsel_reg = S2MPU02_REG_B7CTRL2, \
1037 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
1038 .enable_reg = S2MPU02_REG_B7CTRL1, \
1039 .enable_mask = S2MPU02_ENABLE_MASK \
1040}
1041
1042static const struct regulator_desc s2mpu02_regulators[] = {
1043 regulator_desc_s2mpu02_ldo1(1),
1044 regulator_desc_s2mpu02_ldo2(2),
1045 regulator_desc_s2mpu02_ldo4(3),
1046 regulator_desc_s2mpu02_ldo5(4),
1047 regulator_desc_s2mpu02_ldo4(5),
1048 regulator_desc_s2mpu02_ldo3(6),
1049 regulator_desc_s2mpu02_ldo3(7),
1050 regulator_desc_s2mpu02_ldo4(8),
1051 regulator_desc_s2mpu02_ldo5(9),
1052 regulator_desc_s2mpu02_ldo3(10),
1053 regulator_desc_s2mpu02_ldo4(11),
1054 regulator_desc_s2mpu02_ldo5(12),
1055 regulator_desc_s2mpu02_ldo5(13),
1056 regulator_desc_s2mpu02_ldo5(14),
1057 regulator_desc_s2mpu02_ldo5(15),
1058 regulator_desc_s2mpu02_ldo5(16),
1059 regulator_desc_s2mpu02_ldo4(17),
1060 regulator_desc_s2mpu02_ldo5(18),
1061 regulator_desc_s2mpu02_ldo3(19),
1062 regulator_desc_s2mpu02_ldo4(20),
1063 regulator_desc_s2mpu02_ldo5(21),
1064 regulator_desc_s2mpu02_ldo5(22),
1065 regulator_desc_s2mpu02_ldo5(23),
1066 regulator_desc_s2mpu02_ldo4(24),
1067 regulator_desc_s2mpu02_ldo5(25),
1068 regulator_desc_s2mpu02_ldo4(26),
1069 regulator_desc_s2mpu02_ldo5(27),
1070 regulator_desc_s2mpu02_ldo5(28),
1071 regulator_desc_s2mpu02_buck1234(1),
1072 regulator_desc_s2mpu02_buck1234(2),
1073 regulator_desc_s2mpu02_buck1234(3),
1074 regulator_desc_s2mpu02_buck1234(4),
1075 regulator_desc_s2mpu02_buck5(5),
1076 regulator_desc_s2mpu02_buck6(6),
1077 regulator_desc_s2mpu02_buck7(7),
1078};
1079
Bill Pembertona5023572012-11-19 13:22:22 -05001080static int s2mps11_pmic_probe(struct platform_device *pdev)
Sangbeom Kimcb746852012-07-11 21:08:17 +09001081{
1082 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001083 struct sec_platform_data *pdata = NULL;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001084 struct of_regulator_match *rdata = NULL;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001085 struct regulator_config config = { };
Sangbeom Kimcb746852012-07-11 21:08:17 +09001086 struct s2mps11_info *s2mps11;
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001087 unsigned int rdev_num = 0;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001088 int i, ret = 0;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001089 const struct regulator_desc *regulators;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001090
Sangbeom Kimcb746852012-07-11 21:08:17 +09001091 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
1092 GFP_KERNEL);
1093 if (!s2mps11)
1094 return -ENOMEM;
1095
Chanwoo Choi00e25732014-06-25 16:14:45 +09001096 s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
1097 switch (s2mps11->dev_type) {
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001098 case S2MPS11X:
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001099 rdev_num = ARRAY_SIZE(s2mps11_regulators);
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001100 regulators = s2mps11_regulators;
Krzysztof Kozlowski297eaaa2016-02-18 09:35:07 +09001101 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001102 break;
Chanwoo Choi76b98402014-11-18 17:59:40 +09001103 case S2MPS13X:
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001104 rdev_num = ARRAY_SIZE(s2mps13_regulators);
Chanwoo Choi76b98402014-11-18 17:59:40 +09001105 regulators = s2mps13_regulators;
Krzysztof Kozlowski297eaaa2016-02-18 09:35:07 +09001106 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators));
Chanwoo Choi76b98402014-11-18 17:59:40 +09001107 break;
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01001108 case S2MPS14X:
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001109 rdev_num = ARRAY_SIZE(s2mps14_regulators);
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01001110 regulators = s2mps14_regulators;
Krzysztof Kozlowski297eaaa2016-02-18 09:35:07 +09001111 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators));
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01001112 break;
Thomas Abraham51af2062015-11-20 16:07:52 +05301113 case S2MPS15X:
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001114 rdev_num = ARRAY_SIZE(s2mps15_regulators);
Thomas Abraham51af2062015-11-20 16:07:52 +05301115 regulators = s2mps15_regulators;
Krzysztof Kozlowski297eaaa2016-02-18 09:35:07 +09001116 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
Thomas Abraham51af2062015-11-20 16:07:52 +05301117 break;
Chanwoo Choi00e25732014-06-25 16:14:45 +09001118 case S2MPU02:
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001119 rdev_num = ARRAY_SIZE(s2mpu02_regulators);
Chanwoo Choi00e25732014-06-25 16:14:45 +09001120 regulators = s2mpu02_regulators;
Krzysztof Kozlowski297eaaa2016-02-18 09:35:07 +09001121 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators));
Chanwoo Choi00e25732014-06-25 16:14:45 +09001122 break;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001123 default:
Chanwoo Choi00e25732014-06-25 16:14:45 +09001124 dev_err(&pdev->dev, "Invalid device type: %u\n",
1125 s2mps11->dev_type);
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001126 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +09001127 }
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001128
Kees Cook3c4211b2018-06-12 14:04:57 -07001129 s2mps11->ext_control_gpio = devm_kmalloc_array(&pdev->dev,
1130 rdev_num, sizeof(*s2mps11->ext_control_gpio),
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001131 GFP_KERNEL);
1132 if (!s2mps11->ext_control_gpio)
1133 return -ENOMEM;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001134 /*
1135 * 0 is a valid GPIO so initialize all GPIO-s to negative value
1136 * to indicate that external control won't be used for this regulator.
1137 */
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001138 for (i = 0; i < rdev_num; i++)
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001139 s2mps11->ext_control_gpio[i] = -EINVAL;
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001140
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +05301141 if (!iodev->dev->of_node) {
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001142 if (iodev->pdata) {
1143 pdata = iodev->pdata;
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +05301144 goto common_reg;
1145 } else {
1146 dev_err(pdev->dev.parent,
1147 "Platform data or DT node not supplied\n");
1148 return -ENODEV;
1149 }
1150 }
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301151
Kees Cook6396bb22018-06-12 14:03:40 -07001152 rdata = kcalloc(rdev_num, sizeof(*rdata), GFP_KERNEL);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001153 if (!rdata)
1154 return -ENOMEM;
1155
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001156 for (i = 0; i < rdev_num; i++)
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301157 rdata[i].name = regulators[i].name;
1158
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001159 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11, rdev_num);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001160 if (ret)
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001161 goto out;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301162
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301163common_reg:
1164 platform_set_drvdata(pdev, s2mps11);
Sangbeom Kimcb746852012-07-11 21:08:17 +09001165
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301166 config.dev = &pdev->dev;
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +01001167 config.regmap = iodev->regmap_pmic;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301168 config.driver_data = s2mps11;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001169 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
Markus Pargmann1de38212014-11-03 19:12:04 +01001170 config.ena_gpio_initialized = true;
Krzysztof Kozlowski7ddec642016-02-18 14:57:03 +09001171 for (i = 0; i < rdev_num; i++) {
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001172 struct regulator_dev *regulator;
1173
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001174 if (pdata) {
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301175 config.init_data = pdata->regulators[i].initdata;
Krzysztof Kozlowski54820f52014-01-30 14:51:19 +01001176 config.of_node = pdata->regulators[i].reg_node;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301177 } else {
1178 config.init_data = rdata[i].init_data;
1179 config.of_node = rdata[i].of_node;
1180 }
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001181 config.ena_gpio = s2mps11->ext_control_gpio[i];
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001182
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001183 regulator = devm_regulator_register(&pdev->dev,
Sachin Kamatd55cd792013-09-04 12:12:15 +05301184 &regulators[i], &config);
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001185 if (IS_ERR(regulator)) {
1186 ret = PTR_ERR(regulator);
Axel Lin232b2502012-07-12 09:37:37 +08001187 dev_err(&pdev->dev, "regulator init failed for %d\n",
1188 i);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001189 goto out;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001190 }
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001191
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001192 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001193 ret = s2mps14_pmic_enable_ext_control(s2mps11,
1194 regulator);
1195 if (ret < 0) {
1196 dev_err(&pdev->dev,
1197 "failed to enable GPIO control over %s: %d\n",
1198 regulator->desc->name, ret);
1199 goto out;
1200 }
1201 }
Sangbeom Kimcb746852012-07-11 21:08:17 +09001202 }
1203
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001204out:
1205 kfree(rdata);
1206
1207 return ret;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001208}
1209
1210static const struct platform_device_id s2mps11_pmic_id[] = {
Alim Akhtar2fadbbf2015-11-20 16:18:26 +05301211 { "s2mps11-regulator", S2MPS11X},
1212 { "s2mps13-regulator", S2MPS13X},
1213 { "s2mps14-regulator", S2MPS14X},
Thomas Abraham51af2062015-11-20 16:07:52 +05301214 { "s2mps15-regulator", S2MPS15X},
Alim Akhtar2fadbbf2015-11-20 16:18:26 +05301215 { "s2mpu02-regulator", S2MPU02},
Sangbeom Kimcb746852012-07-11 21:08:17 +09001216 { },
1217};
1218MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1219
1220static struct platform_driver s2mps11_pmic_driver = {
1221 .driver = {
1222 .name = "s2mps11-pmic",
Sangbeom Kimcb746852012-07-11 21:08:17 +09001223 },
1224 .probe = s2mps11_pmic_probe,
Sangbeom Kimcb746852012-07-11 21:08:17 +09001225 .id_table = s2mps11_pmic_id,
1226};
1227
Javier Martinez Canillas5ab3c492016-04-06 09:49:46 -04001228module_platform_driver(s2mps11_pmic_driver);
Sangbeom Kimcb746852012-07-11 21:08:17 +09001229
1230/* Module information */
1231MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
Thomas Abraham51af2062015-11-20 16:07:52 +05301232MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPS15/S2MPU02 Regulator Driver");
Sangbeom Kimcb746852012-07-11 21:08:17 +09001233MODULE_LICENSE("GPL");