rk31xx:RT5025:support pmic rt5025
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / rt5025-regulator.c
1 /*
2  *  drivers/regulator/rt5025-regulator.c
3  *  Driver foo Richtek RT5025 PMIC Regulator
4  *
5  *  Copyright (C) 2013 Richtek Electronics
6  *  cy_huang <cy_huang@richtek.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 version 2 as
10  * published by the Free Software Foundation.
11  */
12
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>
23
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;
31         int     min_uV;
32         int     max_uV;
33         int     vol_reg;
34         int     vol_shift;
35         int     vol_mask;
36         int     enable_bit;
37         int     enable_reg;
38 };
39
40 //for DCDC1
41 static const unsigned int rt5025_vol_output_list1[] =
42 {
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,
51 };
52 #define rt5025_vol_output_size1 ARRAY_SIZE(rt5025_vol_output_list1)
53
54 //DCDC2, LDO1, LDO2
55 static const unsigned int rt5025_vol_output_list2[] =
56 {
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,
73 };
74 #define rt5025_vol_output_size2 ARRAY_SIZE(rt5025_vol_output_list2)
75
76 //DCDC3
77 static const unsigned int rt5025_vol_output_list3[] =
78 {
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,
87 };
88 #define rt5025_vol_output_size3 ARRAY_SIZE(rt5025_vol_output_list3)
89
90 //DCDC4
91 static const unsigned int rt5025_vol_output_list4[] =
92 {
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,
95 };
96 #define rt5025_vol_output_size4 ARRAY_SIZE(rt5025_vol_output_list4)
97
98 //LDO3, LDO4, LDO5, LDO6
99 static const unsigned int rt5025_vol_output_list5[] = 
100 {
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,
106 };
107 #define rt5025_vol_output_size5 ARRAY_SIZE(rt5025_vol_output_list5)
108
109 static inline int check_range(struct rt5025_regulator_info *info,
110                               int min_uV, int max_uV)
111 {
112         if (min_uV < info->min_uV || min_uV > info->max_uV)
113                 return -EINVAL;
114
115         return 0;
116 }
117
118 static int rt5025_list_voltage(struct regulator_dev *rdev, unsigned index)
119 {
120         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
121
122         return (index>=info->vol_output_size)? \
123                  -EINVAL: \
124                 info->vol_output_list[index ];
125 }
126
127 //#if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38))
128 static int rt5025_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
129 {
130         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
131         unsigned char data;
132         const int count = info->vol_output_size;
133
134         if (selector>count)
135                 return -EINVAL;
136         data = (unsigned char)selector;
137         data <<= info->vol_shift;
138         return rt5025_assign_bits(info->i2c, info->vol_reg, info->vol_mask, data);
139 }
140
141 static int rt5025_get_voltage_sel(struct regulator_dev *rdev)
142 {
143         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
144         int ret;
145         ret = rt5025_reg_read(info->i2c, info->vol_reg);
146         if (ret < 0)
147                 return ret;
148         return (ret & info->vol_mask)  >> info->vol_shift;
149 }
150 //#else
151 static int rt5025_find_voltage(struct regulator_dev *rdev,
152                                int min_uV, int max_uV)
153 {
154         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
155         int i=0;
156         const int count = info->vol_output_size;
157         for (i=0;i<count;i++)
158         {
159                 if ((info->vol_output_list[i]>=min_uV)
160                         && (info->vol_output_list[i]<=max_uV))
161                         return i;
162         }
163         return -EINVAL;
164 }
165
166 static int rt5025_set_voltage(struct regulator_dev *rdev,
167                                int min_uV, int max_uV, unsigned *selector)
168         
169 {
170         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
171         unsigned char data;
172         if (check_range(info, min_uV, max_uV)) {
173                 dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
174                         min_uV, max_uV);
175                 return -EINVAL;
176         }
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);
180 }
181
182
183 static int rt5025_get_voltage(struct regulator_dev *rdev)
184 {
185         int ret;
186         ret = rt5025_get_voltage_sel(rdev);
187         if (ret < 0)
188                 return ret;
189         return rt5025_list_voltage(rdev, ret );
190
191 }
192 //#endif /* LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,38) */
193
194 static int rt5025_enable(struct regulator_dev *rdev)
195 {
196         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
197
198         return rt5025_set_bits(info->i2c, info->enable_reg,
199                                 info->enable_bit);
200 }
201
202 static int rt5025_disable(struct regulator_dev *rdev)
203 {
204         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
205
206         return rt5025_clr_bits(info->i2c, info->enable_reg,
207                                 info->enable_bit);
208 }
209
210 static int rt5025_is_enabled(struct regulator_dev *rdev)
211 {
212         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
213         int ret;
214
215         ret = rt5025_reg_read(info->i2c, info->enable_reg);
216         if (ret < 0)
217                 return ret;
218
219         return (ret & (info->enable_bit))?1:0;
220 }
221 static int rt5025_dcdc_get_mode(struct regulator_dev *rdev)
222 {
223         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
224         int buck = rdev_get_id(rdev) - 0;
225         int ret;
226         uint8_t control;
227         
228         ret = rt5025_reg_read(info->i2c, 0x0c);
229         if (ret < 0) {
230                 return ret;
231         }
232         if (buck ==0){
233         control =(ret & 0x80)>>7;
234         }
235         else if (buck ==1){
236         control =(ret & 0x40)>>6;
237         }
238         else if (buck ==2){
239         control =(ret & 0x20)>>5;
240         }
241         else{
242         return -1;
243         }
244         switch (control) {
245         case 0:
246                 return REGULATOR_MODE_FAST;
247         case 1:
248                 return REGULATOR_MODE_NORMAL;
249         default:
250                 return -1;
251         }
252         
253 }
254 static int rt5025_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
255 {
256         struct rt5025_regulator_info *info = rdev_get_drvdata(rdev);
257         int buck = rdev_get_id(rdev) - 0;
258         int ret;
259         uint8_t control;
260         
261         ret = rt5025_reg_read(info->i2c, 0x0c);
262         if (buck ==0){
263         control =ret &(~ (1 <<7));
264         }
265         else if (buck ==1){
266         control =ret &(~ (1 <<6));
267         }
268         else if (buck ==2){
269         control =ret &(~ (1 <<5));
270         }
271         else{
272         return -1;
273         }
274         
275         switch(mode)
276         {
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))));
281         default:
282                 printk("error:pmu_rt5025 only powersave pwm & auto mode\n");
283                 return -EINVAL;
284         }
285 }
286 static int rt5025_dcdc_set_voltage_time_sel(struct regulator_dev *rdev,   unsigned int old_selector,
287                                      unsigned int new_selector)
288 {
289         int old_volt, new_volt;
290         
291         old_volt = rt5025_list_voltage(rdev, old_selector);
292         if (old_volt < 0)
293                 return old_volt;
294         
295         new_volt = rt5025_list_voltage(rdev, new_selector);
296         if (new_volt < 0)
297                 return new_volt;
298
299         return DIV_ROUND_UP(abs(old_volt - new_volt), 25000);
300 }
301
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,
307 //#else
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,
317 };
318
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
329
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
340
341
342 #define RT5025_DCDC(_id, min, max)                              \
343 {                                                               \
344         .desc   = {                                             \
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,                          \
351         },                                                      \
352         .vol_output_list= RT5025_DCDCVOUT_LIST##_id,            \
353         .vol_output_size= RT5025_DCDCVOUT_SIZE##_id,            \
354         .min_uV         = min ,                         \
355         .max_uV         = max ,                         \
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,              \
361 }
362
363 #define RT5025_LDO(_id, min, max)                               \
364 {                                                               \
365         .desc   = {                                             \
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,                          \
372         },                                                      \
373         .vol_output_list= RT5025_LDOVOUT_LIST##_id,             \
374         .vol_output_size= RT5025_LDOVOUT_SIZE##_id,             \
375         .min_uV         = min ,                         \
376         .max_uV         = max,                          \
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,               \
382 }
383
384 static struct rt5025_regulator_info rt5025_regulator_info[] = 
385 {
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),
396 };
397
398 static struct rt5025_regulator_info * __devinit find_regulator_info(int id)
399 {
400         struct rt5025_regulator_info *ri;
401         int i;
402
403         for (i = 0; i < ARRAY_SIZE(rt5025_regulator_info); i++) {
404                 ri = &rt5025_regulator_info[i];
405                 if (ri->desc.id == id)
406                         return ri;
407         }
408         return NULL;
409 }
410
411 inline struct regulator_dev* rt5025_regulator_register(struct regulator_desc *regulator_desc,
412             struct device *dev, struct regulator_init_data *init_data,
413             void *driver_data)
414 {
415 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(3,5,0))
416     struct regulator_config config = {
417         .dev = dev,
418         .init_data = init_data,
419         .driver_data = driver_data,
420     };
421     return regulator_register(&regulator_desc, &config);
422 #else
423     return regulator_register(regulator_desc,dev,init_data,driver_data);
424 #endif /* LINUX_VERSION_CODE>=KERNEL_VERSION(3,5,0)) */
425 }
426
427 static int __devinit rt5025_regulator_probe(struct platform_device *pdev)
428 {
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;
434
435         ri = find_regulator_info(pdev->id);
436         if (ri == NULL) {
437                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
438                 return -EINVAL;
439         }
440         init_data = pdata->regulator[pdev->id];
441         if (init_data == NULL) {
442                 dev_err(&pdev->dev, "no initializing data\n");
443                 return -EINVAL;
444         }
445         ri->i2c = chip->i2c;
446         ri->chip = chip;
447
448         rdev = rt5025_regulator_register(&ri->desc, &pdev->dev,
449                                   init_data, ri);
450         if (IS_ERR(rdev)) {
451                 dev_err(&pdev->dev, "failed to register regulator %s\n",
452                                 ri->desc.name);
453                 return PTR_ERR(rdev);
454         }
455
456         platform_set_drvdata(pdev, rdev);
457
458         return 0;
459 }
460
461 static int __devexit rt5025_regulator_remove(struct platform_device *pdev)
462 {
463         struct regulator_dev *rdev = platform_get_drvdata(pdev);
464
465         platform_set_drvdata(pdev, NULL);
466         regulator_unregister(rdev);
467
468         return 0;
469 }
470
471 static struct platform_driver rt5025_regulator_driver = 
472 {
473         .driver = {
474                 .name = RT5025_DEVICE_NAME "-regulator",
475                 .owner = THIS_MODULE,
476         },
477         .probe = rt5025_regulator_probe,
478         .remove = __devexit_p(rt5025_regulator_remove),
479 };
480
481 static int __init rt5025_regulator_init(void)
482 {
483         return platform_driver_register(&rt5025_regulator_driver);
484 }
485 subsys_initcall_sync(rt5025_regulator_init);
486
487 static void __exit rt5025_regulator_exit(void)
488 {
489         platform_driver_unregister(&rt5025_regulator_driver);
490 }
491 module_exit(rt5025_regulator_exit);
492
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");