177c129bfSVenkat Reddy Talla /*
277c129bfSVenkat Reddy Talla * TI TPS65132 Regulator driver
377c129bfSVenkat Reddy Talla *
477c129bfSVenkat Reddy Talla * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
577c129bfSVenkat Reddy Talla *
677c129bfSVenkat Reddy Talla * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
777c129bfSVenkat Reddy Talla * Laxman Dewangan <ldewangan@nvidia.com>
877c129bfSVenkat Reddy Talla *
977c129bfSVenkat Reddy Talla * This program is free software; you can redistribute it and/or
1077c129bfSVenkat Reddy Talla * modify it under the terms of the GNU General Public License as
1177c129bfSVenkat Reddy Talla * published by the Free Software Foundation; either version 2 of the
1277c129bfSVenkat Reddy Talla * License, or (at your option) any later version.
1377c129bfSVenkat Reddy Talla *
1477c129bfSVenkat Reddy Talla * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
1577c129bfSVenkat Reddy Talla * whether express or implied; without even the implied warranty of
1677c129bfSVenkat Reddy Talla * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1777c129bfSVenkat Reddy Talla * General Public License for more details.
1877c129bfSVenkat Reddy Talla */
1977c129bfSVenkat Reddy Talla
2077c129bfSVenkat Reddy Talla #include <linux/delay.h>
2177c129bfSVenkat Reddy Talla #include <linux/err.h>
2277c129bfSVenkat Reddy Talla #include <linux/gpio/consumer.h>
2377c129bfSVenkat Reddy Talla #include <linux/i2c.h>
2477c129bfSVenkat Reddy Talla #include <linux/module.h>
2577c129bfSVenkat Reddy Talla #include <linux/regmap.h>
2677c129bfSVenkat Reddy Talla #include <linux/regulator/driver.h>
2777c129bfSVenkat Reddy Talla #include <linux/regulator/machine.h>
2877c129bfSVenkat Reddy Talla
2977c129bfSVenkat Reddy Talla #define TPS65132_REG_VPOS 0x00
3077c129bfSVenkat Reddy Talla #define TPS65132_REG_VNEG 0x01
3177c129bfSVenkat Reddy Talla #define TPS65132_REG_APPS_DISP_DISN 0x03
3277c129bfSVenkat Reddy Talla #define TPS65132_REG_CONTROL 0x0FF
3377c129bfSVenkat Reddy Talla
3477c129bfSVenkat Reddy Talla #define TPS65132_VOUT_MASK 0x1F
3577c129bfSVenkat Reddy Talla #define TPS65132_VOUT_N_VOLTAGE 0x15
3677c129bfSVenkat Reddy Talla #define TPS65132_VOUT_VMIN 4000000
3777c129bfSVenkat Reddy Talla #define TPS65132_VOUT_VMAX 6000000
3877c129bfSVenkat Reddy Talla #define TPS65132_VOUT_STEP 100000
3977c129bfSVenkat Reddy Talla
4077c129bfSVenkat Reddy Talla #define TPS65132_REG_APPS_DIS_VPOS BIT(0)
4177c129bfSVenkat Reddy Talla #define TPS65132_REG_APPS_DIS_VNEG BIT(1)
4277c129bfSVenkat Reddy Talla
4377c129bfSVenkat Reddy Talla #define TPS65132_REGULATOR_ID_VPOS 0
4477c129bfSVenkat Reddy Talla #define TPS65132_REGULATOR_ID_VNEG 1
4577c129bfSVenkat Reddy Talla #define TPS65132_MAX_REGULATORS 2
4677c129bfSVenkat Reddy Talla
4777c129bfSVenkat Reddy Talla #define TPS65132_ACT_DIS_TIME_SLACK 1000
4877c129bfSVenkat Reddy Talla
4977c129bfSVenkat Reddy Talla struct tps65132_reg_pdata {
5077c129bfSVenkat Reddy Talla struct gpio_desc *en_gpiod;
5177c129bfSVenkat Reddy Talla struct gpio_desc *act_dis_gpiod;
5277c129bfSVenkat Reddy Talla unsigned int act_dis_time_us;
5377c129bfSVenkat Reddy Talla int ena_gpio_state;
5477c129bfSVenkat Reddy Talla };
5577c129bfSVenkat Reddy Talla
5677c129bfSVenkat Reddy Talla struct tps65132_regulator {
5777c129bfSVenkat Reddy Talla struct device *dev;
5877c129bfSVenkat Reddy Talla struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
5977c129bfSVenkat Reddy Talla };
6077c129bfSVenkat Reddy Talla
tps65132_regulator_enable(struct regulator_dev * rdev)6177c129bfSVenkat Reddy Talla static int tps65132_regulator_enable(struct regulator_dev *rdev)
6277c129bfSVenkat Reddy Talla {
6377c129bfSVenkat Reddy Talla struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
6477c129bfSVenkat Reddy Talla int id = rdev_get_id(rdev);
6577c129bfSVenkat Reddy Talla struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
6677c129bfSVenkat Reddy Talla int ret;
6777c129bfSVenkat Reddy Talla
6877c129bfSVenkat Reddy Talla if (!IS_ERR(rpdata->en_gpiod)) {
6977c129bfSVenkat Reddy Talla gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
7077c129bfSVenkat Reddy Talla rpdata->ena_gpio_state = 1;
7177c129bfSVenkat Reddy Talla }
7277c129bfSVenkat Reddy Talla
7377c129bfSVenkat Reddy Talla /* Hardware automatically enable discharge bit in enable */
7477c129bfSVenkat Reddy Talla if (rdev->constraints->active_discharge ==
7577c129bfSVenkat Reddy Talla REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
7677c129bfSVenkat Reddy Talla ret = regulator_set_active_discharge_regmap(rdev, false);
7777c129bfSVenkat Reddy Talla if (ret < 0) {
7877c129bfSVenkat Reddy Talla dev_err(tps->dev, "Failed to disable active discharge: %d\n",
7977c129bfSVenkat Reddy Talla ret);
8077c129bfSVenkat Reddy Talla return ret;
8177c129bfSVenkat Reddy Talla }
8277c129bfSVenkat Reddy Talla }
8377c129bfSVenkat Reddy Talla
8477c129bfSVenkat Reddy Talla return 0;
8577c129bfSVenkat Reddy Talla }
8677c129bfSVenkat Reddy Talla
tps65132_regulator_disable(struct regulator_dev * rdev)8777c129bfSVenkat Reddy Talla static int tps65132_regulator_disable(struct regulator_dev *rdev)
8877c129bfSVenkat Reddy Talla {
8977c129bfSVenkat Reddy Talla struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
9077c129bfSVenkat Reddy Talla int id = rdev_get_id(rdev);
9177c129bfSVenkat Reddy Talla struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
9277c129bfSVenkat Reddy Talla
9377c129bfSVenkat Reddy Talla if (!IS_ERR(rpdata->en_gpiod)) {
9477c129bfSVenkat Reddy Talla gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
9577c129bfSVenkat Reddy Talla rpdata->ena_gpio_state = 0;
9677c129bfSVenkat Reddy Talla }
9777c129bfSVenkat Reddy Talla
9877c129bfSVenkat Reddy Talla if (!IS_ERR(rpdata->act_dis_gpiod)) {
9977c129bfSVenkat Reddy Talla gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
10077c129bfSVenkat Reddy Talla usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
10177c129bfSVenkat Reddy Talla TPS65132_ACT_DIS_TIME_SLACK);
10277c129bfSVenkat Reddy Talla gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
10377c129bfSVenkat Reddy Talla }
10477c129bfSVenkat Reddy Talla
10577c129bfSVenkat Reddy Talla return 0;
10677c129bfSVenkat Reddy Talla }
10777c129bfSVenkat Reddy Talla
tps65132_regulator_is_enabled(struct regulator_dev * rdev)10877c129bfSVenkat Reddy Talla static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
10977c129bfSVenkat Reddy Talla {
11077c129bfSVenkat Reddy Talla struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
11177c129bfSVenkat Reddy Talla int id = rdev_get_id(rdev);
11277c129bfSVenkat Reddy Talla struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
11377c129bfSVenkat Reddy Talla
11477c129bfSVenkat Reddy Talla if (!IS_ERR(rpdata->en_gpiod))
11577c129bfSVenkat Reddy Talla return rpdata->ena_gpio_state;
11677c129bfSVenkat Reddy Talla
11777c129bfSVenkat Reddy Talla return 1;
11877c129bfSVenkat Reddy Talla }
11977c129bfSVenkat Reddy Talla
1204e105e3bSAxel Lin static const struct regulator_ops tps65132_regulator_ops = {
12177c129bfSVenkat Reddy Talla .enable = tps65132_regulator_enable,
12277c129bfSVenkat Reddy Talla .disable = tps65132_regulator_disable,
12377c129bfSVenkat Reddy Talla .is_enabled = tps65132_regulator_is_enabled,
12477c129bfSVenkat Reddy Talla .list_voltage = regulator_list_voltage_linear,
12577c129bfSVenkat Reddy Talla .map_voltage = regulator_map_voltage_linear,
12677c129bfSVenkat Reddy Talla .get_voltage_sel = regulator_get_voltage_sel_regmap,
12777c129bfSVenkat Reddy Talla .set_voltage_sel = regulator_set_voltage_sel_regmap,
12877c129bfSVenkat Reddy Talla .set_active_discharge = regulator_set_active_discharge_regmap,
12977c129bfSVenkat Reddy Talla };
13077c129bfSVenkat Reddy Talla
tps65132_of_parse_cb(struct device_node * np,const struct regulator_desc * desc,struct regulator_config * config)13177c129bfSVenkat Reddy Talla static int tps65132_of_parse_cb(struct device_node *np,
13277c129bfSVenkat Reddy Talla const struct regulator_desc *desc,
13377c129bfSVenkat Reddy Talla struct regulator_config *config)
13477c129bfSVenkat Reddy Talla {
13577c129bfSVenkat Reddy Talla struct tps65132_regulator *tps = config->driver_data;
13677c129bfSVenkat Reddy Talla struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
13777c129bfSVenkat Reddy Talla int ret;
13877c129bfSVenkat Reddy Talla
13922803ca3SDmitry Torokhov rpdata->en_gpiod = devm_fwnode_gpiod_get(tps->dev, of_fwnode_handle(np),
14022803ca3SDmitry Torokhov "enable", GPIOD_ASIS,
14122803ca3SDmitry Torokhov "enable");
14222803ca3SDmitry Torokhov if (IS_ERR(rpdata->en_gpiod)) {
14377c129bfSVenkat Reddy Talla ret = PTR_ERR(rpdata->en_gpiod);
14477c129bfSVenkat Reddy Talla
14577c129bfSVenkat Reddy Talla /* Ignore the error other than probe defer */
14677c129bfSVenkat Reddy Talla if (ret == -EPROBE_DEFER)
14777c129bfSVenkat Reddy Talla return ret;
14877c129bfSVenkat Reddy Talla return 0;
14977c129bfSVenkat Reddy Talla }
15077c129bfSVenkat Reddy Talla
15122803ca3SDmitry Torokhov rpdata->act_dis_gpiod = devm_fwnode_gpiod_get(tps->dev,
15222803ca3SDmitry Torokhov of_fwnode_handle(np),
15322803ca3SDmitry Torokhov "active-discharge",
15422803ca3SDmitry Torokhov GPIOD_ASIS,
15522803ca3SDmitry Torokhov "active-discharge");
15622803ca3SDmitry Torokhov if (IS_ERR(rpdata->act_dis_gpiod)) {
15777c129bfSVenkat Reddy Talla ret = PTR_ERR(rpdata->act_dis_gpiod);
15877c129bfSVenkat Reddy Talla
15977c129bfSVenkat Reddy Talla /* Ignore the error other than probe defer */
16077c129bfSVenkat Reddy Talla if (ret == -EPROBE_DEFER)
16177c129bfSVenkat Reddy Talla return ret;
16277c129bfSVenkat Reddy Talla
16377c129bfSVenkat Reddy Talla return 0;
16477c129bfSVenkat Reddy Talla }
16577c129bfSVenkat Reddy Talla
16677c129bfSVenkat Reddy Talla ret = of_property_read_u32(np, "ti,active-discharge-time-us",
16777c129bfSVenkat Reddy Talla &rpdata->act_dis_time_us);
16877c129bfSVenkat Reddy Talla if (ret < 0) {
16977c129bfSVenkat Reddy Talla dev_err(tps->dev, "Failed to read active discharge time:%d\n",
17077c129bfSVenkat Reddy Talla ret);
17177c129bfSVenkat Reddy Talla return ret;
17277c129bfSVenkat Reddy Talla }
17377c129bfSVenkat Reddy Talla
17477c129bfSVenkat Reddy Talla return 0;
17577c129bfSVenkat Reddy Talla }
17677c129bfSVenkat Reddy Talla
17777c129bfSVenkat Reddy Talla #define TPS65132_REGULATOR_DESC(_id, _name) \
17877c129bfSVenkat Reddy Talla [TPS65132_REGULATOR_ID_##_id] = { \
17977c129bfSVenkat Reddy Talla .name = "tps65132-"#_name, \
18077c129bfSVenkat Reddy Talla .supply_name = "vin", \
18177c129bfSVenkat Reddy Talla .id = TPS65132_REGULATOR_ID_##_id, \
18277c129bfSVenkat Reddy Talla .of_match = of_match_ptr(#_name), \
18377c129bfSVenkat Reddy Talla .of_parse_cb = tps65132_of_parse_cb, \
18477c129bfSVenkat Reddy Talla .ops = &tps65132_regulator_ops, \
18577c129bfSVenkat Reddy Talla .n_voltages = TPS65132_VOUT_N_VOLTAGE, \
18677c129bfSVenkat Reddy Talla .min_uV = TPS65132_VOUT_VMIN, \
18777c129bfSVenkat Reddy Talla .uV_step = TPS65132_VOUT_STEP, \
18877c129bfSVenkat Reddy Talla .enable_time = 500, \
18977c129bfSVenkat Reddy Talla .vsel_mask = TPS65132_VOUT_MASK, \
19077c129bfSVenkat Reddy Talla .vsel_reg = TPS65132_REG_##_id, \
19177c129bfSVenkat Reddy Talla .active_discharge_off = 0, \
19277c129bfSVenkat Reddy Talla .active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
19377c129bfSVenkat Reddy Talla .active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
19477c129bfSVenkat Reddy Talla .active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
19577c129bfSVenkat Reddy Talla .type = REGULATOR_VOLTAGE, \
19677c129bfSVenkat Reddy Talla .owner = THIS_MODULE, \
19777c129bfSVenkat Reddy Talla }
19877c129bfSVenkat Reddy Talla
1994e105e3bSAxel Lin static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
20077c129bfSVenkat Reddy Talla TPS65132_REGULATOR_DESC(VPOS, outp),
20177c129bfSVenkat Reddy Talla TPS65132_REGULATOR_DESC(VNEG, outn),
20277c129bfSVenkat Reddy Talla };
20377c129bfSVenkat Reddy Talla
20477c129bfSVenkat Reddy Talla static const struct regmap_range tps65132_no_reg_ranges[] = {
20577c129bfSVenkat Reddy Talla regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
20677c129bfSVenkat Reddy Talla TPS65132_REG_CONTROL - 1),
20777c129bfSVenkat Reddy Talla };
20877c129bfSVenkat Reddy Talla
20977c129bfSVenkat Reddy Talla static const struct regmap_access_table tps65132_no_reg_table = {
21077c129bfSVenkat Reddy Talla .no_ranges = tps65132_no_reg_ranges,
21177c129bfSVenkat Reddy Talla .n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
21277c129bfSVenkat Reddy Talla };
21377c129bfSVenkat Reddy Talla
21477c129bfSVenkat Reddy Talla static const struct regmap_config tps65132_regmap_config = {
21577c129bfSVenkat Reddy Talla .reg_bits = 8,
21677c129bfSVenkat Reddy Talla .val_bits = 8,
2175abca06cSAxel Lin .max_register = TPS65132_REG_CONTROL,
21877c129bfSVenkat Reddy Talla .cache_type = REGCACHE_NONE,
21977c129bfSVenkat Reddy Talla .rd_table = &tps65132_no_reg_table,
22077c129bfSVenkat Reddy Talla .wr_table = &tps65132_no_reg_table,
22177c129bfSVenkat Reddy Talla };
22277c129bfSVenkat Reddy Talla
tps65132_probe(struct i2c_client * client)22377e29598SAxel Lin static int tps65132_probe(struct i2c_client *client)
22477c129bfSVenkat Reddy Talla {
22577c129bfSVenkat Reddy Talla struct device *dev = &client->dev;
22677c129bfSVenkat Reddy Talla struct tps65132_regulator *tps;
2272e62e033SAxel Lin struct regulator_dev *rdev;
2282e62e033SAxel Lin struct regmap *rmap;
22977c129bfSVenkat Reddy Talla struct regulator_config config = { };
23077c129bfSVenkat Reddy Talla int id;
23177c129bfSVenkat Reddy Talla int ret;
23277c129bfSVenkat Reddy Talla
23377c129bfSVenkat Reddy Talla tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
23477c129bfSVenkat Reddy Talla if (!tps)
23577c129bfSVenkat Reddy Talla return -ENOMEM;
23677c129bfSVenkat Reddy Talla
2372e62e033SAxel Lin rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
2382e62e033SAxel Lin if (IS_ERR(rmap)) {
2392e62e033SAxel Lin ret = PTR_ERR(rmap);
24077c129bfSVenkat Reddy Talla dev_err(dev, "regmap init failed: %d\n", ret);
24177c129bfSVenkat Reddy Talla return ret;
24277c129bfSVenkat Reddy Talla }
24377c129bfSVenkat Reddy Talla
24477c129bfSVenkat Reddy Talla i2c_set_clientdata(client, tps);
24577c129bfSVenkat Reddy Talla tps->dev = dev;
24677c129bfSVenkat Reddy Talla
24777c129bfSVenkat Reddy Talla for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
2482e62e033SAxel Lin config.regmap = rmap;
24977c129bfSVenkat Reddy Talla config.dev = dev;
25077c129bfSVenkat Reddy Talla config.driver_data = tps;
25177c129bfSVenkat Reddy Talla
2522e62e033SAxel Lin rdev = devm_regulator_register(dev, &tps_regs_desc[id],
2532e62e033SAxel Lin &config);
2542e62e033SAxel Lin if (IS_ERR(rdev)) {
2552e62e033SAxel Lin ret = PTR_ERR(rdev);
25677c129bfSVenkat Reddy Talla dev_err(dev, "regulator %s register failed: %d\n",
2572e62e033SAxel Lin tps_regs_desc[id].name, ret);
25877c129bfSVenkat Reddy Talla return ret;
25977c129bfSVenkat Reddy Talla }
26077c129bfSVenkat Reddy Talla }
26177c129bfSVenkat Reddy Talla return 0;
26277c129bfSVenkat Reddy Talla }
26377c129bfSVenkat Reddy Talla
26477c129bfSVenkat Reddy Talla static const struct i2c_device_id tps65132_id[] = {
26577c129bfSVenkat Reddy Talla {.name = "tps65132",},
26677c129bfSVenkat Reddy Talla {},
26777c129bfSVenkat Reddy Talla };
26877c129bfSVenkat Reddy Talla MODULE_DEVICE_TABLE(i2c, tps65132_id);
26977c129bfSVenkat Reddy Talla
270*a469158eSAndré Apitzsch static const struct of_device_id __maybe_unused tps65132_of_match[] = {
271*a469158eSAndré Apitzsch { .compatible = "ti,tps65132" },
272*a469158eSAndré Apitzsch {},
273*a469158eSAndré Apitzsch };
274*a469158eSAndré Apitzsch MODULE_DEVICE_TABLE(of, tps65132_of_match);
275*a469158eSAndré Apitzsch
27677c129bfSVenkat Reddy Talla static struct i2c_driver tps65132_i2c_driver = {
27777c129bfSVenkat Reddy Talla .driver = {
27877c129bfSVenkat Reddy Talla .name = "tps65132",
279259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
280*a469158eSAndré Apitzsch .of_match_table = of_match_ptr(tps65132_of_match),
28177c129bfSVenkat Reddy Talla },
282964e1865SUwe Kleine-König .probe = tps65132_probe,
28377c129bfSVenkat Reddy Talla .id_table = tps65132_id,
28477c129bfSVenkat Reddy Talla };
28577c129bfSVenkat Reddy Talla
28677c129bfSVenkat Reddy Talla module_i2c_driver(tps65132_i2c_driver);
28777c129bfSVenkat Reddy Talla
28877c129bfSVenkat Reddy Talla MODULE_DESCRIPTION("tps65132 regulator driver");
28977c129bfSVenkat Reddy Talla MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
29077c129bfSVenkat Reddy Talla MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
29177c129bfSVenkat Reddy Talla MODULE_LICENSE("GPL v2");
292