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'
56 #define INVALID_INDEX (-1U)
58 static struct sensor_group
{
60 const char *compatible
;
61 struct attribute_group group
;
65 {"fan", "ibm,opal-sensor-cooling-fan"},
66 {"temp", "ibm,opal-sensor-amb-temp"},
67 {"in", "ibm,opal-sensor-power-supply"},
68 {"power", "ibm,opal-sensor-power"}
72 u32 id
; /* An opaque id of the firmware for each sensor */
76 char label
[MAX_LABEL_LEN
];
77 char name
[MAX_ATTR_LEN
];
78 struct device_attribute dev_attr
;
81 struct platform_data
{
82 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
83 u32 sensors_count
; /* Total count of sensors from each group */
86 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
89 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
94 ret
= opal_get_sensor_data(sdata
->id
, &x
);
98 /* Convert temperature to milli-degrees */
99 if (sdata
->type
== TEMP
)
101 /* Convert power to micro-watts */
102 else if (sdata
->type
== POWER_INPUT
)
105 return sprintf(buf
, "%u\n", x
);
108 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*devattr
,
111 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
114 return sprintf(buf
, "%s\n", sdata
->label
);
117 static int __init
get_logical_cpu(int hwcpu
)
121 for_each_possible_cpu(cpu
)
122 if (get_hard_smp_processor_id(cpu
) == hwcpu
)
128 static void __init
make_sensor_label(struct device_node
*np
,
129 struct sensor_data
*sdata
,
135 n
= snprintf(sdata
->label
, sizeof(sdata
->label
), "%s", label
);
138 * Core temp pretty print
140 if (!of_property_read_u32(np
, "ibm,pir", &id
)) {
141 int cpuid
= get_logical_cpu(id
);
145 * The digital thermal sensors are associated
148 n
+= snprintf(sdata
->label
+ n
,
149 sizeof(sdata
->label
) - n
, " %d",
152 n
+= snprintf(sdata
->label
+ n
,
153 sizeof(sdata
->label
) - n
, " phy%d", id
);
157 * Membuffer pretty print
159 if (!of_property_read_u32(np
, "ibm,chip-id", &id
))
160 n
+= snprintf(sdata
->label
+ n
, sizeof(sdata
->label
) - n
,
164 static int get_sensor_index_attr(const char *name
, u32
*index
, char *attr
)
166 char *hash_pos
= strchr(name
, '#');
175 dash_pos
= strchr(hash_pos
, '-');
179 copy_len
= dash_pos
- hash_pos
- 1;
180 if (copy_len
>= sizeof(buf
))
183 strncpy(buf
, hash_pos
+ 1, copy_len
);
185 err
= kstrtou32(buf
, 10, index
);
189 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
194 static const char *convert_opal_attr_name(enum sensors type
,
195 const char *opal_attr
)
197 const char *attr_name
= NULL
;
199 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
201 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
203 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
206 else if (type
== FAN
)
214 * This function translates the DT node name into the 'hwmon' attribute name.
215 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
216 * which need to be mapped as fan2_input, temp1_max respectively before
217 * populating them inside hwmon device class.
219 static const char *parse_opal_node_name(const char *node_name
,
220 enum sensors type
, u32
*index
)
222 char attr_suffix
[MAX_ATTR_LEN
];
223 const char *attr_name
;
226 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
230 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
232 return ERR_PTR(-ENOENT
);
237 static int get_sensor_type(struct device_node
*np
)
242 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
243 if (of_device_is_compatible(np
, sensor_groups
[type
].compatible
))
248 * Let's check if we have a newer device tree
250 if (!of_device_is_compatible(np
, "ibm,opal-sensor"))
251 return MAX_SENSOR_TYPE
;
253 if (of_property_read_string(np
, "sensor-type", &str
))
254 return MAX_SENSOR_TYPE
;
256 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
257 if (!strcmp(str
, sensor_groups
[type
].name
))
260 return MAX_SENSOR_TYPE
;
263 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
264 struct sensor_data
*sdata_table
, int count
)
269 * We don't use the OPAL index on newer device trees
271 if (sdata
->opal_index
!= INVALID_INDEX
) {
272 for (i
= 0; i
< count
; i
++)
273 if (sdata_table
[i
].opal_index
== sdata
->opal_index
&&
274 sdata_table
[i
].type
== sdata
->type
)
275 return sdata_table
[i
].hwmon_index
;
277 return ++sensor_groups
[sdata
->type
].hwmon_index
;
280 static int populate_attr_groups(struct platform_device
*pdev
)
282 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
283 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
284 struct device_node
*opal
, *np
;
287 opal
= of_find_node_by_path("/ibm,opal/sensors");
288 for_each_child_of_node(opal
, np
) {
291 if (np
->name
== NULL
)
294 type
= get_sensor_type(np
);
295 if (type
== MAX_SENSOR_TYPE
)
298 sensor_groups
[type
].attr_count
++;
301 * add a new attribute for labels
303 if (!of_property_read_string(np
, "label", &label
))
304 sensor_groups
[type
].attr_count
++;
309 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
310 sensor_groups
[type
].group
.attrs
= devm_kzalloc(&pdev
->dev
,
311 sizeof(struct attribute
*) *
312 (sensor_groups
[type
].attr_count
+ 1),
314 if (!sensor_groups
[type
].group
.attrs
)
317 pgroups
[type
] = &sensor_groups
[type
].group
;
318 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
319 sensor_groups
[type
].attr_count
= 0;
325 static void create_hwmon_attr(struct sensor_data
*sdata
, const char *attr_name
,
326 ssize_t (*show
)(struct device
*dev
,
327 struct device_attribute
*attr
,
330 snprintf(sdata
->name
, MAX_ATTR_LEN
, "%s%d_%s",
331 sensor_groups
[sdata
->type
].name
, sdata
->hwmon_index
,
334 sysfs_attr_init(&sdata
->dev_attr
.attr
);
335 sdata
->dev_attr
.attr
.name
= sdata
->name
;
336 sdata
->dev_attr
.attr
.mode
= S_IRUGO
;
337 sdata
->dev_attr
.show
= show
;
341 * Iterate through the device tree for each child of 'sensors' node, create
342 * a sysfs attribute file, the file is named by translating the DT node name
343 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
346 static int create_device_attrs(struct platform_device
*pdev
)
348 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
349 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
350 struct device_node
*opal
, *np
;
351 struct sensor_data
*sdata
;
357 opal
= of_find_node_by_path("/ibm,opal/sensors");
358 sdata
= devm_kzalloc(&pdev
->dev
, pdata
->sensors_count
* sizeof(*sdata
),
365 for_each_child_of_node(opal
, np
) {
366 const char *attr_name
;
370 if (np
->name
== NULL
)
373 type
= get_sensor_type(np
);
374 if (type
== MAX_SENSOR_TYPE
)
378 * Newer device trees use a "sensor-data" property
381 if (of_property_read_u32(np
, "sensor-id", &sensor_id
) &&
382 of_property_read_u32(np
, "sensor-data", &sensor_id
)) {
384 "'sensor-id' missing in the node '%s'\n",
389 sdata
[count
].id
= sensor_id
;
390 sdata
[count
].type
= type
;
393 * If we can not parse the node name, it means we are
394 * running on a newer device tree. We can just forget
395 * about the OPAL index and use a defaut value for the
396 * hwmon attribute name
398 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
399 if (IS_ERR(attr_name
)) {
401 opal_index
= INVALID_INDEX
;
404 sdata
[count
].opal_index
= opal_index
;
405 sdata
[count
].hwmon_index
=
406 get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
408 create_hwmon_attr(&sdata
[count
], attr_name
, show_sensor
);
410 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
411 &sdata
[count
++].dev_attr
.attr
;
413 if (!of_property_read_string(np
, "label", &label
)) {
415 * For the label attribute, we can reuse the
416 * "properties" of the previous "input"
417 * attribute. They are related to the same
420 sdata
[count
].type
= type
;
421 sdata
[count
].opal_index
= sdata
[count
- 1].opal_index
;
422 sdata
[count
].hwmon_index
= sdata
[count
- 1].hwmon_index
;
424 make_sensor_label(np
, &sdata
[count
], label
);
426 create_hwmon_attr(&sdata
[count
], "label", show_label
);
428 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
429 &sdata
[count
++].dev_attr
.attr
;
438 static int ibmpowernv_probe(struct platform_device
*pdev
)
440 struct platform_data
*pdata
;
441 struct device
*hwmon_dev
;
444 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
448 platform_set_drvdata(pdev
, pdata
);
449 pdata
->sensors_count
= 0;
450 err
= populate_attr_groups(pdev
);
454 /* Create sysfs attribute data for each sensor found in the DT */
455 err
= create_device_attrs(pdev
);
459 /* Finally, register with hwmon */
460 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
464 return PTR_ERR_OR_ZERO(hwmon_dev
);
467 static const struct platform_device_id opal_sensor_driver_ids
[] = {
469 .name
= "opal-sensor",
473 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
475 static const struct of_device_id opal_sensor_match
[] = {
476 { .compatible
= "ibm,opal-sensor" },
479 MODULE_DEVICE_TABLE(of
, opal_sensor_match
);
481 static struct platform_driver ibmpowernv_driver
= {
482 .probe
= ibmpowernv_probe
,
483 .id_table
= opal_sensor_driver_ids
,
486 .of_match_table
= opal_sensor_match
,
490 module_platform_driver(ibmpowernv_driver
);
492 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
493 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
494 MODULE_LICENSE("GPL");