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
36 /* Sensor suffix name from DT */
37 #define DT_FAULT_ATTR_SUFFIX "faulted"
38 #define DT_DATA_ATTR_SUFFIX "data"
39 #define DT_THRESHOLD_ATTR_SUFFIX "thrs"
42 * Enumerates all the types of sensors in the POWERNV platform and does index
43 * into 'struct sensor_group'
53 static struct sensor_group
{
55 const char *compatible
;
56 struct attribute_group group
;
59 {"fan", "ibm,opal-sensor-cooling-fan"},
60 {"temp", "ibm,opal-sensor-amb-temp"},
61 {"in", "ibm,opal-sensor-power-supply"},
62 {"power", "ibm,opal-sensor-power"}
66 u32 id
; /* An opaque id of the firmware for each sensor */
68 char name
[MAX_ATTR_LEN
];
69 struct device_attribute dev_attr
;
72 struct platform_data
{
73 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
74 u32 sensors_count
; /* Total count of sensors from each group */
77 /* Platform device representing all the ibmpowernv sensors */
78 static struct platform_device
*pdevice
;
80 static ssize_t
show_sensor(struct device
*dev
, struct device_attribute
*devattr
,
83 struct sensor_data
*sdata
= container_of(devattr
, struct sensor_data
,
88 ret
= opal_get_sensor_data(sdata
->id
, &x
);
92 /* Convert temperature to milli-degrees */
93 if (sdata
->type
== AMBIENT_TEMP
)
95 /* Convert power to micro-watts */
96 else if (sdata
->type
== POWER_INPUT
)
99 return sprintf(buf
, "%u\n", x
);
102 static int __init
get_sensor_index_attr(const char *name
, u32
*index
,
105 char *hash_pos
= strchr(name
, '#');
114 dash_pos
= strchr(hash_pos
, '-');
118 copy_len
= dash_pos
- hash_pos
- 1;
119 if (copy_len
>= sizeof(buf
))
122 strncpy(buf
, hash_pos
+ 1, copy_len
);
124 err
= kstrtou32(buf
, 10, index
);
128 strncpy(attr
, dash_pos
+ 1, MAX_ATTR_LEN
);
134 * This function translates the DT node name into the 'hwmon' attribute name.
135 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
136 * which need to be mapped as fan2_input, temp1_max respectively before
137 * populating them inside hwmon device class.
139 static int __init
create_hwmon_attr_name(struct device
*dev
, enum sensors type
,
140 const char *node_name
,
141 char *hwmon_attr_name
)
143 char attr_suffix
[MAX_ATTR_LEN
];
148 err
= get_sensor_index_attr(node_name
, &index
, attr_suffix
);
150 dev_err(dev
, "Sensor device node name '%s' is invalid\n",
155 if (!strcmp(attr_suffix
, DT_FAULT_ATTR_SUFFIX
)) {
157 } else if (!strcmp(attr_suffix
, DT_DATA_ATTR_SUFFIX
)) {
159 } else if (!strcmp(attr_suffix
, DT_THRESHOLD_ATTR_SUFFIX
)) {
160 if (type
== AMBIENT_TEMP
)
162 else if (type
== FAN
)
170 snprintf(hwmon_attr_name
, MAX_ATTR_LEN
, "%s%d_%s",
171 sensor_groups
[type
].name
, index
, attr_name
);
175 static int __init
populate_attr_groups(struct platform_device
*pdev
)
177 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
178 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
179 struct device_node
*opal
, *np
;
182 opal
= of_find_node_by_path("/ibm,opal/sensors");
184 dev_err(&pdev
->dev
, "Opal node 'sensors' not found\n");
188 for_each_child_of_node(opal
, np
) {
189 if (np
->name
== NULL
)
192 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
193 if (of_device_is_compatible(np
,
194 sensor_groups
[type
].compatible
)) {
195 sensor_groups
[type
].attr_count
++;
202 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
203 sensor_groups
[type
].group
.attrs
= devm_kzalloc(&pdev
->dev
,
204 sizeof(struct attribute
*) *
205 (sensor_groups
[type
].attr_count
+ 1),
207 if (!sensor_groups
[type
].group
.attrs
)
210 pgroups
[type
] = &sensor_groups
[type
].group
;
211 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
212 sensor_groups
[type
].attr_count
= 0;
219 * Iterate through the device tree for each child of 'sensors' node, create
220 * a sysfs attribute file, the file is named by translating the DT node name
221 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
224 static int __init
create_device_attrs(struct platform_device
*pdev
)
226 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
227 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
228 struct device_node
*opal
, *np
;
229 struct sensor_data
*sdata
;
235 opal
= of_find_node_by_path("/ibm,opal/sensors");
236 sdata
= devm_kzalloc(&pdev
->dev
, pdata
->sensors_count
* sizeof(*sdata
),
243 for_each_child_of_node(opal
, np
) {
244 if (np
->name
== NULL
)
247 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++)
248 if (of_device_is_compatible(np
,
249 sensor_groups
[type
].compatible
))
252 if (type
== MAX_SENSOR_TYPE
)
255 if (of_property_read_u32(np
, "sensor-id", &sensor_id
)) {
257 "'sensor-id' missing in the node '%s'\n",
262 sdata
[count
].id
= sensor_id
;
263 sdata
[count
].type
= type
;
264 err
= create_hwmon_attr_name(&pdev
->dev
, type
, np
->name
,
269 sysfs_attr_init(&sdata
[count
].dev_attr
.attr
);
270 sdata
[count
].dev_attr
.attr
.name
= sdata
[count
].name
;
271 sdata
[count
].dev_attr
.attr
.mode
= S_IRUGO
;
272 sdata
[count
].dev_attr
.show
= show_sensor
;
274 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
275 &sdata
[count
++].dev_attr
.attr
;
283 static int __init
ibmpowernv_probe(struct platform_device
*pdev
)
285 struct platform_data
*pdata
;
286 struct device
*hwmon_dev
;
289 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
293 platform_set_drvdata(pdev
, pdata
);
294 pdata
->sensors_count
= 0;
295 err
= populate_attr_groups(pdev
);
299 /* Create sysfs attribute data for each sensor found in the DT */
300 err
= create_device_attrs(pdev
);
304 /* Finally, register with hwmon */
305 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
309 return PTR_ERR_OR_ZERO(hwmon_dev
);
312 static struct platform_driver ibmpowernv_driver
= {
314 .owner
= THIS_MODULE
,
319 static int __init
ibmpowernv_init(void)
323 pdevice
= platform_device_alloc(DRVNAME
, 0);
325 pr_err("Device allocation failed\n");
330 err
= platform_device_add(pdevice
);
332 pr_err("Device addition failed (%d)\n", err
);
333 goto exit_device_put
;
336 err
= platform_driver_probe(&ibmpowernv_driver
, ibmpowernv_probe
);
338 pr_err("Platfrom driver probe failed\n");
339 goto exit_device_del
;
345 platform_device_del(pdevice
);
347 platform_device_put(pdevice
);
352 static void __exit
ibmpowernv_exit(void)
354 platform_driver_unregister(&ibmpowernv_driver
);
355 platform_device_unregister(pdevice
);
358 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
359 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
360 MODULE_LICENSE("GPL");
362 module_init(ibmpowernv_init
);
363 module_exit(ibmpowernv_exit
);