Merge tag 'lsk-android-14.02' into develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / ricoh619-regulator.c
1 /*
2  * drivers/regulator/ricoh619-regulator.c
3  *
4  * Regulator driver for RICOH619 power management chip.
5  *
6  * Copyright (C) 2012-2013 RICOH COMPANY,LTD
7  *
8  * Based on code
9  *      Copyright (C) 2011 NVIDIA Corporation
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25  
26 /*#define DEBUG                 1*/
27 /*#define VERBOSE_DEBUG         1*/
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/err.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/driver.h>
35 #include <linux/regulator/machine.h>
36 #include <linux/mfd/ricoh619.h>
37 #include <linux/regulator/ricoh619-regulator.h>
38
39 struct ricoh619_regulator {
40         int             id;
41         int             sleep_id;
42         /* Regulator register address.*/
43         u8              reg_en_reg;
44         u8              en_bit;
45         u8              reg_disc_reg;
46         u8              disc_bit;
47         u8              vout_reg;
48         u8              vout_mask;
49         u8              vout_reg_cache;
50         u8              sleep_reg;
51         u8              eco_reg;
52         u8              eco_bit;
53         u8              eco_slp_reg;
54         u8              eco_slp_bit;
55
56         /* chip constraints on regulator behavior */
57         int                     min_uV;
58         int                     max_uV;
59         int                     step_uV;
60         int                     nsteps;
61
62         /* regulator specific turn-on delay */
63         u16                     delay;
64
65         /* used by regulator core */
66         struct regulator_desc   desc;
67
68         /* Device */
69         struct device           *dev;
70 };
71
72 //static unsigned int ricoh619_suspend_status = 0;
73
74 static inline struct device *to_ricoh619_dev(struct regulator_dev *rdev)
75 {
76         return rdev_get_dev(rdev)->parent->parent;
77 }
78
79 static int ricoh619_regulator_enable_time(struct regulator_dev *rdev)
80 {
81         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
82
83         return ri->delay;
84 }
85
86 static int ricoh619_reg_is_enabled(struct regulator_dev *rdev)
87 {
88         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
89         struct device *parent = to_ricoh619_dev(rdev);
90         uint8_t control;
91         int ret;
92
93         ret = ricoh619_read(parent, ri->reg_en_reg, &control);
94         if (ret < 0) {
95                 dev_err(&rdev->dev, "Error in reading the control register\n");
96                 return ret;
97         }
98         return (((control >> ri->en_bit) & 1) == 1);
99 }
100
101 static int ricoh619_reg_enable(struct regulator_dev *rdev)
102 {
103         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
104         struct device *parent = to_ricoh619_dev(rdev);
105         int ret;
106         ret = ricoh619_set_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
107         if (ret < 0) {
108                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
109                 return ret;
110         }
111         udelay(ri->delay);
112         return ret;
113 }
114
115 static int ricoh619_reg_disable(struct regulator_dev *rdev)
116 {
117         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
118         struct device *parent = to_ricoh619_dev(rdev);
119         int ret;
120         ret = ricoh619_clr_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
121         if (ret < 0)
122                 dev_err(&rdev->dev, "Error in updating the STATE register\n");
123
124         return ret;
125 }
126
127 static int ricoh619_list_voltage(struct regulator_dev *rdev, unsigned index)
128 {
129         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
130
131         return ri->min_uV + (ri->step_uV * index);
132 }
133
134 static int __ricoh619_set_s_voltage(struct device *parent,
135                 struct ricoh619_regulator *ri, int min_uV, int max_uV)
136 {
137         int vsel;
138         int ret;
139
140         if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
141                 return -EDOM;
142
143         vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
144         if (vsel > ri->nsteps)
145                 return -EDOM;
146
147         ret = ricoh619_update(parent, ri->sleep_reg, vsel, ri->vout_mask);
148         if (ret < 0)
149                 dev_err(ri->dev, "Error in writing the sleep register\n");
150         return ret;
151 }
152
153 static int __ricoh619_set_voltage(struct device *parent,
154                 struct ricoh619_regulator *ri, int min_uV, int max_uV,
155                 unsigned *selector)
156 {
157         int vsel;
158         int ret;
159         uint8_t vout_val;
160
161         if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
162                 return -EDOM;
163
164         vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
165         if (vsel > ri->nsteps)
166                 return -EDOM;
167
168         if (selector)
169                 *selector = vsel;
170
171         vout_val = (ri->vout_reg_cache & ~ri->vout_mask) |
172                                 (vsel & ri->vout_mask);
173         ret = ricoh619_write(parent, ri->vout_reg, vout_val);
174         if (ret < 0)
175                 dev_err(ri->dev, "Error in writing the Voltage register\n");
176         else
177                 ri->vout_reg_cache = vout_val;
178
179         return ret;
180 }
181
182 static int ricoh619_set_voltage(struct regulator_dev *rdev,
183                 int min_uV, int max_uV, unsigned *selector)
184 {
185         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
186         struct device *parent = to_ricoh619_dev(rdev);
187
188 //      if(ricoh619_suspend_status)
189 //              return -EBUSY;
190
191         return __ricoh619_set_voltage(parent, ri, min_uV, max_uV, selector);
192 }
193
194 static int ricoh619_set_suspend_voltage(struct regulator_dev *rdev,
195                 int uV)
196 {
197         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
198         struct device *parent = to_ricoh619_dev(rdev);
199
200         return __ricoh619_set_s_voltage(parent, ri, uV, uV);
201 }
202
203 static int ricoh619_get_voltage(struct regulator_dev *rdev)
204 {
205         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
206         uint8_t vsel;
207
208         vsel = ri->vout_reg_cache & ri->vout_mask;
209         return ri->min_uV + vsel * ri->step_uV;
210 }
211
212  int ricoh619_regulator_enable_eco_mode(struct regulator_dev *rdev)
213 {
214         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
215         struct device *parent = to_ricoh619_dev(rdev);
216         int ret;
217
218         ret = ricoh619_set_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
219         if (ret < 0)
220                 dev_err(&rdev->dev, "Error Enable LDO eco mode\n");
221
222         return ret;
223 }
224 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_mode);
225
226 int ricoh619_regulator_disable_eco_mode(struct regulator_dev *rdev)
227 {
228         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
229         struct device *parent = to_ricoh619_dev(rdev);
230         int ret;
231
232         ret = ricoh619_clr_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
233         if (ret < 0)
234                 dev_err(&rdev->dev, "Error Disable LDO eco mode\n");
235
236         return ret;
237 }
238 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_mode);
239
240 int ricoh619_regulator_enable_eco_slp_mode(struct regulator_dev *rdev)
241 {
242         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
243         struct device *parent = to_ricoh619_dev(rdev);
244         int ret;
245
246         ret = ricoh619_set_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
247         if (ret < 0)
248                 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
249
250         return ret;
251 }
252 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_slp_mode);
253
254 int ricoh619_regulator_disable_eco_slp_mode(struct regulator_dev *rdev)
255 {
256         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
257         struct device *parent = to_ricoh619_dev(rdev);
258         int ret;
259
260         ret = ricoh619_clr_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
261         if (ret < 0)
262                 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
263
264         return ret;
265 }
266 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_slp_mode);
267
268 static unsigned int ricoh619_dcdc_get_mode(struct regulator_dev *rdev)
269 {
270         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
271         struct device *parent = to_ricoh619_dev(rdev);
272         int ret;
273         uint8_t control;
274         u8 mask = 0x30;
275         
276         ret = ricoh619_read(parent, ri->reg_en_reg,&control);
277         if (ret < 0) {
278                 return ret;
279         }
280         control=(control & mask) >> 4;
281         switch (control) {
282         case 1:
283                 return REGULATOR_MODE_FAST;
284         case 0:
285                 return REGULATOR_MODE_NORMAL;
286         case 2:
287                 return REGULATOR_MODE_STANDBY;
288         case 3:
289                 return REGULATOR_MODE_NORMAL;
290         default:
291                 return -1;
292         }
293
294 }
295 static int ricoh619_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
296 {
297         struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
298         struct device *parent = to_ricoh619_dev(rdev);
299         int ret;
300         uint8_t control;
301         
302         ret = ricoh619_read(parent, ri->reg_en_reg,&control);
303         switch(mode)
304         {
305         case REGULATOR_MODE_FAST:
306                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x10));
307         case REGULATOR_MODE_NORMAL:
308                 return ricoh619_write(parent, ri->reg_en_reg, (control & 0xcf));
309         case REGULATOR_MODE_STANDBY:
310                 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x20));       
311         default:
312                 printk("error:pmu_619 only powersave pwm psm mode\n");
313                 return -EINVAL;
314         }
315         
316
317 }
318
319 static int ricoh619_dcdc_set_voltage_time_sel(struct regulator_dev *rdev,   unsigned int old_selector,
320                                      unsigned int new_selector)
321 {
322         int old_volt, new_volt;
323         
324         old_volt = ricoh619_list_voltage(rdev, old_selector);
325         if (old_volt < 0)
326                 return old_volt;
327         
328         new_volt = ricoh619_list_voltage(rdev, new_selector);
329         if (new_volt < 0)
330                 return new_volt;
331
332         return DIV_ROUND_UP(abs(old_volt - new_volt)*2, 14000);
333 }
334
335
336 static struct regulator_ops ricoh619_ops = {
337         .list_voltage                   = ricoh619_list_voltage,
338         .set_voltage                    = ricoh619_set_voltage,
339         .get_voltage                    = ricoh619_get_voltage,
340         .set_suspend_voltage = ricoh619_set_suspend_voltage,
341         .set_voltage_time_sel = ricoh619_dcdc_set_voltage_time_sel,
342         .get_mode = ricoh619_dcdc_get_mode,
343         .set_mode = ricoh619_dcdc_set_mode,
344         .enable                         = ricoh619_reg_enable,
345         .disable                                = ricoh619_reg_disable,
346         .is_enabled                     = ricoh619_reg_is_enabled,
347 };
348
349 #define RICOH619_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \
350                 _vout_mask, _ds_reg, _min_uv, _max_uv, _step_uV, _nsteps,    \
351                 _ops, _delay, _eco_reg, _eco_bit, _eco_slp_reg, _eco_slp_bit)           \
352 {                                                               \
353         .reg_en_reg     = _en_reg,                              \
354         .en_bit         = _en_bit,                              \
355         .reg_disc_reg   = _disc_reg,                            \
356         .disc_bit       = _disc_bit,                            \
357         .vout_reg       = _vout_reg,                            \
358         .vout_mask      = _vout_mask,                           \
359         .sleep_reg      = _ds_reg,                              \
360         .min_uV         = _min_uv,                      \
361         .max_uV         = _max_uv ,                     \
362         .step_uV        = _step_uV,                             \
363         .nsteps         = _nsteps,                              \
364         .delay          = _delay,                               \
365         .id             = RICOH619_ID_##_id,                    \
366         .sleep_id       = RICOH619_DS_##_id,                    \
367         .eco_reg                        =  _eco_reg,                            \
368         .eco_bit                        =  _eco_bit,                            \
369         .eco_slp_reg            =  _eco_slp_reg,                                \
370         .eco_slp_bit            =  _eco_slp_bit,                                \
371         .desc = {                                               \
372                 .name = ricoh619_rails(_id),                    \
373                 .id = RICOH619_ID_##_id,                        \
374                 .n_voltages = _nsteps,                          \
375                 .ops = &_ops,                                   \
376                 .type = REGULATOR_VOLTAGE,                      \
377                 .owner = THIS_MODULE,                           \
378         },                                                      \
379 }
380
381 static struct ricoh619_regulator ricoh619_regulator[] = {
382         RICOH619_REG(DC1, 0x2C, 0, 0x2C, 1, 0x36, 0xFF, 0x3B,
383                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
384                         0x00, 0, 0x00, 0),
385
386         RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
387                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
388                         0x00, 0, 0x00, 0),
389
390         RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
391                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
392                         0x00, 0, 0x00, 0),
393
394         RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
395                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
396                         0x00, 0, 0x00, 0),
397
398         RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
399                         600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
400                         0x00, 0, 0x00, 0),
401                         
402         RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
403                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
404                         0x48, 0, 0x4A, 0),
405
406         RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
407                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
408                         0x48, 1, 0x4A, 1),
409
410         RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
411                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
412                         0x48, 2, 0x4A, 2),
413
414         RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
415                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
416                         0x48, 3, 0x4A, 3),
417
418         RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
419                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
420                         0x48, 4, 0x4A, 4),
421
422         RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
423                         600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
424                         0x48, 5, 0x4A, 5),
425
426         RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
427                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
428                         0x00, 0, 0x00, 0),
429
430         RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
431                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
432                         0x00, 0, 0x00, 0),
433
434         RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
435                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
436                         0x00, 0, 0x00, 0),
437
438         RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
439                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
440                         0x00, 0, 0x00, 0),
441
442         RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
443                         1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
444                         0x00, 0, 0x00, 0),
445
446         RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
447                         900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
448                         0x00, 0, 0x00, 0),
449 };
450 static inline struct ricoh619_regulator *find_regulator_info(int id)
451 {
452         struct ricoh619_regulator *ri;
453         int i;
454
455         for (i = 0; i < ARRAY_SIZE(ricoh619_regulator); i++) {
456                 ri = &ricoh619_regulator[i];
457                 if (ri->desc.id == id)
458                         return ri;
459         }
460         return NULL;
461 }
462
463 static int ricoh619_regulator_preinit(struct device *parent,
464                 struct ricoh619_regulator *ri,
465                 struct ricoh619_regulator_platform_data *ricoh619_pdata)
466 {
467         int ret = 0;
468
469         if (!ricoh619_pdata->init_apply)
470                 return 0;
471 /*
472         if (ricoh619_pdata->init_uV >= 0) {
473                 ret = __ricoh619_set_voltage(parent, ri,
474                                 ricoh619_pdata->init_uV,
475                                 ricoh619_pdata->init_uV, 0);
476                 if (ret < 0) {
477                         dev_err(ri->dev, "Not able to initialize voltage %d "
478                                 "for rail %d err %d\n", ricoh619_pdata->init_uV,
479                                 ri->desc.id, ret);
480                         return ret;
481                 }
482         }
483 */
484         if (ricoh619_pdata->init_enable)
485                 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
486                                                         (1 << ri->en_bit));
487         else
488                 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
489                                                         (1 << ri->en_bit));
490         if (ret < 0)
491                 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
492                         (ricoh619_pdata->init_enable) ? "enable" : "disable",
493                         ri->desc.id, ret);
494
495         return ret;
496 }
497
498 static inline int ricoh619_cache_regulator_register(struct device *parent,
499         struct ricoh619_regulator *ri)
500 {
501         ri->vout_reg_cache = 0;
502         return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
503 }
504
505 static int __devinit ricoh619_regulator_probe(struct platform_device *pdev)
506 {
507         struct ricoh619_regulator *ri = NULL;
508         struct regulator_dev *rdev;
509         struct ricoh619_regulator_platform_data *tps_pdata;
510         int id = pdev->id;
511         int err;
512
513         ri = find_regulator_info(id);
514         if (ri == NULL) {
515                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
516                 return -EINVAL;
517         }
518         tps_pdata = pdev->dev.platform_data;
519         ri->dev = &pdev->dev;
520 /*
521         err = ricoh619_cache_regulator_register(pdev->dev.parent, ri);
522         if (err) {
523                 dev_err(&pdev->dev, "Fail in caching register\n");
524                 return err;
525         }
526
527         err = ricoh619_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
528         if (err) {
529                 dev_err(&pdev->dev, "Fail in pre-initialisation\n");
530                 return err;
531         }
532         */
533         rdev = regulator_register(&ri->desc, &pdev->dev,
534                                 &tps_pdata->regulator, ri);
535         if (IS_ERR_OR_NULL(rdev)) {
536                 dev_err(&pdev->dev, "failed to register regulator %s\n",
537                                 ri->desc.name);
538                 return PTR_ERR(rdev);
539         }
540
541         platform_set_drvdata(pdev, rdev);
542         return 0;
543 }
544
545 static int __devexit ricoh619_regulator_remove(struct platform_device *pdev)
546 {
547         struct regulator_dev *rdev = platform_get_drvdata(pdev);
548
549         regulator_unregister(rdev);
550         return 0;
551 }
552
553 static struct platform_driver ricoh619_regulator_driver = {
554         .driver = {
555                 .name   = "ricoh619-regulator",
556                 .owner  = THIS_MODULE,
557         },
558         .probe          = ricoh619_regulator_probe,
559         .remove         = __devexit_p(ricoh619_regulator_remove),
560 };
561
562 static int __init ricoh619_regulator_init(void)
563 {
564
565         return platform_driver_register(&ricoh619_regulator_driver);
566 }
567 subsys_initcall_sync(ricoh619_regulator_init);
568
569 static void __exit ricoh619_regulator_exit(void)
570 {
571         platform_driver_unregister(&ricoh619_regulator_driver);
572 }
573 module_exit(ricoh619_regulator_exit);
574
575 MODULE_DESCRIPTION("RICOH619 regulator driver");
576 MODULE_ALIAS("platform:ricoh619-regulator");
577 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
578 MODULE_LICENSE("GPL");