2 * drivers/regulator/rt5025-regulator.c
3 * Driver foo Richtek RT5025 PMIC Regulator
5 * Copyright (C) 2013 Richtek Electronics
6 * cy_huang <cy_huang@richtek.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 version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/version.h>
21 #include <linux/mfd/rt5025.h>
22 #include <linux/regulator/rt5025-regulator.h>
24 struct rt5025_regulator_info {
25 struct regulator_desc desc;
26 struct regulator_dev *regulator;
27 struct i2c_client *i2c;
28 struct rt5025_chip *chip;
29 const unsigned int *vol_output_list;
30 const int vol_output_size;
41 static const unsigned int rt5025_vol_output_list1[] =
43 700*1000, 725*1000, 750*1000, 775*1000, 800*1000, 825*1000, 850*1000, 875*1000,
44 900*1000, 925*1000, 950*1000, 975*1000, 1000*1000, 1025*1000, 1050*1000, 1075*1000,
45 1100*1000, 1125*1000, 1150*1000, 1175*1000, 1200*1000, 1225*1000, 1250*1000, 1275*1000,
46 1300*1000, 1325*1000, 1350*1000, 1375*1000, 1400*1000, 1425*1000, 1450*1000, 1475*1000,
47 1500*1000, 1525*1000, 1550*1000, 1575*1000, 1600*1000, 1625*1000, 1650*1000, 1675*1000,
48 1700*1000, 1725*1000, 1750*1000, 1775*1000, 1800*1000, 1825*1000, 1850*1000, 1875*1000,
49 1900*1000, 1925*1000, 1950*1000, 1975*1000, 2000*2000, 2025*1000, 2050*1000, 2075*1000,
50 2100*1000, 2125*1000, 2150*1000, 2175*1000, 2200*1000, 2225*1000, 2250*1000, 2275*1000,
52 #define rt5025_vol_output_size1 ARRAY_SIZE(rt5025_vol_output_list1)
55 static const unsigned int rt5025_vol_output_list2[] =
57 700*1000, 725*1000, 750*1000, 775*1000, 800*1000, 825*1000, 850*1000, 875*1000,
58 900*1000, 925*1000, 950*1000, 975*1000, 1000*1000, 1025*1000, 1050*1000, 1075*1000,
59 1100*1000, 1125*1000, 1150*1000, 1175*1000, 1200*1000, 1225*1000, 1250*1000, 1275*1000,
60 1300*1000, 1325*1000, 1350*1000, 1375*1000, 1400*1000, 1425*1000, 1450*1000, 1475*1000,
61 1500*1000, 1525*1000, 1550*1000, 1575*1000, 1600*1000, 1625*1000, 1650*1000, 1675*1000,
62 1700*1000, 1725*1000, 1750*1000, 1775*1000, 1800*1000, 1825*1000, 1850*1000, 1875*1000,
63 1900*1000, 1925*1000, 1950*1000, 1975*1000, 2000*2000, 2025*1000, 2050*1000, 2075*1000,
64 2100*1000, 2125*1000, 2150*1000, 2175*1000, 2200*1000, 2225*1000, 2250*1000, 2275*1000,
65 2300*1000, 2325*1000, 2350*1000, 2375*1000, 2400*1000, 2425*1000, 2450*1000, 2475*1000,
66 2500*1000, 2525*1000, 2550*1000, 2575*1000, 2600*1000, 2625*1000, 2650*1000, 2675*1000,
67 2700*1000, 2725*1000, 2750*1000, 2775*1000, 2800*1000, 2825*1000, 2850*1000, 2875*1000,
68 2900*1000, 2925*1000, 2950*1000, 2975*1000, 3000*1000, 3025*1000, 3050*1000, 3075*1000,
69 3100*1000, 3125*1000, 3150*1000, 3175*1000, 3200*1000, 3225*1000, 3250*1000, 3275*1000,
70 3300*1000, 3325*1000, 3350*1000, 3375*1000, 3400*1000, 3425*1000, 3450*1000, 3475*1000,
71 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000,
72 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000,
74 #define rt5025_vol_output_size2 ARRAY_SIZE(rt5025_vol_output_list2)
77 static const unsigned int rt5025_vol_output_list3[] =
79 700*1000, 750*1000, 800*1000, 850*1000, 900*1000, 950*1000, 1000*1000, 1050*1000,
80 1100*1000, 1150*1000, 1200*1000, 1250*1000, 1300*1000, 1350*1000, 1400*1000, 1450*1000,
81 1500*1000, 1550*1000, 1600*1000, 1650*1000, 1700*1000, 1750*1000, 1800*1000, 1850*1000,
82 1900*1000, 1950*1000, 2000*1000, 2050*1000, 2100*1000, 2150*1000, 2200*1000, 2250*1000,
83 2300*1000, 2350*1000, 2400*1000, 2450*1000, 2500*1000, 2550*1000, 2600*1000, 2650*1000,
84 2700*1000, 2750*1000, 2800*1000, 2850*1000, 2900*1000, 2950*1000, 3000*1000, 3050*1000,
85 3100*1000, 3150*1000, 3200*1000, 3250*1000, 3300*1000, 3350*1000, 3400*1000, 3450*1000,
86 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000, 3500*1000,
88 #define rt5025_vol_output_size3 ARRAY_SIZE(rt5025_vol_output_list3)
91 static const unsigned int rt5025_vol_output_list4[] =
93 4500*1000, 4600*1000, 4700*1000, 4800*1000, 4900*1000, 5000*1000, 5100*1000, 5200*1000,
94 5300*1000, 5400*1000, 5500*1000, 5500*1000, 5500*1000, 5500*1000, 5500*1000, 5500*1000,
96 #define rt5025_vol_output_size4 ARRAY_SIZE(rt5025_vol_output_list4)
98 //LDO3, LDO4, LDO5, LDO6
99 static const unsigned int rt5025_vol_output_list5[] =
101 1000*1000, 1100*1000, 1200*1000, 1300*1000, 1400*1000, 1500*1000, 1600*1000, 1700*1000,
102 1800*1000, 1900*1000, 2000*1000, 2100*1000, 2200*1000, 2300*1000, 2400*1000, 2500*1000,
103 2600*1000, 2700*1000, 2800*1000, 2900*1000, 3000*1000, 3100*1000, 3200*1000, 3300*1000,
104 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000,
105 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000, 3300*1000,
107 #define rt5025_vol_output_size5 ARRAY_SIZE(rt5025_vol_output_list5)
109 static inline int check_range(struct rt5025_regulator_info *info,
110 int min_uV, int max_uV)
112 if (min_uV < info->min_uV || min_uV > info->max_uV)
118 static int rt5025_list_voltage(struct regulator_dev *rdev, unsigned index)
120 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
122 return (index>=info->vol_output_size)? \
124 info->vol_output_list[index ];
127 //#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38))
128 static int rt5025_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
130 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
132 const int count = info->vol_output_size;
136 data = (unsigned char)selector;
137 data <<= info->vol_shift;
138 return rt5025_assign_bits(info->i2c, info->vol_reg, info->vol_mask, data);
141 static int rt5025_get_voltage_sel(struct regulator_dev *rdev)
143 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
145 ret = rt5025_reg_read(info->i2c, info->vol_reg);
148 return (ret & info->vol_mask) >> info->vol_shift;
151 static int rt5025_find_voltage(struct regulator_dev *rdev,
152 int min_uV, int max_uV)
154 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
156 const int count = info->vol_output_size;
157 for (i=0;i<count;i++)
159 if ((info->vol_output_list[i]>=min_uV)
160 && (info->vol_output_list[i]<=max_uV))
166 static int rt5025_set_voltage(struct regulator_dev *rdev,
167 int min_uV, int max_uV, unsigned *selector)
170 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
172 if (check_range(info, min_uV, max_uV)) {
173 dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
177 data = rt5025_find_voltage(rdev,min_uV,max_uV);
178 data <<= info->vol_shift;
179 return rt5025_assign_bits(info->i2c, info->vol_reg, info->vol_mask, data);
183 static int rt5025_get_voltage(struct regulator_dev *rdev)
186 ret = rt5025_get_voltage_sel(rdev);
189 return rt5025_list_voltage(rdev, ret );
192 //#endif /* LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38) */
194 static int rt5025_enable(struct regulator_dev *rdev)
196 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
198 return rt5025_set_bits(info->i2c, info->enable_reg,
202 static int rt5025_disable(struct regulator_dev *rdev)
204 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
206 return rt5025_clr_bits(info->i2c, info->enable_reg,
210 static int rt5025_is_enabled(struct regulator_dev *rdev)
212 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
215 ret = rt5025_reg_read(info->i2c, info->enable_reg);
219 return (ret & (info->enable_bit))?1:0;
221 static int rt5025_dcdc_get_mode(struct regulator_dev *rdev)
223 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
224 int buck = rdev_get_id(rdev) - 0;
228 ret = rt5025_reg_read(info->i2c, 0x0c);
233 control =(ret & 0x80)>>7;
236 control =(ret & 0x40)>>6;
239 control =(ret & 0x20)>>5;
246 return REGULATOR_MODE_FAST;
248 return REGULATOR_MODE_NORMAL;
254 static int rt5025_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
256 struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
257 int buck = rdev_get_id(rdev) - 0;
261 ret = rt5025_reg_read(info->i2c, 0x0c);
263 control =ret &(~ (1 <<7));
266 control =ret &(~ (1 <<6));
269 control =ret &(~ (1 <<5));
277 case REGULATOR_MODE_FAST:
278 return rt5025_reg_write(info->i2c, 0x0c,control);
279 case REGULATOR_MODE_NORMAL:
280 return rt5025_reg_write(info->i2c, 0x0c,(control | (1 <<(7-buck))));
282 printk("error:pmu_rt5025 only powersave pwm & auto mode\n");
286 static int rt5025_dcdc_set_voltage_time_sel(struct regulator_dev *rdev, unsigned int old_selector,
287 unsigned int new_selector)
289 int old_volt, new_volt;
291 old_volt = rt5025_list_voltage(rdev, old_selector);
295 new_volt = rt5025_list_voltage(rdev, new_selector);
299 return DIV_ROUND_UP(abs(old_volt - new_volt), 25000);
302 static struct regulator_ops rt5025_regulator_ops = {
303 .list_voltage = rt5025_list_voltage,
304 //#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38))
305 // .get_voltage_sel = rt5025_get_voltage_sel,
306 // .set_voltage_sel = rt5025_set_voltage_sel,
308 .set_voltage = rt5025_set_voltage,
309 .get_voltage = rt5025_get_voltage,
310 //#endif /* LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38) */
311 .enable = rt5025_enable,
312 .disable = rt5025_disable,
313 .is_enabled = rt5025_is_enabled,
314 .get_mode = rt5025_dcdc_get_mode,
315 .set_mode = rt5025_dcdc_set_mode,
316 .set_voltage_time_sel = rt5025_dcdc_set_voltage_time_sel,
319 #define RT5025_DCDCVOUT_LIST1 rt5025_vol_output_list1
320 #define RT5025_DCDCVOUT_LIST2 rt5025_vol_output_list2
321 #define RT5025_DCDCVOUT_LIST3 rt5025_vol_output_list3
322 #define RT5025_DCDCVOUT_LIST4 rt5025_vol_output_list4
323 #define RT5025_LDOVOUT_LIST1 rt5025_vol_output_list2
324 #define RT5025_LDOVOUT_LIST2 rt5025_vol_output_list2
325 #define RT5025_LDOVOUT_LIST3 rt5025_vol_output_list5
326 #define RT5025_LDOVOUT_LIST4 rt5025_vol_output_list5
327 #define RT5025_LDOVOUT_LIST5 rt5025_vol_output_list5
328 #define RT5025_LDOVOUT_LIST6 rt5025_vol_output_list5
330 #define RT5025_DCDCVOUT_SIZE1 rt5025_vol_output_size1
331 #define RT5025_DCDCVOUT_SIZE2 rt5025_vol_output_size2
332 #define RT5025_DCDCVOUT_SIZE3 rt5025_vol_output_size3
333 #define RT5025_DCDCVOUT_SIZE4 rt5025_vol_output_size4
334 #define RT5025_LDOVOUT_SIZE1 rt5025_vol_output_size2
335 #define RT5025_LDOVOUT_SIZE2 rt5025_vol_output_size2
336 #define RT5025_LDOVOUT_SIZE3 rt5025_vol_output_size5
337 #define RT5025_LDOVOUT_SIZE4 rt5025_vol_output_size5
338 #define RT5025_LDOVOUT_SIZE5 rt5025_vol_output_size5
339 #define RT5025_LDOVOUT_SIZE6 rt5025_vol_output_size5
342 #define RT5025_DCDC(_id, min, max) \
345 .name = "rt5025-dcdc" #_id, \
346 .n_voltages = RT5025_DCDCVOUT_SIZE##_id, \
347 .ops = &rt5025_regulator_ops, \
348 .type = REGULATOR_VOLTAGE, \
349 .id = RT5025_ID_DCDC##_id, \
350 .owner = THIS_MODULE, \
352 .vol_output_list= RT5025_DCDCVOUT_LIST##_id, \
353 .vol_output_size= RT5025_DCDCVOUT_SIZE##_id, \
356 .vol_reg = RT5025_DCDCVOUT##_id, \
357 .vol_shift = RT5025_DCDCVOUT_SHIFT##_id, \
358 .vol_mask = RT5025_DCDCVOUT_MASK##_id, \
359 .enable_reg = RT5025_DCDC_OUTPUT_EN, \
360 .enable_bit = RT5025_DCDCEN_MASK##_id, \
363 #define RT5025_LDO(_id, min, max) \
366 .name = "rt5025-ldo" #_id, \
367 .n_voltages = RT5025_LDOVOUT_SIZE##_id, \
368 .ops = &rt5025_regulator_ops, \
369 .type = REGULATOR_VOLTAGE, \
370 .id = RT5025_ID_LDO##_id, \
371 .owner = THIS_MODULE, \
373 .vol_output_list= RT5025_LDOVOUT_LIST##_id, \
374 .vol_output_size= RT5025_LDOVOUT_SIZE##_id, \
377 .vol_reg = RT5025_LDOVOUT##_id, \
378 .vol_shift = RT5025_LDOVOUT_SHIFT##_id, \
379 .vol_mask = RT5025_LDOVOUT_MASK##_id, \
380 .enable_reg = RT5025_LDO_OUTPUT_EN, \
381 .enable_bit = RT5025_LDOEN_MASK##_id, \
384 static struct rt5025_regulator_info rt5025_regulator_info[] =
386 RT5025_DCDC(1, 700000, 2275000),
387 RT5025_DCDC(2, 700000, 3500000),
388 RT5025_DCDC(3, 700000, 3500000),
389 RT5025_DCDC(4, 4500000, 5500000),
390 RT5025_LDO( 1, 700000, 3500000),
391 RT5025_LDO( 2, 700000, 3500000),
392 RT5025_LDO( 3, 1000000, 3300000),
393 RT5025_LDO( 4, 1000000, 3300000),
394 RT5025_LDO( 5, 1000000, 3300000),
395 RT5025_LDO( 6, 1000000, 3300000),
398 static struct rt5025_regulator_info * __devinit find_regulator_info(int id)
400 struct rt5025_regulator_info *ri;
403 for (i = 0; i < ARRAY_SIZE(rt5025_regulator_info); i++) {
404 ri = &rt5025_regulator_info[i];
405 if (ri->desc.id == id)
411 inline struct regulator_dev* rt5025_regulator_register(struct regulator_desc *regulator_desc,
412 struct device *dev, struct regulator_init_data *init_data,
415 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(3,5,0))
416 struct regulator_config config = {
418 .init_data = init_data,
419 .driver_data = driver_data,
421 return regulator_register(®ulator_desc, &config);
423 return regulator_register(regulator_desc,dev,init_data,driver_data);
424 #endif /* LINUX_VERSION_CODE>=KERNEL_VERSION(3,5,0)) */
427 static int __devinit rt5025_regulator_probe(struct platform_device *pdev)
429 struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
430 struct rt5025_platform_data *pdata = chip->dev->platform_data;
431 struct rt5025_regulator_info *ri;
432 struct regulator_dev *rdev;
433 struct regulator_init_data* init_data;
435 ri = find_regulator_info(pdev->id);
437 dev_err(&pdev->dev, "invalid regulator ID specified\n");
440 init_data = pdata->regulator[pdev->id];
441 if (init_data == NULL) {
442 dev_err(&pdev->dev, "no initializing data\n");
448 rdev = rt5025_regulator_register(&ri->desc, &pdev->dev,
451 dev_err(&pdev->dev, "failed to register regulator %s\n",
453 return PTR_ERR(rdev);
456 platform_set_drvdata(pdev, rdev);
461 static int __devexit rt5025_regulator_remove(struct platform_device *pdev)
463 struct regulator_dev *rdev = platform_get_drvdata(pdev);
465 platform_set_drvdata(pdev, NULL);
466 regulator_unregister(rdev);
471 static struct platform_driver rt5025_regulator_driver =
474 .name = RT5025_DEVICE_NAME "-regulator",
475 .owner = THIS_MODULE,
477 .probe = rt5025_regulator_probe,
478 .remove = __devexit_p(rt5025_regulator_remove),
481 static int __init rt5025_regulator_init(void)
483 return platform_driver_register(&rt5025_regulator_driver);
485 subsys_initcall_sync(rt5025_regulator_init);
487 static void __exit rt5025_regulator_exit(void)
489 platform_driver_unregister(&rt5025_regulator_driver);
491 module_exit(rt5025_regulator_exit);
493 MODULE_LICENSE("GPL v2");
494 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
495 MODULE_DESCRIPTION("Regulator driver for RT5025");
496 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-regulator");