fd0be888382906ddeb129f63aec4fc89c8f75ea0
[linux-2.6-microblaze.git] / drivers / hwmon / tps23861.c
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         .max_register = 0x6f,
121 };
122
123 static int tps23861_read_temp(struct tps23861_data *data, long *val)
124 {
125         unsigned int regval;
126         int err;
127
128         err = regmap_read(data->regmap, TEMPERATURE, &regval);
129         if (err < 0)
130                 return err;
131
132         *val = (regval * TEMPERATURE_LSB) - 20000;
133
134         return 0;
135 }
136
137 static int tps23861_read_voltage(struct tps23861_data *data, int channel,
138                                  long *val)
139 {
140         unsigned int regval;
141         int err;
142
143         if (channel < TPS23861_NUM_PORTS) {
144                 err = regmap_bulk_read(data->regmap,
145                                        PORT_1_VOLTAGE_LSB + channel * PORT_N_VOLTAGE_LSB_OFFSET,
146                                        &regval, 2);
147         } else {
148                 err = regmap_bulk_read(data->regmap,
149                                        INPUT_VOLTAGE_LSB,
150                                        &regval, 2);
151         }
152         if (err < 0)
153                 return err;
154
155         *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * VOLTAGE_LSB) / 1000;
156
157         return 0;
158 }
159
160 static int tps23861_read_current(struct tps23861_data *data, int channel,
161                                  long *val)
162 {
163         unsigned int current_lsb;
164         unsigned int regval;
165         int err;
166
167         if (data->shunt_resistor == SHUNT_RESISTOR_DEFAULT)
168                 current_lsb = CURRENT_LSB_255;
169         else
170                 current_lsb = CURRENT_LSB_250;
171
172         err = regmap_bulk_read(data->regmap,
173                                PORT_1_CURRENT_LSB + channel * PORT_N_CURRENT_LSB_OFFSET,
174                                &regval, 2);
175         if (err < 0)
176                 return err;
177
178         *val = (FIELD_GET(VOLTAGE_CURRENT_MASK, regval) * current_lsb) / 1000000;
179
180         return 0;
181 }
182
183 static int tps23861_port_disable(struct tps23861_data *data, int channel)
184 {
185         unsigned int regval = 0;
186         int err;
187
188         regval |= BIT(channel + 4);
189         err = regmap_write(data->regmap, POWER_ENABLE, regval);
190
191         return err;
192 }
193
194 static int tps23861_port_enable(struct tps23861_data *data, int channel)
195 {
196         unsigned int regval = 0;
197         int err;
198
199         regval |= BIT(channel);
200         regval |= BIT(channel + 4);
201         err = regmap_write(data->regmap, DETECT_CLASS_RESTART, regval);
202
203         return err;
204 }
205
206 static umode_t tps23861_is_visible(const void *data, enum hwmon_sensor_types type,
207                                    u32 attr, int channel)
208 {
209         switch (type) {
210         case hwmon_temp:
211                 switch (attr) {
212                 case hwmon_temp_input:
213                 case hwmon_temp_label:
214                         return 0444;
215                 default:
216                         return 0;
217                 }
218         case hwmon_in:
219                 switch (attr) {
220                 case hwmon_in_input:
221                 case hwmon_in_label:
222                         return 0444;
223                 case hwmon_in_enable:
224                         return 0200;
225                 default:
226                         return 0;
227                 }
228         case hwmon_curr:
229                 switch (attr) {
230                 case hwmon_curr_input:
231                 case hwmon_curr_label:
232                         return 0444;
233                 default:
234                         return 0;
235                 }
236         default:
237                 return 0;
238         }
239 }
240
241 static int tps23861_write(struct device *dev, enum hwmon_sensor_types type,
242                           u32 attr, int channel, long val)
243 {
244         struct tps23861_data *data = dev_get_drvdata(dev);
245         int err;
246
247         switch (type) {
248         case hwmon_in:
249                 switch (attr) {
250                 case hwmon_in_enable:
251                         if (val == 0)
252                                 err = tps23861_port_disable(data, channel);
253                         else if (val == 1)
254                                 err = tps23861_port_enable(data, channel);
255                         else
256                                 err = -EINVAL;
257                         break;
258                 default:
259                         return -EOPNOTSUPP;
260                 }
261                 break;
262         default:
263                 return -EOPNOTSUPP;
264         }
265
266         return err;
267 }
268
269 static int tps23861_read(struct device *dev, enum hwmon_sensor_types type,
270                          u32 attr, int channel, long *val)
271 {
272         struct tps23861_data *data = dev_get_drvdata(dev);
273         int err;
274
275         switch (type) {
276         case hwmon_temp:
277                 switch (attr) {
278                 case hwmon_temp_input:
279                         err = tps23861_read_temp(data, val);
280                         break;
281                 default:
282                         return -EOPNOTSUPP;
283                 }
284                 break;
285         case hwmon_in:
286                 switch (attr) {
287                 case hwmon_in_input:
288                         err = tps23861_read_voltage(data, channel, val);
289                         break;
290                 default:
291                         return -EOPNOTSUPP;
292                 }
293                 break;
294         case hwmon_curr:
295                 switch (attr) {
296                 case hwmon_curr_input:
297                         err = tps23861_read_current(data, channel, val);
298                         break;
299                 default:
300                         return -EOPNOTSUPP;
301                 }
302                 break;
303         default:
304                 return -EOPNOTSUPP;
305         }
306
307         return err;
308 }
309
310 static const char * const tps23861_port_label[] = {
311         "Port1",
312         "Port2",
313         "Port3",
314         "Port4",
315         "Input",
316 };
317
318 static int tps23861_read_string(struct device *dev,
319                                 enum hwmon_sensor_types type,
320                                 u32 attr, int channel, const char **str)
321 {
322         switch (type) {
323         case hwmon_in:
324         case hwmon_curr:
325                 *str = tps23861_port_label[channel];
326                 break;
327         case hwmon_temp:
328                 *str = "Die";
329                 break;
330         default:
331                 return -EOPNOTSUPP;
332         }
333
334         return 0;
335 }
336
337 static const struct hwmon_channel_info *tps23861_info[] = {
338         HWMON_CHANNEL_INFO(chip,
339                            HWMON_C_REGISTER_TZ),
340         HWMON_CHANNEL_INFO(temp,
341                            HWMON_T_INPUT | HWMON_T_LABEL),
342         HWMON_CHANNEL_INFO(in,
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_ENABLE | HWMON_I_LABEL,
347                            HWMON_I_INPUT | HWMON_I_LABEL),
348         HWMON_CHANNEL_INFO(curr,
349                            HWMON_C_INPUT | HWMON_C_LABEL,
350                            HWMON_C_INPUT | HWMON_C_LABEL,
351                            HWMON_C_INPUT | HWMON_C_LABEL,
352                            HWMON_C_INPUT | HWMON_C_LABEL),
353         NULL
354 };
355
356 static const struct hwmon_ops tps23861_hwmon_ops = {
357         .is_visible = tps23861_is_visible,
358         .write = tps23861_write,
359         .read = tps23861_read,
360         .read_string = tps23861_read_string,
361 };
362
363 static const struct hwmon_chip_info tps23861_chip_info = {
364         .ops = &tps23861_hwmon_ops,
365         .info = tps23861_info,
366 };
367
368 static char *tps23861_port_operating_mode(struct tps23861_data *data, int port)
369 {
370         unsigned int regval;
371         int mode;
372
373         regmap_read(data->regmap, OPERATING_MODE, &regval);
374
375         switch (port) {
376         case 1:
377                 mode = FIELD_GET(OPERATING_MODE_PORT_1_MASK, regval);
378                 break;
379         case 2:
380                 mode = FIELD_GET(OPERATING_MODE_PORT_2_MASK, regval);
381                 break;
382         case 3:
383                 mode = FIELD_GET(OPERATING_MODE_PORT_3_MASK, regval);
384                 break;
385         case 4:
386                 mode = FIELD_GET(OPERATING_MODE_PORT_4_MASK, regval);
387                 break;
388         default:
389                 mode = -EINVAL;
390         }
391
392         switch (mode) {
393         case OPERATING_MODE_OFF:
394                 return "Off";
395         case OPERATING_MODE_MANUAL:
396                 return "Manual";
397         case OPERATING_MODE_SEMI:
398                 return "Semi-Auto";
399         case OPERATING_MODE_AUTO:
400                 return "Auto";
401         default:
402                 return "Invalid";
403         }
404 }
405
406 static char *tps23861_port_detect_status(struct tps23861_data *data, int port)
407 {
408         unsigned int regval;
409
410         regmap_read(data->regmap,
411                     PORT_1_STATUS + (port - 1),
412                     &regval);
413
414         switch (FIELD_GET(PORT_STATUS_DETECT_MASK, regval)) {
415         case PORT_DETECT_UNKNOWN:
416                 return "Unknown device";
417         case PORT_DETECT_SHORT:
418                 return "Short circuit";
419         case PORT_DETECT_RESISTANCE_LOW:
420                 return "Too low resistance";
421         case PORT_DETECT_RESISTANCE_OK:
422                 return "Valid resistance";
423         case PORT_DETECT_RESISTANCE_HIGH:
424                 return "Too high resistance";
425         case PORT_DETECT_OPEN_CIRCUIT:
426                 return "Open circuit";
427         case PORT_DETECT_MOSFET_FAULT:
428                 return "MOSFET fault";
429         case PORT_DETECT_LEGACY:
430                 return "Legacy device";
431         case PORT_DETECT_CAPACITANCE_INVALID_BEYOND:
432                 return "Invalid capacitance, beyond clamp voltage";
433         case PORT_DETECT_CAPACITANCE_INVALID_DELTA:
434                 return "Invalid capacitance, insufficient voltage delta";
435         case PORT_DETECT_CAPACITANCE_OUT_OF_RANGE:
436                 return "Valid capacitance, outside of legacy range";
437         case PORT_DETECT_RESERVED:
438         case PORT_DETECT_RESERVED_2:
439         default:
440                 return "Invalid";
441         }
442 }
443
444 static char *tps23861_port_class_status(struct tps23861_data *data, int port)
445 {
446         unsigned int regval;
447
448         regmap_read(data->regmap,
449                     PORT_1_STATUS + (port - 1),
450                     &regval);
451
452         switch (FIELD_GET(PORT_STATUS_CLASS_MASK, regval)) {
453         case PORT_CLASS_UNKNOWN:
454                 return "Unknown";
455         case PORT_CLASS_RESERVED:
456         case PORT_CLASS_0:
457                 return "0";
458         case PORT_CLASS_1:
459                 return "1";
460         case PORT_CLASS_2:
461                 return "2";
462         case PORT_CLASS_3:
463                 return "3";
464         case PORT_CLASS_4:
465                 return "4";
466         case PORT_CLASS_OVERCURRENT:
467                 return "Overcurrent";
468         case PORT_CLASS_MISMATCH:
469                 return "Mismatch";
470         default:
471                 return "Invalid";
472         }
473 }
474
475 static char *tps23861_port_poe_plus_status(struct tps23861_data *data, int port)
476 {
477         unsigned int regval;
478
479         regmap_read(data->regmap, POE_PLUS, &regval);
480
481         if (BIT(port + 3) & regval)
482                 return "Yes";
483         else
484                 return "No";
485 }
486
487 static int tps23861_port_resistance(struct tps23861_data *data, int port)
488 {
489         u16 regval;
490
491         regmap_bulk_read(data->regmap,
492                          PORT_1_RESISTANCE_LSB + PORT_N_RESISTANCE_LSB_OFFSET * (port - 1),
493                          &regval,
494                          2);
495
496         switch (FIELD_GET(PORT_RESISTANCE_RSN_MASK, regval)) {
497         case PORT_RESISTANCE_RSN_OTHER:
498                 return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB) / 10000;
499         case PORT_RESISTANCE_RSN_LOW:
500                 return (FIELD_GET(PORT_RESISTANCE_MASK, regval) * RESISTANCE_LSB_LOW) / 10000;
501         case PORT_RESISTANCE_RSN_SHORT:
502         case PORT_RESISTANCE_RSN_OPEN:
503         default:
504                 return 0;
505         }
506 }
507
508 static int tps23861_port_status_show(struct seq_file *s, void *data)
509 {
510         struct tps23861_data *priv = s->private;
511         int i;
512
513         for (i = 1; i < TPS23861_NUM_PORTS + 1; i++) {
514                 seq_printf(s, "Port: \t\t%d\n", i);
515                 seq_printf(s, "Operating mode: %s\n", tps23861_port_operating_mode(priv, i));
516                 seq_printf(s, "Detected: \t%s\n", tps23861_port_detect_status(priv, i));
517                 seq_printf(s, "Class: \t\t%s\n", tps23861_port_class_status(priv, i));
518                 seq_printf(s, "PoE Plus: \t%s\n", tps23861_port_poe_plus_status(priv, i));
519                 seq_printf(s, "Resistance: \t%d\n", tps23861_port_resistance(priv, i));
520                 seq_putc(s, '\n');
521         }
522
523         return 0;
524 }
525
526 DEFINE_SHOW_ATTRIBUTE(tps23861_port_status);
527
528 static void tps23861_init_debugfs(struct tps23861_data *data)
529 {
530         data->debugfs_dir = debugfs_create_dir(data->client->name, NULL);
531
532         debugfs_create_file("port_status",
533                             0400,
534                             data->debugfs_dir,
535                             data,
536                             &tps23861_port_status_fops);
537 }
538
539 static int tps23861_probe(struct i2c_client *client)
540 {
541         struct device *dev = &client->dev;
542         struct tps23861_data *data;
543         struct device *hwmon_dev;
544         u32 shunt_resistor;
545
546         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
547         if (!data)
548                 return -ENOMEM;
549
550         data->client = client;
551         i2c_set_clientdata(client, data);
552
553         data->regmap = devm_regmap_init_i2c(client, &tps23861_regmap_config);
554         if (IS_ERR(data->regmap)) {
555                 dev_err(dev, "failed to allocate register map\n");
556                 return PTR_ERR(data->regmap);
557         }
558
559         if (!of_property_read_u32(dev->of_node, "shunt-resistor-micro-ohms", &shunt_resistor))
560                 data->shunt_resistor = shunt_resistor;
561         else
562                 data->shunt_resistor = SHUNT_RESISTOR_DEFAULT;
563
564         hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
565                                                          data, &tps23861_chip_info,
566                                                          NULL);
567         if (IS_ERR(hwmon_dev))
568                 return PTR_ERR(hwmon_dev);
569
570         tps23861_init_debugfs(data);
571
572         return 0;
573 }
574
575 static int tps23861_remove(struct i2c_client *client)
576 {
577         struct tps23861_data *data = i2c_get_clientdata(client);
578
579         debugfs_remove_recursive(data->debugfs_dir);
580
581         return 0;
582 }
583
584 static const struct of_device_id __maybe_unused tps23861_of_match[] = {
585         { .compatible = "ti,tps23861", },
586         { },
587 };
588 MODULE_DEVICE_TABLE(of, tps23861_of_match);
589
590 static struct i2c_driver tps23861_driver = {
591         .probe_new              = tps23861_probe,
592         .remove                 = tps23861_remove,
593         .driver = {
594                 .name           = "tps23861",
595                 .of_match_table = of_match_ptr(tps23861_of_match),
596         },
597 };
598 module_i2c_driver(tps23861_driver);
599
600 MODULE_LICENSE("GPL");
601 MODULE_AUTHOR("Robert Marko <robert.marko@sartura.hr>");
602 MODULE_DESCRIPTION("TI TPS23861 PoE PSE");