xref: /linux/drivers/regulator/tps65185.c (revision 49bda4826843be0ef97a162009a29ea3a63f3935)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2025 Andreas Kemnade
3 
4 /* Datasheet: https://www.ti.com/lit/gpn/tps65185 */
5 
6 #include <linux/cleanup.h>
7 #include <linux/completion.h>
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/hwmon.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/property.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/regmap.h>
20 
21 #define TPS65185_REG_TMST_VALUE 0
22 #define TPS65185_REG_ENABLE 1
23 #define TPS65185_REG_VADJ 2
24 #define TPS65185_REG_VCOM1 3
25 #define TPS65185_REG_VCOM2 4
26 #define TPS65185_REG_INT_EN1 5
27 #define TPS65185_REG_INT_EN2 6
28 #define TPS65185_REG_INT1 7
29 #define TPS65185_REG_INT2 8
30 #define TPS65185_REG_TMST1 0xd
31 #define TPS65185_REG_TMST2 0xe
32 #define TPS65185_REG_PG 0xf
33 #define TPS65185_REG_REVID 0x10
34 
35 #define TPS65185_READ_THERM BIT(7)
36 #define TPS65185_CONV_END BIT(5)
37 
38 #define TPS65185_ENABLE_ACTIVE BIT(7)
39 #define TPS65185_ENABLE_STANDBY BIT(6)
40 
41 #define PGOOD_TIMEOUT_MSECS 200
42 
43 struct tps65185_data {
44 	struct device *dev;
45 	struct regmap *regmap;
46 	struct gpio_desc *pgood_gpio;
47 	struct gpio_desc *pwrup_gpio;
48 	struct gpio_desc *vcom_ctrl_gpio;
49 	struct gpio_desc *wakeup_gpio;
50 	struct completion pgood_completion;
51 	int pgood_irq;
52 	struct completion tmst_completion;
53 };
54 
55 static const struct hwmon_channel_info *tps65185_info[] = {
56 	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
57 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
58 	NULL
59 };
60 
tps65185_hwmon_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * temp)61 static int tps65185_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
62 			       u32 attr, int channel, long *temp)
63 {
64 	struct tps65185_data *data = dev_get_drvdata(dev);
65 	unsigned int val;
66 	int ret;
67 
68 	reinit_completion(&data->tmst_completion);
69 	/* start acquisition */
70 	regmap_update_bits(data->regmap, TPS65185_REG_TMST1,
71 			   TPS65185_READ_THERM, TPS65185_READ_THERM);
72 	wait_for_completion_timeout(&data->tmst_completion,
73 				    msecs_to_jiffies(PGOOD_TIMEOUT_MSECS));
74 	ret = regmap_read(data->regmap, TPS65185_REG_TMST1, &val);
75 	if (!(val & TPS65185_CONV_END))
76 		return -ETIMEDOUT;
77 
78 	ret = regmap_read(data->regmap, TPS65185_REG_TMST_VALUE, &val);
79 	if (ret)
80 		return ret;
81 
82 	*temp = (s8)val * 1000;
83 
84 	return 0;
85 }
86 
tps65185_hwmon_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)87 static umode_t tps65185_hwmon_is_visible(const void *data,
88 					 enum hwmon_sensor_types type,
89 					 u32 attr, int channel)
90 {
91 	return 0444;
92 }
93 
94 static const struct hwmon_ops tps65185_hwmon_ops = {
95 	.is_visible = tps65185_hwmon_is_visible,
96 	.read = tps65185_hwmon_read,
97 };
98 
99 static const struct hwmon_chip_info tps65185_chip_info = {
100 	.ops = &tps65185_hwmon_ops,
101 	.info = tps65185_info,
102 };
103 
tps65185_volatile_reg(struct device * dev,unsigned int reg)104 static bool tps65185_volatile_reg(struct device *dev, unsigned int reg)
105 {
106 	switch (reg) {
107 	case TPS65185_REG_TMST_VALUE:
108 	case TPS65185_REG_ENABLE:
109 	case TPS65185_REG_VCOM2:
110 	case TPS65185_REG_INT1:
111 	case TPS65185_REG_INT2:
112 	case TPS65185_REG_TMST1:
113 		return true;
114 	default:
115 		return false;
116 	}
117 }
118 
119 static const struct regmap_config regmap_config = {
120 	.reg_bits = 8,
121 	.val_bits = 8,
122 	.max_register = 0x10,
123 	.cache_type = REGCACHE_MAPLE,
124 	.volatile_reg = tps65185_volatile_reg,
125 };
126 
127 static const struct regulator_ops tps65185_v3p3ops = {
128 	.list_voltage = regulator_list_voltage_linear,
129 	.enable = regulator_enable_regmap,
130 	.disable = regulator_disable_regmap,
131 	.is_enabled = regulator_is_enabled_regmap,
132 };
133 
tps65185_check_powergood(struct regulator_dev * rdev)134 static int tps65185_check_powergood(struct regulator_dev *rdev)
135 {
136 	struct tps65185_data *data = rdev_get_drvdata(rdev);
137 
138 	return gpiod_get_value_cansleep(data->pgood_gpio);
139 }
140 
tps65185_vposneg_get_voltage_sel(struct regulator_dev * rdev)141 static int tps65185_vposneg_get_voltage_sel(struct regulator_dev *rdev)
142 {
143 	int ret;
144 
145 	ret = regulator_get_voltage_sel_regmap(rdev);
146 	if (ret < 0)
147 		return ret;
148 
149 	/* highest value is lowest voltage */
150 	return 6 - ret;
151 }
152 
tps65185_vposneg_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)153 static int tps65185_vposneg_set_voltage_sel(struct regulator_dev *rdev, unsigned int selector)
154 {
155 	return regulator_set_voltage_sel_regmap(rdev, 6 - selector);
156 }
157 
pgood_handler(int irq,void * dev_id)158 static irqreturn_t pgood_handler(int irq, void *dev_id)
159 {
160 	struct tps65185_data *data = dev_id;
161 
162 	complete(&data->pgood_completion);
163 
164 	return IRQ_HANDLED;
165 }
166 
tps65185_vposneg_enable(struct regulator_dev * rdev)167 static int tps65185_vposneg_enable(struct regulator_dev *rdev)
168 {
169 	struct tps65185_data *data = rdev_get_drvdata(rdev);
170 	int ret;
171 
172 	reinit_completion(&data->pgood_completion);
173 	if (data->pwrup_gpio)
174 		ret = gpiod_set_value_cansleep(data->pwrup_gpio, 1);
175 	else
176 		ret = regmap_update_bits(data->regmap, TPS65185_REG_ENABLE,
177 					 TPS65185_ENABLE_ACTIVE,
178 					 TPS65185_ENABLE_ACTIVE);
179 
180 	if (ret)
181 		return ret;
182 
183 	dev_dbg(data->dev, "turning on...");
184 	wait_for_completion_timeout(&data->pgood_completion,
185 				    msecs_to_jiffies(PGOOD_TIMEOUT_MSECS));
186 	dev_dbg(data->dev, "turned on");
187 	ret = gpiod_get_value_cansleep(data->pgood_gpio);
188 	if (ret < 0)
189 		return ret;
190 	if (!ret)
191 		return -ETIMEDOUT;
192 
193 	return 0;
194 }
195 
tps65185_vposneg_disable(struct regulator_dev * rdev)196 static int tps65185_vposneg_disable(struct regulator_dev *rdev)
197 {
198 	struct tps65185_data *data = rdev_get_drvdata(rdev);
199 	int ret;
200 
201 	if (data->pwrup_gpio)
202 		ret = gpiod_set_value_cansleep(data->pwrup_gpio, 0);
203 	else
204 		ret = regmap_update_bits(data->regmap, TPS65185_REG_ENABLE,
205 					 TPS65185_ENABLE_STANDBY,
206 					 TPS65185_ENABLE_STANDBY);
207 
208 	return ret;
209 }
210 
tps65185_vcom_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)211 static int tps65185_vcom_set_voltage_sel(struct regulator_dev *rdev, unsigned int selector)
212 {
213 	struct tps65185_data *data = rdev_get_drvdata(rdev);
214 	int ret;
215 
216 	ret = regmap_update_bits(data->regmap, TPS65185_REG_VCOM2, BIT(0), selector >> 8);
217 	if (ret < 0)
218 		return ret;
219 
220 	return regmap_write(data->regmap, TPS65185_REG_VCOM1, selector & 0xFF);
221 }
222 
tps65185_vcom_get_voltage_sel(struct regulator_dev * rdev)223 static int tps65185_vcom_get_voltage_sel(struct regulator_dev *rdev)
224 {
225 	struct tps65185_data *data = rdev_get_drvdata(rdev);
226 	int ret;
227 	unsigned int sel, sel2;
228 
229 	ret = regmap_read(data->regmap, TPS65185_REG_VCOM1, &sel);
230 	if (ret < 0)
231 		return ret;
232 
233 	ret = regmap_read(data->regmap, TPS65185_REG_VCOM2, &sel2);
234 	if (ret < 0)
235 		return ret;
236 
237 	if (sel2 & BIT(0))
238 		sel |= 0x100;
239 
240 	return sel;
241 }
242 
243 static const struct regulator_ops tps65185_vcom_ops = {
244 	.list_voltage = regulator_list_voltage_linear,
245 	.map_voltage = regulator_map_voltage_linear,
246 	.set_voltage_sel = tps65185_vcom_set_voltage_sel,
247 	.get_voltage_sel = tps65185_vcom_get_voltage_sel,
248 };
249 
250 static const struct regulator_ops tps65185_vposneg_ops = {
251 	.list_voltage = regulator_list_voltage_linear,
252 	.map_voltage = regulator_map_voltage_linear,
253 	.enable = tps65185_vposneg_enable,
254 	.disable = tps65185_vposneg_disable,
255 	.is_enabled = tps65185_check_powergood,
256 	.set_voltage_sel = tps65185_vposneg_set_voltage_sel,
257 	.get_voltage_sel = tps65185_vposneg_get_voltage_sel,
258 };
259 
260 static const struct regulator_desc regulators[] = {
261 	{
262 		.name = "v3p3",
263 		.of_match = of_match_ptr("v3p3"),
264 		.regulators_node = of_match_ptr("regulators"),
265 		.id = 0,
266 		.ops = &tps65185_v3p3ops,
267 		.type = REGULATOR_VOLTAGE,
268 		.owner = THIS_MODULE,
269 		.enable_reg = TPS65185_REG_ENABLE,
270 		.enable_mask = BIT(5),
271 		.n_voltages = 1,
272 		.min_uV = 3300000,
273 	},
274 	{
275 		.name = "vposneg",
276 		.of_match = of_match_ptr("vposneg"),
277 		.regulators_node = of_match_ptr("regulators"),
278 		.id = 1,
279 		.ops = &tps65185_vposneg_ops,
280 		.type = REGULATOR_VOLTAGE,
281 		.owner = THIS_MODULE,
282 		.n_voltages = 4,
283 		.vsel_reg = TPS65185_REG_VADJ,
284 		.vsel_mask = 0x7,
285 		.min_uV = 14250000,
286 		.uV_step = 250000,
287 	}
288 };
289 
290 static const struct regulator_desc vcom_regulator_desc = {
291 	.name = "vcom",
292 	.of_match = of_match_ptr("vcom"),
293 	.regulators_node = of_match_ptr("regulators"),
294 	.supply_name = "vposneg",
295 	.id = 2,
296 	.ops = &tps65185_vcom_ops,
297 	.type = REGULATOR_VOLTAGE,
298 	.owner = THIS_MODULE,
299 	.n_voltages = 511,
300 	.min_uV = 0,
301 	.uV_step = 10000,
302 };
303 
tps65185_irq_thread(int irq,void * dev_id)304 static irqreturn_t tps65185_irq_thread(int irq, void *dev_id)
305 {
306 	struct tps65185_data *data = dev_id;
307 	unsigned int int_status_1, int_status_2;
308 	int ret;
309 
310 	/* read both status to have irq cleared */
311 	ret = regmap_read(data->regmap, TPS65185_REG_INT1, &int_status_1);
312 	if (ret)
313 		return IRQ_NONE;
314 
315 	ret = regmap_read(data->regmap, TPS65185_REG_INT2, &int_status_2);
316 	if (ret)
317 		return IRQ_NONE;
318 
319 	if (int_status_2 & BIT(0))
320 		complete(&data->tmst_completion);
321 
322 	dev_dbg(data->dev, "irq status %02x %02x\n", int_status_1, int_status_2);
323 
324 	if (int_status_1 || int_status_2)
325 		return IRQ_HANDLED;
326 
327 	return IRQ_NONE;
328 }
329 
tps65185_probe(struct i2c_client * client)330 static int tps65185_probe(struct i2c_client *client)
331 {
332 	struct tps65185_data *data;
333 	struct regulator_config config = { };
334 	struct regulator_dev *rdev;
335 	int ret = 0;
336 	int i;
337 
338 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
339 	if (!data)
340 		return -ENOMEM;
341 
342 	data->regmap = devm_regmap_init_i2c(client, &regmap_config);
343 	if (IS_ERR(data->regmap))
344 		return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
345 				     "failed to allocate regmap!\n");
346 
347 	data->pgood_gpio = devm_gpiod_get(&client->dev, "pwr-good", GPIOD_IN);
348 	if (IS_ERR(data->pgood_gpio))
349 		return dev_err_probe(&client->dev,
350 				     PTR_ERR(data->pgood_gpio),
351 				     "failed to get power good gpio\n");
352 
353 	data->pgood_irq = gpiod_to_irq(data->pgood_gpio);
354 	if (data->pgood_irq < 0)
355 		return data->pgood_irq;
356 
357 	data->pwrup_gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_LOW);
358 	if (IS_ERR(data->pwrup_gpio))
359 		return dev_err_probe(&client->dev, PTR_ERR(data->pwrup_gpio),
360 				     "failed to get pwrup gpio\n");
361 
362 	data->wakeup_gpio = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_OUT_HIGH);
363 	if (IS_ERR(data->wakeup_gpio))
364 		return dev_err_probe(&client->dev,
365 				     PTR_ERR(data->wakeup_gpio),
366 				     "failed to get wakeup gpio\n");
367 
368 	data->vcom_ctrl_gpio = devm_gpiod_get_optional(&client->dev, "vcom-ctrl", GPIOD_OUT_LOW);
369 	if (IS_ERR(data->vcom_ctrl_gpio))
370 		return dev_err_probe(&client->dev,
371 				     PTR_ERR(data->vcom_ctrl_gpio),
372 				     "failed to get vcm ctrl gpio\n");
373 
374 	ret = devm_regulator_get_enable(&client->dev, "vin");
375 	if (ret)
376 		return dev_err_probe(&client->dev, ret,
377 				     "failed to get vin regulator\n");
378 
379 	// TPS65185x PMIC for E Ink Vizplex Enabled Electronic Paper Display Chapter 7.6 Figure 2:
380 	// "Minimum delay time between WAKEUP rising edge and IC ready to accept I2C transaction."
381 	// https://www.ti.com/lit/ds/symlink/tps65185.pdf
382 	usleep_range(1800, 3000);
383 
384 	data->dev = &client->dev;
385 	i2c_set_clientdata(client, data);
386 
387 	init_completion(&data->pgood_completion);
388 	init_completion(&data->tmst_completion);
389 
390 	ret = devm_request_threaded_irq(&client->dev, data->pgood_irq, NULL,
391 					pgood_handler,
392 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
393 					"PGOOD", data);
394 	if (ret)
395 		return dev_err_probe(&client->dev, ret,
396 				     "failed to request power good irq\n");
397 
398 	if (client->irq) {
399 		ret = devm_request_threaded_irq(&client->dev, client->irq,
400 						NULL, tps65185_irq_thread,
401 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
402 						"tps65185", data);
403 		if (ret)
404 			return dev_err_probe(&client->dev, ret,
405 					     "failed to request irq\n");
406 	}
407 
408 	ret = regmap_update_bits(data->regmap, TPS65185_REG_INT_EN2, BIT(0), BIT(0));
409 	if (ret)
410 		return dev_err_probe(&client->dev, ret,
411 				     "failed to enable temp irq\n");
412 
413 	config.driver_data = data;
414 	config.dev = &client->dev;
415 	config.regmap = data->regmap;
416 
417 	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
418 		rdev = devm_regulator_register(&client->dev, &regulators[i],
419 					       &config);
420 		if (IS_ERR(rdev))
421 			return dev_err_probe(&client->dev, PTR_ERR(rdev),
422 					     "failed to register %s regulator\n",
423 					     regulators[i].name);
424 	}
425 
426 	config.ena_gpiod = data->vcom_ctrl_gpio;
427 	rdev = devm_regulator_register(&client->dev, &vcom_regulator_desc, &config);
428 	if (IS_ERR(rdev))
429 		return dev_err_probe(&client->dev, PTR_ERR(rdev),
430 				     "failed to register vcom regulator\n");
431 
432 	if (IS_REACHABLE(CONFIG_HWMON)) {
433 		struct device *hwmon_dev;
434 
435 		hwmon_dev = devm_hwmon_device_register_with_info(&client->dev, "tps65185", data,
436 								 &tps65185_chip_info, NULL);
437 		if (IS_ERR(hwmon_dev))
438 			dev_notice(&client->dev, "failed to register hwmon\n");
439 	}
440 
441 	return 0;
442 }
443 
444 static const struct of_device_id tps65185_dt_ids[] = {
445 	{
446 		.compatible = "ti,tps65185",
447 	}, {
448 		/* sentinel */
449 	}
450 };
451 MODULE_DEVICE_TABLE(of, tps65185_dt_ids);
452 
453 static struct i2c_driver tps65185_i2c_driver = {
454 	.driver = {
455 		   .name = "tps65185",
456 		   .of_match_table = tps65185_dt_ids,
457 	},
458 	.probe = tps65185_probe,
459 };
460 
461 module_i2c_driver(tps65185_i2c_driver);
462 
463 /* Module information */
464 MODULE_DESCRIPTION("TPS65185 regulator driver");
465 MODULE_LICENSE("GPL");
466 
467