16219929fSLaxman Dewangan /* 26219929fSLaxman Dewangan * tps62360.c -- TI tps62360 36219929fSLaxman Dewangan * 4d1cf4f65SAxel Lin * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363. 56219929fSLaxman Dewangan * 66219929fSLaxman Dewangan * Copyright (c) 2012, NVIDIA Corporation. 76219929fSLaxman Dewangan * 86219929fSLaxman Dewangan * Author: Laxman Dewangan <ldewangan@nvidia.com> 96219929fSLaxman Dewangan * 106219929fSLaxman Dewangan * This program is free software; you can redistribute it and/or 116219929fSLaxman Dewangan * modify it under the terms of the GNU General Public License as 126219929fSLaxman Dewangan * published by the Free Software Foundation version 2. 136219929fSLaxman Dewangan * 146219929fSLaxman Dewangan * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind, 156219929fSLaxman Dewangan * whether express or implied; without even the implied warranty of 166219929fSLaxman Dewangan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 176219929fSLaxman Dewangan * General Public License for more details. 186219929fSLaxman Dewangan * 196219929fSLaxman Dewangan * You should have received a copy of the GNU General Public License 206219929fSLaxman Dewangan * along with this program; if not, write to the Free Software 216219929fSLaxman Dewangan * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 226219929fSLaxman Dewangan * 02111-1307, USA 236219929fSLaxman Dewangan */ 246219929fSLaxman Dewangan 256219929fSLaxman Dewangan #include <linux/kernel.h> 266219929fSLaxman Dewangan #include <linux/module.h> 276219929fSLaxman Dewangan #include <linux/init.h> 286219929fSLaxman Dewangan #include <linux/err.h> 29*684ae39bSLaxman Dewangan #include <linux/of.h> 30*684ae39bSLaxman Dewangan #include <linux/of_device.h> 31*684ae39bSLaxman Dewangan #include <linux/of_gpio.h> 32*684ae39bSLaxman Dewangan #include <linux/regulator/of_regulator.h> 336219929fSLaxman Dewangan #include <linux/platform_device.h> 346219929fSLaxman Dewangan #include <linux/regulator/driver.h> 356219929fSLaxman Dewangan #include <linux/regulator/machine.h> 366219929fSLaxman Dewangan #include <linux/regulator/tps62360.h> 376219929fSLaxman Dewangan #include <linux/gpio.h> 386219929fSLaxman Dewangan #include <linux/i2c.h> 396219929fSLaxman Dewangan #include <linux/slab.h> 406219929fSLaxman Dewangan #include <linux/regmap.h> 416219929fSLaxman Dewangan 426219929fSLaxman Dewangan /* Register definitions */ 436219929fSLaxman Dewangan #define REG_VSET0 0 446219929fSLaxman Dewangan #define REG_VSET1 1 456219929fSLaxman Dewangan #define REG_VSET2 2 466219929fSLaxman Dewangan #define REG_VSET3 3 476219929fSLaxman Dewangan #define REG_CONTROL 4 486219929fSLaxman Dewangan #define REG_TEMP 5 496219929fSLaxman Dewangan #define REG_RAMPCTRL 6 506219929fSLaxman Dewangan #define REG_CHIPID 8 516219929fSLaxman Dewangan 52d1cf4f65SAxel Lin enum chips {TPS62360, TPS62361, TPS62362, TPS62363}; 536219929fSLaxman Dewangan 542935fb18SLaxman Dewangan #define TPS62360_BASE_VOLTAGE 770000 556219929fSLaxman Dewangan #define TPS62360_N_VOLTAGES 64 566219929fSLaxman Dewangan 572935fb18SLaxman Dewangan #define TPS62361_BASE_VOLTAGE 500000 586219929fSLaxman Dewangan #define TPS62361_N_VOLTAGES 128 596219929fSLaxman Dewangan 606219929fSLaxman Dewangan /* tps 62360 chip information */ 616219929fSLaxman Dewangan struct tps62360_chip { 626219929fSLaxman Dewangan struct device *dev; 636219929fSLaxman Dewangan struct regulator_desc desc; 646219929fSLaxman Dewangan struct regulator_dev *rdev; 656219929fSLaxman Dewangan struct regmap *regmap; 666219929fSLaxman Dewangan int chip_id; 676219929fSLaxman Dewangan int vsel0_gpio; 686219929fSLaxman Dewangan int vsel1_gpio; 696219929fSLaxman Dewangan int voltage_base; 706219929fSLaxman Dewangan u8 voltage_reg_mask; 716219929fSLaxman Dewangan bool en_internal_pulldn; 726219929fSLaxman Dewangan bool en_force_pwm; 736219929fSLaxman Dewangan bool en_discharge; 746219929fSLaxman Dewangan bool valid_gpios; 756219929fSLaxman Dewangan int lru_index[4]; 766219929fSLaxman Dewangan int curr_vset_vsel[4]; 776219929fSLaxman Dewangan int curr_vset_id; 78a60cfce0SLaxman Dewangan int change_uv_per_us; 796219929fSLaxman Dewangan }; 806219929fSLaxman Dewangan 816219929fSLaxman Dewangan /* 826219929fSLaxman Dewangan * find_voltage_set_register: Find new voltage configuration register 836219929fSLaxman Dewangan * (VSET) id. 846219929fSLaxman Dewangan * The finding of the new VSET register will be based on the LRU mechanism. 856219929fSLaxman Dewangan * Each VSET register will have different voltage configured . This 866219929fSLaxman Dewangan * Function will look if any of the VSET register have requested voltage set 876219929fSLaxman Dewangan * or not. 886219929fSLaxman Dewangan * - If it is already there then it will make that register as most 896219929fSLaxman Dewangan * recently used and return as found so that caller need not to set 906219929fSLaxman Dewangan * the VSET register but need to set the proper gpios to select this 916219929fSLaxman Dewangan * VSET register. 926219929fSLaxman Dewangan * - If requested voltage is not found then it will use the least 936219929fSLaxman Dewangan * recently mechanism to get new VSET register for new configuration 946219929fSLaxman Dewangan * and will return not_found so that caller need to set new VSET 956219929fSLaxman Dewangan * register and then gpios (both). 966219929fSLaxman Dewangan */ 976219929fSLaxman Dewangan static bool find_voltage_set_register(struct tps62360_chip *tps, 986219929fSLaxman Dewangan int req_vsel, int *vset_reg_id) 996219929fSLaxman Dewangan { 1006219929fSLaxman Dewangan int i; 1016219929fSLaxman Dewangan bool found = false; 1026219929fSLaxman Dewangan int new_vset_reg = tps->lru_index[3]; 1036219929fSLaxman Dewangan int found_index = 3; 1042935fb18SLaxman Dewangan 1056219929fSLaxman Dewangan for (i = 0; i < 4; ++i) { 1066219929fSLaxman Dewangan if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) { 1076219929fSLaxman Dewangan new_vset_reg = tps->lru_index[i]; 1086219929fSLaxman Dewangan found_index = i; 1096219929fSLaxman Dewangan found = true; 1106219929fSLaxman Dewangan goto update_lru_index; 1116219929fSLaxman Dewangan } 1126219929fSLaxman Dewangan } 1136219929fSLaxman Dewangan 1146219929fSLaxman Dewangan update_lru_index: 1156219929fSLaxman Dewangan for (i = found_index; i > 0; i--) 1166219929fSLaxman Dewangan tps->lru_index[i] = tps->lru_index[i - 1]; 1176219929fSLaxman Dewangan 1186219929fSLaxman Dewangan tps->lru_index[0] = new_vset_reg; 1196219929fSLaxman Dewangan *vset_reg_id = new_vset_reg; 1206219929fSLaxman Dewangan return found; 1216219929fSLaxman Dewangan } 1226219929fSLaxman Dewangan 123a60cfce0SLaxman Dewangan static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev) 1246219929fSLaxman Dewangan { 1256219929fSLaxman Dewangan struct tps62360_chip *tps = rdev_get_drvdata(dev); 1266219929fSLaxman Dewangan int vsel; 1276219929fSLaxman Dewangan unsigned int data; 1286219929fSLaxman Dewangan int ret; 1296219929fSLaxman Dewangan 1306219929fSLaxman Dewangan ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data); 1316219929fSLaxman Dewangan if (ret < 0) { 1322935fb18SLaxman Dewangan dev_err(tps->dev, "%s(): register %d read failed with err %d\n", 1332935fb18SLaxman Dewangan __func__, REG_VSET0 + tps->curr_vset_id, ret); 1346219929fSLaxman Dewangan return ret; 1356219929fSLaxman Dewangan } 1366219929fSLaxman Dewangan vsel = (int)data & tps->voltage_reg_mask; 137a60cfce0SLaxman Dewangan return vsel; 1386219929fSLaxman Dewangan } 1396219929fSLaxman Dewangan 1406219929fSLaxman Dewangan static int tps62360_dcdc_set_voltage(struct regulator_dev *dev, 1416219929fSLaxman Dewangan int min_uV, int max_uV, unsigned *selector) 1426219929fSLaxman Dewangan { 1436219929fSLaxman Dewangan struct tps62360_chip *tps = rdev_get_drvdata(dev); 1446219929fSLaxman Dewangan int vsel; 1456219929fSLaxman Dewangan int ret; 1466219929fSLaxman Dewangan bool found = false; 1476219929fSLaxman Dewangan int new_vset_id = tps->curr_vset_id; 1486219929fSLaxman Dewangan 1492935fb18SLaxman Dewangan if ((max_uV < min_uV) || (max_uV < tps->voltage_base)) 1506219929fSLaxman Dewangan return -EINVAL; 1516219929fSLaxman Dewangan 1522935fb18SLaxman Dewangan if (min_uV > (tps->voltage_base + (tps->desc.n_voltages - 1) * 10000)) 1536219929fSLaxman Dewangan return -EINVAL; 1546219929fSLaxman Dewangan 1552935fb18SLaxman Dewangan vsel = DIV_ROUND_UP(min_uV - tps->voltage_base, 10000); 1566219929fSLaxman Dewangan if (selector) 1576219929fSLaxman Dewangan *selector = (vsel & tps->voltage_reg_mask); 1586219929fSLaxman Dewangan 1596219929fSLaxman Dewangan /* 1606219929fSLaxman Dewangan * If gpios are available to select the VSET register then least 1616219929fSLaxman Dewangan * recently used register for new configuration. 1626219929fSLaxman Dewangan */ 1636219929fSLaxman Dewangan if (tps->valid_gpios) 1646219929fSLaxman Dewangan found = find_voltage_set_register(tps, vsel, &new_vset_id); 1656219929fSLaxman Dewangan 1666219929fSLaxman Dewangan if (!found) { 1676219929fSLaxman Dewangan ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id, 1686219929fSLaxman Dewangan tps->voltage_reg_mask, vsel); 1696219929fSLaxman Dewangan if (ret < 0) { 1702935fb18SLaxman Dewangan dev_err(tps->dev, 1712935fb18SLaxman Dewangan "%s(): register %d update failed with err %d\n", 1722935fb18SLaxman Dewangan __func__, REG_VSET0 + new_vset_id, ret); 1736219929fSLaxman Dewangan return ret; 1746219929fSLaxman Dewangan } 1756219929fSLaxman Dewangan tps->curr_vset_id = new_vset_id; 1766219929fSLaxman Dewangan tps->curr_vset_vsel[new_vset_id] = vsel; 1776219929fSLaxman Dewangan } 1786219929fSLaxman Dewangan 1796219929fSLaxman Dewangan /* Select proper VSET register vio gpios */ 1806219929fSLaxman Dewangan if (tps->valid_gpios) { 1812935fb18SLaxman Dewangan gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1); 1826219929fSLaxman Dewangan gpio_set_value_cansleep(tps->vsel1_gpio, 1836219929fSLaxman Dewangan (new_vset_id >> 1) & 0x1); 1846219929fSLaxman Dewangan } 1856219929fSLaxman Dewangan return 0; 1866219929fSLaxman Dewangan } 1876219929fSLaxman Dewangan 1886219929fSLaxman Dewangan static int tps62360_dcdc_list_voltage(struct regulator_dev *dev, 1896219929fSLaxman Dewangan unsigned selector) 1906219929fSLaxman Dewangan { 1916219929fSLaxman Dewangan struct tps62360_chip *tps = rdev_get_drvdata(dev); 1926219929fSLaxman Dewangan 19346783a04SAxel Lin if (selector >= tps->desc.n_voltages) 1946219929fSLaxman Dewangan return -EINVAL; 1952935fb18SLaxman Dewangan 1962935fb18SLaxman Dewangan return tps->voltage_base + selector * 10000; 1976219929fSLaxman Dewangan } 1986219929fSLaxman Dewangan 199a60cfce0SLaxman Dewangan static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev, 200a60cfce0SLaxman Dewangan unsigned int old_selector, unsigned int new_selector) 201a60cfce0SLaxman Dewangan { 202a60cfce0SLaxman Dewangan struct tps62360_chip *tps = rdev_get_drvdata(rdev); 203a60cfce0SLaxman Dewangan int old_uV, new_uV; 204a60cfce0SLaxman Dewangan 205a60cfce0SLaxman Dewangan old_uV = tps62360_dcdc_list_voltage(rdev, old_selector); 206a60cfce0SLaxman Dewangan if (old_uV < 0) 207a60cfce0SLaxman Dewangan return old_uV; 208a60cfce0SLaxman Dewangan 209a60cfce0SLaxman Dewangan new_uV = tps62360_dcdc_list_voltage(rdev, new_selector); 210a60cfce0SLaxman Dewangan if (new_uV < 0) 211a60cfce0SLaxman Dewangan return new_uV; 212a60cfce0SLaxman Dewangan 213a60cfce0SLaxman Dewangan return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us); 214a60cfce0SLaxman Dewangan } 215a60cfce0SLaxman Dewangan 2166219929fSLaxman Dewangan static struct regulator_ops tps62360_dcdc_ops = { 217a60cfce0SLaxman Dewangan .get_voltage_sel = tps62360_dcdc_get_voltage_sel, 2186219929fSLaxman Dewangan .set_voltage = tps62360_dcdc_set_voltage, 2196219929fSLaxman Dewangan .list_voltage = tps62360_dcdc_list_voltage, 220a60cfce0SLaxman Dewangan .set_voltage_time_sel = tps62360_set_voltage_time_sel, 2216219929fSLaxman Dewangan }; 2226219929fSLaxman Dewangan 2232935fb18SLaxman Dewangan static int __devinit tps62360_init_force_pwm(struct tps62360_chip *tps, 2246219929fSLaxman Dewangan struct tps62360_regulator_platform_data *pdata, 2256219929fSLaxman Dewangan int vset_id) 2266219929fSLaxman Dewangan { 2276219929fSLaxman Dewangan int ret; 2282935fb18SLaxman Dewangan int bit = 0; 2292935fb18SLaxman Dewangan 2306219929fSLaxman Dewangan if (pdata->en_force_pwm) 2312935fb18SLaxman Dewangan bit = BIT(7); 2322935fb18SLaxman Dewangan 2332935fb18SLaxman Dewangan ret = regmap_update_bits(tps->regmap, REG_VSET0 + vset_id, BIT(7), bit); 2346219929fSLaxman Dewangan if (ret < 0) 2352935fb18SLaxman Dewangan dev_err(tps->dev, 2362935fb18SLaxman Dewangan "%s(): register %d update failed with err %d\n", 2372935fb18SLaxman Dewangan __func__, REG_VSET0 + vset_id, ret); 2386219929fSLaxman Dewangan return ret; 2396219929fSLaxman Dewangan } 2406219929fSLaxman Dewangan 2412935fb18SLaxman Dewangan static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps, 2426219929fSLaxman Dewangan struct tps62360_regulator_platform_data *pdata) 2436219929fSLaxman Dewangan { 2446219929fSLaxman Dewangan int ret; 2456219929fSLaxman Dewangan int i; 246a60cfce0SLaxman Dewangan unsigned int ramp_ctrl; 2476219929fSLaxman Dewangan 2482935fb18SLaxman Dewangan /* Initialize internal pull up/down control */ 2496219929fSLaxman Dewangan if (tps->en_internal_pulldn) 2506219929fSLaxman Dewangan ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0); 2516219929fSLaxman Dewangan else 2526219929fSLaxman Dewangan ret = regmap_write(tps->regmap, REG_CONTROL, 0x0); 2536219929fSLaxman Dewangan if (ret < 0) { 2542935fb18SLaxman Dewangan dev_err(tps->dev, 2552935fb18SLaxman Dewangan "%s(): register %d write failed with err %d\n", 2562935fb18SLaxman Dewangan __func__, REG_CONTROL, ret); 2576219929fSLaxman Dewangan return ret; 2586219929fSLaxman Dewangan } 2596219929fSLaxman Dewangan 2602935fb18SLaxman Dewangan /* Initialize force PWM mode */ 2616219929fSLaxman Dewangan if (tps->valid_gpios) { 2626219929fSLaxman Dewangan for (i = 0; i < 4; ++i) { 2636219929fSLaxman Dewangan ret = tps62360_init_force_pwm(tps, pdata, i); 2646219929fSLaxman Dewangan if (ret < 0) 2656219929fSLaxman Dewangan return ret; 2666219929fSLaxman Dewangan } 2676219929fSLaxman Dewangan } else { 2686219929fSLaxman Dewangan ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id); 2696219929fSLaxman Dewangan if (ret < 0) 2706219929fSLaxman Dewangan return ret; 2716219929fSLaxman Dewangan } 2726219929fSLaxman Dewangan 2736219929fSLaxman Dewangan /* Reset output discharge path to reduce power consumption */ 2746219929fSLaxman Dewangan ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0); 275a60cfce0SLaxman Dewangan if (ret < 0) { 2762935fb18SLaxman Dewangan dev_err(tps->dev, 2772935fb18SLaxman Dewangan "%s(): register %d update failed with err %d\n", 2782935fb18SLaxman Dewangan __func__, REG_RAMPCTRL, ret); 2796219929fSLaxman Dewangan return ret; 2806219929fSLaxman Dewangan } 2816219929fSLaxman Dewangan 282a60cfce0SLaxman Dewangan /* Get ramp value from ramp control register */ 283a60cfce0SLaxman Dewangan ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl); 284a60cfce0SLaxman Dewangan if (ret < 0) { 2852935fb18SLaxman Dewangan dev_err(tps->dev, 2862935fb18SLaxman Dewangan "%s(): register %d read failed with err %d\n", 2872935fb18SLaxman Dewangan __func__, REG_RAMPCTRL, ret); 288a60cfce0SLaxman Dewangan return ret; 289a60cfce0SLaxman Dewangan } 290a60cfce0SLaxman Dewangan ramp_ctrl = (ramp_ctrl >> 4) & 0x7; 291a60cfce0SLaxman Dewangan 292a60cfce0SLaxman Dewangan /* ramp mV/us = 32/(2^ramp_ctrl) */ 293a60cfce0SLaxman Dewangan tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl)); 294a60cfce0SLaxman Dewangan return ret; 295a60cfce0SLaxman Dewangan } 296a60cfce0SLaxman Dewangan 2976219929fSLaxman Dewangan static const struct regmap_config tps62360_regmap_config = { 2986219929fSLaxman Dewangan .reg_bits = 8, 2996219929fSLaxman Dewangan .val_bits = 8, 30016ea003bSLaxman Dewangan .max_register = REG_CHIPID, 30116ea003bSLaxman Dewangan .cache_type = REGCACHE_RBTREE, 3026219929fSLaxman Dewangan }; 3036219929fSLaxman Dewangan 304*684ae39bSLaxman Dewangan static struct tps62360_regulator_platform_data * 305*684ae39bSLaxman Dewangan of_get_tps62360_platform_data(struct device *dev) 306*684ae39bSLaxman Dewangan { 307*684ae39bSLaxman Dewangan struct tps62360_regulator_platform_data *pdata; 308*684ae39bSLaxman Dewangan struct device_node *np = dev->of_node; 309*684ae39bSLaxman Dewangan 310*684ae39bSLaxman Dewangan pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 311*684ae39bSLaxman Dewangan if (!pdata) { 312*684ae39bSLaxman Dewangan dev_err(dev, "Memory alloc failed for platform data\n"); 313*684ae39bSLaxman Dewangan return NULL; 314*684ae39bSLaxman Dewangan } 315*684ae39bSLaxman Dewangan 316*684ae39bSLaxman Dewangan pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node); 317*684ae39bSLaxman Dewangan if (!pdata->reg_init_data) { 318*684ae39bSLaxman Dewangan dev_err(dev, "Not able to get OF regulator init data\n"); 319*684ae39bSLaxman Dewangan return NULL; 320*684ae39bSLaxman Dewangan } 321*684ae39bSLaxman Dewangan 322*684ae39bSLaxman Dewangan pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0); 323*684ae39bSLaxman Dewangan pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0); 324*684ae39bSLaxman Dewangan 325*684ae39bSLaxman Dewangan if (of_find_property(np, "ti,vsel0-state-high", NULL)) 326*684ae39bSLaxman Dewangan pdata->vsel0_def_state = 1; 327*684ae39bSLaxman Dewangan 328*684ae39bSLaxman Dewangan if (of_find_property(np, "ti,vsel1-state-high", NULL)) 329*684ae39bSLaxman Dewangan pdata->vsel1_def_state = 1; 330*684ae39bSLaxman Dewangan 331*684ae39bSLaxman Dewangan if (of_find_property(np, "ti,enable-pull-down", NULL)) 332*684ae39bSLaxman Dewangan pdata->en_internal_pulldn = true; 333*684ae39bSLaxman Dewangan 334*684ae39bSLaxman Dewangan if (of_find_property(np, "ti,enable-force-pwm", NULL)) 335*684ae39bSLaxman Dewangan pdata->en_force_pwm = true; 336*684ae39bSLaxman Dewangan 337*684ae39bSLaxman Dewangan if (of_find_property(np, "ti,enable-vout-discharge", NULL)) 338*684ae39bSLaxman Dewangan pdata->en_discharge = true; 339*684ae39bSLaxman Dewangan 340*684ae39bSLaxman Dewangan return pdata; 341*684ae39bSLaxman Dewangan } 342*684ae39bSLaxman Dewangan 343*684ae39bSLaxman Dewangan #if defined(CONFIG_OF) 344*684ae39bSLaxman Dewangan static const struct of_device_id tps62360_of_match[] = { 345*684ae39bSLaxman Dewangan { .compatible = "ti,tps62360", .data = (void *)TPS62360}, 346*684ae39bSLaxman Dewangan { .compatible = "ti,tps62361", .data = (void *)TPS62361}, 347*684ae39bSLaxman Dewangan { .compatible = "ti,tps62362", .data = (void *)TPS62362}, 348*684ae39bSLaxman Dewangan { .compatible = "ti,tps62363", .data = (void *)TPS62363}, 349*684ae39bSLaxman Dewangan {}, 350*684ae39bSLaxman Dewangan } 351*684ae39bSLaxman Dewangan MODULE_DEVICE_TABLE(of, tps62360_of_match); 352*684ae39bSLaxman Dewangan #endif 353*684ae39bSLaxman Dewangan 3546219929fSLaxman Dewangan static int __devinit tps62360_probe(struct i2c_client *client, 3556219929fSLaxman Dewangan const struct i2c_device_id *id) 3566219929fSLaxman Dewangan { 357c172708dSMark Brown struct regulator_config config = { }; 3586219929fSLaxman Dewangan struct tps62360_regulator_platform_data *pdata; 3596219929fSLaxman Dewangan struct regulator_dev *rdev; 3606219929fSLaxman Dewangan struct tps62360_chip *tps; 3616219929fSLaxman Dewangan int ret; 3626219929fSLaxman Dewangan int i; 363*684ae39bSLaxman Dewangan int chip_id; 3646219929fSLaxman Dewangan 3656219929fSLaxman Dewangan pdata = client->dev.platform_data; 366*684ae39bSLaxman Dewangan chip_id = id->driver_data; 367*684ae39bSLaxman Dewangan 368*684ae39bSLaxman Dewangan if (client->dev.of_node) { 369*684ae39bSLaxman Dewangan const struct of_device_id *match; 370*684ae39bSLaxman Dewangan match = of_match_device(of_match_ptr(tps62360_of_match), 371*684ae39bSLaxman Dewangan &client->dev); 372*684ae39bSLaxman Dewangan if (!match) { 373*684ae39bSLaxman Dewangan dev_err(&client->dev, "Error: No device match found\n"); 374*684ae39bSLaxman Dewangan return -ENODEV; 375*684ae39bSLaxman Dewangan } 376*684ae39bSLaxman Dewangan chip_id = (int)match->data; 377*684ae39bSLaxman Dewangan if (!pdata) 378*684ae39bSLaxman Dewangan pdata = of_get_tps62360_platform_data(&client->dev); 379*684ae39bSLaxman Dewangan } 380*684ae39bSLaxman Dewangan 3816219929fSLaxman Dewangan if (!pdata) { 3822935fb18SLaxman Dewangan dev_err(&client->dev, "%s(): Platform data not found\n", 3836219929fSLaxman Dewangan __func__); 3846219929fSLaxman Dewangan return -EIO; 3856219929fSLaxman Dewangan } 3866219929fSLaxman Dewangan 3876219929fSLaxman Dewangan tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); 3886219929fSLaxman Dewangan if (!tps) { 3892935fb18SLaxman Dewangan dev_err(&client->dev, "%s(): Memory allocation failed\n", 3906219929fSLaxman Dewangan __func__); 3916219929fSLaxman Dewangan return -ENOMEM; 3926219929fSLaxman Dewangan } 3936219929fSLaxman Dewangan 3946219929fSLaxman Dewangan tps->en_force_pwm = pdata->en_force_pwm; 3956219929fSLaxman Dewangan tps->en_discharge = pdata->en_discharge; 3966219929fSLaxman Dewangan tps->en_internal_pulldn = pdata->en_internal_pulldn; 3976219929fSLaxman Dewangan tps->vsel0_gpio = pdata->vsel0_gpio; 3986219929fSLaxman Dewangan tps->vsel1_gpio = pdata->vsel1_gpio; 3996219929fSLaxman Dewangan tps->dev = &client->dev; 400d1cf4f65SAxel Lin 401*684ae39bSLaxman Dewangan switch (chip_id) { 402d1cf4f65SAxel Lin case TPS62360: 403d1cf4f65SAxel Lin case TPS62362: 404d1cf4f65SAxel Lin tps->voltage_base = TPS62360_BASE_VOLTAGE; 405d1cf4f65SAxel Lin tps->voltage_reg_mask = 0x3F; 406d1cf4f65SAxel Lin tps->desc.n_voltages = TPS62360_N_VOLTAGES; 407d1cf4f65SAxel Lin break; 408d1cf4f65SAxel Lin case TPS62361: 409d1cf4f65SAxel Lin case TPS62363: 410d1cf4f65SAxel Lin tps->voltage_base = TPS62361_BASE_VOLTAGE; 411d1cf4f65SAxel Lin tps->voltage_reg_mask = 0x7F; 412d1cf4f65SAxel Lin tps->desc.n_voltages = TPS62361_N_VOLTAGES; 413d1cf4f65SAxel Lin break; 414d1cf4f65SAxel Lin default: 415d1cf4f65SAxel Lin return -ENODEV; 416d1cf4f65SAxel Lin } 4176219929fSLaxman Dewangan 4186219929fSLaxman Dewangan tps->desc.name = id->name; 4196219929fSLaxman Dewangan tps->desc.id = 0; 4206219929fSLaxman Dewangan tps->desc.ops = &tps62360_dcdc_ops; 4216219929fSLaxman Dewangan tps->desc.type = REGULATOR_VOLTAGE; 4226219929fSLaxman Dewangan tps->desc.owner = THIS_MODULE; 4239a4bdd87SAxel Lin tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config); 4246219929fSLaxman Dewangan if (IS_ERR(tps->regmap)) { 4256219929fSLaxman Dewangan ret = PTR_ERR(tps->regmap); 4262935fb18SLaxman Dewangan dev_err(&client->dev, 4272935fb18SLaxman Dewangan "%s(): regmap allocation failed with err %d\n", 4282935fb18SLaxman Dewangan __func__, ret); 4296219929fSLaxman Dewangan return ret; 4306219929fSLaxman Dewangan } 4316219929fSLaxman Dewangan i2c_set_clientdata(client, tps); 4326219929fSLaxman Dewangan 4336219929fSLaxman Dewangan tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 + 4346219929fSLaxman Dewangan (pdata->vsel0_def_state & 1); 4356219929fSLaxman Dewangan tps->lru_index[0] = tps->curr_vset_id; 4366219929fSLaxman Dewangan tps->valid_gpios = false; 4376219929fSLaxman Dewangan 4386219929fSLaxman Dewangan if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) { 4392935fb18SLaxman Dewangan int gpio_flags; 4402935fb18SLaxman Dewangan gpio_flags = (pdata->vsel0_def_state) ? 4412935fb18SLaxman Dewangan GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; 4422935fb18SLaxman Dewangan ret = gpio_request_one(tps->vsel0_gpio, 4432935fb18SLaxman Dewangan gpio_flags, "tps62360-vsel0"); 4446219929fSLaxman Dewangan if (ret) { 4456219929fSLaxman Dewangan dev_err(&client->dev, 4462935fb18SLaxman Dewangan "%s(): Could not obtain vsel0 GPIO %d: %d\n", 4472935fb18SLaxman Dewangan __func__, tps->vsel0_gpio, ret); 4486219929fSLaxman Dewangan goto err_gpio0; 4496219929fSLaxman Dewangan } 4506219929fSLaxman Dewangan 4512935fb18SLaxman Dewangan gpio_flags = (pdata->vsel1_def_state) ? 4522935fb18SLaxman Dewangan GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW; 4532935fb18SLaxman Dewangan ret = gpio_request_one(tps->vsel1_gpio, 4542935fb18SLaxman Dewangan gpio_flags, "tps62360-vsel1"); 4556219929fSLaxman Dewangan if (ret) { 4566219929fSLaxman Dewangan dev_err(&client->dev, 4572935fb18SLaxman Dewangan "%s(): Could not obtain vsel1 GPIO %d: %d\n", 4582935fb18SLaxman Dewangan __func__, tps->vsel1_gpio, ret); 4596219929fSLaxman Dewangan goto err_gpio1; 4606219929fSLaxman Dewangan } 4616219929fSLaxman Dewangan tps->valid_gpios = true; 4626219929fSLaxman Dewangan 4636219929fSLaxman Dewangan /* 4646219929fSLaxman Dewangan * Initialize the lru index with vset_reg id 4656219929fSLaxman Dewangan * The index 0 will be most recently used and 4666219929fSLaxman Dewangan * set with the tps->curr_vset_id */ 4676219929fSLaxman Dewangan for (i = 0; i < 4; ++i) 4686219929fSLaxman Dewangan tps->lru_index[i] = i; 4696219929fSLaxman Dewangan tps->lru_index[0] = tps->curr_vset_id; 4706219929fSLaxman Dewangan tps->lru_index[tps->curr_vset_id] = 0; 4716219929fSLaxman Dewangan } 4726219929fSLaxman Dewangan 4736219929fSLaxman Dewangan ret = tps62360_init_dcdc(tps, pdata); 4746219929fSLaxman Dewangan if (ret < 0) { 4752935fb18SLaxman Dewangan dev_err(tps->dev, "%s(): Init failed with err = %d\n", 4766219929fSLaxman Dewangan __func__, ret); 4776219929fSLaxman Dewangan goto err_init; 4786219929fSLaxman Dewangan } 4796219929fSLaxman Dewangan 480c172708dSMark Brown config.dev = &client->dev; 4818bdca009SLaxman Dewangan config.init_data = pdata->reg_init_data; 482c172708dSMark Brown config.driver_data = tps; 483c172708dSMark Brown 4846219929fSLaxman Dewangan /* Register the regulators */ 485c172708dSMark Brown rdev = regulator_register(&tps->desc, &config); 4866219929fSLaxman Dewangan if (IS_ERR(rdev)) { 4872935fb18SLaxman Dewangan dev_err(tps->dev, 4882935fb18SLaxman Dewangan "%s(): regulator register failed with err %s\n", 4896219929fSLaxman Dewangan __func__, id->name); 4906219929fSLaxman Dewangan ret = PTR_ERR(rdev); 4916219929fSLaxman Dewangan goto err_init; 4926219929fSLaxman Dewangan } 4936219929fSLaxman Dewangan 4946219929fSLaxman Dewangan tps->rdev = rdev; 4956219929fSLaxman Dewangan return 0; 4966219929fSLaxman Dewangan 4976219929fSLaxman Dewangan err_init: 4986219929fSLaxman Dewangan if (gpio_is_valid(tps->vsel1_gpio)) 4996219929fSLaxman Dewangan gpio_free(tps->vsel1_gpio); 5006219929fSLaxman Dewangan err_gpio1: 5016219929fSLaxman Dewangan if (gpio_is_valid(tps->vsel0_gpio)) 5026219929fSLaxman Dewangan gpio_free(tps->vsel0_gpio); 5036219929fSLaxman Dewangan err_gpio0: 5046219929fSLaxman Dewangan return ret; 5056219929fSLaxman Dewangan } 5066219929fSLaxman Dewangan 5076219929fSLaxman Dewangan /** 5086219929fSLaxman Dewangan * tps62360_remove - tps62360 driver i2c remove handler 5096219929fSLaxman Dewangan * @client: i2c driver client device structure 5106219929fSLaxman Dewangan * 5116219929fSLaxman Dewangan * Unregister TPS driver as an i2c client device driver 5126219929fSLaxman Dewangan */ 5136219929fSLaxman Dewangan static int __devexit tps62360_remove(struct i2c_client *client) 5146219929fSLaxman Dewangan { 5156219929fSLaxman Dewangan struct tps62360_chip *tps = i2c_get_clientdata(client); 5166219929fSLaxman Dewangan 5176219929fSLaxman Dewangan if (gpio_is_valid(tps->vsel1_gpio)) 5186219929fSLaxman Dewangan gpio_free(tps->vsel1_gpio); 5196219929fSLaxman Dewangan 5206219929fSLaxman Dewangan if (gpio_is_valid(tps->vsel0_gpio)) 5216219929fSLaxman Dewangan gpio_free(tps->vsel0_gpio); 5226219929fSLaxman Dewangan 5236219929fSLaxman Dewangan regulator_unregister(tps->rdev); 5246219929fSLaxman Dewangan return 0; 5256219929fSLaxman Dewangan } 5266219929fSLaxman Dewangan 5276219929fSLaxman Dewangan static void tps62360_shutdown(struct i2c_client *client) 5286219929fSLaxman Dewangan { 5296219929fSLaxman Dewangan struct tps62360_chip *tps = i2c_get_clientdata(client); 5306219929fSLaxman Dewangan int st; 5316219929fSLaxman Dewangan 5326219929fSLaxman Dewangan if (!tps->en_discharge) 5336219929fSLaxman Dewangan return; 5346219929fSLaxman Dewangan 5356219929fSLaxman Dewangan /* Configure the output discharge path */ 5366219929fSLaxman Dewangan st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2)); 5376219929fSLaxman Dewangan if (st < 0) 5382935fb18SLaxman Dewangan dev_err(tps->dev, 5392935fb18SLaxman Dewangan "%s(): register %d update failed with err %d\n", 5402935fb18SLaxman Dewangan __func__, REG_RAMPCTRL, st); 5416219929fSLaxman Dewangan } 5426219929fSLaxman Dewangan 5436219929fSLaxman Dewangan static const struct i2c_device_id tps62360_id[] = { 5446219929fSLaxman Dewangan {.name = "tps62360", .driver_data = TPS62360}, 5456219929fSLaxman Dewangan {.name = "tps62361", .driver_data = TPS62361}, 546d1cf4f65SAxel Lin {.name = "tps62362", .driver_data = TPS62362}, 547d1cf4f65SAxel Lin {.name = "tps62363", .driver_data = TPS62363}, 5486219929fSLaxman Dewangan {}, 5496219929fSLaxman Dewangan }; 5506219929fSLaxman Dewangan 5516219929fSLaxman Dewangan MODULE_DEVICE_TABLE(i2c, tps62360_id); 5526219929fSLaxman Dewangan 5536219929fSLaxman Dewangan static struct i2c_driver tps62360_i2c_driver = { 5546219929fSLaxman Dewangan .driver = { 5556219929fSLaxman Dewangan .name = "tps62360", 5566219929fSLaxman Dewangan .owner = THIS_MODULE, 557*684ae39bSLaxman Dewangan .of_match_table = of_match_ptr(tps62360_of_match), 5586219929fSLaxman Dewangan }, 5596219929fSLaxman Dewangan .probe = tps62360_probe, 5606219929fSLaxman Dewangan .remove = __devexit_p(tps62360_remove), 5616219929fSLaxman Dewangan .shutdown = tps62360_shutdown, 5626219929fSLaxman Dewangan .id_table = tps62360_id, 5636219929fSLaxman Dewangan }; 5646219929fSLaxman Dewangan 5656219929fSLaxman Dewangan static int __init tps62360_init(void) 5666219929fSLaxman Dewangan { 5676219929fSLaxman Dewangan return i2c_add_driver(&tps62360_i2c_driver); 5686219929fSLaxman Dewangan } 5696219929fSLaxman Dewangan subsys_initcall(tps62360_init); 5706219929fSLaxman Dewangan 5716219929fSLaxman Dewangan static void __exit tps62360_cleanup(void) 5726219929fSLaxman Dewangan { 5736219929fSLaxman Dewangan i2c_del_driver(&tps62360_i2c_driver); 5746219929fSLaxman Dewangan } 5756219929fSLaxman Dewangan module_exit(tps62360_cleanup); 5766219929fSLaxman Dewangan 5776219929fSLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); 578d1cf4f65SAxel Lin MODULE_DESCRIPTION("TPS6236x voltage regulator driver"); 5796219929fSLaxman Dewangan MODULE_LICENSE("GPL v2"); 580