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