xref: /openbmc/linux/drivers/hwmon/tps23861.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2020 Sartura Ltd.
4  *
5  * Driver for the TI TPS23861 PoE PSE.
6  *
7  * Author: Robert Marko <robert.marko@sartura.hr>
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/debugfs.h>
12 #include <linux/delay.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/hwmon.h>
15 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 
20 #define TEMPERATURE			0x2c
21 #define INPUT_VOLTAGE_LSB		0x2e
22 #define INPUT_VOLTAGE_MSB		0x2f
23 #define PORT_1_CURRENT_LSB		0x30
24 #define PORT_1_CURRENT_MSB		0x31
25 #define PORT_1_VOLTAGE_LSB		0x32
26 #define PORT_1_VOLTAGE_MSB		0x33
27 #define PORT_2_CURRENT_LSB		0x34
28 #define PORT_2_CURRENT_MSB		0x35
29 #define PORT_2_VOLTAGE_LSB		0x36
30 #define PORT_2_VOLTAGE_MSB		0x37
31 #define PORT_3_CURRENT_LSB		0x38
32 #define PORT_3_CURRENT_MSB		0x39
33 #define PORT_3_VOLTAGE_LSB		0x3a
34 #define PORT_3_VOLTAGE_MSB		0x3b
35 #define PORT_4_CURRENT_LSB		0x3c
36 #define PORT_4_CURRENT_MSB		0x3d
37 #define PORT_4_VOLTAGE_LSB		0x3e
38 #define PORT_4_VOLTAGE_MSB		0x3f
39 #define PORT_N_CURRENT_LSB_OFFSET	0x04
40 #define PORT_N_VOLTAGE_LSB_OFFSET	0x04
41 #define VOLTAGE_CURRENT_MASK		GENMASK(13, 0)
42 #define PORT_1_RESISTANCE_LSB		0x60
43 #define PORT_1_RESISTANCE_MSB		0x61
44 #define PORT_2_RESISTANCE_LSB		0x62
45 #define PORT_2_RESISTANCE_MSB		0x63
46 #define PORT_3_RESISTANCE_LSB		0x64
47 #define PORT_3_RESISTANCE_MSB		0x65
48 #define PORT_4_RESISTANCE_LSB		0x66
49 #define PORT_4_RESISTANCE_MSB		0x67
50 #define PORT_N_RESISTANCE_LSB_OFFSET	0x02
51 #define PORT_RESISTANCE_MASK		GENMASK(13, 0)
52 #define PORT_RESISTANCE_RSN_MASK	GENMASK(15, 14)
53 #define PORT_RESISTANCE_RSN_OTHER	0
54 #define PORT_RESISTANCE_RSN_LOW		1
55 #define PORT_RESISTANCE_RSN_OPEN	2
56 #define PORT_RESISTANCE_RSN_SHORT	3
57 #define PORT_1_STATUS			0x0c
58 #define PORT_2_STATUS			0x0d
59 #define PORT_3_STATUS			0x0e
60 #define PORT_4_STATUS			0x0f
61 #define PORT_STATUS_CLASS_MASK		GENMASK(7, 4)
62 #define PORT_STATUS_DETECT_MASK		GENMASK(3, 0)
63 #define PORT_CLASS_UNKNOWN		0
64 #define PORT_CLASS_1			1
65 #define PORT_CLASS_2			2
66 #define PORT_CLASS_3			3
67 #define PORT_CLASS_4			4
68 #define PORT_CLASS_RESERVED		5
69 #define PORT_CLASS_0			6
70 #define PORT_CLASS_OVERCURRENT		7
71 #define PORT_CLASS_MISMATCH		8
72 #define PORT_DETECT_UNKNOWN		0
73 #define PORT_DETECT_SHORT		1
74 #define PORT_DETECT_RESERVED		2
75 #define PORT_DETECT_RESISTANCE_LOW	3
76 #define PORT_DETECT_RESISTANCE_OK	4
77 #define PORT_DETECT_RESISTANCE_HIGH	5
78 #define PORT_DETECT_OPEN_CIRCUIT	6
79 #define PORT_DETECT_RESERVED_2		7
80 #define PORT_DETECT_MOSFET_FAULT	8
81 #define PORT_DETECT_LEGACY		9
82 /* Measurment beyond clamp voltage */
83 #define PORT_DETECT_CAPACITANCE_INVALID_BEYOND	10
84 /* Insufficient voltage delta */
85 #define PORT_DETECT_CAPACITANCE_INVALID_DELTA	11
86 #define PORT_DETECT_CAPACITANCE_OUT_OF_RANGE	12
87 #define POE_PLUS			0x40
88 #define OPERATING_MODE			0x12
89 #define OPERATING_MODE_OFF		0
90 #define OPERATING_MODE_MANUAL		1
91 #define OPERATING_MODE_SEMI		2
92 #define OPERATING_MODE_AUTO		3
93 #define OPERATING_MODE_PORT_1_MASK	GENMASK(1, 0)
94 #define OPERATING_MODE_PORT_2_MASK	GENMASK(3, 2)
95 #define OPERATING_MODE_PORT_3_MASK	GENMASK(5, 4)
96 #define OPERATING_MODE_PORT_4_MASK	GENMASK(7, 6)
97 
98 #define DETECT_CLASS_RESTART		0x18
99 #define POWER_ENABLE			0x19
100 #define TPS23861_NUM_PORTS		4
101 
102 #define TEMPERATURE_LSB			652 /* 0.652 degrees Celsius */
103 #define VOLTAGE_LSB			3662 /* 3.662 mV */
104 #define SHUNT_RESISTOR_DEFAULT		255000 /* 255 mOhm */
105 #define CURRENT_LSB_255			62260 /* 62.260 uA */
106 #define CURRENT_LSB_250			61039 /* 61.039 uA */
107 #define RESISTANCE_LSB			110966 /* 11.0966 Ohm*/
108 #define RESISTANCE_LSB_LOW		157216 /* 15.7216 Ohm*/
109 
110 struct tps23861_data {
111 	struct regmap *regmap;
112 	u32 shunt_resistor;
113 	struct i2c_client *client;
114 	struct dentry *debugfs_dir;
115 };
116 
117 static struct regmap_config tps23861_regmap_config = {
118 	.reg_bits = 8,
119 	.val_bits = 8,
120 };
121 
122 static int tps23861_read_temp(struct tps23861_data *data, long *val)
123 {
124 	unsigned int regval;
125 	int err;
126 
127 	err = regmap_read(data->regmap, TEMPERATURE, &regval);
128 	if (err < 0)
129 		return err;
130 
131 	*val = (regval * TEMPERATURE_LSB) - 20000;
132 
133 	return 0;
134 }
135 
136 static int tps23861_read_voltage(struct tps23861_data *data, int channel,
137 				 long *val)
138 {
139 	unsigned int regval;
140 	int err;
141 
142 	if (channel < TPS23861_NUM_PORTS) {
143 		err = regmap_bulk_read(data->regmap,
144 				       PORT_1_VOLTAGE_LSB + channel * PORT_N_VOLTAGE_LSB_OFFSET,
145 				       &regval, 2);
146 	} else {
147 		err = regmap_bulk_read(data->regmap,
148 				       INPUT_VOLTAGE_LSB,
149 				       &regval, 2);
150 	}
151 	if (err < 0)
152 		return err;
153 
154 	*val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * VOLTAGE_LSB) / 1000;
155 
156 	return 0;
157 }
158 
159 static int tps23861_read_current(struct tps23861_data *data, int channel,
160 				 long *val)
161 {
162 	unsigned int current_lsb;
163 	unsigned int regval;
164 	int err;
165 
166 	if (data->shunt_resistor == SHUNT_RESISTOR_DEFAULT)
167 		current_lsb = CURRENT_LSB_255;
168 	else
169 		current_lsb = CURRENT_LSB_250;
170 
171 	err = regmap_bulk_read(data->regmap,
172 			       PORT_1_CURRENT_LSB + channel * PORT_N_CURRENT_LSB_OFFSET,
173 			       &regval, 2);
174 	if (err < 0)
175 		return err;
176 
177 	*val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * current_lsb) / 1000000;
178 
179 	return 0;
180 }
181 
182 static int tps23861_port_disable(struct tps23861_data *data, int channel)
183 {
184 	unsigned int regval = 0;
185 	int err;
186 
187 	regval |= BIT(channel + 4);
188 	err = regmap_write(data->regmap, POWER_ENABLE, regval);
189 
190 	return err;
191 }
192 
193 static int tps23861_port_enable(struct tps23861_data *data, int channel)
194 {
195 	unsigned int regval = 0;
196 	int err;
197 
198 	regval |= BIT(channel);
199 	regval |= BIT(channel + 4);
200 	err = regmap_write(data->regmap, DETECT_CLASS_RESTART, regval);
201 
202 	return err;
203 }
204 
205 static umode_t tps23861_is_visible(const void *data, enum hwmon_sensor_types type,
206 				   u32 attr, int channel)
207 {
208 	switch (type) {
209 	case hwmon_temp:
210 		switch (attr) {
211 		case hwmon_temp_input:
212 		case hwmon_temp_label:
213 			return 0444;
214 		default:
215 			return 0;
216 		}
217 	case hwmon_in:
218 		switch (attr) {
219 		case hwmon_in_input:
220 		case hwmon_in_label:
221 			return 0444;
222 		case hwmon_in_enable:
223 			return 0200;
224 		default:
225 			return 0;
226 		}
227 	case hwmon_curr:
228 		switch (attr) {
229 		case hwmon_curr_input:
230 		case hwmon_curr_label:
231 			return 0444;
232 		default:
233 			return 0;
234 		}
235 	default:
236 		return 0;
237 	}
238 }
239 
240 static int tps23861_write(struct device *dev, enum hwmon_sensor_types type,
241 			  u32 attr, int channel, long val)
242 {
243 	struct tps23861_data *data = dev_get_drvdata(dev);
244 	int err;
245 
246 	switch (type) {
247 	case hwmon_in:
248 		switch (attr) {
249 		case hwmon_in_enable:
250 			if (val == 0)
251 				err = tps23861_port_disable(data, channel);
252 			else if (val == 1)
253 				err = tps23861_port_enable(data, channel);
254 			else
255 				err = -EINVAL;
256 			break;
257 		default:
258 			return -EOPNOTSUPP;
259 		}
260 		break;
261 	default:
262 		return -EOPNOTSUPP;
263 	}
264 
265 	return err;
266 }
267 
268 static int tps23861_read(struct device *dev, enum hwmon_sensor_types type,
269 			 u32 attr, int channel, long *val)
270 {
271 	struct tps23861_data *data = dev_get_drvdata(dev);
272 	int err;
273 
274 	switch (type) {
275 	case hwmon_temp:
276 		switch (attr) {
277 		case hwmon_temp_input:
278 			err = tps23861_read_temp(data, val);
279 			break;
280 		default:
281 			return -EOPNOTSUPP;
282 		}
283 		break;
284 	case hwmon_in:
285 		switch (attr) {
286 		case hwmon_in_input:
287 			err = tps23861_read_voltage(data, channel, val);
288 			break;
289 		default:
290 			return -EOPNOTSUPP;
291 		}
292 		break;
293 	case hwmon_curr:
294 		switch (attr) {
295 		case hwmon_curr_input:
296 			err = tps23861_read_current(data, channel, val);
297 			break;
298 		default:
299 			return -EOPNOTSUPP;
300 		}
301 		break;
302 	default:
303 		return -EOPNOTSUPP;
304 	}
305 
306 	return err;
307 }
308 
309 static const char * const tps23861_port_label[] = {
310 	"Port1",
311 	"Port2",
312 	"Port3",
313 	"Port4",
314 	"Input",
315 };
316 
317 static int tps23861_read_string(struct device *dev,
318 				enum hwmon_sensor_types type,
319 				u32 attr, int channel, const char **str)
320 {
321 	switch (type) {
322 	case hwmon_in:
323 	case hwmon_curr:
324 		*str = tps23861_port_label[channel];
325 		break;
326 	case hwmon_temp:
327 		*str = "Die";
328 		break;
329 	default:
330 		return -EOPNOTSUPP;
331 	}
332 
333 	return 0;
334 }
335 
336 static const struct hwmon_channel_info *tps23861_info[] = {
337 	HWMON_CHANNEL_INFO(chip,
338 			   HWMON_C_REGISTER_TZ),
339 	HWMON_CHANNEL_INFO(temp,
340 			   HWMON_T_INPUT | HWMON_T_LABEL),
341 	HWMON_CHANNEL_INFO(in,
342 			   HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
343 			   HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
344 			   HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
345 			   HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
346 			   HWMON_I_INPUT | HWMON_I_LABEL),
347 	HWMON_CHANNEL_INFO(curr,
348 			   HWMON_C_INPUT | HWMON_C_LABEL,
349 			   HWMON_C_INPUT | HWMON_C_LABEL,
350 			   HWMON_C_INPUT | HWMON_C_LABEL,
351 			   HWMON_C_INPUT | HWMON_C_LABEL),
352 	NULL
353 };
354 
355 static const struct hwmon_ops tps23861_hwmon_ops = {
356 	.is_visible = tps23861_is_visible,
357 	.write = tps23861_write,
358 	.read = tps23861_read,
359 	.read_string = tps23861_read_string,
360 };
361 
362 static const struct hwmon_chip_info tps23861_chip_info = {
363 	.ops = &tps23861_hwmon_ops,
364 	.info = tps23861_info,
365 };
366 
367 static char *tps23861_port_operating_mode(struct tps23861_data *data, int port)
368 {
369 	unsigned int regval;
370 	int mode;
371 
372 	regmap_read(data->regmap, OPERATING_MODE, &regval);
373 
374 	switch (port) {
375 	case 1:
376 		mode = FIELD_GET(OPERATING_MODE_PORT_1_MASK, regval);
377 		break;
378 	case 2:
379 		mode = FIELD_GET(OPERATING_MODE_PORT_2_MASK, regval);
380 		break;
381 	case 3:
382 		mode = FIELD_GET(OPERATING_MODE_PORT_3_MASK, regval);
383 		break;
384 	case 4:
385 		mode = FIELD_GET(OPERATING_MODE_PORT_4_MASK, regval);
386 		break;
387 	default:
388 		mode = -EINVAL;
389 	}
390 
391 	switch (mode) {
392 	case OPERATING_MODE_OFF:
393 		return "Off";
394 	case OPERATING_MODE_MANUAL:
395 		return "Manual";
396 	case OPERATING_MODE_SEMI:
397 		return "Semi-Auto";
398 	case OPERATING_MODE_AUTO:
399 		return "Auto";
400 	default:
401 		return "Invalid";
402 	}
403 }
404 
405 static char *tps23861_port_detect_status(struct tps23861_data *data, int port)
406 {
407 	unsigned int regval;
408 
409 	regmap_read(data->regmap,
410 		    PORT_1_STATUS + (port - 1),
411 		    &regval);
412 
413 	switch (FIELD_GET(PORT_STATUS_DETECT_MASK, regval)) {
414 	case PORT_DETECT_UNKNOWN:
415 		return "Unknown device";
416 	case PORT_DETECT_SHORT:
417 		return "Short circuit";
418 	case PORT_DETECT_RESISTANCE_LOW:
419 		return "Too low resistance";
420 	case PORT_DETECT_RESISTANCE_OK:
421 		return "Valid resistance";
422 	case PORT_DETECT_RESISTANCE_HIGH:
423 		return "Too high resistance";
424 	case PORT_DETECT_OPEN_CIRCUIT:
425 		return "Open circuit";
426 	case PORT_DETECT_MOSFET_FAULT:
427 		return "MOSFET fault";
428 	case PORT_DETECT_LEGACY:
429 		return "Legacy device";
430 	case PORT_DETECT_CAPACITANCE_INVALID_BEYOND:
431 		return "Invalid capacitance, beyond clamp voltage";
432 	case PORT_DETECT_CAPACITANCE_INVALID_DELTA:
433 		return "Invalid capacitance, insufficient voltage delta";
434 	case PORT_DETECT_CAPACITANCE_OUT_OF_RANGE:
435 		return "Valid capacitance, outside of legacy range";
436 	case PORT_DETECT_RESERVED:
437 	case PORT_DETECT_RESERVED_2:
438 	default:
439 		return "Invalid";
440 	}
441 }
442 
443 static char *tps23861_port_class_status(struct tps23861_data *data, int port)
444 {
445 	unsigned int regval;
446 
447 	regmap_read(data->regmap,
448 		    PORT_1_STATUS + (port - 1),
449 		    &regval);
450 
451 	switch (FIELD_GET(PORT_STATUS_CLASS_MASK, regval)) {
452 	case PORT_CLASS_UNKNOWN:
453 		return "Unknown";
454 	case PORT_CLASS_RESERVED:
455 	case PORT_CLASS_0:
456 		return "0";
457 	case PORT_CLASS_1:
458 		return "1";
459 	case PORT_CLASS_2:
460 		return "2";
461 	case PORT_CLASS_3:
462 		return "3";
463 	case PORT_CLASS_4:
464 		return "4";
465 	case PORT_CLASS_OVERCURRENT:
466 		return "Overcurrent";
467 	case PORT_CLASS_MISMATCH:
468 		return "Mismatch";
469 	default:
470 		return "Invalid";
471 	}
472 }
473 
474 static char *tps23861_port_poe_plus_status(struct tps23861_data *data, int port)
475 {
476 	unsigned int regval;
477 
478 	regmap_read(data->regmap, POE_PLUS, &regval);
479 
480 	if (BIT(port + 3) & regval)
481 		return "Yes";
482 	else
483 		return "No";
484 }
485 
486 static int tps23861_port_resistance(struct tps23861_data *data, int port)
487 {
488 	u16 regval;
489 
490 	regmap_bulk_read(data->regmap,
491 			 PORT_1_RESISTANCE_LSB + PORT_N_RESISTANCE_LSB_OFFSET * (port - 1),
492 			 &regval,
493 			 2);
494 
495 	switch (FIELD_GET(PORT_RESISTANCE_RSN_MASK, regval)) {
496 	case PORT_RESISTANCE_RSN_OTHER:
497 		return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB) / 10000;
498 	case PORT_RESISTANCE_RSN_LOW:
499 		return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB_LOW) / 10000;
500 	case PORT_RESISTANCE_RSN_SHORT:
501 	case PORT_RESISTANCE_RSN_OPEN:
502 	default:
503 		return 0;
504 	}
505 }
506 
507 static int tps23861_port_status_show(struct seq_file *s, void *data)
508 {
509 	struct tps23861_data *priv = s->private;
510 	int i;
511 
512 	for (i = 1; i < TPS23861_NUM_PORTS + 1; i++) {
513 		seq_printf(s, "Port: \t\t%d\n", i);
514 		seq_printf(s, "Operating mode: %s\n", tps23861_port_operating_mode(priv, i));
515 		seq_printf(s, "Detected: \t%s\n", tps23861_port_detect_status(priv, i));
516 		seq_printf(s, "Class: \t\t%s\n", tps23861_port_class_status(priv, i));
517 		seq_printf(s, "PoE Plus: \t%s\n", tps23861_port_poe_plus_status(priv, i));
518 		seq_printf(s, "Resistance: \t%d\n", tps23861_port_resistance(priv, i));
519 		seq_putc(s, '\n');
520 	}
521 
522 	return 0;
523 }
524 
525 DEFINE_SHOW_ATTRIBUTE(tps23861_port_status);
526 
527 static void tps23861_init_debugfs(struct tps23861_data *data)
528 {
529 	data->debugfs_dir = debugfs_create_dir(data->client->name, NULL);
530 
531 	debugfs_create_file("port_status",
532 			    0400,
533 			    data->debugfs_dir,
534 			    data,
535 			    &tps23861_port_status_fops);
536 }
537 
538 static int tps23861_probe(struct i2c_client *client)
539 {
540 	struct device *dev = &client->dev;
541 	struct tps23861_data *data;
542 	struct device *hwmon_dev;
543 	u32 shunt_resistor;
544 
545 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
546 	if (!data)
547 		return -ENOMEM;
548 
549 	data->client = client;
550 	i2c_set_clientdata(client, data);
551 
552 	data->regmap = devm_regmap_init_i2c(client, &tps23861_regmap_config);
553 	if (IS_ERR(data->regmap)) {
554 		dev_err(dev, "failed to allocate register map\n");
555 		return PTR_ERR(data->regmap);
556 	}
557 
558 	if (!of_property_read_u32(dev->of_node, "shunt-resistor-micro-ohms", &shunt_resistor))
559 		data->shunt_resistor = shunt_resistor;
560 	else
561 		data->shunt_resistor = SHUNT_RESISTOR_DEFAULT;
562 
563 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
564 							 data, &tps23861_chip_info,
565 							 NULL);
566 	if (IS_ERR(hwmon_dev))
567 		return PTR_ERR(hwmon_dev);
568 
569 	tps23861_init_debugfs(data);
570 
571 	return 0;
572 }
573 
574 static int tps23861_remove(struct i2c_client *client)
575 {
576 	struct tps23861_data *data = i2c_get_clientdata(client);
577 
578 	debugfs_remove_recursive(data->debugfs_dir);
579 
580 	return 0;
581 }
582 
583 static const struct of_device_id __maybe_unused tps23861_of_match[] = {
584 	{ .compatible = "ti,tps23861", },
585 	{ },
586 };
587 MODULE_DEVICE_TABLE(of, tps23861_of_match);
588 
589 static struct i2c_driver tps23861_driver = {
590 	.probe_new		= tps23861_probe,
591 	.remove			= tps23861_remove,
592 	.driver = {
593 		.name		= "tps23861",
594 		.of_match_table	= of_match_ptr(tps23861_of_match),
595 	},
596 };
597 module_i2c_driver(tps23861_driver);
598 
599 MODULE_LICENSE("GPL");
600 MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
601 MODULE_DESCRIPTION("TI TPS23861 PoE PSE");
602