1 From 97c95dbbba64dbd6e98e033e396695f328033966 Mon Sep 17 00:00:00 2001
2 From: Robert Marko <robert.marko@sartura.hr>
3 Date: Thu, 21 Jan 2021 14:44:33 +0100
4 Subject: [PATCH 1/4] hwmon: add Texas Instruments TPS23861 driver
6 Add basic monitoring support as well as port on/off control for Texas
7 Instruments TPS23861 PoE PSE IC.
9 Signed-off-by: Robert Marko <robert.marko@sartura.hr>
10 Cc: Luka Perkov <luka.perkov@sartura.hr>
11 Reviewed-by: Guenter Roeck <linux@roeck-us.net>
12 Link: https://lore.kernel.org/r/20210121134434.2782405-2-robert.marko@sartura.hr
13 Signed-off-by: Guenter Roeck <linux@roeck-us.net>
15 Documentation/hwmon/index.rst | 1 +
16 Documentation/hwmon/tps23861.rst | 41 +++
17 drivers/hwmon/Kconfig | 11 +
18 drivers/hwmon/Makefile | 1 +
19 drivers/hwmon/tps23861.c | 601 +++++++++++++++++++++++++++++++
20 5 files changed, 655 insertions(+)
21 create mode 100644 Documentation/hwmon/tps23861.rst
22 create mode 100644 drivers/hwmon/tps23861.c
24 --- a/Documentation/hwmon/index.rst
25 +++ b/Documentation/hwmon/index.rst
26 @@ -172,6 +172,7 @@ Hardware Monitoring Kernel Drivers
35 +++ b/Documentation/hwmon/tps23861.rst
37 +.. SPDX-License-Identifier: GPL-2.0-only
39 +Kernel driver tps23861
40 +======================
43 + * Texas Instruments TPS23861
47 + Datasheet: https://www.ti.com/lit/gpn/tps23861
49 +Author: Robert Marko <robert.marko@sartura.hr>
54 +This driver supports hardware monitoring for Texas Instruments TPS23861 PoE PSE.
56 +TPS23861 is a quad port IEEE802.3at PSE controller with optional I2C control
57 +and monitoring capabilities.
59 +TPS23861 offers three modes of operation: Auto, Semi-Auto and Manual.
61 +This driver only supports the Auto mode of operation providing monitoring
62 +as well as enabling/disabling the four ports.
67 +======================= =====================================================================
68 +in[0-3]_input Voltage on ports [1-4]
69 +in[0-3]_label "Port[1-4]"
70 +in4_input IC input voltage
72 +temp1_input IC die temperature
74 +curr[1-4]_input Current on ports [1-4]
75 +in[1-4]_label "Port[1-4]"
76 +in[0-3]_enable Enable/disable ports [1-4]
77 +======================= =====================================================================
78 --- a/drivers/hwmon/Kconfig
79 +++ b/drivers/hwmon/Kconfig
80 @@ -1102,6 +1102,17 @@ config SENSORS_TC654
81 This driver can also be built as a module. If so, the module
84 +config SENSORS_TPS23861
85 + tristate "Texas Instruments TPS23861 PoE PSE"
89 + If you say yes here you get support for Texas Instruments
90 + TPS23861 802.3at PoE PSE chips.
92 + This driver can also be built as a module. If so, the module
93 + will be called tps23861.
95 config SENSORS_MENF21BMC_HWMON
96 tristate "MEN 14F021P00 BMC Hardware Monitoring"
97 depends on MFD_MENF21BMC
98 --- a/drivers/hwmon/Makefile
99 +++ b/drivers/hwmon/Makefile
100 @@ -141,6 +141,7 @@ obj-$(CONFIG_SENSORS_MAX31790) += max317
101 obj-$(CONFIG_SENSORS_MC13783_ADC)+= mc13783-adc.o
102 obj-$(CONFIG_SENSORS_MCP3021) += mcp3021.o
103 obj-$(CONFIG_SENSORS_TC654) += tc654.o
104 +obj-$(CONFIG_SENSORS_TPS23861) += tps23861.o
105 obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o
106 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
107 obj-$(CONFIG_SENSORS_MR75203) += mr75203.o
109 +++ b/drivers/hwmon/tps23861.c
111 +// SPDX-License-Identifier: GPL-2.0-only
113 + * Copyright (c) 2020 Sartura Ltd.
115 + * Driver for the TI TPS23861 PoE PSE.
117 + * Author: Robert Marko <robert.marko@sartura.hr>
120 +#include <linux/bitfield.h>
121 +#include <linux/debugfs.h>
122 +#include <linux/delay.h>
123 +#include <linux/hwmon-sysfs.h>
124 +#include <linux/hwmon.h>
125 +#include <linux/i2c.h>
126 +#include <linux/module.h>
127 +#include <linux/of_device.h>
128 +#include <linux/regmap.h>
130 +#define TEMPERATURE 0x2c
131 +#define INPUT_VOLTAGE_LSB 0x2e
132 +#define INPUT_VOLTAGE_MSB 0x2f
133 +#define PORT_1_CURRENT_LSB 0x30
134 +#define PORT_1_CURRENT_MSB 0x31
135 +#define PORT_1_VOLTAGE_LSB 0x32
136 +#define PORT_1_VOLTAGE_MSB 0x33
137 +#define PORT_2_CURRENT_LSB 0x34
138 +#define PORT_2_CURRENT_MSB 0x35
139 +#define PORT_2_VOLTAGE_LSB 0x36
140 +#define PORT_2_VOLTAGE_MSB 0x37
141 +#define PORT_3_CURRENT_LSB 0x38
142 +#define PORT_3_CURRENT_MSB 0x39
143 +#define PORT_3_VOLTAGE_LSB 0x3a
144 +#define PORT_3_VOLTAGE_MSB 0x3b
145 +#define PORT_4_CURRENT_LSB 0x3c
146 +#define PORT_4_CURRENT_MSB 0x3d
147 +#define PORT_4_VOLTAGE_LSB 0x3e
148 +#define PORT_4_VOLTAGE_MSB 0x3f
149 +#define PORT_N_CURRENT_LSB_OFFSET 0x04
150 +#define PORT_N_VOLTAGE_LSB_OFFSET 0x04
151 +#define VOLTAGE_CURRENT_MASK GENMASK(13, 0)
152 +#define PORT_1_RESISTANCE_LSB 0x60
153 +#define PORT_1_RESISTANCE_MSB 0x61
154 +#define PORT_2_RESISTANCE_LSB 0x62
155 +#define PORT_2_RESISTANCE_MSB 0x63
156 +#define PORT_3_RESISTANCE_LSB 0x64
157 +#define PORT_3_RESISTANCE_MSB 0x65
158 +#define PORT_4_RESISTANCE_LSB 0x66
159 +#define PORT_4_RESISTANCE_MSB 0x67
160 +#define PORT_N_RESISTANCE_LSB_OFFSET 0x02
161 +#define PORT_RESISTANCE_MASK GENMASK(13, 0)
162 +#define PORT_RESISTANCE_RSN_MASK GENMASK(15, 14)
163 +#define PORT_RESISTANCE_RSN_OTHER 0
164 +#define PORT_RESISTANCE_RSN_LOW 1
165 +#define PORT_RESISTANCE_RSN_OPEN 2
166 +#define PORT_RESISTANCE_RSN_SHORT 3
167 +#define PORT_1_STATUS 0x0c
168 +#define PORT_2_STATUS 0x0d
169 +#define PORT_3_STATUS 0x0e
170 +#define PORT_4_STATUS 0x0f
171 +#define PORT_STATUS_CLASS_MASK GENMASK(7, 4)
172 +#define PORT_STATUS_DETECT_MASK GENMASK(3, 0)
173 +#define PORT_CLASS_UNKNOWN 0
174 +#define PORT_CLASS_1 1
175 +#define PORT_CLASS_2 2
176 +#define PORT_CLASS_3 3
177 +#define PORT_CLASS_4 4
178 +#define PORT_CLASS_RESERVED 5
179 +#define PORT_CLASS_0 6
180 +#define PORT_CLASS_OVERCURRENT 7
181 +#define PORT_CLASS_MISMATCH 8
182 +#define PORT_DETECT_UNKNOWN 0
183 +#define PORT_DETECT_SHORT 1
184 +#define PORT_DETECT_RESERVED 2
185 +#define PORT_DETECT_RESISTANCE_LOW 3
186 +#define PORT_DETECT_RESISTANCE_OK 4
187 +#define PORT_DETECT_RESISTANCE_HIGH 5
188 +#define PORT_DETECT_OPEN_CIRCUIT 6
189 +#define PORT_DETECT_RESERVED_2 7
190 +#define PORT_DETECT_MOSFET_FAULT 8
191 +#define PORT_DETECT_LEGACY 9
192 +/* Measurment beyond clamp voltage */
193 +#define PORT_DETECT_CAPACITANCE_INVALID_BEYOND 10
194 +/* Insufficient voltage delta */
195 +#define PORT_DETECT_CAPACITANCE_INVALID_DELTA 11
196 +#define PORT_DETECT_CAPACITANCE_OUT_OF_RANGE 12
197 +#define POE_PLUS 0x40
198 +#define OPERATING_MODE 0x12
199 +#define OPERATING_MODE_OFF 0
200 +#define OPERATING_MODE_MANUAL 1
201 +#define OPERATING_MODE_SEMI 2
202 +#define OPERATING_MODE_AUTO 3
203 +#define OPERATING_MODE_PORT_1_MASK GENMASK(1, 0)
204 +#define OPERATING_MODE_PORT_2_MASK GENMASK(3, 2)
205 +#define OPERATING_MODE_PORT_3_MASK GENMASK(5, 4)
206 +#define OPERATING_MODE_PORT_4_MASK GENMASK(7, 6)
208 +#define DETECT_CLASS_RESTART 0x18
209 +#define POWER_ENABLE 0x19
210 +#define TPS23861_NUM_PORTS 4
212 +#define TEMPERATURE_LSB 652 /* 0.652 degrees Celsius */
213 +#define VOLTAGE_LSB 3662 /* 3.662 mV */
214 +#define SHUNT_RESISTOR_DEFAULT 255000 /* 255 mOhm */
215 +#define CURRENT_LSB_255 62260 /* 62.260 uA */
216 +#define CURRENT_LSB_250 61039 /* 61.039 uA */
217 +#define RESISTANCE_LSB 110966 /* 11.0966 Ohm*/
218 +#define RESISTANCE_LSB_LOW 157216 /* 15.7216 Ohm*/
220 +struct tps23861_data {
221 + struct regmap *regmap;
222 + u32 shunt_resistor;
223 + struct i2c_client *client;
224 + struct dentry *debugfs_dir;
227 +static struct regmap_config tps23861_regmap_config = {
232 +static int tps23861_read_temp(struct tps23861_data *data, long *val)
234 + unsigned int regval;
237 + err = regmap_read(data->regmap, TEMPERATURE, ®val);
241 + *val = (regval * TEMPERATURE_LSB) - 20000;
246 +static int tps23861_read_voltage(struct tps23861_data *data, int channel,
249 + unsigned int regval;
252 + if (channel < TPS23861_NUM_PORTS) {
253 + err = regmap_bulk_read(data->regmap,
254 + PORT_1_VOLTAGE_LSB + channel * PORT_N_VOLTAGE_LSB_OFFSET,
257 + err = regmap_bulk_read(data->regmap,
264 + *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * VOLTAGE_LSB) / 1000;
269 +static int tps23861_read_current(struct tps23861_data *data, int channel,
272 + unsigned int current_lsb;
273 + unsigned int regval;
276 + if (data->shunt_resistor == SHUNT_RESISTOR_DEFAULT)
277 + current_lsb = CURRENT_LSB_255;
279 + current_lsb = CURRENT_LSB_250;
281 + err = regmap_bulk_read(data->regmap,
282 + PORT_1_CURRENT_LSB + channel * PORT_N_CURRENT_LSB_OFFSET,
287 + *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * current_lsb) / 1000000;
292 +static int tps23861_port_disable(struct tps23861_data *data, int channel)
294 + unsigned int regval = 0;
297 + regval |= BIT(channel + 4);
298 + err = regmap_write(data->regmap, POWER_ENABLE, regval);
303 +static int tps23861_port_enable(struct tps23861_data *data, int channel)
305 + unsigned int regval = 0;
308 + regval |= BIT(channel);
309 + regval |= BIT(channel + 4);
310 + err = regmap_write(data->regmap, DETECT_CLASS_RESTART, regval);
315 +static umode_t tps23861_is_visible(const void *data, enum hwmon_sensor_types type,
316 + u32 attr, int channel)
321 + case hwmon_temp_input:
322 + case hwmon_temp_label:
329 + case hwmon_in_input:
330 + case hwmon_in_label:
332 + case hwmon_in_enable:
339 + case hwmon_curr_input:
340 + case hwmon_curr_label:
350 +static int tps23861_write(struct device *dev, enum hwmon_sensor_types type,
351 + u32 attr, int channel, long val)
353 + struct tps23861_data *data = dev_get_drvdata(dev);
359 + case hwmon_in_enable:
361 + err = tps23861_port_disable(data, channel);
363 + err = tps23861_port_enable(data, channel);
368 + return -EOPNOTSUPP;
372 + return -EOPNOTSUPP;
378 +static int tps23861_read(struct device *dev, enum hwmon_sensor_types type,
379 + u32 attr, int channel, long *val)
381 + struct tps23861_data *data = dev_get_drvdata(dev);
387 + case hwmon_temp_input:
388 + err = tps23861_read_temp(data, val);
391 + return -EOPNOTSUPP;
396 + case hwmon_in_input:
397 + err = tps23861_read_voltage(data, channel, val);
400 + return -EOPNOTSUPP;
405 + case hwmon_curr_input:
406 + err = tps23861_read_current(data, channel, val);
409 + return -EOPNOTSUPP;
413 + return -EOPNOTSUPP;
419 +static const char * const tps23861_port_label[] = {
427 +static int tps23861_read_string(struct device *dev,
428 + enum hwmon_sensor_types type,
429 + u32 attr, int channel, const char **str)
434 + *str = tps23861_port_label[channel];
440 + return -EOPNOTSUPP;
446 +static const struct hwmon_channel_info *tps23861_info[] = {
447 + HWMON_CHANNEL_INFO(chip,
448 + HWMON_C_REGISTER_TZ),
449 + HWMON_CHANNEL_INFO(temp,
450 + HWMON_T_INPUT | HWMON_T_LABEL),
451 + HWMON_CHANNEL_INFO(in,
452 + HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
453 + HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
454 + HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
455 + HWMON_I_INPUT | HWMON_I_ENABLE | HWMON_I_LABEL,
456 + HWMON_I_INPUT | HWMON_I_LABEL),
457 + HWMON_CHANNEL_INFO(curr,
458 + HWMON_C_INPUT | HWMON_C_LABEL,
459 + HWMON_C_INPUT | HWMON_C_LABEL,
460 + HWMON_C_INPUT | HWMON_C_LABEL,
461 + HWMON_C_INPUT | HWMON_C_LABEL),
465 +static const struct hwmon_ops tps23861_hwmon_ops = {
466 + .is_visible = tps23861_is_visible,
467 + .write = tps23861_write,
468 + .read = tps23861_read,
469 + .read_string = tps23861_read_string,
472 +static const struct hwmon_chip_info tps23861_chip_info = {
473 + .ops = &tps23861_hwmon_ops,
474 + .info = tps23861_info,
477 +static char *tps23861_port_operating_mode(struct tps23861_data *data, int port)
479 + unsigned int regval;
482 + regmap_read(data->regmap, OPERATING_MODE, ®val);
486 + mode = FIELD_GET(OPERATING_MODE_PORT_1_MASK, regval);
489 + mode = FIELD_GET(OPERATING_MODE_PORT_2_MASK, regval);
492 + mode = FIELD_GET(OPERATING_MODE_PORT_3_MASK, regval);
495 + mode = FIELD_GET(OPERATING_MODE_PORT_4_MASK, regval);
502 + case OPERATING_MODE_OFF:
504 + case OPERATING_MODE_MANUAL:
506 + case OPERATING_MODE_SEMI:
507 + return "Semi-Auto";
508 + case OPERATING_MODE_AUTO:
515 +static char *tps23861_port_detect_status(struct tps23861_data *data, int port)
517 + unsigned int regval;
519 + regmap_read(data->regmap,
520 + PORT_1_STATUS + (port - 1),
523 + switch (FIELD_GET(PORT_STATUS_DETECT_MASK, regval)) {
524 + case PORT_DETECT_UNKNOWN:
525 + return "Unknown device";
526 + case PORT_DETECT_SHORT:
527 + return "Short circuit";
528 + case PORT_DETECT_RESISTANCE_LOW:
529 + return "Too low resistance";
530 + case PORT_DETECT_RESISTANCE_OK:
531 + return "Valid resistance";
532 + case PORT_DETECT_RESISTANCE_HIGH:
533 + return "Too high resistance";
534 + case PORT_DETECT_OPEN_CIRCUIT:
535 + return "Open circuit";
536 + case PORT_DETECT_MOSFET_FAULT:
537 + return "MOSFET fault";
538 + case PORT_DETECT_LEGACY:
539 + return "Legacy device";
540 + case PORT_DETECT_CAPACITANCE_INVALID_BEYOND:
541 + return "Invalid capacitance, beyond clamp voltage";
542 + case PORT_DETECT_CAPACITANCE_INVALID_DELTA:
543 + return "Invalid capacitance, insufficient voltage delta";
544 + case PORT_DETECT_CAPACITANCE_OUT_OF_RANGE:
545 + return "Valid capacitance, outside of legacy range";
546 + case PORT_DETECT_RESERVED:
547 + case PORT_DETECT_RESERVED_2:
553 +static char *tps23861_port_class_status(struct tps23861_data *data, int port)
555 + unsigned int regval;
557 + regmap_read(data->regmap,
558 + PORT_1_STATUS + (port - 1),
561 + switch (FIELD_GET(PORT_STATUS_CLASS_MASK, regval)) {
562 + case PORT_CLASS_UNKNOWN:
564 + case PORT_CLASS_RESERVED:
575 + case PORT_CLASS_OVERCURRENT:
576 + return "Overcurrent";
577 + case PORT_CLASS_MISMATCH:
584 +static char *tps23861_port_poe_plus_status(struct tps23861_data *data, int port)
586 + unsigned int regval;
588 + regmap_read(data->regmap, POE_PLUS, ®val);
590 + if (BIT(port + 3) & regval)
596 +static int tps23861_port_resistance(struct tps23861_data *data, int port)
600 + regmap_bulk_read(data->regmap,
601 + PORT_1_RESISTANCE_LSB + PORT_N_RESISTANCE_LSB_OFFSET * (port - 1),
605 + switch (FIELD_GET(PORT_RESISTANCE_RSN_MASK, regval)) {
606 + case PORT_RESISTANCE_RSN_OTHER:
607 + return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB) / 10000;
608 + case PORT_RESISTANCE_RSN_LOW:
609 + return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB_LOW) / 10000;
610 + case PORT_RESISTANCE_RSN_SHORT:
611 + case PORT_RESISTANCE_RSN_OPEN:
617 +static int tps23861_port_status_show(struct seq_file *s, void *data)
619 + struct tps23861_data *priv = s->private;
622 + for (i = 1; i < TPS23861_NUM_PORTS + 1; i++) {
623 + seq_printf(s, "Port: \t\t%d\n", i);
624 + seq_printf(s, "Operating mode: %s\n", tps23861_port_operating_mode(priv, i));
625 + seq_printf(s, "Detected: \t%s\n", tps23861_port_detect_status(priv, i));
626 + seq_printf(s, "Class: \t\t%s\n", tps23861_port_class_status(priv, i));
627 + seq_printf(s, "PoE Plus: \t%s\n", tps23861_port_poe_plus_status(priv, i));
628 + seq_printf(s, "Resistance: \t%d\n", tps23861_port_resistance(priv, i));
635 +DEFINE_SHOW_ATTRIBUTE(tps23861_port_status);
637 +static void tps23861_init_debugfs(struct tps23861_data *data)
639 + data->debugfs_dir = debugfs_create_dir(data->client->name, NULL);
641 + debugfs_create_file("port_status",
645 + &tps23861_port_status_fops);
648 +static int tps23861_probe(struct i2c_client *client)
650 + struct device *dev = &client->dev;
651 + struct tps23861_data *data;
652 + struct device *hwmon_dev;
653 + u32 shunt_resistor;
655 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
659 + data->client = client;
660 + i2c_set_clientdata(client, data);
662 + data->regmap = devm_regmap_init_i2c(client, &tps23861_regmap_config);
663 + if (IS_ERR(data->regmap)) {
664 + dev_err(dev, "failed to allocate register map\n");
665 + return PTR_ERR(data->regmap);
668 + if (!of_property_read_u32(dev->of_node, "shunt-resistor-micro-ohms", &shunt_resistor))
669 + data->shunt_resistor = shunt_resistor;
671 + data->shunt_resistor = SHUNT_RESISTOR_DEFAULT;
673 + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
674 + data, &tps23861_chip_info,
676 + if (IS_ERR(hwmon_dev))
677 + return PTR_ERR(hwmon_dev);
679 + tps23861_init_debugfs(data);
684 +static int tps23861_remove(struct i2c_client *client)
686 + struct tps23861_data *data = i2c_get_clientdata(client);
688 + debugfs_remove_recursive(data->debugfs_dir);
693 +static const struct of_device_id __maybe_unused tps23861_of_match[] = {
694 + { .compatible = "ti,tps23861", },
697 +MODULE_DEVICE_TABLE(of, tps23861_of_match);
699 +static struct i2c_driver tps23861_driver = {
700 + .probe_new = tps23861_probe,
701 + .remove = tps23861_remove,
703 + .name = "tps23861",
704 + .of_match_table = of_match_ptr(tps23861_of_match),
707 +module_i2c_driver(tps23861_driver);
709 +MODULE_LICENSE("GPL");
710 +MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
711 +MODULE_DESCRIPTION("TI TPS23861 PoE PSE");