2 * IBM PowerNV platform sensors for temperature/fan/voltage/power
3 * Copyright (C) 2014 IBM
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
19 #define DRVNAME "ibmpowernv"
20 #define pr_fmt(fmt) DRVNAME ": " fmt
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>
28 #include <linux/slab.h>
30 #include <linux/platform_device.h>
32 #include <linux/err.h>
33 #include <asm/cputhreads.h>
36 #define MAX_ATTR_LEN 32
37 #define MAX_LABEL_LEN 64
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"
45 * Enumerates all the types of sensors in the POWERNV platform and does index
46 * into 'struct sensor_group'
58 #define INVALID_INDEX (-1U)
61 * 'compatible' string properties for sensor types as defined in old
62 * PowerNV firmware (skiboot). These are ordered as 'enum sensors'.
64 static const char * const legacy_compatibles
[] = {
65 "ibm,opal-sensor-cooling-fan",
66 "ibm,opal-sensor-amb-temp",
67 "ibm,opal-sensor-power-supply",
68 "ibm,opal-sensor-power"
71 static struct sensor_group
{
72 const char *name
; /* matches property 'sensor-type' */
73 struct attribute_group group
;
86 u32 id
; /* An opaque id of the firmware for each sensor */
90 char label
[MAX_LABEL_LEN
];
91 char name
[MAX_ATTR_LEN
];
92 struct device_attribute dev_attr
;
95 struct platform_data
{
96 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
97 u32 sensors_count
; /* Total count of sensors from each group */
100 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
103 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
108 ret
= opal_get_sensor_data_u64(sdata
->id
, &x
);
113 /* Convert temperature to milli-degrees */
114 if (sdata
->type
== TEMP
)
116 /* Convert power to micro-watts */
117 else if (sdata
->type
== POWER_INPUT
)
120 return sprintf(buf
, "%llu\n", x
);
123 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*devattr
,
126 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
129 return sprintf(buf
, "%s\n", sdata
->label
);
132 static int __init
get_logical_cpu(int hwcpu
)
136 for_each_possible_cpu(cpu
)
137 if (get_hard_smp_processor_id(cpu
) == hwcpu
)
143 static void __init
make_sensor_label(struct device_node
*np
,
144 struct sensor_data
*sdata
,
150 n
= snprintf(sdata
->label
, sizeof(sdata
->label
), "%s", label
);
153 * Core temp pretty print
155 if (!of_property_read_u32(np
, "ibm,pir", &id
)) {
156 int cpuid
= get_logical_cpu(id
);
160 * The digital thermal sensors are associated
163 n
+= snprintf(sdata
->label
+ n
,
164 sizeof(sdata
->label
) - n
, " %d",
167 n
+= snprintf(sdata
->label
+ n
,
168 sizeof(sdata
->label
) - n
, " phy%d", id
);
172 * Membuffer pretty print
174 if (!of_property_read_u32(np
, "ibm,chip-id", &id
))
175 n
+= snprintf(sdata
->label
+ n
, sizeof(sdata
->label
) - n
,
179 static int get_sensor_index_attr(const char *name
, u32
*index
, char *attr
)
181 char *hash_pos
= strchr(name
, '#');
190 dash_pos
= strchr(hash_pos
, '-');
194 copy_len
= dash_pos
- hash_pos
- 1;
195 if (copy_len
>= sizeof(buf
))
198 strncpy(buf
, hash_pos
+ 1, copy_len
);
200 err
= kstrtou32(buf
, 10, index
);
204 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
209 static const char *convert_opal_attr_name(enum sensors type
,
210 const char *opal_attr
)
212 const char *attr_name
= NULL
;
214 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
216 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
218 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
221 else if (type
== FAN
)
229 * This function translates the DT node name into the 'hwmon' attribute name.
230 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
231 * which need to be mapped as fan2_input, temp1_max respectively before
232 * populating them inside hwmon device class.
234 static const char *parse_opal_node_name(const char *node_name
,
235 enum sensors type
, u32
*index
)
237 char attr_suffix
[MAX_ATTR_LEN
];
238 const char *attr_name
;
241 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
245 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
247 return ERR_PTR(-ENOENT
);
252 static int get_sensor_type(struct device_node
*np
)
257 for (type
= 0; type
< ARRAY_SIZE(legacy_compatibles
); type
++) {
258 if (of_device_is_compatible(np
, legacy_compatibles
[type
]))
263 * Let's check if we have a newer device tree
265 if (!of_device_is_compatible(np
, "ibm,opal-sensor"))
266 return MAX_SENSOR_TYPE
;
268 if (of_property_read_string(np
, "sensor-type", &str
))
269 return MAX_SENSOR_TYPE
;
271 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
272 if (!strcmp(str
, sensor_groups
[type
].name
))
275 return MAX_SENSOR_TYPE
;
278 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
279 struct sensor_data
*sdata_table
, int count
)
284 * We don't use the OPAL index on newer device trees
286 if (sdata
->opal_index
!= INVALID_INDEX
) {
287 for (i
= 0; i
< count
; i
++)
288 if (sdata_table
[i
].opal_index
== sdata
->opal_index
&&
289 sdata_table
[i
].type
== sdata
->type
)
290 return sdata_table
[i
].hwmon_index
;
292 return ++sensor_groups
[sdata
->type
].hwmon_index
;
295 static int populate_attr_groups(struct platform_device
*pdev
)
297 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
298 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
299 struct device_node
*opal
, *np
;
302 opal
= of_find_node_by_path("/ibm,opal/sensors");
303 for_each_child_of_node(opal
, np
) {
306 if (np
->name
== NULL
)
309 type
= get_sensor_type(np
);
310 if (type
== MAX_SENSOR_TYPE
)
313 sensor_groups
[type
].attr_count
++;
316 * add attributes for labels, min and max
318 if (!of_property_read_string(np
, "label", &label
))
319 sensor_groups
[type
].attr_count
++;
320 if (of_find_property(np
, "sensor-data-min", NULL
))
321 sensor_groups
[type
].attr_count
++;
322 if (of_find_property(np
, "sensor-data-max", NULL
))
323 sensor_groups
[type
].attr_count
++;
328 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
329 sensor_groups
[type
].group
.attrs
= devm_kcalloc(&pdev
->dev
,
330 sensor_groups
[type
].attr_count
+ 1,
331 sizeof(struct attribute
*),
333 if (!sensor_groups
[type
].group
.attrs
)
336 pgroups
[type
] = &sensor_groups
[type
].group
;
337 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
338 sensor_groups
[type
].attr_count
= 0;
344 static void create_hwmon_attr(struct sensor_data
*sdata
, const char *attr_name
,
345 ssize_t (*show
)(struct device
*dev
,
346 struct device_attribute
*attr
,
349 snprintf(sdata
->name
, MAX_ATTR_LEN
, "%s%d_%s",
350 sensor_groups
[sdata
->type
].name
, sdata
->hwmon_index
,
353 sysfs_attr_init(&sdata
->dev_attr
.attr
);
354 sdata
->dev_attr
.attr
.name
= sdata
->name
;
355 sdata
->dev_attr
.attr
.mode
= S_IRUGO
;
356 sdata
->dev_attr
.show
= show
;
359 static void populate_sensor(struct sensor_data
*sdata
, int od
, int hd
, int sid
,
360 const char *attr_name
, enum sensors type
,
361 const struct attribute_group
*pgroup
,
362 ssize_t (*show
)(struct device
*dev
,
363 struct device_attribute
*attr
,
368 sdata
->opal_index
= od
;
369 sdata
->hwmon_index
= hd
;
370 create_hwmon_attr(sdata
, attr_name
, show
);
371 pgroup
->attrs
[sensor_groups
[type
].attr_count
++] = &sdata
->dev_attr
.attr
;
374 static char *get_max_attr(enum sensors type
)
378 return "input_highest";
384 static char *get_min_attr(enum sensors type
)
388 return "input_lowest";
395 * Iterate through the device tree for each child of 'sensors' node, create
396 * a sysfs attribute file, the file is named by translating the DT node name
397 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
400 static int create_device_attrs(struct platform_device
*pdev
)
402 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
403 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
404 struct device_node
*opal
, *np
;
405 struct sensor_data
*sdata
;
411 opal
= of_find_node_by_path("/ibm,opal/sensors");
412 sdata
= devm_kcalloc(&pdev
->dev
,
413 pdata
->sensors_count
, sizeof(*sdata
),
420 for_each_child_of_node(opal
, np
) {
421 const char *attr_name
;
425 if (np
->name
== NULL
)
428 type
= get_sensor_type(np
);
429 if (type
== MAX_SENSOR_TYPE
)
433 * Newer device trees use a "sensor-data" property
436 if (of_property_read_u32(np
, "sensor-id", &sensor_id
) &&
437 of_property_read_u32(np
, "sensor-data", &sensor_id
)) {
439 "'sensor-id' missing in the node '%s'\n",
444 sdata
[count
].id
= sensor_id
;
445 sdata
[count
].type
= type
;
448 * If we can not parse the node name, it means we are
449 * running on a newer device tree. We can just forget
450 * about the OPAL index and use a defaut value for the
451 * hwmon attribute name
453 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
454 if (IS_ERR(attr_name
)) {
456 opal_index
= INVALID_INDEX
;
459 sdata
[count
].opal_index
= opal_index
;
460 sdata
[count
].hwmon_index
=
461 get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
463 create_hwmon_attr(&sdata
[count
], attr_name
, show_sensor
);
465 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
466 &sdata
[count
++].dev_attr
.attr
;
468 if (!of_property_read_string(np
, "label", &label
)) {
470 * For the label attribute, we can reuse the
471 * "properties" of the previous "input"
472 * attribute. They are related to the same
476 make_sensor_label(np
, &sdata
[count
], label
);
477 populate_sensor(&sdata
[count
], opal_index
,
478 sdata
[count
- 1].hwmon_index
,
479 sensor_id
, "label", type
, pgroups
[type
],
484 if (!of_property_read_u32(np
, "sensor-data-max", &sensor_id
)) {
485 attr_name
= get_max_attr(type
);
486 populate_sensor(&sdata
[count
], opal_index
,
487 sdata
[count
- 1].hwmon_index
,
488 sensor_id
, attr_name
, type
,
489 pgroups
[type
], show_sensor
);
493 if (!of_property_read_u32(np
, "sensor-data-min", &sensor_id
)) {
494 attr_name
= get_min_attr(type
);
495 populate_sensor(&sdata
[count
], opal_index
,
496 sdata
[count
- 1].hwmon_index
,
497 sensor_id
, attr_name
, type
,
498 pgroups
[type
], show_sensor
);
508 static int ibmpowernv_probe(struct platform_device
*pdev
)
510 struct platform_data
*pdata
;
511 struct device
*hwmon_dev
;
514 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
518 platform_set_drvdata(pdev
, pdata
);
519 pdata
->sensors_count
= 0;
520 err
= populate_attr_groups(pdev
);
524 /* Create sysfs attribute data for each sensor found in the DT */
525 err
= create_device_attrs(pdev
);
529 /* Finally, register with hwmon */
530 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
534 return PTR_ERR_OR_ZERO(hwmon_dev
);
537 static const struct platform_device_id opal_sensor_driver_ids
[] = {
539 .name
= "opal-sensor",
543 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
545 static const struct of_device_id opal_sensor_match
[] = {
546 { .compatible
= "ibm,opal-sensor" },
549 MODULE_DEVICE_TABLE(of
, opal_sensor_match
);
551 static struct platform_driver ibmpowernv_driver
= {
552 .probe
= ibmpowernv_probe
,
553 .id_table
= opal_sensor_driver_ids
,
556 .of_match_table
= opal_sensor_match
,
560 module_platform_driver(ibmpowernv_driver
);
562 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
563 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
564 MODULE_LICENSE("GPL");