blob: 70046f8032fdd15b77afc4e69420060ba6f65bb5 [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>
34#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
43#define MAX77686_DVS_MINUV 600000
44#define MAX77686_DVS_UVSTEP 12500
45
46#define MAX77686_OPMODE_SHIFT 6
47#define MAX77686_OPMODE_BUCK234_SHIFT 4
48#define MAX77686_OPMODE_MASK 0x3
49
50#define MAX77686_VSEL_MASK 0x3F
51#define MAX77686_DVS_VSEL_MASK 0xFF
52
53#define MAX77686_RAMP_RATE_MASK 0xC0
54
55#define MAX77686_REGULATORS MAX77686_REG_MAX
56#define MAX77686_LDOS 26
57
58enum max77686_ramp_rate {
59 RAMP_RATE_13P75MV,
60 RAMP_RATE_27P5MV,
61 RAMP_RATE_55MV,
62 RAMP_RATE_NO_CTRL, /* 100mV/us */
63};
64
65struct max77686_data {
66 struct device *dev;
67 struct max77686_dev *iodev;
68 int num_regulators;
69 struct regulator_dev **rdev;
70 int ramp_delay; /* in mV/us */
71};
72
73static int max77686_set_dvs_voltage_time_sel(struct regulator_dev *rdev,
74 unsigned int old_selector, unsigned int new_selector)
75{
76 struct max77686_data *max77686 = rdev_get_drvdata(rdev);
77 int ramp_rate[] = {13, 27, 55, 100};
78 return (DIV_ROUND_UP(rdev->desc->uV_step
79 * abs(new_selector - old_selector),
80 ramp_rate[max77686->ramp_delay]));
81}
82
83static int max77686_set_voltage_time_sel(struct regulator_dev *rdev,
84 unsigned int old_selector, unsigned int new_selector)
85{
86 /* Unconditionally 100 mV/us */
87 return (DIV_ROUND_UP(rdev->desc->uV_step
88 * abs(new_selector - old_selector), 100));
89}
90
91static struct regulator_ops max77686_ops = {
92 .list_voltage = regulator_list_voltage_linear,
93 .is_enabled = regulator_is_enabled_regmap,
94 .enable = regulator_enable_regmap,
95 .disable = regulator_disable_regmap,
96 .get_voltage_sel = regulator_get_voltage_sel_regmap,
97 .set_voltage_sel = regulator_set_voltage_sel_regmap,
98 .set_voltage_time_sel = max77686_set_voltage_time_sel,
99};
100
101static struct regulator_ops max77686_buck_dvs_ops = {
102 .list_voltage = regulator_list_voltage_linear,
103 .is_enabled = regulator_is_enabled_regmap,
104 .enable = regulator_enable_regmap,
105 .disable = regulator_disable_regmap,
106 .get_voltage_sel = regulator_get_voltage_sel_regmap,
107 .set_voltage_sel = regulator_set_voltage_sel_regmap,
108 .set_voltage_time_sel = max77686_set_dvs_voltage_time_sel,
109};
110
111#define regulator_desc_ldo(num) { \
112 .name = "LDO"#num, \
113 .id = MAX77686_LDO##num, \
114 .ops = &max77686_ops, \
115 .type = REGULATOR_VOLTAGE, \
116 .owner = THIS_MODULE, \
117 .min_uV = MAX77686_LDO_MINUV, \
118 .uV_step = MAX77686_LDO_UVSTEP, \
119 .n_voltages = MAX77686_VSEL_MASK + 1, \
120 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
121 .vsel_mask = MAX77686_VSEL_MASK, \
122 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
123 .enable_mask = MAX77686_OPMODE_MASK \
124 << MAX77686_OPMODE_SHIFT, \
125}
126#define regulator_desc_ldo_low(num) { \
127 .name = "LDO"#num, \
128 .id = MAX77686_LDO##num, \
129 .ops = &max77686_ops, \
130 .type = REGULATOR_VOLTAGE, \
131 .owner = THIS_MODULE, \
132 .min_uV = MAX77686_LDO_LOW_MINUV, \
133 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
134 .n_voltages = MAX77686_VSEL_MASK + 1, \
135 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
136 .vsel_mask = MAX77686_VSEL_MASK, \
137 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
138 .enable_mask = MAX77686_OPMODE_MASK \
139 << MAX77686_OPMODE_SHIFT, \
140}
141#define regulator_desc_buck(num) { \
142 .name = "BUCK"#num, \
143 .id = MAX77686_BUCK##num, \
144 .ops = &max77686_ops, \
145 .type = REGULATOR_VOLTAGE, \
146 .owner = THIS_MODULE, \
147 .min_uV = MAX77686_BUCK_MINUV, \
148 .uV_step = MAX77686_BUCK_UVSTEP, \
149 .n_voltages = MAX77686_VSEL_MASK + 1, \
150 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
151 .vsel_mask = MAX77686_VSEL_MASK, \
152 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
153 .enable_mask = MAX77686_OPMODE_MASK, \
154}
155#define regulator_desc_buck1(num) { \
156 .name = "BUCK"#num, \
157 .id = MAX77686_BUCK##num, \
158 .ops = &max77686_ops, \
159 .type = REGULATOR_VOLTAGE, \
160 .owner = THIS_MODULE, \
161 .min_uV = MAX77686_BUCK_MINUV, \
162 .uV_step = MAX77686_BUCK_UVSTEP, \
163 .n_voltages = MAX77686_VSEL_MASK + 1, \
164 .vsel_reg = MAX77686_REG_BUCK1OUT, \
165 .vsel_mask = MAX77686_VSEL_MASK, \
166 .enable_reg = MAX77686_REG_BUCK1CTRL, \
167 .enable_mask = MAX77686_OPMODE_MASK, \
168}
169#define regulator_desc_buck_dvs(num) { \
170 .name = "BUCK"#num, \
171 .id = MAX77686_BUCK##num, \
172 .ops = &max77686_buck_dvs_ops, \
173 .type = REGULATOR_VOLTAGE, \
174 .owner = THIS_MODULE, \
175 .min_uV = MAX77686_DVS_MINUV, \
176 .uV_step = MAX77686_DVS_UVSTEP, \
177 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
178 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
179 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
180 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
181 .enable_mask = MAX77686_OPMODE_MASK \
182 << MAX77686_OPMODE_BUCK234_SHIFT, \
183}
184
185static struct regulator_desc regulators[] = {
186 regulator_desc_ldo_low(1),
187 regulator_desc_ldo_low(2),
188 regulator_desc_ldo(3),
189 regulator_desc_ldo(4),
190 regulator_desc_ldo(5),
191 regulator_desc_ldo_low(6),
192 regulator_desc_ldo_low(7),
193 regulator_desc_ldo_low(8),
194 regulator_desc_ldo(9),
195 regulator_desc_ldo(10),
196 regulator_desc_ldo(11),
197 regulator_desc_ldo(12),
198 regulator_desc_ldo(13),
199 regulator_desc_ldo(14),
200 regulator_desc_ldo_low(15),
201 regulator_desc_ldo(16),
202 regulator_desc_ldo(17),
203 regulator_desc_ldo(18),
204 regulator_desc_ldo(19),
205 regulator_desc_ldo(20),
206 regulator_desc_ldo(21),
207 regulator_desc_ldo(22),
208 regulator_desc_ldo(23),
209 regulator_desc_ldo(24),
210 regulator_desc_ldo(25),
211 regulator_desc_ldo(26),
212 regulator_desc_buck1(1),
213 regulator_desc_buck_dvs(2),
214 regulator_desc_buck_dvs(3),
215 regulator_desc_buck_dvs(4),
216 regulator_desc_buck(5),
217 regulator_desc_buck(6),
218 regulator_desc_buck(7),
219 regulator_desc_buck(8),
220 regulator_desc_buck(9),
221};
222
223static __devinit int max77686_pmic_probe(struct platform_device *pdev)
224{
225 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
226 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
227 struct regulator_dev **rdev;
228 struct max77686_data *max77686;
229 int i, size;
230 int ret = 0;
231 struct regulator_config config;
232
233 dev_dbg(&pdev->dev, "%s\n", __func__);
234
235 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
236 GFP_KERNEL);
237 if (!max77686)
238 return -ENOMEM;
239
240 size = sizeof(struct regulator_dev *) * MAX77686_REGULATORS;
241 max77686->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
242 if (!max77686->rdev)
243 return -ENOMEM;
244
245 rdev = max77686->rdev;
246 max77686->dev = &pdev->dev;
247 max77686->iodev = iodev;
248 if (pdata)
249 max77686->num_regulators = pdata->num_regulators;
250 platform_set_drvdata(pdev, max77686);
251
252 max77686->ramp_delay = RAMP_RATE_NO_CTRL; /* Set 0x3 for RAMP */
253 regmap_update_bits(max77686->iodev->regmap,
254 MAX77686_REG_BUCK2CTRL1, MAX77686_RAMP_RATE_MASK,
255 max77686->ramp_delay << 6);
256 regmap_update_bits(max77686->iodev->regmap,
257 MAX77686_REG_BUCK3CTRL1, MAX77686_RAMP_RATE_MASK,
258 max77686->ramp_delay << 6);
259 regmap_update_bits(max77686->iodev->regmap,
260 MAX77686_REG_BUCK4CTRL1, MAX77686_RAMP_RATE_MASK,
261 max77686->ramp_delay << 6);
262
263 if (pdata->num_regulators == MAX77686_REGULATORS) {
264 for (i = 0; i < MAX77686_REGULATORS; i++) {
265 config.dev = max77686->dev;
266 config.regmap = iodev->regmap;
267 config.driver_data = max77686;
268 config.init_data = pdata->regulators[i].initdata;
269
270 rdev[i] = regulator_register(&regulators[i], &config);
271
272 if (IS_ERR(rdev[i])) {
273 ret = PTR_ERR(rdev[i]);
274 dev_err(max77686->dev,
275 "regulator init failed for %d\n", i);
276 rdev[i] = NULL;
277 goto err;
278 }
279 }
280 } else {
281 dev_err(max77686->dev,
282 "Lack of initial data for regulator's initialiation\n");
283 return -EINVAL;
284 }
285 return 0;
286err:
287 for (i = 0; i < MAX77686_REGULATORS; i++) {
288 if (rdev[i])
289 regulator_unregister(rdev[i]);
290 }
291 return ret;
292}
293
294static int __devexit max77686_pmic_remove(struct platform_device *pdev)
295{
296 struct max77686_data *max77686 = platform_get_drvdata(pdev);
297 struct regulator_dev **rdev = max77686->rdev;
298 int i;
299
300 for (i = 0; i < MAX77686_REGULATORS; i++)
301 if (rdev[i])
302 regulator_unregister(rdev[i]);
303
304 return 0;
305}
306
307static const struct platform_device_id max77686_pmic_id[] = {
308 {"max77686-pmic", 0},
309 { },
310};
311MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
312
313static struct platform_driver max77686_pmic_driver = {
314 .driver = {
315 .name = "max77686-pmic",
316 .owner = THIS_MODULE,
317 },
318 .probe = max77686_pmic_probe,
319 .remove = __devexit_p(max77686_pmic_remove),
320 .id_table = max77686_pmic_id,
321};
322
323static int __init max77686_pmic_init(void)
324{
325 return platform_driver_register(&max77686_pmic_driver);
326}
327subsys_initcall(max77686_pmic_init);
328
329static void __exit max77686_pmic_cleanup(void)
330{
331 platform_driver_unregister(&max77686_pmic_driver);
332}
333module_exit(max77686_pmic_cleanup);
334
335MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
336MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
337MODULE_LICENSE("GPL");