blob: 72fc3c32db49828ce6a2a9256ecabd01bdb034e8 [file] [log] [blame]
Sangbeom Kimcb746852012-07-11 21:08:17 +09001/*
2 * s2mps11.c
3 *
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01004 * Copyright (c) 2012-2014 Samsung Electronics Co., Ltd
Sangbeom Kimcb746852012-07-11 21:08:17 +09005 * http://www.samsung.com
6 *
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01007 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Sangbeom Kimcb746852012-07-11 21:08:17 +090016 *
17 */
18
19#include <linux/bug.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +090020#include <linux/err.h>
21#include <linux/gpio.h>
22#include <linux/slab.h>
23#include <linux/module.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053024#include <linux/of.h>
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +053025#include <linux/regmap.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +090026#include <linux/platform_device.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053029#include <linux/regulator/of_regulator.h>
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +020030#include <linux/of_gpio.h>
Sangbeom Kimcb746852012-07-11 21:08:17 +090031#include <linux/mfd/samsung/core.h>
32#include <linux/mfd/samsung/s2mps11.h>
Chanwoo Choi76b98402014-11-18 17:59:40 +090033#include <linux/mfd/samsung/s2mps13.h>
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +010034#include <linux/mfd/samsung/s2mps14.h>
Chanwoo Choi00e25732014-06-25 16:14:45 +090035#include <linux/mfd/samsung/s2mpu02.h>
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +053036
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +090037/* The highest number of possible regulators for supported devices. */
38#define S2MPS_REGULATOR_MAX S2MPS13_REGULATOR_MAX
Sangbeom Kimcb746852012-07-11 21:08:17 +090039struct s2mps11_info {
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +010040 unsigned int rdev_num;
Sangbeom Kimcb746852012-07-11 21:08:17 +090041 int ramp_delay2;
42 int ramp_delay34;
43 int ramp_delay5;
44 int ramp_delay16;
45 int ramp_delay7810;
46 int ramp_delay9;
Chanwoo Choi00e25732014-06-25 16:14:45 +090047
48 enum sec_device_type dev_type;
49
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010050 /*
Chanwoo Choi76b98402014-11-18 17:59:40 +090051 * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether
52 * the suspend mode was enabled.
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010053 */
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +090054 DECLARE_BITMAP(suspend_state, S2MPS_REGULATOR_MAX);
Chanwoo Choi00e25732014-06-25 16:14:45 +090055
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +020056 /* Array of size rdev_num with GPIO-s for external sleep control */
57 int *ext_control_gpio;
Sangbeom Kimcb746852012-07-11 21:08:17 +090058};
59
60static int get_ramp_delay(int ramp_delay)
61{
62 unsigned char cnt = 0;
63
Yadwinder Singh Brar90068342013-06-24 16:50:55 +053064 ramp_delay /= 6250;
Sangbeom Kimcb746852012-07-11 21:08:17 +090065
66 while (true) {
67 ramp_delay = ramp_delay >> 1;
68 if (ramp_delay == 0)
69 break;
70 cnt++;
71 }
Axel Linf8f1d482013-08-05 14:08:37 +080072
73 if (cnt > 3)
74 cnt = 3;
75
Sangbeom Kimcb746852012-07-11 21:08:17 +090076 return cnt;
77}
78
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053079static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
80 unsigned int old_selector,
81 unsigned int new_selector)
82{
83 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
84 unsigned int ramp_delay = 0;
85 int old_volt, new_volt;
86
Thiago Farinad55efa42014-01-26 21:57:12 -020087 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053088 case S2MPS11_BUCK2:
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053089 ramp_delay = s2mps11->ramp_delay2;
90 break;
91 case S2MPS11_BUCK3:
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053092 case S2MPS11_BUCK4:
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053093 ramp_delay = s2mps11->ramp_delay34;
94 break;
95 case S2MPS11_BUCK5:
96 ramp_delay = s2mps11->ramp_delay5;
97 break;
98 case S2MPS11_BUCK6:
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +053099 case S2MPS11_BUCK1:
100 ramp_delay = s2mps11->ramp_delay16;
101 break;
102 case S2MPS11_BUCK7:
103 case S2MPS11_BUCK8:
104 case S2MPS11_BUCK10:
105 ramp_delay = s2mps11->ramp_delay7810;
106 break;
107 case S2MPS11_BUCK9:
108 ramp_delay = s2mps11->ramp_delay9;
109 }
110
111 if (ramp_delay == 0)
112 ramp_delay = rdev->desc->ramp_delay;
113
114 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
115 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
116
117 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
118}
119
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530120static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
121{
122 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
123 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
124 unsigned int ramp_enable = 1, enable_shift = 0;
125 int ret;
126
Thiago Farinad55efa42014-01-26 21:57:12 -0200127 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530128 case S2MPS11_BUCK1:
129 if (ramp_delay > s2mps11->ramp_delay16)
130 s2mps11->ramp_delay16 = ramp_delay;
131 else
132 ramp_delay = s2mps11->ramp_delay16;
133
134 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
135 break;
136 case S2MPS11_BUCK2:
137 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
138 if (!ramp_delay) {
139 ramp_enable = 0;
140 break;
141 }
142
143 s2mps11->ramp_delay2 = ramp_delay;
144 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
145 ramp_reg = S2MPS11_REG_RAMP;
146 break;
147 case S2MPS11_BUCK3:
148 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
149 if (!ramp_delay) {
150 ramp_enable = 0;
151 break;
152 }
153
154 if (ramp_delay > s2mps11->ramp_delay34)
155 s2mps11->ramp_delay34 = ramp_delay;
156 else
157 ramp_delay = s2mps11->ramp_delay34;
158
159 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
160 ramp_reg = S2MPS11_REG_RAMP;
161 break;
162 case S2MPS11_BUCK4:
163 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
164 if (!ramp_delay) {
165 ramp_enable = 0;
166 break;
167 }
168
169 if (ramp_delay > s2mps11->ramp_delay34)
170 s2mps11->ramp_delay34 = ramp_delay;
171 else
172 ramp_delay = s2mps11->ramp_delay34;
173
174 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
175 ramp_reg = S2MPS11_REG_RAMP;
176 break;
177 case S2MPS11_BUCK5:
178 s2mps11->ramp_delay5 = ramp_delay;
179 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
180 break;
181 case S2MPS11_BUCK6:
182 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
183 if (!ramp_delay) {
184 ramp_enable = 0;
185 break;
186 }
187
188 if (ramp_delay > s2mps11->ramp_delay16)
189 s2mps11->ramp_delay16 = ramp_delay;
190 else
191 ramp_delay = s2mps11->ramp_delay16;
192
193 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
194 break;
195 case S2MPS11_BUCK7:
196 case S2MPS11_BUCK8:
197 case S2MPS11_BUCK10:
198 if (ramp_delay > s2mps11->ramp_delay7810)
199 s2mps11->ramp_delay7810 = ramp_delay;
200 else
201 ramp_delay = s2mps11->ramp_delay7810;
202
203 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
204 break;
205 case S2MPS11_BUCK9:
206 s2mps11->ramp_delay9 = ramp_delay;
207 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
208 break;
209 default:
210 return 0;
211 }
212
213 if (!ramp_enable)
214 goto ramp_disable;
215
Krzysztof Kozlowskib203e0d2014-05-06 08:37:36 +0200216 /* Ramp delay can be enabled/disabled only for buck[2346] */
217 if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
218 rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
219 rdev_get_id(rdev) == S2MPS11_BUCK6) {
220 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
221 1 << enable_shift, 1 << enable_shift);
222 if (ret) {
223 dev_err(&rdev->dev, "failed to enable ramp rate\n");
224 return ret;
225 }
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530226 }
227
228 ramp_val = get_ramp_delay(ramp_delay);
229
Axel Linf8f1d482013-08-05 14:08:37 +0800230 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
231 ramp_val << ramp_shift);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530232
233ramp_disable:
Axel Lin80853302013-08-03 17:10:42 +0800234 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
235 1 << enable_shift, 0);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530236}
237
Sangbeom Kimcb746852012-07-11 21:08:17 +0900238static struct regulator_ops s2mps11_ldo_ops = {
239 .list_voltage = regulator_list_voltage_linear,
240 .map_voltage = regulator_map_voltage_linear,
241 .is_enabled = regulator_is_enabled_regmap,
242 .enable = regulator_enable_regmap,
243 .disable = regulator_disable_regmap,
244 .get_voltage_sel = regulator_get_voltage_sel_regmap,
245 .set_voltage_sel = regulator_set_voltage_sel_regmap,
246 .set_voltage_time_sel = regulator_set_voltage_time_sel,
247};
248
249static struct regulator_ops s2mps11_buck_ops = {
250 .list_voltage = regulator_list_voltage_linear,
251 .map_voltage = regulator_map_voltage_linear,
252 .is_enabled = regulator_is_enabled_regmap,
253 .enable = regulator_enable_regmap,
254 .disable = regulator_disable_regmap,
255 .get_voltage_sel = regulator_get_voltage_sel_regmap,
256 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar1e1598ed2013-06-24 16:50:56 +0530257 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530258 .set_ramp_delay = s2mps11_set_ramp_delay,
Sangbeom Kimcb746852012-07-11 21:08:17 +0900259};
260
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530261#define regulator_desc_s2mps11_ldo(num, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900262 .name = "LDO"#num, \
263 .id = S2MPS11_LDO##num, \
264 .ops = &s2mps11_ldo_ops, \
265 .type = REGULATOR_VOLTAGE, \
266 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530267 .min_uV = MIN_800_MV, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530268 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900269 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
270 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
Axel Lin2693fca2012-07-12 09:35:50 +0800271 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900272 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
273 .enable_mask = S2MPS11_ENABLE_MASK \
274}
275
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100276#define regulator_desc_s2mps11_buck1_4(num) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900277 .name = "BUCK"#num, \
278 .id = S2MPS11_BUCK##num, \
279 .ops = &s2mps11_buck_ops, \
280 .type = REGULATOR_VOLTAGE, \
281 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530282 .min_uV = MIN_600_MV, \
283 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900284 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530285 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900286 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800287 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900288 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
289 .enable_mask = S2MPS11_ENABLE_MASK \
290}
291
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100292#define regulator_desc_s2mps11_buck5 { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900293 .name = "BUCK5", \
294 .id = S2MPS11_BUCK5, \
295 .ops = &s2mps11_buck_ops, \
296 .type = REGULATOR_VOLTAGE, \
297 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530298 .min_uV = MIN_600_MV, \
299 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900300 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530301 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900302 .vsel_reg = S2MPS11_REG_B5CTRL2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800303 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900304 .enable_reg = S2MPS11_REG_B5CTRL1, \
305 .enable_mask = S2MPS11_ENABLE_MASK \
306}
307
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530308#define regulator_desc_s2mps11_buck6_10(num, min, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900309 .name = "BUCK"#num, \
310 .id = S2MPS11_BUCK##num, \
311 .ops = &s2mps11_buck_ops, \
312 .type = REGULATOR_VOLTAGE, \
313 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530314 .min_uV = min, \
315 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900316 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530317 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900318 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800319 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900320 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
321 .enable_mask = S2MPS11_ENABLE_MASK \
322}
323
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100324static const struct regulator_desc s2mps11_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530325 regulator_desc_s2mps11_ldo(1, STEP_25_MV),
326 regulator_desc_s2mps11_ldo(2, STEP_50_MV),
327 regulator_desc_s2mps11_ldo(3, STEP_50_MV),
328 regulator_desc_s2mps11_ldo(4, STEP_50_MV),
329 regulator_desc_s2mps11_ldo(5, STEP_50_MV),
330 regulator_desc_s2mps11_ldo(6, STEP_25_MV),
331 regulator_desc_s2mps11_ldo(7, STEP_50_MV),
332 regulator_desc_s2mps11_ldo(8, STEP_50_MV),
333 regulator_desc_s2mps11_ldo(9, STEP_50_MV),
334 regulator_desc_s2mps11_ldo(10, STEP_50_MV),
335 regulator_desc_s2mps11_ldo(11, STEP_25_MV),
336 regulator_desc_s2mps11_ldo(12, STEP_50_MV),
337 regulator_desc_s2mps11_ldo(13, STEP_50_MV),
338 regulator_desc_s2mps11_ldo(14, STEP_50_MV),
339 regulator_desc_s2mps11_ldo(15, STEP_50_MV),
340 regulator_desc_s2mps11_ldo(16, STEP_50_MV),
341 regulator_desc_s2mps11_ldo(17, STEP_50_MV),
342 regulator_desc_s2mps11_ldo(18, STEP_50_MV),
343 regulator_desc_s2mps11_ldo(19, STEP_50_MV),
344 regulator_desc_s2mps11_ldo(20, STEP_50_MV),
345 regulator_desc_s2mps11_ldo(21, STEP_50_MV),
346 regulator_desc_s2mps11_ldo(22, STEP_25_MV),
347 regulator_desc_s2mps11_ldo(23, STEP_25_MV),
348 regulator_desc_s2mps11_ldo(24, STEP_50_MV),
349 regulator_desc_s2mps11_ldo(25, STEP_50_MV),
350 regulator_desc_s2mps11_ldo(26, STEP_50_MV),
351 regulator_desc_s2mps11_ldo(27, STEP_25_MV),
352 regulator_desc_s2mps11_ldo(28, STEP_50_MV),
353 regulator_desc_s2mps11_ldo(29, STEP_50_MV),
354 regulator_desc_s2mps11_ldo(30, STEP_50_MV),
355 regulator_desc_s2mps11_ldo(31, STEP_50_MV),
356 regulator_desc_s2mps11_ldo(32, STEP_50_MV),
357 regulator_desc_s2mps11_ldo(33, STEP_50_MV),
358 regulator_desc_s2mps11_ldo(34, STEP_50_MV),
359 regulator_desc_s2mps11_ldo(35, STEP_50_MV),
360 regulator_desc_s2mps11_ldo(36, STEP_50_MV),
361 regulator_desc_s2mps11_ldo(37, STEP_50_MV),
362 regulator_desc_s2mps11_ldo(38, STEP_50_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100363 regulator_desc_s2mps11_buck1_4(1),
364 regulator_desc_s2mps11_buck1_4(2),
365 regulator_desc_s2mps11_buck1_4(3),
366 regulator_desc_s2mps11_buck1_4(4),
367 regulator_desc_s2mps11_buck5,
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530368 regulator_desc_s2mps11_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
369 regulator_desc_s2mps11_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
370 regulator_desc_s2mps11_buck6_10(8, MIN_600_MV, STEP_6_25_MV),
371 regulator_desc_s2mps11_buck6_10(9, MIN_3000_MV, STEP_25_MV),
372 regulator_desc_s2mps11_buck6_10(10, MIN_750_MV, STEP_12_5_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100373};
374
Chanwoo Choi76b98402014-11-18 17:59:40 +0900375static struct regulator_ops s2mps14_reg_ops;
376
377#define regulator_desc_s2mps13_ldo(num, min, step, min_sel) { \
378 .name = "LDO"#num, \
379 .id = S2MPS13_LDO##num, \
380 .ops = &s2mps14_reg_ops, \
381 .type = REGULATOR_VOLTAGE, \
382 .owner = THIS_MODULE, \
383 .min_uV = min, \
384 .uV_step = step, \
385 .linear_min_sel = min_sel, \
386 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
387 .vsel_reg = S2MPS13_REG_L1CTRL + num - 1, \
388 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
389 .enable_reg = S2MPS13_REG_L1CTRL + num - 1, \
390 .enable_mask = S2MPS14_ENABLE_MASK \
391}
392
393#define regulator_desc_s2mps13_buck(num, min, step, min_sel) { \
394 .name = "BUCK"#num, \
395 .id = S2MPS13_BUCK##num, \
396 .ops = &s2mps14_reg_ops, \
397 .type = REGULATOR_VOLTAGE, \
398 .owner = THIS_MODULE, \
399 .min_uV = min, \
400 .uV_step = step, \
401 .linear_min_sel = min_sel, \
402 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
403 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
404 .vsel_reg = S2MPS13_REG_B1OUT + (num - 1) * 2, \
405 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
406 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
407 .enable_mask = S2MPS14_ENABLE_MASK \
408}
409
Jonghwa Leead26aa62015-01-08 11:04:07 +0900410#define regulator_desc_s2mps13_buck7(num, min, step, min_sel) { \
411 .name = "BUCK"#num, \
412 .id = S2MPS13_BUCK##num, \
413 .ops = &s2mps14_reg_ops, \
414 .type = REGULATOR_VOLTAGE, \
415 .owner = THIS_MODULE, \
416 .min_uV = min, \
417 .uV_step = step, \
418 .linear_min_sel = min_sel, \
419 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
420 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
421 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
422 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
423 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
424 .enable_mask = S2MPS14_ENABLE_MASK \
425}
426
427#define regulator_desc_s2mps13_buck8_10(num, min, step, min_sel) { \
428 .name = "BUCK"#num, \
429 .id = S2MPS13_BUCK##num, \
430 .ops = &s2mps14_reg_ops, \
431 .type = REGULATOR_VOLTAGE, \
432 .owner = THIS_MODULE, \
433 .min_uV = min, \
434 .uV_step = step, \
435 .linear_min_sel = min_sel, \
436 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
437 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
438 .vsel_reg = S2MPS13_REG_B1OUT + (num) * 2 - 1, \
439 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
440 .enable_reg = S2MPS13_REG_B1CTRL + (num) * 2 - 1, \
441 .enable_mask = S2MPS14_ENABLE_MASK \
442}
443
Chanwoo Choi76b98402014-11-18 17:59:40 +0900444static const struct regulator_desc s2mps13_regulators[] = {
445 regulator_desc_s2mps13_ldo(1, MIN_800_MV, STEP_12_5_MV, 0x00),
446 regulator_desc_s2mps13_ldo(2, MIN_1400_MV, STEP_50_MV, 0x0C),
447 regulator_desc_s2mps13_ldo(3, MIN_1000_MV, STEP_25_MV, 0x08),
448 regulator_desc_s2mps13_ldo(4, MIN_800_MV, STEP_12_5_MV, 0x00),
449 regulator_desc_s2mps13_ldo(5, MIN_800_MV, STEP_12_5_MV, 0x00),
450 regulator_desc_s2mps13_ldo(6, MIN_800_MV, STEP_12_5_MV, 0x00),
451 regulator_desc_s2mps13_ldo(7, MIN_1000_MV, STEP_25_MV, 0x08),
452 regulator_desc_s2mps13_ldo(8, MIN_1000_MV, STEP_25_MV, 0x08),
453 regulator_desc_s2mps13_ldo(9, MIN_1000_MV, STEP_25_MV, 0x08),
454 regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV, 0x0C),
455 regulator_desc_s2mps13_ldo(11, MIN_800_MV, STEP_25_MV, 0x10),
456 regulator_desc_s2mps13_ldo(12, MIN_800_MV, STEP_25_MV, 0x10),
457 regulator_desc_s2mps13_ldo(13, MIN_800_MV, STEP_25_MV, 0x10),
458 regulator_desc_s2mps13_ldo(14, MIN_800_MV, STEP_12_5_MV, 0x00),
459 regulator_desc_s2mps13_ldo(15, MIN_800_MV, STEP_12_5_MV, 0x00),
460 regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV, 0x0C),
461 regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV, 0x0C),
462 regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV, 0x08),
463 regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV, 0x08),
464 regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV, 0x0C),
465 regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV, 0x08),
466 regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV, 0x08),
467 regulator_desc_s2mps13_ldo(23, MIN_800_MV, STEP_12_5_MV, 0x00),
468 regulator_desc_s2mps13_ldo(24, MIN_800_MV, STEP_12_5_MV, 0x00),
469 regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV, 0x0C),
470 regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV, 0x0C),
471 regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV, 0x0C),
472 regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV, 0x08),
473 regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV, 0x0C),
474 regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV, 0x0C),
475 regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV, 0x08),
476 regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV, 0x08),
477 regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV, 0x0C),
478 regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV, 0x08),
479 regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV, 0x0C),
480 regulator_desc_s2mps13_ldo(36, MIN_800_MV, STEP_12_5_MV, 0x00),
481 regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV, 0x08),
482 regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV, 0x0C),
483 regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV, 0x08),
484 regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV, 0x0C),
485 regulator_desc_s2mps13_buck(1, MIN_500_MV, STEP_6_25_MV, 0x10),
486 regulator_desc_s2mps13_buck(2, MIN_500_MV, STEP_6_25_MV, 0x10),
487 regulator_desc_s2mps13_buck(3, MIN_500_MV, STEP_6_25_MV, 0x10),
488 regulator_desc_s2mps13_buck(4, MIN_500_MV, STEP_6_25_MV, 0x10),
489 regulator_desc_s2mps13_buck(5, MIN_500_MV, STEP_6_25_MV, 0x10),
490 regulator_desc_s2mps13_buck(6, MIN_500_MV, STEP_6_25_MV, 0x10),
Jonghwa Leead26aa62015-01-08 11:04:07 +0900491 regulator_desc_s2mps13_buck7(7, MIN_500_MV, STEP_6_25_MV, 0x10),
492 regulator_desc_s2mps13_buck8_10(8, MIN_1000_MV, STEP_12_5_MV, 0x20),
493 regulator_desc_s2mps13_buck8_10(9, MIN_1000_MV, STEP_12_5_MV, 0x20),
494 regulator_desc_s2mps13_buck8_10(10, MIN_500_MV, STEP_6_25_MV, 0x10),
Chanwoo Choi76b98402014-11-18 17:59:40 +0900495};
496
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100497static int s2mps14_regulator_enable(struct regulator_dev *rdev)
498{
499 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
500 unsigned int val;
501
Chanwoo Choi00e25732014-06-25 16:14:45 +0900502 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900503 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900504 case S2MPS14X:
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900505 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
Chanwoo Choi00e25732014-06-25 16:14:45 +0900506 val = S2MPS14_ENABLE_SUSPEND;
507 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
508 val = S2MPS14_ENABLE_EXT_CONTROL;
509 else
510 val = rdev->desc->enable_mask;
511 break;
512 case S2MPU02:
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900513 if (test_bit(rdev_get_id(rdev), s2mps11->suspend_state))
Chanwoo Choi00e25732014-06-25 16:14:45 +0900514 val = S2MPU02_ENABLE_SUSPEND;
515 else
516 val = rdev->desc->enable_mask;
517 break;
518 default:
519 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900520 }
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100521
522 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
523 rdev->desc->enable_mask, val);
524}
525
526static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
527{
528 int ret;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900529 unsigned int val, state;
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100530 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900531 int rdev_id = rdev_get_id(rdev);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100532
Chanwoo Choi00e25732014-06-25 16:14:45 +0900533 /* Below LDO should be always on or does not support suspend mode. */
534 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900535 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900536 case S2MPS14X:
537 switch (rdev_id) {
538 case S2MPS14_LDO3:
539 return 0;
540 default:
541 state = S2MPS14_ENABLE_SUSPEND;
542 break;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900543 }
Chanwoo Choi00e25732014-06-25 16:14:45 +0900544 break;
545 case S2MPU02:
546 switch (rdev_id) {
547 case S2MPU02_LDO13:
548 case S2MPU02_LDO14:
549 case S2MPU02_LDO15:
550 case S2MPU02_LDO17:
551 case S2MPU02_BUCK7:
552 state = S2MPU02_DISABLE_SUSPEND;
553 break;
554 default:
555 state = S2MPU02_ENABLE_SUSPEND;
556 break;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900557 }
Chanwoo Choi00e25732014-06-25 16:14:45 +0900558 break;
559 default:
560 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900561 }
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100562
563 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
564 if (ret < 0)
565 return ret;
566
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900567 set_bit(rdev_get_id(rdev), s2mps11->suspend_state);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100568 /*
569 * Don't enable suspend mode if regulator is already disabled because
570 * this would effectively for a short time turn on the regulator after
571 * resuming.
572 * However we still want to toggle the suspend_state bit for regulator
573 * in case if it got enabled before suspending the system.
574 */
575 if (!(val & rdev->desc->enable_mask))
576 return 0;
577
578 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Chanwoo Choi00e25732014-06-25 16:14:45 +0900579 rdev->desc->enable_mask, state);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100580}
581
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100582static struct regulator_ops s2mps14_reg_ops = {
583 .list_voltage = regulator_list_voltage_linear,
584 .map_voltage = regulator_map_voltage_linear,
585 .is_enabled = regulator_is_enabled_regmap,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100586 .enable = s2mps14_regulator_enable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100587 .disable = regulator_disable_regmap,
588 .get_voltage_sel = regulator_get_voltage_sel_regmap,
589 .set_voltage_sel = regulator_set_voltage_sel_regmap,
590 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100591 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100592};
593
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530594#define regulator_desc_s2mps14_ldo(num, min, step) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100595 .name = "LDO"#num, \
596 .id = S2MPS14_LDO##num, \
597 .ops = &s2mps14_reg_ops, \
598 .type = REGULATOR_VOLTAGE, \
599 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530600 .min_uV = min, \
601 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100602 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
603 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
604 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
605 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
606 .enable_mask = S2MPS14_ENABLE_MASK \
607}
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530608
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100609#define regulator_desc_s2mps14_buck(num, min, step, min_sel) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100610 .name = "BUCK"#num, \
611 .id = S2MPS14_BUCK##num, \
612 .ops = &s2mps14_reg_ops, \
613 .type = REGULATOR_VOLTAGE, \
614 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530615 .min_uV = min, \
616 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100617 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100618 .linear_min_sel = min_sel, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100619 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
620 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
621 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
622 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
623 .enable_mask = S2MPS14_ENABLE_MASK \
624}
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100625
626static const struct regulator_desc s2mps14_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530627 regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
628 regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
629 regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
630 regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
631 regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
632 regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
633 regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
634 regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
635 regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
636 regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
637 regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
638 regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
639 regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
640 regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
641 regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
642 regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
643 regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
644 regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
645 regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
646 regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
647 regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
648 regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
649 regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
650 regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
651 regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
Krzysztof Kozlowski1222d8f2014-12-11 14:40:21 +0100652 regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV,
653 S2MPS14_BUCK1235_START_SEL),
654 regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV,
655 S2MPS14_BUCK1235_START_SEL),
656 regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV,
657 S2MPS14_BUCK1235_START_SEL),
658 regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV,
659 S2MPS14_BUCK4_START_SEL),
660 regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV,
661 S2MPS14_BUCK1235_START_SEL),
Sangbeom Kimcb746852012-07-11 21:08:17 +0900662};
663
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200664static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
665 struct regulator_dev *rdev)
666{
667 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
668 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
669}
670
671static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200672 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
673{
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200674 int *gpio = s2mps11->ext_control_gpio;
675 unsigned int i;
676 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
677 S2MPS14_LDO12 };
678
679 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
680 unsigned int reg = valid_regulators[i];
681
682 if (!rdata[reg].init_data || !rdata[reg].of_node)
683 continue;
684
685 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
686 "samsung,ext-control-gpios", 0);
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200687 if (gpio_is_valid(gpio[reg]))
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200688 dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
689 gpio[reg], reg, rdata[reg].name);
690 }
691}
692
693static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
Chanwoo Choi00e25732014-06-25 16:14:45 +0900694 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200695{
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200696 struct device_node *reg_np;
697
698 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
699 if (!reg_np) {
700 dev_err(&pdev->dev, "could not find regulators sub-node\n");
701 return -EINVAL;
702 }
703
704 of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900705 if (s2mps11->dev_type == S2MPS14X)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200706 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
707
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200708 of_node_put(reg_np);
709
710 return 0;
711}
712
Chanwoo Choi00e25732014-06-25 16:14:45 +0900713static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
714{
715 unsigned int ramp_val, ramp_shift, ramp_reg;
716
717 switch (rdev_get_id(rdev)) {
718 case S2MPU02_BUCK1:
719 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
720 break;
721 case S2MPU02_BUCK2:
722 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
723 break;
724 case S2MPU02_BUCK3:
725 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
726 break;
727 case S2MPU02_BUCK4:
728 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
729 break;
730 default:
731 return 0;
732 }
733 ramp_reg = S2MPU02_REG_RAMP1;
734 ramp_val = get_ramp_delay(ramp_delay);
735
736 return regmap_update_bits(rdev->regmap, ramp_reg,
737 S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
738 ramp_val << ramp_shift);
739}
740
741static struct regulator_ops s2mpu02_ldo_ops = {
742 .list_voltage = regulator_list_voltage_linear,
743 .map_voltage = regulator_map_voltage_linear,
744 .is_enabled = regulator_is_enabled_regmap,
745 .enable = s2mps14_regulator_enable,
746 .disable = regulator_disable_regmap,
747 .get_voltage_sel = regulator_get_voltage_sel_regmap,
748 .set_voltage_sel = regulator_set_voltage_sel_regmap,
749 .set_voltage_time_sel = regulator_set_voltage_time_sel,
750 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
751};
752
753static struct regulator_ops s2mpu02_buck_ops = {
754 .list_voltage = regulator_list_voltage_linear,
755 .map_voltage = regulator_map_voltage_linear,
756 .is_enabled = regulator_is_enabled_regmap,
757 .enable = s2mps14_regulator_enable,
758 .disable = regulator_disable_regmap,
759 .get_voltage_sel = regulator_get_voltage_sel_regmap,
760 .set_voltage_sel = regulator_set_voltage_sel_regmap,
761 .set_voltage_time_sel = regulator_set_voltage_time_sel,
762 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
763 .set_ramp_delay = s2mpu02_set_ramp_delay,
764};
765
766#define regulator_desc_s2mpu02_ldo1(num) { \
767 .name = "LDO"#num, \
768 .id = S2MPU02_LDO##num, \
769 .ops = &s2mpu02_ldo_ops, \
770 .type = REGULATOR_VOLTAGE, \
771 .owner = THIS_MODULE, \
772 .min_uV = S2MPU02_LDO_MIN_900MV, \
773 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
774 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
775 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
776 .vsel_reg = S2MPU02_REG_L1CTRL, \
777 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
778 .enable_reg = S2MPU02_REG_L1CTRL, \
779 .enable_mask = S2MPU02_ENABLE_MASK \
780}
781#define regulator_desc_s2mpu02_ldo2(num) { \
782 .name = "LDO"#num, \
783 .id = S2MPU02_LDO##num, \
784 .ops = &s2mpu02_ldo_ops, \
785 .type = REGULATOR_VOLTAGE, \
786 .owner = THIS_MODULE, \
787 .min_uV = S2MPU02_LDO_MIN_1050MV, \
788 .uV_step = S2MPU02_LDO_STEP_25MV, \
789 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
790 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
791 .vsel_reg = S2MPU02_REG_L2CTRL1, \
792 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
793 .enable_reg = S2MPU02_REG_L2CTRL1, \
794 .enable_mask = S2MPU02_ENABLE_MASK \
795}
796#define regulator_desc_s2mpu02_ldo3(num) { \
797 .name = "LDO"#num, \
798 .id = S2MPU02_LDO##num, \
799 .ops = &s2mpu02_ldo_ops, \
800 .type = REGULATOR_VOLTAGE, \
801 .owner = THIS_MODULE, \
802 .min_uV = S2MPU02_LDO_MIN_900MV, \
803 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
804 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
805 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
806 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
807 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
808 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
809 .enable_mask = S2MPU02_ENABLE_MASK \
810}
811#define regulator_desc_s2mpu02_ldo4(num) { \
812 .name = "LDO"#num, \
813 .id = S2MPU02_LDO##num, \
814 .ops = &s2mpu02_ldo_ops, \
815 .type = REGULATOR_VOLTAGE, \
816 .owner = THIS_MODULE, \
817 .min_uV = S2MPU02_LDO_MIN_1050MV, \
818 .uV_step = S2MPU02_LDO_STEP_25MV, \
819 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
820 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
821 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
822 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
823 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
824 .enable_mask = S2MPU02_ENABLE_MASK \
825}
826#define regulator_desc_s2mpu02_ldo5(num) { \
827 .name = "LDO"#num, \
828 .id = S2MPU02_LDO##num, \
829 .ops = &s2mpu02_ldo_ops, \
830 .type = REGULATOR_VOLTAGE, \
831 .owner = THIS_MODULE, \
832 .min_uV = S2MPU02_LDO_MIN_1600MV, \
833 .uV_step = S2MPU02_LDO_STEP_50MV, \
834 .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
835 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
836 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
837 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
838 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
839 .enable_mask = S2MPU02_ENABLE_MASK \
840}
841
842#define regulator_desc_s2mpu02_buck1234(num) { \
843 .name = "BUCK"#num, \
844 .id = S2MPU02_BUCK##num, \
845 .ops = &s2mpu02_buck_ops, \
846 .type = REGULATOR_VOLTAGE, \
847 .owner = THIS_MODULE, \
848 .min_uV = S2MPU02_BUCK1234_MIN_600MV, \
849 .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \
850 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
851 .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \
852 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
853 .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \
854 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
855 .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \
856 .enable_mask = S2MPU02_ENABLE_MASK \
857}
858#define regulator_desc_s2mpu02_buck5(num) { \
859 .name = "BUCK"#num, \
860 .id = S2MPU02_BUCK##num, \
861 .ops = &s2mpu02_ldo_ops, \
862 .type = REGULATOR_VOLTAGE, \
863 .owner = THIS_MODULE, \
864 .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \
865 .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \
866 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
867 .linear_min_sel = S2MPU02_BUCK5_START_SEL, \
868 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
869 .vsel_reg = S2MPU02_REG_B5CTRL2, \
870 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
871 .enable_reg = S2MPU02_REG_B5CTRL1, \
872 .enable_mask = S2MPU02_ENABLE_MASK \
873}
874#define regulator_desc_s2mpu02_buck6(num) { \
875 .name = "BUCK"#num, \
876 .id = S2MPU02_BUCK##num, \
877 .ops = &s2mpu02_ldo_ops, \
878 .type = REGULATOR_VOLTAGE, \
879 .owner = THIS_MODULE, \
880 .min_uV = S2MPU02_BUCK6_MIN_1700MV, \
881 .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \
882 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
883 .linear_min_sel = S2MPU02_BUCK6_START_SEL, \
884 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
885 .vsel_reg = S2MPU02_REG_B6CTRL2, \
886 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
887 .enable_reg = S2MPU02_REG_B6CTRL1, \
888 .enable_mask = S2MPU02_ENABLE_MASK \
889}
890#define regulator_desc_s2mpu02_buck7(num) { \
891 .name = "BUCK"#num, \
892 .id = S2MPU02_BUCK##num, \
893 .ops = &s2mpu02_ldo_ops, \
894 .type = REGULATOR_VOLTAGE, \
895 .owner = THIS_MODULE, \
896 .min_uV = S2MPU02_BUCK7_MIN_900MV, \
897 .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \
898 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
899 .linear_min_sel = S2MPU02_BUCK7_START_SEL, \
900 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
901 .vsel_reg = S2MPU02_REG_B7CTRL2, \
902 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
903 .enable_reg = S2MPU02_REG_B7CTRL1, \
904 .enable_mask = S2MPU02_ENABLE_MASK \
905}
906
907static const struct regulator_desc s2mpu02_regulators[] = {
908 regulator_desc_s2mpu02_ldo1(1),
909 regulator_desc_s2mpu02_ldo2(2),
910 regulator_desc_s2mpu02_ldo4(3),
911 regulator_desc_s2mpu02_ldo5(4),
912 regulator_desc_s2mpu02_ldo4(5),
913 regulator_desc_s2mpu02_ldo3(6),
914 regulator_desc_s2mpu02_ldo3(7),
915 regulator_desc_s2mpu02_ldo4(8),
916 regulator_desc_s2mpu02_ldo5(9),
917 regulator_desc_s2mpu02_ldo3(10),
918 regulator_desc_s2mpu02_ldo4(11),
919 regulator_desc_s2mpu02_ldo5(12),
920 regulator_desc_s2mpu02_ldo5(13),
921 regulator_desc_s2mpu02_ldo5(14),
922 regulator_desc_s2mpu02_ldo5(15),
923 regulator_desc_s2mpu02_ldo5(16),
924 regulator_desc_s2mpu02_ldo4(17),
925 regulator_desc_s2mpu02_ldo5(18),
926 regulator_desc_s2mpu02_ldo3(19),
927 regulator_desc_s2mpu02_ldo4(20),
928 regulator_desc_s2mpu02_ldo5(21),
929 regulator_desc_s2mpu02_ldo5(22),
930 regulator_desc_s2mpu02_ldo5(23),
931 regulator_desc_s2mpu02_ldo4(24),
932 regulator_desc_s2mpu02_ldo5(25),
933 regulator_desc_s2mpu02_ldo4(26),
934 regulator_desc_s2mpu02_ldo5(27),
935 regulator_desc_s2mpu02_ldo5(28),
936 regulator_desc_s2mpu02_buck1234(1),
937 regulator_desc_s2mpu02_buck1234(2),
938 regulator_desc_s2mpu02_buck1234(3),
939 regulator_desc_s2mpu02_buck1234(4),
940 regulator_desc_s2mpu02_buck5(5),
941 regulator_desc_s2mpu02_buck6(6),
942 regulator_desc_s2mpu02_buck7(7),
943};
944
Bill Pembertona5023572012-11-19 13:22:22 -0500945static int s2mps11_pmic_probe(struct platform_device *pdev)
Sangbeom Kimcb746852012-07-11 21:08:17 +0900946{
947 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200948 struct sec_platform_data *pdata = NULL;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100949 struct of_regulator_match *rdata = NULL;
Sangbeom Kimcb746852012-07-11 21:08:17 +0900950 struct regulator_config config = { };
Sangbeom Kimcb746852012-07-11 21:08:17 +0900951 struct s2mps11_info *s2mps11;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100952 int i, ret = 0;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100953 const struct regulator_desc *regulators;
Sangbeom Kimcb746852012-07-11 21:08:17 +0900954
Sangbeom Kimcb746852012-07-11 21:08:17 +0900955 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
956 GFP_KERNEL);
957 if (!s2mps11)
958 return -ENOMEM;
959
Chanwoo Choi00e25732014-06-25 16:14:45 +0900960 s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
961 switch (s2mps11->dev_type) {
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100962 case S2MPS11X:
963 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
964 regulators = s2mps11_regulators;
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900965 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100966 break;
Chanwoo Choi76b98402014-11-18 17:59:40 +0900967 case S2MPS13X:
968 s2mps11->rdev_num = ARRAY_SIZE(s2mps13_regulators);
969 regulators = s2mps13_regulators;
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900970 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
Chanwoo Choi76b98402014-11-18 17:59:40 +0900971 break;
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100972 case S2MPS14X:
973 s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
974 regulators = s2mps14_regulators;
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900975 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100976 break;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900977 case S2MPU02:
978 s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
979 regulators = s2mpu02_regulators;
Krzysztof Kozlowski32c848e2015-06-24 19:48:43 +0900980 BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900981 break;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100982 default:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900983 dev_err(&pdev->dev, "Invalid device type: %u\n",
984 s2mps11->dev_type);
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100985 return -EINVAL;
Krzysztof Kozlowski7cf225b2015-04-23 21:24:32 +0900986 }
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100987
Krzysztof Kozlowski80e82ac2014-10-16 10:23:28 +0200988 s2mps11->ext_control_gpio = devm_kmalloc(&pdev->dev,
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200989 sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
990 GFP_KERNEL);
991 if (!s2mps11->ext_control_gpio)
992 return -ENOMEM;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200993 /*
994 * 0 is a valid GPIO so initialize all GPIO-s to negative value
995 * to indicate that external control won't be used for this regulator.
996 */
997 for (i = 0; i < s2mps11->rdev_num; i++)
998 s2mps11->ext_control_gpio[i] = -EINVAL;
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200999
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +05301000 if (!iodev->dev->of_node) {
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001001 if (iodev->pdata) {
1002 pdata = iodev->pdata;
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +05301003 goto common_reg;
1004 } else {
1005 dev_err(pdev->dev.parent,
1006 "Platform data or DT node not supplied\n");
1007 return -ENODEV;
1008 }
1009 }
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301010
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001011 rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001012 if (!rdata)
1013 return -ENOMEM;
1014
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001015 for (i = 0; i < s2mps11->rdev_num; i++)
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301016 rdata[i].name = regulators[i].name;
1017
Chanwoo Choi00e25732014-06-25 16:14:45 +09001018 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001019 if (ret)
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001020 goto out;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301021
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301022common_reg:
1023 platform_set_drvdata(pdev, s2mps11);
Sangbeom Kimcb746852012-07-11 21:08:17 +09001024
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301025 config.dev = &pdev->dev;
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +01001026 config.regmap = iodev->regmap_pmic;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301027 config.driver_data = s2mps11;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001028 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
Markus Pargmann1de38212014-11-03 19:12:04 +01001029 config.ena_gpio_initialized = true;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +01001030 for (i = 0; i < s2mps11->rdev_num; i++) {
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001031 struct regulator_dev *regulator;
1032
Krzysztof Kozlowski01170382014-04-14 10:09:06 +02001033 if (pdata) {
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301034 config.init_data = pdata->regulators[i].initdata;
Krzysztof Kozlowski54820f52014-01-30 14:51:19 +01001035 config.of_node = pdata->regulators[i].reg_node;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +05301036 } else {
1037 config.init_data = rdata[i].init_data;
1038 config.of_node = rdata[i].of_node;
1039 }
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001040 config.ena_gpio = s2mps11->ext_control_gpio[i];
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001041
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001042 regulator = devm_regulator_register(&pdev->dev,
Sachin Kamatd55cd792013-09-04 12:12:15 +05301043 &regulators[i], &config);
Krzysztof Kozlowski31195252014-02-28 11:01:48 +01001044 if (IS_ERR(regulator)) {
1045 ret = PTR_ERR(regulator);
Axel Lin232b2502012-07-12 09:37:37 +08001046 dev_err(&pdev->dev, "regulator init failed for %d\n",
1047 i);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001048 goto out;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001049 }
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001050
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001051 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001052 ret = s2mps14_pmic_enable_ext_control(s2mps11,
1053 regulator);
1054 if (ret < 0) {
1055 dev_err(&pdev->dev,
1056 "failed to enable GPIO control over %s: %d\n",
1057 regulator->desc->name, ret);
1058 goto out;
1059 }
1060 }
Sangbeom Kimcb746852012-07-11 21:08:17 +09001061 }
1062
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001063out:
1064 kfree(rdata);
1065
1066 return ret;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001067}
1068
1069static const struct platform_device_id s2mps11_pmic_id[] = {
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001070 { "s2mps11-pmic", S2MPS11X},
Chanwoo Choi76b98402014-11-18 17:59:40 +09001071 { "s2mps13-pmic", S2MPS13X},
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01001072 { "s2mps14-pmic", S2MPS14X},
Chanwoo Choi00e25732014-06-25 16:14:45 +09001073 { "s2mpu02-pmic", S2MPU02},
Sangbeom Kimcb746852012-07-11 21:08:17 +09001074 { },
1075};
1076MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1077
1078static struct platform_driver s2mps11_pmic_driver = {
1079 .driver = {
1080 .name = "s2mps11-pmic",
Sangbeom Kimcb746852012-07-11 21:08:17 +09001081 },
1082 .probe = s2mps11_pmic_probe,
Sangbeom Kimcb746852012-07-11 21:08:17 +09001083 .id_table = s2mps11_pmic_id,
1084};
1085
1086static int __init s2mps11_pmic_init(void)
1087{
1088 return platform_driver_register(&s2mps11_pmic_driver);
1089}
1090subsys_initcall(s2mps11_pmic_init);
1091
1092static void __exit s2mps11_pmic_exit(void)
1093{
1094 platform_driver_unregister(&s2mps11_pmic_driver);
1095}
1096module_exit(s2mps11_pmic_exit);
1097
1098/* Module information */
1099MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
Axel Lincad35c32014-07-23 16:47:31 +08001100MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
Sangbeom Kimcb746852012-07-11 21:08:17 +09001101MODULE_LICENSE("GPL");