Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Regulator driver for RICOH RC5T583 power management chip. |
| 4 | * |
| 5 | * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved. |
| 6 | * Author: Laxman dewangan <ldewangan@nvidia.com> |
| 7 | * |
| 8 | * based on code |
| 9 | * Copyright (C) 2011 RICOH COMPANY,LTD |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/regulator/driver.h> |
| 18 | #include <linux/regulator/machine.h> |
| 19 | #include <linux/gpio.h> |
| 20 | #include <linux/mfd/rc5t583.h> |
| 21 | |
| 22 | struct rc5t583_regulator_info { |
| 23 | int deepsleep_id; |
| 24 | |
| 25 | /* Regulator register address.*/ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 26 | uint8_t reg_disc_reg; |
| 27 | uint8_t disc_bit; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 28 | uint8_t deepsleep_reg; |
| 29 | |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 30 | /* Regulator specific turn-on delay and voltage settling time*/ |
| 31 | int enable_uv_per_us; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 32 | |
| 33 | /* Used by regulator core */ |
| 34 | struct regulator_desc desc; |
| 35 | }; |
| 36 | |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 37 | static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) |
| 38 | { |
Axel Lin | c07608f | 2019-03-27 19:54:13 +0800 | [diff] [blame] | 39 | struct rc5t583_regulator_info *reg_info = rdev_get_drvdata(rdev); |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 40 | int vsel = regulator_get_voltage_sel_regmap(rdev); |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame] | 41 | int curr_uV = regulator_list_voltage_linear(rdev, vsel); |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 42 | |
Axel Lin | c07608f | 2019-03-27 19:54:13 +0800 | [diff] [blame] | 43 | return DIV_ROUND_UP(curr_uV, reg_info->enable_uv_per_us); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 44 | } |
| 45 | |
Bhumika Goyal | fb74036 | 2017-01-28 20:48:51 +0530 | [diff] [blame] | 46 | static const struct regulator_ops rc5t583_ops = { |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 47 | .is_enabled = regulator_is_enabled_regmap, |
| 48 | .enable = regulator_enable_regmap, |
| 49 | .disable = regulator_disable_regmap, |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 50 | .enable_time = rc5t583_regulator_enable_time, |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 51 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | 70e5f64 | 2012-05-14 10:55:50 +0800 | [diff] [blame] | 52 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame] | 53 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 70e5f64 | 2012-05-14 10:55:50 +0800 | [diff] [blame] | 54 | .map_voltage = regulator_map_voltage_linear, |
Axel Lin | 2f6ae6e | 2012-06-19 09:26:52 +0800 | [diff] [blame] | 55 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 56 | }; |
| 57 | |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 58 | #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \ |
| 59 | _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 60 | { \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 61 | .reg_disc_reg = RC5T583_REG_##_disc_reg, \ |
| 62 | .disc_bit = _disc_bit, \ |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 63 | .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 64 | .enable_uv_per_us = _enable_mv * 1000, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 65 | .deepsleep_id = RC5T583_DS_##_id, \ |
| 66 | .desc = { \ |
| 67 | .name = "rc5t583-regulator-"#_id, \ |
| 68 | .id = RC5T583_REGULATOR_##_id, \ |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame] | 69 | .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 70 | .ops = &rc5t583_ops, \ |
| 71 | .type = REGULATOR_VOLTAGE, \ |
| 72 | .owner = THIS_MODULE, \ |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 73 | .vsel_reg = RC5T583_REG_##_id##DAC, \ |
| 74 | .vsel_mask = _vout_mask, \ |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 75 | .enable_reg = RC5T583_REG_##_en_reg, \ |
| 76 | .enable_mask = BIT(_en_bit), \ |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame] | 77 | .min_uV = _min_mv * 1000, \ |
| 78 | .uV_step = _step_uV, \ |
Axel Lin | 2f6ae6e | 2012-06-19 09:26:52 +0800 | [diff] [blame] | 79 | .ramp_delay = 40 * 1000, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 80 | }, \ |
| 81 | } |
| 82 | |
| 83 | static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 84 | RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4), |
| 85 | RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14), |
| 86 | RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14), |
| 87 | RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14), |
| 88 | RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160), |
| 89 | RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160), |
| 90 | RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160), |
| 91 | RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160), |
| 92 | RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133), |
| 93 | RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267), |
| 94 | RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133), |
| 95 | RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233), |
| 96 | RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233), |
| 97 | RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 98 | }; |
| 99 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 100 | static int rc5t583_regulator_probe(struct platform_device *pdev) |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 101 | { |
| 102 | struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); |
| 103 | struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 104 | struct regulator_config config = { }; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 105 | struct regulator_dev *rdev; |
| 106 | struct rc5t583_regulator_info *ri; |
| 107 | int ret; |
| 108 | int id; |
| 109 | |
| 110 | if (!pdata) { |
| 111 | dev_err(&pdev->dev, "No platform data, exiting...\n"); |
| 112 | return -ENODEV; |
| 113 | } |
| 114 | |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 115 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 116 | ri = &rc5t583_reg_info[id]; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 117 | |
| 118 | if (ri->deepsleep_id == RC5T583_DS_NONE) |
| 119 | goto skip_ext_pwr_config; |
| 120 | |
| 121 | ret = rc5t583_ext_power_req_config(rc5t583->dev, |
| 122 | ri->deepsleep_id, |
| 123 | pdata->regulator_ext_pwr_control[id], |
| 124 | pdata->regulator_deepsleep_slot[id]); |
| 125 | /* |
| 126 | * Configuring external control is not a major issue, |
| 127 | * just give warning. |
| 128 | */ |
| 129 | if (ret < 0) |
| 130 | dev_warn(&pdev->dev, |
| 131 | "Failed to configure ext control %d\n", id); |
| 132 | |
| 133 | skip_ext_pwr_config: |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 134 | config.dev = &pdev->dev; |
Axel Lin | 3f24bf7 | 2014-02-06 14:49:11 +0800 | [diff] [blame] | 135 | config.init_data = pdata->reg_init_data[id]; |
Axel Lin | c07608f | 2019-03-27 19:54:13 +0800 | [diff] [blame] | 136 | config.driver_data = ri; |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 137 | config.regmap = rc5t583->regmap; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 138 | |
Sachin Kamat | 1377530 | 2013-09-04 12:01:02 +0530 | [diff] [blame] | 139 | rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config); |
Axel Lin | a69df8a | 2012-04-04 19:52:35 +0800 | [diff] [blame] | 140 | if (IS_ERR(rdev)) { |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 141 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 142 | ri->desc.name); |
Sachin Kamat | 1377530 | 2013-09-04 12:01:02 +0530 | [diff] [blame] | 143 | return PTR_ERR(rdev); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 144 | } |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 145 | } |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 146 | return 0; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static struct platform_driver rc5t583_regulator_driver = { |
| 150 | .driver = { |
| 151 | .name = "rc5t583-regulator", |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 152 | }, |
| 153 | .probe = rc5t583_regulator_probe, |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static int __init rc5t583_regulator_init(void) |
| 157 | { |
| 158 | return platform_driver_register(&rc5t583_regulator_driver); |
| 159 | } |
| 160 | subsys_initcall(rc5t583_regulator_init); |
| 161 | |
| 162 | static void __exit rc5t583_regulator_exit(void) |
| 163 | { |
| 164 | platform_driver_unregister(&rc5t583_regulator_driver); |
| 165 | } |
| 166 | module_exit(rc5t583_regulator_exit); |
| 167 | |
| 168 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 169 | MODULE_DESCRIPTION("RC5T583 regulator driver"); |
| 170 | MODULE_ALIAS("platform:rc5t583-regulator"); |
Laxman Dewangan | 4eb0645 | 2012-04-06 10:58:33 +0530 | [diff] [blame] | 171 | MODULE_LICENSE("GPL v2"); |