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> |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 20 | #include <linux/delay.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 21 | #include <linux/init.h> |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 22 | #include <linux/gpio.h> |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 23 | #include <linux/of_gpio.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/regulator/driver.h> |
| 28 | #include <linux/regulator/machine.h> |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 29 | #include <linux/regulator/of_regulator.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 30 | #include <linux/mfd/tps65090.h> |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 31 | |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 32 | #define MAX_CTRL_READ_TRIES 5 |
| 33 | #define MAX_FET_ENABLE_TRIES 1000 |
| 34 | |
| 35 | #define CTRL_EN_BIT 0 /* Regulator enable bit, active high */ |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 36 | #define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */ |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 37 | #define CTRL_PG_BIT 4 /* Regulator power good bit, 1=good */ |
| 38 | #define CTRL_TO_BIT 7 /* Regulator timeout bit, 1=wait */ |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 39 | |
| 40 | #define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */ |
| 41 | |
| 42 | /** |
| 43 | * struct tps65090_regulator - Per-regulator data for a tps65090 regulator |
| 44 | * |
| 45 | * @dev: Pointer to our device. |
| 46 | * @desc: The struct regulator_desc for the regulator. |
| 47 | * @rdev: The struct regulator_dev for the regulator. |
| 48 | * @overcurrent_wait_valid: True if overcurrent_wait is valid. |
| 49 | * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield. |
| 50 | */ |
| 51 | |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 52 | struct tps65090_regulator { |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 53 | struct device *dev; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 54 | struct regulator_desc *desc; |
| 55 | struct regulator_dev *rdev; |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 56 | bool overcurrent_wait_valid; |
| 57 | int overcurrent_wait; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 58 | }; |
| 59 | |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 60 | static struct regulator_ops tps65090_ext_control_ops = { |
| 61 | }; |
| 62 | |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 63 | /** |
| 64 | * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait |
| 65 | * |
| 66 | * This will set the overcurrent wait time based on what's in the regulator |
| 67 | * info. |
| 68 | * |
| 69 | * @ri: Overall regulator data |
| 70 | * @rdev: Regulator device |
| 71 | * |
| 72 | * Return: 0 if no error, non-zero if there was an error writing the register. |
| 73 | */ |
| 74 | static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri, |
| 75 | struct regulator_dev *rdev) |
| 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 80 | MAX_OVERCURRENT_WAIT << CTRL_WT_BIT, |
| 81 | ri->overcurrent_wait << CTRL_WT_BIT); |
| 82 | if (ret) { |
| 83 | dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n", |
| 84 | rdev->desc->enable_reg); |
| 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 90 | /** |
| 91 | * tps65090_try_enable_fet - Try to enable a FET |
| 92 | * |
| 93 | * @rdev: Regulator device |
| 94 | * |
| 95 | * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get |
| 96 | * set, or some other -ve value if another error occurred (e.g. i2c error) |
| 97 | */ |
| 98 | static int tps65090_try_enable_fet(struct regulator_dev *rdev) |
| 99 | { |
| 100 | unsigned int control; |
| 101 | int ret, i; |
| 102 | |
| 103 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 104 | rdev->desc->enable_mask, |
| 105 | rdev->desc->enable_mask); |
| 106 | if (ret < 0) { |
| 107 | dev_err(&rdev->dev, "Error in updating reg %#x\n", |
| 108 | rdev->desc->enable_reg); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < MAX_CTRL_READ_TRIES; i++) { |
| 113 | ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, |
| 114 | &control); |
| 115 | if (ret < 0) |
| 116 | return ret; |
| 117 | |
| 118 | if (!(control & BIT(CTRL_TO_BIT))) |
| 119 | break; |
| 120 | |
| 121 | usleep_range(1000, 1500); |
| 122 | } |
| 123 | if (!(control & BIT(CTRL_PG_BIT))) |
| 124 | return -ENOTRECOVERABLE; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * tps65090_fet_enable - Enable a FET, trying a few times if it fails |
| 131 | * |
| 132 | * Some versions of the tps65090 have issues when turning on the FETs. |
| 133 | * This function goes through several steps to ensure the best chance of the |
| 134 | * FET going on. Specifically: |
| 135 | * - We'll make sure that we bump the "overcurrent wait" to the maximum, which |
| 136 | * increases the chances that we'll turn on properly. |
| 137 | * - We'll retry turning the FET on multiple times (turning off in between). |
| 138 | * |
| 139 | * @rdev: Regulator device |
| 140 | * |
| 141 | * Return: 0 if ok, non-zero if it fails. |
| 142 | */ |
| 143 | static int tps65090_fet_enable(struct regulator_dev *rdev) |
| 144 | { |
| 145 | int ret, tries; |
| 146 | |
| 147 | /* |
| 148 | * Try enabling multiple times until we succeed since sometimes the |
| 149 | * first try times out. |
| 150 | */ |
| 151 | tries = 0; |
| 152 | while (true) { |
| 153 | ret = tps65090_try_enable_fet(rdev); |
| 154 | if (!ret) |
| 155 | break; |
| 156 | if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES) |
| 157 | goto err; |
| 158 | |
| 159 | /* Try turning the FET off (and then on again) */ |
| 160 | ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 161 | rdev->desc->enable_mask, 0); |
| 162 | if (ret) |
| 163 | goto err; |
| 164 | |
| 165 | tries++; |
| 166 | } |
| 167 | |
| 168 | if (tries) |
| 169 | dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n", |
| 170 | rdev->desc->enable_reg, tries); |
| 171 | |
| 172 | return 0; |
| 173 | err: |
| 174 | dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg); |
| 175 | WARN_ON(1); |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static struct regulator_ops tps65090_reg_control_ops = { |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 181 | .enable = regulator_enable_regmap, |
| 182 | .disable = regulator_disable_regmap, |
| 183 | .is_enabled = regulator_is_enabled_regmap, |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 184 | }; |
| 185 | |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 186 | static struct regulator_ops tps65090_fet_control_ops = { |
| 187 | .enable = tps65090_fet_enable, |
| 188 | .disable = regulator_disable_regmap, |
| 189 | .is_enabled = regulator_is_enabled_regmap, |
| 190 | }; |
| 191 | |
Laxman Dewangan | 3a81ef8 | 2012-10-09 15:19:01 +0530 | [diff] [blame] | 192 | static struct regulator_ops tps65090_ldo_ops = { |
| 193 | }; |
| 194 | |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 195 | #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _ops) \ |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 196 | { \ |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 197 | .name = "TPS65090_RAILS"#_id, \ |
| 198 | .supply_name = _sname, \ |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 199 | .id = TPS65090_REGULATOR_##_id, \ |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 200 | .ops = &_ops, \ |
| 201 | .enable_reg = _en_reg, \ |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 202 | .enable_val = _en_bits, \ |
| 203 | .enable_mask = _en_bits, \ |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 204 | .type = REGULATOR_VOLTAGE, \ |
| 205 | .owner = THIS_MODULE, \ |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 208 | static struct regulator_desc tps65090_regulator_desc[] = { |
Doug Anderson | ed11f1e | 2014-04-23 08:56:05 -0700 | [diff] [blame^] | 209 | tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, BIT(CTRL_EN_BIT), |
| 210 | tps65090_reg_control_ops), |
| 211 | tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, BIT(CTRL_EN_BIT), |
| 212 | tps65090_reg_control_ops), |
| 213 | tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, BIT(CTRL_EN_BIT), |
| 214 | tps65090_reg_control_ops), |
| 215 | |
| 216 | tps65090_REG_DESC(FET1, "infet1", 0x0F, |
| 217 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 218 | tps65090_fet_control_ops), |
| 219 | tps65090_REG_DESC(FET2, "infet2", 0x10, |
| 220 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 221 | tps65090_fet_control_ops), |
| 222 | tps65090_REG_DESC(FET3, "infet3", 0x11, |
| 223 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 224 | tps65090_fet_control_ops), |
| 225 | tps65090_REG_DESC(FET4, "infet4", 0x12, |
| 226 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 227 | tps65090_fet_control_ops), |
| 228 | tps65090_REG_DESC(FET5, "infet5", 0x13, |
| 229 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 230 | tps65090_fet_control_ops), |
| 231 | tps65090_REG_DESC(FET6, "infet6", 0x14, |
| 232 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 233 | tps65090_fet_control_ops), |
| 234 | tps65090_REG_DESC(FET7, "infet7", 0x15, |
| 235 | BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT), |
| 236 | tps65090_fet_control_ops), |
| 237 | |
| 238 | tps65090_REG_DESC(LDO1, "vsys-l1", 0, 0, |
| 239 | tps65090_ldo_ops), |
| 240 | tps65090_REG_DESC(LDO2, "vsys-l2", 0, 0, |
| 241 | tps65090_ldo_ops), |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 242 | }; |
| 243 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 244 | static inline bool is_dcdc(int id) |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 245 | { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 246 | switch (id) { |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 247 | case TPS65090_REGULATOR_DCDC1: |
| 248 | case TPS65090_REGULATOR_DCDC2: |
| 249 | case TPS65090_REGULATOR_DCDC3: |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 250 | return true; |
| 251 | default: |
| 252 | return false; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 253 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 254 | } |
| 255 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 256 | static int tps65090_config_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 257 | struct tps65090_regulator *ri, bool enable) |
| 258 | { |
| 259 | int ret; |
| 260 | struct device *parent = ri->dev->parent; |
| 261 | unsigned int reg_en_reg = ri->desc->enable_reg; |
| 262 | |
| 263 | if (enable) |
| 264 | ret = tps65090_set_bits(parent, reg_en_reg, 1); |
| 265 | else |
| 266 | ret = tps65090_clr_bits(parent, reg_en_reg, 1); |
| 267 | if (ret < 0) |
| 268 | dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg); |
| 269 | return ret; |
| 270 | } |
| 271 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 272 | static int tps65090_regulator_disable_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 273 | struct tps65090_regulator *ri, |
| 274 | struct tps65090_regulator_plat_data *tps_pdata) |
| 275 | { |
| 276 | int ret = 0; |
| 277 | struct device *parent = ri->dev->parent; |
| 278 | unsigned int reg_en_reg = ri->desc->enable_reg; |
| 279 | |
| 280 | /* |
| 281 | * First enable output for internal control if require. |
| 282 | * And then disable external control. |
| 283 | */ |
| 284 | if (tps_pdata->reg_init_data->constraints.always_on || |
| 285 | tps_pdata->reg_init_data->constraints.boot_on) { |
| 286 | ret = tps65090_set_bits(parent, reg_en_reg, 0); |
| 287 | if (ret < 0) { |
| 288 | dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg); |
| 289 | return ret; |
| 290 | } |
| 291 | } |
| 292 | return tps65090_config_ext_control(ri, false); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 293 | } |
| 294 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 295 | static void tps65090_configure_regulator_config( |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 296 | struct tps65090_regulator_plat_data *tps_pdata, |
| 297 | struct regulator_config *config) |
| 298 | { |
| 299 | if (gpio_is_valid(tps_pdata->gpio)) { |
| 300 | int gpio_flag = GPIOF_OUT_INIT_LOW; |
| 301 | |
| 302 | if (tps_pdata->reg_init_data->constraints.always_on || |
| 303 | tps_pdata->reg_init_data->constraints.boot_on) |
| 304 | gpio_flag = GPIOF_OUT_INIT_HIGH; |
| 305 | |
| 306 | config->ena_gpio = tps_pdata->gpio; |
| 307 | config->ena_gpio_flags = gpio_flag; |
| 308 | } |
| 309 | } |
| 310 | |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 311 | #ifdef CONFIG_OF |
| 312 | static struct of_regulator_match tps65090_matches[] = { |
| 313 | { .name = "dcdc1", }, |
| 314 | { .name = "dcdc2", }, |
| 315 | { .name = "dcdc3", }, |
| 316 | { .name = "fet1", }, |
| 317 | { .name = "fet2", }, |
| 318 | { .name = "fet3", }, |
| 319 | { .name = "fet4", }, |
| 320 | { .name = "fet5", }, |
| 321 | { .name = "fet6", }, |
| 322 | { .name = "fet7", }, |
| 323 | { .name = "ldo1", }, |
| 324 | { .name = "ldo2", }, |
| 325 | }; |
| 326 | |
| 327 | static struct tps65090_platform_data *tps65090_parse_dt_reg_data( |
| 328 | struct platform_device *pdev, |
| 329 | struct of_regulator_match **tps65090_reg_matches) |
| 330 | { |
| 331 | struct tps65090_platform_data *tps65090_pdata; |
| 332 | struct device_node *np = pdev->dev.parent->of_node; |
| 333 | struct device_node *regulators; |
| 334 | int idx = 0, ret; |
| 335 | struct tps65090_regulator_plat_data *reg_pdata; |
| 336 | |
| 337 | tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata), |
| 338 | GFP_KERNEL); |
Sachin Kamat | 0ad91c6 | 2014-02-20 14:23:15 +0530 | [diff] [blame] | 339 | if (!tps65090_pdata) |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 340 | return ERR_PTR(-ENOMEM); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 341 | |
| 342 | reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * |
| 343 | sizeof(*reg_pdata), GFP_KERNEL); |
Sachin Kamat | 0ad91c6 | 2014-02-20 14:23:15 +0530 | [diff] [blame] | 344 | if (!reg_pdata) |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 345 | return ERR_PTR(-ENOMEM); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 346 | |
Laxman Dewangan | 4c850ea | 2013-10-08 19:31:02 +0530 | [diff] [blame] | 347 | regulators = of_get_child_by_name(np, "regulators"); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 348 | if (!regulators) { |
| 349 | dev_err(&pdev->dev, "regulator node not found\n"); |
| 350 | return ERR_PTR(-ENODEV); |
| 351 | } |
| 352 | |
Axel Lin | 09a228e | 2013-01-30 20:28:20 +0800 | [diff] [blame] | 353 | ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches, |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 354 | ARRAY_SIZE(tps65090_matches)); |
Sachin Kamat | 026cdfe | 2014-02-17 14:33:37 +0530 | [diff] [blame] | 355 | of_node_put(regulators); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 356 | if (ret < 0) { |
| 357 | dev_err(&pdev->dev, |
| 358 | "Error parsing regulator init data: %d\n", ret); |
| 359 | return ERR_PTR(ret); |
| 360 | } |
| 361 | |
| 362 | *tps65090_reg_matches = tps65090_matches; |
| 363 | for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) { |
| 364 | struct regulator_init_data *ri_data; |
| 365 | struct tps65090_regulator_plat_data *rpdata; |
| 366 | |
| 367 | rpdata = ®_pdata[idx]; |
| 368 | ri_data = tps65090_matches[idx].init_data; |
| 369 | if (!ri_data || !tps65090_matches[idx].of_node) |
| 370 | continue; |
| 371 | |
| 372 | rpdata->reg_init_data = ri_data; |
| 373 | rpdata->enable_ext_control = of_property_read_bool( |
| 374 | tps65090_matches[idx].of_node, |
| 375 | "ti,enable-ext-control"); |
| 376 | if (rpdata->enable_ext_control) |
| 377 | rpdata->gpio = of_get_named_gpio(np, |
| 378 | "dcdc-ext-control-gpios", 0); |
| 379 | |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 380 | if (of_property_read_u32(tps65090_matches[idx].of_node, |
| 381 | "ti,overcurrent-wait", |
| 382 | &rpdata->overcurrent_wait) == 0) |
| 383 | rpdata->overcurrent_wait_valid = true; |
| 384 | |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 385 | tps65090_pdata->reg_pdata[idx] = rpdata; |
| 386 | } |
| 387 | return tps65090_pdata; |
| 388 | } |
| 389 | #else |
| 390 | static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data( |
| 391 | struct platform_device *pdev, |
| 392 | struct of_regulator_match **tps65090_reg_matches) |
| 393 | { |
| 394 | *tps65090_reg_matches = NULL; |
| 395 | return NULL; |
| 396 | } |
| 397 | #endif |
| 398 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 399 | static int tps65090_regulator_probe(struct platform_device *pdev) |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 400 | { |
Axel Lin | 06c4998 | 2012-04-17 17:08:56 +0800 | [diff] [blame] | 401 | struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 402 | struct tps65090_regulator *ri = NULL; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 403 | struct regulator_config config = { }; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 404 | struct regulator_dev *rdev; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 405 | struct tps65090_regulator_plat_data *tps_pdata; |
| 406 | struct tps65090_regulator *pmic; |
| 407 | struct tps65090_platform_data *tps65090_pdata; |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 408 | struct of_regulator_match *tps65090_reg_matches = NULL; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 409 | int num; |
| 410 | int ret; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 411 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 412 | dev_dbg(&pdev->dev, "Probing regulator\n"); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 413 | |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 414 | tps65090_pdata = dev_get_platdata(pdev->dev.parent); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 415 | if (!tps65090_pdata && tps65090_mfd->dev->of_node) |
| 416 | tps65090_pdata = tps65090_parse_dt_reg_data(pdev, |
| 417 | &tps65090_reg_matches); |
| 418 | if (IS_ERR_OR_NULL(tps65090_pdata)) { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 419 | dev_err(&pdev->dev, "Platform data missing\n"); |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 420 | return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 421 | } |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 422 | |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 423 | pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic), |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 424 | GFP_KERNEL); |
Sachin Kamat | 0ad91c6 | 2014-02-20 14:23:15 +0530 | [diff] [blame] | 425 | if (!pmic) |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 426 | return -ENOMEM; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 427 | |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 428 | for (num = 0; num < TPS65090_REGULATOR_MAX; num++) { |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 429 | tps_pdata = tps65090_pdata->reg_pdata[num]; |
| 430 | |
| 431 | ri = &pmic[num]; |
| 432 | ri->dev = &pdev->dev; |
| 433 | ri->desc = &tps65090_regulator_desc[num]; |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 434 | ri->overcurrent_wait_valid = tps_pdata->overcurrent_wait_valid; |
| 435 | ri->overcurrent_wait = tps_pdata->overcurrent_wait; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * TPS5090 DCDC support the control from external digital input. |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 439 | * Configure it as per platform data. |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 440 | */ |
| 441 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) { |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 442 | if (tps_pdata->enable_ext_control) { |
| 443 | tps65090_configure_regulator_config( |
| 444 | tps_pdata, &config); |
| 445 | ri->desc->ops = &tps65090_ext_control_ops; |
| 446 | } else { |
| 447 | ret = tps65090_regulator_disable_ext_control( |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 448 | ri, tps_pdata); |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 449 | if (ret < 0) { |
| 450 | dev_err(&pdev->dev, |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 451 | "failed disable ext control\n"); |
Sachin Kamat | 9738efa | 2013-09-04 17:17:48 +0530 | [diff] [blame] | 452 | return ret; |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 453 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 454 | } |
| 455 | } |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 456 | |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 457 | config.dev = pdev->dev.parent; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 458 | config.driver_data = ri; |
| 459 | config.regmap = tps65090_mfd->rmap; |
| 460 | if (tps_pdata) |
| 461 | config.init_data = tps_pdata->reg_init_data; |
| 462 | else |
| 463 | config.init_data = NULL; |
Laxman Dewangan | 6c7a7a0 | 2013-01-29 14:35:16 +0530 | [diff] [blame] | 464 | if (tps65090_reg_matches) |
| 465 | config.of_node = tps65090_reg_matches[num].of_node; |
| 466 | else |
| 467 | config.of_node = NULL; |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 468 | |
Sachin Kamat | 9738efa | 2013-09-04 17:17:48 +0530 | [diff] [blame] | 469 | rdev = devm_regulator_register(&pdev->dev, ri->desc, &config); |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 470 | if (IS_ERR(rdev)) { |
| 471 | dev_err(&pdev->dev, "failed to register regulator %s\n", |
| 472 | ri->desc->name); |
Sachin Kamat | 9738efa | 2013-09-04 17:17:48 +0530 | [diff] [blame] | 473 | return PTR_ERR(rdev); |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 474 | } |
| 475 | ri->rdev = rdev; |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 476 | |
Doug Anderson | 2904144 | 2014-04-16 16:12:28 -0700 | [diff] [blame] | 477 | if (ri->overcurrent_wait_valid) { |
| 478 | ret = tps65090_reg_set_overcurrent_wait(ri, rdev); |
| 479 | if (ret < 0) |
| 480 | return ret; |
| 481 | } |
| 482 | |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 483 | /* Enable external control if it is require */ |
| 484 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data && |
| 485 | tps_pdata->enable_ext_control) { |
| 486 | ret = tps65090_config_ext_control(ri, true); |
Sachin Kamat | 9738efa | 2013-09-04 17:17:48 +0530 | [diff] [blame] | 487 | if (ret < 0) |
| 488 | return ret; |
Laxman Dewangan | f329b17 | 2012-10-09 15:19:02 +0530 | [diff] [blame] | 489 | } |
Laxman Dewangan | 24282a1 | 2012-10-09 15:18:59 +0530 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | platform_set_drvdata(pdev, pmic); |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 493 | return 0; |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static struct platform_driver tps65090_regulator_driver = { |
| 497 | .driver = { |
Laxman Dewangan | 8620ca9 | 2012-10-09 15:19:00 +0530 | [diff] [blame] | 498 | .name = "tps65090-pmic", |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 499 | .owner = THIS_MODULE, |
| 500 | }, |
| 501 | .probe = tps65090_regulator_probe, |
Venu Byravarasu | 452534e | 2012-03-22 18:34:09 +0530 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | static int __init tps65090_regulator_init(void) |
| 505 | { |
| 506 | return platform_driver_register(&tps65090_regulator_driver); |
| 507 | } |
| 508 | subsys_initcall(tps65090_regulator_init); |
| 509 | |
| 510 | static void __exit tps65090_regulator_exit(void) |
| 511 | { |
| 512 | platform_driver_unregister(&tps65090_regulator_driver); |
| 513 | } |
| 514 | module_exit(tps65090_regulator_exit); |
| 515 | |
| 516 | MODULE_DESCRIPTION("tps65090 regulator driver"); |
| 517 | MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>"); |
| 518 | MODULE_LICENSE("GPL v2"); |
Axel Lin | d95420b | 2012-11-23 23:47:16 +0800 | [diff] [blame] | 519 | MODULE_ALIAS("platform:tps65090-pmic"); |