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/mfd/max77686.h>
35 #include <linux/mfd/max77686-private.h>
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_RAMP_DELAY 100000 /* uV/us */
44 #define MAX77686_DVS_RAMP_DELAY 27500 /* uV/us */
45 #define MAX77686_DVS_MINUV 600000
46 #define MAX77686_DVS_UVSTEP 12500
48 #define MAX77686_OPMODE_SHIFT 6
49 #define MAX77686_OPMODE_BUCK234_SHIFT 4
50 #define MAX77686_OPMODE_MASK 0x3
52 #define MAX77686_VSEL_MASK 0x3F
53 #define MAX77686_DVS_VSEL_MASK 0xFF
55 #define MAX77686_RAMP_RATE_MASK 0xC0
57 #define MAX77686_REGULATORS MAX77686_REG_MAX
58 #define MAX77686_LDOS 26
60 enum max77686_ramp_rate {
64 RAMP_RATE_NO_CTRL, /* 100mV/us */
67 struct max77686_data {
68 struct regulator_dev **rdev;
71 static int max77686_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
73 unsigned int ramp_value = RAMP_RATE_NO_CTRL;
77 ramp_value = RAMP_RATE_13P75MV;
80 ramp_value = RAMP_RATE_27P5MV;
83 ramp_value = RAMP_RATE_55MV;
85 case 55001 ... 100000:
88 pr_warn("%s: ramp_delay: %d not supported, setting 100000\n",
89 rdev->desc->name, ramp_delay);
92 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
93 MAX77686_RAMP_RATE_MASK, ramp_value << 6);
96 static struct regulator_ops max77686_ops = {
97 .list_voltage = regulator_list_voltage_linear,
98 .map_voltage = regulator_map_voltage_linear,
99 .is_enabled = regulator_is_enabled_regmap,
100 .enable = regulator_enable_regmap,
101 .disable = regulator_disable_regmap,
102 .get_voltage_sel = regulator_get_voltage_sel_regmap,
103 .set_voltage_sel = regulator_set_voltage_sel_regmap,
104 .set_voltage_time_sel = regulator_set_voltage_time_sel,
107 static struct regulator_ops max77686_buck_dvs_ops = {
108 .list_voltage = regulator_list_voltage_linear,
109 .map_voltage = regulator_map_voltage_linear,
110 .is_enabled = regulator_is_enabled_regmap,
111 .enable = regulator_enable_regmap,
112 .disable = regulator_disable_regmap,
113 .get_voltage_sel = regulator_get_voltage_sel_regmap,
114 .set_voltage_sel = regulator_set_voltage_sel_regmap,
115 .set_voltage_time_sel = regulator_set_voltage_time_sel,
116 .set_ramp_delay = max77686_set_ramp_delay,
119 #define regulator_desc_ldo(num) { \
121 .id = MAX77686_LDO##num, \
122 .ops = &max77686_ops, \
123 .type = REGULATOR_VOLTAGE, \
124 .owner = THIS_MODULE, \
125 .min_uV = MAX77686_LDO_MINUV, \
126 .uV_step = MAX77686_LDO_UVSTEP, \
127 .ramp_delay = MAX77686_RAMP_DELAY, \
128 .n_voltages = MAX77686_VSEL_MASK + 1, \
129 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
130 .vsel_mask = MAX77686_VSEL_MASK, \
131 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
132 .enable_mask = MAX77686_OPMODE_MASK \
133 << MAX77686_OPMODE_SHIFT, \
135 #define regulator_desc_ldo_low(num) { \
137 .id = MAX77686_LDO##num, \
138 .ops = &max77686_ops, \
139 .type = REGULATOR_VOLTAGE, \
140 .owner = THIS_MODULE, \
141 .min_uV = MAX77686_LDO_LOW_MINUV, \
142 .uV_step = MAX77686_LDO_LOW_UVSTEP, \
143 .ramp_delay = MAX77686_RAMP_DELAY, \
144 .n_voltages = MAX77686_VSEL_MASK + 1, \
145 .vsel_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
146 .vsel_mask = MAX77686_VSEL_MASK, \
147 .enable_reg = MAX77686_REG_LDO1CTRL1 + num - 1, \
148 .enable_mask = MAX77686_OPMODE_MASK \
149 << MAX77686_OPMODE_SHIFT, \
151 #define regulator_desc_buck(num) { \
152 .name = "BUCK"#num, \
153 .id = MAX77686_BUCK##num, \
154 .ops = &max77686_ops, \
155 .type = REGULATOR_VOLTAGE, \
156 .owner = THIS_MODULE, \
157 .min_uV = MAX77686_BUCK_MINUV, \
158 .uV_step = MAX77686_BUCK_UVSTEP, \
159 .ramp_delay = MAX77686_RAMP_DELAY, \
160 .n_voltages = MAX77686_VSEL_MASK + 1, \
161 .vsel_reg = MAX77686_REG_BUCK5OUT + (num - 5) * 2, \
162 .vsel_mask = MAX77686_VSEL_MASK, \
163 .enable_reg = MAX77686_REG_BUCK5CTRL + (num - 5) * 2, \
164 .enable_mask = MAX77686_OPMODE_MASK, \
166 #define regulator_desc_buck1(num) { \
167 .name = "BUCK"#num, \
168 .id = MAX77686_BUCK##num, \
169 .ops = &max77686_ops, \
170 .type = REGULATOR_VOLTAGE, \
171 .owner = THIS_MODULE, \
172 .min_uV = MAX77686_BUCK_MINUV, \
173 .uV_step = MAX77686_BUCK_UVSTEP, \
174 .ramp_delay = MAX77686_RAMP_DELAY, \
175 .n_voltages = MAX77686_VSEL_MASK + 1, \
176 .vsel_reg = MAX77686_REG_BUCK1OUT, \
177 .vsel_mask = MAX77686_VSEL_MASK, \
178 .enable_reg = MAX77686_REG_BUCK1CTRL, \
179 .enable_mask = MAX77686_OPMODE_MASK, \
181 #define regulator_desc_buck_dvs(num) { \
182 .name = "BUCK"#num, \
183 .id = MAX77686_BUCK##num, \
184 .ops = &max77686_buck_dvs_ops, \
185 .type = REGULATOR_VOLTAGE, \
186 .owner = THIS_MODULE, \
187 .min_uV = MAX77686_DVS_MINUV, \
188 .uV_step = MAX77686_DVS_UVSTEP, \
189 .ramp_delay = MAX77686_DVS_RAMP_DELAY, \
190 .n_voltages = MAX77686_DVS_VSEL_MASK + 1, \
191 .vsel_reg = MAX77686_REG_BUCK2DVS1 + (num - 2) * 10, \
192 .vsel_mask = MAX77686_DVS_VSEL_MASK, \
193 .enable_reg = MAX77686_REG_BUCK2CTRL1 + (num - 2) * 10, \
194 .enable_mask = MAX77686_OPMODE_MASK \
195 << MAX77686_OPMODE_BUCK234_SHIFT, \
198 static struct regulator_desc regulators[] = {
199 regulator_desc_ldo_low(1),
200 regulator_desc_ldo_low(2),
201 regulator_desc_ldo(3),
202 regulator_desc_ldo(4),
203 regulator_desc_ldo(5),
204 regulator_desc_ldo_low(6),
205 regulator_desc_ldo_low(7),
206 regulator_desc_ldo_low(8),
207 regulator_desc_ldo(9),
208 regulator_desc_ldo(10),
209 regulator_desc_ldo(11),
210 regulator_desc_ldo(12),
211 regulator_desc_ldo(13),
212 regulator_desc_ldo(14),
213 regulator_desc_ldo_low(15),
214 regulator_desc_ldo(16),
215 regulator_desc_ldo(17),
216 regulator_desc_ldo(18),
217 regulator_desc_ldo(19),
218 regulator_desc_ldo(20),
219 regulator_desc_ldo(21),
220 regulator_desc_ldo(22),
221 regulator_desc_ldo(23),
222 regulator_desc_ldo(24),
223 regulator_desc_ldo(25),
224 regulator_desc_ldo(26),
225 regulator_desc_buck1(1),
226 regulator_desc_buck_dvs(2),
227 regulator_desc_buck_dvs(3),
228 regulator_desc_buck_dvs(4),
229 regulator_desc_buck(5),
230 regulator_desc_buck(6),
231 regulator_desc_buck(7),
232 regulator_desc_buck(8),
233 regulator_desc_buck(9),
236 static __devinit int max77686_pmic_probe(struct platform_device *pdev)
238 struct max77686_dev *iodev = dev_get_drvdata(pdev->dev.parent);
239 struct max77686_platform_data *pdata = dev_get_platdata(iodev->dev);
240 struct regulator_dev **rdev;
241 struct max77686_data *max77686;
244 struct regulator_config config = { };
246 dev_dbg(&pdev->dev, "%s\n", __func__);
248 if (!pdata || pdata->num_regulators != MAX77686_REGULATORS) {
250 "Invalid initial data for regulator's initialiation\n");
254 max77686 = devm_kzalloc(&pdev->dev, sizeof(struct max77686_data),
259 size = sizeof(struct regulator_dev *) * MAX77686_REGULATORS;
260 max77686->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
264 rdev = max77686->rdev;
265 config.dev = &pdev->dev;
266 config.regmap = iodev->regmap;
267 platform_set_drvdata(pdev, max77686);
269 for (i = 0; i < MAX77686_REGULATORS; i++) {
270 config.init_data = pdata->regulators[i].initdata;
272 rdev[i] = regulator_register(®ulators[i], &config);
273 if (IS_ERR(rdev[i])) {
274 ret = PTR_ERR(rdev[i]);
276 "regulator init failed for %d\n", i);
285 regulator_unregister(rdev[i]);
289 static int __devexit max77686_pmic_remove(struct platform_device *pdev)
291 struct max77686_data *max77686 = platform_get_drvdata(pdev);
292 struct regulator_dev **rdev = max77686->rdev;
295 for (i = 0; i < MAX77686_REGULATORS; i++)
297 regulator_unregister(rdev[i]);
302 static const struct platform_device_id max77686_pmic_id[] = {
303 {"max77686-pmic", 0},
306 MODULE_DEVICE_TABLE(platform, max77686_pmic_id);
308 static struct platform_driver max77686_pmic_driver = {
310 .name = "max77686-pmic",
311 .owner = THIS_MODULE,
313 .probe = max77686_pmic_probe,
314 .remove = __devexit_p(max77686_pmic_remove),
315 .id_table = max77686_pmic_id,
318 static int __init max77686_pmic_init(void)
320 return platform_driver_register(&max77686_pmic_driver);
322 subsys_initcall(max77686_pmic_init);
324 static void __exit max77686_pmic_cleanup(void)
326 platform_driver_unregister(&max77686_pmic_driver);
328 module_exit(max77686_pmic_cleanup);
330 MODULE_DESCRIPTION("MAXIM 77686 Regulator Driver");
331 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
332 MODULE_LICENSE("GPL");