Thomas Gleixner | e634cf4 | 2022-06-07 16:11:30 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 2 | /* |
| 3 | * tps62360.c -- TI tps62360 |
| 4 | * |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 5 | * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363. |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 6 | * |
| 7 | * Copyright (c) 2012, NVIDIA Corporation. |
| 8 | * |
| 9 | * Author: Laxman Dewangan <ldewangan@nvidia.com> |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/err.h> |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 18 | #include <linux/regulator/of_regulator.h> |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/regulator/driver.h> |
| 21 | #include <linux/regulator/machine.h> |
| 22 | #include <linux/regulator/tps62360.h> |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 24 | #include <linux/i2c.h> |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/regmap.h> |
| 27 | |
| 28 | /* Register definitions */ |
| 29 | #define REG_VSET0 0 |
| 30 | #define REG_VSET1 1 |
| 31 | #define REG_VSET2 2 |
| 32 | #define REG_VSET3 3 |
| 33 | #define REG_CONTROL 4 |
| 34 | #define REG_TEMP 5 |
| 35 | #define REG_RAMPCTRL 6 |
| 36 | #define REG_CHIPID 8 |
| 37 | |
Laxman Dewangan | 9a00630 | 2012-05-14 17:46:51 +0530 | [diff] [blame] | 38 | #define FORCE_PWM_ENABLE BIT(7) |
| 39 | |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 40 | enum chips {TPS62360, TPS62361, TPS62362, TPS62363}; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 41 | |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 42 | #define TPS62360_BASE_VOLTAGE 770000 |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 43 | #define TPS62360_N_VOLTAGES 64 |
| 44 | |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 45 | #define TPS62361_BASE_VOLTAGE 500000 |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 46 | #define TPS62361_N_VOLTAGES 128 |
| 47 | |
| 48 | /* tps 62360 chip information */ |
| 49 | struct tps62360_chip { |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 50 | struct device *dev; |
| 51 | struct regulator_desc desc; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 52 | struct regulator_dev *rdev; |
| 53 | struct regmap *regmap; |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 54 | struct gpio_desc *vsel0_gpio; |
| 55 | struct gpio_desc *vsel1_gpio; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 56 | u8 voltage_reg_mask; |
| 57 | bool en_internal_pulldn; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 58 | bool en_discharge; |
| 59 | bool valid_gpios; |
| 60 | int lru_index[4]; |
| 61 | int curr_vset_vsel[4]; |
| 62 | int curr_vset_id; |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | * find_voltage_set_register: Find new voltage configuration register |
| 67 | * (VSET) id. |
| 68 | * The finding of the new VSET register will be based on the LRU mechanism. |
| 69 | * Each VSET register will have different voltage configured . This |
| 70 | * Function will look if any of the VSET register have requested voltage set |
| 71 | * or not. |
| 72 | * - If it is already there then it will make that register as most |
| 73 | * recently used and return as found so that caller need not to set |
| 74 | * the VSET register but need to set the proper gpios to select this |
| 75 | * VSET register. |
| 76 | * - If requested voltage is not found then it will use the least |
| 77 | * recently mechanism to get new VSET register for new configuration |
| 78 | * and will return not_found so that caller need to set new VSET |
| 79 | * register and then gpios (both). |
| 80 | */ |
| 81 | static bool find_voltage_set_register(struct tps62360_chip *tps, |
| 82 | int req_vsel, int *vset_reg_id) |
| 83 | { |
| 84 | int i; |
| 85 | bool found = false; |
| 86 | int new_vset_reg = tps->lru_index[3]; |
| 87 | int found_index = 3; |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 88 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 89 | for (i = 0; i < 4; ++i) { |
| 90 | if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) { |
| 91 | new_vset_reg = tps->lru_index[i]; |
| 92 | found_index = i; |
| 93 | found = true; |
| 94 | goto update_lru_index; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | update_lru_index: |
| 99 | for (i = found_index; i > 0; i--) |
| 100 | tps->lru_index[i] = tps->lru_index[i - 1]; |
| 101 | |
| 102 | tps->lru_index[0] = new_vset_reg; |
| 103 | *vset_reg_id = new_vset_reg; |
| 104 | return found; |
| 105 | } |
| 106 | |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 107 | static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev) |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 108 | { |
| 109 | struct tps62360_chip *tps = rdev_get_drvdata(dev); |
| 110 | int vsel; |
| 111 | unsigned int data; |
| 112 | int ret; |
| 113 | |
| 114 | ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data); |
| 115 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 116 | dev_err(tps->dev, "%s(): register %d read failed with err %d\n", |
| 117 | __func__, REG_VSET0 + tps->curr_vset_id, ret); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 118 | return ret; |
| 119 | } |
| 120 | vsel = (int)data & tps->voltage_reg_mask; |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 121 | return vsel; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 122 | } |
| 123 | |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 124 | static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev, |
| 125 | unsigned selector) |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 126 | { |
| 127 | struct tps62360_chip *tps = rdev_get_drvdata(dev); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 128 | int ret; |
| 129 | bool found = false; |
| 130 | int new_vset_id = tps->curr_vset_id; |
| 131 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 132 | /* |
| 133 | * If gpios are available to select the VSET register then least |
| 134 | * recently used register for new configuration. |
| 135 | */ |
| 136 | if (tps->valid_gpios) |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 137 | found = find_voltage_set_register(tps, selector, &new_vset_id); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 138 | |
| 139 | if (!found) { |
| 140 | ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id, |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 141 | tps->voltage_reg_mask, selector); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 142 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 143 | dev_err(tps->dev, |
| 144 | "%s(): register %d update failed with err %d\n", |
| 145 | __func__, REG_VSET0 + new_vset_id, ret); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | tps->curr_vset_id = new_vset_id; |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 149 | tps->curr_vset_vsel[new_vset_id] = selector; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Select proper VSET register vio gpios */ |
| 153 | if (tps->valid_gpios) { |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 154 | gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1); |
| 155 | gpiod_set_value_cansleep(tps->vsel1_gpio, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 156 | (new_vset_id >> 1) & 0x1); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
Laxman Dewangan | 9a00630 | 2012-05-14 17:46:51 +0530 | [diff] [blame] | 161 | static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 162 | { |
| 163 | struct tps62360_chip *tps = rdev_get_drvdata(rdev); |
| 164 | int i; |
| 165 | int val; |
| 166 | int ret; |
| 167 | |
| 168 | /* Enable force PWM mode in FAST mode only. */ |
| 169 | switch (mode) { |
| 170 | case REGULATOR_MODE_FAST: |
| 171 | val = FORCE_PWM_ENABLE; |
| 172 | break; |
| 173 | |
| 174 | case REGULATOR_MODE_NORMAL: |
| 175 | val = 0; |
| 176 | break; |
| 177 | |
| 178 | default: |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | if (!tps->valid_gpios) { |
| 183 | ret = regmap_update_bits(tps->regmap, |
| 184 | REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val); |
| 185 | if (ret < 0) |
| 186 | dev_err(tps->dev, |
| 187 | "%s(): register %d update failed with err %d\n", |
| 188 | __func__, REG_VSET0 + tps->curr_vset_id, ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* If gpios are valid then all register set need to be control */ |
| 193 | for (i = 0; i < 4; ++i) { |
| 194 | ret = regmap_update_bits(tps->regmap, |
| 195 | REG_VSET0 + i, FORCE_PWM_ENABLE, val); |
| 196 | if (ret < 0) { |
| 197 | dev_err(tps->dev, |
| 198 | "%s(): register %d update failed with err %d\n", |
| 199 | __func__, REG_VSET0 + i, ret); |
| 200 | return ret; |
| 201 | } |
| 202 | } |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static unsigned int tps62360_get_mode(struct regulator_dev *rdev) |
| 207 | { |
| 208 | struct tps62360_chip *tps = rdev_get_drvdata(rdev); |
| 209 | unsigned int data; |
| 210 | int ret; |
| 211 | |
| 212 | ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data); |
| 213 | if (ret < 0) { |
| 214 | dev_err(tps->dev, "%s(): register %d read failed with err %d\n", |
| 215 | __func__, REG_VSET0 + tps->curr_vset_id, ret); |
| 216 | return ret; |
| 217 | } |
| 218 | return (data & FORCE_PWM_ENABLE) ? |
| 219 | REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL; |
| 220 | } |
| 221 | |
Rikard Falkeborn | 01167e8 | 2020-08-30 00:10:59 +0200 | [diff] [blame] | 222 | static const struct regulator_ops tps62360_dcdc_ops = { |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 223 | .get_voltage_sel = tps62360_dcdc_get_voltage_sel, |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 224 | .set_voltage_sel = tps62360_dcdc_set_voltage_sel, |
Axel Lin | 7f225ba | 2012-05-14 11:25:42 +0800 | [diff] [blame] | 225 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 41097af | 2012-05-14 11:27:25 +0800 | [diff] [blame] | 226 | .map_voltage = regulator_map_voltage_linear, |
Axel Lin | 0072f0a | 2012-06-20 22:55:50 +0800 | [diff] [blame] | 227 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Laxman Dewangan | 9a00630 | 2012-05-14 17:46:51 +0530 | [diff] [blame] | 228 | .set_mode = tps62360_set_mode, |
| 229 | .get_mode = tps62360_get_mode, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 230 | }; |
| 231 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 232 | static int tps62360_init_dcdc(struct tps62360_chip *tps, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 233 | struct tps62360_regulator_platform_data *pdata) |
| 234 | { |
| 235 | int ret; |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 236 | unsigned int ramp_ctrl; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 237 | |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 238 | /* Initialize internal pull up/down control */ |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 239 | if (tps->en_internal_pulldn) |
| 240 | ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0); |
| 241 | else |
| 242 | ret = regmap_write(tps->regmap, REG_CONTROL, 0x0); |
| 243 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 244 | dev_err(tps->dev, |
| 245 | "%s(): register %d write failed with err %d\n", |
| 246 | __func__, REG_CONTROL, ret); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 250 | /* Reset output discharge path to reduce power consumption */ |
| 251 | ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0); |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 252 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 253 | dev_err(tps->dev, |
| 254 | "%s(): register %d update failed with err %d\n", |
| 255 | __func__, REG_RAMPCTRL, ret); |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | /* Get ramp value from ramp control register */ |
| 260 | ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl); |
| 261 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 262 | dev_err(tps->dev, |
| 263 | "%s(): register %d read failed with err %d\n", |
| 264 | __func__, REG_RAMPCTRL, ret); |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 265 | return ret; |
| 266 | } |
Axel Lin | 1864b67 | 2013-04-23 12:35:53 +0800 | [diff] [blame] | 267 | ramp_ctrl = (ramp_ctrl >> 5) & 0x7; |
Laxman Dewangan | a60cfce | 2012-05-07 18:08:26 +0530 | [diff] [blame] | 268 | |
| 269 | /* ramp mV/us = 32/(2^ramp_ctrl) */ |
Axel Lin | 0072f0a | 2012-06-20 22:55:50 +0800 | [diff] [blame] | 270 | tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl)); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static const struct regmap_config tps62360_regmap_config = { |
Laxman Dewangan | 16ea003 | 2012-05-07 18:08:25 +0530 | [diff] [blame] | 275 | .reg_bits = 8, |
| 276 | .val_bits = 8, |
| 277 | .max_register = REG_CHIPID, |
Bo Liu | fe258f5 | 2024-03-20 04:57:39 -0400 | [diff] [blame] | 278 | .cache_type = REGCACHE_MAPLE, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 279 | }; |
| 280 | |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 281 | static struct tps62360_regulator_platform_data * |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 282 | of_get_tps62360_platform_data(struct device *dev, |
| 283 | const struct regulator_desc *desc) |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 284 | { |
| 285 | struct tps62360_regulator_platform_data *pdata; |
| 286 | struct device_node *np = dev->of_node; |
| 287 | |
| 288 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
Sachin Kamat | 33e63ba | 2014-02-20 14:23:13 +0530 | [diff] [blame] | 289 | if (!pdata) |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 290 | return NULL; |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 291 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 292 | pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node, |
| 293 | desc); |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 294 | if (!pdata->reg_init_data) { |
| 295 | dev_err(dev, "Not able to get OF regulator init data\n"); |
| 296 | return NULL; |
| 297 | } |
| 298 | |
Rob Herring | 5bd73a1 | 2023-03-10 08:47:22 -0600 | [diff] [blame] | 299 | pdata->vsel0_def_state = of_property_read_bool(np, "ti,vsel0-state-high"); |
| 300 | pdata->vsel1_def_state = of_property_read_bool(np, "ti,vsel1-state-high"); |
| 301 | pdata->en_internal_pulldn = of_property_read_bool(np, "ti,enable-pull-down"); |
| 302 | pdata->en_discharge = of_property_read_bool(np, "ti,enable-vout-discharge"); |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 303 | |
| 304 | return pdata; |
| 305 | } |
| 306 | |
| 307 | #if defined(CONFIG_OF) |
| 308 | static const struct of_device_id tps62360_of_match[] = { |
| 309 | { .compatible = "ti,tps62360", .data = (void *)TPS62360}, |
| 310 | { .compatible = "ti,tps62361", .data = (void *)TPS62361}, |
| 311 | { .compatible = "ti,tps62362", .data = (void *)TPS62362}, |
| 312 | { .compatible = "ti,tps62363", .data = (void *)TPS62363}, |
| 313 | {}, |
Axel Lin | be15411 | 2012-05-14 23:58:08 +0800 | [diff] [blame] | 314 | }; |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 315 | MODULE_DEVICE_TABLE(of, tps62360_of_match); |
| 316 | #endif |
| 317 | |
Uwe Kleine-König | 1880416 | 2022-11-18 23:44:51 +0100 | [diff] [blame] | 318 | static int tps62360_probe(struct i2c_client *client) |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 319 | { |
Uwe Kleine-König | 1880416 | 2022-11-18 23:44:51 +0100 | [diff] [blame] | 320 | const struct i2c_device_id *id = i2c_client_get_device_id(client); |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 321 | struct regulator_config config = { }; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 322 | struct tps62360_regulator_platform_data *pdata; |
| 323 | struct regulator_dev *rdev; |
| 324 | struct tps62360_chip *tps; |
| 325 | int ret; |
| 326 | int i; |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 327 | int chip_id; |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 328 | int gpio_flags; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 329 | |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 330 | pdata = dev_get_platdata(&client->dev); |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 331 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 332 | tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); |
| 333 | if (!tps) |
| 334 | return -ENOMEM; |
| 335 | |
| 336 | tps->desc.name = client->name; |
| 337 | tps->desc.id = 0; |
| 338 | tps->desc.ops = &tps62360_dcdc_ops; |
| 339 | tps->desc.type = REGULATOR_VOLTAGE; |
| 340 | tps->desc.owner = THIS_MODULE; |
| 341 | tps->desc.uV_step = 10000; |
| 342 | |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 343 | if (client->dev.of_node) { |
| 344 | const struct of_device_id *match; |
| 345 | match = of_match_device(of_match_ptr(tps62360_of_match), |
| 346 | &client->dev); |
| 347 | if (!match) { |
| 348 | dev_err(&client->dev, "Error: No device match found\n"); |
| 349 | return -ENODEV; |
| 350 | } |
David Howells | 541f597 | 2014-01-03 16:07:55 +0000 | [diff] [blame] | 351 | chip_id = (int)(long)match->data; |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 352 | if (!pdata) |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 353 | pdata = of_get_tps62360_platform_data(&client->dev, |
| 354 | &tps->desc); |
Tuomas Tynkkynen | 0a62d03 | 2013-06-18 13:14:41 +0300 | [diff] [blame] | 355 | } else if (id) { |
| 356 | chip_id = id->driver_data; |
| 357 | } else { |
| 358 | dev_err(&client->dev, "No device tree match or id table match found\n"); |
| 359 | return -ENODEV; |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 360 | } |
| 361 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 362 | if (!pdata) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 363 | dev_err(&client->dev, "%s(): Platform data not found\n", |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 364 | __func__); |
| 365 | return -EIO; |
| 366 | } |
| 367 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 368 | tps->en_discharge = pdata->en_discharge; |
| 369 | tps->en_internal_pulldn = pdata->en_internal_pulldn; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 370 | tps->dev = &client->dev; |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 371 | |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 372 | switch (chip_id) { |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 373 | case TPS62360: |
| 374 | case TPS62362: |
Axel Lin | b515241 | 2012-06-14 09:38:26 +0800 | [diff] [blame] | 375 | tps->desc.min_uV = TPS62360_BASE_VOLTAGE; |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 376 | tps->voltage_reg_mask = 0x3F; |
| 377 | tps->desc.n_voltages = TPS62360_N_VOLTAGES; |
| 378 | break; |
| 379 | case TPS62361: |
| 380 | case TPS62363: |
Axel Lin | b515241 | 2012-06-14 09:38:26 +0800 | [diff] [blame] | 381 | tps->desc.min_uV = TPS62361_BASE_VOLTAGE; |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 382 | tps->voltage_reg_mask = 0x7F; |
| 383 | tps->desc.n_voltages = TPS62361_N_VOLTAGES; |
| 384 | break; |
| 385 | default: |
| 386 | return -ENODEV; |
| 387 | } |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 388 | |
Axel Lin | 9a4bdd8 | 2012-04-07 23:29:56 +0800 | [diff] [blame] | 389 | tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 390 | if (IS_ERR(tps->regmap)) { |
| 391 | ret = PTR_ERR(tps->regmap); |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 392 | dev_err(&client->dev, |
| 393 | "%s(): regmap allocation failed with err %d\n", |
| 394 | __func__, ret); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 395 | return ret; |
| 396 | } |
| 397 | i2c_set_clientdata(client, tps); |
| 398 | |
| 399 | tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 + |
| 400 | (pdata->vsel0_def_state & 1); |
| 401 | tps->lru_index[0] = tps->curr_vset_id; |
| 402 | tps->valid_gpios = false; |
| 403 | |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 404 | gpio_flags = (pdata->vsel0_def_state) ? |
| 405 | GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 406 | tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags); |
| 407 | if (IS_ERR(tps->vsel0_gpio)) { |
| 408 | dev_err(&client->dev, |
| 409 | "%s(): Could not obtain vsel0 GPIO: %ld\n", |
| 410 | __func__, PTR_ERR(tps->vsel0_gpio)); |
| 411 | return PTR_ERR(tps->vsel0_gpio); |
| 412 | } |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 413 | |
Maíra Canal | 6a8b5bb | 2021-10-17 15:06:39 -0300 | [diff] [blame] | 414 | gpio_flags = (pdata->vsel1_def_state) ? |
| 415 | GPIOD_OUT_HIGH : GPIOD_OUT_LOW; |
| 416 | tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags); |
| 417 | if (IS_ERR(tps->vsel1_gpio)) { |
| 418 | dev_err(&client->dev, |
| 419 | "%s(): Could not obtain vsel1 GPIO: %ld\n", |
| 420 | __func__, PTR_ERR(tps->vsel1_gpio)); |
| 421 | return PTR_ERR(tps->vsel1_gpio); |
| 422 | } |
| 423 | |
| 424 | if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) { |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 425 | tps->valid_gpios = true; |
| 426 | |
| 427 | /* |
| 428 | * Initialize the lru index with vset_reg id |
| 429 | * The index 0 will be most recently used and |
| 430 | * set with the tps->curr_vset_id */ |
| 431 | for (i = 0; i < 4; ++i) |
| 432 | tps->lru_index[i] = i; |
| 433 | tps->lru_index[0] = tps->curr_vset_id; |
| 434 | tps->lru_index[tps->curr_vset_id] = 0; |
| 435 | } |
| 436 | |
| 437 | ret = tps62360_init_dcdc(tps, pdata); |
| 438 | if (ret < 0) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 439 | dev_err(tps->dev, "%s(): Init failed with err = %d\n", |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 440 | __func__, ret); |
Laxman Dewangan | 8a8e3d5 | 2012-07-02 15:35:48 +0530 | [diff] [blame] | 441 | return ret; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 442 | } |
| 443 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 444 | config.dev = &client->dev; |
Laxman Dewangan | 8bdca009 | 2012-05-11 12:08:42 +0530 | [diff] [blame] | 445 | config.init_data = pdata->reg_init_data; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 446 | config.driver_data = tps; |
Laxman Dewangan | 9fc3815e | 2012-05-20 21:48:47 +0530 | [diff] [blame] | 447 | config.of_node = client->dev.of_node; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 448 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 449 | /* Register the regulators */ |
Sachin Kamat | 58c6e93 | 2013-09-04 17:17:45 +0530 | [diff] [blame] | 450 | rdev = devm_regulator_register(&client->dev, &tps->desc, &config); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 451 | if (IS_ERR(rdev)) { |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 452 | dev_err(tps->dev, |
| 453 | "%s(): regulator register failed with err %s\n", |
| 454 | __func__, id->name); |
Laxman Dewangan | 8a8e3d5 | 2012-07-02 15:35:48 +0530 | [diff] [blame] | 455 | return PTR_ERR(rdev); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | tps->rdev = rdev; |
| 459 | return 0; |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 460 | } |
| 461 | |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 462 | static void tps62360_shutdown(struct i2c_client *client) |
| 463 | { |
| 464 | struct tps62360_chip *tps = i2c_get_clientdata(client); |
| 465 | int st; |
| 466 | |
| 467 | if (!tps->en_discharge) |
| 468 | return; |
| 469 | |
| 470 | /* Configure the output discharge path */ |
| 471 | st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2)); |
| 472 | if (st < 0) |
Laxman Dewangan | 2935fb1 | 2012-05-08 17:05:58 +0530 | [diff] [blame] | 473 | dev_err(tps->dev, |
| 474 | "%s(): register %d update failed with err %d\n", |
| 475 | __func__, REG_RAMPCTRL, st); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static const struct i2c_device_id tps62360_id[] = { |
| 479 | {.name = "tps62360", .driver_data = TPS62360}, |
| 480 | {.name = "tps62361", .driver_data = TPS62361}, |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 481 | {.name = "tps62362", .driver_data = TPS62362}, |
| 482 | {.name = "tps62363", .driver_data = TPS62363}, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 483 | {}, |
| 484 | }; |
| 485 | |
| 486 | MODULE_DEVICE_TABLE(i2c, tps62360_id); |
| 487 | |
| 488 | static struct i2c_driver tps62360_i2c_driver = { |
| 489 | .driver = { |
| 490 | .name = "tps62360", |
Douglas Anderson | 259b93b | 2023-03-16 12:54:38 -0700 | [diff] [blame] | 491 | .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
Laxman Dewangan | 684ae39 | 2012-05-11 12:08:43 +0530 | [diff] [blame] | 492 | .of_match_table = of_match_ptr(tps62360_of_match), |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 493 | }, |
Uwe Kleine-König | 964e186 | 2023-05-06 00:02:18 +0200 | [diff] [blame] | 494 | .probe = tps62360_probe, |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 495 | .shutdown = tps62360_shutdown, |
| 496 | .id_table = tps62360_id, |
| 497 | }; |
| 498 | |
| 499 | static int __init tps62360_init(void) |
| 500 | { |
| 501 | return i2c_add_driver(&tps62360_i2c_driver); |
| 502 | } |
| 503 | subsys_initcall(tps62360_init); |
| 504 | |
| 505 | static void __exit tps62360_cleanup(void) |
| 506 | { |
| 507 | i2c_del_driver(&tps62360_i2c_driver); |
| 508 | } |
| 509 | module_exit(tps62360_cleanup); |
| 510 | |
| 511 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
Axel Lin | d1cf4f6 | 2012-04-02 18:19:28 +0800 | [diff] [blame] | 512 | MODULE_DESCRIPTION("TPS6236x voltage regulator driver"); |
Laxman Dewangan | 6219929 | 2012-01-09 20:27:41 +0530 | [diff] [blame] | 513 | MODULE_LICENSE("GPL v2"); |