2 * da9052-regulator.c: Regulator driver for DA9052
4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.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.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
24 #include <linux/regulator/of_regulator.h>
27 #include <linux/mfd/da9052/da9052.h>
28 #include <linux/mfd/da9052/reg.h>
29 #include <linux/mfd/da9052/pdata.h>
32 #define DA9052_BUCK_PERI_3uV_STEP 100000
33 #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV 24
34 #define DA9052_CONST_3uV 3000000
36 #define DA9052_MIN_UA 0
37 #define DA9052_MAX_UA 3
38 #define DA9052_CURRENT_RANGE 4
41 #define DA9052_BUCK_ILIM_MASK_EVEN 0x0c
42 #define DA9052_BUCK_ILIM_MASK_ODD 0xc0
44 /* DA9052 REGULATOR IDs */
45 #define DA9052_ID_BUCK1 0
46 #define DA9052_ID_BUCK2 1
47 #define DA9052_ID_BUCK3 2
48 #define DA9052_ID_BUCK4 3
49 #define DA9052_ID_LDO1 4
50 #define DA9052_ID_LDO2 5
51 #define DA9052_ID_LDO3 6
52 #define DA9052_ID_LDO4 7
53 #define DA9052_ID_LDO5 8
54 #define DA9052_ID_LDO6 9
55 #define DA9052_ID_LDO7 10
56 #define DA9052_ID_LDO8 11
57 #define DA9052_ID_LDO9 12
58 #define DA9052_ID_LDO10 13
60 static const u32 da9052_current_limits[3][4] = {
61 {700000, 800000, 1000000, 1200000}, /* DA9052-BC BUCKs */
62 {1600000, 2000000, 2400000, 3000000}, /* DA9053-AA/Bx BUCK-CORE */
63 {800000, 1000000, 1200000, 1500000}, /* DA9053-AA/Bx BUCK-PRO,
64 * BUCK-MEM and BUCK-PERI
68 struct da9052_regulator_info {
69 struct regulator_desc reg_desc;
75 struct da9052_regulator {
76 struct da9052 *da9052;
77 struct da9052_regulator_info *info;
78 struct regulator_dev *rdev;
81 static int verify_range(struct da9052_regulator_info *info,
82 int min_uV, int max_uV)
84 if (min_uV > info->max_uV || max_uV < info->min_uV)
90 static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
92 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
93 int offset = rdev_get_id(rdev);
96 ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
100 /* Determine the even or odd position of the buck current limit
104 ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
106 ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
108 /* Select the appropriate current limit range */
109 if (regulator->da9052->chip_id == DA9052)
111 else if (offset == 0)
114 return da9052_current_limits[row][ret];
117 static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
120 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
121 int offset = rdev_get_id(rdev);
125 /* Select the appropriate current limit range */
126 if (regulator->da9052->chip_id == DA9052)
128 else if (offset == 0)
131 for (i = DA9052_CURRENT_RANGE - 1; i >= 0; i--) {
132 if ((min_uA <= da9052_current_limits[row][i]) &&
133 (da9052_current_limits[row][i] <= max_uA)) {
142 /* Determine the even or odd position of the buck current limit
146 return da9052_reg_update(regulator->da9052,
147 DA9052_BUCKA_REG + offset/2,
148 DA9052_BUCK_ILIM_MASK_EVEN,
151 return da9052_reg_update(regulator->da9052,
152 DA9052_BUCKA_REG + offset/2,
153 DA9052_BUCK_ILIM_MASK_ODD,
157 static int da9052_list_voltage(struct regulator_dev *rdev,
158 unsigned int selector)
160 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
161 struct da9052_regulator_info *info = regulator->info;
162 int id = rdev_get_id(rdev);
165 if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
166 && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
167 volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
169 volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
170 * (DA9052_BUCK_PERI_3uV_STEP);
172 volt_uV = (selector * info->step_uV) + info->min_uV;
175 if (volt_uV > info->max_uV)
181 static int da9052_map_voltage(struct regulator_dev *rdev,
182 int min_uV, int max_uV)
184 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
185 struct da9052_regulator_info *info = regulator->info;
186 int id = rdev_get_id(rdev);
189 ret = verify_range(info, min_uV, max_uV);
193 if (min_uV < info->min_uV)
194 min_uV = info->min_uV;
196 if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
197 && (min_uV >= DA9052_CONST_3uV)) {
198 sel = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
199 DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
200 DA9052_BUCK_PERI_3uV_STEP);
202 sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
205 ret = da9052_list_voltage(rdev, sel);
212 static struct regulator_ops da9052_dcdc_ops = {
213 .get_current_limit = da9052_dcdc_get_current_limit,
214 .set_current_limit = da9052_dcdc_set_current_limit,
216 .list_voltage = da9052_list_voltage,
217 .map_voltage = da9052_map_voltage,
218 .get_voltage_sel = regulator_get_voltage_sel_regmap,
219 .set_voltage_sel = regulator_set_voltage_sel_regmap,
220 .is_enabled = regulator_is_enabled_regmap,
221 .enable = regulator_enable_regmap,
222 .disable = regulator_disable_regmap,
225 static struct regulator_ops da9052_ldo_ops = {
226 .list_voltage = da9052_list_voltage,
227 .map_voltage = da9052_map_voltage,
228 .get_voltage_sel = regulator_get_voltage_sel_regmap,
229 .set_voltage_sel = regulator_set_voltage_sel_regmap,
230 .is_enabled = regulator_is_enabled_regmap,
231 .enable = regulator_enable_regmap,
232 .disable = regulator_disable_regmap,
235 #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
239 .ops = &da9052_ldo_ops,\
240 .type = REGULATOR_VOLTAGE,\
241 .id = DA9052_ID_##_id,\
242 .n_voltages = (max - min) / step + 1, \
243 .owner = THIS_MODULE,\
244 .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
245 .vsel_mask = (1 << (sbits)) - 1,\
246 .apply_reg = DA9052_SUPPLY_REG, \
247 .apply_bit = (abits), \
248 .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
249 .enable_mask = 1 << (ebits),\
251 .min_uV = (min) * 1000,\
252 .max_uV = (max) * 1000,\
253 .step_uV = (step) * 1000,\
256 #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
260 .ops = &da9052_dcdc_ops,\
261 .type = REGULATOR_VOLTAGE,\
262 .id = DA9052_ID_##_id,\
263 .n_voltages = (max - min) / step + 1, \
264 .owner = THIS_MODULE,\
265 .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
266 .vsel_mask = (1 << (sbits)) - 1,\
267 .apply_reg = DA9052_SUPPLY_REG, \
268 .apply_bit = (abits), \
269 .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
270 .enable_mask = 1 << (ebits),\
272 .min_uV = (min) * 1000,\
273 .max_uV = (max) * 1000,\
274 .step_uV = (step) * 1000,\
277 static struct da9052_regulator_info da9052_regulator_info[] = {
278 DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
279 DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
280 DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
281 DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
282 DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
283 DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
284 DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
285 DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
286 DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
287 DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
288 DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
289 DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
290 DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
291 DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
294 static struct da9052_regulator_info da9053_regulator_info[] = {
295 DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
296 DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
297 DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
298 DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
299 DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
300 DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
301 DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
302 DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
303 DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
304 DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
305 DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
306 DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
307 DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
308 DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
311 static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
314 struct da9052_regulator_info *info;
319 for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
320 info = &da9052_regulator_info[i];
321 if (info->reg_desc.id == id)
328 for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
329 info = &da9053_regulator_info[i];
330 if (info->reg_desc.id == id)
339 static int da9052_regulator_probe(struct platform_device *pdev)
341 struct regulator_config config = { };
342 struct da9052_regulator *regulator;
343 struct da9052 *da9052;
344 struct da9052_pdata *pdata;
346 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
351 da9052 = dev_get_drvdata(pdev->dev.parent);
352 pdata = dev_get_platdata(da9052->dev);
353 regulator->da9052 = da9052;
355 regulator->info = find_regulator_info(regulator->da9052->chip_id,
357 if (regulator->info == NULL) {
358 dev_err(&pdev->dev, "invalid regulator ID specified\n");
362 config.dev = &pdev->dev;
363 config.driver_data = regulator;
364 config.regmap = da9052->regmap;
365 if (pdata && pdata->regulators) {
366 config.init_data = pdata->regulators[pdev->id];
369 struct device_node *nproot, *np;
371 nproot = of_node_get(da9052->dev->of_node);
375 nproot = of_find_node_by_name(nproot, "regulators");
379 for_each_child_of_node(nproot, np) {
380 if (!of_node_cmp(np->name,
381 regulator->info->reg_desc.name)) {
382 config.init_data = of_get_regulator_init_data(
392 regulator->rdev = regulator_register(®ulator->info->reg_desc,
394 if (IS_ERR(regulator->rdev)) {
395 dev_err(&pdev->dev, "failed to register regulator %s\n",
396 regulator->info->reg_desc.name);
397 return PTR_ERR(regulator->rdev);
400 platform_set_drvdata(pdev, regulator);
405 static int da9052_regulator_remove(struct platform_device *pdev)
407 struct da9052_regulator *regulator = platform_get_drvdata(pdev);
409 regulator_unregister(regulator->rdev);
413 static struct platform_driver da9052_regulator_driver = {
414 .probe = da9052_regulator_probe,
415 .remove = da9052_regulator_remove,
417 .name = "da9052-regulator",
418 .owner = THIS_MODULE,
422 static int __init da9052_regulator_init(void)
424 return platform_driver_register(&da9052_regulator_driver);
426 subsys_initcall(da9052_regulator_init);
428 static void __exit da9052_regulator_exit(void)
430 platform_driver_unregister(&da9052_regulator_driver);
432 module_exit(da9052_regulator_exit);
434 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
435 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
436 MODULE_LICENSE("GPL");
437 MODULE_ALIAS("platform:da9052-regulator");