Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * tps62360.c -- TI tps62360
0004  *
0005  * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
0006  *
0007  * Copyright (c) 2012, NVIDIA Corporation.
0008  *
0009  * Author: Laxman Dewangan <ldewangan@nvidia.com>
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/init.h>
0015 #include <linux/err.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/regulator/of_regulator.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regulator/driver.h>
0021 #include <linux/regulator/machine.h>
0022 #include <linux/regulator/tps62360.h>
0023 #include <linux/gpio/consumer.h>
0024 #include <linux/i2c.h>
0025 #include <linux/slab.h>
0026 #include <linux/regmap.h>
0027 
0028 /* Register definitions */
0029 #define REG_VSET0       0
0030 #define REG_VSET1       1
0031 #define REG_VSET2       2
0032 #define REG_VSET3       3
0033 #define REG_CONTROL     4
0034 #define REG_TEMP        5
0035 #define REG_RAMPCTRL        6
0036 #define REG_CHIPID      8
0037 
0038 #define FORCE_PWM_ENABLE    BIT(7)
0039 
0040 enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
0041 
0042 #define TPS62360_BASE_VOLTAGE   770000
0043 #define TPS62360_N_VOLTAGES 64
0044 
0045 #define TPS62361_BASE_VOLTAGE   500000
0046 #define TPS62361_N_VOLTAGES 128
0047 
0048 /* tps 62360 chip information */
0049 struct tps62360_chip {
0050     struct device *dev;
0051     struct regulator_desc desc;
0052     struct regulator_dev *rdev;
0053     struct regmap *regmap;
0054     struct gpio_desc *vsel0_gpio;
0055     struct gpio_desc *vsel1_gpio;
0056     u8 voltage_reg_mask;
0057     bool en_internal_pulldn;
0058     bool en_discharge;
0059     bool valid_gpios;
0060     int lru_index[4];
0061     int curr_vset_vsel[4];
0062     int curr_vset_id;
0063 };
0064 
0065 /*
0066  * find_voltage_set_register: Find new voltage configuration register
0067  * (VSET) id.
0068  * The finding of the new VSET register will be based on the LRU mechanism.
0069  * Each VSET register will have different voltage configured . This
0070  * Function will look if any of the VSET register have requested voltage set
0071  * or not.
0072  *     - If it is already there then it will make that register as most
0073  *       recently used and return as found so that caller need not to set
0074  *       the VSET register but need to set the proper gpios to select this
0075  *       VSET register.
0076  *     - If requested voltage is not found then it will use the least
0077  *       recently mechanism to get new VSET register for new configuration
0078  *       and will return not_found so that caller need to set new VSET
0079  *       register and then gpios (both).
0080  */
0081 static bool find_voltage_set_register(struct tps62360_chip *tps,
0082         int req_vsel, int *vset_reg_id)
0083 {
0084     int i;
0085     bool found = false;
0086     int new_vset_reg = tps->lru_index[3];
0087     int found_index = 3;
0088 
0089     for (i = 0; i < 4; ++i) {
0090         if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
0091             new_vset_reg = tps->lru_index[i];
0092             found_index = i;
0093             found = true;
0094             goto update_lru_index;
0095         }
0096     }
0097 
0098 update_lru_index:
0099     for (i = found_index; i > 0; i--)
0100         tps->lru_index[i] = tps->lru_index[i - 1];
0101 
0102     tps->lru_index[0] = new_vset_reg;
0103     *vset_reg_id = new_vset_reg;
0104     return found;
0105 }
0106 
0107 static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
0108 {
0109     struct tps62360_chip *tps = rdev_get_drvdata(dev);
0110     int vsel;
0111     unsigned int data;
0112     int ret;
0113 
0114     ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
0115     if (ret < 0) {
0116         dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
0117             __func__, REG_VSET0 + tps->curr_vset_id, ret);
0118         return ret;
0119     }
0120     vsel = (int)data & tps->voltage_reg_mask;
0121     return vsel;
0122 }
0123 
0124 static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
0125                      unsigned selector)
0126 {
0127     struct tps62360_chip *tps = rdev_get_drvdata(dev);
0128     int ret;
0129     bool found = false;
0130     int new_vset_id = tps->curr_vset_id;
0131 
0132     /*
0133      * If gpios are available to select the VSET register then least
0134      * recently used register for new configuration.
0135      */
0136     if (tps->valid_gpios)
0137         found = find_voltage_set_register(tps, selector, &new_vset_id);
0138 
0139     if (!found) {
0140         ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
0141                 tps->voltage_reg_mask, selector);
0142         if (ret < 0) {
0143             dev_err(tps->dev,
0144                 "%s(): register %d update failed with err %d\n",
0145                  __func__, REG_VSET0 + new_vset_id, ret);
0146             return ret;
0147         }
0148         tps->curr_vset_id = new_vset_id;
0149         tps->curr_vset_vsel[new_vset_id] = selector;
0150     }
0151 
0152     /* Select proper VSET register vio gpios */
0153     if (tps->valid_gpios) {
0154         gpiod_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
0155         gpiod_set_value_cansleep(tps->vsel1_gpio,
0156                     (new_vset_id >> 1) & 0x1);
0157     }
0158     return 0;
0159 }
0160 
0161 static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
0162 {
0163     struct tps62360_chip *tps = rdev_get_drvdata(rdev);
0164     int i;
0165     int val;
0166     int ret;
0167 
0168     /* Enable force PWM mode in FAST mode only. */
0169     switch (mode) {
0170     case REGULATOR_MODE_FAST:
0171         val = FORCE_PWM_ENABLE;
0172         break;
0173 
0174     case REGULATOR_MODE_NORMAL:
0175         val = 0;
0176         break;
0177 
0178     default:
0179         return -EINVAL;
0180     }
0181 
0182     if (!tps->valid_gpios) {
0183         ret = regmap_update_bits(tps->regmap,
0184             REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
0185         if (ret < 0)
0186             dev_err(tps->dev,
0187                 "%s(): register %d update failed with err %d\n",
0188                 __func__, REG_VSET0 + tps->curr_vset_id, ret);
0189         return ret;
0190     }
0191 
0192     /* If gpios are valid then all register set need to be control */
0193     for (i = 0; i < 4; ++i) {
0194         ret = regmap_update_bits(tps->regmap,
0195                     REG_VSET0 + i, FORCE_PWM_ENABLE, val);
0196         if (ret < 0) {
0197             dev_err(tps->dev,
0198                 "%s(): register %d update failed with err %d\n",
0199                 __func__, REG_VSET0 + i, ret);
0200             return ret;
0201         }
0202     }
0203     return ret;
0204 }
0205 
0206 static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
0207 {
0208     struct tps62360_chip *tps = rdev_get_drvdata(rdev);
0209     unsigned int data;
0210     int ret;
0211 
0212     ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
0213     if (ret < 0) {
0214         dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
0215             __func__, REG_VSET0 + tps->curr_vset_id, ret);
0216         return ret;
0217     }
0218     return (data & FORCE_PWM_ENABLE) ?
0219                 REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
0220 }
0221 
0222 static const struct regulator_ops tps62360_dcdc_ops = {
0223     .get_voltage_sel    = tps62360_dcdc_get_voltage_sel,
0224     .set_voltage_sel    = tps62360_dcdc_set_voltage_sel,
0225     .list_voltage       = regulator_list_voltage_linear,
0226     .map_voltage        = regulator_map_voltage_linear,
0227     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0228     .set_mode       = tps62360_set_mode,
0229     .get_mode       = tps62360_get_mode,
0230 };
0231 
0232 static int tps62360_init_dcdc(struct tps62360_chip *tps,
0233         struct tps62360_regulator_platform_data *pdata)
0234 {
0235     int ret;
0236     unsigned int ramp_ctrl;
0237 
0238     /* Initialize internal pull up/down control */
0239     if (tps->en_internal_pulldn)
0240         ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
0241     else
0242         ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
0243     if (ret < 0) {
0244         dev_err(tps->dev,
0245             "%s(): register %d write failed with err %d\n",
0246             __func__, REG_CONTROL, ret);
0247         return ret;
0248     }
0249 
0250     /* Reset output discharge path to reduce power consumption */
0251     ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
0252     if (ret < 0) {
0253         dev_err(tps->dev,
0254             "%s(): register %d update failed with err %d\n",
0255             __func__, REG_RAMPCTRL, ret);
0256         return ret;
0257     }
0258 
0259     /* Get ramp value from ramp control register */
0260     ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
0261     if (ret < 0) {
0262         dev_err(tps->dev,
0263             "%s(): register %d read failed with err %d\n",
0264             __func__, REG_RAMPCTRL, ret);
0265         return ret;
0266     }
0267     ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
0268 
0269     /* ramp mV/us = 32/(2^ramp_ctrl) */
0270     tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
0271     return ret;
0272 }
0273 
0274 static const struct regmap_config tps62360_regmap_config = {
0275     .reg_bits       = 8,
0276     .val_bits       = 8,
0277     .max_register       = REG_CHIPID,
0278     .cache_type     = REGCACHE_RBTREE,
0279 };
0280 
0281 static struct tps62360_regulator_platform_data *
0282     of_get_tps62360_platform_data(struct device *dev,
0283                       const struct regulator_desc *desc)
0284 {
0285     struct tps62360_regulator_platform_data *pdata;
0286     struct device_node *np = dev->of_node;
0287 
0288     pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
0289     if (!pdata)
0290         return NULL;
0291 
0292     pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
0293                               desc);
0294     if (!pdata->reg_init_data) {
0295         dev_err(dev, "Not able to get OF regulator init data\n");
0296         return NULL;
0297     }
0298 
0299     if (of_find_property(np, "ti,vsel0-state-high", NULL))
0300         pdata->vsel0_def_state = 1;
0301 
0302     if (of_find_property(np, "ti,vsel1-state-high", NULL))
0303         pdata->vsel1_def_state = 1;
0304 
0305     if (of_find_property(np, "ti,enable-pull-down", NULL))
0306         pdata->en_internal_pulldn = true;
0307 
0308     if (of_find_property(np, "ti,enable-vout-discharge", NULL))
0309         pdata->en_discharge = true;
0310 
0311     return pdata;
0312 }
0313 
0314 #if defined(CONFIG_OF)
0315 static const struct of_device_id tps62360_of_match[] = {
0316      { .compatible = "ti,tps62360", .data = (void *)TPS62360},
0317      { .compatible = "ti,tps62361", .data = (void *)TPS62361},
0318      { .compatible = "ti,tps62362", .data = (void *)TPS62362},
0319      { .compatible = "ti,tps62363", .data = (void *)TPS62363},
0320     {},
0321 };
0322 MODULE_DEVICE_TABLE(of, tps62360_of_match);
0323 #endif
0324 
0325 static int tps62360_probe(struct i2c_client *client,
0326                      const struct i2c_device_id *id)
0327 {
0328     struct regulator_config config = { };
0329     struct tps62360_regulator_platform_data *pdata;
0330     struct regulator_dev *rdev;
0331     struct tps62360_chip *tps;
0332     int ret;
0333     int i;
0334     int chip_id;
0335     int gpio_flags;
0336 
0337     pdata = dev_get_platdata(&client->dev);
0338 
0339     tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
0340     if (!tps)
0341         return -ENOMEM;
0342 
0343     tps->desc.name = client->name;
0344     tps->desc.id = 0;
0345     tps->desc.ops = &tps62360_dcdc_ops;
0346     tps->desc.type = REGULATOR_VOLTAGE;
0347     tps->desc.owner = THIS_MODULE;
0348     tps->desc.uV_step = 10000;
0349 
0350     if (client->dev.of_node) {
0351         const struct of_device_id *match;
0352         match = of_match_device(of_match_ptr(tps62360_of_match),
0353                 &client->dev);
0354         if (!match) {
0355             dev_err(&client->dev, "Error: No device match found\n");
0356             return -ENODEV;
0357         }
0358         chip_id = (int)(long)match->data;
0359         if (!pdata)
0360             pdata = of_get_tps62360_platform_data(&client->dev,
0361                                   &tps->desc);
0362     } else if (id) {
0363         chip_id = id->driver_data;
0364     } else {
0365         dev_err(&client->dev, "No device tree match or id table match found\n");
0366         return -ENODEV;
0367     }
0368 
0369     if (!pdata) {
0370         dev_err(&client->dev, "%s(): Platform data not found\n",
0371                         __func__);
0372         return -EIO;
0373     }
0374 
0375     tps->en_discharge = pdata->en_discharge;
0376     tps->en_internal_pulldn = pdata->en_internal_pulldn;
0377     tps->dev = &client->dev;
0378 
0379     switch (chip_id) {
0380     case TPS62360:
0381     case TPS62362:
0382         tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
0383         tps->voltage_reg_mask = 0x3F;
0384         tps->desc.n_voltages = TPS62360_N_VOLTAGES;
0385         break;
0386     case TPS62361:
0387     case TPS62363:
0388         tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
0389         tps->voltage_reg_mask = 0x7F;
0390         tps->desc.n_voltages = TPS62361_N_VOLTAGES;
0391         break;
0392     default:
0393         return -ENODEV;
0394     }
0395 
0396     tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
0397     if (IS_ERR(tps->regmap)) {
0398         ret = PTR_ERR(tps->regmap);
0399         dev_err(&client->dev,
0400             "%s(): regmap allocation failed with err %d\n",
0401             __func__, ret);
0402         return ret;
0403     }
0404     i2c_set_clientdata(client, tps);
0405 
0406     tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
0407                 (pdata->vsel0_def_state & 1);
0408     tps->lru_index[0] = tps->curr_vset_id;
0409     tps->valid_gpios = false;
0410 
0411     gpio_flags = (pdata->vsel0_def_state) ?
0412             GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0413     tps->vsel0_gpio = devm_gpiod_get_optional(&client->dev, "vsel0", gpio_flags);
0414     if (IS_ERR(tps->vsel0_gpio)) {
0415         dev_err(&client->dev,
0416             "%s(): Could not obtain vsel0 GPIO: %ld\n",
0417             __func__, PTR_ERR(tps->vsel0_gpio));
0418         return PTR_ERR(tps->vsel0_gpio);
0419     }
0420 
0421     gpio_flags = (pdata->vsel1_def_state) ?
0422             GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
0423     tps->vsel1_gpio = devm_gpiod_get_optional(&client->dev, "vsel1", gpio_flags);
0424     if (IS_ERR(tps->vsel1_gpio)) {
0425         dev_err(&client->dev,
0426             "%s(): Could not obtain vsel1 GPIO: %ld\n",
0427             __func__, PTR_ERR(tps->vsel1_gpio));
0428         return PTR_ERR(tps->vsel1_gpio);
0429     }
0430 
0431     if (tps->vsel0_gpio != NULL && tps->vsel1_gpio != NULL) {
0432         tps->valid_gpios = true;
0433 
0434         /*
0435          * Initialize the lru index with vset_reg id
0436          * The index 0 will be most recently used and
0437          * set with the tps->curr_vset_id */
0438         for (i = 0; i < 4; ++i)
0439             tps->lru_index[i] = i;
0440         tps->lru_index[0] = tps->curr_vset_id;
0441         tps->lru_index[tps->curr_vset_id] = 0;
0442     }
0443 
0444     ret = tps62360_init_dcdc(tps, pdata);
0445     if (ret < 0) {
0446         dev_err(tps->dev, "%s(): Init failed with err = %d\n",
0447                 __func__, ret);
0448         return ret;
0449     }
0450 
0451     config.dev = &client->dev;
0452     config.init_data = pdata->reg_init_data;
0453     config.driver_data = tps;
0454     config.of_node = client->dev.of_node;
0455 
0456     /* Register the regulators */
0457     rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
0458     if (IS_ERR(rdev)) {
0459         dev_err(tps->dev,
0460             "%s(): regulator register failed with err %s\n",
0461             __func__, id->name);
0462         return PTR_ERR(rdev);
0463     }
0464 
0465     tps->rdev = rdev;
0466     return 0;
0467 }
0468 
0469 static void tps62360_shutdown(struct i2c_client *client)
0470 {
0471     struct tps62360_chip *tps = i2c_get_clientdata(client);
0472     int st;
0473 
0474     if (!tps->en_discharge)
0475         return;
0476 
0477     /* Configure the output discharge path */
0478     st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
0479     if (st < 0)
0480         dev_err(tps->dev,
0481             "%s(): register %d update failed with err %d\n",
0482             __func__, REG_RAMPCTRL, st);
0483 }
0484 
0485 static const struct i2c_device_id tps62360_id[] = {
0486     {.name = "tps62360", .driver_data = TPS62360},
0487     {.name = "tps62361", .driver_data = TPS62361},
0488     {.name = "tps62362", .driver_data = TPS62362},
0489     {.name = "tps62363", .driver_data = TPS62363},
0490     {},
0491 };
0492 
0493 MODULE_DEVICE_TABLE(i2c, tps62360_id);
0494 
0495 static struct i2c_driver tps62360_i2c_driver = {
0496     .driver = {
0497         .name = "tps62360",
0498         .of_match_table = of_match_ptr(tps62360_of_match),
0499     },
0500     .probe = tps62360_probe,
0501     .shutdown = tps62360_shutdown,
0502     .id_table = tps62360_id,
0503 };
0504 
0505 static int __init tps62360_init(void)
0506 {
0507     return i2c_add_driver(&tps62360_i2c_driver);
0508 }
0509 subsys_initcall(tps62360_init);
0510 
0511 static void __exit tps62360_cleanup(void)
0512 {
0513     i2c_del_driver(&tps62360_i2c_driver);
0514 }
0515 module_exit(tps62360_cleanup);
0516 
0517 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
0518 MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
0519 MODULE_LICENSE("GPL v2");