blob: 0c1541754e8cb33000618f7a2e412a7eaf234747 [file] [log] [blame]
Jonghwa Lee133d4012012-06-01 13:17:14 +09001/*
2 * max77686.c - Regulator driver for the Maxim 77686
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@smasung.com>
6 * Jonghwa Lee <jonghwa3.lee@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This driver is based on max8997.c
23 */
24
25#include <linux/kernel.h>
26#include <linux/bug.h>
Jonghwa Lee133d4012012-06-01 13:17:14 +090027#include <linux/err.h>
28#include <linux/gpio.h>
29#include <linux/slab.h>
30#include <linux/platform_device.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +053033#include <linux/regulator/of_regulator.h>
Jonghwa Lee133d4012012-06-01 13:17:14 +090034#include <linux/mfd/max77686.h>
35#include <linux/mfd/max77686-private.h>
36
37#define MAX77686_LDO_MINUV 800000
38#define MAX77686_LDO_UVSTEP 50000
39#define MAX77686_LDO_LOW_MINUV 800000
40#define MAX77686_LDO_LOW_UVSTEP 25000
41#define MAX77686_BUCK_MINUV 750000
42#define MAX77686_BUCK_UVSTEP 50000
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +053043#define MAX77686_RAMP_DELAY 100000 /* uV/us */
44#define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
Jonghwa Lee133d4012012-06-01 13:17:14 +090045#define MAX77686_DVS_MINUV 600000
46#define MAX77686_DVS_UVSTEP 12500
47
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +010048/*
49 * Values used for configuring LDOs and bucks.
50 * Forcing low power mode: LDO1, 3-5, 9, 13, 17-26
51 */
52#define MAX77686_LDO_LOWPOWER 0x1
53/*
54 * On/off controlled by PWRREQ:
55 * - LDO2, 6-8, 10-12, 14-16
56 * - buck[1234]
57 */
58#define MAX77686_OFF_PWRREQ 0x1
59/* Low power mode controlled by PWRREQ: All LDOs */
60#define MAX77686_LDO_LOWPOWER_PWRREQ 0x2
61/* Forcing low power mode: buck[234] */
62#define MAX77686_BUCK_LOWPOWER 0x2
63#define MAX77686_NORMAL 0x3
64
Jonghwa Lee133d4012012-06-01 13:17:14 +090065#define MAX77686_OPMODE_SHIFT 6
66#define MAX77686_OPMODE_BUCK234_SHIFT 4
67#define MAX77686_OPMODE_MASK 0x3
68
69#define MAX77686_VSEL_MASK 0x3F
70#define MAX77686_DVS_VSEL_MASK 0xFF
71
72#define MAX77686_RAMP_RATE_MASK 0xC0
73
74#define MAX77686_REGULATORS MAX77686_REG_MAX
75#define MAX77686_LDOS 26
76
77enum max77686_ramp_rate {
78 RAMP_RATE_13P75MV,
79 RAMP_RATE_27P5MV,
80 RAMP_RATE_55MV,
81 RAMP_RATE_NO_CTRL, /* 100mV/us */
82};
83
84struct max77686_data {
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +053085 unsigned int opmode[MAX77686_REGULATORS];
Jonghwa Lee133d4012012-06-01 13:17:14 +090086};
87
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +010088static unsigned int max77686_get_opmode_shift(int id)
89{
90 switch (id) {
91 case MAX77686_BUCK1:
92 case MAX77686_BUCK5 ... MAX77686_BUCK9:
93 return 0;
94 case MAX77686_BUCK2 ... MAX77686_BUCK4:
95 return MAX77686_OPMODE_BUCK234_SHIFT;
96 default:
97 /* all LDOs */
98 return MAX77686_OPMODE_SHIFT;
99 }
100}
101
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530102/* Some BUCKS supports Normal[ON/OFF] mode during suspend */
103static int max77686_buck_set_suspend_disable(struct regulator_dev *rdev)
104{
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100105 unsigned int val, shift;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530106 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +0800107 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530108
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100109 shift = max77686_get_opmode_shift(id);
110 val = MAX77686_OFF_PWRREQ;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530111
Axel Lin3ecf1982013-04-27 16:18:16 +0800112 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100113 rdev->desc->enable_mask, val << shift);
Axel Lin3ecf1982013-04-27 16:18:16 +0800114 if (ret)
115 return ret;
116
Thiago Farina40e72142013-02-13 17:52:26 -0200117 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800118 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530119}
120
121/* Some LDOs supports [LPM/Normal]ON mode during suspend state */
122static int max77686_set_suspend_mode(struct regulator_dev *rdev,
123 unsigned int mode)
124{
125 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
126 unsigned int val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800127 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530128
129 /* BUCK[5-9] doesn't support this feature */
Thiago Farina40e72142013-02-13 17:52:26 -0200130 if (id >= MAX77686_BUCK5)
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530131 return 0;
132
133 switch (mode) {
134 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100135 val = MAX77686_LDO_LOWPOWER_PWRREQ;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530136 break;
137 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100138 val = MAX77686_NORMAL;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530139 break;
140 default:
141 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
142 rdev->desc->name, mode);
143 return -EINVAL;
144 }
145
Axel Lin3ecf1982013-04-27 16:18:16 +0800146 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100147 rdev->desc->enable_mask,
148 val << MAX77686_OPMODE_SHIFT);
Axel Lin3ecf1982013-04-27 16:18:16 +0800149 if (ret)
150 return ret;
151
Thiago Farina40e72142013-02-13 17:52:26 -0200152 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800153 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530154}
155
156/* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
157static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
158 unsigned int mode)
159{
160 unsigned int val;
161 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +0800162 int ret;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530163
164 switch (mode) {
165 case REGULATOR_MODE_STANDBY: /* switch off */
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100166 val = MAX77686_OFF_PWRREQ;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530167 break;
168 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100169 val = MAX77686_LDO_LOWPOWER_PWRREQ;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530170 break;
171 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100172 val = MAX77686_NORMAL;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530173 break;
174 default:
175 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
176 rdev->desc->name, mode);
177 return -EINVAL;
178 }
179
Axel Lin3ecf1982013-04-27 16:18:16 +0800180 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100181 rdev->desc->enable_mask,
182 val << MAX77686_OPMODE_SHIFT);
Axel Lin3ecf1982013-04-27 16:18:16 +0800183 if (ret)
184 return ret;
185
Thiago Farina40e72142013-02-13 17:52:26 -0200186 max77686->opmode[rdev_get_id(rdev)] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800187 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530188}
189
Yadwinder Singh Brar38d34032012-10-22 09:39:28 +0530190static int max77686_enable(struct regulator_dev *rdev)
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530191{
192 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100193 unsigned int shift;
194 int id = rdev_get_id(rdev);
195
196 shift = max77686_get_opmode_shift(id);
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530197
198 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
199 rdev->desc->enable_mask,
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100200 max77686->opmode[id] << shift);
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530201}
202
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530203static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
204{
205 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
206
207 switch (ramp_delay) {
208 case 1 ... 13750:
209 ramp_value = RAMP_RATE_13P75MV;
210 break;
211 case 13751 ... 27500:
212 ramp_value = RAMP_RATE_27P5MV;
213 break;
214 case 27501 ... 55000:
215 ramp_value = RAMP_RATE_55MV;
216 break;
217 case 55001 ... 100000:
218 break;
219 default:
220 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
221 rdev->desc->name, ramp_delay);
222 }
223
224 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
225 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
226}
227
Jonghwa Lee133d4012012-06-01 13:17:14 +0900228static struct regulator_ops max77686_ops = {
229 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800230 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900231 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530232 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900233 .disable = regulator_disable_regmap,
234 .get_voltage_sel = regulator_get_voltage_sel_regmap,
235 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530236 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530237 .set_suspend_mode = max77686_set_suspend_mode,
238};
239
240static struct regulator_ops max77686_ldo_ops = {
241 .list_voltage = regulator_list_voltage_linear,
242 .map_voltage = regulator_map_voltage_linear,
243 .is_enabled = regulator_is_enabled_regmap,
244 .enable = max77686_enable,
245 .disable = regulator_disable_regmap,
246 .get_voltage_sel = regulator_get_voltage_sel_regmap,
247 .set_voltage_sel = regulator_set_voltage_sel_regmap,
248 .set_voltage_time_sel = regulator_set_voltage_time_sel,
249 .set_suspend_mode = max77686_ldo_set_suspend_mode,
250};
251
252static struct regulator_ops max77686_buck1_ops = {
253 .list_voltage = regulator_list_voltage_linear,
254 .map_voltage = regulator_map_voltage_linear,
255 .is_enabled = regulator_is_enabled_regmap,
256 .enable = max77686_enable,
257 .disable = regulator_disable_regmap,
258 .get_voltage_sel = regulator_get_voltage_sel_regmap,
259 .set_voltage_sel = regulator_set_voltage_sel_regmap,
260 .set_voltage_time_sel = regulator_set_voltage_time_sel,
261 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900262};
263
264static struct regulator_ops max77686_buck_dvs_ops = {
265 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800266 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900267 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530268 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900269 .disable = regulator_disable_regmap,
270 .get_voltage_sel = regulator_get_voltage_sel_regmap,
271 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530272 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530273 .set_ramp_delay = max77686_set_ramp_delay,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530274 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900275};
276
277#define regulator_desc_ldo(num) { \
278 .name = "LDO"#num, \
279 .id = MAX77686_LDO##num, \
280 .ops = &max77686_ops, \
281 .type = REGULATOR_VOLTAGE, \
282 .owner = THIS_MODULE, \
283 .min_uV = MAX77686_LDO_MINUV, \
284 .uV_step = MAX77686_LDO_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530285 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900286 .n_voltages = MAX77686_VSEL_MASK + 1, \
287 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
288 .vsel_mask = MAX77686_VSEL_MASK, \
289 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
290 .enable_mask = MAX77686_OPMODE_MASK \
291 << MAX77686_OPMODE_SHIFT, \
292}
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530293#define regulator_desc_lpm_ldo(num) { \
294 .name = "LDO"#num, \
295 .id = MAX77686_LDO##num, \
296 .ops = &max77686_ldo_ops, \
297 .type = REGULATOR_VOLTAGE, \
298 .owner = THIS_MODULE, \
299 .min_uV = MAX77686_LDO_MINUV, \
300 .uV_step = MAX77686_LDO_UVSTEP, \
301 .ramp_delay = MAX77686_RAMP_DELAY, \
302 .n_voltages = MAX77686_VSEL_MASK + 1, \
303 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
304 .vsel_mask = MAX77686_VSEL_MASK, \
305 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
306 .enable_mask = MAX77686_OPMODE_MASK \
307 << MAX77686_OPMODE_SHIFT, \
308}
Jonghwa Lee133d4012012-06-01 13:17:14 +0900309#define regulator_desc_ldo_low(num) { \
310 .name = "LDO"#num, \
311 .id = MAX77686_LDO##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530312 .ops = &max77686_ldo_ops, \
313 .type = REGULATOR_VOLTAGE, \
314 .owner = THIS_MODULE, \
315 .min_uV = MAX77686_LDO_LOW_MINUV, \
316 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
317 .ramp_delay = MAX77686_RAMP_DELAY, \
318 .n_voltages = MAX77686_VSEL_MASK + 1, \
319 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
320 .vsel_mask = MAX77686_VSEL_MASK, \
321 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
322 .enable_mask = MAX77686_OPMODE_MASK \
323 << MAX77686_OPMODE_SHIFT, \
324}
325#define regulator_desc_ldo1_low(num) { \
326 .name = "LDO"#num, \
327 .id = MAX77686_LDO##num, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900328 .ops = &max77686_ops, \
329 .type = REGULATOR_VOLTAGE, \
330 .owner = THIS_MODULE, \
331 .min_uV = MAX77686_LDO_LOW_MINUV, \
332 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530333 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900334 .n_voltages = MAX77686_VSEL_MASK + 1, \
335 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
336 .vsel_mask = MAX77686_VSEL_MASK, \
337 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
338 .enable_mask = MAX77686_OPMODE_MASK \
339 << MAX77686_OPMODE_SHIFT, \
340}
341#define regulator_desc_buck(num) { \
342 .name = "BUCK"#num, \
343 .id = MAX77686_BUCK##num, \
344 .ops = &max77686_ops, \
345 .type = REGULATOR_VOLTAGE, \
346 .owner = THIS_MODULE, \
347 .min_uV = MAX77686_BUCK_MINUV, \
348 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530349 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900350 .n_voltages = MAX77686_VSEL_MASK + 1, \
351 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
352 .vsel_mask = MAX77686_VSEL_MASK, \
353 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
354 .enable_mask = MAX77686_OPMODE_MASK, \
355}
356#define regulator_desc_buck1(num) { \
357 .name = "BUCK"#num, \
358 .id = MAX77686_BUCK##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530359 .ops = &max77686_buck1_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900360 .type = REGULATOR_VOLTAGE, \
361 .owner = THIS_MODULE, \
362 .min_uV = MAX77686_BUCK_MINUV, \
363 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530364 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900365 .n_voltages = MAX77686_VSEL_MASK + 1, \
366 .vsel_reg = MAX77686_REG_BUCK1OUT, \
367 .vsel_mask = MAX77686_VSEL_MASK, \
368 .enable_reg = MAX77686_REG_BUCK1CTRL, \
369 .enable_mask = MAX77686_OPMODE_MASK, \
370}
371#define regulator_desc_buck_dvs(num) { \
372 .name = "BUCK"#num, \
373 .id = MAX77686_BUCK##num, \
Axel Lin74adfee52012-06-04 10:19:18 +0800374 .ops = &max77686_buck_dvs_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900375 .type = REGULATOR_VOLTAGE, \
376 .owner = THIS_MODULE, \
377 .min_uV = MAX77686_DVS_MINUV, \
378 .uV_step = MAX77686_DVS_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530379 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900380 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
381 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
382 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
383 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
384 .enable_mask = MAX77686_OPMODE_MASK \
385 << MAX77686_OPMODE_BUCK234_SHIFT, \
386}
387
Krzysztof Kozlowski73dbdf82014-10-27 16:03:42 +0100388static const struct regulator_desc regulators[] = {
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530389 regulator_desc_ldo1_low(1),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900390 regulator_desc_ldo_low(2),
391 regulator_desc_ldo(3),
392 regulator_desc_ldo(4),
393 regulator_desc_ldo(5),
394 regulator_desc_ldo_low(6),
395 regulator_desc_ldo_low(7),
396 regulator_desc_ldo_low(8),
397 regulator_desc_ldo(9),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530398 regulator_desc_lpm_ldo(10),
399 regulator_desc_lpm_ldo(11),
400 regulator_desc_lpm_ldo(12),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900401 regulator_desc_ldo(13),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530402 regulator_desc_lpm_ldo(14),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900403 regulator_desc_ldo_low(15),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530404 regulator_desc_lpm_ldo(16),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900405 regulator_desc_ldo(17),
406 regulator_desc_ldo(18),
407 regulator_desc_ldo(19),
408 regulator_desc_ldo(20),
409 regulator_desc_ldo(21),
410 regulator_desc_ldo(22),
411 regulator_desc_ldo(23),
412 regulator_desc_ldo(24),
413 regulator_desc_ldo(25),
414 regulator_desc_ldo(26),
415 regulator_desc_buck1(1),
416 regulator_desc_buck_dvs(2),
417 regulator_desc_buck_dvs(3),
418 regulator_desc_buck_dvs(4),
419 regulator_desc_buck(5),
420 regulator_desc_buck(6),
421 regulator_desc_buck(7),
422 regulator_desc_buck(8),
423 regulator_desc_buck(9),
424};
425
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530426#ifdef CONFIG_OF
Axel Linb19f5752013-01-25 10:24:18 +0800427static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530428 struct max77686_platform_data *pdata)
429{
Axel Linb19f5752013-01-25 10:24:18 +0800430 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530431 struct device_node *pmic_np, *regulators_np;
432 struct max77686_regulator_data *rdata;
433 struct of_regulator_match rmatch;
434 unsigned int i;
435
436 pmic_np = iodev->dev->of_node;
Sachin Kamat398a6612014-02-14 17:19:47 +0530437 regulators_np = of_get_child_by_name(pmic_np, "voltage-regulators");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530438 if (!regulators_np) {
Axel Linb19f5752013-01-25 10:24:18 +0800439 dev_err(&pdev->dev, "could not find regulators sub-node\n");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530440 return -EINVAL;
441 }
442
443 pdata->num_regulators = ARRAY_SIZE(regulators);
Axel Linb19f5752013-01-25 10:24:18 +0800444 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530445 pdata->num_regulators, GFP_KERNEL);
446 if (!rdata) {
Sachin Kamatda0ee722014-02-17 14:33:32 +0530447 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530448 return -ENOMEM;
449 }
450
451 for (i = 0; i < pdata->num_regulators; i++) {
452 rmatch.name = regulators[i].name;
453 rmatch.init_data = NULL;
Yadwinder Singh Brard1ef0652012-07-06 14:50:08 +0530454 rmatch.of_node = NULL;
Axel Linb19f5752013-01-25 10:24:18 +0800455 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530456 rdata[i].initdata = rmatch.init_data;
Axel Lin2c58e262012-08-05 10:09:57 +0800457 rdata[i].of_node = rmatch.of_node;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530458 }
459
460 pdata->regulators = rdata;
Sachin Kamatda0ee722014-02-17 14:33:32 +0530461 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530462
463 return 0;
464}
465#else
Axel Linb19f5752013-01-25 10:24:18 +0800466static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530467 struct max77686_platform_data *pdata)
468{
469 return 0;
470}
471#endif /* CONFIG_OF */
472
Bill Pembertona5023572012-11-19 13:22:22 -0500473static int max77686_pmic_probe(struct platform_device *pdev)
Jonghwa Lee133d4012012-06-01 13:17:14 +0900474{
475 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
476 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900477 struct max77686_data *max77686;
Axel Lincb44cde2012-08-05 10:11:16 +0800478 int i, ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800479 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900480
481 dev_dbg(&pdev->dev, "%s\n", __func__);
482
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530483 if (!pdata) {
484 dev_err(&pdev->dev, "no platform data found for regulator\n");
485 return -ENODEV;
486 }
487
488 if (iodev->dev->of_node) {
Axel Linb19f5752013-01-25 10:24:18 +0800489 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530490 if (ret)
491 return ret;
492 }
493
494 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800495 dev_err(&pdev->dev,
496 "Invalid initial data for regulator's initialiation\n");
497 return -EINVAL;
498 }
499
Jonghwa Lee133d4012012-06-01 13:17:14 +0900500 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
501 GFP_KERNEL);
502 if (!max77686)
503 return -ENOMEM;
504
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530505 config.dev = &pdev->dev;
506 config.regmap = iodev->regmap;
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530507 config.driver_data = max77686;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900508 platform_set_drvdata(pdev, max77686);
509
Axel Linab0d1cb2012-06-08 12:03:04 +0800510 for (i = 0; i < MAX77686_REGULATORS; i++) {
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100511 struct regulator_dev *rdev;
512
Axel Linab0d1cb2012-06-08 12:03:04 +0800513 config.init_data = pdata->regulators[i].initdata;
Axel Lin2c58e262012-08-05 10:09:57 +0800514 config.of_node = pdata->regulators[i].of_node;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900515
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100516 max77686->opmode[i] = regulators[i].enable_mask >>
517 max77686_get_opmode_shift(i);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100518 rdev = devm_regulator_register(&pdev->dev,
Sachin Kamat44815b42013-09-04 11:07:54 +0530519 &regulators[i], &config);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100520 if (IS_ERR(rdev)) {
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530521 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900522 "regulator init failed for %d\n", i);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100523 return PTR_ERR(rdev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900524 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900525 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800526
Jonghwa Lee133d4012012-06-01 13:17:14 +0900527 return 0;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900528}
529
530static const struct platform_device_id max77686_pmic_id[] = {
531 {"max77686-pmic", 0},
532 { },
533};
534MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
535
536static struct platform_driver max77686_pmic_driver = {
537 .driver = {
538 .name = "max77686-pmic",
539 .owner = THIS_MODULE,
540 },
541 .probe = max77686_pmic_probe,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900542 .id_table = max77686_pmic_id,
543};
544
545static int __init max77686_pmic_init(void)
546{
547 return platform_driver_register(&max77686_pmic_driver);
548}
549subsys_initcall(max77686_pmic_init);
550
551static void __exit max77686_pmic_cleanup(void)
552{
553 platform_driver_unregister(&max77686_pmic_driver);
554}
555module_exit(max77686_pmic_cleanup);
556
557MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
558MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
559MODULE_LICENSE("GPL");