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'
57 #define INVALID_INDEX (-1U)
60 * 'compatible' string properties for sensor types as defined in old
61 * PowerNV firmware (skiboot). These are ordered as 'enum sensors'.
63 static 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"
70 static struct sensor_group
{
71 const char *name
; /* matches property 'sensor-type' */
72 struct attribute_group group
;
84 u32 id
; /* An opaque id of the firmware for each sensor */
88 char label
[MAX_LABEL_LEN
];
89 char name
[MAX_ATTR_LEN
];
90 struct device_attribute dev_attr
;
93 struct platform_data
{
94 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
95 u32 sensors_count
; /* Total count of sensors from each group */
98 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
101 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
106 ret
= opal_get_sensor_data(sdata
->id
, &x
);
110 /* Convert temperature to milli-degrees */
111 if (sdata
->type
== TEMP
)
113 /* Convert power to micro-watts */
114 else if (sdata
->type
== POWER_INPUT
)
117 return sprintf(buf
, "%u\n", x
);
120 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*devattr
,
123 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
126 return sprintf(buf
, "%s\n", sdata
->label
);
129 static int get_logical_cpu(int hwcpu
)
133 for_each_possible_cpu(cpu
)
134 if (get_hard_smp_processor_id(cpu
) == hwcpu
)
140 static void make_sensor_label(struct device_node
*np
,
141 struct sensor_data
*sdata
, const char *label
)
146 n
= snprintf(sdata
->label
, sizeof(sdata
->label
), "%s", label
);
149 * Core temp pretty print
151 if (!of_property_read_u32(np
, "ibm,pir", &id
)) {
152 int cpuid
= get_logical_cpu(id
);
156 * The digital thermal sensors are associated
159 n
+= snprintf(sdata
->label
+ n
,
160 sizeof(sdata
->label
) - n
, " %d",
163 n
+= snprintf(sdata
->label
+ n
,
164 sizeof(sdata
->label
) - n
, " phy%d", id
);
168 * Membuffer pretty print
170 if (!of_property_read_u32(np
, "ibm,chip-id", &id
))
171 n
+= snprintf(sdata
->label
+ n
, sizeof(sdata
->label
) - n
,
175 static int get_sensor_index_attr(const char *name
, u32
*index
, char *attr
)
177 char *hash_pos
= strchr(name
, '#');
186 dash_pos
= strchr(hash_pos
, '-');
190 copy_len
= dash_pos
- hash_pos
- 1;
191 if (copy_len
>= sizeof(buf
))
194 strncpy(buf
, hash_pos
+ 1, copy_len
);
196 err
= kstrtou32(buf
, 10, index
);
200 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
205 static const char *convert_opal_attr_name(enum sensors type
,
206 const char *opal_attr
)
208 const char *attr_name
= NULL
;
210 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
212 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
214 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
217 else if (type
== FAN
)
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.
230 static const char *parse_opal_node_name(const char *node_name
,
231 enum sensors type
, u32
*index
)
233 char attr_suffix
[MAX_ATTR_LEN
];
234 const char *attr_name
;
237 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
241 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
243 return ERR_PTR(-ENOENT
);
248 static int get_sensor_type(struct device_node
*np
)
253 for (type
= 0; type
< ARRAY_SIZE(legacy_compatibles
); type
++) {
254 if (of_device_is_compatible(np
, legacy_compatibles
[type
]))
259 * Let's check if we have a newer device tree
261 if (!of_device_is_compatible(np
, "ibm,opal-sensor"))
262 return MAX_SENSOR_TYPE
;
264 if (of_property_read_string(np
, "sensor-type", &str
))
265 return MAX_SENSOR_TYPE
;
267 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
268 if (!strcmp(str
, sensor_groups
[type
].name
))
271 return MAX_SENSOR_TYPE
;
274 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
275 struct sensor_data
*sdata_table
, int count
)
280 * We don't use the OPAL index on newer device trees
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
;
288 return ++sensor_groups
[sdata
->type
].hwmon_index
;
291 static int populate_attr_groups(struct platform_device
*pdev
)
293 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
294 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
295 struct device_node
*opal
, *np
;
298 opal
= of_find_node_by_path("/ibm,opal/sensors");
299 for_each_child_of_node(opal
, np
) {
302 if (np
->name
== NULL
)
305 type
= get_sensor_type(np
);
306 if (type
== MAX_SENSOR_TYPE
)
309 sensor_groups
[type
].attr_count
++;
312 * add attributes for labels, min and max
314 if (!of_property_read_string(np
, "label", &label
))
315 sensor_groups
[type
].attr_count
++;
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
++;
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),
329 if (!sensor_groups
[type
].group
.attrs
)
332 pgroups
[type
] = &sensor_groups
[type
].group
;
333 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
334 sensor_groups
[type
].attr_count
= 0;
340 static void create_hwmon_attr(struct sensor_data
*sdata
, const char *attr_name
,
341 ssize_t (*show
)(struct device
*dev
,
342 struct device_attribute
*attr
,
345 snprintf(sdata
->name
, MAX_ATTR_LEN
, "%s%d_%s",
346 sensor_groups
[sdata
->type
].name
, sdata
->hwmon_index
,
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
;
355 static 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
,
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
;
370 static char *get_max_attr(enum sensors type
)
374 return "input_highest";
380 static char *get_min_attr(enum sensors type
)
384 return "input_lowest";
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
396 static int create_device_attrs(struct platform_device
*pdev
)
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
;
407 opal
= of_find_node_by_path("/ibm,opal/sensors");
408 sdata
= devm_kzalloc(&pdev
->dev
, pdata
->sensors_count
* sizeof(*sdata
),
415 for_each_child_of_node(opal
, np
) {
416 const char *attr_name
;
420 if (np
->name
== NULL
)
423 type
= get_sensor_type(np
);
424 if (type
== MAX_SENSOR_TYPE
)
428 * Newer device trees use a "sensor-data" property
431 if (of_property_read_u32(np
, "sensor-id", &sensor_id
) &&
432 of_property_read_u32(np
, "sensor-data", &sensor_id
)) {
434 "'sensor-id' missing in the node '%s'\n",
439 sdata
[count
].id
= sensor_id
;
440 sdata
[count
].type
= type
;
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
448 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
449 if (IS_ERR(attr_name
)) {
451 opal_index
= INVALID_INDEX
;
454 sdata
[count
].opal_index
= opal_index
;
455 sdata
[count
].hwmon_index
=
456 get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
458 create_hwmon_attr(&sdata
[count
], attr_name
, show_sensor
);
460 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
461 &sdata
[count
++].dev_attr
.attr
;
463 if (!of_property_read_string(np
, "label", &label
)) {
465 * For the label attribute, we can reuse the
466 * "properties" of the previous "input"
467 * attribute. They are related to the same
471 make_sensor_label(np
, &sdata
[count
], label
);
472 populate_sensor(&sdata
[count
], opal_index
,
473 sdata
[count
- 1].hwmon_index
,
474 sensor_id
, "label", type
, pgroups
[type
],
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
);
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
);
503 static int ibmpowernv_probe(struct platform_device
*pdev
)
505 struct platform_data
*pdata
;
506 struct device
*hwmon_dev
;
509 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
513 platform_set_drvdata(pdev
, pdata
);
514 pdata
->sensors_count
= 0;
515 err
= populate_attr_groups(pdev
);
519 /* Create sysfs attribute data for each sensor found in the DT */
520 err
= create_device_attrs(pdev
);
524 /* Finally, register with hwmon */
525 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
529 return PTR_ERR_OR_ZERO(hwmon_dev
);
532 static const struct platform_device_id opal_sensor_driver_ids
[] = {
534 .name
= "opal-sensor",
538 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
540 static const struct of_device_id opal_sensor_match
[] = {
541 { .compatible
= "ibm,opal-sensor" },
544 MODULE_DEVICE_TABLE(of
, opal_sensor_match
);
546 static struct platform_driver ibmpowernv_driver
= {
547 .probe
= ibmpowernv_probe
,
548 .id_table
= opal_sensor_driver_ids
,
551 .of_match_table
= opal_sensor_match
,
555 module_platform_driver(ibmpowernv_driver
);
557 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
558 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
559 MODULE_LICENSE("GPL");