]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/ibmpowernv.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / ibmpowernv.c
CommitLineData
24c1aa85
NG
1/*
2 * IBM PowerNV platform sensors for temperature/fan/voltage/power
3 * Copyright (C) 2014 IBM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 */
18
19#define DRVNAME "ibmpowernv"
20#define pr_fmt(fmt) DRVNAME ": " fmt
21
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/of.h>
28#include <linux/slab.h>
29
30#include <linux/platform_device.h>
31#include <asm/opal.h>
32#include <linux/err.h>
3df2f59f 33#include <asm/cputhreads.h>
8416915c 34#include <asm/smp.h>
24c1aa85
NG
35
36#define MAX_ATTR_LEN 32
2bcd3787 37#define MAX_LABEL_LEN 64
24c1aa85
NG
38
39/* Sensor suffix name from DT */
40#define DT_FAULT_ATTR_SUFFIX "faulted"
41#define DT_DATA_ATTR_SUFFIX "data"
42#define DT_THRESHOLD_ATTR_SUFFIX "thrs"
43
44/*
45 * Enumerates all the types of sensors in the POWERNV platform and does index
46 * into 'struct sensor_group'
47 */
48enum sensors {
49 FAN,
96124610 50 TEMP,
24c1aa85
NG
51 POWER_SUPPLY,
52 POWER_INPUT,
3a2b3d37 53 CURRENT,
24c1aa85
NG
54 MAX_SENSOR_TYPE,
55};
56
14681637
CLG
57#define INVALID_INDEX (-1U)
58
3ab52160
CLG
59/*
60 * 'compatible' string properties for sensor types as defined in old
61 * PowerNV firmware (skiboot). These are ordered as 'enum sensors'.
62 */
63static const char * const legacy_compatibles[] = {
64 "ibm,opal-sensor-cooling-fan",
65 "ibm,opal-sensor-amb-temp",
66 "ibm,opal-sensor-power-supply",
67 "ibm,opal-sensor-power"
68};
69
24c1aa85 70static struct sensor_group {
3ab52160 71 const char *name; /* matches property 'sensor-type' */
24c1aa85
NG
72 struct attribute_group group;
73 u32 attr_count;
fcaf57b6 74 u32 hwmon_index;
24c1aa85 75} sensor_groups[] = {
3ab52160
CLG
76 { "fan" },
77 { "temp" },
78 { "in" },
3a2b3d37
SB
79 { "power" },
80 { "curr" },
24c1aa85
NG
81};
82
83struct sensor_data {
84 u32 id; /* An opaque id of the firmware for each sensor */
fcaf57b6
CLG
85 u32 hwmon_index;
86 u32 opal_index;
24c1aa85 87 enum sensors type;
2bcd3787 88 char label[MAX_LABEL_LEN];
24c1aa85
NG
89 char name[MAX_ATTR_LEN];
90 struct device_attribute dev_attr;
91};
92
93struct platform_data {
94 const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
95 u32 sensors_count; /* Total count of sensors from each group */
96};
97
24c1aa85
NG
98static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
99 char *buf)
100{
101 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
102 dev_attr);
103 ssize_t ret;
104 u32 x;
105
106 ret = opal_get_sensor_data(sdata->id, &x);
107 if (ret)
108 return ret;
109
110 /* Convert temperature to milli-degrees */
96124610 111 if (sdata->type == TEMP)
24c1aa85
NG
112 x *= 1000;
113 /* Convert power to micro-watts */
114 else if (sdata->type == POWER_INPUT)
115 x *= 1000000;
116
117 return sprintf(buf, "%u\n", x);
118}
119
2bcd3787
CLG
120static ssize_t show_label(struct device *dev, struct device_attribute *devattr,
121 char *buf)
122{
123 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
124 dev_attr);
125
126 return sprintf(buf, "%s\n", sdata->label);
127}
128
84323dde 129static int get_logical_cpu(int hwcpu)
3df2f59f
CLG
130{
131 int cpu;
132
133 for_each_possible_cpu(cpu)
134 if (get_hard_smp_processor_id(cpu) == hwcpu)
135 return cpu;
136
137 return -ENOENT;
138}
139
84323dde
GU
140static void make_sensor_label(struct device_node *np,
141 struct sensor_data *sdata, const char *label)
2bcd3787 142{
3df2f59f 143 u32 id;
2bcd3787
CLG
144 size_t n;
145
146 n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
3df2f59f
CLG
147
148 /*
149 * Core temp pretty print
150 */
151 if (!of_property_read_u32(np, "ibm,pir", &id)) {
152 int cpuid = get_logical_cpu(id);
153
154 if (cpuid >= 0)
155 /*
156 * The digital thermal sensors are associated
acf32964 157 * with a core.
3df2f59f
CLG
158 */
159 n += snprintf(sdata->label + n,
acf32964
MN
160 sizeof(sdata->label) - n, " %d",
161 cpuid);
3df2f59f
CLG
162 else
163 n += snprintf(sdata->label + n,
164 sizeof(sdata->label) - n, " phy%d", id);
165 }
166
167 /*
168 * Membuffer pretty print
169 */
170 if (!of_property_read_u32(np, "ibm,chip-id", &id))
171 n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
172 " %d", id & 0xffff);
2bcd3787
CLG
173}
174
175static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
24c1aa85
NG
176{
177 char *hash_pos = strchr(name, '#');
178 char buf[8] = { 0 };
179 char *dash_pos;
180 u32 copy_len;
181 int err;
182
183 if (!hash_pos)
184 return -EINVAL;
185
186 dash_pos = strchr(hash_pos, '-');
187 if (!dash_pos)
188 return -EINVAL;
189
190 copy_len = dash_pos - hash_pos - 1;
191 if (copy_len >= sizeof(buf))
192 return -EINVAL;
193
194 strncpy(buf, hash_pos + 1, copy_len);
195
196 err = kstrtou32(buf, 10, index);
197 if (err)
198 return err;
199
200 strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
201
202 return 0;
203}
204
ccc9ac6c
CLG
205static const char *convert_opal_attr_name(enum sensors type,
206 const char *opal_attr)
207{
208 const char *attr_name = NULL;
209
210 if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
211 attr_name = "fault";
212 } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
213 attr_name = "input";
214 } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
215 if (type == TEMP)
216 attr_name = "max";
217 else if (type == FAN)
218 attr_name = "min";
219 }
220
221 return attr_name;
222}
223
24c1aa85
NG
224/*
225 * This function translates the DT node name into the 'hwmon' attribute name.
226 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
227 * which need to be mapped as fan2_input, temp1_max respectively before
228 * populating them inside hwmon device class.
229 */
f9f54f16
CLG
230static const char *parse_opal_node_name(const char *node_name,
231 enum sensors type, u32 *index)
24c1aa85
NG
232{
233 char attr_suffix[MAX_ATTR_LEN];
ccc9ac6c 234 const char *attr_name;
24c1aa85
NG
235 int err;
236
f9f54f16
CLG
237 err = get_sensor_index_attr(node_name, index, attr_suffix);
238 if (err)
239 return ERR_PTR(err);
24c1aa85 240
ccc9ac6c
CLG
241 attr_name = convert_opal_attr_name(type, attr_suffix);
242 if (!attr_name)
f9f54f16 243 return ERR_PTR(-ENOENT);
24c1aa85 244
f9f54f16 245 return attr_name;
24c1aa85
NG
246}
247
c4ad4720
CLG
248static int get_sensor_type(struct device_node *np)
249{
250 enum sensors type;
14681637 251 const char *str;
c4ad4720 252
3ab52160
CLG
253 for (type = 0; type < ARRAY_SIZE(legacy_compatibles); type++) {
254 if (of_device_is_compatible(np, legacy_compatibles[type]))
c4ad4720
CLG
255 return type;
256 }
14681637
CLG
257
258 /*
259 * Let's check if we have a newer device tree
260 */
261 if (!of_device_is_compatible(np, "ibm,opal-sensor"))
262 return MAX_SENSOR_TYPE;
263
264 if (of_property_read_string(np, "sensor-type", &str))
265 return MAX_SENSOR_TYPE;
266
267 for (type = 0; type < MAX_SENSOR_TYPE; type++)
268 if (!strcmp(str, sensor_groups[type].name))
269 return type;
270
c4ad4720
CLG
271 return MAX_SENSOR_TYPE;
272}
273
fcaf57b6
CLG
274static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
275 struct sensor_data *sdata_table, int count)
276{
277 int i;
278
14681637
CLG
279 /*
280 * We don't use the OPAL index on newer device trees
281 */
282 if (sdata->opal_index != INVALID_INDEX) {
283 for (i = 0; i < count; i++)
284 if (sdata_table[i].opal_index == sdata->opal_index &&
285 sdata_table[i].type == sdata->type)
286 return sdata_table[i].hwmon_index;
287 }
fcaf57b6
CLG
288 return ++sensor_groups[sdata->type].hwmon_index;
289}
290
8de303ba 291static int populate_attr_groups(struct platform_device *pdev)
24c1aa85
NG
292{
293 struct platform_data *pdata = platform_get_drvdata(pdev);
294 const struct attribute_group **pgroups = pdata->attr_groups;
295 struct device_node *opal, *np;
296 enum sensors type;
297
298 opal = of_find_node_by_path("/ibm,opal/sensors");
24c1aa85 299 for_each_child_of_node(opal, np) {
2bcd3787
CLG
300 const char *label;
301
24c1aa85
NG
302 if (np->name == NULL)
303 continue;
304
c4ad4720 305 type = get_sensor_type(np);
2bcd3787
CLG
306 if (type == MAX_SENSOR_TYPE)
307 continue;
308
309 sensor_groups[type].attr_count++;
310
311 /*
996cf5a5 312 * add attributes for labels, min and max
2bcd3787
CLG
313 */
314 if (!of_property_read_string(np, "label", &label))
c4ad4720 315 sensor_groups[type].attr_count++;
996cf5a5
SB
316 if (of_find_property(np, "sensor-data-min", NULL))
317 sensor_groups[type].attr_count++;
318 if (of_find_property(np, "sensor-data-max", NULL))
319 sensor_groups[type].attr_count++;
24c1aa85
NG
320 }
321
322 of_node_put(opal);
323
324 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
325 sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
326 sizeof(struct attribute *) *
327 (sensor_groups[type].attr_count + 1),
328 GFP_KERNEL);
329 if (!sensor_groups[type].group.attrs)
330 return -ENOMEM;
331
332 pgroups[type] = &sensor_groups[type].group;
333 pdata->sensors_count += sensor_groups[type].attr_count;
334 sensor_groups[type].attr_count = 0;
335 }
336
337 return 0;
338}
339
9e4f74b1
CLG
340static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
341 ssize_t (*show)(struct device *dev,
342 struct device_attribute *attr,
343 char *buf))
344{
345 snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
346 sensor_groups[sdata->type].name, sdata->hwmon_index,
347 attr_name);
348
349 sysfs_attr_init(&sdata->dev_attr.attr);
350 sdata->dev_attr.attr.name = sdata->name;
351 sdata->dev_attr.attr.mode = S_IRUGO;
352 sdata->dev_attr.show = show;
353}
354
996cf5a5
SB
355static void populate_sensor(struct sensor_data *sdata, int od, int hd, int sid,
356 const char *attr_name, enum sensors type,
357 const struct attribute_group *pgroup,
358 ssize_t (*show)(struct device *dev,
359 struct device_attribute *attr,
360 char *buf))
361{
362 sdata->id = sid;
363 sdata->type = type;
364 sdata->opal_index = od;
365 sdata->hwmon_index = hd;
366 create_hwmon_attr(sdata, attr_name, show);
367 pgroup->attrs[sensor_groups[type].attr_count++] = &sdata->dev_attr.attr;
368}
369
370static char *get_max_attr(enum sensors type)
371{
372 switch (type) {
373 case POWER_INPUT:
374 return "input_highest";
375 default:
376 return "highest";
377 }
378}
379
380static char *get_min_attr(enum sensors type)
381{
382 switch (type) {
383 case POWER_INPUT:
384 return "input_lowest";
385 default:
386 return "lowest";
387 }
388}
389
24c1aa85
NG
390/*
391 * Iterate through the device tree for each child of 'sensors' node, create
392 * a sysfs attribute file, the file is named by translating the DT node name
393 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
394 * etc..
395 */
8de303ba 396static int create_device_attrs(struct platform_device *pdev)
24c1aa85
NG
397{
398 struct platform_data *pdata = platform_get_drvdata(pdev);
399 const struct attribute_group **pgroups = pdata->attr_groups;
400 struct device_node *opal, *np;
401 struct sensor_data *sdata;
18d03f3c 402 u32 sensor_id;
24c1aa85
NG
403 enum sensors type;
404 u32 count = 0;
405 int err = 0;
406
407 opal = of_find_node_by_path("/ibm,opal/sensors");
408 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
409 GFP_KERNEL);
410 if (!sdata) {
411 err = -ENOMEM;
412 goto exit_put_node;
413 }
414
415 for_each_child_of_node(opal, np) {
f9f54f16
CLG
416 const char *attr_name;
417 u32 opal_index;
2bcd3787 418 const char *label;
f9f54f16 419
24c1aa85
NG
420 if (np->name == NULL)
421 continue;
422
c4ad4720 423 type = get_sensor_type(np);
24c1aa85
NG
424 if (type == MAX_SENSOR_TYPE)
425 continue;
426
14681637
CLG
427 /*
428 * Newer device trees use a "sensor-data" property
429 * name for input.
430 */
431 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
432 of_property_read_u32(np, "sensor-data", &sensor_id)) {
24c1aa85
NG
433 dev_info(&pdev->dev,
434 "'sensor-id' missing in the node '%s'\n",
435 np->name);
436 continue;
437 }
438
18d03f3c 439 sdata[count].id = sensor_id;
24c1aa85 440 sdata[count].type = type;
f9f54f16 441
14681637
CLG
442 /*
443 * If we can not parse the node name, it means we are
444 * running on a newer device tree. We can just forget
445 * about the OPAL index and use a defaut value for the
446 * hwmon attribute name
447 */
f9f54f16
CLG
448 attr_name = parse_opal_node_name(np->name, type, &opal_index);
449 if (IS_ERR(attr_name)) {
14681637
CLG
450 attr_name = "input";
451 opal_index = INVALID_INDEX;
f9f54f16
CLG
452 }
453
fcaf57b6
CLG
454 sdata[count].opal_index = opal_index;
455 sdata[count].hwmon_index =
456 get_sensor_hwmon_index(&sdata[count], sdata, count);
457
9e4f74b1 458 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
24c1aa85
NG
459
460 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
461 &sdata[count++].dev_attr.attr;
2bcd3787
CLG
462
463 if (!of_property_read_string(np, "label", &label)) {
464 /*
465 * For the label attribute, we can reuse the
466 * "properties" of the previous "input"
467 * attribute. They are related to the same
468 * sensor.
469 */
2bcd3787
CLG
470
471 make_sensor_label(np, &sdata[count], label);
996cf5a5
SB
472 populate_sensor(&sdata[count], opal_index,
473 sdata[count - 1].hwmon_index,
474 sensor_id, "label", type, pgroups[type],
475 show_label);
476 count++;
477 }
2bcd3787 478
996cf5a5
SB
479 if (!of_property_read_u32(np, "sensor-data-max", &sensor_id)) {
480 attr_name = get_max_attr(type);
481 populate_sensor(&sdata[count], opal_index,
482 sdata[count - 1].hwmon_index,
483 sensor_id, attr_name, type,
484 pgroups[type], show_sensor);
485 count++;
486 }
2bcd3787 487
996cf5a5
SB
488 if (!of_property_read_u32(np, "sensor-data-min", &sensor_id)) {
489 attr_name = get_min_attr(type);
490 populate_sensor(&sdata[count], opal_index,
491 sdata[count - 1].hwmon_index,
492 sensor_id, attr_name, type,
493 pgroups[type], show_sensor);
494 count++;
2bcd3787 495 }
24c1aa85
NG
496 }
497
498exit_put_node:
499 of_node_put(opal);
500 return err;
501}
502
8de303ba 503static int ibmpowernv_probe(struct platform_device *pdev)
24c1aa85
NG
504{
505 struct platform_data *pdata;
506 struct device *hwmon_dev;
507 int err;
508
509 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
510 if (!pdata)
511 return -ENOMEM;
512
513 platform_set_drvdata(pdev, pdata);
514 pdata->sensors_count = 0;
515 err = populate_attr_groups(pdev);
516 if (err)
517 return err;
518
519 /* Create sysfs attribute data for each sensor found in the DT */
520 err = create_device_attrs(pdev);
521 if (err)
522 return err;
523
524 /* Finally, register with hwmon */
525 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
526 pdata,
527 pdata->attr_groups);
528
529 return PTR_ERR_OR_ZERO(hwmon_dev);
530}
531
8de303ba
NG
532static const struct platform_device_id opal_sensor_driver_ids[] = {
533 {
534 .name = "opal-sensor",
535 },
536 { }
537};
538MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
539
0b056b29
CLG
540static const struct of_device_id opal_sensor_match[] = {
541 { .compatible = "ibm,opal-sensor" },
542 { },
543};
544MODULE_DEVICE_TABLE(of, opal_sensor_match);
545
24c1aa85 546static struct platform_driver ibmpowernv_driver = {
8de303ba
NG
547 .probe = ibmpowernv_probe,
548 .id_table = opal_sensor_driver_ids,
549 .driver = {
8de303ba 550 .name = DRVNAME,
0b056b29 551 .of_match_table = opal_sensor_match,
24c1aa85
NG
552 },
553};
554
3bdec670 555module_platform_driver(ibmpowernv_driver);
24c1aa85
NG
556
557MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
558MODULE_DESCRIPTION("IBM POWERNV platform sensors");
559MODULE_LICENSE("GPL");