Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
da9052-regulator.c
Go to the documentation of this file.
1 /*
2 * da9052-regulator.c: Regulator driver for DA9052
3 *
4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <[email protected]>
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>
22 #ifdef CONFIG_OF
23 #include <linux/of.h>
25 #endif
26 
28 #include <linux/mfd/da9052/reg.h>
29 #include <linux/mfd/da9052/pdata.h>
30 
31 /* Buck step size */
32 #define DA9052_BUCK_PERI_3uV_STEP 100000
33 #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV 24
34 #define DA9052_CONST_3uV 3000000
35 
36 #define DA9052_MIN_UA 0
37 #define DA9052_MAX_UA 3
38 #define DA9052_CURRENT_RANGE 4
39 
40 /* Bit masks */
41 #define DA9052_BUCK_ILIM_MASK_EVEN 0x0c
42 #define DA9052_BUCK_ILIM_MASK_ODD 0xc0
43 
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
59 
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
65  */
66 };
67 
70  int step_uV;
71  int min_uV;
72  int max_uV;
73  unsigned char activate_bit;
74 };
75 
77  struct da9052 *da9052;
80 };
81 
82 static 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 
91 static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
92 {
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 
118 static 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 = DA9052_CURRENT_RANGE - 1; i >= 0; i--) {
137  if (da9052_current_limits[row][i] <= max_uA) {
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,
150  reg_val << 2);
151  else
152  return da9052_reg_update(regulator->da9052,
153  DA9052_BUCKA_REG + offset/2,
155  reg_val << 6);
156 }
157 
158 static 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;
163  int id = rdev_get_id(rdev);
164  int volt_uV;
165 
166  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)
172  } else {
173  volt_uV = (selector * info->step_uV) + info->min_uV;
174  }
175 
176  if (volt_uV > info->max_uV)
177  return -EINVAL;
178 
179  return volt_uV;
180 }
181 
182 static int da9052_map_voltage(struct regulator_dev *rdev,
183  int min_uV, int max_uV)
184 {
185  struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
186  struct da9052_regulator_info *info = regulator->info;
187  int id = rdev_get_id(rdev);
188  int ret, sel;
189 
190  ret = verify_range(info, min_uV, max_uV);
191  if (ret < 0)
192  return ret;
193 
194  if (min_uV < info->min_uV)
195  min_uV = info->min_uV;
196 
197  if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
198  && (min_uV >= DA9052_CONST_3uV)) {
202  } else {
203  sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
204  }
205 
206  ret = da9052_list_voltage(rdev, sel);
207  if (ret < 0)
208  return ret;
209 
210  return sel;
211 }
212 
213 static int da9052_regulator_set_voltage_sel(struct regulator_dev *rdev,
214  unsigned int selector)
215 {
216  struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
217  struct da9052_regulator_info *info = regulator->info;
218  int id = rdev_get_id(rdev);
219  int ret;
220 
221  ret = da9052_reg_update(regulator->da9052, rdev->desc->vsel_reg,
222  rdev->desc->vsel_mask, selector);
223  if (ret < 0)
224  return ret;
225 
226  /* Some LDOs and DCDCs are DVC controlled which requires enabling of
227  * the activate bit to implment the changes on the output.
228  */
229  switch (id) {
230  case DA9052_ID_BUCK1:
231  case DA9052_ID_BUCK2:
232  case DA9052_ID_BUCK3:
233  case DA9052_ID_LDO2:
234  case DA9052_ID_LDO3:
235  ret = da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
236  info->activate_bit, info->activate_bit);
237  break;
238  }
239 
240  return ret;
241 }
242 
243 static struct regulator_ops da9052_dcdc_ops = {
244  .get_current_limit = da9052_dcdc_get_current_limit,
245  .set_current_limit = da9052_dcdc_set_current_limit,
246 
247  .list_voltage = da9052_list_voltage,
248  .map_voltage = da9052_map_voltage,
249  .get_voltage_sel = regulator_get_voltage_sel_regmap,
250  .set_voltage_sel = da9052_regulator_set_voltage_sel,
251  .is_enabled = regulator_is_enabled_regmap,
252  .enable = regulator_enable_regmap,
253  .disable = regulator_disable_regmap,
254 };
255 
256 static struct regulator_ops da9052_ldo_ops = {
257  .list_voltage = da9052_list_voltage,
258  .map_voltage = da9052_map_voltage,
259  .get_voltage_sel = regulator_get_voltage_sel_regmap,
260  .set_voltage_sel = da9052_regulator_set_voltage_sel,
261  .is_enabled = regulator_is_enabled_regmap,
262  .enable = regulator_enable_regmap,
263  .disable = regulator_disable_regmap,
264 };
265 
266 #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
267 {\
268  .reg_desc = {\
269  .name = #_id,\
270  .ops = &da9052_ldo_ops,\
271  .type = REGULATOR_VOLTAGE,\
272  .id = DA9052_ID_##_id,\
273  .n_voltages = (max - min) / step + 1, \
274  .owner = THIS_MODULE,\
275  .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
276  .vsel_mask = (1 << (sbits)) - 1,\
277  .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
278  .enable_mask = 1 << (ebits),\
279  },\
280  .min_uV = (min) * 1000,\
281  .max_uV = (max) * 1000,\
282  .step_uV = (step) * 1000,\
283  .activate_bit = (abits),\
284 }
285 
286 #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
287 {\
288  .reg_desc = {\
289  .name = #_id,\
290  .ops = &da9052_dcdc_ops,\
291  .type = REGULATOR_VOLTAGE,\
292  .id = DA9052_ID_##_id,\
293  .n_voltages = (max - min) / step + 1, \
294  .owner = THIS_MODULE,\
295  .vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
296  .vsel_mask = (1 << (sbits)) - 1,\
297  .enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
298  .enable_mask = 1 << (ebits),\
299  },\
300  .min_uV = (min) * 1000,\
301  .max_uV = (max) * 1000,\
302  .step_uV = (step) * 1000,\
303  .activate_bit = (abits),\
304 }
305 
307  DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
308  DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
309  DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
310  DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
311  DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
312  DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
313  DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
314  DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
315  DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
316  DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
317  DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
318  DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
319  DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
320  DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
321 };
322 
323 static struct da9052_regulator_info da9053_regulator_info[] = {
324  DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
325  DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
326  DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
327  DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
328  DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
329  DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
330  DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
331  DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
332  DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
333  DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
334  DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
335  DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
336  DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
337  DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
338 };
339 
340 static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
341  int id)
342 {
343  struct da9052_regulator_info *info;
344  int i;
345 
346  switch (chip_id) {
347  case DA9052:
348  for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
349  info = &da9052_regulator_info[i];
350  if (info->reg_desc.id == id)
351  return info;
352  }
353  break;
354  case DA9053_AA:
355  case DA9053_BA:
356  case DA9053_BB:
357  for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
358  info = &da9053_regulator_info[i];
359  if (info->reg_desc.id == id)
360  return info;
361  }
362  break;
363  }
364 
365  return NULL;
366 }
367 
368 static int __devinit da9052_regulator_probe(struct platform_device *pdev)
369 {
370  struct regulator_config config = { };
371  struct da9052_regulator *regulator;
372  struct da9052 *da9052;
373  struct da9052_pdata *pdata;
374 
375  regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
376  GFP_KERNEL);
377  if (!regulator)
378  return -ENOMEM;
379 
380  da9052 = dev_get_drvdata(pdev->dev.parent);
381  pdata = da9052->dev->platform_data;
382  regulator->da9052 = da9052;
383 
384  regulator->info = find_regulator_info(regulator->da9052->chip_id,
385  pdev->id);
386  if (regulator->info == NULL) {
387  dev_err(&pdev->dev, "invalid regulator ID specified\n");
388  return -EINVAL;
389  }
390 
391  config.dev = &pdev->dev;
392  config.driver_data = regulator;
393  config.regmap = da9052->regmap;
394  if (pdata && pdata->regulators) {
395  config.init_data = pdata->regulators[pdev->id];
396  } else {
397 #ifdef CONFIG_OF
398  struct device_node *nproot = da9052->dev->of_node;
399  struct device_node *np;
400 
401  if (!nproot)
402  return -ENODEV;
403 
404  nproot = of_find_node_by_name(nproot, "regulators");
405  if (!nproot)
406  return -ENODEV;
407 
408  for_each_child_of_node(nproot, np) {
409  if (!of_node_cmp(np->name,
410  regulator->info->reg_desc.name)) {
412  &pdev->dev, np);
413  config.of_node = np;
414  break;
415  }
416  }
417 #endif
418  }
419 
420  regulator->rdev = regulator_register(&regulator->info->reg_desc,
421  &config);
422  if (IS_ERR(regulator->rdev)) {
423  dev_err(&pdev->dev, "failed to register regulator %s\n",
424  regulator->info->reg_desc.name);
425  return PTR_ERR(regulator->rdev);
426  }
427 
428  platform_set_drvdata(pdev, regulator);
429 
430  return 0;
431 }
432 
433 static int __devexit da9052_regulator_remove(struct platform_device *pdev)
434 {
435  struct da9052_regulator *regulator = platform_get_drvdata(pdev);
436 
437  regulator_unregister(regulator->rdev);
438  return 0;
439 }
440 
441 static struct platform_driver da9052_regulator_driver = {
442  .probe = da9052_regulator_probe,
443  .remove = __devexit_p(da9052_regulator_remove),
444  .driver = {
445  .name = "da9052-regulator",
446  .owner = THIS_MODULE,
447  },
448 };
449 
450 static int __init da9052_regulator_init(void)
451 {
452  return platform_driver_register(&da9052_regulator_driver);
453 }
454 subsys_initcall(da9052_regulator_init);
455 
456 static void __exit da9052_regulator_exit(void)
457 {
458  platform_driver_unregister(&da9052_regulator_driver);
459 }
460 module_exit(da9052_regulator_exit);
461 
462 MODULE_AUTHOR("David Dajun Chen <[email protected]>");
463 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS("platform:da9052-regulator");