blob: c564af6f05a360c92f18be7cad558b1b73723c2f [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 {
Jonghwa Lee133d4012012-06-01 13:17:14 +090069 struct regulator_dev **rdev;
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;
268 }
269
270 pdata->regulators = rdata;
271
272 return 0;
273}
274#else
275static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
276 struct max77686_platform_data *pdata)
277{
278 return 0;
279}
280#endif /* CONFIG_OF */
281
Jonghwa Lee133d4012012-06-01 13:17:14 +0900282static __devinit int max77686_pmic_probe(struct platform_device *pdev)
283{
284 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
285 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
286 struct regulator_dev **rdev;
287 struct max77686_data *max77686;
288 int i, size;
289 int ret = 0;
Axel Linc59d2a62012-06-20 15:01:25 +0800290 struct regulator_config config = { };
Jonghwa Lee133d4012012-06-01 13:17:14 +0900291
292 dev_dbg(&pdev->dev, "%s\n", __func__);
293
Yadwinder Singh Brar4706fca2012-07-05 09:28:25 +0530294 if (!pdata) {
295 dev_err(&pdev->dev, "no platform data found for regulator\n");
296 return -ENODEV;
297 }
298
299 if (iodev->dev->of_node) {
300 ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
301 if (ret)
302 return ret;
303 }
304
305 if (pdata->num_regulators != MAX77686_REGULATORS) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800306 dev_err(&pdev->dev,
307 "Invalid initial data for regulator's initialiation\n");
308 return -EINVAL;
309 }
310
Jonghwa Lee133d4012012-06-01 13:17:14 +0900311 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
312 GFP_KERNEL);
313 if (!max77686)
314 return -ENOMEM;
315
316 size = sizeof(struct regulator_dev *) * MAX77686_REGULATORS;
317 max77686->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
318 if (!max77686->rdev)
319 return -ENOMEM;
320
321 rdev = max77686->rdev;
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530322 config.dev = &pdev->dev;
323 config.regmap = iodev->regmap;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900324 platform_set_drvdata(pdev, max77686);
325
Axel Linab0d1cb2012-06-08 12:03:04 +0800326 for (i = 0; i < MAX77686_REGULATORS; i++) {
Axel Linab0d1cb2012-06-08 12:03:04 +0800327 config.init_data = pdata->regulators[i].initdata;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900328
Axel Linab0d1cb2012-06-08 12:03:04 +0800329 rdev[i] = regulator_register(&regulators[i], &config);
330 if (IS_ERR(rdev[i])) {
331 ret = PTR_ERR(rdev[i]);
Yadwinder Singh Brarf5030712012-06-20 10:50:51 +0530332 dev_err(&pdev->dev,
Jonghwa Lee133d4012012-06-01 13:17:14 +0900333 "regulator init failed for %d\n", i);
334 rdev[i] = NULL;
335 goto err;
Jonghwa Lee133d4012012-06-01 13:17:14 +0900336 }
Jonghwa Lee133d4012012-06-01 13:17:14 +0900337 }
Axel Linab0d1cb2012-06-08 12:03:04 +0800338
Jonghwa Lee133d4012012-06-01 13:17:14 +0900339 return 0;
340err:
Axel Linab0d1cb2012-06-08 12:03:04 +0800341 while (--i >= 0)
342 regulator_unregister(rdev[i]);
Jonghwa Lee133d4012012-06-01 13:17:14 +0900343 return ret;
344}
345
346static int __devexit max77686_pmic_remove(struct platform_device *pdev)
347{
348 struct max77686_data *max77686 = platform_get_drvdata(pdev);
349 struct regulator_dev **rdev = max77686->rdev;
350 int i;
351
352 for (i = 0; i < MAX77686_REGULATORS; i++)
353 if (rdev[i])
354 regulator_unregister(rdev[i]);
355
356 return 0;
357}
358
359static const struct platform_device_id max77686_pmic_id[] = {
360 {"max77686-pmic", 0},
361 { },
362};
363MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
364
365static struct platform_driver max77686_pmic_driver = {
366 .driver = {
367 .name = "max77686-pmic",
368 .owner = THIS_MODULE,
369 },
370 .probe = max77686_pmic_probe,
371 .remove = __devexit_p(max77686_pmic_remove),
372 .id_table = max77686_pmic_id,
373};
374
375static int __init max77686_pmic_init(void)
376{
377 return platform_driver_register(&max77686_pmic_driver);
378}
379subsys_initcall(max77686_pmic_init);
380
381static void __exit max77686_pmic_cleanup(void)
382{
383 platform_driver_unregister(&max77686_pmic_driver);
384}
385module_exit(max77686_pmic_cleanup);
386
387MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
388MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
389MODULE_LICENSE("GPL");