blob: 2a67d08658add7dfcabbc1c00d4a0a02b9b70a0d [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];
Jonghwa Lee133d4012012-06-01 13:17:14 +090070};
71
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +053072static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
73{
74 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
75
76 switch (ramp_delay) {
77 case 1 ... 13750:
78 ramp_value = RAMP_RATE_13P75MV;
79 break;
80 case 13751 ... 27500:
81 ramp_value = RAMP_RATE_27P5MV;
82 break;
83 case 27501 ... 55000:
84 ramp_value = RAMP_RATE_55MV;
85 break;
86 case 55001 ... 100000:
87 break;
88 default:
89 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
90 rdev->desc->name, ramp_delay);
91 }
92
93 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
94 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
95}
96
Jonghwa Lee133d4012012-06-01 13:17:14 +090097static struct regulator_ops max77686_ops = {
98 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +080099 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900100 .is_enabled = regulator_is_enabled_regmap,
101 .enable = regulator_enable_regmap,
102 .disable = regulator_disable_regmap,
103 .get_voltage_sel = regulator_get_voltage_sel_regmap,
104 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530105 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900106};
107
108static struct regulator_ops max77686_buck_dvs_ops = {
109 .list_voltage = regulator_list_voltage_linear,
Axel Lin2e3f7f22012-06-03 22:46:14 +0800110 .map_voltage = regulator_map_voltage_linear,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900111 .is_enabled = regulator_is_enabled_regmap,
112 .enable = regulator_enable_regmap,
113 .disable = regulator_disable_regmap,
114 .get_voltage_sel = regulator_get_voltage_sel_regmap,
115 .set_voltage_sel = regulator_set_voltage_sel_regmap,
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530116 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530117 .set_ramp_delay = max77686_set_ramp_delay,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900118};
119
120#define regulator_desc_ldo(num) { \
121 .name = "LDO"#num, \
122 .id = MAX77686_LDO##num, \
123 .ops = &max77686_ops, \
124 .type = REGULATOR_VOLTAGE, \
125 .owner = THIS_MODULE, \
126 .min_uV = MAX77686_LDO_MINUV, \
127 .uV_step = MAX77686_LDO_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530128 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900129 .n_voltages = MAX77686_VSEL_MASK + 1, \
130 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
131 .vsel_mask = MAX77686_VSEL_MASK, \
132 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
133 .enable_mask = MAX77686_OPMODE_MASK \
134 << MAX77686_OPMODE_SHIFT, \
135}
136#define regulator_desc_ldo_low(num) { \
137 .name = "LDO"#num, \
138 .id = MAX77686_LDO##num, \
139 .ops = &max77686_ops, \
140 .type = REGULATOR_VOLTAGE, \
141 .owner = THIS_MODULE, \
142 .min_uV = MAX77686_LDO_LOW_MINUV, \
143 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530144 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900145 .n_voltages = MAX77686_VSEL_MASK + 1, \
146 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
147 .vsel_mask = MAX77686_VSEL_MASK, \
148 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
149 .enable_mask = MAX77686_OPMODE_MASK \
150 << MAX77686_OPMODE_SHIFT, \
151}
152#define regulator_desc_buck(num) { \
153 .name = "BUCK"#num, \
154 .id = MAX77686_BUCK##num, \
155 .ops = &max77686_ops, \
156 .type = REGULATOR_VOLTAGE, \
157 .owner = THIS_MODULE, \
158 .min_uV = MAX77686_BUCK_MINUV, \
159 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530160 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900161 .n_voltages = MAX77686_VSEL_MASK + 1, \
162 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
163 .vsel_mask = MAX77686_VSEL_MASK, \
164 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
165 .enable_mask = MAX77686_OPMODE_MASK, \
166}
167#define regulator_desc_buck1(num) { \
168 .name = "BUCK"#num, \
169 .id = MAX77686_BUCK##num, \
170 .ops = &max77686_ops, \
171 .type = REGULATOR_VOLTAGE, \
172 .owner = THIS_MODULE, \
173 .min_uV = MAX77686_BUCK_MINUV, \
174 .uV_step = MAX77686_BUCK_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530175 .ramp_delay = MAX77686_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900176 .n_voltages = MAX77686_VSEL_MASK + 1, \
177 .vsel_reg = MAX77686_REG_BUCK1OUT, \
178 .vsel_mask = MAX77686_VSEL_MASK, \
179 .enable_reg = MAX77686_REG_BUCK1CTRL, \
180 .enable_mask = MAX77686_OPMODE_MASK, \
181}
182#define regulator_desc_buck_dvs(num) { \
183 .name = "BUCK"#num, \
184 .id = MAX77686_BUCK##num, \
Axel Lin74adfee52012-06-04 10:19:18 +0800185 .ops = &max77686_buck_dvs_ops, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900186 .type = REGULATOR_VOLTAGE, \
187 .owner = THIS_MODULE, \
188 .min_uV = MAX77686_DVS_MINUV, \
189 .uV_step = MAX77686_DVS_UVSTEP, \
Yadwinder Singh Brar852abad2012-06-19 13:23:42 +0530190 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
Jonghwa Lee133d4012012-06-01 13:17:14 +0900191 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
192 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
193 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
194 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
195 .enable_mask = MAX77686_OPMODE_MASK \
196 << MAX77686_OPMODE_BUCK234_SHIFT, \
197}
198
199static struct regulator_desc regulators[] = {
200 regulator_desc_ldo_low(1),
201 regulator_desc_ldo_low(2),
202 regulator_desc_ldo(3),
203 regulator_desc_ldo(4),
204 regulator_desc_ldo(5),
205 regulator_desc_ldo_low(6),
206 regulator_desc_ldo_low(7),
207 regulator_desc_ldo_low(8),
208 regulator_desc_ldo(9),
209 regulator_desc_ldo(10),
210 regulator_desc_ldo(11),
211 regulator_desc_ldo(12),
212 regulator_desc_ldo(13),
213 regulator_desc_ldo(14),
214 regulator_desc_ldo_low(15),
215 regulator_desc_ldo(16),
216 regulator_desc_ldo(17),
217 regulator_desc_ldo(18),
218 regulator_desc_ldo(19),
219 regulator_desc_ldo(20),
220 regulator_desc_ldo(21),
221 regulator_desc_ldo(22),
222 regulator_desc_ldo(23),
223 regulator_desc_ldo(24),
224 regulator_desc_ldo(25),
225 regulator_desc_ldo(26),
226 regulator_desc_buck1(1),
227 regulator_desc_buck_dvs(2),
228 regulator_desc_buck_dvs(3),
229 regulator_desc_buck_dvs(4),
230 regulator_desc_buck(5),
231 regulator_desc_buck(6),
232 regulator_desc_buck(7),
233 regulator_desc_buck(8),
234 regulator_desc_buck(9),
235};
236
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530237#ifdef CONFIG_OF
238static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
239 struct max77686_platform_data *pdata)
240{
241 struct device_node *pmic_np, *regulators_np;
242 struct max77686_regulator_data *rdata;
243 struct of_regulator_match rmatch;
244 unsigned int i;
245
246 pmic_np = iodev->dev->of_node;
247 regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
248 if (!regulators_np) {
249 dev_err(iodev->dev, "could not find regulators sub-node\n");
250 return -EINVAL;
251 }
252
253 pdata->num_regulators = ARRAY_SIZE(regulators);
254 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
255 pdata->num_regulators, GFP_KERNEL);
256 if (!rdata) {
257 dev_err(iodev->dev,
258 "could not allocate memory for regulator data\n");
259 return -ENOMEM;
260 }
261
262 for (i = 0; i < pdata->num_regulators; i++) {
263 rmatch.name = regulators[i].name;
264 rmatch.init_data = NULL;
Yadwinder Singh Brard1ef0652012-07-06 14:50:08 +0530265 rmatch.of_node = NULL;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530266 of_regulator_match(iodev->dev, regulators_np, &rmatch, 1);
267 rdata[i].initdata = rmatch.init_data;
Axel Lin2c58e262012-08-05 10:09:57 +0800268 rdata[i].of_node = rmatch.of_node;
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530269 }
270
271 pdata->regulators = rdata;
272
273 return 0;
274}
275#else
276static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
277 struct max77686_platform_data *pdata)
278{
279 return 0;
280}
281#endif /* CONFIG_OF */
282
Jonghwa Lee133d4012012-06-01 13:17:14 +0900283static __devinit int max77686_pmic_probe(struct platform_device *pdev)
284{
285 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
286 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900287 struct max77686_data *max77686;
Axel Lincb44cde2012-08-05 10:11:16 +0800288 int i, ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800289 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900290
291 dev_dbg(&pdev->dev, "%s\n", __func__);
292
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530293 if (!pdata) {
294 dev_err(&pdev->dev, "no platform data found for regulator\n");
295 return -ENODEV;
296 }
297
298 if (iodev->dev->of_node) {
299 ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
300 if (ret)
301 return ret;
302 }
303
304 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800305 dev_err(&pdev->dev,
306 "Invalid initial data for regulator's initialiation\n");
307 return -EINVAL;
308 }
309
Jonghwa Lee133d4012012-06-01 13:17:14 +0900310 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
311 GFP_KERNEL);
312 if (!max77686)
313 return -ENOMEM;
314
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530315 config.dev = &pdev->dev;
316 config.regmap = iodev->regmap;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900317 platform_set_drvdata(pdev, max77686);
318
Axel Linab0d1cb2012-06-08 12:03:04 +0800319 for (i = 0; i < MAX77686_REGULATORS; i++) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800320 config.init_data = pdata->regulators[i].initdata;
Axel Lin2c58e262012-08-05 10:09:57 +0800321 config.of_node = pdata->regulators[i].of_node;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900322
Axel Lincb44cde2012-08-05 10:11:16 +0800323 max77686->rdev[i] = regulator_register(&regulators[i], &config);
324 if (IS_ERR(max77686->rdev[i])) {
325 ret = PTR_ERR(max77686->rdev[i]);
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530326 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900327 "regulator init failed for %d\n", i);
Axel Lincb44cde2012-08-05 10:11:16 +0800328 max77686->rdev[i] = NULL;
329 goto err;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900330 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900331 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800332
Jonghwa Lee133d4012012-06-01 13:17:14 +0900333 return 0;
334err:
Axel Linab0d1cb2012-06-08 12:03:04 +0800335 while (--i >= 0)
Axel Lincb44cde2012-08-05 10:11:16 +0800336 regulator_unregister(max77686->rdev[i]);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900337 return ret;
338}
339
340static int __devexit max77686_pmic_remove(struct platform_device *pdev)
341{
342 struct max77686_data *max77686 = platform_get_drvdata(pdev);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900343 int i;
344
345 for (i = 0; i < MAX77686_REGULATORS; i++)
Axel Lincb44cde2012-08-05 10:11:16 +0800346 regulator_unregister(max77686->rdev[i]);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900347
348 return 0;
349}
350
351static const struct platform_device_id max77686_pmic_id[] = {
352 {"max77686-pmic", 0},
353 { },
354};
355MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
356
357static struct platform_driver max77686_pmic_driver = {
358 .driver = {
359 .name = "max77686-pmic",
360 .owner = THIS_MODULE,
361 },
362 .probe = max77686_pmic_probe,
363 .remove = __devexit_p(max77686_pmic_remove),
364 .id_table = max77686_pmic_id,
365};
366
367static int __init max77686_pmic_init(void)
368{
369 return platform_driver_register(&max77686_pmic_driver);
370}
371subsys_initcall(max77686_pmic_init);
372
373static void __exit max77686_pmic_cleanup(void)
374{
375 platform_driver_unregister(&max77686_pmic_driver);
376}
377module_exit(max77686_pmic_cleanup);
378
379MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
380MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
381MODULE_LICENSE("GPL");