Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Regulator driver for tps65090 power management chip. |
| 3 | * |
| 4 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 5 | |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 20 | #include <linux/init.h> |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 21 | #include <linux/gpio.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/regulator/driver.h> |
| 26 | #include <linux/regulator/machine.h> |
| 27 | #include <linux/mfd/tps65090.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 28 | |
| 29 | struct tps65090_regulator { |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 30 | struct device *dev; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 31 | struct regulator_desc *desc; |
| 32 | struct regulator_dev *rdev; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 33 | }; |
| 34 | |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 35 | static struct regulator_ops tps65090_ext_control_ops = { |
| 36 | }; |
| 37 | |
| 38 | static struct regulator_ops tps65090_reg_contol_ops = { |
| 39 | .enable = regulator_enable_regmap, |
| 40 | .disable = regulator_disable_regmap, |
| 41 | .is_enabled = regulator_is_enabled_regmap, |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 42 | }; |
| 43 | |
Laxman Dewangan | 3a81ef8 | 2012-10-09 15:19:01 +0530 | [diff] [blame] | 44 | static struct regulator_ops tps65090_ldo_ops = { |
| 45 | }; |
| 46 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 47 | #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \ |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 48 | { \ |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 49 | .name = "TPS65090_RAILS"#_id, \ |
| 50 | .supply_name = _sname, \ |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 51 | .id = TPS65090_REGULATOR_##_id, \ |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 52 | .ops = &_ops, \ |
| 53 | .enable_reg = _en_reg, \ |
| 54 | .enable_mask = BIT(0), \ |
| 55 | .type = REGULATOR_VOLTAGE, \ |
| 56 | .owner = THIS_MODULE, \ |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 57 | } |
| 58 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 59 | static struct regulator_desc tps65090_regulator_desc[] = { |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 60 | tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops), |
| 61 | tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops), |
| 62 | tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops), |
| 63 | tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops), |
| 64 | tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops), |
| 65 | tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops), |
| 66 | tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops), |
| 67 | tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops), |
| 68 | tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops), |
| 69 | tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops), |
Laxman Dewangan | 3a81ef8 | 2012-10-09 15:19:01 +0530 | [diff] [blame] | 70 | tps65090_REG_DESC(LDO1, "vsys_l1", 0, tps65090_ldo_ops), |
| 71 | tps65090_REG_DESC(LDO2, "vsys_l2", 0, tps65090_ldo_ops), |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 72 | }; |
| 73 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 74 | static inline bool is_dcdc(int id) |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 75 | { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 76 | switch (id) { |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 77 | case TPS65090_REGULATOR_DCDC1: |
| 78 | case TPS65090_REGULATOR_DCDC2: |
| 79 | case TPS65090_REGULATOR_DCDC3: |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 80 | return true; |
| 81 | default: |
| 82 | return false; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 83 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 84 | } |
| 85 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 86 | static int tps65090_config_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 87 | struct tps65090_regulator *ri, bool enable) |
| 88 | { |
| 89 | int ret; |
| 90 | struct device *parent = ri->dev->parent; |
| 91 | unsigned int reg_en_reg = ri->desc->enable_reg; |
| 92 | |
| 93 | if (enable) |
| 94 | ret = tps65090_set_bits(parent, reg_en_reg, 1); |
| 95 | else |
| 96 | ret = tps65090_clr_bits(parent, reg_en_reg, 1); |
| 97 | if (ret < 0) |
| 98 | dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg); |
| 99 | return ret; |
| 100 | } |
| 101 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 102 | static int tps65090_regulator_disable_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 103 | struct tps65090_regulator *ri, |
| 104 | struct tps65090_regulator_plat_data *tps_pdata) |
| 105 | { |
| 106 | int ret = 0; |
| 107 | struct device *parent = ri->dev->parent; |
| 108 | unsigned int reg_en_reg = ri->desc->enable_reg; |
| 109 | |
| 110 | /* |
| 111 | * First enable output for internal control if require. |
| 112 | * And then disable external control. |
| 113 | */ |
| 114 | if (tps_pdata->reg_init_data->constraints.always_on || |
| 115 | tps_pdata->reg_init_data->constraints.boot_on) { |
| 116 | ret = tps65090_set_bits(parent, reg_en_reg, 0); |
| 117 | if (ret < 0) { |
| 118 | dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg); |
| 119 | return ret; |
| 120 | } |
| 121 | } |
| 122 | return tps65090_config_ext_control(ri, false); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 125 | static void tps65090_configure_regulator_config( |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 126 | struct tps65090_regulator_plat_data *tps_pdata, |
| 127 | struct regulator_config *config) |
| 128 | { |
| 129 | if (gpio_is_valid(tps_pdata->gpio)) { |
| 130 | int gpio_flag = GPIOF_OUT_INIT_LOW; |
| 131 | |
| 132 | if (tps_pdata->reg_init_data->constraints.always_on || |
| 133 | tps_pdata->reg_init_data->constraints.boot_on) |
| 134 | gpio_flag = GPIOF_OUT_INIT_HIGH; |
| 135 | |
| 136 | config->ena_gpio = tps_pdata->gpio; |
| 137 | config->ena_gpio_flags = gpio_flag; |
| 138 | } |
| 139 | } |
| 140 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 141 | static int tps65090_regulator_probe(struct platform_device *pdev) |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 142 | { |
Axel Lin | 06c4998 | 2012-04-17 17:08:56 +0800 | [diff] [blame] | 143 | struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 144 | struct tps65090_regulator *ri = NULL; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 145 | struct regulator_config config = { }; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 146 | struct regulator_dev *rdev; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 147 | struct tps65090_regulator_plat_data *tps_pdata; |
| 148 | struct tps65090_regulator *pmic; |
| 149 | struct tps65090_platform_data *tps65090_pdata; |
| 150 | int num; |
| 151 | int ret; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 152 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 153 | dev_dbg(&pdev->dev, "Probing regulator\n"); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 154 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 155 | tps65090_pdata = dev_get_platdata(pdev->dev.parent); |
| 156 | if (!tps65090_pdata) { |
| 157 | dev_err(&pdev->dev, "Platform data missing\n"); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | } |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 160 | |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 161 | pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic), |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 162 | GFP_KERNEL); |
| 163 | if (!pmic) { |
| 164 | dev_err(&pdev->dev, "mem alloc for pmic failed\n"); |
| 165 | return -ENOMEM; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 166 | } |
| 167 | |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 168 | for (num = 0; num < TPS65090_REGULATOR_MAX; num++) { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 169 | tps_pdata = tps65090_pdata->reg_pdata[num]; |
| 170 | |
| 171 | ri = &pmic[num]; |
| 172 | ri->dev = &pdev->dev; |
| 173 | ri->desc = &tps65090_regulator_desc[num]; |
| 174 | |
| 175 | /* |
| 176 | * TPS5090 DCDC support the control from external digital input. |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 177 | * Configure it as per platform data. |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 178 | */ |
| 179 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) { |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 180 | if (tps_pdata->enable_ext_control) { |
| 181 | tps65090_configure_regulator_config( |
| 182 | tps_pdata, &config); |
| 183 | ri->desc->ops = &tps65090_ext_control_ops; |
| 184 | } else { |
| 185 | ret = tps65090_regulator_disable_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 186 | ri, tps_pdata); |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 187 | if (ret < 0) { |
| 188 | dev_err(&pdev->dev, |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 189 | "failed disable ext control\n"); |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 190 | goto scrub; |
| 191 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 194 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 195 | config.dev = &pdev->dev; |
| 196 | config.driver_data = ri; |
| 197 | config.regmap = tps65090_mfd->rmap; |
| 198 | if (tps_pdata) |
| 199 | config.init_data = tps_pdata->reg_init_data; |
| 200 | else |
| 201 | config.init_data = NULL; |
| 202 | |
| 203 | rdev = regulator_register(ri->desc, &config); |
| 204 | if (IS_ERR(rdev)) { |
| 205 | dev_err(&pdev->dev, "failed to register regulator %s\n", |
| 206 | ri->desc->name); |
| 207 | ret = PTR_ERR(rdev); |
| 208 | goto scrub; |
| 209 | } |
| 210 | ri->rdev = rdev; |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 211 | |
| 212 | /* Enable external control if it is require */ |
| 213 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data && |
| 214 | tps_pdata->enable_ext_control) { |
| 215 | ret = tps65090_config_ext_control(ri, true); |
| 216 | if (ret < 0) { |
| 217 | /* Increment num to get unregister rdev */ |
| 218 | num++; |
| 219 | goto scrub; |
| 220 | } |
| 221 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | platform_set_drvdata(pdev, pmic); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 225 | return 0; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 226 | |
| 227 | scrub: |
| 228 | while (--num >= 0) { |
| 229 | ri = &pmic[num]; |
| 230 | regulator_unregister(ri->rdev); |
| 231 | } |
| 232 | return ret; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 233 | } |
| 234 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame^] | 235 | static int tps65090_regulator_remove(struct platform_device *pdev) |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 236 | { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 237 | struct tps65090_regulator *pmic = platform_get_drvdata(pdev); |
| 238 | struct tps65090_regulator *ri; |
| 239 | int num; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 240 | |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 241 | for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 242 | ri = &pmic[num]; |
| 243 | regulator_unregister(ri->rdev); |
| 244 | } |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static struct platform_driver tps65090_regulator_driver = { |
| 249 | .driver = { |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 250 | .name = "tps65090-pmic", |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 251 | .owner = THIS_MODULE, |
| 252 | }, |
| 253 | .probe = tps65090_regulator_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 254 | .remove = tps65090_regulator_remove, |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | static int __init tps65090_regulator_init(void) |
| 258 | { |
| 259 | return platform_driver_register(&tps65090_regulator_driver); |
| 260 | } |
| 261 | subsys_initcall(tps65090_regulator_init); |
| 262 | |
| 263 | static void __exit tps65090_regulator_exit(void) |
| 264 | { |
| 265 | platform_driver_unregister(&tps65090_regulator_driver); |
| 266 | } |
| 267 | module_exit(tps65090_regulator_exit); |
| 268 | |
| 269 | MODULE_DESCRIPTION("tps65090 regulator driver"); |
| 270 | MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>"); |
| 271 | MODULE_LICENSE("GPL v2"); |