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