2 * max77686.c - Regulator driver for the Maxim 77686
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@smasung.com>
6 * Jonghwa Lee <jonghwa3.lee@samsung.com>
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.
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.
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
22 * This driver is based on max8997.c
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/regulator/of_regulator.h>
35 #include <linux/mfd/max77686.h>
36 #include <linux/mfd/max77686-private.h>
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
44 #define MAX77686_RAMP_DELAY 100000 /* uV/us */
45 #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
46 #define MAX77686_DVS_MINUV 600000
47 #define MAX77686_DVS_UVSTEP 12500
49 #define MAX77686_OPMODE_SHIFT 6
50 #define MAX77686_OPMODE_BUCK234_SHIFT 4
51 #define MAX77686_OPMODE_MASK 0x3
53 #define MAX77686_VSEL_MASK 0x3F
54 #define MAX77686_DVS_VSEL_MASK 0xFF
56 #define MAX77686_RAMP_RATE_MASK 0xC0
58 #define MAX77686_REGULATORS MAX77686_REG_MAX
59 #define MAX77686_LDOS 26
61 enum max77686_ramp_rate {
65 RAMP_RATE_NO_CTRL, /* 100mV/us */
68 struct max77686_data {
69 struct regulator_dev *rdev[MAX77686_REGULATORS];
72 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
74 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
78 ramp_value = RAMP_RATE_13P75MV;
81 ramp_value = RAMP_RATE_27P5MV;
84 ramp_value = RAMP_RATE_55MV;
86 case 55001 ... 100000:
89 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
90 rdev->desc->name, ramp_delay);
93 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
94 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
97 static struct regulator_ops max77686_ops = {
98 .list_voltage = regulator_list_voltage_linear,
99 .map_voltage = regulator_map_voltage_linear,
100 .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,
105 .set_voltage_time_sel = regulator_set_voltage_time_sel,
108 static struct regulator_ops max77686_buck_dvs_ops = {
109 .list_voltage = regulator_list_voltage_linear,
110 .map_voltage = regulator_map_voltage_linear,
111 .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,
116 .set_voltage_time_sel = regulator_set_voltage_time_sel,
117 .set_ramp_delay = max77686_set_ramp_delay,
120 #define regulator_desc_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, \
128 .ramp_delay = MAX77686_RAMP_DELAY, \
129 .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, \
136 #define regulator_desc_ldo_low(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, \
144 .ramp_delay = MAX77686_RAMP_DELAY, \
145 .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, \
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, \
160 .ramp_delay = MAX77686_RAMP_DELAY, \
161 .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, \
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, \
175 .ramp_delay = MAX77686_RAMP_DELAY, \
176 .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, \
182 #define regulator_desc_buck_dvs(num) { \
183 .name = "BUCK"#num, \
184 .id = MAX77686_BUCK##num, \
185 .ops = &max77686_buck_dvs_ops, \
186 .type = REGULATOR_VOLTAGE, \
187 .owner = THIS_MODULE, \
188 .min_uV = MAX77686_DVS_MINUV, \
189 .uV_step = MAX77686_DVS_UVSTEP, \
190 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
191 .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, \
199 static 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),
238 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
239 struct max77686_platform_data *pdata)
241 struct device_node *pmic_np, *regulators_np;
242 struct max77686_regulator_data *rdata;
243 struct of_regulator_match rmatch;
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");
253 pdata->num_regulators = ARRAY_SIZE(regulators);
254 rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
255 pdata->num_regulators, GFP_KERNEL);
258 "could not allocate memory for regulator data\n");
262 for (i = 0; i < pdata->num_regulators; i++) {
263 rmatch.name = regulators[i].name;
264 rmatch.init_data = NULL;
265 rmatch.of_node = NULL;
266 of_regulator_match(iodev->dev, regulators_np, &rmatch, 1);
267 rdata[i].initdata = rmatch.init_data;
268 rdata[i].of_node = rmatch.of_node;
271 pdata->regulators = rdata;
276 static int max77686_pmic_dt_parse_pdata(struct max77686_dev *iodev,
277 struct max77686_platform_data *pdata)
281 #endif /* CONFIG_OF */
283 static __devinit int max77686_pmic_probe(struct platform_device *pdev)
285 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
286 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
287 struct max77686_data *max77686;
289 struct regulator_config config = { };
291 dev_dbg(&pdev->dev, "%s\n", __func__);
294 dev_err(&pdev->dev, "no platform data found for regulator\n");
298 if (iodev->dev->of_node) {
299 ret = max77686_pmic_dt_parse_pdata(iodev, pdata);
304 if (pdata->num_regulators != MAX77686_REGULATORS) {
306 "Invalid initial data for regulator's initialiation\n");
310 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
315 config.dev = &pdev->dev;
316 config.regmap = iodev->regmap;
317 platform_set_drvdata(pdev, max77686);
319 for (i = 0; i < MAX77686_REGULATORS; i++) {
320 config.init_data = pdata->regulators[i].initdata;
321 config.of_node = pdata->regulators[i].of_node;
323 max77686->rdev[i] = regulator_register(®ulators[i], &config);
324 if (IS_ERR(max77686->rdev[i])) {
325 ret = PTR_ERR(max77686->rdev[i]);
327 "regulator init failed for %d\n", i);
328 max77686->rdev[i] = NULL;
336 regulator_unregister(max77686->rdev[i]);
340 static int __devexit max77686_pmic_remove(struct platform_device *pdev)
342 struct max77686_data *max77686 = platform_get_drvdata(pdev);
345 for (i = 0; i < MAX77686_REGULATORS; i++)
346 regulator_unregister(max77686->rdev[i]);
351 static const struct platform_device_id max77686_pmic_id[] = {
352 {"max77686-pmic", 0},
355 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
357 static struct platform_driver max77686_pmic_driver = {
359 .name = "max77686-pmic",
360 .owner = THIS_MODULE,
362 .probe = max77686_pmic_probe,
363 .remove = __devexit_p(max77686_pmic_remove),
364 .id_table = max77686_pmic_id,
367 static int __init max77686_pmic_init(void)
369 return platform_driver_register(&max77686_pmic_driver);
371 subsys_initcall(max77686_pmic_init);
373 static void __exit max77686_pmic_cleanup(void)
375 platform_driver_unregister(&max77686_pmic_driver);
377 module_exit(max77686_pmic_cleanup);
379 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
380 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
381 MODULE_LICENSE("GPL");