Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Regulator driver for RICOH RC5T583 power management chip. |
| 3 | * |
| 4 | * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved. |
| 5 | * Author: Laxman dewangan <ldewangan@nvidia.com> |
| 6 | * |
| 7 | * based on code |
| 8 | * Copyright (C) 2011 RICOH COMPANY,LTD |
| 9 | * |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify it |
| 12 | * under the terms and conditions of the GNU General Public License, |
| 13 | * version 2, as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 18 | * more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <linux/module.h> |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/regulator/driver.h> |
| 31 | #include <linux/regulator/machine.h> |
| 32 | #include <linux/gpio.h> |
| 33 | #include <linux/mfd/rc5t583.h> |
| 34 | |
| 35 | struct rc5t583_regulator_info { |
| 36 | int deepsleep_id; |
| 37 | |
| 38 | /* Regulator register address.*/ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 39 | uint8_t reg_disc_reg; |
| 40 | uint8_t disc_bit; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 41 | uint8_t deepsleep_reg; |
| 42 | |
| 43 | /* Chip constraints on regulator behavior */ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 44 | int max_uV; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 45 | |
| 46 | /* Regulator specific turn-on delay and voltage settling time*/ |
| 47 | int enable_uv_per_us; |
| 48 | int change_uv_per_us; |
| 49 | |
| 50 | /* Used by regulator core */ |
| 51 | struct regulator_desc desc; |
| 52 | }; |
| 53 | |
| 54 | struct rc5t583_regulator { |
| 55 | struct rc5t583_regulator_info *reg_info; |
| 56 | |
| 57 | /* Devices */ |
| 58 | struct device *dev; |
| 59 | struct rc5t583 *mfd; |
| 60 | struct regulator_dev *rdev; |
| 61 | }; |
| 62 | |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 63 | static int rc5t583_set_voltage(struct regulator_dev *rdev, |
| 64 | int min_uV, int max_uV, unsigned *selector) |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 65 | { |
| 66 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 67 | int sel, ret; |
| 68 | |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 69 | if (min_uV < rdev->desc->min_uV) |
| 70 | min_uV = rdev->desc->min_uV; |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 71 | |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 72 | sel = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 73 | |
| 74 | if (sel >= rdev->desc->n_voltages) { |
| 75 | dev_err(&rdev->dev, "Invalid selector 0x%02x\n", sel); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 76 | return -EINVAL; |
| 77 | } |
| 78 | |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 79 | *selector = sel; |
| 80 | |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 81 | ret = rc5t583_update(reg->mfd->dev, rdev->desc->vsel_reg, sel, |
| 82 | rdev->desc->vsel_mask); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 83 | if (ret < 0) |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 84 | dev_err(&rdev->dev, "Error in update voltage register 0x%02x\n", |
| 85 | rdev->desc->vsel_reg); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 86 | return ret; |
| 87 | } |
| 88 | |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 89 | static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) |
| 90 | { |
| 91 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 92 | int vsel = regulator_get_voltage_sel_regmap(rdev); |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 93 | int curr_uV = regulator_list_voltage_linear(rdev, vsel); |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 94 | |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 95 | return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us); |
| 96 | } |
| 97 | |
| 98 | static int rc5t583_set_voltage_time_sel(struct regulator_dev *rdev, |
| 99 | unsigned int old_selector, unsigned int new_selector) |
| 100 | { |
| 101 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 102 | int old_uV, new_uV; |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 103 | old_uV = regulator_list_voltage_linear(rdev, old_selector); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 104 | |
| 105 | if (old_uV < 0) |
| 106 | return old_uV; |
| 107 | |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 108 | new_uV = regulator_list_voltage_linear(rdev, new_selector); |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 109 | if (new_uV < 0) |
| 110 | return new_uV; |
| 111 | |
| 112 | return DIV_ROUND_UP(abs(old_uV - new_uV), |
| 113 | reg->reg_info->change_uv_per_us); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static struct regulator_ops rc5t583_ops = { |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 118 | .is_enabled = regulator_is_enabled_regmap, |
| 119 | .enable = regulator_enable_regmap, |
| 120 | .disable = regulator_disable_regmap, |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 121 | .enable_time = rc5t583_regulator_enable_time, |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 122 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | f604c10 | 2012-04-05 14:07:36 +0800 | [diff] [blame] | 123 | .set_voltage = rc5t583_set_voltage, |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 124 | .list_voltage = regulator_list_voltage_linear, |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 125 | .set_voltage_time_sel = rc5t583_set_voltage_time_sel, |
| 126 | }; |
| 127 | |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 128 | #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \ |
| 129 | _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 130 | { \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 131 | .reg_disc_reg = RC5T583_REG_##_disc_reg, \ |
| 132 | .disc_bit = _disc_bit, \ |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 133 | .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 134 | .max_uV = _max_mv * 1000, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 135 | .enable_uv_per_us = _enable_mv * 1000, \ |
| 136 | .change_uv_per_us = 40 * 1000, \ |
| 137 | .deepsleep_id = RC5T583_DS_##_id, \ |
| 138 | .desc = { \ |
| 139 | .name = "rc5t583-regulator-"#_id, \ |
| 140 | .id = RC5T583_REGULATOR_##_id, \ |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame] | 141 | .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 142 | .ops = &rc5t583_ops, \ |
| 143 | .type = REGULATOR_VOLTAGE, \ |
| 144 | .owner = THIS_MODULE, \ |
Axel Lin | 15b397d7 | 2012-04-24 09:56:43 +0800 | [diff] [blame] | 145 | .vsel_reg = RC5T583_REG_##_id##DAC, \ |
| 146 | .vsel_mask = _vout_mask, \ |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 147 | .enable_reg = RC5T583_REG_##_en_reg, \ |
| 148 | .enable_mask = BIT(_en_bit), \ |
Axel Lin | 3209be1 | 2012-05-14 10:54:46 +0800 | [diff] [blame^] | 149 | .min_uV = _min_mv * 1000, \ |
| 150 | .uV_step = _step_uV, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 151 | }, \ |
| 152 | } |
| 153 | |
| 154 | static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { |
Axel Lin | 95e301b | 2012-04-06 08:01:36 +0800 | [diff] [blame] | 155 | RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4), |
| 156 | RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14), |
| 157 | RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14), |
| 158 | RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14), |
| 159 | RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160), |
| 160 | RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160), |
| 161 | RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160), |
| 162 | RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160), |
| 163 | RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133), |
| 164 | RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267), |
| 165 | RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133), |
| 166 | RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233), |
| 167 | RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233), |
| 168 | RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | static int __devinit rc5t583_regulator_probe(struct platform_device *pdev) |
| 172 | { |
| 173 | struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); |
| 174 | struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); |
| 175 | struct regulator_init_data *reg_data; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 176 | struct regulator_config config = { }; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 177 | struct rc5t583_regulator *reg = NULL; |
| 178 | struct rc5t583_regulator *regs; |
| 179 | struct regulator_dev *rdev; |
| 180 | struct rc5t583_regulator_info *ri; |
| 181 | int ret; |
| 182 | int id; |
| 183 | |
| 184 | if (!pdata) { |
| 185 | dev_err(&pdev->dev, "No platform data, exiting...\n"); |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | |
| 189 | regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX * |
| 190 | sizeof(struct rc5t583_regulator), GFP_KERNEL); |
| 191 | if (!regs) { |
| 192 | dev_err(&pdev->dev, "Memory allocation failed exiting..\n"); |
| 193 | return -ENOMEM; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { |
| 198 | reg_data = pdata->reg_init_data[id]; |
| 199 | |
| 200 | /* No need to register if there is no regulator data */ |
| 201 | if (!reg_data) |
| 202 | continue; |
| 203 | |
| 204 | reg = ®s[id]; |
| 205 | ri = &rc5t583_reg_info[id]; |
| 206 | reg->reg_info = ri; |
| 207 | reg->mfd = rc5t583; |
| 208 | reg->dev = &pdev->dev; |
| 209 | |
| 210 | if (ri->deepsleep_id == RC5T583_DS_NONE) |
| 211 | goto skip_ext_pwr_config; |
| 212 | |
| 213 | ret = rc5t583_ext_power_req_config(rc5t583->dev, |
| 214 | ri->deepsleep_id, |
| 215 | pdata->regulator_ext_pwr_control[id], |
| 216 | pdata->regulator_deepsleep_slot[id]); |
| 217 | /* |
| 218 | * Configuring external control is not a major issue, |
| 219 | * just give warning. |
| 220 | */ |
| 221 | if (ret < 0) |
| 222 | dev_warn(&pdev->dev, |
| 223 | "Failed to configure ext control %d\n", id); |
| 224 | |
| 225 | skip_ext_pwr_config: |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 226 | config.dev = &pdev->dev; |
| 227 | config.init_data = reg_data; |
| 228 | config.driver_data = reg; |
Axel Lin | 5bb6936 | 2012-04-17 14:11:31 +0800 | [diff] [blame] | 229 | config.regmap = rc5t583->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 230 | |
| 231 | rdev = regulator_register(&ri->desc, &config); |
Axel Lin | a69df8a | 2012-04-04 19:52:35 +0800 | [diff] [blame] | 232 | if (IS_ERR(rdev)) { |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 233 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 234 | ri->desc.name); |
| 235 | ret = PTR_ERR(rdev); |
| 236 | goto clean_exit; |
| 237 | } |
| 238 | reg->rdev = rdev; |
| 239 | } |
| 240 | platform_set_drvdata(pdev, regs); |
| 241 | return 0; |
| 242 | |
| 243 | clean_exit: |
Axel Lin | a69df8a | 2012-04-04 19:52:35 +0800 | [diff] [blame] | 244 | while (--id >= 0) |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 245 | regulator_unregister(regs[id].rdev); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static int __devexit rc5t583_regulator_remove(struct platform_device *pdev) |
| 251 | { |
| 252 | struct rc5t583_regulator *regs = platform_get_drvdata(pdev); |
| 253 | int id; |
| 254 | |
| 255 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) |
| 256 | regulator_unregister(regs[id].rdev); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static struct platform_driver rc5t583_regulator_driver = { |
| 261 | .driver = { |
| 262 | .name = "rc5t583-regulator", |
| 263 | .owner = THIS_MODULE, |
| 264 | }, |
| 265 | .probe = rc5t583_regulator_probe, |
| 266 | .remove = __devexit_p(rc5t583_regulator_remove), |
| 267 | }; |
| 268 | |
| 269 | static int __init rc5t583_regulator_init(void) |
| 270 | { |
| 271 | return platform_driver_register(&rc5t583_regulator_driver); |
| 272 | } |
| 273 | subsys_initcall(rc5t583_regulator_init); |
| 274 | |
| 275 | static void __exit rc5t583_regulator_exit(void) |
| 276 | { |
| 277 | platform_driver_unregister(&rc5t583_regulator_driver); |
| 278 | } |
| 279 | module_exit(rc5t583_regulator_exit); |
| 280 | |
| 281 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 282 | MODULE_DESCRIPTION("RC5T583 regulator driver"); |
| 283 | MODULE_ALIAS("platform:rc5t583-regulator"); |
Laxman Dewangan | 4eb0645 | 2012-04-06 10:58:33 +0530 | [diff] [blame] | 284 | MODULE_LICENSE("GPL v2"); |