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> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/regulator/driver.h> |
| 32 | #include <linux/regulator/machine.h> |
| 33 | #include <linux/gpio.h> |
| 34 | #include <linux/mfd/rc5t583.h> |
| 35 | |
| 36 | struct rc5t583_regulator_info { |
| 37 | int deepsleep_id; |
| 38 | |
| 39 | /* Regulator register address.*/ |
| 40 | uint8_t reg_en_reg; |
| 41 | uint8_t en_bit; |
| 42 | uint8_t reg_disc_reg; |
| 43 | uint8_t disc_bit; |
| 44 | uint8_t vout_reg; |
| 45 | uint8_t vout_mask; |
| 46 | uint8_t deepsleep_reg; |
| 47 | |
| 48 | /* Chip constraints on regulator behavior */ |
| 49 | int min_uV; |
| 50 | int max_uV; |
| 51 | int step_uV; |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 52 | |
| 53 | /* Regulator specific turn-on delay and voltage settling time*/ |
| 54 | int enable_uv_per_us; |
| 55 | int change_uv_per_us; |
| 56 | |
| 57 | /* Used by regulator core */ |
| 58 | struct regulator_desc desc; |
| 59 | }; |
| 60 | |
| 61 | struct rc5t583_regulator { |
| 62 | struct rc5t583_regulator_info *reg_info; |
| 63 | |
| 64 | /* Devices */ |
| 65 | struct device *dev; |
| 66 | struct rc5t583 *mfd; |
| 67 | struct regulator_dev *rdev; |
| 68 | }; |
| 69 | |
| 70 | static int rc5t583_reg_is_enabled(struct regulator_dev *rdev) |
| 71 | { |
| 72 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 73 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 74 | uint8_t control; |
| 75 | int ret; |
| 76 | |
| 77 | ret = rc5t583_read(reg->mfd->dev, ri->reg_en_reg, &control); |
| 78 | if (ret < 0) { |
| 79 | dev_err(&rdev->dev, |
| 80 | "Error in reading the control register 0x%02x\n", |
| 81 | ri->reg_en_reg); |
| 82 | return ret; |
| 83 | } |
| 84 | return !!(control & BIT(ri->en_bit)); |
| 85 | } |
| 86 | |
| 87 | static int rc5t583_reg_enable(struct regulator_dev *rdev) |
| 88 | { |
| 89 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 90 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 91 | int ret; |
| 92 | |
| 93 | ret = rc5t583_set_bits(reg->mfd->dev, ri->reg_en_reg, |
| 94 | (1 << ri->en_bit)); |
| 95 | if (ret < 0) { |
| 96 | dev_err(&rdev->dev, |
| 97 | "Error in setting bit of STATE register 0x%02x\n", |
| 98 | ri->reg_en_reg); |
| 99 | return ret; |
| 100 | } |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int rc5t583_reg_disable(struct regulator_dev *rdev) |
| 105 | { |
| 106 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 107 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 108 | int ret; |
| 109 | |
| 110 | ret = rc5t583_clear_bits(reg->mfd->dev, ri->reg_en_reg, |
| 111 | (1 << ri->en_bit)); |
| 112 | if (ret < 0) |
| 113 | dev_err(&rdev->dev, |
| 114 | "Error in clearing bit of STATE register 0x%02x\n", |
| 115 | ri->reg_en_reg); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int rc5t583_list_voltage(struct regulator_dev *rdev, unsigned selector) |
| 121 | { |
| 122 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 123 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 124 | return ri->min_uV + (ri->step_uV * selector); |
| 125 | } |
| 126 | |
| 127 | static int rc5t583_set_voltage_sel(struct regulator_dev *rdev, |
| 128 | unsigned int selector) |
| 129 | { |
| 130 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 131 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 132 | int ret; |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 133 | if (selector >= rdev->desc->n_voltages) { |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 134 | dev_err(&rdev->dev, "Invalid selector 0x%02x\n", selector); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | ret = rc5t583_update(reg->mfd->dev, ri->vout_reg, |
| 139 | selector, ri->vout_mask); |
| 140 | if (ret < 0) |
| 141 | dev_err(&rdev->dev, |
| 142 | "Error in update voltage register 0x%02x\n", ri->vout_reg); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | static int rc5t583_get_voltage_sel(struct regulator_dev *rdev) |
| 147 | { |
| 148 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 149 | struct rc5t583_regulator_info *ri = reg->reg_info; |
| 150 | uint8_t vsel; |
| 151 | int ret; |
| 152 | ret = rc5t583_read(reg->mfd->dev, ri->vout_reg, &vsel); |
| 153 | if (ret < 0) { |
| 154 | dev_err(&rdev->dev, |
| 155 | "Error in reading voltage register 0x%02x\n", ri->vout_reg); |
| 156 | return ret; |
| 157 | } |
| 158 | return vsel & ri->vout_mask; |
| 159 | } |
| 160 | |
| 161 | static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) |
| 162 | { |
| 163 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 164 | int vsel = rc5t583_get_voltage_sel(rdev); |
| 165 | int curr_uV = rc5t583_list_voltage(rdev, vsel); |
| 166 | return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us); |
| 167 | } |
| 168 | |
| 169 | static int rc5t583_set_voltage_time_sel(struct regulator_dev *rdev, |
| 170 | unsigned int old_selector, unsigned int new_selector) |
| 171 | { |
| 172 | struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); |
| 173 | int old_uV, new_uV; |
| 174 | old_uV = rc5t583_list_voltage(rdev, old_selector); |
| 175 | |
| 176 | if (old_uV < 0) |
| 177 | return old_uV; |
| 178 | |
| 179 | new_uV = rc5t583_list_voltage(rdev, new_selector); |
| 180 | if (new_uV < 0) |
| 181 | return new_uV; |
| 182 | |
| 183 | return DIV_ROUND_UP(abs(old_uV - new_uV), |
| 184 | reg->reg_info->change_uv_per_us); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | static struct regulator_ops rc5t583_ops = { |
| 189 | .is_enabled = rc5t583_reg_is_enabled, |
| 190 | .enable = rc5t583_reg_enable, |
| 191 | .disable = rc5t583_reg_disable, |
| 192 | .enable_time = rc5t583_regulator_enable_time, |
| 193 | .get_voltage_sel = rc5t583_get_voltage_sel, |
| 194 | .set_voltage_sel = rc5t583_set_voltage_sel, |
| 195 | .list_voltage = rc5t583_list_voltage, |
| 196 | .set_voltage_time_sel = rc5t583_set_voltage_time_sel, |
| 197 | }; |
| 198 | |
| 199 | #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, _vout_reg, \ |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 200 | _vout_mask, _ds_reg, _min_mv, _max_mv, _step_uV, _enable_mv) \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 201 | { \ |
| 202 | .reg_en_reg = RC5T583_REG_##_en_reg, \ |
| 203 | .en_bit = _en_bit, \ |
| 204 | .reg_disc_reg = RC5T583_REG_##_disc_reg, \ |
| 205 | .disc_bit = _disc_bit, \ |
| 206 | .vout_reg = RC5T583_REG_##_vout_reg, \ |
| 207 | .vout_mask = _vout_mask, \ |
| 208 | .deepsleep_reg = RC5T583_REG_##_ds_reg, \ |
| 209 | .min_uV = _min_mv * 1000, \ |
| 210 | .max_uV = _max_mv * 1000, \ |
| 211 | .step_uV = _step_uV, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 212 | .enable_uv_per_us = _enable_mv * 1000, \ |
| 213 | .change_uv_per_us = 40 * 1000, \ |
| 214 | .deepsleep_id = RC5T583_DS_##_id, \ |
| 215 | .desc = { \ |
| 216 | .name = "rc5t583-regulator-"#_id, \ |
| 217 | .id = RC5T583_REGULATOR_##_id, \ |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 218 | .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 219 | .ops = &rc5t583_ops, \ |
| 220 | .type = REGULATOR_VOLTAGE, \ |
| 221 | .owner = THIS_MODULE, \ |
| 222 | }, \ |
| 223 | } |
| 224 | |
| 225 | static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { |
| 226 | RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, DC0DAC, 0x7F, DC0DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 227 | 700, 1500, 12500, 4), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 228 | RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, DC1DAC, 0x7F, DC1DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 229 | 700, 1500, 12500, 14), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 230 | RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, DC2DAC, 0x7F, DC2DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 231 | 900, 2400, 12500, 14), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 232 | RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, DC3DAC, 0x7F, DC3DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 233 | 900, 2400, 12500, 14), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 234 | RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, LDO0DAC, 0x7F, LDO0DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 235 | 900, 3400, 25000, 160), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 236 | RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, LDO1DAC, 0x7F, LDO1DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 237 | 900, 3400, 25000, 160), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 238 | RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, LDO2DAC, 0x7F, LDO2DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 239 | 900, 3400, 25000, 160), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 240 | RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, LDO3DAC, 0x7F, LDO3DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 241 | 900, 3400, 25000, 160), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 242 | RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, LDO4DAC, 0x3F, LDO4DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 243 | 750, 1500, 12500, 133), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 244 | RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, LDO5DAC, 0x7F, LDO5DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 245 | 900, 3400, 25000, 267), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 246 | RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, LDO6DAC, 0x7F, LDO6DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 247 | 900, 3400, 25000, 133), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 248 | RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, LDO7DAC, 0x7F, LDO7DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 249 | 900, 3400, 25000, 233), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 250 | RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, LDO8DAC, 0x7F, LDO8DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 251 | 900, 3400, 25000, 233), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 252 | RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, LDO9DAC, 0x7F, LDO9DAC_DS, |
Axel Lin | e3a7384 | 2012-04-05 14:04:48 +0800 | [diff] [blame^] | 253 | 900, 3400, 25000, 133), |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static int __devinit rc5t583_regulator_probe(struct platform_device *pdev) |
| 257 | { |
| 258 | struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); |
| 259 | struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); |
| 260 | struct regulator_init_data *reg_data; |
| 261 | struct rc5t583_regulator *reg = NULL; |
| 262 | struct rc5t583_regulator *regs; |
| 263 | struct regulator_dev *rdev; |
| 264 | struct rc5t583_regulator_info *ri; |
| 265 | int ret; |
| 266 | int id; |
| 267 | |
| 268 | if (!pdata) { |
| 269 | dev_err(&pdev->dev, "No platform data, exiting...\n"); |
| 270 | return -ENODEV; |
| 271 | } |
| 272 | |
| 273 | regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX * |
| 274 | sizeof(struct rc5t583_regulator), GFP_KERNEL); |
| 275 | if (!regs) { |
| 276 | dev_err(&pdev->dev, "Memory allocation failed exiting..\n"); |
| 277 | return -ENOMEM; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { |
| 282 | reg_data = pdata->reg_init_data[id]; |
| 283 | |
| 284 | /* No need to register if there is no regulator data */ |
| 285 | if (!reg_data) |
| 286 | continue; |
| 287 | |
| 288 | reg = ®s[id]; |
| 289 | ri = &rc5t583_reg_info[id]; |
| 290 | reg->reg_info = ri; |
| 291 | reg->mfd = rc5t583; |
| 292 | reg->dev = &pdev->dev; |
| 293 | |
| 294 | if (ri->deepsleep_id == RC5T583_DS_NONE) |
| 295 | goto skip_ext_pwr_config; |
| 296 | |
| 297 | ret = rc5t583_ext_power_req_config(rc5t583->dev, |
| 298 | ri->deepsleep_id, |
| 299 | pdata->regulator_ext_pwr_control[id], |
| 300 | pdata->regulator_deepsleep_slot[id]); |
| 301 | /* |
| 302 | * Configuring external control is not a major issue, |
| 303 | * just give warning. |
| 304 | */ |
| 305 | if (ret < 0) |
| 306 | dev_warn(&pdev->dev, |
| 307 | "Failed to configure ext control %d\n", id); |
| 308 | |
| 309 | skip_ext_pwr_config: |
| 310 | rdev = regulator_register(&ri->desc, &pdev->dev, |
| 311 | reg_data, reg, NULL); |
Axel Lin | a69df8a | 2012-04-04 19:52:35 +0800 | [diff] [blame] | 312 | if (IS_ERR(rdev)) { |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 313 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 314 | ri->desc.name); |
| 315 | ret = PTR_ERR(rdev); |
| 316 | goto clean_exit; |
| 317 | } |
| 318 | reg->rdev = rdev; |
| 319 | } |
| 320 | platform_set_drvdata(pdev, regs); |
| 321 | return 0; |
| 322 | |
| 323 | clean_exit: |
Axel Lin | a69df8a | 2012-04-04 19:52:35 +0800 | [diff] [blame] | 324 | while (--id >= 0) |
Laxman Dewangan | 6ffc327 | 2012-04-04 12:44:00 +0530 | [diff] [blame] | 325 | regulator_unregister(regs[id].rdev); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static int __devexit rc5t583_regulator_remove(struct platform_device *pdev) |
| 331 | { |
| 332 | struct rc5t583_regulator *regs = platform_get_drvdata(pdev); |
| 333 | int id; |
| 334 | |
| 335 | for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) |
| 336 | regulator_unregister(regs[id].rdev); |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static struct platform_driver rc5t583_regulator_driver = { |
| 341 | .driver = { |
| 342 | .name = "rc5t583-regulator", |
| 343 | .owner = THIS_MODULE, |
| 344 | }, |
| 345 | .probe = rc5t583_regulator_probe, |
| 346 | .remove = __devexit_p(rc5t583_regulator_remove), |
| 347 | }; |
| 348 | |
| 349 | static int __init rc5t583_regulator_init(void) |
| 350 | { |
| 351 | return platform_driver_register(&rc5t583_regulator_driver); |
| 352 | } |
| 353 | subsys_initcall(rc5t583_regulator_init); |
| 354 | |
| 355 | static void __exit rc5t583_regulator_exit(void) |
| 356 | { |
| 357 | platform_driver_unregister(&rc5t583_regulator_driver); |
| 358 | } |
| 359 | module_exit(rc5t583_regulator_exit); |
| 360 | |
| 361 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 362 | MODULE_DESCRIPTION("RC5T583 regulator driver"); |
| 363 | MODULE_ALIAS("platform:rc5t583-regulator"); |
| 364 | MODULE_LICENSE("GPL V2"); |