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>
34 #define MAX_ATTR_LEN 32
35 #define MAX_LABEL_LEN 64
37 /* Sensor suffix name from DT */
38 #define DT_FAULT_ATTR_SUFFIX "faulted"
39 #define DT_DATA_ATTR_SUFFIX "data"
40 #define DT_THRESHOLD_ATTR_SUFFIX "thrs"
43 * Enumerates all the types of sensors in the POWERNV platform and does index
44 * into 'struct sensor_group'
54 #define INVALID_INDEX (-1U)
56 static struct sensor_group
{
58 const char *compatible
;
59 struct attribute_group group
;
63 {"fan", "ibm,opal-sensor-cooling-fan"},
64 {"temp", "ibm,opal-sensor-amb-temp"},
65 {"in", "ibm,opal-sensor-power-supply"},
66 {"power", "ibm,opal-sensor-power"}
70 u32 id
; /* An opaque id of the firmware for each sensor */
74 char label
[MAX_LABEL_LEN
];
75 char name
[MAX_ATTR_LEN
];
76 struct device_attribute dev_attr
;
79 struct platform_data
{
80 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
81 u32 sensors_count
; /* Total count of sensors from each group */
84 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
87 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
92 ret
= opal_get_sensor_data(sdata
->id
, &x
);
96 /* Convert temperature to milli-degrees */
97 if (sdata
->type
== TEMP
)
99 /* Convert power to micro-watts */
100 else if (sdata
->type
== POWER_INPUT
)
103 return sprintf(buf
, "%u\n", x
);
106 static ssize_t
show_label(struct device
*dev
, struct device_attribute
*devattr
,
109 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
112 return sprintf(buf
, "%s\n", sdata
->label
);
115 static void __init
make_sensor_label(struct device_node
*np
,
116 struct sensor_data
*sdata
,
121 n
= snprintf(sdata
->label
, sizeof(sdata
->label
), "%s", label
);
124 static int get_sensor_index_attr(const char *name
, u32
*index
, char *attr
)
126 char *hash_pos
= strchr(name
, '#');
135 dash_pos
= strchr(hash_pos
, '-');
139 copy_len
= dash_pos
- hash_pos
- 1;
140 if (copy_len
>= sizeof(buf
))
143 strncpy(buf
, hash_pos
+ 1, copy_len
);
145 err
= kstrtou32(buf
, 10, index
);
149 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
154 static const char *convert_opal_attr_name(enum sensors type
,
155 const char *opal_attr
)
157 const char *attr_name
= NULL
;
159 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
161 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
163 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
166 else if (type
== FAN
)
174 * This function translates the DT node name into the 'hwmon' attribute name.
175 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
176 * which need to be mapped as fan2_input, temp1_max respectively before
177 * populating them inside hwmon device class.
179 static const char *parse_opal_node_name(const char *node_name
,
180 enum sensors type
, u32
*index
)
182 char attr_suffix
[MAX_ATTR_LEN
];
183 const char *attr_name
;
186 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
190 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
192 return ERR_PTR(-ENOENT
);
197 static int get_sensor_type(struct device_node
*np
)
202 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
203 if (of_device_is_compatible(np
, sensor_groups
[type
].compatible
))
208 * Let's check if we have a newer device tree
210 if (!of_device_is_compatible(np
, "ibm,opal-sensor"))
211 return MAX_SENSOR_TYPE
;
213 if (of_property_read_string(np
, "sensor-type", &str
))
214 return MAX_SENSOR_TYPE
;
216 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
217 if (!strcmp(str
, sensor_groups
[type
].name
))
220 return MAX_SENSOR_TYPE
;
223 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
224 struct sensor_data
*sdata_table
, int count
)
229 * We don't use the OPAL index on newer device trees
231 if (sdata
->opal_index
!= INVALID_INDEX
) {
232 for (i
= 0; i
< count
; i
++)
233 if (sdata_table
[i
].opal_index
== sdata
->opal_index
&&
234 sdata_table
[i
].type
== sdata
->type
)
235 return sdata_table
[i
].hwmon_index
;
237 return ++sensor_groups
[sdata
->type
].hwmon_index
;
240 static int populate_attr_groups(struct platform_device
*pdev
)
242 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
243 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
244 struct device_node
*opal
, *np
;
247 opal
= of_find_node_by_path("/ibm,opal/sensors");
248 for_each_child_of_node(opal
, np
) {
251 if (np
->name
== NULL
)
254 type
= get_sensor_type(np
);
255 if (type
== MAX_SENSOR_TYPE
)
258 sensor_groups
[type
].attr_count
++;
261 * add a new attribute for labels
263 if (!of_property_read_string(np
, "label", &label
))
264 sensor_groups
[type
].attr_count
++;
269 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
270 sensor_groups
[type
].group
.attrs
= devm_kzalloc(&pdev
->dev
,
271 sizeof(struct attribute
*) *
272 (sensor_groups
[type
].attr_count
+ 1),
274 if (!sensor_groups
[type
].group
.attrs
)
277 pgroups
[type
] = &sensor_groups
[type
].group
;
278 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
279 sensor_groups
[type
].attr_count
= 0;
285 static void create_hwmon_attr(struct sensor_data
*sdata
, const char *attr_name
,
286 ssize_t (*show
)(struct device
*dev
,
287 struct device_attribute
*attr
,
290 snprintf(sdata
->name
, MAX_ATTR_LEN
, "%s%d_%s",
291 sensor_groups
[sdata
->type
].name
, sdata
->hwmon_index
,
294 sysfs_attr_init(&sdata
->dev_attr
.attr
);
295 sdata
->dev_attr
.attr
.name
= sdata
->name
;
296 sdata
->dev_attr
.attr
.mode
= S_IRUGO
;
297 sdata
->dev_attr
.show
= show
;
301 * Iterate through the device tree for each child of 'sensors' node, create
302 * a sysfs attribute file, the file is named by translating the DT node name
303 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
306 static int create_device_attrs(struct platform_device
*pdev
)
308 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
309 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
310 struct device_node
*opal
, *np
;
311 struct sensor_data
*sdata
;
317 opal
= of_find_node_by_path("/ibm,opal/sensors");
318 sdata
= devm_kzalloc(&pdev
->dev
, pdata
->sensors_count
* sizeof(*sdata
),
325 for_each_child_of_node(opal
, np
) {
326 const char *attr_name
;
330 if (np
->name
== NULL
)
333 type
= get_sensor_type(np
);
334 if (type
== MAX_SENSOR_TYPE
)
338 * Newer device trees use a "sensor-data" property
341 if (of_property_read_u32(np
, "sensor-id", &sensor_id
) &&
342 of_property_read_u32(np
, "sensor-data", &sensor_id
)) {
344 "'sensor-id' missing in the node '%s'\n",
349 sdata
[count
].id
= sensor_id
;
350 sdata
[count
].type
= type
;
353 * If we can not parse the node name, it means we are
354 * running on a newer device tree. We can just forget
355 * about the OPAL index and use a defaut value for the
356 * hwmon attribute name
358 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
359 if (IS_ERR(attr_name
)) {
361 opal_index
= INVALID_INDEX
;
364 sdata
[count
].opal_index
= opal_index
;
365 sdata
[count
].hwmon_index
=
366 get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
368 create_hwmon_attr(&sdata
[count
], attr_name
, show_sensor
);
370 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
371 &sdata
[count
++].dev_attr
.attr
;
373 if (!of_property_read_string(np
, "label", &label
)) {
375 * For the label attribute, we can reuse the
376 * "properties" of the previous "input"
377 * attribute. They are related to the same
380 sdata
[count
].type
= type
;
381 sdata
[count
].opal_index
= sdata
[count
- 1].opal_index
;
382 sdata
[count
].hwmon_index
= sdata
[count
- 1].hwmon_index
;
384 make_sensor_label(np
, &sdata
[count
], label
);
386 create_hwmon_attr(&sdata
[count
], "label", show_label
);
388 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
389 &sdata
[count
++].dev_attr
.attr
;
398 static int ibmpowernv_probe(struct platform_device
*pdev
)
400 struct platform_data
*pdata
;
401 struct device
*hwmon_dev
;
404 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
408 platform_set_drvdata(pdev
, pdata
);
409 pdata
->sensors_count
= 0;
410 err
= populate_attr_groups(pdev
);
414 /* Create sysfs attribute data for each sensor found in the DT */
415 err
= create_device_attrs(pdev
);
419 /* Finally, register with hwmon */
420 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
424 return PTR_ERR_OR_ZERO(hwmon_dev
);
427 static const struct platform_device_id opal_sensor_driver_ids
[] = {
429 .name
= "opal-sensor",
433 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
435 static struct platform_driver ibmpowernv_driver
= {
436 .probe
= ibmpowernv_probe
,
437 .id_table
= opal_sensor_driver_ids
,
443 module_platform_driver(ibmpowernv_driver
);
445 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
446 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
447 MODULE_LICENSE("GPL");