8c0329960ce41b97f94b8db65ec9a1a46adf0d1b
[openwrt/staging/ldir.git] / target / linux / generic / backport-5.10 / 871-v5.12-hwmon-add-Texas-Instruments-TPS23861-driver.patch
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
5
6 Add basic monitoring support as well as port on/off control for Texas
7 Instruments TPS23861 PoE PSE IC.
8
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>
14 ---
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
23
24 --- a/Documentation/hwmon/index.rst
25 +++ b/Documentation/hwmon/index.rst
26 @@ -172,6 +172,7 @@ Hardware Monitoring Kernel Drivers
27 tmp401
28 tmp421
29 tmp513
30 + tps23861
31 tps40422
32 tps53679
33 twl4030-madc-hwmon
34 --- /dev/null
35 +++ b/Documentation/hwmon/tps23861.rst
36 @@ -0,0 +1,41 @@
37 +.. SPDX-License-Identifier: GPL-2.0-only
38 +
39 +Kernel driver tps23861
40 +======================
41 +
42 +Supported chips:
43 + * Texas Instruments TPS23861
44 +
45 + Prefix: 'tps23861'
46 +
47 + Datasheet: https://www.ti.com/lit/gpn/tps23861
48 +
49 +Author: Robert Marko <robert.marko@sartura.hr>
50 +
51 +Description
52 +-----------
53 +
54 +This driver supports hardware monitoring for Texas Instruments TPS23861 PoE PSE.
55 +
56 +TPS23861 is a quad port IEEE802.3at PSE controller with optional I2C control
57 +and monitoring capabilities.
58 +
59 +TPS23861 offers three modes of operation: Auto, Semi-Auto and Manual.
60 +
61 +This driver only supports the Auto mode of operation providing monitoring
62 +as well as enabling/disabling the four ports.
63 +
64 +Sysfs entries
65 +-------------
66 +
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
71 +in4_label "Input"
72 +temp1_input IC die temperature
73 +temp1_label "Die"
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 @@ -1103,6 +1103,17 @@ config SENSORS_TC654
81 This driver can also be built as a module. If so, the module
82 will be called tc654.
83
84 +config SENSORS_TPS23861
85 + tristate "Texas Instruments TPS23861 PoE PSE"
86 + depends on I2C
87 + select REGMAP_I2C
88 + help
89 + If you say yes here you get support for Texas Instruments
90 + TPS23861 802.3at PoE PSE chips.
91 +
92 + This driver can also be built as a module. If so, the module
93 + will be called tps23861.
94 +
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
108 --- /dev/null
109 +++ b/drivers/hwmon/tps23861.c
110 @@ -0,0 +1,601 @@
111 +// SPDX-License-Identifier: GPL-2.0-only
112 +/*
113 + * Copyright (c) 2020 Sartura Ltd.
114 + *
115 + * Driver for the TI TPS23861 PoE PSE.
116 + *
117 + * Author: Robert Marko <robert.marko@sartura.hr>
118 + */
119 +
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>
129 +
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)
207 +
208 +#define DETECT_CLASS_RESTART 0x18
209 +#define POWER_ENABLE 0x19
210 +#define TPS23861_NUM_PORTS 4
211 +
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*/
219 +
220 +struct tps23861_data {
221 + struct regmap *regmap;
222 + u32 shunt_resistor;
223 + struct i2c_client *client;
224 + struct dentry *debugfs_dir;
225 +};
226 +
227 +static struct regmap_config tps23861_regmap_config = {
228 + .reg_bits = 8,
229 + .val_bits = 8,
230 +};
231 +
232 +static int tps23861_read_temp(struct tps23861_data *data, long *val)
233 +{
234 + unsigned int regval;
235 + int err;
236 +
237 + err = regmap_read(data->regmap, TEMPERATURE, &regval);
238 + if (err < 0)
239 + return err;
240 +
241 + *val = (regval * TEMPERATURE_LSB) - 20000;
242 +
243 + return 0;
244 +}
245 +
246 +static int tps23861_read_voltage(struct tps23861_data *data, int channel,
247 + long *val)
248 +{
249 + unsigned int regval;
250 + int err;
251 +
252 + if (channel < TPS23861_NUM_PORTS) {
253 + err = regmap_bulk_read(data->regmap,
254 + PORT_1_VOLTAGE_LSB + channel * PORT_N_VOLTAGE_LSB_OFFSET,
255 + &regval, 2);
256 + } else {
257 + err = regmap_bulk_read(data->regmap,
258 + INPUT_VOLTAGE_LSB,
259 + &regval, 2);
260 + }
261 + if (err < 0)
262 + return err;
263 +
264 + *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * VOLTAGE_LSB) / 1000;
265 +
266 + return 0;
267 +}
268 +
269 +static int tps23861_read_current(struct tps23861_data *data, int channel,
270 + long *val)
271 +{
272 + unsigned int current_lsb;
273 + unsigned int regval;
274 + int err;
275 +
276 + if (data->shunt_resistor == SHUNT_RESISTOR_DEFAULT)
277 + current_lsb = CURRENT_LSB_255;
278 + else
279 + current_lsb = CURRENT_LSB_250;
280 +
281 + err = regmap_bulk_read(data->regmap,
282 + PORT_1_CURRENT_LSB + channel * PORT_N_CURRENT_LSB_OFFSET,
283 + &regval, 2);
284 + if (err < 0)
285 + return err;
286 +
287 + *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * current_lsb) / 1000000;
288 +
289 + return 0;
290 +}
291 +
292 +static int tps23861_port_disable(struct tps23861_data *data, int channel)
293 +{
294 + unsigned int regval = 0;
295 + int err;
296 +
297 + regval |= BIT(channel + 4);
298 + err = regmap_write(data->regmap, POWER_ENABLE, regval);
299 +
300 + return err;
301 +}
302 +
303 +static int tps23861_port_enable(struct tps23861_data *data, int channel)
304 +{
305 + unsigned int regval = 0;
306 + int err;
307 +
308 + regval |= BIT(channel);
309 + regval |= BIT(channel + 4);
310 + err = regmap_write(data->regmap, DETECT_CLASS_RESTART, regval);
311 +
312 + return err;
313 +}
314 +
315 +static umode_t tps23861_is_visible(const void *data, enum hwmon_sensor_types type,
316 + u32 attr, int channel)
317 +{
318 + switch (type) {
319 + case hwmon_temp:
320 + switch (attr) {
321 + case hwmon_temp_input:
322 + case hwmon_temp_label:
323 + return 0444;
324 + default:
325 + return 0;
326 + }
327 + case hwmon_in:
328 + switch (attr) {
329 + case hwmon_in_input:
330 + case hwmon_in_label:
331 + return 0444;
332 + case hwmon_in_enable:
333 + return 0200;
334 + default:
335 + return 0;
336 + }
337 + case hwmon_curr:
338 + switch (attr) {
339 + case hwmon_curr_input:
340 + case hwmon_curr_label:
341 + return 0444;
342 + default:
343 + return 0;
344 + }
345 + default:
346 + return 0;
347 + }
348 +}
349 +
350 +static int tps23861_write(struct device *dev, enum hwmon_sensor_types type,
351 + u32 attr, int channel, long val)
352 +{
353 + struct tps23861_data *data = dev_get_drvdata(dev);
354 + int err;
355 +
356 + switch (type) {
357 + case hwmon_in:
358 + switch (attr) {
359 + case hwmon_in_enable:
360 + if (val == 0)
361 + err = tps23861_port_disable(data, channel);
362 + else if (val == 1)
363 + err = tps23861_port_enable(data, channel);
364 + else
365 + err = -EINVAL;
366 + break;
367 + default:
368 + return -EOPNOTSUPP;
369 + }
370 + break;
371 + default:
372 + return -EOPNOTSUPP;
373 + }
374 +
375 + return err;
376 +}
377 +
378 +static int tps23861_read(struct device *dev, enum hwmon_sensor_types type,
379 + u32 attr, int channel, long *val)
380 +{
381 + struct tps23861_data *data = dev_get_drvdata(dev);
382 + int err;
383 +
384 + switch (type) {
385 + case hwmon_temp:
386 + switch (attr) {
387 + case hwmon_temp_input:
388 + err = tps23861_read_temp(data, val);
389 + break;
390 + default:
391 + return -EOPNOTSUPP;
392 + }
393 + break;
394 + case hwmon_in:
395 + switch (attr) {
396 + case hwmon_in_input:
397 + err = tps23861_read_voltage(data, channel, val);
398 + break;
399 + default:
400 + return -EOPNOTSUPP;
401 + }
402 + break;
403 + case hwmon_curr:
404 + switch (attr) {
405 + case hwmon_curr_input:
406 + err = tps23861_read_current(data, channel, val);
407 + break;
408 + default:
409 + return -EOPNOTSUPP;
410 + }
411 + break;
412 + default:
413 + return -EOPNOTSUPP;
414 + }
415 +
416 + return err;
417 +}
418 +
419 +static const char * const tps23861_port_label[] = {
420 + "Port1",
421 + "Port2",
422 + "Port3",
423 + "Port4",
424 + "Input",
425 +};
426 +
427 +static int tps23861_read_string(struct device *dev,
428 + enum hwmon_sensor_types type,
429 + u32 attr, int channel, const char **str)
430 +{
431 + switch (type) {
432 + case hwmon_in:
433 + case hwmon_curr:
434 + *str = tps23861_port_label[channel];
435 + break;
436 + case hwmon_temp:
437 + *str = "Die";
438 + break;
439 + default:
440 + return -EOPNOTSUPP;
441 + }
442 +
443 + return 0;
444 +}
445 +
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),
462 + NULL
463 +};
464 +
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,
470 +};
471 +
472 +static const struct hwmon_chip_info tps23861_chip_info = {
473 + .ops = &tps23861_hwmon_ops,
474 + .info = tps23861_info,
475 +};
476 +
477 +static char *tps23861_port_operating_mode(struct tps23861_data *data, int port)
478 +{
479 + unsigned int regval;
480 + int mode;
481 +
482 + regmap_read(data->regmap, OPERATING_MODE, &regval);
483 +
484 + switch (port) {
485 + case 1:
486 + mode = FIELD_GET(OPERATING_MODE_PORT_1_MASK, regval);
487 + break;
488 + case 2:
489 + mode = FIELD_GET(OPERATING_MODE_PORT_2_MASK, regval);
490 + break;
491 + case 3:
492 + mode = FIELD_GET(OPERATING_MODE_PORT_3_MASK, regval);
493 + break;
494 + case 4:
495 + mode = FIELD_GET(OPERATING_MODE_PORT_4_MASK, regval);
496 + break;
497 + default:
498 + mode = -EINVAL;
499 + }
500 +
501 + switch (mode) {
502 + case OPERATING_MODE_OFF:
503 + return "Off";
504 + case OPERATING_MODE_MANUAL:
505 + return "Manual";
506 + case OPERATING_MODE_SEMI:
507 + return "Semi-Auto";
508 + case OPERATING_MODE_AUTO:
509 + return "Auto";
510 + default:
511 + return "Invalid";
512 + }
513 +}
514 +
515 +static char *tps23861_port_detect_status(struct tps23861_data *data, int port)
516 +{
517 + unsigned int regval;
518 +
519 + regmap_read(data->regmap,
520 + PORT_1_STATUS + (port - 1),
521 + &regval);
522 +
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:
548 + default:
549 + return "Invalid";
550 + }
551 +}
552 +
553 +static char *tps23861_port_class_status(struct tps23861_data *data, int port)
554 +{
555 + unsigned int regval;
556 +
557 + regmap_read(data->regmap,
558 + PORT_1_STATUS + (port - 1),
559 + &regval);
560 +
561 + switch (FIELD_GET(PORT_STATUS_CLASS_MASK, regval)) {
562 + case PORT_CLASS_UNKNOWN:
563 + return "Unknown";
564 + case PORT_CLASS_RESERVED:
565 + case PORT_CLASS_0:
566 + return "0";
567 + case PORT_CLASS_1:
568 + return "1";
569 + case PORT_CLASS_2:
570 + return "2";
571 + case PORT_CLASS_3:
572 + return "3";
573 + case PORT_CLASS_4:
574 + return "4";
575 + case PORT_CLASS_OVERCURRENT:
576 + return "Overcurrent";
577 + case PORT_CLASS_MISMATCH:
578 + return "Mismatch";
579 + default:
580 + return "Invalid";
581 + }
582 +}
583 +
584 +static char *tps23861_port_poe_plus_status(struct tps23861_data *data, int port)
585 +{
586 + unsigned int regval;
587 +
588 + regmap_read(data->regmap, POE_PLUS, &regval);
589 +
590 + if (BIT(port + 3) & regval)
591 + return "Yes";
592 + else
593 + return "No";
594 +}
595 +
596 +static int tps23861_port_resistance(struct tps23861_data *data, int port)
597 +{
598 + u16 regval;
599 +
600 + regmap_bulk_read(data->regmap,
601 + PORT_1_RESISTANCE_LSB + PORT_N_RESISTANCE_LSB_OFFSET * (port - 1),
602 + &regval,
603 + 2);
604 +
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:
612 + default:
613 + return 0;
614 + }
615 +}
616 +
617 +static int tps23861_port_status_show(struct seq_file *s, void *data)
618 +{
619 + struct tps23861_data *priv = s->private;
620 + int i;
621 +
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));
629 + seq_putc(s, '\n');
630 + }
631 +
632 + return 0;
633 +}
634 +
635 +DEFINE_SHOW_ATTRIBUTE(tps23861_port_status);
636 +
637 +static void tps23861_init_debugfs(struct tps23861_data *data)
638 +{
639 + data->debugfs_dir = debugfs_create_dir(data->client->name, NULL);
640 +
641 + debugfs_create_file("port_status",
642 + 0400,
643 + data->debugfs_dir,
644 + data,
645 + &tps23861_port_status_fops);
646 +}
647 +
648 +static int tps23861_probe(struct i2c_client *client)
649 +{
650 + struct device *dev = &client->dev;
651 + struct tps23861_data *data;
652 + struct device *hwmon_dev;
653 + u32 shunt_resistor;
654 +
655 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
656 + if (!data)
657 + return -ENOMEM;
658 +
659 + data->client = client;
660 + i2c_set_clientdata(client, data);
661 +
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);
666 + }
667 +
668 + if (!of_property_read_u32(dev->of_node, "shunt-resistor-micro-ohms", &shunt_resistor))
669 + data->shunt_resistor = shunt_resistor;
670 + else
671 + data->shunt_resistor = SHUNT_RESISTOR_DEFAULT;
672 +
673 + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
674 + data, &tps23861_chip_info,
675 + NULL);
676 + if (IS_ERR(hwmon_dev))
677 + return PTR_ERR(hwmon_dev);
678 +
679 + tps23861_init_debugfs(data);
680 +
681 + return 0;
682 +}
683 +
684 +static int tps23861_remove(struct i2c_client *client)
685 +{
686 + struct tps23861_data *data = i2c_get_clientdata(client);
687 +
688 + debugfs_remove_recursive(data->debugfs_dir);
689 +
690 + return 0;
691 +}
692 +
693 +static const struct of_device_id __maybe_unused tps23861_of_match[] = {
694 + { .compatible = "ti,tps23861", },
695 + { },
696 +};
697 +MODULE_DEVICE_TABLE(of, tps23861_of_match);
698 +
699 +static struct i2c_driver tps23861_driver = {
700 + .probe_new = tps23861_probe,
701 + .remove = tps23861_remove,
702 + .driver = {
703 + .name = "tps23861",
704 + .of_match_table = of_match_ptr(tps23861_of_match),
705 + },
706 +};
707 +module_i2c_driver(tps23861_driver);
708 +
709 +MODULE_LICENSE("GPL");
710 +MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
711 +MODULE_DESCRIPTION("TI TPS23861 PoE PSE");