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/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.*/ 39 uint8_t reg_disc_reg; 40 uint8_t disc_bit; 41 uint8_t deepsleep_reg; 42 43 /* Regulator specific turn-on delay and voltage settling time*/ 44 int enable_uv_per_us; 45 int change_uv_per_us; 46 47 /* Used by regulator core */ 48 struct regulator_desc desc; 49 }; 50 51 struct rc5t583_regulator { 52 struct rc5t583_regulator_info *reg_info; 53 54 /* Devices */ 55 struct device *dev; 56 struct rc5t583 *mfd; 57 struct regulator_dev *rdev; 58 }; 59 60 static int rc5t583_regulator_enable_time(struct regulator_dev *rdev) 61 { 62 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); 63 int vsel = regulator_get_voltage_sel_regmap(rdev); 64 int curr_uV = regulator_list_voltage_linear(rdev, vsel); 65 66 return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us); 67 } 68 69 static int rc5t583_set_voltage_time_sel(struct regulator_dev *rdev, 70 unsigned int old_selector, unsigned int new_selector) 71 { 72 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev); 73 int old_uV, new_uV; 74 old_uV = regulator_list_voltage_linear(rdev, old_selector); 75 76 if (old_uV < 0) 77 return old_uV; 78 79 new_uV = regulator_list_voltage_linear(rdev, new_selector); 80 if (new_uV < 0) 81 return new_uV; 82 83 return DIV_ROUND_UP(abs(old_uV - new_uV), 84 reg->reg_info->change_uv_per_us); 85 } 86 87 88 static struct regulator_ops rc5t583_ops = { 89 .is_enabled = regulator_is_enabled_regmap, 90 .enable = regulator_enable_regmap, 91 .disable = regulator_disable_regmap, 92 .enable_time = rc5t583_regulator_enable_time, 93 .get_voltage_sel = regulator_get_voltage_sel_regmap, 94 .set_voltage_sel = regulator_set_voltage_sel_regmap, 95 .list_voltage = regulator_list_voltage_linear, 96 .map_voltage = regulator_map_voltage_linear, 97 .set_voltage_time_sel = rc5t583_set_voltage_time_sel, 98 }; 99 100 #define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \ 101 _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \ 102 { \ 103 .reg_disc_reg = RC5T583_REG_##_disc_reg, \ 104 .disc_bit = _disc_bit, \ 105 .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \ 106 .enable_uv_per_us = _enable_mv * 1000, \ 107 .change_uv_per_us = 40 * 1000, \ 108 .deepsleep_id = RC5T583_DS_##_id, \ 109 .desc = { \ 110 .name = "rc5t583-regulator-"#_id, \ 111 .id = RC5T583_REGULATOR_##_id, \ 112 .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \ 113 .ops = &rc5t583_ops, \ 114 .type = REGULATOR_VOLTAGE, \ 115 .owner = THIS_MODULE, \ 116 .vsel_reg = RC5T583_REG_##_id##DAC, \ 117 .vsel_mask = _vout_mask, \ 118 .enable_reg = RC5T583_REG_##_en_reg, \ 119 .enable_mask = BIT(_en_bit), \ 120 .min_uV = _min_mv * 1000, \ 121 .uV_step = _step_uV, \ 122 }, \ 123 } 124 125 static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = { 126 RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4), 127 RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14), 128 RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14), 129 RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14), 130 RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160), 131 RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160), 132 RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160), 133 RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160), 134 RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133), 135 RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267), 136 RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133), 137 RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233), 138 RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233), 139 RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133), 140 }; 141 142 static int __devinit rc5t583_regulator_probe(struct platform_device *pdev) 143 { 144 struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent); 145 struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev); 146 struct regulator_init_data *reg_data; 147 struct regulator_config config = { }; 148 struct rc5t583_regulator *reg = NULL; 149 struct rc5t583_regulator *regs; 150 struct regulator_dev *rdev; 151 struct rc5t583_regulator_info *ri; 152 int ret; 153 int id; 154 155 if (!pdata) { 156 dev_err(&pdev->dev, "No platform data, exiting...\n"); 157 return -ENODEV; 158 } 159 160 regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX * 161 sizeof(struct rc5t583_regulator), GFP_KERNEL); 162 if (!regs) { 163 dev_err(&pdev->dev, "Memory allocation failed exiting..\n"); 164 return -ENOMEM; 165 } 166 167 168 for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) { 169 reg_data = pdata->reg_init_data[id]; 170 171 /* No need to register if there is no regulator data */ 172 if (!reg_data) 173 continue; 174 175 reg = ®s[id]; 176 ri = &rc5t583_reg_info[id]; 177 reg->reg_info = ri; 178 reg->mfd = rc5t583; 179 reg->dev = &pdev->dev; 180 181 if (ri->deepsleep_id == RC5T583_DS_NONE) 182 goto skip_ext_pwr_config; 183 184 ret = rc5t583_ext_power_req_config(rc5t583->dev, 185 ri->deepsleep_id, 186 pdata->regulator_ext_pwr_control[id], 187 pdata->regulator_deepsleep_slot[id]); 188 /* 189 * Configuring external control is not a major issue, 190 * just give warning. 191 */ 192 if (ret < 0) 193 dev_warn(&pdev->dev, 194 "Failed to configure ext control %d\n", id); 195 196 skip_ext_pwr_config: 197 config.dev = &pdev->dev; 198 config.init_data = reg_data; 199 config.driver_data = reg; 200 config.regmap = rc5t583->regmap; 201 202 rdev = regulator_register(&ri->desc, &config); 203 if (IS_ERR(rdev)) { 204 dev_err(&pdev->dev, "Failed to register regulator %s\n", 205 ri->desc.name); 206 ret = PTR_ERR(rdev); 207 goto clean_exit; 208 } 209 reg->rdev = rdev; 210 } 211 platform_set_drvdata(pdev, regs); 212 return 0; 213 214 clean_exit: 215 while (--id >= 0) 216 regulator_unregister(regs[id].rdev); 217 218 return ret; 219 } 220 221 static int __devexit rc5t583_regulator_remove(struct platform_device *pdev) 222 { 223 struct rc5t583_regulator *regs = platform_get_drvdata(pdev); 224 int id; 225 226 for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) 227 regulator_unregister(regs[id].rdev); 228 return 0; 229 } 230 231 static struct platform_driver rc5t583_regulator_driver = { 232 .driver = { 233 .name = "rc5t583-regulator", 234 .owner = THIS_MODULE, 235 }, 236 .probe = rc5t583_regulator_probe, 237 .remove = __devexit_p(rc5t583_regulator_remove), 238 }; 239 240 static int __init rc5t583_regulator_init(void) 241 { 242 return platform_driver_register(&rc5t583_regulator_driver); 243 } 244 subsys_initcall(rc5t583_regulator_init); 245 246 static void __exit rc5t583_regulator_exit(void) 247 { 248 platform_driver_unregister(&rc5t583_regulator_driver); 249 } 250 module_exit(rc5t583_regulator_exit); 251 252 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 253 MODULE_DESCRIPTION("RC5T583 regulator driver"); 254 MODULE_ALIAS("platform:rc5t583-regulator"); 255 MODULE_LICENSE("GPL v2"); 256