xref: /linux/drivers/regulator/tps65132-regulator.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 /*
2  * TI TPS65132 Regulator driver
3  *
4  * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
5  *
6  * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
7  *		Laxman Dewangan <ldewangan@nvidia.com>
8  *
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.
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 
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>
28 
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
33 
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
39 
40 #define TPS65132_REG_APPS_DIS_VPOS		BIT(0)
41 #define TPS65132_REG_APPS_DIS_VNEG		BIT(1)
42 
43 #define TPS65132_REGULATOR_ID_VPOS	0
44 #define TPS65132_REGULATOR_ID_VNEG	1
45 #define TPS65132_MAX_REGULATORS		2
46 
47 #define TPS65132_ACT_DIS_TIME_SLACK		1000
48 
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;
53 	int ena_gpio_state;
54 };
55 
56 struct tps65132_regulator {
57 	struct device *dev;
58 	struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
59 };
60 
61 static int tps65132_regulator_enable(struct regulator_dev *rdev)
62 {
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];
66 	int ret;
67 
68 	if (!IS_ERR(rpdata->en_gpiod)) {
69 		gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
70 		rpdata->ena_gpio_state = 1;
71 	}
72 
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);
77 		if (ret < 0) {
78 			dev_err(tps->dev, "Failed to disable active discharge: %d\n",
79 				ret);
80 			return ret;
81 		}
82 	}
83 
84 	return 0;
85 }
86 
87 static int tps65132_regulator_disable(struct regulator_dev *rdev)
88 {
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];
92 
93 	if (!IS_ERR(rpdata->en_gpiod)) {
94 		gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
95 		rpdata->ena_gpio_state = 0;
96 	}
97 
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);
103 	}
104 
105 	return 0;
106 }
107 
108 static int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
109 {
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];
113 
114 	if (!IS_ERR(rpdata->en_gpiod))
115 		return rpdata->ena_gpio_state;
116 
117 	return 1;
118 }
119 
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,
129 };
130 
131 static int tps65132_of_parse_cb(struct device_node *np,
132 				const struct regulator_desc *desc,
133 				struct regulator_config *config)
134 {
135 	struct tps65132_regulator *tps = config->driver_data;
136 	struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
137 	int ret;
138 
139 	rpdata->en_gpiod = devm_fwnode_gpiod_get(tps->dev, of_fwnode_handle(np),
140 						 "enable", GPIOD_ASIS,
141 						 "enable");
142 	if (IS_ERR(rpdata->en_gpiod)) {
143 		ret = PTR_ERR(rpdata->en_gpiod);
144 
145 		/* Ignore the error other than probe defer */
146 		if (ret == -EPROBE_DEFER)
147 			return ret;
148 		return 0;
149 	}
150 
151 	rpdata->act_dis_gpiod = devm_fwnode_gpiod_get(tps->dev,
152 						      of_fwnode_handle(np),
153 						      "active-discharge",
154 						      GPIOD_ASIS,
155 						      "active-discharge");
156 	if (IS_ERR(rpdata->act_dis_gpiod)) {
157 		ret = PTR_ERR(rpdata->act_dis_gpiod);
158 
159 		/* Ignore the error other than probe defer */
160 		if (ret == -EPROBE_DEFER)
161 			return ret;
162 
163 		return 0;
164 	}
165 
166 	ret = of_property_read_u32(np, "ti,active-discharge-time-us",
167 				   &rpdata->act_dis_time_us);
168 	if (ret < 0) {
169 		dev_err(tps->dev, "Failed to read active discharge time:%d\n",
170 			ret);
171 		return ret;
172 	}
173 
174 	return 0;
175 }
176 
177 #define TPS65132_REGULATOR_DESC(_id, _name)		\
178 	[TPS65132_REGULATOR_ID_##_id] = {		\
179 		.name = "tps65132-"#_name,		\
180 		.supply_name = "vin",			\
181 		.id = TPS65132_REGULATOR_ID_##_id,	\
182 		.of_match = of_match_ptr(#_name),	\
183 		.of_parse_cb	= tps65132_of_parse_cb,	\
184 		.ops = &tps65132_regulator_ops,		\
185 		.n_voltages = TPS65132_VOUT_N_VOLTAGE,	\
186 		.min_uV = TPS65132_VOUT_VMIN,		\
187 		.uV_step = TPS65132_VOUT_STEP,		\
188 		.enable_time = 500,			\
189 		.vsel_mask = TPS65132_VOUT_MASK,	\
190 		.vsel_reg = TPS65132_REG_##_id,		\
191 		.active_discharge_off = 0,			\
192 		.active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
193 		.active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
194 		.active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
195 		.type = REGULATOR_VOLTAGE,		\
196 		.owner = THIS_MODULE,			\
197 	}
198 
199 static const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
200 	TPS65132_REGULATOR_DESC(VPOS, outp),
201 	TPS65132_REGULATOR_DESC(VNEG, outn),
202 };
203 
204 static const struct regmap_range tps65132_no_reg_ranges[] = {
205 	regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
206 			 TPS65132_REG_CONTROL - 1),
207 };
208 
209 static const struct regmap_access_table tps65132_no_reg_table = {
210 	.no_ranges = tps65132_no_reg_ranges,
211 	.n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
212 };
213 
214 static const struct regmap_config tps65132_regmap_config = {
215 	.reg_bits	= 8,
216 	.val_bits	= 8,
217 	.max_register	= TPS65132_REG_CONTROL,
218 	.cache_type	= REGCACHE_NONE,
219 	.rd_table	= &tps65132_no_reg_table,
220 	.wr_table	= &tps65132_no_reg_table,
221 };
222 
223 static int tps65132_probe(struct i2c_client *client)
224 {
225 	struct device *dev = &client->dev;
226 	struct tps65132_regulator *tps;
227 	struct regulator_dev *rdev;
228 	struct regmap *rmap;
229 	struct regulator_config config = { };
230 	int id;
231 	int ret;
232 
233 	tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
234 	if (!tps)
235 		return -ENOMEM;
236 
237 	rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
238 	if (IS_ERR(rmap)) {
239 		ret = PTR_ERR(rmap);
240 		dev_err(dev, "regmap init failed: %d\n", ret);
241 		return ret;
242 	}
243 
244 	i2c_set_clientdata(client, tps);
245 	tps->dev = dev;
246 
247 	for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
248 		config.regmap = rmap;
249 		config.dev = dev;
250 		config.driver_data = tps;
251 
252 		rdev = devm_regulator_register(dev, &tps_regs_desc[id],
253 					       &config);
254 		if (IS_ERR(rdev)) {
255 			ret = PTR_ERR(rdev);
256 			dev_err(dev, "regulator %s register failed: %d\n",
257 				tps_regs_desc[id].name, ret);
258 			return ret;
259 		}
260 	}
261 	return 0;
262 }
263 
264 static const struct i2c_device_id tps65132_id[] = {
265 	{.name = "tps65132",},
266 	{},
267 };
268 MODULE_DEVICE_TABLE(i2c, tps65132_id);
269 
270 static struct i2c_driver tps65132_i2c_driver = {
271 	.driver = {
272 		.name = "tps65132",
273 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
274 	},
275 	.probe = tps65132_probe,
276 	.id_table = tps65132_id,
277 };
278 
279 module_i2c_driver(tps65132_i2c_driver);
280 
281 MODULE_DESCRIPTION("tps65132 regulator driver");
282 MODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
283 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
284 MODULE_LICENSE("GPL v2");
285