blob: 20935b1a6ed4fd85a4061f0d80fe94dfc529b9f6 [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>
27#include <linux/delay.h>
28#include <linux/err.h>
29#include <linux/gpio.h>
30#include <linux/slab.h>
31#include <linux/platform_device.h>
32#include <linux/regulator/driver.h>
33#include <linux/regulator/machine.h>
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +053034#include <linux/regulator/of_regulator.h>
Jonghwa Lee133d4012012-06-01 13:17:14 +090035#include <linux/mfd/max77686.h>
36#include <linux/mfd/max77686-private.h>
37
38#define MAX77686_LDO_MINUV 800000
39#define MAX77686_LDO_UVSTEP 50000
40#define MAX77686_LDO_LOW_MINUV 800000
41#define MAX77686_LDO_LOW_UVSTEP 25000
42#define MAX77686_BUCK_MINUV 750000
43#define MAX77686_BUCK_UVSTEP 50000
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +053044#define MAX77686_RAMP_DELAY 100000 /* uV/us */
45#define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
Jonghwa Lee133d4012012-06-01 13:17:14 +090046#define MAX77686_DVS_MINUV 600000
47#define MAX77686_DVS_UVSTEP 12500
48
49#define MAX77686_OPMODE_SHIFT 6
50#define MAX77686_OPMODE_BUCK234_SHIFT 4
51#define MAX77686_OPMODE_MASK 0x3
52
53#define MAX77686_VSEL_MASK 0x3F
54#define MAX77686_DVS_VSEL_MASK 0xFF
55
56#define MAX77686_RAMP_RATE_MASK 0xC0
57
58#define MAX77686_REGULATORS MAX77686_REG_MAX
59#define MAX77686_LDOS 26
60
61enum max77686_ramp_rate {
62 RAMP_RATE_13P75MV,
63 RAMP_RATE_27P5MV,
64 RAMP_RATE_55MV,
65 RAMP_RATE_NO_CTRL, /* 100mV/us */
66};
67
68struct max77686_data {
Axel Lincb44cde2012-08-05 10:11:16 +080069 struct regulator_dev *rdev[MAX77686_REGULATORS];
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +053070 unsigned int opmode[MAX77686_REGULATORS];
Jonghwa Lee133d4012012-06-01 13:17:14 +090071};
72
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053073/* Some BUCKS supports Normal[ON/OFF] mode during suspend */
74static int max77686_buck_set_suspend_disable(struct regulator_dev *rdev)
75{
76 unsigned int val;
77 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +080078 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053079
Thiago Farina40e72142013-02-13 17:52:26 -020080 if (id == MAX77686_BUCK1)
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053081 val = 0x1;
82 else
83 val = 0x1 << MAX77686_OPMODE_BUCK234_SHIFT;
84
Axel Lin3ecf1982013-04-27 16:18:16 +080085 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
86 rdev->desc->enable_mask, val);
87 if (ret)
88 return ret;
89
Thiago Farina40e72142013-02-13 17:52:26 -020090 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +080091 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +053092}
93
94/* Some LDOs supports [LPM/Normal]ON mode during suspend state */
95static int max77686_set_suspend_mode(struct regulator_dev *rdev,
96 unsigned int mode)
97{
98 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
99 unsigned int val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800100 int ret, id = rdev_get_id(rdev);
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530101
102 /* BUCK[5-9] doesn't support this feature */
Thiago Farina40e72142013-02-13 17:52:26 -0200103 if (id >= MAX77686_BUCK5)
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530104 return 0;
105
106 switch (mode) {
107 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
108 val = 0x2 << MAX77686_OPMODE_SHIFT;
109 break;
110 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
111 val = 0x3 << MAX77686_OPMODE_SHIFT;
112 break;
113 default:
114 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
115 rdev->desc->name, mode);
116 return -EINVAL;
117 }
118
Axel Lin3ecf1982013-04-27 16:18:16 +0800119 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
120 rdev->desc->enable_mask, val);
121 if (ret)
122 return ret;
123
Thiago Farina40e72142013-02-13 17:52:26 -0200124 max77686->opmode[id] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800125 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530126}
127
128/* Some LDOs supports LPM-ON/OFF/Normal-ON mode during suspend state */
129static int max77686_ldo_set_suspend_mode(struct regulator_dev *rdev,
130 unsigned int mode)
131{
132 unsigned int val;
133 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
Axel Lin3ecf1982013-04-27 16:18:16 +0800134 int ret;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530135
136 switch (mode) {
137 case REGULATOR_MODE_STANDBY: /* switch off */
138 val = 0x1 << MAX77686_OPMODE_SHIFT;
139 break;
140 case REGULATOR_MODE_IDLE: /* ON in LP Mode */
141 val = 0x2 << MAX77686_OPMODE_SHIFT;
142 break;
143 case REGULATOR_MODE_NORMAL: /* ON in Normal Mode */
144 val = 0x3 << MAX77686_OPMODE_SHIFT;
145 break;
146 default:
147 pr_warn("%s: regulator_suspend_mode : 0x%x not supported\n",
148 rdev->desc->name, mode);
149 return -EINVAL;
150 }
151
Axel Lin3ecf1982013-04-27 16:18:16 +0800152 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
153 rdev->desc->enable_mask, val);
154 if (ret)
155 return ret;
156
Thiago Farina40e72142013-02-13 17:52:26 -0200157 max77686->opmode[rdev_get_id(rdev)] = val;
Axel Lin3ecf1982013-04-27 16:18:16 +0800158 return 0;
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530159}
160
Yadwinder Singh Brar38d34032012-10-22 09:39:28 +0530161static int max77686_enable(struct regulator_dev *rdev)
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530162{
163 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
164
165 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
166 rdev->desc->enable_mask,
Thiago Farina40e72142013-02-13 17:52:26 -0200167 max77686->opmode[rdev_get_id(rdev)]);
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530168}
169
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530170static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
171{
172 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
173
174 switch (ramp_delay) {
175 case 1 ... 13750:
176 ramp_value = RAMP_RATE_13P75MV;
177 break;
178 case 13751 ... 27500:
179 ramp_value = RAMP_RATE_27P5MV;
180 break;
181 case 27501 ... 55000:
182 ramp_value = RAMP_RATE_55MV;
183 break;
184 case 55001 ... 100000:
185 break;
186 default:
187 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
188 rdev->desc->name, ramp_delay);
189 }
190
191 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
192 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
193}
194
Jonghwa Lee133d4012012-06-01 13:17:14 +0900195static struct regulator_ops max77686_ops = {
196 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800197 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900198 .is_enabled = regulator_is_enabled_regmap,
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530199 .enable = max77686_enable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900200 .disable = regulator_disable_regmap,
201 .get_voltage_sel = regulator_get_voltage_sel_regmap,
202 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530203 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530204 .set_suspend_mode = max77686_set_suspend_mode,
205};
206
207static struct regulator_ops max77686_ldo_ops = {
208 .list_voltage = regulator_list_voltage_linear,
209 .map_voltage = regulator_map_voltage_linear,
210 .is_enabled = regulator_is_enabled_regmap,
211 .enable = max77686_enable,
212 .disable = regulator_disable_regmap,
213 .get_voltage_sel = regulator_get_voltage_sel_regmap,
214 .set_voltage_sel = regulator_set_voltage_sel_regmap,
215 .set_voltage_time_sel = regulator_set_voltage_time_sel,
216 .set_suspend_mode = max77686_ldo_set_suspend_mode,
217};
218
219static struct regulator_ops max77686_buck1_ops = {
220 .list_voltage = regulator_list_voltage_linear,
221 .map_voltage = regulator_map_voltage_linear,
222 .is_enabled = regulator_is_enabled_regmap,
223 .enable = max77686_enable,
224 .disable = regulator_disable_regmap,
225 .get_voltage_sel = regulator_get_voltage_sel_regmap,
226 .set_voltage_sel = regulator_set_voltage_sel_regmap,
227 .set_voltage_time_sel = regulator_set_voltage_time_sel,
228 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900229};
230
231static struct regulator_ops max77686_buck_dvs_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 Brarf5030712012-06-20 10:50:51 +0530240 .set_ramp_delay = max77686_set_ramp_delay,
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530241 .set_suspend_disable = max77686_buck_set_suspend_disable,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900242};
243
244#define regulator_desc_ldo(num) { \
245 .name = "LDO"#num, \
246 .id = MAX77686_LDO##num, \
247 .ops = &max77686_ops, \
248 .type = REGULATOR_VOLTAGE, \
249 .owner = THIS_MODULE, \
250 .min_uV = MAX77686_LDO_MINUV, \
251 .uV_step = MAX77686_LDO_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530252 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900253 .n_voltages = MAX77686_VSEL_MASK + 1, \
254 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
255 .vsel_mask = MAX77686_VSEL_MASK, \
256 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
257 .enable_mask = MAX77686_OPMODE_MASK \
258 << MAX77686_OPMODE_SHIFT, \
259}
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530260#define regulator_desc_lpm_ldo(num) { \
261 .name = "LDO"#num, \
262 .id = MAX77686_LDO##num, \
263 .ops = &max77686_ldo_ops, \
264 .type = REGULATOR_VOLTAGE, \
265 .owner = THIS_MODULE, \
266 .min_uV = MAX77686_LDO_MINUV, \
267 .uV_step = MAX77686_LDO_UVSTEP, \
268 .ramp_delay = MAX77686_RAMP_DELAY, \
269 .n_voltages = MAX77686_VSEL_MASK + 1, \
270 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
271 .vsel_mask = MAX77686_VSEL_MASK, \
272 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
273 .enable_mask = MAX77686_OPMODE_MASK \
274 << MAX77686_OPMODE_SHIFT, \
275}
Jonghwa Lee133d4012012-06-01 13:17:14 +0900276#define regulator_desc_ldo_low(num) { \
277 .name = "LDO"#num, \
278 .id = MAX77686_LDO##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530279 .ops = &max77686_ldo_ops, \
280 .type = REGULATOR_VOLTAGE, \
281 .owner = THIS_MODULE, \
282 .min_uV = MAX77686_LDO_LOW_MINUV, \
283 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
284 .ramp_delay = MAX77686_RAMP_DELAY, \
285 .n_voltages = MAX77686_VSEL_MASK + 1, \
286 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
287 .vsel_mask = MAX77686_VSEL_MASK, \
288 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
289 .enable_mask = MAX77686_OPMODE_MASK \
290 << MAX77686_OPMODE_SHIFT, \
291}
292#define regulator_desc_ldo1_low(num) { \
293 .name = "LDO"#num, \
294 .id = MAX77686_LDO##num, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900295 .ops = &max77686_ops, \
296 .type = REGULATOR_VOLTAGE, \
297 .owner = THIS_MODULE, \
298 .min_uV = MAX77686_LDO_LOW_MINUV, \
299 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530300 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900301 .n_voltages = MAX77686_VSEL_MASK + 1, \
302 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
303 .vsel_mask = MAX77686_VSEL_MASK, \
304 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
305 .enable_mask = MAX77686_OPMODE_MASK \
306 << MAX77686_OPMODE_SHIFT, \
307}
308#define regulator_desc_buck(num) { \
309 .name = "BUCK"#num, \
310 .id = MAX77686_BUCK##num, \
311 .ops = &max77686_ops, \
312 .type = REGULATOR_VOLTAGE, \
313 .owner = THIS_MODULE, \
314 .min_uV = MAX77686_BUCK_MINUV, \
315 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530316 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900317 .n_voltages = MAX77686_VSEL_MASK + 1, \
318 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
319 .vsel_mask = MAX77686_VSEL_MASK, \
320 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
321 .enable_mask = MAX77686_OPMODE_MASK, \
322}
323#define regulator_desc_buck1(num) { \
324 .name = "BUCK"#num, \
325 .id = MAX77686_BUCK##num, \
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530326 .ops = &max77686_buck1_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900327 .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_BUCK1OUT, \
334 .vsel_mask = MAX77686_VSEL_MASK, \
335 .enable_reg = MAX77686_REG_BUCK1CTRL, \
336 .enable_mask = MAX77686_OPMODE_MASK, \
337}
338#define regulator_desc_buck_dvs(num) { \
339 .name = "BUCK"#num, \
340 .id = MAX77686_BUCK##num, \
Axel Lin74adfee52012-06-04 10:19:18 +0800341 .ops = &max77686_buck_dvs_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900342 .type = REGULATOR_VOLTAGE, \
343 .owner = THIS_MODULE, \
344 .min_uV = MAX77686_DVS_MINUV, \
345 .uV_step = MAX77686_DVS_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530346 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900347 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
348 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
349 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
350 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
351 .enable_mask = MAX77686_OPMODE_MASK \
352 << MAX77686_OPMODE_BUCK234_SHIFT, \
353}
354
355static struct regulator_desc regulators[] = {
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530356 regulator_desc_ldo1_low(1),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900357 regulator_desc_ldo_low(2),
358 regulator_desc_ldo(3),
359 regulator_desc_ldo(4),
360 regulator_desc_ldo(5),
361 regulator_desc_ldo_low(6),
362 regulator_desc_ldo_low(7),
363 regulator_desc_ldo_low(8),
364 regulator_desc_ldo(9),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530365 regulator_desc_lpm_ldo(10),
366 regulator_desc_lpm_ldo(11),
367 regulator_desc_lpm_ldo(12),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900368 regulator_desc_ldo(13),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530369 regulator_desc_lpm_ldo(14),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900370 regulator_desc_ldo_low(15),
Yadwinder Singh Brar15282ba2012-10-16 17:24:19 +0530371 regulator_desc_lpm_ldo(16),
Jonghwa Lee133d4012012-06-01 13:17:14 +0900372 regulator_desc_ldo(17),
373 regulator_desc_ldo(18),
374 regulator_desc_ldo(19),
375 regulator_desc_ldo(20),
376 regulator_desc_ldo(21),
377 regulator_desc_ldo(22),
378 regulator_desc_ldo(23),
379 regulator_desc_ldo(24),
380 regulator_desc_ldo(25),
381 regulator_desc_ldo(26),
382 regulator_desc_buck1(1),
383 regulator_desc_buck_dvs(2),
384 regulator_desc_buck_dvs(3),
385 regulator_desc_buck_dvs(4),
386 regulator_desc_buck(5),
387 regulator_desc_buck(6),
388 regulator_desc_buck(7),
389 regulator_desc_buck(8),
390 regulator_desc_buck(9),
391};
392
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530393#ifdef CONFIG_OF
Axel Linb19f5752013-01-25 10:24:18 +0800394static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530395 struct max77686_platform_data *pdata)
396{
Axel Linb19f5752013-01-25 10:24:18 +0800397 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530398 struct device_node *pmic_np, *regulators_np;
399 struct max77686_regulator_data *rdata;
400 struct of_regulator_match rmatch;
401 unsigned int i;
402
403 pmic_np = iodev->dev->of_node;
404 regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
405 if (!regulators_np) {
Axel Linb19f5752013-01-25 10:24:18 +0800406 dev_err(&pdev->dev, "could not find regulators sub-node\n");
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530407 return -EINVAL;
408 }
409
410 pdata->num_regulators = ARRAY_SIZE(regulators);
Axel Linb19f5752013-01-25 10:24:18 +0800411 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530412 pdata->num_regulators, GFP_KERNEL);
413 if (!rdata) {
Axel Linb19f5752013-01-25 10:24:18 +0800414 dev_err(&pdev->dev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530415 "could not allocate memory for regulator data\n");
416 return -ENOMEM;
417 }
418
419 for (i = 0; i < pdata->num_regulators; i++) {
420 rmatch.name = regulators[i].name;
421 rmatch.init_data = NULL;
Yadwinder Singh Brard1ef0652012-07-06 14:50:08 +0530422 rmatch.of_node = NULL;
Axel Linb19f5752013-01-25 10:24:18 +0800423 of_regulator_match(&pdev->dev, regulators_np, &rmatch, 1);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530424 rdata[i].initdata = rmatch.init_data;
Axel Lin2c58e262012-08-05 10:09:57 +0800425 rdata[i].of_node = rmatch.of_node;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530426 }
427
428 pdata->regulators = rdata;
429
430 return 0;
431}
432#else
Axel Linb19f5752013-01-25 10:24:18 +0800433static int max77686_pmic_dt_parse_pdata(struct platform_device *pdev,
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530434 struct max77686_platform_data *pdata)
435{
436 return 0;
437}
438#endif /* CONFIG_OF */
439
Bill Pembertona5023572012-11-19 13:22:22 -0500440static int max77686_pmic_probe(struct platform_device *pdev)
Jonghwa Lee133d4012012-06-01 13:17:14 +0900441{
442 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
443 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900444 struct max77686_data *max77686;
Axel Lincb44cde2012-08-05 10:11:16 +0800445 int i, ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800446 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900447
448 dev_dbg(&pdev->dev, "%s\n", __func__);
449
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530450 if (!pdata) {
451 dev_err(&pdev->dev, "no platform data found for regulator\n");
452 return -ENODEV;
453 }
454
455 if (iodev->dev->of_node) {
Axel Linb19f5752013-01-25 10:24:18 +0800456 ret = max77686_pmic_dt_parse_pdata(pdev, pdata);
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530457 if (ret)
458 return ret;
459 }
460
461 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800462 dev_err(&pdev->dev,
463 "Invalid initial data for regulator's initialiation\n");
464 return -EINVAL;
465 }
466
Jonghwa Lee133d4012012-06-01 13:17:14 +0900467 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
468 GFP_KERNEL);
469 if (!max77686)
470 return -ENOMEM;
471
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530472 config.dev = &pdev->dev;
473 config.regmap = iodev->regmap;
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530474 config.driver_data = max77686;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900475 platform_set_drvdata(pdev, max77686);
476
Axel Linab0d1cb2012-06-08 12:03:04 +0800477 for (i = 0; i < MAX77686_REGULATORS; i++) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800478 config.init_data = pdata->regulators[i].initdata;
Axel Lin2c58e262012-08-05 10:09:57 +0800479 config.of_node = pdata->regulators[i].of_node;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900480
Yadwinder Singh Brar6d2c8962012-10-16 17:24:18 +0530481 max77686->opmode[i] = regulators[i].enable_mask;
Axel Lincb44cde2012-08-05 10:11:16 +0800482 max77686->rdev[i] = regulator_register(&regulators[i], &config);
483 if (IS_ERR(max77686->rdev[i])) {
484 ret = PTR_ERR(max77686->rdev[i]);
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530485 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900486 "regulator init failed for %d\n", i);
Axel Lincb44cde2012-08-05 10:11:16 +0800487 max77686->rdev[i] = NULL;
488 goto err;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900489 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900490 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800491
Jonghwa Lee133d4012012-06-01 13:17:14 +0900492 return 0;
493err:
Axel Linab0d1cb2012-06-08 12:03:04 +0800494 while (--i >= 0)
Axel Lincb44cde2012-08-05 10:11:16 +0800495 regulator_unregister(max77686->rdev[i]);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900496 return ret;
497}
498
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500499static int max77686_pmic_remove(struct platform_device *pdev)
Jonghwa Lee133d4012012-06-01 13:17:14 +0900500{
501 struct max77686_data *max77686 = platform_get_drvdata(pdev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900502 int i;
503
504 for (i = 0; i < MAX77686_REGULATORS; i++)
Axel Lincb44cde2012-08-05 10:11:16 +0800505 regulator_unregister(max77686->rdev[i]);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900506
507 return 0;
508}
509
510static const struct platform_device_id max77686_pmic_id[] = {
511 {"max77686-pmic", 0},
512 { },
513};
514MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
515
516static struct platform_driver max77686_pmic_driver = {
517 .driver = {
518 .name = "max77686-pmic",
519 .owner = THIS_MODULE,
520 },
521 .probe = max77686_pmic_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500522 .remove = max77686_pmic_remove,
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");