1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2020 Sartura Ltd.
5 * Driver for the TI TPS23861 PoE PSE.
7 * Author: Robert Marko <robert.marko@sartura.hr>
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>
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
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)
98 #define DETECT_CLASS_RESTART 0x18
99 #define POWER_ENABLE 0x19
100 #define TPS23861_NUM_PORTS 4
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*/
110 struct tps23861_data {
111 struct regmap *regmap;
113 struct i2c_client *client;
114 struct dentry *debugfs_dir;
117 static struct regmap_config tps23861_regmap_config = {
122 static int tps23861_read_temp(struct tps23861_data *data, long *val)
127 err = regmap_read(data->regmap, TEMPERATURE, ®val);
131 *val = (regval * TEMPERATURE_LSB) - 20000;
136 static int tps23861_read_voltage(struct tps23861_data *data, int channel,
142 if (channel < TPS23861_NUM_PORTS) {
143 err = regmap_bulk_read(data->regmap,
144 PORT_1_VOLTAGE_LSB + channel * PORT_N_VOLTAGE_LSB_OFFSET,
147 err = regmap_bulk_read(data->regmap,
154 *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * VOLTAGE_LSB) / 1000;
159 static int tps23861_read_current(struct tps23861_data *data, int channel,
162 unsigned int current_lsb;
166 if (data->shunt_resistor == SHUNT_RESISTOR_DEFAULT)
167 current_lsb = CURRENT_LSB_255;
169 current_lsb = CURRENT_LSB_250;
171 err = regmap_bulk_read(data->regmap,
172 PORT_1_CURRENT_LSB + channel * PORT_N_CURRENT_LSB_OFFSET,
177 *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * current_lsb) / 1000000;
182 static int tps23861_port_disable(struct tps23861_data *data, int channel)
184 unsigned int regval = 0;
187 regval |= BIT(channel + 4);
188 err = regmap_write(data->regmap, POWER_ENABLE, regval);
193 static int tps23861_port_enable(struct tps23861_data *data, int channel)
195 unsigned int regval = 0;
198 regval |= BIT(channel);
199 regval |= BIT(channel + 4);
200 err = regmap_write(data->regmap, DETECT_CLASS_RESTART, regval);
205 static umode_t tps23861_is_visible(const void *data, enum hwmon_sensor_types type,
206 u32 attr, int channel)
211 case hwmon_temp_input:
212 case hwmon_temp_label:
222 case hwmon_in_enable:
229 case hwmon_curr_input:
230 case hwmon_curr_label:
240 static int tps23861_write(struct device *dev, enum hwmon_sensor_types type,
241 u32 attr, int channel, long val)
243 struct tps23861_data *data = dev_get_drvdata(dev);
249 case hwmon_in_enable:
251 err = tps23861_port_disable(data, channel);
253 err = tps23861_port_enable(data, channel);
268 static int tps23861_read(struct device *dev, enum hwmon_sensor_types type,
269 u32 attr, int channel, long *val)
271 struct tps23861_data *data = dev_get_drvdata(dev);
277 case hwmon_temp_input:
278 err = tps23861_read_temp(data, val);
287 err = tps23861_read_voltage(data, channel, val);
295 case hwmon_curr_input:
296 err = tps23861_read_current(data, channel, val);
309 static const char * const tps23861_port_label[] = {
317 static int tps23861_read_string(struct device *dev,
318 enum hwmon_sensor_types type,
319 u32 attr, int channel, const char **str)
324 *str = tps23861_port_label[channel];
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),
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,
362 static const struct hwmon_chip_info tps23861_chip_info = {
363 .ops = &tps23861_hwmon_ops,
364 .info = tps23861_info,
367 static char *tps23861_port_operating_mode(struct tps23861_data *data, int port)
372 regmap_read(data->regmap, OPERATING_MODE, ®val);
376 mode = FIELD_GET(OPERATING_MODE_PORT_1_MASK, regval);
379 mode = FIELD_GET(OPERATING_MODE_PORT_2_MASK, regval);
382 mode = FIELD_GET(OPERATING_MODE_PORT_3_MASK, regval);
385 mode = FIELD_GET(OPERATING_MODE_PORT_4_MASK, regval);
392 case OPERATING_MODE_OFF:
394 case OPERATING_MODE_MANUAL:
396 case OPERATING_MODE_SEMI:
398 case OPERATING_MODE_AUTO:
405 static char *tps23861_port_detect_status(struct tps23861_data *data, int port)
409 regmap_read(data->regmap,
410 PORT_1_STATUS + (port - 1),
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:
443 static char *tps23861_port_class_status(struct tps23861_data *data, int port)
447 regmap_read(data->regmap,
448 PORT_1_STATUS + (port - 1),
451 switch (FIELD_GET(PORT_STATUS_CLASS_MASK, regval)) {
452 case PORT_CLASS_UNKNOWN:
454 case PORT_CLASS_RESERVED:
465 case PORT_CLASS_OVERCURRENT:
466 return "Overcurrent";
467 case PORT_CLASS_MISMATCH:
474 static char *tps23861_port_poe_plus_status(struct tps23861_data *data, int port)
478 regmap_read(data->regmap, POE_PLUS, ®val);
480 if (BIT(port + 3) & regval)
486 static int tps23861_port_resistance(struct tps23861_data *data, int port)
490 regmap_bulk_read(data->regmap,
491 PORT_1_RESISTANCE_LSB + PORT_N_RESISTANCE_LSB_OFFSET * (port - 1),
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:
507 static int tps23861_port_status_show(struct seq_file *s, void *data)
509 struct tps23861_data *priv = s->private;
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));
525 DEFINE_SHOW_ATTRIBUTE(tps23861_port_status);
527 static void tps23861_init_debugfs(struct tps23861_data *data)
529 data->debugfs_dir = debugfs_create_dir(data->client->name, NULL);
531 debugfs_create_file("port_status",
535 &tps23861_port_status_fops);
538 static int tps23861_probe(struct i2c_client *client)
540 struct device *dev = &client->dev;
541 struct tps23861_data *data;
542 struct device *hwmon_dev;
545 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
549 data->client = client;
550 i2c_set_clientdata(client, data);
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);
558 if (!of_property_read_u32(dev->of_node, "shunt-resistor-micro-ohms", &shunt_resistor))
559 data->shunt_resistor = shunt_resistor;
561 data->shunt_resistor = SHUNT_RESISTOR_DEFAULT;
563 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
564 data, &tps23861_chip_info,
566 if (IS_ERR(hwmon_dev))
567 return PTR_ERR(hwmon_dev);
569 tps23861_init_debugfs(data);
574 static int tps23861_remove(struct i2c_client *client)
576 struct tps23861_data *data = i2c_get_clientdata(client);
578 debugfs_remove_recursive(data->debugfs_dir);
583 static const struct of_device_id __maybe_unused tps23861_of_match[] = {
584 { .compatible = "ti,tps23861", },
587 MODULE_DEVICE_TABLE(of, tps23861_of_match);
589 static struct i2c_driver tps23861_driver = {
590 .probe_new = tps23861_probe,
591 .remove = tps23861_remove,
594 .of_match_table = of_match_ptr(tps23861_of_match),
597 module_i2c_driver(tps23861_driver);
599 MODULE_LICENSE("GPL");
600 MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
601 MODULE_DESCRIPTION("TI TPS23861 PoE PSE");