blob: 09b0d8c20a9d0b6980a60f26df02c92c5cb397b4 [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
Krzysztof Kozlowski78ce6122014-10-29 12:14:52 +0100102/* Some BUCKs and LDOs supports Normal[ON/OFF] mode during suspend */
103static int max77686_set_suspend_disable(struct regulator_dev *rdev)
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530104{
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
Krzysztof Kozlowski78ce6122014-10-29 12:14:52 +0100198 if (max77686->opmode[id] == MAX77686_OFF_PWRREQ)
199 max77686->opmode[id] = MAX77686_NORMAL;
200
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530201 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
202 rdev->desc->enable_mask,
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100203 max77686->opmode[id] << shift);
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530204}
205
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530206static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
207{
208 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
209
210 switch (ramp_delay) {
211 case 1 ... 13750:
212 ramp_value = RAMP_RATE_13P75MV;
213 break;
214 case 13751 ... 27500:
215 ramp_value = RAMP_RATE_27P5MV;
216 break;
217 case 27501 ... 55000:
218 ramp_value = RAMP_RATE_55MV;
219 break;
220 case 55001 ... 100000:
221 break;
222 default:
223 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
224 rdev->desc->name, ramp_delay);
225 }
226
227 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
228 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
229}
230
Jonghwa Lee133d4012012-06-01 13:17:14 +0900231static struct regulator_ops max77686_ops = {
232 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800233 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900234 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530235 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900236 .disable = regulator_disable_regmap,
237 .get_voltage_sel = regulator_get_voltage_sel_regmap,
238 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530239 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530240 .set_suspend_mode = max77686_set_suspend_mode,
241};
242
243static struct regulator_ops max77686_ldo_ops = {
244 .list_voltage = regulator_list_voltage_linear,
245 .map_voltage = regulator_map_voltage_linear,
246 .is_enabled = regulator_is_enabled_regmap,
247 .enable = max77686_enable,
248 .disable = regulator_disable_regmap,
249 .get_voltage_sel = regulator_get_voltage_sel_regmap,
250 .set_voltage_sel = regulator_set_voltage_sel_regmap,
251 .set_voltage_time_sel = regulator_set_voltage_time_sel,
252 .set_suspend_mode = max77686_ldo_set_suspend_mode,
Krzysztof Kozlowski78ce6122014-10-29 12:14:52 +0100253 .set_suspend_disable = max77686_set_suspend_disable,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530254};
255
256static struct regulator_ops max77686_buck1_ops = {
257 .list_voltage = regulator_list_voltage_linear,
258 .map_voltage = regulator_map_voltage_linear,
259 .is_enabled = regulator_is_enabled_regmap,
260 .enable = max77686_enable,
261 .disable = regulator_disable_regmap,
262 .get_voltage_sel = regulator_get_voltage_sel_regmap,
263 .set_voltage_sel = regulator_set_voltage_sel_regmap,
264 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Krzysztof Kozlowski78ce6122014-10-29 12:14:52 +0100265 .set_suspend_disable = max77686_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900266};
267
268static struct regulator_ops max77686_buck_dvs_ops = {
269 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800270 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900271 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530272 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900273 .disable = regulator_disable_regmap,
274 .get_voltage_sel = regulator_get_voltage_sel_regmap,
275 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530276 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530277 .set_ramp_delay = max77686_set_ramp_delay,
Krzysztof Kozlowski78ce6122014-10-29 12:14:52 +0100278 .set_suspend_disable = max77686_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900279};
280
281#define regulator_desc_ldo(num) { \
282 .name = "LDO"#num, \
283 .id = MAX77686_LDO##num, \
284 .ops = &max77686_ops, \
285 .type = REGULATOR_VOLTAGE, \
286 .owner = THIS_MODULE, \
287 .min_uV = MAX77686_LDO_MINUV, \
288 .uV_step = MAX77686_LDO_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530289 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900290 .n_voltages = MAX77686_VSEL_MASK + 1, \
291 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
292 .vsel_mask = MAX77686_VSEL_MASK, \
293 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
294 .enable_mask = MAX77686_OPMODE_MASK \
295 << MAX77686_OPMODE_SHIFT, \
296}
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530297#define regulator_desc_lpm_ldo(num) { \
298 .name = "LDO"#num, \
299 .id = MAX77686_LDO##num, \
300 .ops = &max77686_ldo_ops, \
301 .type = REGULATOR_VOLTAGE, \
302 .owner = THIS_MODULE, \
303 .min_uV = MAX77686_LDO_MINUV, \
304 .uV_step = MAX77686_LDO_UVSTEP, \
305 .ramp_delay = MAX77686_RAMP_DELAY, \
306 .n_voltages = MAX77686_VSEL_MASK + 1, \
307 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
308 .vsel_mask = MAX77686_VSEL_MASK, \
309 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
310 .enable_mask = MAX77686_OPMODE_MASK \
311 << MAX77686_OPMODE_SHIFT, \
312}
Jonghwa Lee133d4012012-06-01 13:17:14 +0900313#define regulator_desc_ldo_low(num) { \
314 .name = "LDO"#num, \
315 .id = MAX77686_LDO##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530316 .ops = &max77686_ldo_ops, \
317 .type = REGULATOR_VOLTAGE, \
318 .owner = THIS_MODULE, \
319 .min_uV = MAX77686_LDO_LOW_MINUV, \
320 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
321 .ramp_delay = MAX77686_RAMP_DELAY, \
322 .n_voltages = MAX77686_VSEL_MASK + 1, \
323 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
324 .vsel_mask = MAX77686_VSEL_MASK, \
325 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
326 .enable_mask = MAX77686_OPMODE_MASK \
327 << MAX77686_OPMODE_SHIFT, \
328}
329#define regulator_desc_ldo1_low(num) { \
330 .name = "LDO"#num, \
331 .id = MAX77686_LDO##num, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900332 .ops = &max77686_ops, \
333 .type = REGULATOR_VOLTAGE, \
334 .owner = THIS_MODULE, \
335 .min_uV = MAX77686_LDO_LOW_MINUV, \
336 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530337 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900338 .n_voltages = MAX77686_VSEL_MASK + 1, \
339 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
340 .vsel_mask = MAX77686_VSEL_MASK, \
341 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
342 .enable_mask = MAX77686_OPMODE_MASK \
343 << MAX77686_OPMODE_SHIFT, \
344}
345#define regulator_desc_buck(num) { \
346 .name = "BUCK"#num, \
347 .id = MAX77686_BUCK##num, \
348 .ops = &max77686_ops, \
349 .type = REGULATOR_VOLTAGE, \
350 .owner = THIS_MODULE, \
351 .min_uV = MAX77686_BUCK_MINUV, \
352 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530353 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900354 .n_voltages = MAX77686_VSEL_MASK + 1, \
355 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
356 .vsel_mask = MAX77686_VSEL_MASK, \
357 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
358 .enable_mask = MAX77686_OPMODE_MASK, \
359}
360#define regulator_desc_buck1(num) { \
361 .name = "BUCK"#num, \
362 .id = MAX77686_BUCK##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530363 .ops = &max77686_buck1_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900364 .type = REGULATOR_VOLTAGE, \
365 .owner = THIS_MODULE, \
366 .min_uV = MAX77686_BUCK_MINUV, \
367 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530368 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900369 .n_voltages = MAX77686_VSEL_MASK + 1, \
370 .vsel_reg = MAX77686_REG_BUCK1OUT, \
371 .vsel_mask = MAX77686_VSEL_MASK, \
372 .enable_reg = MAX77686_REG_BUCK1CTRL, \
373 .enable_mask = MAX77686_OPMODE_MASK, \
374}
375#define regulator_desc_buck_dvs(num) { \
376 .name = "BUCK"#num, \
377 .id = MAX77686_BUCK##num, \
Axel Lin74adfee52012-06-04 10:19:18 +0800378 .ops = &max77686_buck_dvs_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900379 .type = REGULATOR_VOLTAGE, \
380 .owner = THIS_MODULE, \
381 .min_uV = MAX77686_DVS_MINUV, \
382 .uV_step = MAX77686_DVS_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530383 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900384 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
385 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
386 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
387 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
388 .enable_mask = MAX77686_OPMODE_MASK \
389 << MAX77686_OPMODE_BUCK234_SHIFT, \
390}
391
Krzysztof Kozlowski73dbdf82014-10-27 16:03:42 +0100392static const struct regulator_desc regulators[] = {
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530393 regulator_desc_ldo1_low(1),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900394 regulator_desc_ldo_low(2),
395 regulator_desc_ldo(3),
396 regulator_desc_ldo(4),
397 regulator_desc_ldo(5),
398 regulator_desc_ldo_low(6),
399 regulator_desc_ldo_low(7),
400 regulator_desc_ldo_low(8),
401 regulator_desc_ldo(9),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530402 regulator_desc_lpm_ldo(10),
403 regulator_desc_lpm_ldo(11),
404 regulator_desc_lpm_ldo(12),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900405 regulator_desc_ldo(13),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530406 regulator_desc_lpm_ldo(14),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900407 regulator_desc_ldo_low(15),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530408 regulator_desc_lpm_ldo(16),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900409 regulator_desc_ldo(17),
410 regulator_desc_ldo(18),
411 regulator_desc_ldo(19),
412 regulator_desc_ldo(20),
413 regulator_desc_ldo(21),
414 regulator_desc_ldo(22),
415 regulator_desc_ldo(23),
416 regulator_desc_ldo(24),
417 regulator_desc_ldo(25),
418 regulator_desc_ldo(26),
419 regulator_desc_buck1(1),
420 regulator_desc_buck_dvs(2),
421 regulator_desc_buck_dvs(3),
422 regulator_desc_buck_dvs(4),
423 regulator_desc_buck(5),
424 regulator_desc_buck(6),
425 regulator_desc_buck(7),
426 regulator_desc_buck(8),
427 regulator_desc_buck(9),
428};
429
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530430#ifdef CONFIG_OF
Axel Linb19f5752013-01-25 10:24:18 +0800431static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530432 struct max77686_platform_data *pdata)
433{
Axel Linb19f5752013-01-25 10:24:18 +0800434 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530435 struct device_node *pmic_np, *regulators_np;
436 struct max77686_regulator_data *rdata;
437 struct of_regulator_match rmatch;
438 unsigned int i;
439
440 pmic_np = iodev->dev->of_node;
Sachin Kamat398a6612014-02-14 17:19:47 +0530441 regulators_np = of_get_child_by_name(pmic_np, "voltage-regulators");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530442 if (!regulators_np) {
Axel Linb19f5752013-01-25 10:24:18 +0800443 dev_err(&pdev->dev, "could not find regulators sub-node\n");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530444 return -EINVAL;
445 }
446
447 pdata->num_regulators = ARRAY_SIZE(regulators);
Axel Linb19f5752013-01-25 10:24:18 +0800448 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530449 pdata->num_regulators, GFP_KERNEL);
450 if (!rdata) {
Sachin Kamatda0ee722014-02-17 14:33:32 +0530451 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530452 return -ENOMEM;
453 }
454
455 for (i = 0; i < pdata->num_regulators; i++) {
456 rmatch.name = regulators[i].name;
457 rmatch.init_data = NULL;
Yadwinder Singh Brard1ef0652012-07-06 14:50:08 +0530458 rmatch.of_node = NULL;
Axel Linb19f5752013-01-25 10:24:18 +0800459 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530460 rdata[i].initdata = rmatch.init_data;
Axel Lin2c58e262012-08-05 10:09:57 +0800461 rdata[i].of_node = rmatch.of_node;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530462 }
463
464 pdata->regulators = rdata;
Sachin Kamatda0ee722014-02-17 14:33:32 +0530465 of_node_put(regulators_np);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530466
467 return 0;
468}
469#else
Axel Linb19f5752013-01-25 10:24:18 +0800470static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530471 struct max77686_platform_data *pdata)
472{
473 return 0;
474}
475#endif /* CONFIG_OF */
476
Bill Pembertona5023572012-11-19 13:22:22 -0500477static int max77686_pmic_probe(struct platform_device *pdev)
Jonghwa Lee133d4012012-06-01 13:17:14 +0900478{
479 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
480 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900481 struct max77686_data *max77686;
Axel Lincb44cde2012-08-05 10:11:16 +0800482 int i, ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800483 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900484
485 dev_dbg(&pdev->dev, "%s\n", __func__);
486
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530487 if (!pdata) {
488 dev_err(&pdev->dev, "no platform data found for regulator\n");
489 return -ENODEV;
490 }
491
492 if (iodev->dev->of_node) {
Axel Linb19f5752013-01-25 10:24:18 +0800493 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530494 if (ret)
495 return ret;
496 }
497
498 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800499 dev_err(&pdev->dev,
500 "Invalid initial data for regulator's initialiation\n");
501 return -EINVAL;
502 }
503
Jonghwa Lee133d4012012-06-01 13:17:14 +0900504 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
505 GFP_KERNEL);
506 if (!max77686)
507 return -ENOMEM;
508
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530509 config.dev = &pdev->dev;
510 config.regmap = iodev->regmap;
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530511 config.driver_data = max77686;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900512 platform_set_drvdata(pdev, max77686);
513
Axel Linab0d1cb2012-06-08 12:03:04 +0800514 for (i = 0; i < MAX77686_REGULATORS; i++) {
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100515 struct regulator_dev *rdev;
516
Axel Linab0d1cb2012-06-08 12:03:04 +0800517 config.init_data = pdata->regulators[i].initdata;
Axel Lin2c58e262012-08-05 10:09:57 +0800518 config.of_node = pdata->regulators[i].of_node;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900519
Krzysztof Kozlowski68c5d182014-10-27 13:11:48 +0100520 max77686->opmode[i] = regulators[i].enable_mask >>
521 max77686_get_opmode_shift(i);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100522 rdev = devm_regulator_register(&pdev->dev,
Sachin Kamat44815b42013-09-04 11:07:54 +0530523 &regulators[i], &config);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100524 if (IS_ERR(rdev)) {
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530525 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900526 "regulator init failed for %d\n", i);
Krzysztof Kozlowskib0c13e82014-03-10 09:32:45 +0100527 return PTR_ERR(rdev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900528 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900529 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800530
Jonghwa Lee133d4012012-06-01 13:17:14 +0900531 return 0;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900532}
533
534static const struct platform_device_id max77686_pmic_id[] = {
535 {"max77686-pmic", 0},
536 { },
537};
538MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
539
540static struct platform_driver max77686_pmic_driver = {
541 .driver = {
542 .name = "max77686-pmic",
543 .owner = THIS_MODULE,
544 },
545 .probe = max77686_pmic_probe,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900546 .id_table = max77686_pmic_id,
547};
548
549static int __init max77686_pmic_init(void)
550{
551 return platform_driver_register(&max77686_pmic_driver);
552}
553subsys_initcall(max77686_pmic_init);
554
555static void __exit max77686_pmic_cleanup(void)
556{
557 platform_driver_unregister(&max77686_pmic_driver);
558}
559module_exit(max77686_pmic_cleanup);
560
561MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
562MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
563MODULE_LICENSE("GPL");