2 * drivers/regulator/ricoh619-regulator.c
4 * Regulator driver for RICOH619 power management chip.
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
9 * Copyright (C) 2011 NVIDIA Corporation
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.
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
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/>.
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>
39 struct ricoh619_regulator {
42 /* Regulator register address.*/
56 /* chip constraints on regulator behavior */
62 /* regulator specific turn-on delay */
65 /* used by regulator core */
66 struct regulator_desc desc;
72 //static unsigned int ricoh619_suspend_status = 0;
74 static inline struct device *to_ricoh619_dev(struct regulator_dev *rdev)
76 return rdev_get_dev(rdev)->parent->parent;
79 static int ricoh619_regulator_enable_time(struct regulator_dev *rdev)
81 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
86 static int ricoh619_reg_is_enabled(struct regulator_dev *rdev)
88 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
89 struct device *parent = to_ricoh619_dev(rdev);
93 ret = ricoh619_read(parent, ri->reg_en_reg, &control);
95 dev_err(&rdev->dev, "Error in reading the control register\n");
98 return (((control >> ri->en_bit) & 1) == 1);
101 static int ricoh619_reg_enable(struct regulator_dev *rdev)
103 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
104 struct device *parent = to_ricoh619_dev(rdev);
106 ret = ricoh619_set_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
108 dev_err(&rdev->dev, "Error in updating the STATE register\n");
115 static int ricoh619_reg_disable(struct regulator_dev *rdev)
117 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
118 struct device *parent = to_ricoh619_dev(rdev);
120 ret = ricoh619_clr_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
122 dev_err(&rdev->dev, "Error in updating the STATE register\n");
127 static int ricoh619_list_voltage(struct regulator_dev *rdev, unsigned index)
129 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
131 return ri->min_uV + (ri->step_uV * index);
134 static int __ricoh619_set_s_voltage(struct device *parent,
135 struct ricoh619_regulator *ri, int min_uV, int max_uV)
140 if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
143 vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
144 if (vsel > ri->nsteps)
147 ret = ricoh619_update(parent, ri->sleep_reg, vsel, ri->vout_mask);
149 dev_err(ri->dev, "Error in writing the sleep register\n");
153 static int __ricoh619_set_voltage(struct device *parent,
154 struct ricoh619_regulator *ri, int min_uV, int max_uV,
161 if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
164 vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
165 if (vsel > ri->nsteps)
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);
175 dev_err(ri->dev, "Error in writing the Voltage register\n");
177 ri->vout_reg_cache = vout_val;
182 static int ricoh619_set_voltage(struct regulator_dev *rdev,
183 int min_uV, int max_uV, unsigned *selector)
185 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
186 struct device *parent = to_ricoh619_dev(rdev);
188 // if(ricoh619_suspend_status)
191 return __ricoh619_set_voltage(parent, ri, min_uV, max_uV, selector);
194 static int ricoh619_set_suspend_voltage(struct regulator_dev *rdev,
197 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
198 struct device *parent = to_ricoh619_dev(rdev);
200 return __ricoh619_set_s_voltage(parent, ri, uV, uV);
203 static int ricoh619_get_voltage(struct regulator_dev *rdev)
205 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
208 vsel = ri->vout_reg_cache & ri->vout_mask;
209 return ri->min_uV + vsel * ri->step_uV;
212 int ricoh619_regulator_enable_eco_mode(struct regulator_dev *rdev)
214 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
215 struct device *parent = to_ricoh619_dev(rdev);
218 ret = ricoh619_set_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
220 dev_err(&rdev->dev, "Error Enable LDO eco mode\n");
224 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_mode);
226 int ricoh619_regulator_disable_eco_mode(struct regulator_dev *rdev)
228 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
229 struct device *parent = to_ricoh619_dev(rdev);
232 ret = ricoh619_clr_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
234 dev_err(&rdev->dev, "Error Disable LDO eco mode\n");
238 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_mode);
240 int ricoh619_regulator_enable_eco_slp_mode(struct regulator_dev *rdev)
242 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
243 struct device *parent = to_ricoh619_dev(rdev);
246 ret = ricoh619_set_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
248 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
252 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_slp_mode);
254 int ricoh619_regulator_disable_eco_slp_mode(struct regulator_dev *rdev)
256 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
257 struct device *parent = to_ricoh619_dev(rdev);
260 ret = ricoh619_clr_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
262 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
266 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_slp_mode);
268 static unsigned int ricoh619_dcdc_get_mode(struct regulator_dev *rdev)
270 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
271 struct device *parent = to_ricoh619_dev(rdev);
276 ret = ricoh619_read(parent, ri->reg_en_reg,&control);
280 control=(control & mask) >> 4;
283 return REGULATOR_MODE_FAST;
285 return REGULATOR_MODE_NORMAL;
287 return REGULATOR_MODE_STANDBY;
289 return REGULATOR_MODE_NORMAL;
295 static int ricoh619_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
297 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
298 struct device *parent = to_ricoh619_dev(rdev);
302 ret = ricoh619_read(parent, ri->reg_en_reg,&control);
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));
312 printk("error:pmu_619 only powersave pwm psm mode\n");
319 static int ricoh619_dcdc_set_voltage_time_sel(struct regulator_dev *rdev, unsigned int old_selector,
320 unsigned int new_selector)
322 int old_volt, new_volt;
324 old_volt = ricoh619_list_voltage(rdev, old_selector);
328 new_volt = ricoh619_list_voltage(rdev, new_selector);
332 return DIV_ROUND_UP(abs(old_volt - new_volt)*2, 14000);
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,
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) \
353 .reg_en_reg = _en_reg, \
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, \
361 .max_uV = _max_uv , \
362 .step_uV = _step_uV, \
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, \
372 .name = ricoh619_rails(_id), \
373 .id = RICOH619_ID_##_id, \
374 .n_voltages = _nsteps, \
376 .type = REGULATOR_VOLTAGE, \
377 .owner = THIS_MODULE, \
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,
386 RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
387 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
390 RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
391 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
394 RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
395 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
398 RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
399 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
402 RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
403 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
406 RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
407 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
410 RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
411 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
414 RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
415 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
418 RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
419 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
422 RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
423 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
426 RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
427 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
430 RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
431 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
434 RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
435 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
438 RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
439 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
442 RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
443 1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
446 RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
447 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
450 static inline struct ricoh619_regulator *find_regulator_info(int id)
452 struct ricoh619_regulator *ri;
455 for (i = 0; i < ARRAY_SIZE(ricoh619_regulator); i++) {
456 ri = &ricoh619_regulator[i];
457 if (ri->desc.id == id)
463 static int ricoh619_regulator_preinit(struct device *parent,
464 struct ricoh619_regulator *ri,
465 struct ricoh619_regulator_platform_data *ricoh619_pdata)
469 if (!ricoh619_pdata->init_apply)
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);
477 dev_err(ri->dev, "Not able to initialize voltage %d "
478 "for rail %d err %d\n", ricoh619_pdata->init_uV,
484 if (ricoh619_pdata->init_enable)
485 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
488 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
491 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
492 (ricoh619_pdata->init_enable) ? "enable" : "disable",
498 static inline int ricoh619_cache_regulator_register(struct device *parent,
499 struct ricoh619_regulator *ri)
501 ri->vout_reg_cache = 0;
502 return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
505 static int __devinit ricoh619_regulator_probe(struct platform_device *pdev)
507 struct ricoh619_regulator *ri = NULL;
508 struct regulator_dev *rdev;
509 struct ricoh619_regulator_platform_data *tps_pdata;
513 ri = find_regulator_info(id);
515 dev_err(&pdev->dev, "invalid regulator ID specified\n");
518 tps_pdata = pdev->dev.platform_data;
519 ri->dev = &pdev->dev;
521 err = ricoh619_cache_regulator_register(pdev->dev.parent, ri);
523 dev_err(&pdev->dev, "Fail in caching register\n");
527 err = ricoh619_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
529 dev_err(&pdev->dev, "Fail in pre-initialisation\n");
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",
538 return PTR_ERR(rdev);
541 platform_set_drvdata(pdev, rdev);
545 static int __devexit ricoh619_regulator_remove(struct platform_device *pdev)
547 struct regulator_dev *rdev = platform_get_drvdata(pdev);
549 regulator_unregister(rdev);
553 static struct platform_driver ricoh619_regulator_driver = {
555 .name = "ricoh619-regulator",
556 .owner = THIS_MODULE,
558 .probe = ricoh619_regulator_probe,
559 .remove = __devexit_p(ricoh619_regulator_remove),
562 static int __init ricoh619_regulator_init(void)
565 return platform_driver_register(&ricoh619_regulator_driver);
567 subsys_initcall_sync(ricoh619_regulator_init);
569 static void __exit ricoh619_regulator_exit(void)
571 platform_driver_unregister(&ricoh619_regulator_driver);
573 module_exit(ricoh619_regulator_exit);
575 MODULE_DESCRIPTION("RICOH619 regulator driver");
576 MODULE_ALIAS("platform:ricoh619-regulator");
577 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
578 MODULE_LICENSE("GPL");