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 #include <linux/regulator/of_regulator.h>
41 struct ricoh619_regulator {
44 /* Regulator register address.*/
58 /* chip constraints on regulator behavior */
64 /* regulator specific turn-on delay */
67 /* used by regulator core */
68 struct regulator_desc desc;
74 //static unsigned int ricoh619_suspend_status = 0;
76 static inline struct device *to_ricoh619_dev(struct regulator_dev *rdev)
78 return rdev_get_dev(rdev)->parent->parent;
81 static int ricoh619_regulator_enable_time(struct regulator_dev *rdev)
83 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
88 static int ricoh619_reg_is_enabled(struct regulator_dev *rdev)
90 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
91 struct device *parent = to_ricoh619_dev(rdev);
95 ret = ricoh619_read(parent, ri->reg_en_reg, &control);
97 dev_err(&rdev->dev, "Error in reading the control register\n");
100 return (((control >> ri->en_bit) & 1) == 1);
103 static int ricoh619_reg_enable(struct regulator_dev *rdev)
105 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
106 struct device *parent = to_ricoh619_dev(rdev);
108 ret = ricoh619_set_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
110 dev_err(&rdev->dev, "Error in updating the STATE register\n");
117 static int ricoh619_reg_disable(struct regulator_dev *rdev)
119 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
120 struct device *parent = to_ricoh619_dev(rdev);
122 ret = ricoh619_clr_bits(parent, ri->reg_en_reg, (1 << ri->en_bit));
124 dev_err(&rdev->dev, "Error in updating the STATE register\n");
129 static int ricoh619_list_voltage(struct regulator_dev *rdev, unsigned index)
131 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
133 return ri->min_uV + (ri->step_uV * index);
136 static int __ricoh619_set_s_voltage(struct device *parent,
137 struct ricoh619_regulator *ri, int min_uV, int max_uV)
142 if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
145 vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
146 if (vsel > ri->nsteps)
149 ret = ricoh619_update(parent, ri->sleep_reg, vsel, ri->vout_mask);
151 dev_err(ri->dev, "Error in writing the sleep register\n");
155 static int __ricoh619_set_voltage(struct device *parent,
156 struct ricoh619_regulator *ri, int min_uV, int max_uV,
163 if ((min_uV < ri->min_uV) || (max_uV > ri->max_uV))
166 vsel = (min_uV - ri->min_uV + ri->step_uV - 1)/ri->step_uV;
167 if (vsel > ri->nsteps)
173 vout_val = (ri->vout_reg_cache & ~ri->vout_mask) |
174 (vsel & ri->vout_mask);
175 ret = ricoh619_write(parent, ri->vout_reg, vout_val);
177 dev_err(ri->dev, "Error in writing the Voltage register\n");
179 ri->vout_reg_cache = vout_val;
184 static int ricoh619_set_voltage(struct regulator_dev *rdev,
185 int min_uV, int max_uV, unsigned *selector)
187 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
188 struct device *parent = to_ricoh619_dev(rdev);
190 // if(ricoh619_suspend_status)
193 return __ricoh619_set_voltage(parent, ri, min_uV, max_uV, selector);
196 static int ricoh619_set_suspend_voltage(struct regulator_dev *rdev,
199 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
200 struct device *parent = to_ricoh619_dev(rdev);
202 return __ricoh619_set_s_voltage(parent, ri, uV, uV);
205 static int ricoh619_get_voltage(struct regulator_dev *rdev)
207 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
208 struct device *parent = to_ricoh619_dev(rdev);
212 ret = ricoh619_read(parent, ri->vout_reg, &vsel);
213 return ricoh619_list_voltage(rdev,vsel);
216 int ricoh619_regulator_enable_eco_mode(struct regulator_dev *rdev)
218 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
219 struct device *parent = to_ricoh619_dev(rdev);
222 ret = ricoh619_set_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
224 dev_err(&rdev->dev, "Error Enable LDO eco mode\n");
228 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_mode);
230 int ricoh619_regulator_disable_eco_mode(struct regulator_dev *rdev)
232 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
233 struct device *parent = to_ricoh619_dev(rdev);
236 ret = ricoh619_clr_bits(parent, ri->eco_reg, (1 << ri->eco_bit));
238 dev_err(&rdev->dev, "Error Disable LDO eco mode\n");
242 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_mode);
244 int ricoh619_regulator_enable_eco_slp_mode(struct regulator_dev *rdev)
246 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
247 struct device *parent = to_ricoh619_dev(rdev);
250 ret = ricoh619_set_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
252 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
256 EXPORT_SYMBOL_GPL(ricoh619_regulator_enable_eco_slp_mode);
258 int ricoh619_regulator_disable_eco_slp_mode(struct regulator_dev *rdev)
260 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
261 struct device *parent = to_ricoh619_dev(rdev);
264 ret = ricoh619_clr_bits(parent, ri->eco_slp_reg, (1 << ri->eco_slp_bit));
266 dev_err(&rdev->dev, "Error Enable LDO eco mode in d during sleep\n");
270 EXPORT_SYMBOL_GPL(ricoh619_regulator_disable_eco_slp_mode);
272 static unsigned int ricoh619_dcdc_get_mode(struct regulator_dev *rdev)
274 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
275 struct device *parent = to_ricoh619_dev(rdev);
280 ret = ricoh619_read(parent, ri->reg_en_reg,&control);
284 control=(control & mask) >> 4;
287 return REGULATOR_MODE_FAST;
289 return REGULATOR_MODE_NORMAL;
291 return REGULATOR_MODE_STANDBY;
293 return REGULATOR_MODE_NORMAL;
299 static int ricoh619_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
301 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
302 struct device *parent = to_ricoh619_dev(rdev);
306 ret = ricoh619_read(parent, ri->reg_en_reg,&control);
309 case REGULATOR_MODE_FAST:
310 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x10));
311 case REGULATOR_MODE_NORMAL:
312 return ricoh619_write(parent, ri->reg_en_reg, (control & 0xcf));
313 case REGULATOR_MODE_STANDBY:
314 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0xcf) | 0x20));
316 printk("error:pmu_619 only powersave pwm psm mode\n");
323 static int ricoh619_dcdc_set_voltage_time_sel(struct regulator_dev *rdev, unsigned int old_selector,
324 unsigned int new_selector)
326 int old_volt, new_volt;
328 old_volt = ricoh619_list_voltage(rdev, old_selector);
332 new_volt = ricoh619_list_voltage(rdev, new_selector);
336 return DIV_ROUND_UP(abs(old_volt - new_volt)*2, 14000);
338 static int ricoh619_dcdc_set_suspend_mode(struct regulator_dev *rdev, unsigned int mode)
340 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
341 struct device *parent = to_ricoh619_dev(rdev);
345 ret = ricoh619_read(parent, ri->reg_en_reg,&control);
348 case REGULATOR_MODE_FAST:
349 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0x3f) | 0x40));
350 case REGULATOR_MODE_NORMAL:
351 return ricoh619_write(parent, ri->reg_en_reg, (control & 0x3f));
352 case REGULATOR_MODE_STANDBY:
353 return ricoh619_write(parent, ri->reg_en_reg, ((control & 0x3f) | 0x80));
355 printk("error:pmu_619 only powersave pwm psm mode\n");
361 static int ricoh619_reg_suspend_enable(struct regulator_dev *rdev)
363 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
364 struct device *parent = to_ricoh619_dev(rdev);
366 ret = ricoh619_set_bits(parent, (0x16 + ri->id), (0xf << 0));
368 dev_err(&rdev->dev, "Error in updating the STATE register\n");
375 static int ricoh619_reg_suspend_disable(struct regulator_dev *rdev)
377 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
378 struct device *parent = to_ricoh619_dev(rdev);
380 ret = ricoh619_clr_bits(parent, (0x16 + ri->id), (0xf <<0));
382 dev_err(&rdev->dev, "Error in updating the STATE register\n");
387 static struct regulator_ops ricoh619_ops = {
388 .list_voltage = ricoh619_list_voltage,
389 .set_voltage = ricoh619_set_voltage,
390 .get_voltage = ricoh619_get_voltage,
391 .set_suspend_voltage = ricoh619_set_suspend_voltage,
392 .set_voltage_time_sel = ricoh619_dcdc_set_voltage_time_sel,
393 .get_mode = ricoh619_dcdc_get_mode,
394 .set_mode = ricoh619_dcdc_set_mode,
395 .enable = ricoh619_reg_enable,
396 .disable = ricoh619_reg_disable,
397 .set_suspend_mode = ricoh619_dcdc_set_suspend_mode,
398 .set_suspend_enable = ricoh619_reg_suspend_enable,
399 .set_suspend_disable = ricoh619_reg_suspend_disable,
400 .is_enabled = ricoh619_reg_is_enabled,
403 #define RICOH619_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \
404 _vout_mask, _ds_reg, _min_uv, _max_uv, _step_uV, _nsteps, \
405 _ops, _delay, _eco_reg, _eco_bit, _eco_slp_reg, _eco_slp_bit) \
407 .reg_en_reg = _en_reg, \
409 .reg_disc_reg = _disc_reg, \
410 .disc_bit = _disc_bit, \
411 .vout_reg = _vout_reg, \
412 .vout_mask = _vout_mask, \
413 .sleep_reg = _ds_reg, \
415 .max_uV = _max_uv , \
416 .step_uV = _step_uV, \
419 .id = RICOH619_ID_##_id, \
420 .sleep_id = RICOH619_DS_##_id, \
421 .eco_reg = _eco_reg, \
422 .eco_bit = _eco_bit, \
423 .eco_slp_reg = _eco_slp_reg, \
424 .eco_slp_bit = _eco_slp_bit, \
426 .name = ricoh619_rails(_id), \
427 .id = RICOH619_ID_##_id, \
428 .n_voltages = _nsteps, \
430 .type = REGULATOR_VOLTAGE, \
431 .owner = THIS_MODULE, \
435 static struct ricoh619_regulator ricoh619_regulator_data[] = {
436 RICOH619_REG(DC1, 0x2C, 0, 0x2C, 1, 0x36, 0xFF, 0x3B,
437 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
440 RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
441 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
444 RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
445 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
448 RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
449 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
452 RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
453 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
456 RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
457 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
460 RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
461 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
464 RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
465 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
468 RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
469 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
472 RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
473 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
476 RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
477 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
480 RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
481 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
484 RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
485 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
488 RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
489 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
492 RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
493 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
496 RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
497 1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
500 RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
501 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
505 static inline struct ricoh619_regulator *find_regulator_info(int id)
507 struct ricoh619_regulator *ri;
510 for (i = 0; i < ARRAY_SIZE(ricoh619_regulator_data); i++) {
511 ri = &ricoh619_regulator_data[i];
512 if (ri->desc.id == id)
518 static int ricoh619_regulator_preinit(struct device *parent,
519 struct ricoh619_regulator *ri,
520 struct ricoh619_regulator_platform_data *ricoh619_pdata)
524 if (!ricoh619_pdata->init_apply)
527 if (ricoh619_pdata->init_uV >= 0) {
528 ret = __ricoh619_set_voltage(parent, ri,
529 ricoh619_pdata->init_uV,
530 ricoh619_pdata->init_uV, 0);
532 dev_err(ri->dev, "Not able to initialize voltage %d "
533 "for rail %d err %d\n", ricoh619_pdata->init_uV,
539 if (ricoh619_pdata->init_enable)
540 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
543 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
546 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
547 (ricoh619_pdata->init_enable) ? "enable" : "disable",
553 static inline int ricoh619_cache_regulator_register(struct device *parent,
554 struct ricoh619_regulator *ri)
556 ri->vout_reg_cache = 0;
557 return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
561 static struct of_regulator_match ricoh619_regulator_matches[] = {
562 { .name = "ricoh619_dc1",},
563 { .name = "ricoh619_dc2",},
564 { .name = "ricoh619_dc3",},
565 { .name = "ricoh619_dc4",},
566 { .name = "ricoh619_dc5",},
567 { .name = "ricoh619_ldo1",},
568 { .name = "ricoh619_ldo2",},
569 { .name = "ricoh619_ldo3",},
570 { .name = "ricoh619_ldo4",},
571 { .name = "ricoh619_ldo5",},
572 { .name = "ricoh619_ldo6",},
573 { .name = "ricoh619_ldo7",},
574 { .name = "ricoh619_ldo8",},
575 { .name = "ricoh619_ldo9",},
576 { .name = "ricoh619_ldo10",},
577 { .name = "ricoh619_ldortc1",},
578 { .name = "ricoh619_ldortc2",},
583 static int ricoh619_regulator_dt_init(struct platform_device *pdev,
584 struct regulator_config *config,
587 struct device_node *nproot, *np;
589 nproot = of_node_get(pdev->dev.parent->of_node);
592 np = of_find_node_by_name(nproot, "regulators");
594 dev_err(&pdev->dev, "failed to find regulators node\n");
598 rcount = of_regulator_match(&pdev->dev, np,
599 &ricoh619_regulator_matches[regidx], 1);
603 config->init_data = ricoh619_regulator_matches[regidx].init_data;
604 config->of_node = ricoh619_regulator_matches[regidx].of_node;
609 #define ricoh619_regulator_dt_init(x, y, z) (-1)
612 static int ricoh619_regulator_probe(struct platform_device *pdev)
614 struct ricoh619_regulator *ri = NULL;
615 struct regulator_dev *rdev;
616 struct regulator_config config = { };
619 rdev = devm_kzalloc(&pdev->dev, RICOH619_NUM_REGULATOR *
620 sizeof(*rdev), GFP_KERNEL);
622 dev_err(&pdev->dev, "Mmemory alloc failed\n");
626 for (id = 0; id < RICOH619_NUM_REGULATOR; ++id) {
628 ri = find_regulator_info(id);
630 dev_err(&pdev->dev, "invalid regulator ID specified\n");
634 ri->dev = &pdev->dev;
635 config.dev = &pdev->dev;
636 config.driver_data = ri;
638 config.of_node = ricoh619_regulator_matches[id].of_node;
640 err = ricoh619_regulator_dt_init(pdev, &config, id);
642 dev_err(&pdev->dev, "failed to regulator dt init\n");
645 rdev = regulator_register(&ri->desc, &config);
646 if (IS_ERR_OR_NULL(rdev)) {
647 dev_err(&pdev->dev, "failed to register regulator %s\n",
649 return PTR_ERR(rdev);
653 platform_set_drvdata(pdev, rdev);
657 static int ricoh619_regulator_remove(struct platform_device *pdev)
659 struct regulator_dev *rdev = platform_get_drvdata(pdev);
661 regulator_unregister(rdev);
665 static struct platform_driver ricoh619_regulator_driver = {
667 .name = "ricoh619-regulator",
668 .owner = THIS_MODULE,
670 .probe = ricoh619_regulator_probe,
671 .remove = ricoh619_regulator_remove,
674 static int __init ricoh619_regulator_init(void)
677 return platform_driver_register(&ricoh619_regulator_driver);
679 subsys_initcall_sync(ricoh619_regulator_init);
681 static void __exit ricoh619_regulator_exit(void)
683 platform_driver_unregister(&ricoh619_regulator_driver);
685 module_exit(ricoh619_regulator_exit);
687 MODULE_DESCRIPTION("RICOH619 regulator driver");
688 MODULE_ALIAS("platform:ricoh619-regulator");
689 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
690 MODULE_LICENSE("GPL");