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);
367 ret = ricoh619_set_bits(parent, (0x16 + ri->id), (0xf << 0));
369 dev_err(&rdev->dev, "Error in updating the STATE register\n");
376 static int ricoh619_reg_suspend_disable(struct regulator_dev *rdev)
378 struct ricoh619_regulator *ri = rdev_get_drvdata(rdev);
379 struct device *parent = to_ricoh619_dev(rdev);
381 ret = ricoh619_clr_bits(parent, (0x16 + ri->id), (0xf <<0));
383 dev_err(&rdev->dev, "Error in updating the STATE register\n");
388 static struct regulator_ops ricoh619_ops = {
389 .list_voltage = ricoh619_list_voltage,
390 .set_voltage = ricoh619_set_voltage,
391 .get_voltage = ricoh619_get_voltage,
392 .set_suspend_voltage = ricoh619_set_suspend_voltage,
393 .set_voltage_time_sel = ricoh619_dcdc_set_voltage_time_sel,
394 .get_mode = ricoh619_dcdc_get_mode,
395 .set_mode = ricoh619_dcdc_set_mode,
396 .enable = ricoh619_reg_enable,
397 .disable = ricoh619_reg_disable,
398 .set_suspend_mode = ricoh619_dcdc_set_suspend_mode,
399 .set_suspend_enable = ricoh619_reg_suspend_enable,
400 .set_suspend_disable = ricoh619_reg_suspend_disable,
401 .is_enabled = ricoh619_reg_is_enabled,
404 #define RICOH619_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \
405 _vout_mask, _ds_reg, _min_uv, _max_uv, _step_uV, _nsteps, \
406 _ops, _delay, _eco_reg, _eco_bit, _eco_slp_reg, _eco_slp_bit) \
408 .reg_en_reg = _en_reg, \
410 .reg_disc_reg = _disc_reg, \
411 .disc_bit = _disc_bit, \
412 .vout_reg = _vout_reg, \
413 .vout_mask = _vout_mask, \
414 .sleep_reg = _ds_reg, \
416 .max_uV = _max_uv , \
417 .step_uV = _step_uV, \
420 .id = RICOH619_ID_##_id, \
421 .sleep_id = RICOH619_DS_##_id, \
422 .eco_reg = _eco_reg, \
423 .eco_bit = _eco_bit, \
424 .eco_slp_reg = _eco_slp_reg, \
425 .eco_slp_bit = _eco_slp_bit, \
427 .name = ricoh619_rails(_id), \
428 .id = RICOH619_ID_##_id, \
429 .n_voltages = _nsteps, \
431 .type = REGULATOR_VOLTAGE, \
432 .owner = THIS_MODULE, \
436 static struct ricoh619_regulator ricoh619_regulator_data[] = {
437 RICOH619_REG(DC1, 0x2C, 0, 0x2C, 1, 0x36, 0xFF, 0x3B,
438 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
441 RICOH619_REG(DC2, 0x2E, 0, 0x2E, 1, 0x37, 0xFF, 0x3C,
442 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
445 RICOH619_REG(DC3, 0x30, 0, 0x30, 1, 0x38, 0xFF, 0x3D,
446 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
449 RICOH619_REG(DC4, 0x32, 0, 0x32, 1, 0x39, 0xFF, 0x3E,
450 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
453 RICOH619_REG(DC5, 0x34, 0, 0x34, 1, 0x3A, 0xFF, 0x3F,
454 600000, 3500000, 12500, 0xE8, ricoh619_ops, 500,
457 RICOH619_REG(LDO1, 0x44, 0, 0x46, 0, 0x4C, 0x7F, 0x58,
458 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
461 RICOH619_REG(LDO2, 0x44, 1, 0x46, 1, 0x4D, 0x7F, 0x59,
462 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
465 RICOH619_REG(LDO3, 0x44, 2, 0x46, 2, 0x4E, 0x7F, 0x5A,
466 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
469 RICOH619_REG(LDO4, 0x44, 3, 0x46, 3, 0x4F, 0x7F, 0x5B,
470 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
473 RICOH619_REG(LDO5, 0x44, 4, 0x46, 4, 0x50, 0x7F, 0x5C,
474 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
477 RICOH619_REG(LDO6, 0x44, 5, 0x46, 5, 0x51, 0x7F, 0x5D,
478 600000, 3500000, 25000, 0x74, ricoh619_ops, 500,
481 RICOH619_REG(LDO7, 0x44, 6, 0x46, 6, 0x52, 0x7F, 0x5E,
482 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
485 RICOH619_REG(LDO8, 0x44, 7, 0x46, 7, 0x53, 0x7F, 0x5F,
486 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
489 RICOH619_REG(LDO9, 0x45, 0, 0x47, 0, 0x54, 0x7F, 0x60,
490 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
493 RICOH619_REG(LDO10, 0x45, 1, 0x47, 1, 0x55, 0x7F, 0x61,
494 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
497 RICOH619_REG(LDORTC1, 0x45, 4, 0x00, 0, 0x56, 0x7F, 0x00,
498 1700000, 3500000, 25000, 0x48, ricoh619_ops, 500,
501 RICOH619_REG(LDORTC2, 0x45, 5, 0x00, 0, 0x57, 0x7F, 0x00,
502 900000, 3500000, 25000, 0x68, ricoh619_ops, 500,
506 static inline struct ricoh619_regulator *find_regulator_info(int id)
508 struct ricoh619_regulator *ri;
511 for (i = 0; i < ARRAY_SIZE(ricoh619_regulator_data); i++) {
512 ri = &ricoh619_regulator_data[i];
513 if (ri->desc.id == id)
519 static int ricoh619_regulator_preinit(struct device *parent,
520 struct ricoh619_regulator *ri,
521 struct ricoh619_regulator_platform_data *ricoh619_pdata)
525 if (!ricoh619_pdata->init_apply)
528 if (ricoh619_pdata->init_uV >= 0) {
529 ret = __ricoh619_set_voltage(parent, ri,
530 ricoh619_pdata->init_uV,
531 ricoh619_pdata->init_uV, 0);
533 dev_err(ri->dev, "Not able to initialize voltage %d "
534 "for rail %d err %d\n", ricoh619_pdata->init_uV,
540 if (ricoh619_pdata->init_enable)
541 ret = ricoh619_set_bits(parent, ri->reg_en_reg,
544 ret = ricoh619_clr_bits(parent, ri->reg_en_reg,
547 dev_err(ri->dev, "Not able to %s rail %d err %d\n",
548 (ricoh619_pdata->init_enable) ? "enable" : "disable",
554 static inline int ricoh619_cache_regulator_register(struct device *parent,
555 struct ricoh619_regulator *ri)
557 ri->vout_reg_cache = 0;
558 return ricoh619_read(parent, ri->vout_reg, &ri->vout_reg_cache);
562 static struct of_regulator_match ricoh619_regulator_matches[] = {
563 { .name = "ricoh619_dc1",},
564 { .name = "ricoh619_dc2",},
565 { .name = "ricoh619_dc3",},
566 { .name = "ricoh619_dc4",},
567 { .name = "ricoh619_dc5",},
568 { .name = "ricoh619_ldo1",},
569 { .name = "ricoh619_ldo2",},
570 { .name = "ricoh619_ldo3",},
571 { .name = "ricoh619_ldo4",},
572 { .name = "ricoh619_ldo5",},
573 { .name = "ricoh619_ldo6",},
574 { .name = "ricoh619_ldo7",},
575 { .name = "ricoh619_ldo8",},
576 { .name = "ricoh619_ldo9",},
577 { .name = "ricoh619_ldo10",},
578 { .name = "ricoh619_ldortc1",},
579 { .name = "ricoh619_ldortc2",},
584 static int ricoh619_regulator_dt_init(struct platform_device *pdev,
585 struct regulator_config *config,
588 struct device_node *nproot, *np;
590 nproot = of_node_get(pdev->dev.parent->of_node);
593 np = of_find_node_by_name(nproot, "regulators");
595 dev_err(&pdev->dev, "failed to find regulators node\n");
599 rcount = of_regulator_match(&pdev->dev, np,
600 &ricoh619_regulator_matches[regidx], 1);
604 config->init_data = ricoh619_regulator_matches[regidx].init_data;
605 config->of_node = ricoh619_regulator_matches[regidx].of_node;
610 #define ricoh619_regulator_dt_init(x, y, z) (-1)
613 static int ricoh619_regulator_probe(struct platform_device *pdev)
615 struct ricoh619_regulator *ri = NULL;
616 struct regulator_dev *rdev;
617 struct regulator_config config = { };
618 struct regulator_init_data *pdata_regulator = dev_get_platdata(&pdev->dev);
621 rdev = devm_kzalloc(&pdev->dev, RICOH619_NUM_REGULATOR *
622 sizeof(*rdev), GFP_KERNEL);
624 dev_err(&pdev->dev, "Mmemory alloc failed\n");
628 for (id = 0; id < RICOH619_NUM_REGULATOR; ++id) {
630 ri = find_regulator_info(id);
632 dev_err(&pdev->dev, "invalid regulator ID specified\n");
636 ri->dev = &pdev->dev;
637 config.dev = &pdev->dev;
638 config.driver_data = ri;
640 if (ricoh619_regulator_matches)
641 config.of_node = ricoh619_regulator_matches[id].of_node;
643 if (ricoh619_regulator_dt_init(pdev, &config, id))
645 config.init_data = &pdata_regulator;
647 rdev = regulator_register(&ri->desc, &config);
648 if (IS_ERR_OR_NULL(rdev)) {
649 dev_err(&pdev->dev, "failed to register regulator %s\n",
651 return PTR_ERR(rdev);
655 platform_set_drvdata(pdev, rdev);
659 static int ricoh619_regulator_remove(struct platform_device *pdev)
661 struct regulator_dev *rdev = platform_get_drvdata(pdev);
663 regulator_unregister(rdev);
667 static struct platform_driver ricoh619_regulator_driver = {
669 .name = "ricoh619-regulator",
670 .owner = THIS_MODULE,
672 .probe = ricoh619_regulator_probe,
673 .remove = ricoh619_regulator_remove,
676 static int __init ricoh619_regulator_init(void)
679 return platform_driver_register(&ricoh619_regulator_driver);
681 subsys_initcall_sync(ricoh619_regulator_init);
683 static void __exit ricoh619_regulator_exit(void)
685 platform_driver_unregister(&ricoh619_regulator_driver);
687 module_exit(ricoh619_regulator_exit);
689 MODULE_DESCRIPTION("RICOH619 regulator driver");
690 MODULE_ALIAS("platform:ricoh619-regulator");
691 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
692 MODULE_LICENSE("GPL");