blob: f6eee2924de9c7fa556f2b36ea74c3a4391b2509 [file] [log] [blame]
Ashish Jangam08bf1c02011-12-09 19:48:20 +05301/*
2* da9052-regulator.c: Regulator driver for DA9052
3*
4* Copyright(c) 2011 Dialog Semiconductor Ltd.
5*
6* Author: David Dajun Chen <dchen@diasemi.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*/
14
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>
Ying-Chun Liu (PaulLiu)88c84c12012-04-13 21:37:41 +080022#ifdef CONFIG_OF
23#include <linux/regulator/of_regulator.h>
24#endif
Ashish Jangam08bf1c02011-12-09 19:48:20 +053025
26#include <linux/mfd/da9052/da9052.h>
27#include <linux/mfd/da9052/reg.h>
28#include <linux/mfd/da9052/pdata.h>
29
30/* Buck step size */
31#define DA9052_BUCK_PERI_3uV_STEP 100000
32#define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV 24
33#define DA9052_CONST_3uV 3000000
34
35#define DA9052_MIN_UA 0
36#define DA9052_MAX_UA 3
37#define DA9052_CURRENT_RANGE 4
38
39/* Bit masks */
40#define DA9052_BUCK_ILIM_MASK_EVEN 0x0c
41#define DA9052_BUCK_ILIM_MASK_ODD 0xc0
42
Axel Lin9210f052012-03-15 19:58:39 +080043/* DA9052 REGULATOR IDs */
44#define DA9052_ID_BUCK1 0
45#define DA9052_ID_BUCK2 1
46#define DA9052_ID_BUCK3 2
47#define DA9052_ID_BUCK4 3
48#define DA9052_ID_LDO1 4
49#define DA9052_ID_LDO2 5
50#define DA9052_ID_LDO3 6
51#define DA9052_ID_LDO4 7
52#define DA9052_ID_LDO5 8
53#define DA9052_ID_LDO6 9
54#define DA9052_ID_LDO7 10
55#define DA9052_ID_LDO8 11
56#define DA9052_ID_LDO9 12
57#define DA9052_ID_LDO10 13
58
Ashish Jangam08bf1c02011-12-09 19:48:20 +053059static const u32 da9052_current_limits[3][4] = {
60 {700000, 800000, 1000000, 1200000}, /* DA9052-BC BUCKs */
61 {1600000, 2000000, 2400000, 3000000}, /* DA9053-AA/Bx BUCK-CORE */
62 {800000, 1000000, 1200000, 1500000}, /* DA9053-AA/Bx BUCK-PRO,
63 * BUCK-MEM and BUCK-PERI
64 */
65};
66
67struct da9052_regulator_info {
68 struct regulator_desc reg_desc;
69 int step_uV;
70 int min_uV;
71 int max_uV;
72 unsigned char volt_shift;
Ashish Jangam08bf1c02011-12-09 19:48:20 +053073 unsigned char activate_bit;
74};
75
76struct da9052_regulator {
77 struct da9052 *da9052;
78 struct da9052_regulator_info *info;
79 struct regulator_dev *rdev;
80};
81
82static int verify_range(struct da9052_regulator_info *info,
83 int min_uV, int max_uV)
84{
85 if (min_uV > info->max_uV || max_uV < info->min_uV)
86 return -EINVAL;
87
88 return 0;
89}
90
Ashish Jangam08bf1c02011-12-09 19:48:20 +053091static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
92{
93 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
94 int offset = rdev_get_id(rdev);
95 int ret, row = 2;
96
97 ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
98 if (ret < 0)
99 return ret;
100
101 /* Determine the even or odd position of the buck current limit
102 * register field
103 */
104 if (offset % 2 == 0)
105 ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
106 else
107 ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
108
109 /* Select the appropriate current limit range */
110 if (regulator->da9052->chip_id == DA9052)
111 row = 0;
112 else if (offset == 0)
113 row = 1;
114
115 return da9052_current_limits[row][ret];
116}
117
118static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
119 int max_uA)
120{
121 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
122 int offset = rdev_get_id(rdev);
123 int reg_val = 0;
124 int i, row = 2;
125
126 /* Select the appropriate current limit range */
127 if (regulator->da9052->chip_id == DA9052)
128 row = 0;
129 else if (offset == 0)
130 row = 1;
131
132 if (min_uA > da9052_current_limits[row][DA9052_MAX_UA] ||
133 max_uA < da9052_current_limits[row][DA9052_MIN_UA])
134 return -EINVAL;
135
136 for (i = 0; i < DA9052_CURRENT_RANGE; i++) {
137 if (min_uA <= da9052_current_limits[row][i]) {
138 reg_val = i;
139 break;
140 }
141 }
142
143 /* Determine the even or odd position of the buck current limit
144 * register field
145 */
146 if (offset % 2 == 0)
147 return da9052_reg_update(regulator->da9052,
148 DA9052_BUCKA_REG + offset/2,
149 DA9052_BUCK_ILIM_MASK_EVEN,
150 reg_val << 2);
151 else
152 return da9052_reg_update(regulator->da9052,
153 DA9052_BUCKA_REG + offset/2,
154 DA9052_BUCK_ILIM_MASK_ODD,
155 reg_val << 6);
156}
157
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530158static int da9052_list_voltage(struct regulator_dev *rdev,
159 unsigned int selector)
160{
161 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
162 struct da9052_regulator_info *info = regulator->info;
Axel Lin0ec446e2012-03-15 20:00:07 +0800163 int id = rdev_get_id(rdev);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530164 int volt_uV;
165
Axel Lin0ec446e2012-03-15 20:00:07 +0800166 if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
167 && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
168 volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
169 + info->min_uV);
170 volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
171 * (DA9052_BUCK_PERI_3uV_STEP);
172 } else {
173 volt_uV = (selector * info->step_uV) + info->min_uV;
174 }
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530175
176 if (volt_uV > info->max_uV)
177 return -EINVAL;
178
179 return volt_uV;
180}
181
Axel Lin0ec446e2012-03-15 20:00:07 +0800182static int da9052_regulator_set_voltage(struct regulator_dev *rdev,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530183 int min_uV, int max_uV,
184 unsigned int *selector)
185{
186 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
187 struct da9052_regulator_info *info = regulator->info;
Axel Lin0ec446e2012-03-15 20:00:07 +0800188 int id = rdev_get_id(rdev);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530189 int ret;
190
191 ret = verify_range(info, min_uV, max_uV);
192 if (ret < 0)
193 return ret;
194
195 if (min_uV < info->min_uV)
196 min_uV = info->min_uV;
197
Axel Lin0ec446e2012-03-15 20:00:07 +0800198 if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
199 && (min_uV >= DA9052_CONST_3uV)) {
200 *selector = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
201 DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
202 DA9052_BUCK_PERI_3uV_STEP);
203 } else {
204 *selector = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
205 }
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530206
207 ret = da9052_list_voltage(rdev, *selector);
208 if (ret < 0)
209 return ret;
210
Axel Lin0ec446e2012-03-15 20:00:07 +0800211 ret = da9052_reg_update(regulator->da9052,
212 DA9052_BUCKCORE_REG + id,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530213 (1 << info->volt_shift) - 1, *selector);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530214 if (ret < 0)
215 return ret;
216
Axel Lin0ec446e2012-03-15 20:00:07 +0800217 /* Some LDOs and DCDCs are DVC controlled which requires enabling of
218 * the activate bit to implment the changes on the output.
219 */
220 switch (id) {
221 case DA9052_ID_BUCK1:
222 case DA9052_ID_BUCK2:
223 case DA9052_ID_BUCK3:
224 case DA9052_ID_LDO2:
225 case DA9052_ID_LDO3:
226 ret = da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
227 info->activate_bit, info->activate_bit);
228 break;
229 }
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530230
Axel Lin0ec446e2012-03-15 20:00:07 +0800231 return ret;
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530232}
233
234static int da9052_get_regulator_voltage_sel(struct regulator_dev *rdev)
235{
236 struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
237 struct da9052_regulator_info *info = regulator->info;
238 int offset = rdev_get_id(rdev);
239 int ret;
240
241 ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
242 if (ret < 0)
243 return ret;
244
245 ret &= ((1 << info->volt_shift) - 1);
246
247 return ret;
248}
249
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530250static struct regulator_ops da9052_dcdc_ops = {
Axel Lin0ec446e2012-03-15 20:00:07 +0800251 .set_voltage = da9052_regulator_set_voltage,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530252 .get_current_limit = da9052_dcdc_get_current_limit,
253 .set_current_limit = da9052_dcdc_set_current_limit,
254
255 .list_voltage = da9052_list_voltage,
256 .get_voltage_sel = da9052_get_regulator_voltage_sel,
Axel Lin0d481f72012-04-17 11:34:32 +0800257 .is_enabled = regulator_is_enabled_regmap,
258 .enable = regulator_enable_regmap,
259 .disable = regulator_disable_regmap,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530260};
261
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530262static struct regulator_ops da9052_ldo_ops = {
Axel Lin0ec446e2012-03-15 20:00:07 +0800263 .set_voltage = da9052_regulator_set_voltage,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530264
265 .list_voltage = da9052_list_voltage,
266 .get_voltage_sel = da9052_get_regulator_voltage_sel,
Axel Lin0d481f72012-04-17 11:34:32 +0800267 .is_enabled = regulator_is_enabled_regmap,
268 .enable = regulator_enable_regmap,
269 .disable = regulator_disable_regmap,
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530270};
271
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530272#define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
273{\
274 .reg_desc = {\
Axel Lin9210f052012-03-15 19:58:39 +0800275 .name = #_id,\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530276 .ops = &da9052_ldo_ops,\
277 .type = REGULATOR_VOLTAGE,\
Axel Lin9210f052012-03-15 19:58:39 +0800278 .id = DA9052_ID_##_id,\
Axel Lin7b957652012-03-05 20:04:32 +0800279 .n_voltages = (max - min) / step + 1, \
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530280 .owner = THIS_MODULE,\
Axel Lin0d481f72012-04-17 11:34:32 +0800281 .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
282 .enable_mask = 1 << (ebits),\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530283 },\
284 .min_uV = (min) * 1000,\
285 .max_uV = (max) * 1000,\
286 .step_uV = (step) * 1000,\
287 .volt_shift = (sbits),\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530288 .activate_bit = (abits),\
289}
290
291#define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
292{\
293 .reg_desc = {\
Axel Lin9210f052012-03-15 19:58:39 +0800294 .name = #_id,\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530295 .ops = &da9052_dcdc_ops,\
296 .type = REGULATOR_VOLTAGE,\
Axel Lin9210f052012-03-15 19:58:39 +0800297 .id = DA9052_ID_##_id,\
Axel Lin7b957652012-03-05 20:04:32 +0800298 .n_voltages = (max - min) / step + 1, \
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530299 .owner = THIS_MODULE,\
Axel Lin0d481f72012-04-17 11:34:32 +0800300 .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
301 .enable_mask = 1 << (ebits),\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530302 },\
303 .min_uV = (min) * 1000,\
304 .max_uV = (max) * 1000,\
305 .step_uV = (step) * 1000,\
306 .volt_shift = (sbits),\
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530307 .activate_bit = (abits),\
308}
309
Axel Lin6242eae2011-12-20 17:12:00 +0800310static struct da9052_regulator_info da9052_regulator_info[] = {
Axel Lin9210f052012-03-15 19:58:39 +0800311 DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
312 DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
313 DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
Axel Lin0ec446e2012-03-15 20:00:07 +0800314 DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
Axel Lin9210f052012-03-15 19:58:39 +0800315 DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
Axel Lin0ec446e2012-03-15 20:00:07 +0800316 DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
317 DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
Axel Lin9210f052012-03-15 19:58:39 +0800318 DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
319 DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
320 DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
321 DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
322 DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
323 DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
324 DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530325};
326
Axel Lin6242eae2011-12-20 17:12:00 +0800327static struct da9052_regulator_info da9053_regulator_info[] = {
Axel Lin9210f052012-03-15 19:58:39 +0800328 DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
329 DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
330 DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
Axel Lin0ec446e2012-03-15 20:00:07 +0800331 DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
Axel Lin9210f052012-03-15 19:58:39 +0800332 DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
Axel Lin0ec446e2012-03-15 20:00:07 +0800333 DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
334 DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
Axel Lin9210f052012-03-15 19:58:39 +0800335 DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
336 DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
337 DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
338 DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
339 DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
340 DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
341 DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530342};
343
344static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
345 int id)
346{
347 struct da9052_regulator_info *info;
348 int i;
349
Ashish Jangam984b5a62011-12-15 18:59:53 +0530350 switch (chip_id) {
351 case DA9052:
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530352 for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
353 info = &da9052_regulator_info[i];
354 if (info->reg_desc.id == id)
355 return info;
356 }
Ashish Jangam984b5a62011-12-15 18:59:53 +0530357 break;
358 case DA9053_AA:
359 case DA9053_BA:
360 case DA9053_BB:
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530361 for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
362 info = &da9053_regulator_info[i];
363 if (info->reg_desc.id == id)
364 return info;
365 }
Ashish Jangam984b5a62011-12-15 18:59:53 +0530366 break;
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530367 }
368
369 return NULL;
370}
371
372static int __devinit da9052_regulator_probe(struct platform_device *pdev)
373{
Mark Brownc1727082012-04-04 00:50:22 +0100374 struct regulator_config config = { };
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530375 struct da9052_regulator *regulator;
376 struct da9052 *da9052;
377 struct da9052_pdata *pdata;
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530378
Ashish Jangam984b5a62011-12-15 18:59:53 +0530379 regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
380 GFP_KERNEL);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530381 if (!regulator)
382 return -ENOMEM;
383
384 da9052 = dev_get_drvdata(pdev->dev.parent);
385 pdata = da9052->dev->platform_data;
386 regulator->da9052 = da9052;
387
388 regulator->info = find_regulator_info(regulator->da9052->chip_id,
389 pdev->id);
390 if (regulator->info == NULL) {
391 dev_err(&pdev->dev, "invalid regulator ID specified\n");
Axel Lin7eb64442012-04-05 12:08:58 +0800392 return -EINVAL;
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530393 }
Mark Brownc1727082012-04-04 00:50:22 +0100394
395 config.dev = &pdev->dev;
Mark Brownc1727082012-04-04 00:50:22 +0100396 config.driver_data = regulator;
Axel Lin0d481f72012-04-17 11:34:32 +0800397 config.regmap = da9052->regmap;
Ying-Chun Liu (PaulLiu)88c84c12012-04-13 21:37:41 +0800398 if (pdata && pdata->regulators) {
399 config.init_data = pdata->regulators[pdev->id];
400 } else {
401#ifdef CONFIG_OF
402 struct device_node *nproot = da9052->dev->of_node;
403 struct device_node *np;
404
405 if (!nproot)
406 return -ENODEV;
407
408 nproot = of_find_node_by_name(nproot, "regulators");
409 if (!nproot)
410 return -ENODEV;
411
412 for (np = of_get_next_child(nproot, NULL); !np;
413 np = of_get_next_child(nproot, np)) {
414 if (!of_node_cmp(np->name,
415 regulator->info->reg_desc.name)) {
416 config.init_data = of_get_regulator_init_data(
417 &pdev->dev, np);
418 break;
419 }
420 }
421#endif
422 }
Mark Brownc1727082012-04-04 00:50:22 +0100423
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530424 regulator->rdev = regulator_register(&regulator->info->reg_desc,
Mark Brownc1727082012-04-04 00:50:22 +0100425 &config);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530426 if (IS_ERR(regulator->rdev)) {
427 dev_err(&pdev->dev, "failed to register regulator %s\n",
428 regulator->info->reg_desc.name);
Axel Lin7eb64442012-04-05 12:08:58 +0800429 return PTR_ERR(regulator->rdev);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530430 }
431
432 platform_set_drvdata(pdev, regulator);
433
434 return 0;
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530435}
436
437static int __devexit da9052_regulator_remove(struct platform_device *pdev)
438{
439 struct da9052_regulator *regulator = platform_get_drvdata(pdev);
440
441 regulator_unregister(regulator->rdev);
Ashish Jangam08bf1c02011-12-09 19:48:20 +0530442 return 0;
443}
444
445static struct platform_driver da9052_regulator_driver = {
446 .probe = da9052_regulator_probe,
447 .remove = __devexit_p(da9052_regulator_remove),
448 .driver = {
449 .name = "da9052-regulator",
450 .owner = THIS_MODULE,
451 },
452};
453
454static int __init da9052_regulator_init(void)
455{
456 return platform_driver_register(&da9052_regulator_driver);
457}
458subsys_initcall(da9052_regulator_init);
459
460static void __exit da9052_regulator_exit(void)
461{
462 platform_driver_unregister(&da9052_regulator_driver);
463}
464module_exit(da9052_regulator_exit);
465
466MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
467MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
468MODULE_LICENSE("GPL");
469MODULE_ALIAS("platform:da9052-regulator");