blob: db01c09e98f5c835c71dce316ab31620eef82f1b [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
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053088/* Some BUCKS supports Normal[ON/OFF] mode during suspend */
89static int max77686_buck_set_suspend_disable(struct regulator_dev *rdev)
90{
91 unsigned int val;
92 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +080093 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053094
Thiago Farina40e72142013-02-13 17:52:26 -020095 if (id == MAX77686_BUCK1)
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +010096 val = MAX77686_OFF_PWRREQ;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053097 else
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +010098 val = MAX77686_OFF_PWRREQ << MAX77686_OPMODE_BUCK234_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053099
Axel Lin3ecf1982013-04-27 16:18:16 +0800100 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
101 rdev->desc->enable_mask, val);
102 if (ret)
103 return ret;
104
Thiago Farina40e72142013-02-13 17:52:26 -0200105 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800106 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530107}
108
109/* Some LDOs supports [LPM/Normal]ON mode during suspend state */
110static int max77686_set_suspend_mode(struct regulator_dev *rdev,
111 unsigned int mode)
112{
113 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
114 unsigned int val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800115 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530116
117 /* BUCK[5-9] doesn't support this feature */
Thiago Farina40e72142013-02-13 17:52:26 -0200118 if (id >= MAX77686_BUCK5)
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530119 return 0;
120
121 switch (mode) {
122 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +0100123 val = MAX77686_LDO_LOWPOWER_PWRREQ << MAX77686_OPMODE_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530124 break;
125 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +0100126 val = MAX77686_NORMAL << MAX77686_OPMODE_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530127 break;
128 default:
129 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
130 rdev->desc->name, mode);
131 return -EINVAL;
132 }
133
Axel Lin3ecf1982013-04-27 16:18:16 +0800134 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
135 rdev->desc->enable_mask, val);
136 if (ret)
137 return ret;
138
Thiago Farina40e72142013-02-13 17:52:26 -0200139 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800140 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530141}
142
143/* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
144static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
145 unsigned int mode)
146{
147 unsigned int val;
148 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +0800149 int ret;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530150
151 switch (mode) {
152 case REGULATOR_MODE_STANDBY: /* switch off */
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +0100153 val = MAX77686_OFF_PWRREQ << MAX77686_OPMODE_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530154 break;
155 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +0100156 val = MAX77686_LDO_LOWPOWER_PWRREQ << MAX77686_OPMODE_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530157 break;
158 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
Krzysztof Kozlowski7636f192014-10-27 13:11:47 +0100159 val = MAX77686_NORMAL << MAX77686_OPMODE_SHIFT;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530160 break;
161 default:
162 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
163 rdev->desc->name, mode);
164 return -EINVAL;
165 }
166
Axel Lin3ecf1982013-04-27 16:18:16 +0800167 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
168 rdev->desc->enable_mask, val);
169 if (ret)
170 return ret;
171
Thiago Farina40e72142013-02-13 17:52:26 -0200172 max77686->opmode[rdev_get_id(rdev)] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800173 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530174}
175
Yadwinder Singh Brar38d34032012-10-22 09:39:28 +0530176static int max77686_enable(struct regulator_dev *rdev)
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530177{
178 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
179
180 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
181 rdev->desc->enable_mask,
Thiago Farina40e72142013-02-13 17:52:26 -0200182 max77686->opmode[rdev_get_id(rdev)]);
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530183}
184
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530185static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
186{
187 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
188
189 switch (ramp_delay) {
190 case 1 ... 13750:
191 ramp_value = RAMP_RATE_13P75MV;
192 break;
193 case 13751 ... 27500:
194 ramp_value = RAMP_RATE_27P5MV;
195 break;
196 case 27501 ... 55000:
197 ramp_value = RAMP_RATE_55MV;
198 break;
199 case 55001 ... 100000:
200 break;
201 default:
202 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
203 rdev->desc->name, ramp_delay);
204 }
205
206 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
207 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
208}
209
Jonghwa Lee133d4012012-06-01 13:17:14 +0900210static struct regulator_ops max77686_ops = {
211 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800212 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900213 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530214 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900215 .disable = regulator_disable_regmap,
216 .get_voltage_sel = regulator_get_voltage_sel_regmap,
217 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530218 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530219 .set_suspend_mode = max77686_set_suspend_mode,
220};
221
222static struct regulator_ops max77686_ldo_ops = {
223 .list_voltage = regulator_list_voltage_linear,
224 .map_voltage = regulator_map_voltage_linear,
225 .is_enabled = regulator_is_enabled_regmap,
226 .enable = max77686_enable,
227 .disable = regulator_disable_regmap,
228 .get_voltage_sel = regulator_get_voltage_sel_regmap,
229 .set_voltage_sel = regulator_set_voltage_sel_regmap,
230 .set_voltage_time_sel = regulator_set_voltage_time_sel,
231 .set_suspend_mode = max77686_ldo_set_suspend_mode,
232};
233
234static struct regulator_ops max77686_buck1_ops = {
235 .list_voltage = regulator_list_voltage_linear,
236 .map_voltage = regulator_map_voltage_linear,
237 .is_enabled = regulator_is_enabled_regmap,
238 .enable = max77686_enable,
239 .disable = regulator_disable_regmap,
240 .get_voltage_sel = regulator_get_voltage_sel_regmap,
241 .set_voltage_sel = regulator_set_voltage_sel_regmap,
242 .set_voltage_time_sel = regulator_set_voltage_time_sel,
243 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900244};
245
246static struct regulator_ops max77686_buck_dvs_ops = {
247 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800248 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900249 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530250 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900251 .disable = regulator_disable_regmap,
252 .get_voltage_sel = regulator_get_voltage_sel_regmap,
253 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530254 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530255 .set_ramp_delay = max77686_set_ramp_delay,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530256 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900257};
258
259#define regulator_desc_ldo(num) { \
260 .name = "LDO"#num, \
261 .id = MAX77686_LDO##num, \
262 .ops = &max77686_ops, \
263 .type = REGULATOR_VOLTAGE, \
264 .owner = THIS_MODULE, \
265 .min_uV = MAX77686_LDO_MINUV, \
266 .uV_step = MAX77686_LDO_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530267 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900268 .n_voltages = MAX77686_VSEL_MASK + 1, \
269 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
270 .vsel_mask = MAX77686_VSEL_MASK, \
271 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
272 .enable_mask = MAX77686_OPMODE_MASK \
273 << MAX77686_OPMODE_SHIFT, \
274}
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530275#define regulator_desc_lpm_ldo(num) { \
276 .name = "LDO"#num, \
277 .id = MAX77686_LDO##num, \
278 .ops = &max77686_ldo_ops, \
279 .type = REGULATOR_VOLTAGE, \
280 .owner = THIS_MODULE, \
281 .min_uV = MAX77686_LDO_MINUV, \
282 .uV_step = MAX77686_LDO_UVSTEP, \
283 .ramp_delay = MAX77686_RAMP_DELAY, \
284 .n_voltages = MAX77686_VSEL_MASK + 1, \
285 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
286 .vsel_mask = MAX77686_VSEL_MASK, \
287 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
288 .enable_mask = MAX77686_OPMODE_MASK \
289 << MAX77686_OPMODE_SHIFT, \
290}
Jonghwa Lee133d4012012-06-01 13:17:14 +0900291#define regulator_desc_ldo_low(num) { \
292 .name = "LDO"#num, \
293 .id = MAX77686_LDO##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530294 .ops = &max77686_ldo_ops, \
295 .type = REGULATOR_VOLTAGE, \
296 .owner = THIS_MODULE, \
297 .min_uV = MAX77686_LDO_LOW_MINUV, \
298 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
299 .ramp_delay = MAX77686_RAMP_DELAY, \
300 .n_voltages = MAX77686_VSEL_MASK + 1, \
301 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
302 .vsel_mask = MAX77686_VSEL_MASK, \
303 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
304 .enable_mask = MAX77686_OPMODE_MASK \
305 << MAX77686_OPMODE_SHIFT, \
306}
307#define regulator_desc_ldo1_low(num) { \
308 .name = "LDO"#num, \
309 .id = MAX77686_LDO##num, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900310 .ops = &max77686_ops, \
311 .type = REGULATOR_VOLTAGE, \
312 .owner = THIS_MODULE, \
313 .min_uV = MAX77686_LDO_LOW_MINUV, \
314 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530315 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900316 .n_voltages = MAX77686_VSEL_MASK + 1, \
317 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
318 .vsel_mask = MAX77686_VSEL_MASK, \
319 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
320 .enable_mask = MAX77686_OPMODE_MASK \
321 << MAX77686_OPMODE_SHIFT, \
322}
323#define regulator_desc_buck(num) { \
324 .name = "BUCK"#num, \
325 .id = MAX77686_BUCK##num, \
326 .ops = &max77686_ops, \
327 .type = REGULATOR_VOLTAGE, \
328 .owner = THIS_MODULE, \
329 .min_uV = MAX77686_BUCK_MINUV, \
330 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530331 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900332 .n_voltages = MAX77686_VSEL_MASK + 1, \
333 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
334 .vsel_mask = MAX77686_VSEL_MASK, \
335 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
336 .enable_mask = MAX77686_OPMODE_MASK, \
337}
338#define regulator_desc_buck1(num) { \
339 .name = "BUCK"#num, \
340 .id = MAX77686_BUCK##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530341 .ops = &max77686_buck1_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900342 .type = REGULATOR_VOLTAGE, \
343 .owner = THIS_MODULE, \
344 .min_uV = MAX77686_BUCK_MINUV, \
345 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530346 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900347 .n_voltages = MAX77686_VSEL_MASK + 1, \
348 .vsel_reg = MAX77686_REG_BUCK1OUT, \
349 .vsel_mask = MAX77686_VSEL_MASK, \
350 .enable_reg = MAX77686_REG_BUCK1CTRL, \
351 .enable_mask = MAX77686_OPMODE_MASK, \
352}
353#define regulator_desc_buck_dvs(num) { \
354 .name = "BUCK"#num, \
355 .id = MAX77686_BUCK##num, \
Axel Lin74adfee52012-06-04 10:19:18 +0800356 .ops = &max77686_buck_dvs_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900357 .type = REGULATOR_VOLTAGE, \
358 .owner = THIS_MODULE, \
359 .min_uV = MAX77686_DVS_MINUV, \
360 .uV_step = MAX77686_DVS_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530361 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900362 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
363 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
364 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
365 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
366 .enable_mask = MAX77686_OPMODE_MASK \
367 << MAX77686_OPMODE_BUCK234_SHIFT, \
368}
369
Krzysztof Kozlowski73dbdf82014-10-27 16:03:42 +0100370static const struct regulator_desc regulators[] = {
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530371 regulator_desc_ldo1_low(1),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900372 regulator_desc_ldo_low(2),
373 regulator_desc_ldo(3),
374 regulator_desc_ldo(4),
375 regulator_desc_ldo(5),
376 regulator_desc_ldo_low(6),
377 regulator_desc_ldo_low(7),
378 regulator_desc_ldo_low(8),
379 regulator_desc_ldo(9),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530380 regulator_desc_lpm_ldo(10),
381 regulator_desc_lpm_ldo(11),
382 regulator_desc_lpm_ldo(12),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900383 regulator_desc_ldo(13),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530384 regulator_desc_lpm_ldo(14),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900385 regulator_desc_ldo_low(15),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530386 regulator_desc_lpm_ldo(16),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900387 regulator_desc_ldo(17),
388 regulator_desc_ldo(18),
389 regulator_desc_ldo(19),
390 regulator_desc_ldo(20),
391 regulator_desc_ldo(21),
392 regulator_desc_ldo(22),
393 regulator_desc_ldo(23),
394 regulator_desc_ldo(24),
395 regulator_desc_ldo(25),
396 regulator_desc_ldo(26),
397 regulator_desc_buck1(1),
398 regulator_desc_buck_dvs(2),
399 regulator_desc_buck_dvs(3),
400 regulator_desc_buck_dvs(4),
401 regulator_desc_buck(5),
402 regulator_desc_buck(6),
403 regulator_desc_buck(7),
404 regulator_desc_buck(8),
405 regulator_desc_buck(9),
406};
407
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530408#ifdef CONFIG_OF
Axel Linb19f5752013-01-25 10:24:18 +0800409static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530410 struct max77686_platform_data *pdata)
411{
Axel Linb19f5752013-01-25 10:24:18 +0800412 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530413 struct device_node *pmic_np, *regulators_np;
414 struct max77686_regulator_data *rdata;
415 struct of_regulator_match rmatch;
416 unsigned int i;
417
418 pmic_np = iodev->dev->of_node;
Sachin Kamat398a6612014-02-14 17:19:47 +0530419 regulators_np = of_get_child_by_name(pmic_np, "voltage-regulators");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530420 if (!regulators_np) {
Axel Linb19f5752013-01-25 10:24:18 +0800421 dev_err(&pdev->dev, "could not find regulators sub-node\n");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530422 return -EINVAL;
423 }
424
425 pdata->num_regulators = ARRAY_SIZE(regulators);
Axel Linb19f5752013-01-25 10:24:18 +0800426 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530427 pdata->num_regulators, GFP_KERNEL);
428 if (!rdata) {
Sachin Kamatda0ee722014-02-17 14:33:32 +0530429 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530430 return -ENOMEM;
431 }
432
433 for (i = 0; i < pdata->num_regulators; i++) {
434 rmatch.name = regulators[i].name;
435 rmatch.init_data = NULL;
Yadwinder Singh Brard1ef0652012-07-06 14:50:08 +0530436 rmatch.of_node = NULL;
Axel Linb19f5752013-01-25 10:24:18 +0800437 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530438 rdata[i].initdata = rmatch.init_data;
Axel Lin2c58e262012-08-05 10:09:57 +0800439 rdata[i].of_node = rmatch.of_node;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530440 }
441
442 pdata->regulators = rdata;
Sachin Kamatda0ee722014-02-17 14:33:32 +0530443 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530444
445 return 0;
446}
447#else
Axel Linb19f5752013-01-25 10:24:18 +0800448static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530449 struct max77686_platform_data *pdata)
450{
451 return 0;
452}
453#endif /* CONFIG_OF */
454
Bill Pembertona5023572012-11-19 13:22:22 -0500455static int max77686_pmic_probe(struct platform_device *pdev)
Jonghwa Lee133d4012012-06-01 13:17:14 +0900456{
457 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
458 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900459 struct max77686_data *max77686;
Axel Lincb44cde2012-08-05 10:11:16 +0800460 int i, ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800461 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900462
463 dev_dbg(&pdev->dev, "%s\n", __func__);
464
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530465 if (!pdata) {
466 dev_err(&pdev->dev, "no platform data found for regulator\n");
467 return -ENODEV;
468 }
469
470 if (iodev->dev->of_node) {
Axel Linb19f5752013-01-25 10:24:18 +0800471 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530472 if (ret)
473 return ret;
474 }
475
476 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800477 dev_err(&pdev->dev,
478 "Invalid initial data for regulator's initialiation\n");
479 return -EINVAL;
480 }
481
Jonghwa Lee133d4012012-06-01 13:17:14 +0900482 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
483 GFP_KERNEL);
484 if (!max77686)
485 return -ENOMEM;
486
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530487 config.dev = &pdev->dev;
488 config.regmap = iodev->regmap;
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530489 config.driver_data = max77686;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900490 platform_set_drvdata(pdev, max77686);
491
Axel Linab0d1cb2012-06-08 12:03:04 +0800492 for (i = 0; i < MAX77686_REGULATORS; i++) {
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100493 struct regulator_dev *rdev;
494
Axel Linab0d1cb2012-06-08 12:03:04 +0800495 config.init_data = pdata->regulators[i].initdata;
Axel Lin2c58e262012-08-05 10:09:57 +0800496 config.of_node = pdata->regulators[i].of_node;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900497
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530498 max77686->opmode[i] = regulators[i].enable_mask;
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100499 rdev = devm_regulator_register(&pdev->dev,
Sachin Kamat44815b42013-09-04 11:07:54 +0530500 &regulators[i], &config);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100501 if (IS_ERR(rdev)) {
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530502 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900503 "regulator init failed for %d\n", i);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100504 return PTR_ERR(rdev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900505 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900506 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800507
Jonghwa Lee133d4012012-06-01 13:17:14 +0900508 return 0;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900509}
510
511static const struct platform_device_id max77686_pmic_id[] = {
512 {"max77686-pmic", 0},
513 { },
514};
515MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
516
517static struct platform_driver max77686_pmic_driver = {
518 .driver = {
519 .name = "max77686-pmic",
520 .owner = THIS_MODULE,
521 },
522 .probe = max77686_pmic_probe,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900523 .id_table = max77686_pmic_id,
524};
525
526static int __init max77686_pmic_init(void)
527{
528 return platform_driver_register(&max77686_pmic_driver);
529}
530subsys_initcall(max77686_pmic_init);
531
532static void __exit max77686_pmic_cleanup(void)
533{
534 platform_driver_unregister(&max77686_pmic_driver);
535}
536module_exit(max77686_pmic_cleanup);
537
538MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
539MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
540MODULE_LICENSE("GPL");