blob: b345cf51225a71dc374bff3611d74b6ee9a0ad64 [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
Sangbeom Kimcb746852012-07-11 21:08:17 +090037struct s2mps11_info {
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +010038 unsigned int rdev_num;
Sangbeom Kimcb746852012-07-11 21:08:17 +090039 int ramp_delay2;
40 int ramp_delay34;
41 int ramp_delay5;
42 int ramp_delay16;
43 int ramp_delay7810;
44 int ramp_delay9;
Chanwoo Choi00e25732014-06-25 16:14:45 +090045
46 enum sec_device_type dev_type;
47
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010048 /*
Chanwoo Choi76b98402014-11-18 17:59:40 +090049 * One bit for each S2MPS13/S2MPS14/S2MPU02 regulator whether
50 * the suspend mode was enabled.
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +010051 */
Chanwoo Choi76b98402014-11-18 17:59:40 +090052 unsigned long long s2mps14_suspend_state:50;
Chanwoo Choi00e25732014-06-25 16:14:45 +090053
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +020054 /* Array of size rdev_num with GPIO-s for external sleep control */
55 int *ext_control_gpio;
Sangbeom Kimcb746852012-07-11 21:08:17 +090056};
57
58static int get_ramp_delay(int ramp_delay)
59{
60 unsigned char cnt = 0;
61
Yadwinder Singh Brar90068342013-06-24 16:50:55 +053062 ramp_delay /= 6250;
Sangbeom Kimcb746852012-07-11 21:08:17 +090063
64 while (true) {
65 ramp_delay = ramp_delay >> 1;
66 if (ramp_delay == 0)
67 break;
68 cnt++;
69 }
Axel Linf8f1d482013-08-05 14:08:37 +080070
71 if (cnt > 3)
72 cnt = 3;
73
Sangbeom Kimcb746852012-07-11 21:08:17 +090074 return cnt;
75}
76
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053077static int s2mps11_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
78 unsigned int old_selector,
79 unsigned int new_selector)
80{
81 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
82 unsigned int ramp_delay = 0;
83 int old_volt, new_volt;
84
Thiago Farinad55efa42014-01-26 21:57:12 -020085 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053086 case S2MPS11_BUCK2:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053087 ramp_delay = s2mps11->ramp_delay2;
88 break;
89 case S2MPS11_BUCK3:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053090 case S2MPS11_BUCK4:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053091 ramp_delay = s2mps11->ramp_delay34;
92 break;
93 case S2MPS11_BUCK5:
94 ramp_delay = s2mps11->ramp_delay5;
95 break;
96 case S2MPS11_BUCK6:
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +053097 case S2MPS11_BUCK1:
98 ramp_delay = s2mps11->ramp_delay16;
99 break;
100 case S2MPS11_BUCK7:
101 case S2MPS11_BUCK8:
102 case S2MPS11_BUCK10:
103 ramp_delay = s2mps11->ramp_delay7810;
104 break;
105 case S2MPS11_BUCK9:
106 ramp_delay = s2mps11->ramp_delay9;
107 }
108
109 if (ramp_delay == 0)
110 ramp_delay = rdev->desc->ramp_delay;
111
112 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
113 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
114
115 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
116}
117
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530118static int s2mps11_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
119{
120 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
121 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPS11_REG_RAMP_BUCK;
122 unsigned int ramp_enable = 1, enable_shift = 0;
123 int ret;
124
Thiago Farinad55efa42014-01-26 21:57:12 -0200125 switch (rdev_get_id(rdev)) {
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530126 case S2MPS11_BUCK1:
127 if (ramp_delay > s2mps11->ramp_delay16)
128 s2mps11->ramp_delay16 = ramp_delay;
129 else
130 ramp_delay = s2mps11->ramp_delay16;
131
132 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
133 break;
134 case S2MPS11_BUCK2:
135 enable_shift = S2MPS11_BUCK2_RAMP_EN_SHIFT;
136 if (!ramp_delay) {
137 ramp_enable = 0;
138 break;
139 }
140
141 s2mps11->ramp_delay2 = ramp_delay;
142 ramp_shift = S2MPS11_BUCK2_RAMP_SHIFT;
143 ramp_reg = S2MPS11_REG_RAMP;
144 break;
145 case S2MPS11_BUCK3:
146 enable_shift = S2MPS11_BUCK3_RAMP_EN_SHIFT;
147 if (!ramp_delay) {
148 ramp_enable = 0;
149 break;
150 }
151
152 if (ramp_delay > s2mps11->ramp_delay34)
153 s2mps11->ramp_delay34 = ramp_delay;
154 else
155 ramp_delay = s2mps11->ramp_delay34;
156
157 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
158 ramp_reg = S2MPS11_REG_RAMP;
159 break;
160 case S2MPS11_BUCK4:
161 enable_shift = S2MPS11_BUCK4_RAMP_EN_SHIFT;
162 if (!ramp_delay) {
163 ramp_enable = 0;
164 break;
165 }
166
167 if (ramp_delay > s2mps11->ramp_delay34)
168 s2mps11->ramp_delay34 = ramp_delay;
169 else
170 ramp_delay = s2mps11->ramp_delay34;
171
172 ramp_shift = S2MPS11_BUCK34_RAMP_SHIFT;
173 ramp_reg = S2MPS11_REG_RAMP;
174 break;
175 case S2MPS11_BUCK5:
176 s2mps11->ramp_delay5 = ramp_delay;
177 ramp_shift = S2MPS11_BUCK5_RAMP_SHIFT;
178 break;
179 case S2MPS11_BUCK6:
180 enable_shift = S2MPS11_BUCK6_RAMP_EN_SHIFT;
181 if (!ramp_delay) {
182 ramp_enable = 0;
183 break;
184 }
185
186 if (ramp_delay > s2mps11->ramp_delay16)
187 s2mps11->ramp_delay16 = ramp_delay;
188 else
189 ramp_delay = s2mps11->ramp_delay16;
190
191 ramp_shift = S2MPS11_BUCK16_RAMP_SHIFT;
192 break;
193 case S2MPS11_BUCK7:
194 case S2MPS11_BUCK8:
195 case S2MPS11_BUCK10:
196 if (ramp_delay > s2mps11->ramp_delay7810)
197 s2mps11->ramp_delay7810 = ramp_delay;
198 else
199 ramp_delay = s2mps11->ramp_delay7810;
200
201 ramp_shift = S2MPS11_BUCK7810_RAMP_SHIFT;
202 break;
203 case S2MPS11_BUCK9:
204 s2mps11->ramp_delay9 = ramp_delay;
205 ramp_shift = S2MPS11_BUCK9_RAMP_SHIFT;
206 break;
207 default:
208 return 0;
209 }
210
211 if (!ramp_enable)
212 goto ramp_disable;
213
Krzysztof Kozlowskib203e0d2014-05-06 08:37:36 +0200214 /* Ramp delay can be enabled/disabled only for buck[2346] */
215 if ((rdev_get_id(rdev) >= S2MPS11_BUCK2 &&
216 rdev_get_id(rdev) <= S2MPS11_BUCK4) ||
217 rdev_get_id(rdev) == S2MPS11_BUCK6) {
218 ret = regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
219 1 << enable_shift, 1 << enable_shift);
220 if (ret) {
221 dev_err(&rdev->dev, "failed to enable ramp rate\n");
222 return ret;
223 }
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530224 }
225
226 ramp_val = get_ramp_delay(ramp_delay);
227
Axel Linf8f1d482013-08-05 14:08:37 +0800228 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
229 ramp_val << ramp_shift);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530230
231ramp_disable:
Axel Lin80853302013-08-03 17:10:42 +0800232 return regmap_update_bits(rdev->regmap, S2MPS11_REG_RAMP,
233 1 << enable_shift, 0);
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530234}
235
Sangbeom Kimcb746852012-07-11 21:08:17 +0900236static struct regulator_ops s2mps11_ldo_ops = {
237 .list_voltage = regulator_list_voltage_linear,
238 .map_voltage = regulator_map_voltage_linear,
239 .is_enabled = regulator_is_enabled_regmap,
240 .enable = regulator_enable_regmap,
241 .disable = regulator_disable_regmap,
242 .get_voltage_sel = regulator_get_voltage_sel_regmap,
243 .set_voltage_sel = regulator_set_voltage_sel_regmap,
244 .set_voltage_time_sel = regulator_set_voltage_time_sel,
245};
246
247static struct regulator_ops s2mps11_buck_ops = {
248 .list_voltage = regulator_list_voltage_linear,
249 .map_voltage = regulator_map_voltage_linear,
250 .is_enabled = regulator_is_enabled_regmap,
251 .enable = regulator_enable_regmap,
252 .disable = regulator_disable_regmap,
253 .get_voltage_sel = regulator_get_voltage_sel_regmap,
254 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar1e1598e2013-06-24 16:50:56 +0530255 .set_voltage_time_sel = s2mps11_regulator_set_voltage_time_sel,
Yadwinder Singh Brar939c0272013-06-29 18:21:16 +0530256 .set_ramp_delay = s2mps11_set_ramp_delay,
Sangbeom Kimcb746852012-07-11 21:08:17 +0900257};
258
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530259#define regulator_desc_s2mps11_ldo(num, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900260 .name = "LDO"#num, \
261 .id = S2MPS11_LDO##num, \
262 .ops = &s2mps11_ldo_ops, \
263 .type = REGULATOR_VOLTAGE, \
264 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530265 .min_uV = MIN_800_MV, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530266 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900267 .n_voltages = S2MPS11_LDO_N_VOLTAGES, \
268 .vsel_reg = S2MPS11_REG_L1CTRL + num - 1, \
Axel Lin2693fca2012-07-12 09:35:50 +0800269 .vsel_mask = S2MPS11_LDO_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900270 .enable_reg = S2MPS11_REG_L1CTRL + num - 1, \
271 .enable_mask = S2MPS11_ENABLE_MASK \
272}
273
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100274#define regulator_desc_s2mps11_buck1_4(num) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900275 .name = "BUCK"#num, \
276 .id = S2MPS11_BUCK##num, \
277 .ops = &s2mps11_buck_ops, \
278 .type = REGULATOR_VOLTAGE, \
279 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530280 .min_uV = MIN_600_MV, \
281 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900282 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530283 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900284 .vsel_reg = S2MPS11_REG_B1CTRL2 + (num - 1) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800285 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900286 .enable_reg = S2MPS11_REG_B1CTRL1 + (num - 1) * 2, \
287 .enable_mask = S2MPS11_ENABLE_MASK \
288}
289
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100290#define regulator_desc_s2mps11_buck5 { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900291 .name = "BUCK5", \
292 .id = S2MPS11_BUCK5, \
293 .ops = &s2mps11_buck_ops, \
294 .type = REGULATOR_VOLTAGE, \
295 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530296 .min_uV = MIN_600_MV, \
297 .uV_step = STEP_6_25_MV, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900298 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530299 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900300 .vsel_reg = S2MPS11_REG_B5CTRL2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800301 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900302 .enable_reg = S2MPS11_REG_B5CTRL1, \
303 .enable_mask = S2MPS11_ENABLE_MASK \
304}
305
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530306#define regulator_desc_s2mps11_buck6_10(num, min, step) { \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900307 .name = "BUCK"#num, \
308 .id = S2MPS11_BUCK##num, \
309 .ops = &s2mps11_buck_ops, \
310 .type = REGULATOR_VOLTAGE, \
311 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530312 .min_uV = min, \
313 .uV_step = step, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900314 .n_voltages = S2MPS11_BUCK_N_VOLTAGES, \
Yadwinder Singh Brar90068342013-06-24 16:50:55 +0530315 .ramp_delay = S2MPS11_RAMP_DELAY, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900316 .vsel_reg = S2MPS11_REG_B6CTRL2 + (num - 6) * 2, \
Axel Lin2693fca2012-07-12 09:35:50 +0800317 .vsel_mask = S2MPS11_BUCK_VSEL_MASK, \
Sangbeom Kimcb746852012-07-11 21:08:17 +0900318 .enable_reg = S2MPS11_REG_B6CTRL1 + (num - 6) * 2, \
319 .enable_mask = S2MPS11_ENABLE_MASK \
320}
321
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100322static const struct regulator_desc s2mps11_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530323 regulator_desc_s2mps11_ldo(1, STEP_25_MV),
324 regulator_desc_s2mps11_ldo(2, STEP_50_MV),
325 regulator_desc_s2mps11_ldo(3, STEP_50_MV),
326 regulator_desc_s2mps11_ldo(4, STEP_50_MV),
327 regulator_desc_s2mps11_ldo(5, STEP_50_MV),
328 regulator_desc_s2mps11_ldo(6, STEP_25_MV),
329 regulator_desc_s2mps11_ldo(7, STEP_50_MV),
330 regulator_desc_s2mps11_ldo(8, STEP_50_MV),
331 regulator_desc_s2mps11_ldo(9, STEP_50_MV),
332 regulator_desc_s2mps11_ldo(10, STEP_50_MV),
333 regulator_desc_s2mps11_ldo(11, STEP_25_MV),
334 regulator_desc_s2mps11_ldo(12, STEP_50_MV),
335 regulator_desc_s2mps11_ldo(13, STEP_50_MV),
336 regulator_desc_s2mps11_ldo(14, STEP_50_MV),
337 regulator_desc_s2mps11_ldo(15, STEP_50_MV),
338 regulator_desc_s2mps11_ldo(16, STEP_50_MV),
339 regulator_desc_s2mps11_ldo(17, STEP_50_MV),
340 regulator_desc_s2mps11_ldo(18, STEP_50_MV),
341 regulator_desc_s2mps11_ldo(19, STEP_50_MV),
342 regulator_desc_s2mps11_ldo(20, STEP_50_MV),
343 regulator_desc_s2mps11_ldo(21, STEP_50_MV),
344 regulator_desc_s2mps11_ldo(22, STEP_25_MV),
345 regulator_desc_s2mps11_ldo(23, STEP_25_MV),
346 regulator_desc_s2mps11_ldo(24, STEP_50_MV),
347 regulator_desc_s2mps11_ldo(25, STEP_50_MV),
348 regulator_desc_s2mps11_ldo(26, STEP_50_MV),
349 regulator_desc_s2mps11_ldo(27, STEP_25_MV),
350 regulator_desc_s2mps11_ldo(28, STEP_50_MV),
351 regulator_desc_s2mps11_ldo(29, STEP_50_MV),
352 regulator_desc_s2mps11_ldo(30, STEP_50_MV),
353 regulator_desc_s2mps11_ldo(31, STEP_50_MV),
354 regulator_desc_s2mps11_ldo(32, STEP_50_MV),
355 regulator_desc_s2mps11_ldo(33, STEP_50_MV),
356 regulator_desc_s2mps11_ldo(34, STEP_50_MV),
357 regulator_desc_s2mps11_ldo(35, STEP_50_MV),
358 regulator_desc_s2mps11_ldo(36, STEP_50_MV),
359 regulator_desc_s2mps11_ldo(37, STEP_50_MV),
360 regulator_desc_s2mps11_ldo(38, STEP_50_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100361 regulator_desc_s2mps11_buck1_4(1),
362 regulator_desc_s2mps11_buck1_4(2),
363 regulator_desc_s2mps11_buck1_4(3),
364 regulator_desc_s2mps11_buck1_4(4),
365 regulator_desc_s2mps11_buck5,
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530366 regulator_desc_s2mps11_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
367 regulator_desc_s2mps11_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
368 regulator_desc_s2mps11_buck6_10(8, MIN_600_MV, STEP_6_25_MV),
369 regulator_desc_s2mps11_buck6_10(9, MIN_3000_MV, STEP_25_MV),
370 regulator_desc_s2mps11_buck6_10(10, MIN_750_MV, STEP_12_5_MV),
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100371};
372
Chanwoo Choi76b98402014-11-18 17:59:40 +0900373static struct regulator_ops s2mps14_reg_ops;
374
375#define regulator_desc_s2mps13_ldo(num, min, step, min_sel) { \
376 .name = "LDO"#num, \
377 .id = S2MPS13_LDO##num, \
378 .ops = &s2mps14_reg_ops, \
379 .type = REGULATOR_VOLTAGE, \
380 .owner = THIS_MODULE, \
381 .min_uV = min, \
382 .uV_step = step, \
383 .linear_min_sel = min_sel, \
384 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
385 .vsel_reg = S2MPS13_REG_L1CTRL + num - 1, \
386 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
387 .enable_reg = S2MPS13_REG_L1CTRL + num - 1, \
388 .enable_mask = S2MPS14_ENABLE_MASK \
389}
390
391#define regulator_desc_s2mps13_buck(num, min, step, min_sel) { \
392 .name = "BUCK"#num, \
393 .id = S2MPS13_BUCK##num, \
394 .ops = &s2mps14_reg_ops, \
395 .type = REGULATOR_VOLTAGE, \
396 .owner = THIS_MODULE, \
397 .min_uV = min, \
398 .uV_step = step, \
399 .linear_min_sel = min_sel, \
400 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
401 .ramp_delay = S2MPS13_BUCK_RAMP_DELAY, \
402 .vsel_reg = S2MPS13_REG_B1OUT + (num - 1) * 2, \
403 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
404 .enable_reg = S2MPS13_REG_B1CTRL + (num - 1) * 2, \
405 .enable_mask = S2MPS14_ENABLE_MASK \
406}
407
408static const struct regulator_desc s2mps13_regulators[] = {
409 regulator_desc_s2mps13_ldo(1, MIN_800_MV, STEP_12_5_MV, 0x00),
410 regulator_desc_s2mps13_ldo(2, MIN_1400_MV, STEP_50_MV, 0x0C),
411 regulator_desc_s2mps13_ldo(3, MIN_1000_MV, STEP_25_MV, 0x08),
412 regulator_desc_s2mps13_ldo(4, MIN_800_MV, STEP_12_5_MV, 0x00),
413 regulator_desc_s2mps13_ldo(5, MIN_800_MV, STEP_12_5_MV, 0x00),
414 regulator_desc_s2mps13_ldo(6, MIN_800_MV, STEP_12_5_MV, 0x00),
415 regulator_desc_s2mps13_ldo(7, MIN_1000_MV, STEP_25_MV, 0x08),
416 regulator_desc_s2mps13_ldo(8, MIN_1000_MV, STEP_25_MV, 0x08),
417 regulator_desc_s2mps13_ldo(9, MIN_1000_MV, STEP_25_MV, 0x08),
418 regulator_desc_s2mps13_ldo(10, MIN_1400_MV, STEP_50_MV, 0x0C),
419 regulator_desc_s2mps13_ldo(11, MIN_800_MV, STEP_25_MV, 0x10),
420 regulator_desc_s2mps13_ldo(12, MIN_800_MV, STEP_25_MV, 0x10),
421 regulator_desc_s2mps13_ldo(13, MIN_800_MV, STEP_25_MV, 0x10),
422 regulator_desc_s2mps13_ldo(14, MIN_800_MV, STEP_12_5_MV, 0x00),
423 regulator_desc_s2mps13_ldo(15, MIN_800_MV, STEP_12_5_MV, 0x00),
424 regulator_desc_s2mps13_ldo(16, MIN_1400_MV, STEP_50_MV, 0x0C),
425 regulator_desc_s2mps13_ldo(17, MIN_1400_MV, STEP_50_MV, 0x0C),
426 regulator_desc_s2mps13_ldo(18, MIN_1000_MV, STEP_25_MV, 0x08),
427 regulator_desc_s2mps13_ldo(19, MIN_1000_MV, STEP_25_MV, 0x08),
428 regulator_desc_s2mps13_ldo(20, MIN_1400_MV, STEP_50_MV, 0x0C),
429 regulator_desc_s2mps13_ldo(21, MIN_1000_MV, STEP_25_MV, 0x08),
430 regulator_desc_s2mps13_ldo(22, MIN_1000_MV, STEP_25_MV, 0x08),
431 regulator_desc_s2mps13_ldo(23, MIN_800_MV, STEP_12_5_MV, 0x00),
432 regulator_desc_s2mps13_ldo(24, MIN_800_MV, STEP_12_5_MV, 0x00),
433 regulator_desc_s2mps13_ldo(25, MIN_1400_MV, STEP_50_MV, 0x0C),
434 regulator_desc_s2mps13_ldo(26, MIN_1400_MV, STEP_50_MV, 0x0C),
435 regulator_desc_s2mps13_ldo(27, MIN_1400_MV, STEP_50_MV, 0x0C),
436 regulator_desc_s2mps13_ldo(28, MIN_1000_MV, STEP_25_MV, 0x08),
437 regulator_desc_s2mps13_ldo(29, MIN_1400_MV, STEP_50_MV, 0x0C),
438 regulator_desc_s2mps13_ldo(30, MIN_1400_MV, STEP_50_MV, 0x0C),
439 regulator_desc_s2mps13_ldo(31, MIN_1000_MV, STEP_25_MV, 0x08),
440 regulator_desc_s2mps13_ldo(32, MIN_1000_MV, STEP_25_MV, 0x08),
441 regulator_desc_s2mps13_ldo(33, MIN_1400_MV, STEP_50_MV, 0x0C),
442 regulator_desc_s2mps13_ldo(34, MIN_1000_MV, STEP_25_MV, 0x08),
443 regulator_desc_s2mps13_ldo(35, MIN_1400_MV, STEP_50_MV, 0x0C),
444 regulator_desc_s2mps13_ldo(36, MIN_800_MV, STEP_12_5_MV, 0x00),
445 regulator_desc_s2mps13_ldo(37, MIN_1000_MV, STEP_25_MV, 0x08),
446 regulator_desc_s2mps13_ldo(38, MIN_1400_MV, STEP_50_MV, 0x0C),
447 regulator_desc_s2mps13_ldo(39, MIN_1000_MV, STEP_25_MV, 0x08),
448 regulator_desc_s2mps13_ldo(40, MIN_1400_MV, STEP_50_MV, 0x0C),
449 regulator_desc_s2mps13_buck(1, MIN_500_MV, STEP_6_25_MV, 0x10),
450 regulator_desc_s2mps13_buck(2, MIN_500_MV, STEP_6_25_MV, 0x10),
451 regulator_desc_s2mps13_buck(3, MIN_500_MV, STEP_6_25_MV, 0x10),
452 regulator_desc_s2mps13_buck(4, MIN_500_MV, STEP_6_25_MV, 0x10),
453 regulator_desc_s2mps13_buck(5, MIN_500_MV, STEP_6_25_MV, 0x10),
454 regulator_desc_s2mps13_buck(6, MIN_500_MV, STEP_6_25_MV, 0x10),
455 regulator_desc_s2mps13_buck(7, MIN_500_MV, STEP_6_25_MV, 0x10),
456 regulator_desc_s2mps13_buck(8, MIN_1000_MV, STEP_12_5_MV, 0x20),
457 regulator_desc_s2mps13_buck(9, MIN_1000_MV, STEP_12_5_MV, 0x20),
458 regulator_desc_s2mps13_buck(10, MIN_500_MV, STEP_6_25_MV, 0x10),
459};
460
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100461static int s2mps14_regulator_enable(struct regulator_dev *rdev)
462{
463 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
464 unsigned int val;
465
Chanwoo Choi00e25732014-06-25 16:14:45 +0900466 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900467 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900468 case S2MPS14X:
469 if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
470 val = S2MPS14_ENABLE_SUSPEND;
471 else if (gpio_is_valid(s2mps11->ext_control_gpio[rdev_get_id(rdev)]))
472 val = S2MPS14_ENABLE_EXT_CONTROL;
473 else
474 val = rdev->desc->enable_mask;
475 break;
476 case S2MPU02:
477 if (s2mps11->s2mps14_suspend_state & (1 << rdev_get_id(rdev)))
478 val = S2MPU02_ENABLE_SUSPEND;
479 else
480 val = rdev->desc->enable_mask;
481 break;
482 default:
483 return -EINVAL;
484 };
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100485
486 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
487 rdev->desc->enable_mask, val);
488}
489
490static int s2mps14_regulator_set_suspend_disable(struct regulator_dev *rdev)
491{
492 int ret;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900493 unsigned int val, state;
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100494 struct s2mps11_info *s2mps11 = rdev_get_drvdata(rdev);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900495 int rdev_id = rdev_get_id(rdev);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100496
Chanwoo Choi00e25732014-06-25 16:14:45 +0900497 /* Below LDO should be always on or does not support suspend mode. */
498 switch (s2mps11->dev_type) {
Chanwoo Choi76b98402014-11-18 17:59:40 +0900499 case S2MPS13X:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900500 case S2MPS14X:
501 switch (rdev_id) {
502 case S2MPS14_LDO3:
503 return 0;
504 default:
505 state = S2MPS14_ENABLE_SUSPEND;
506 break;
507 };
508 break;
509 case S2MPU02:
510 switch (rdev_id) {
511 case S2MPU02_LDO13:
512 case S2MPU02_LDO14:
513 case S2MPU02_LDO15:
514 case S2MPU02_LDO17:
515 case S2MPU02_BUCK7:
516 state = S2MPU02_DISABLE_SUSPEND;
517 break;
518 default:
519 state = S2MPU02_ENABLE_SUSPEND;
520 break;
521 };
522 break;
523 default:
524 return -EINVAL;
525 };
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100526
527 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
528 if (ret < 0)
529 return ret;
530
531 s2mps11->s2mps14_suspend_state |= (1 << rdev_get_id(rdev));
532 /*
533 * Don't enable suspend mode if regulator is already disabled because
534 * this would effectively for a short time turn on the regulator after
535 * resuming.
536 * However we still want to toggle the suspend_state bit for regulator
537 * in case if it got enabled before suspending the system.
538 */
539 if (!(val & rdev->desc->enable_mask))
540 return 0;
541
542 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Chanwoo Choi00e25732014-06-25 16:14:45 +0900543 rdev->desc->enable_mask, state);
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100544}
545
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100546static struct regulator_ops s2mps14_reg_ops = {
547 .list_voltage = regulator_list_voltage_linear,
548 .map_voltage = regulator_map_voltage_linear,
549 .is_enabled = regulator_is_enabled_regmap,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100550 .enable = s2mps14_regulator_enable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100551 .disable = regulator_disable_regmap,
552 .get_voltage_sel = regulator_get_voltage_sel_regmap,
553 .set_voltage_sel = regulator_set_voltage_sel_regmap,
554 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Krzysztof Kozlowski05be09b2014-03-07 11:10:51 +0100555 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100556};
557
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530558#define regulator_desc_s2mps14_ldo(num, min, step) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100559 .name = "LDO"#num, \
560 .id = S2MPS14_LDO##num, \
561 .ops = &s2mps14_reg_ops, \
562 .type = REGULATOR_VOLTAGE, \
563 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530564 .min_uV = min, \
565 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100566 .n_voltages = S2MPS14_LDO_N_VOLTAGES, \
567 .vsel_reg = S2MPS14_REG_L1CTRL + num - 1, \
568 .vsel_mask = S2MPS14_LDO_VSEL_MASK, \
569 .enable_reg = S2MPS14_REG_L1CTRL + num - 1, \
570 .enable_mask = S2MPS14_ENABLE_MASK \
571}
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530572
573#define regulator_desc_s2mps14_buck(num, min, step) { \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100574 .name = "BUCK"#num, \
575 .id = S2MPS14_BUCK##num, \
576 .ops = &s2mps14_reg_ops, \
577 .type = REGULATOR_VOLTAGE, \
578 .owner = THIS_MODULE, \
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530579 .min_uV = min, \
580 .uV_step = step, \
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100581 .n_voltages = S2MPS14_BUCK_N_VOLTAGES, \
582 .linear_min_sel = S2MPS14_BUCK1235_START_SEL, \
583 .ramp_delay = S2MPS14_BUCK_RAMP_DELAY, \
584 .vsel_reg = S2MPS14_REG_B1CTRL2 + (num - 1) * 2, \
585 .vsel_mask = S2MPS14_BUCK_VSEL_MASK, \
586 .enable_reg = S2MPS14_REG_B1CTRL1 + (num - 1) * 2, \
587 .enable_mask = S2MPS14_ENABLE_MASK \
588}
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100589
590static const struct regulator_desc s2mps14_regulators[] = {
Amit Daniel Kachhap5a867cf2014-07-15 16:32:53 +0530591 regulator_desc_s2mps14_ldo(1, MIN_800_MV, STEP_12_5_MV),
592 regulator_desc_s2mps14_ldo(2, MIN_800_MV, STEP_12_5_MV),
593 regulator_desc_s2mps14_ldo(3, MIN_800_MV, STEP_25_MV),
594 regulator_desc_s2mps14_ldo(4, MIN_800_MV, STEP_25_MV),
595 regulator_desc_s2mps14_ldo(5, MIN_800_MV, STEP_12_5_MV),
596 regulator_desc_s2mps14_ldo(6, MIN_800_MV, STEP_12_5_MV),
597 regulator_desc_s2mps14_ldo(7, MIN_800_MV, STEP_25_MV),
598 regulator_desc_s2mps14_ldo(8, MIN_1800_MV, STEP_25_MV),
599 regulator_desc_s2mps14_ldo(9, MIN_800_MV, STEP_12_5_MV),
600 regulator_desc_s2mps14_ldo(10, MIN_800_MV, STEP_12_5_MV),
601 regulator_desc_s2mps14_ldo(11, MIN_800_MV, STEP_25_MV),
602 regulator_desc_s2mps14_ldo(12, MIN_1800_MV, STEP_25_MV),
603 regulator_desc_s2mps14_ldo(13, MIN_1800_MV, STEP_25_MV),
604 regulator_desc_s2mps14_ldo(14, MIN_1800_MV, STEP_25_MV),
605 regulator_desc_s2mps14_ldo(15, MIN_1800_MV, STEP_25_MV),
606 regulator_desc_s2mps14_ldo(16, MIN_1800_MV, STEP_25_MV),
607 regulator_desc_s2mps14_ldo(17, MIN_1800_MV, STEP_25_MV),
608 regulator_desc_s2mps14_ldo(18, MIN_1800_MV, STEP_25_MV),
609 regulator_desc_s2mps14_ldo(19, MIN_800_MV, STEP_25_MV),
610 regulator_desc_s2mps14_ldo(20, MIN_800_MV, STEP_25_MV),
611 regulator_desc_s2mps14_ldo(21, MIN_800_MV, STEP_25_MV),
612 regulator_desc_s2mps14_ldo(22, MIN_800_MV, STEP_12_5_MV),
613 regulator_desc_s2mps14_ldo(23, MIN_800_MV, STEP_25_MV),
614 regulator_desc_s2mps14_ldo(24, MIN_1800_MV, STEP_25_MV),
615 regulator_desc_s2mps14_ldo(25, MIN_1800_MV, STEP_25_MV),
616 regulator_desc_s2mps14_buck(1, MIN_600_MV, STEP_6_25_MV),
617 regulator_desc_s2mps14_buck(2, MIN_600_MV, STEP_6_25_MV),
618 regulator_desc_s2mps14_buck(3, MIN_600_MV, STEP_6_25_MV),
619 regulator_desc_s2mps14_buck(4, MIN_1400_MV, STEP_12_5_MV),
620 regulator_desc_s2mps14_buck(5, MIN_600_MV, STEP_6_25_MV),
Sangbeom Kimcb746852012-07-11 21:08:17 +0900621};
622
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200623static int s2mps14_pmic_enable_ext_control(struct s2mps11_info *s2mps11,
624 struct regulator_dev *rdev)
625{
626 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
627 rdev->desc->enable_mask, S2MPS14_ENABLE_EXT_CONTROL);
628}
629
630static void s2mps14_pmic_dt_parse_ext_control_gpio(struct platform_device *pdev,
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200631 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
632{
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200633 int *gpio = s2mps11->ext_control_gpio;
634 unsigned int i;
635 unsigned int valid_regulators[3] = { S2MPS14_LDO10, S2MPS14_LDO11,
636 S2MPS14_LDO12 };
637
638 for (i = 0; i < ARRAY_SIZE(valid_regulators); i++) {
639 unsigned int reg = valid_regulators[i];
640
641 if (!rdata[reg].init_data || !rdata[reg].of_node)
642 continue;
643
644 gpio[reg] = of_get_named_gpio(rdata[reg].of_node,
645 "samsung,ext-control-gpios", 0);
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200646 if (gpio_is_valid(gpio[reg]))
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200647 dev_dbg(&pdev->dev, "Using GPIO %d for ext-control over %d/%s\n",
648 gpio[reg], reg, rdata[reg].name);
649 }
650}
651
652static int s2mps11_pmic_dt_parse(struct platform_device *pdev,
Chanwoo Choi00e25732014-06-25 16:14:45 +0900653 struct of_regulator_match *rdata, struct s2mps11_info *s2mps11)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200654{
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200655 struct device_node *reg_np;
656
657 reg_np = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
658 if (!reg_np) {
659 dev_err(&pdev->dev, "could not find regulators sub-node\n");
660 return -EINVAL;
661 }
662
663 of_regulator_match(&pdev->dev, reg_np, rdata, s2mps11->rdev_num);
Chanwoo Choi00e25732014-06-25 16:14:45 +0900664 if (s2mps11->dev_type == S2MPS14X)
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200665 s2mps14_pmic_dt_parse_ext_control_gpio(pdev, rdata, s2mps11);
666
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200667 of_node_put(reg_np);
668
669 return 0;
670}
671
Chanwoo Choi00e25732014-06-25 16:14:45 +0900672static int s2mpu02_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
673{
674 unsigned int ramp_val, ramp_shift, ramp_reg;
675
676 switch (rdev_get_id(rdev)) {
677 case S2MPU02_BUCK1:
678 ramp_shift = S2MPU02_BUCK1_RAMP_SHIFT;
679 break;
680 case S2MPU02_BUCK2:
681 ramp_shift = S2MPU02_BUCK2_RAMP_SHIFT;
682 break;
683 case S2MPU02_BUCK3:
684 ramp_shift = S2MPU02_BUCK3_RAMP_SHIFT;
685 break;
686 case S2MPU02_BUCK4:
687 ramp_shift = S2MPU02_BUCK4_RAMP_SHIFT;
688 break;
689 default:
690 return 0;
691 }
692 ramp_reg = S2MPU02_REG_RAMP1;
693 ramp_val = get_ramp_delay(ramp_delay);
694
695 return regmap_update_bits(rdev->regmap, ramp_reg,
696 S2MPU02_BUCK1234_RAMP_MASK << ramp_shift,
697 ramp_val << ramp_shift);
698}
699
700static struct regulator_ops s2mpu02_ldo_ops = {
701 .list_voltage = regulator_list_voltage_linear,
702 .map_voltage = regulator_map_voltage_linear,
703 .is_enabled = regulator_is_enabled_regmap,
704 .enable = s2mps14_regulator_enable,
705 .disable = regulator_disable_regmap,
706 .get_voltage_sel = regulator_get_voltage_sel_regmap,
707 .set_voltage_sel = regulator_set_voltage_sel_regmap,
708 .set_voltage_time_sel = regulator_set_voltage_time_sel,
709 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
710};
711
712static struct regulator_ops s2mpu02_buck_ops = {
713 .list_voltage = regulator_list_voltage_linear,
714 .map_voltage = regulator_map_voltage_linear,
715 .is_enabled = regulator_is_enabled_regmap,
716 .enable = s2mps14_regulator_enable,
717 .disable = regulator_disable_regmap,
718 .get_voltage_sel = regulator_get_voltage_sel_regmap,
719 .set_voltage_sel = regulator_set_voltage_sel_regmap,
720 .set_voltage_time_sel = regulator_set_voltage_time_sel,
721 .set_suspend_disable = s2mps14_regulator_set_suspend_disable,
722 .set_ramp_delay = s2mpu02_set_ramp_delay,
723};
724
725#define regulator_desc_s2mpu02_ldo1(num) { \
726 .name = "LDO"#num, \
727 .id = S2MPU02_LDO##num, \
728 .ops = &s2mpu02_ldo_ops, \
729 .type = REGULATOR_VOLTAGE, \
730 .owner = THIS_MODULE, \
731 .min_uV = S2MPU02_LDO_MIN_900MV, \
732 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
733 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
734 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
735 .vsel_reg = S2MPU02_REG_L1CTRL, \
736 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
737 .enable_reg = S2MPU02_REG_L1CTRL, \
738 .enable_mask = S2MPU02_ENABLE_MASK \
739}
740#define regulator_desc_s2mpu02_ldo2(num) { \
741 .name = "LDO"#num, \
742 .id = S2MPU02_LDO##num, \
743 .ops = &s2mpu02_ldo_ops, \
744 .type = REGULATOR_VOLTAGE, \
745 .owner = THIS_MODULE, \
746 .min_uV = S2MPU02_LDO_MIN_1050MV, \
747 .uV_step = S2MPU02_LDO_STEP_25MV, \
748 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
749 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
750 .vsel_reg = S2MPU02_REG_L2CTRL1, \
751 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
752 .enable_reg = S2MPU02_REG_L2CTRL1, \
753 .enable_mask = S2MPU02_ENABLE_MASK \
754}
755#define regulator_desc_s2mpu02_ldo3(num) { \
756 .name = "LDO"#num, \
757 .id = S2MPU02_LDO##num, \
758 .ops = &s2mpu02_ldo_ops, \
759 .type = REGULATOR_VOLTAGE, \
760 .owner = THIS_MODULE, \
761 .min_uV = S2MPU02_LDO_MIN_900MV, \
762 .uV_step = S2MPU02_LDO_STEP_12_5MV, \
763 .linear_min_sel = S2MPU02_LDO_GROUP1_START_SEL, \
764 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
765 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
766 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
767 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
768 .enable_mask = S2MPU02_ENABLE_MASK \
769}
770#define regulator_desc_s2mpu02_ldo4(num) { \
771 .name = "LDO"#num, \
772 .id = S2MPU02_LDO##num, \
773 .ops = &s2mpu02_ldo_ops, \
774 .type = REGULATOR_VOLTAGE, \
775 .owner = THIS_MODULE, \
776 .min_uV = S2MPU02_LDO_MIN_1050MV, \
777 .uV_step = S2MPU02_LDO_STEP_25MV, \
778 .linear_min_sel = S2MPU02_LDO_GROUP2_START_SEL, \
779 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
780 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
781 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
782 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
783 .enable_mask = S2MPU02_ENABLE_MASK \
784}
785#define regulator_desc_s2mpu02_ldo5(num) { \
786 .name = "LDO"#num, \
787 .id = S2MPU02_LDO##num, \
788 .ops = &s2mpu02_ldo_ops, \
789 .type = REGULATOR_VOLTAGE, \
790 .owner = THIS_MODULE, \
791 .min_uV = S2MPU02_LDO_MIN_1600MV, \
792 .uV_step = S2MPU02_LDO_STEP_50MV, \
793 .linear_min_sel = S2MPU02_LDO_GROUP3_START_SEL, \
794 .n_voltages = S2MPU02_LDO_N_VOLTAGES, \
795 .vsel_reg = S2MPU02_REG_L3CTRL + num - 3, \
796 .vsel_mask = S2MPU02_LDO_VSEL_MASK, \
797 .enable_reg = S2MPU02_REG_L3CTRL + num - 3, \
798 .enable_mask = S2MPU02_ENABLE_MASK \
799}
800
801#define regulator_desc_s2mpu02_buck1234(num) { \
802 .name = "BUCK"#num, \
803 .id = S2MPU02_BUCK##num, \
804 .ops = &s2mpu02_buck_ops, \
805 .type = REGULATOR_VOLTAGE, \
806 .owner = THIS_MODULE, \
807 .min_uV = S2MPU02_BUCK1234_MIN_600MV, \
808 .uV_step = S2MPU02_BUCK1234_STEP_6_25MV, \
809 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
810 .linear_min_sel = S2MPU02_BUCK1234_START_SEL, \
811 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
812 .vsel_reg = S2MPU02_REG_B1CTRL2 + (num - 1) * 2, \
813 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
814 .enable_reg = S2MPU02_REG_B1CTRL1 + (num - 1) * 2, \
815 .enable_mask = S2MPU02_ENABLE_MASK \
816}
817#define regulator_desc_s2mpu02_buck5(num) { \
818 .name = "BUCK"#num, \
819 .id = S2MPU02_BUCK##num, \
820 .ops = &s2mpu02_ldo_ops, \
821 .type = REGULATOR_VOLTAGE, \
822 .owner = THIS_MODULE, \
823 .min_uV = S2MPU02_BUCK5_MIN_1081_25MV, \
824 .uV_step = S2MPU02_BUCK5_STEP_6_25MV, \
825 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
826 .linear_min_sel = S2MPU02_BUCK5_START_SEL, \
827 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
828 .vsel_reg = S2MPU02_REG_B5CTRL2, \
829 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
830 .enable_reg = S2MPU02_REG_B5CTRL1, \
831 .enable_mask = S2MPU02_ENABLE_MASK \
832}
833#define regulator_desc_s2mpu02_buck6(num) { \
834 .name = "BUCK"#num, \
835 .id = S2MPU02_BUCK##num, \
836 .ops = &s2mpu02_ldo_ops, \
837 .type = REGULATOR_VOLTAGE, \
838 .owner = THIS_MODULE, \
839 .min_uV = S2MPU02_BUCK6_MIN_1700MV, \
840 .uV_step = S2MPU02_BUCK6_STEP_2_50MV, \
841 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
842 .linear_min_sel = S2MPU02_BUCK6_START_SEL, \
843 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
844 .vsel_reg = S2MPU02_REG_B6CTRL2, \
845 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
846 .enable_reg = S2MPU02_REG_B6CTRL1, \
847 .enable_mask = S2MPU02_ENABLE_MASK \
848}
849#define regulator_desc_s2mpu02_buck7(num) { \
850 .name = "BUCK"#num, \
851 .id = S2MPU02_BUCK##num, \
852 .ops = &s2mpu02_ldo_ops, \
853 .type = REGULATOR_VOLTAGE, \
854 .owner = THIS_MODULE, \
855 .min_uV = S2MPU02_BUCK7_MIN_900MV, \
856 .uV_step = S2MPU02_BUCK7_STEP_6_25MV, \
857 .n_voltages = S2MPU02_BUCK_N_VOLTAGES, \
858 .linear_min_sel = S2MPU02_BUCK7_START_SEL, \
859 .ramp_delay = S2MPU02_BUCK_RAMP_DELAY, \
860 .vsel_reg = S2MPU02_REG_B7CTRL2, \
861 .vsel_mask = S2MPU02_BUCK_VSEL_MASK, \
862 .enable_reg = S2MPU02_REG_B7CTRL1, \
863 .enable_mask = S2MPU02_ENABLE_MASK \
864}
865
866static const struct regulator_desc s2mpu02_regulators[] = {
867 regulator_desc_s2mpu02_ldo1(1),
868 regulator_desc_s2mpu02_ldo2(2),
869 regulator_desc_s2mpu02_ldo4(3),
870 regulator_desc_s2mpu02_ldo5(4),
871 regulator_desc_s2mpu02_ldo4(5),
872 regulator_desc_s2mpu02_ldo3(6),
873 regulator_desc_s2mpu02_ldo3(7),
874 regulator_desc_s2mpu02_ldo4(8),
875 regulator_desc_s2mpu02_ldo5(9),
876 regulator_desc_s2mpu02_ldo3(10),
877 regulator_desc_s2mpu02_ldo4(11),
878 regulator_desc_s2mpu02_ldo5(12),
879 regulator_desc_s2mpu02_ldo5(13),
880 regulator_desc_s2mpu02_ldo5(14),
881 regulator_desc_s2mpu02_ldo5(15),
882 regulator_desc_s2mpu02_ldo5(16),
883 regulator_desc_s2mpu02_ldo4(17),
884 regulator_desc_s2mpu02_ldo5(18),
885 regulator_desc_s2mpu02_ldo3(19),
886 regulator_desc_s2mpu02_ldo4(20),
887 regulator_desc_s2mpu02_ldo5(21),
888 regulator_desc_s2mpu02_ldo5(22),
889 regulator_desc_s2mpu02_ldo5(23),
890 regulator_desc_s2mpu02_ldo4(24),
891 regulator_desc_s2mpu02_ldo5(25),
892 regulator_desc_s2mpu02_ldo4(26),
893 regulator_desc_s2mpu02_ldo5(27),
894 regulator_desc_s2mpu02_ldo5(28),
895 regulator_desc_s2mpu02_buck1234(1),
896 regulator_desc_s2mpu02_buck1234(2),
897 regulator_desc_s2mpu02_buck1234(3),
898 regulator_desc_s2mpu02_buck1234(4),
899 regulator_desc_s2mpu02_buck5(5),
900 regulator_desc_s2mpu02_buck6(6),
901 regulator_desc_s2mpu02_buck7(7),
902};
903
Bill Pembertona5023572012-11-19 13:22:22 -0500904static int s2mps11_pmic_probe(struct platform_device *pdev)
Sangbeom Kimcb746852012-07-11 21:08:17 +0900905{
906 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200907 struct sec_platform_data *pdata = NULL;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100908 struct of_regulator_match *rdata = NULL;
Sangbeom Kimcb746852012-07-11 21:08:17 +0900909 struct regulator_config config = { };
Sangbeom Kimcb746852012-07-11 21:08:17 +0900910 struct s2mps11_info *s2mps11;
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100911 int i, ret = 0;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100912 const struct regulator_desc *regulators;
Sangbeom Kimcb746852012-07-11 21:08:17 +0900913
Sangbeom Kimcb746852012-07-11 21:08:17 +0900914 s2mps11 = devm_kzalloc(&pdev->dev, sizeof(struct s2mps11_info),
915 GFP_KERNEL);
916 if (!s2mps11)
917 return -ENOMEM;
918
Chanwoo Choi00e25732014-06-25 16:14:45 +0900919 s2mps11->dev_type = platform_get_device_id(pdev)->driver_data;
920 switch (s2mps11->dev_type) {
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100921 case S2MPS11X:
922 s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
923 regulators = s2mps11_regulators;
924 break;
Chanwoo Choi76b98402014-11-18 17:59:40 +0900925 case S2MPS13X:
926 s2mps11->rdev_num = ARRAY_SIZE(s2mps13_regulators);
927 regulators = s2mps13_regulators;
928 break;
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +0100929 case S2MPS14X:
930 s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
931 regulators = s2mps14_regulators;
932 break;
Chanwoo Choi00e25732014-06-25 16:14:45 +0900933 case S2MPU02:
934 s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
935 regulators = s2mpu02_regulators;
936 break;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100937 default:
Chanwoo Choi00e25732014-06-25 16:14:45 +0900938 dev_err(&pdev->dev, "Invalid device type: %u\n",
939 s2mps11->dev_type);
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100940 return -EINVAL;
941 };
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100942
Krzysztof Kozlowski80e82ac2014-10-16 10:23:28 +0200943 s2mps11->ext_control_gpio = devm_kmalloc(&pdev->dev,
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200944 sizeof(*s2mps11->ext_control_gpio) * s2mps11->rdev_num,
945 GFP_KERNEL);
946 if (!s2mps11->ext_control_gpio)
947 return -ENOMEM;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200948 /*
949 * 0 is a valid GPIO so initialize all GPIO-s to negative value
950 * to indicate that external control won't be used for this regulator.
951 */
952 for (i = 0; i < s2mps11->rdev_num; i++)
953 s2mps11->ext_control_gpio[i] = -EINVAL;
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200954
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +0530955 if (!iodev->dev->of_node) {
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200956 if (iodev->pdata) {
957 pdata = iodev->pdata;
Yadwinder Singh Brar6c683c92013-06-29 18:21:18 +0530958 goto common_reg;
959 } else {
960 dev_err(pdev->dev.parent,
961 "Platform data or DT node not supplied\n");
962 return -ENODEV;
963 }
964 }
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530965
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100966 rdata = kzalloc(sizeof(*rdata) * s2mps11->rdev_num, GFP_KERNEL);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100967 if (!rdata)
968 return -ENOMEM;
969
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100970 for (i = 0; i < s2mps11->rdev_num; i++)
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530971 rdata[i].name = regulators[i].name;
972
Chanwoo Choi00e25732014-06-25 16:14:45 +0900973 ret = s2mps11_pmic_dt_parse(pdev, rdata, s2mps11);
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200974 if (ret)
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +0100975 goto out;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530976
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530977common_reg:
978 platform_set_drvdata(pdev, s2mps11);
Sangbeom Kimcb746852012-07-11 21:08:17 +0900979
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530980 config.dev = &pdev->dev;
Krzysztof Kozlowski1b1ccee2013-12-11 15:07:43 +0100981 config.regmap = iodev->regmap_pmic;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530982 config.driver_data = s2mps11;
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200983 config.ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
Markus Pargmann1de38212014-11-03 19:12:04 +0100984 config.ena_gpio_initialized = true;
Krzysztof Kozlowski0f4cc282014-03-03 16:53:51 +0100985 for (i = 0; i < s2mps11->rdev_num; i++) {
Krzysztof Kozlowski31195252014-02-28 11:01:48 +0100986 struct regulator_dev *regulator;
987
Krzysztof Kozlowski01170382014-04-14 10:09:06 +0200988 if (pdata) {
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530989 config.init_data = pdata->regulators[i].initdata;
Krzysztof Kozlowski54820f52014-01-30 14:51:19 +0100990 config.of_node = pdata->regulators[i].reg_node;
Yadwinder Singh Brara50c6b32013-06-29 18:21:17 +0530991 } else {
992 config.init_data = rdata[i].init_data;
993 config.of_node = rdata[i].of_node;
994 }
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +0200995 config.ena_gpio = s2mps11->ext_control_gpio[i];
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +0200996
Krzysztof Kozlowski31195252014-02-28 11:01:48 +0100997 regulator = devm_regulator_register(&pdev->dev,
Sachin Kamatd55cd792013-09-04 12:12:15 +0530998 &regulators[i], &config);
Krzysztof Kozlowski31195252014-02-28 11:01:48 +0100999 if (IS_ERR(regulator)) {
1000 ret = PTR_ERR(regulator);
Axel Lin232b2502012-07-12 09:37:37 +08001001 dev_err(&pdev->dev, "regulator init failed for %d\n",
1002 i);
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001003 goto out;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001004 }
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001005
Krzysztof Kozlowskide5d0562014-04-30 10:40:42 +02001006 if (gpio_is_valid(s2mps11->ext_control_gpio[i])) {
Krzysztof Kozlowski97f53d72014-04-14 10:09:07 +02001007 ret = s2mps14_pmic_enable_ext_control(s2mps11,
1008 regulator);
1009 if (ret < 0) {
1010 dev_err(&pdev->dev,
1011 "failed to enable GPIO control over %s: %d\n",
1012 regulator->desc->name, ret);
1013 goto out;
1014 }
1015 }
Sangbeom Kimcb746852012-07-11 21:08:17 +09001016 }
1017
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001018out:
1019 kfree(rdata);
1020
1021 return ret;
Sangbeom Kimcb746852012-07-11 21:08:17 +09001022}
1023
1024static const struct platform_device_id s2mps11_pmic_id[] = {
Krzysztof Kozlowski3e80f952014-02-28 11:01:50 +01001025 { "s2mps11-pmic", S2MPS11X},
Chanwoo Choi76b98402014-11-18 17:59:40 +09001026 { "s2mps13-pmic", S2MPS13X},
Krzysztof Kozlowski15f77302014-03-07 11:10:50 +01001027 { "s2mps14-pmic", S2MPS14X},
Chanwoo Choi00e25732014-06-25 16:14:45 +09001028 { "s2mpu02-pmic", S2MPU02},
Sangbeom Kimcb746852012-07-11 21:08:17 +09001029 { },
1030};
1031MODULE_DEVICE_TABLE(platform, s2mps11_pmic_id);
1032
1033static struct platform_driver s2mps11_pmic_driver = {
1034 .driver = {
1035 .name = "s2mps11-pmic",
1036 .owner = THIS_MODULE,
1037 },
1038 .probe = s2mps11_pmic_probe,
Sangbeom Kimcb746852012-07-11 21:08:17 +09001039 .id_table = s2mps11_pmic_id,
1040};
1041
1042static int __init s2mps11_pmic_init(void)
1043{
1044 return platform_driver_register(&s2mps11_pmic_driver);
1045}
1046subsys_initcall(s2mps11_pmic_init);
1047
1048static void __exit s2mps11_pmic_exit(void)
1049{
1050 platform_driver_unregister(&s2mps11_pmic_driver);
1051}
1052module_exit(s2mps11_pmic_exit);
1053
1054/* Module information */
1055MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
Axel Lincad35c32014-07-23 16:47:31 +08001056MODULE_DESCRIPTION("SAMSUNG S2MPS11/S2MPS14/S2MPU02 Regulator Driver");
Sangbeom Kimcb746852012-07-11 21:08:17 +09001057MODULE_LICENSE("GPL");