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
;
93 struct sensor_group_data
*sgrp_data
;
96 struct sensor_group_data
{
102 struct platform_data
{
103 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
104 struct sensor_group_data
*sgrp_data
;
105 u32 sensors_count
; /* Total count of sensors from each group */
106 u32 nr_sensor_groups
; /* Total number of sensor groups */
109 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
112 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
117 if (sdata
->sgrp_data
&& !sdata
->sgrp_data
->enable
)
120 ret
= opal_get_sensor_data_u64(sdata
->id
, &x
);
125 /* Convert temperature to milli-degrees */
126 if (sdata
->type
== TEMP
)
128 /* Convert power to micro-watts */
129 else if (sdata
->type
== POWER_INPUT
)
132 return sprintf(buf
, "%llu\n", x
);
135 static ssize_t
show_enable(struct device
*dev
,
136 struct device_attribute
*devattr
, char *buf
)
138 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
141 return sprintf(buf
, "%u\n", sdata
->sgrp_data
->enable
);
144 static ssize_t
store_enable(struct device
*dev
,
145 struct device_attribute
*devattr
,
146 const char *buf
, size_t count
)
148 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
150 struct sensor_group_data
*sgrp_data
= sdata
->sgrp_data
;
154 ret
= kstrtobool(buf
, &data
);
158 ret
= mutex_lock_interruptible(&sgrp_data
->mutex
);
162 if (data
!= sgrp_data
->enable
) {
163 ret
= sensor_group_enable(sgrp_data
->gid
, data
);
165 sgrp_data
->enable
= data
;
171 mutex_unlock(&sgrp_data
->mutex
);
175 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*devattr
,
178 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
181 return sprintf(buf
, "%s\n", sdata
->label
);
184 static int __init
get_logical_cpu(int hwcpu
)
188 for_each_possible_cpu(cpu
)
189 if (get_hard_smp_processor_id(cpu
) == hwcpu
)
195 static void __init
make_sensor_label(struct device_node
*np
,
196 struct sensor_data
*sdata
,
202 n
= snprintf(sdata
->label
, sizeof(sdata
->label
), "%s", label
);
205 * Core temp pretty print
207 if (!of_property_read_u32(np
, "ibm,pir", &id
)) {
208 int cpuid
= get_logical_cpu(id
);
212 * The digital thermal sensors are associated
215 n
+= snprintf(sdata
->label
+ n
,
216 sizeof(sdata
->label
) - n
, " %d",
219 n
+= snprintf(sdata
->label
+ n
,
220 sizeof(sdata
->label
) - n
, " phy%d", id
);
224 * Membuffer pretty print
226 if (!of_property_read_u32(np
, "ibm,chip-id", &id
))
227 n
+= snprintf(sdata
->label
+ n
, sizeof(sdata
->label
) - n
,
231 static int get_sensor_index_attr(const char *name
, u32
*index
, char *attr
)
233 char *hash_pos
= strchr(name
, '#');
242 dash_pos
= strchr(hash_pos
, '-');
246 copy_len
= dash_pos
- hash_pos
- 1;
247 if (copy_len
>= sizeof(buf
))
250 strncpy(buf
, hash_pos
+ 1, copy_len
);
252 err
= kstrtou32(buf
, 10, index
);
256 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
261 static const char *convert_opal_attr_name(enum sensors type
,
262 const char *opal_attr
)
264 const char *attr_name
= NULL
;
266 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
268 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
270 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
273 else if (type
== FAN
)
281 * This function translates the DT node name into the 'hwmon' attribute name.
282 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
283 * which need to be mapped as fan2_input, temp1_max respectively before
284 * populating them inside hwmon device class.
286 static const char *parse_opal_node_name(const char *node_name
,
287 enum sensors type
, u32
*index
)
289 char attr_suffix
[MAX_ATTR_LEN
];
290 const char *attr_name
;
293 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
297 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
299 return ERR_PTR(-ENOENT
);
304 static int get_sensor_type(struct device_node
*np
)
309 for (type
= 0; type
< ARRAY_SIZE(legacy_compatibles
); type
++) {
310 if (of_device_is_compatible(np
, legacy_compatibles
[type
]))
315 * Let's check if we have a newer device tree
317 if (!of_device_is_compatible(np
, "ibm,opal-sensor"))
318 return MAX_SENSOR_TYPE
;
320 if (of_property_read_string(np
, "sensor-type", &str
))
321 return MAX_SENSOR_TYPE
;
323 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
324 if (!strcmp(str
, sensor_groups
[type
].name
))
327 return MAX_SENSOR_TYPE
;
330 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
331 struct sensor_data
*sdata_table
, int count
)
336 * We don't use the OPAL index on newer device trees
338 if (sdata
->opal_index
!= INVALID_INDEX
) {
339 for (i
= 0; i
< count
; i
++)
340 if (sdata_table
[i
].opal_index
== sdata
->opal_index
&&
341 sdata_table
[i
].type
== sdata
->type
)
342 return sdata_table
[i
].hwmon_index
;
344 return ++sensor_groups
[sdata
->type
].hwmon_index
;
347 static int init_sensor_group_data(struct platform_device
*pdev
,
348 struct platform_data
*pdata
)
350 struct sensor_group_data
*sgrp_data
;
351 struct device_node
*groups
, *sgrp
;
352 int count
= 0, ret
= 0;
355 groups
= of_find_compatible_node(NULL
, NULL
, "ibm,opal-sensor-group");
359 for_each_child_of_node(groups
, sgrp
) {
360 type
= get_sensor_type(sgrp
);
361 if (type
!= MAX_SENSOR_TYPE
)
362 pdata
->nr_sensor_groups
++;
365 if (!pdata
->nr_sensor_groups
)
368 sgrp_data
= devm_kcalloc(&pdev
->dev
, pdata
->nr_sensor_groups
,
369 sizeof(*sgrp_data
), GFP_KERNEL
);
375 for_each_child_of_node(groups
, sgrp
) {
378 type
= get_sensor_type(sgrp
);
379 if (type
== MAX_SENSOR_TYPE
)
382 if (of_property_read_u32(sgrp
, "sensor-group-id", &gid
))
385 if (of_count_phandle_with_args(sgrp
, "sensors", NULL
) <= 0)
388 sensor_groups
[type
].attr_count
++;
389 sgrp_data
[count
].gid
= gid
;
390 mutex_init(&sgrp_data
[count
].mutex
);
391 sgrp_data
[count
++].enable
= false;
394 pdata
->sgrp_data
= sgrp_data
;
400 static struct sensor_group_data
*get_sensor_group(struct platform_data
*pdata
,
401 struct device_node
*node
,
404 struct sensor_group_data
*sgrp_data
= pdata
->sgrp_data
;
405 struct device_node
*groups
, *sgrp
;
407 groups
= of_find_compatible_node(NULL
, NULL
, "ibm,opal-sensor-group");
411 for_each_child_of_node(groups
, sgrp
) {
412 struct of_phandle_iterator it
;
417 type
= get_sensor_type(sgrp
);
421 if (of_property_read_u32(sgrp
, "sensor-group-id", &gid
))
424 of_for_each_phandle(&it
, rc
, sgrp
, "sensors", NULL
, 0)
425 if (it
.phandle
== node
->phandle
) {
426 of_node_put(it
.node
);
433 for (i
= 0; i
< pdata
->nr_sensor_groups
; i
++)
434 if (gid
== sgrp_data
[i
].gid
) {
437 return &sgrp_data
[i
];
445 static int populate_attr_groups(struct platform_device
*pdev
)
447 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
448 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
449 struct device_node
*opal
, *np
;
453 ret
= init_sensor_group_data(pdev
, pdata
);
457 opal
= of_find_node_by_path("/ibm,opal/sensors");
458 for_each_child_of_node(opal
, np
) {
461 type
= get_sensor_type(np
);
462 if (type
== MAX_SENSOR_TYPE
)
465 sensor_groups
[type
].attr_count
++;
468 * add attributes for labels, min and max
470 if (!of_property_read_string(np
, "label", &label
))
471 sensor_groups
[type
].attr_count
++;
472 if (of_find_property(np
, "sensor-data-min", NULL
))
473 sensor_groups
[type
].attr_count
++;
474 if (of_find_property(np
, "sensor-data-max", NULL
))
475 sensor_groups
[type
].attr_count
++;
480 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
481 sensor_groups
[type
].group
.attrs
= devm_kcalloc(&pdev
->dev
,
482 sensor_groups
[type
].attr_count
+ 1,
483 sizeof(struct attribute
*),
485 if (!sensor_groups
[type
].group
.attrs
)
488 pgroups
[type
] = &sensor_groups
[type
].group
;
489 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
490 sensor_groups
[type
].attr_count
= 0;
496 static void create_hwmon_attr(struct sensor_data
*sdata
, const char *attr_name
,
497 ssize_t (*show
)(struct device
*dev
,
498 struct device_attribute
*attr
,
500 ssize_t (*store
)(struct device
*dev
,
501 struct device_attribute
*attr
,
502 const char *buf
, size_t count
))
504 snprintf(sdata
->name
, MAX_ATTR_LEN
, "%s%d_%s",
505 sensor_groups
[sdata
->type
].name
, sdata
->hwmon_index
,
508 sysfs_attr_init(&sdata
->dev_attr
.attr
);
509 sdata
->dev_attr
.attr
.name
= sdata
->name
;
510 sdata
->dev_attr
.show
= show
;
512 sdata
->dev_attr
.store
= store
;
513 sdata
->dev_attr
.attr
.mode
= 0664;
515 sdata
->dev_attr
.attr
.mode
= 0444;
519 static void populate_sensor(struct sensor_data
*sdata
, int od
, int hd
, int sid
,
520 const char *attr_name
, enum sensors type
,
521 const struct attribute_group
*pgroup
,
522 struct sensor_group_data
*sgrp_data
,
523 ssize_t (*show
)(struct device
*dev
,
524 struct device_attribute
*attr
,
526 ssize_t (*store
)(struct device
*dev
,
527 struct device_attribute
*attr
,
528 const char *buf
, size_t count
))
532 sdata
->opal_index
= od
;
533 sdata
->hwmon_index
= hd
;
534 create_hwmon_attr(sdata
, attr_name
, show
, store
);
535 pgroup
->attrs
[sensor_groups
[type
].attr_count
++] = &sdata
->dev_attr
.attr
;
536 sdata
->sgrp_data
= sgrp_data
;
539 static char *get_max_attr(enum sensors type
)
543 return "input_highest";
549 static char *get_min_attr(enum sensors type
)
553 return "input_lowest";
560 * Iterate through the device tree for each child of 'sensors' node, create
561 * a sysfs attribute file, the file is named by translating the DT node name
562 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
565 static int create_device_attrs(struct platform_device
*pdev
)
567 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
568 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
569 struct device_node
*opal
, *np
;
570 struct sensor_data
*sdata
;
572 u32 group_attr_id
[MAX_SENSOR_TYPE
] = {0};
574 sdata
= devm_kcalloc(&pdev
->dev
,
575 pdata
->sensors_count
, sizeof(*sdata
),
580 opal
= of_find_node_by_path("/ibm,opal/sensors");
581 for_each_child_of_node(opal
, np
) {
582 struct sensor_group_data
*sgrp_data
;
583 const char *attr_name
;
584 u32 opal_index
, hw_id
;
589 type
= get_sensor_type(np
);
590 if (type
== MAX_SENSOR_TYPE
)
594 * Newer device trees use a "sensor-data" property
597 if (of_property_read_u32(np
, "sensor-id", &sensor_id
) &&
598 of_property_read_u32(np
, "sensor-data", &sensor_id
)) {
600 "'sensor-id' missing in the node '%pOFn'\n",
605 sdata
[count
].id
= sensor_id
;
606 sdata
[count
].type
= type
;
609 * If we can not parse the node name, it means we are
610 * running on a newer device tree. We can just forget
611 * about the OPAL index and use a defaut value for the
612 * hwmon attribute name
614 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
615 if (IS_ERR(attr_name
)) {
617 opal_index
= INVALID_INDEX
;
620 hw_id
= get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
621 sgrp_data
= get_sensor_group(pdata
, np
, type
);
622 populate_sensor(&sdata
[count
], opal_index
, hw_id
, sensor_id
,
623 attr_name
, type
, pgroups
[type
], sgrp_data
,
627 if (!of_property_read_string(np
, "label", &label
)) {
629 * For the label attribute, we can reuse the
630 * "properties" of the previous "input"
631 * attribute. They are related to the same
635 make_sensor_label(np
, &sdata
[count
], label
);
636 populate_sensor(&sdata
[count
], opal_index
, hw_id
,
637 sensor_id
, "label", type
, pgroups
[type
],
638 NULL
, show_label
, NULL
);
642 if (!of_property_read_u32(np
, "sensor-data-max", &sensor_id
)) {
643 attr_name
= get_max_attr(type
);
644 populate_sensor(&sdata
[count
], opal_index
, hw_id
,
645 sensor_id
, attr_name
, type
,
646 pgroups
[type
], sgrp_data
, show_sensor
,
651 if (!of_property_read_u32(np
, "sensor-data-min", &sensor_id
)) {
652 attr_name
= get_min_attr(type
);
653 populate_sensor(&sdata
[count
], opal_index
, hw_id
,
654 sensor_id
, attr_name
, type
,
655 pgroups
[type
], sgrp_data
, show_sensor
,
660 if (sgrp_data
&& !sgrp_data
->enable
) {
661 sgrp_data
->enable
= true;
662 hw_id
= ++group_attr_id
[type
];
663 populate_sensor(&sdata
[count
], opal_index
, hw_id
,
664 sgrp_data
->gid
, "enable", type
,
665 pgroups
[type
], sgrp_data
, show_enable
,
675 static int ibmpowernv_probe(struct platform_device
*pdev
)
677 struct platform_data
*pdata
;
678 struct device
*hwmon_dev
;
681 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
685 platform_set_drvdata(pdev
, pdata
);
686 pdata
->sensors_count
= 0;
687 pdata
->nr_sensor_groups
= 0;
688 err
= populate_attr_groups(pdev
);
692 /* Create sysfs attribute data for each sensor found in the DT */
693 err
= create_device_attrs(pdev
);
697 /* Finally, register with hwmon */
698 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
702 return PTR_ERR_OR_ZERO(hwmon_dev
);
705 static const struct platform_device_id opal_sensor_driver_ids
[] = {
707 .name
= "opal-sensor",
711 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
713 static const struct of_device_id opal_sensor_match
[] = {
714 { .compatible
= "ibm,opal-sensor" },
717 MODULE_DEVICE_TABLE(of
, opal_sensor_match
);
719 static struct platform_driver ibmpowernv_driver
= {
720 .probe
= ibmpowernv_probe
,
721 .id_table
= opal_sensor_driver_ids
,
724 .of_match_table
= opal_sensor_match
,
728 module_platform_driver(ibmpowernv_driver
);
730 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
731 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
732 MODULE_LICENSE("GPL");