2 * TI TPS65132 Regulator driver
4 * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
6 * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
7 * Laxman Dewangan <ldewangan@nvidia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
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.
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/i2c.h>
24 #include <linux/module.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
29 #define TPS65132_REG_VPOS 0x00
30 #define TPS65132_REG_VNEG 0x01
31 #define TPS65132_REG_APPS_DISP_DISN 0x03
32 #define TPS65132_REG_CONTROL 0x0FF
34 #define TPS65132_VOUT_MASK 0x1F
35 #define TPS65132_VOUT_N_VOLTAGE 0x15
36 #define TPS65132_VOUT_VMIN 4000000
37 #define TPS65132_VOUT_VMAX 6000000
38 #define TPS65132_VOUT_STEP 100000
40 #define TPS65132_REG_APPS_DIS_VPOS BIT(0)
41 #define TPS65132_REG_APPS_DIS_VNEG BIT(1)
43 #define TPS65132_REGULATOR_ID_VPOS 0
44 #define TPS65132_REGULATOR_ID_VNEG 1
45 #define TPS65132_MAX_REGULATORS 2
47 #define TPS65132_ACT_DIS_TIME_SLACK 1000
49 struct tps65132_reg_pdata {
50 struct gpio_desc *en_gpiod;
51 struct gpio_desc *act_dis_gpiod;
52 unsigned int act_dis_time_us;
56 struct tps65132_regulator {
58 struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
61 static int tps65132_regulator_enable(struct regulator_dev *rdev)
63 struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
64 int id = rdev_get_id(rdev);
65 struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
68 if (!IS_ERR(rpdata->en_gpiod)) {
69 gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
70 rpdata->ena_gpio_state = 1;
73 /* Hardware automatically enable discharge bit in enable */
74 if (rdev->constraints->active_discharge ==
75 REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
76 ret = regulator_set_active_discharge_regmap(rdev, false);
78 dev_err(tps->dev, "Failed to disable active discharge: %d\n",
87 static int tps65132_regulator_disable(struct regulator_dev *rdev)
89 struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
90 int id = rdev_get_id(rdev);
91 struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
93 if (!IS_ERR(rpdata->en_gpiod)) {
94 gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
95 rpdata->ena_gpio_state = 0;
98 if (!IS_ERR(rpdata->act_dis_gpiod)) {
99 gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
100 usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
101 TPS65132_ACT_DIS_TIME_SLACK);
102 gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
108 static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
110 struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
111 int id = rdev_get_id(rdev);
112 struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
114 if (!IS_ERR(rpdata->en_gpiod))
115 return rpdata->ena_gpio_state;
120 static const struct regulator_ops tps65132_regulator_ops = {
121 .enable = tps65132_regulator_enable,
122 .disable = tps65132_regulator_disable,
123 .is_enabled = tps65132_regulator_is_enabled,
124 .list_voltage = regulator_list_voltage_linear,
125 .map_voltage = regulator_map_voltage_linear,
126 .get_voltage_sel = regulator_get_voltage_sel_regmap,
127 .set_voltage_sel = regulator_set_voltage_sel_regmap,
128 .set_active_discharge = regulator_set_active_discharge_regmap,
131 static int tps65132_of_parse_cb(struct device_node *np,
132 const struct regulator_desc *desc,
133 struct regulator_config *config)
135 struct tps65132_regulator *tps = config->driver_data;
136 struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
139 rpdata->en_gpiod = devm_fwnode_get_index_gpiod_from_child(tps->dev,
140 "enable", 0, &np->fwnode, 0, "enable");
141 if (IS_ERR(rpdata->en_gpiod)) {
142 ret = PTR_ERR(rpdata->en_gpiod);
144 /* Ignore the error other than probe defer */
145 if (ret == -EPROBE_DEFER)
150 rpdata->act_dis_gpiod = devm_fwnode_get_index_gpiod_from_child(
151 tps->dev, "active-discharge", 0,
152 &np->fwnode, 0, "active-discharge");
153 if (IS_ERR(rpdata->act_dis_gpiod)) {
154 ret = PTR_ERR(rpdata->act_dis_gpiod);
156 /* Ignore the error other than probe defer */
157 if (ret == -EPROBE_DEFER)
163 ret = of_property_read_u32(np, "ti,active-discharge-time-us",
164 &rpdata->act_dis_time_us);
166 dev_err(tps->dev, "Failed to read active discharge time:%d\n",
174 #define TPS65132_REGULATOR_DESC(_id, _name) \
175 [TPS65132_REGULATOR_ID_##_id] = { \
176 .name = "tps65132-"#_name, \
177 .supply_name = "vin", \
178 .id = TPS65132_REGULATOR_ID_##_id, \
179 .of_match = of_match_ptr(#_name), \
180 .of_parse_cb = tps65132_of_parse_cb, \
181 .ops = &tps65132_regulator_ops, \
182 .n_voltages = TPS65132_VOUT_N_VOLTAGE, \
183 .min_uV = TPS65132_VOUT_VMIN, \
184 .uV_step = TPS65132_VOUT_STEP, \
185 .enable_time = 500, \
186 .vsel_mask = TPS65132_VOUT_MASK, \
187 .vsel_reg = TPS65132_REG_##_id, \
188 .active_discharge_off = 0, \
189 .active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
190 .active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
191 .active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
192 .type = REGULATOR_VOLTAGE, \
193 .owner = THIS_MODULE, \
196 static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
197 TPS65132_REGULATOR_DESC(VPOS, outp),
198 TPS65132_REGULATOR_DESC(VNEG, outn),
201 static const struct regmap_range tps65132_no_reg_ranges[] = {
202 regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
203 TPS65132_REG_CONTROL - 1),
206 static const struct regmap_access_table tps65132_no_reg_table = {
207 .no_ranges = tps65132_no_reg_ranges,
208 .n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
211 static const struct regmap_config tps65132_regmap_config = {
214 .max_register = TPS65132_REG_CONTROL,
215 .cache_type = REGCACHE_NONE,
216 .rd_table = &tps65132_no_reg_table,
217 .wr_table = &tps65132_no_reg_table,
220 static int tps65132_probe(struct i2c_client *client,
221 const struct i2c_device_id *client_id)
223 struct device *dev = &client->dev;
224 struct tps65132_regulator *tps;
225 struct regulator_dev *rdev;
227 struct regulator_config config = { };
231 tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
235 rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
238 dev_err(dev, "regmap init failed: %d\n", ret);
242 i2c_set_clientdata(client, tps);
245 for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
246 config.regmap = rmap;
248 config.driver_data = tps;
250 rdev = devm_regulator_register(dev, &tps_regs_desc[id],
254 dev_err(dev, "regulator %s register failed: %d\n",
255 tps_regs_desc[id].name, ret);
262 static const struct i2c_device_id tps65132_id[] = {
263 {.name = "tps65132",},
266 MODULE_DEVICE_TABLE(i2c, tps65132_id);
268 static struct i2c_driver tps65132_i2c_driver = {
272 .probe = tps65132_probe,
273 .id_table = tps65132_id,
276 module_i2c_driver(tps65132_i2c_driver);
278 MODULE_DESCRIPTION("tps65132 regulator driver");
279 MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
280 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
281 MODULE_LICENSE("GPL v2");