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
;
60 {"fan", "ibm,opal-sensor-cooling-fan"},
61 {"temp", "ibm,opal-sensor-amb-temp"},
62 {"in", "ibm,opal-sensor-power-supply"},
63 {"power", "ibm,opal-sensor-power"}
67 u32 id
; /* An opaque id of the firmware for each sensor */
71 char name
[MAX_ATTR_LEN
];
72 struct device_attribute dev_attr
;
75 struct platform_data
{
76 const struct attribute_group
*attr_groups
[MAX_SENSOR_TYPE
+ 1];
77 u32 sensors_count
; /* Total count of sensors from each group */
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
== TEMP
)
95 /* Convert power to micro-watts */
96 else if (sdata
->type
== POWER_INPUT
)
99 return sprintf(buf
, "%u\n", x
);
102 static int 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
);
133 static const char *convert_opal_attr_name(enum sensors type
,
134 const char *opal_attr
)
136 const char *attr_name
= NULL
;
138 if (!strcmp(opal_attr
, DT_FAULT_ATTR_SUFFIX
)) {
140 } else if (!strcmp(opal_attr
, DT_DATA_ATTR_SUFFIX
)) {
142 } else if (!strcmp(opal_attr
, DT_THRESHOLD_ATTR_SUFFIX
)) {
145 else if (type
== FAN
)
153 * This function translates the DT node name into the 'hwmon' attribute name.
154 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
155 * which need to be mapped as fan2_input, temp1_max respectively before
156 * populating them inside hwmon device class.
158 static const char *parse_opal_node_name(const char *node_name
,
159 enum sensors type
, u32
*index
)
161 char attr_suffix
[MAX_ATTR_LEN
];
162 const char *attr_name
;
165 err
= get_sensor_index_attr(node_name
, index
, attr_suffix
);
169 attr_name
= convert_opal_attr_name(type
, attr_suffix
);
171 return ERR_PTR(-ENOENT
);
176 static int get_sensor_type(struct device_node
*np
)
180 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
181 if (of_device_is_compatible(np
, sensor_groups
[type
].compatible
))
184 return MAX_SENSOR_TYPE
;
187 static u32
get_sensor_hwmon_index(struct sensor_data
*sdata
,
188 struct sensor_data
*sdata_table
, int count
)
192 for (i
= 0; i
< count
; i
++)
193 if (sdata_table
[i
].opal_index
== sdata
->opal_index
&&
194 sdata_table
[i
].type
== sdata
->type
)
195 return sdata_table
[i
].hwmon_index
;
197 return ++sensor_groups
[sdata
->type
].hwmon_index
;
200 static int populate_attr_groups(struct platform_device
*pdev
)
202 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
203 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
204 struct device_node
*opal
, *np
;
207 opal
= of_find_node_by_path("/ibm,opal/sensors");
208 for_each_child_of_node(opal
, np
) {
209 if (np
->name
== NULL
)
212 type
= get_sensor_type(np
);
213 if (type
!= MAX_SENSOR_TYPE
)
214 sensor_groups
[type
].attr_count
++;
219 for (type
= 0; type
< MAX_SENSOR_TYPE
; type
++) {
220 sensor_groups
[type
].group
.attrs
= devm_kzalloc(&pdev
->dev
,
221 sizeof(struct attribute
*) *
222 (sensor_groups
[type
].attr_count
+ 1),
224 if (!sensor_groups
[type
].group
.attrs
)
227 pgroups
[type
] = &sensor_groups
[type
].group
;
228 pdata
->sensors_count
+= sensor_groups
[type
].attr_count
;
229 sensor_groups
[type
].attr_count
= 0;
236 * Iterate through the device tree for each child of 'sensors' node, create
237 * a sysfs attribute file, the file is named by translating the DT node name
238 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
241 static int create_device_attrs(struct platform_device
*pdev
)
243 struct platform_data
*pdata
= platform_get_drvdata(pdev
);
244 const struct attribute_group
**pgroups
= pdata
->attr_groups
;
245 struct device_node
*opal
, *np
;
246 struct sensor_data
*sdata
;
252 opal
= of_find_node_by_path("/ibm,opal/sensors");
253 sdata
= devm_kzalloc(&pdev
->dev
, pdata
->sensors_count
* sizeof(*sdata
),
260 for_each_child_of_node(opal
, np
) {
261 const char *attr_name
;
264 if (np
->name
== NULL
)
267 type
= get_sensor_type(np
);
268 if (type
== MAX_SENSOR_TYPE
)
271 if (of_property_read_u32(np
, "sensor-id", &sensor_id
)) {
273 "'sensor-id' missing in the node '%s'\n",
278 sdata
[count
].id
= sensor_id
;
279 sdata
[count
].type
= type
;
281 attr_name
= parse_opal_node_name(np
->name
, type
, &opal_index
);
282 if (IS_ERR(attr_name
)) {
283 dev_err(&pdev
->dev
, "Sensor device node name '%s' is invalid\n",
285 err
= PTR_ERR(attr_name
);
289 sdata
[count
].opal_index
= opal_index
;
290 sdata
[count
].hwmon_index
=
291 get_sensor_hwmon_index(&sdata
[count
], sdata
, count
);
293 snprintf(sdata
[count
].name
, MAX_ATTR_LEN
, "%s%d_%s",
294 sensor_groups
[type
].name
, sdata
[count
].hwmon_index
,
297 sysfs_attr_init(&sdata
[count
].dev_attr
.attr
);
298 sdata
[count
].dev_attr
.attr
.name
= sdata
[count
].name
;
299 sdata
[count
].dev_attr
.attr
.mode
= S_IRUGO
;
300 sdata
[count
].dev_attr
.show
= show_sensor
;
302 pgroups
[type
]->attrs
[sensor_groups
[type
].attr_count
++] =
303 &sdata
[count
++].dev_attr
.attr
;
311 static int ibmpowernv_probe(struct platform_device
*pdev
)
313 struct platform_data
*pdata
;
314 struct device
*hwmon_dev
;
317 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
321 platform_set_drvdata(pdev
, pdata
);
322 pdata
->sensors_count
= 0;
323 err
= populate_attr_groups(pdev
);
327 /* Create sysfs attribute data for each sensor found in the DT */
328 err
= create_device_attrs(pdev
);
332 /* Finally, register with hwmon */
333 hwmon_dev
= devm_hwmon_device_register_with_groups(&pdev
->dev
, DRVNAME
,
337 return PTR_ERR_OR_ZERO(hwmon_dev
);
340 static const struct platform_device_id opal_sensor_driver_ids
[] = {
342 .name
= "opal-sensor",
346 MODULE_DEVICE_TABLE(platform
, opal_sensor_driver_ids
);
348 static struct platform_driver ibmpowernv_driver
= {
349 .probe
= ibmpowernv_probe
,
350 .id_table
= opal_sensor_driver_ids
,
356 module_platform_driver(ibmpowernv_driver
);
358 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
359 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
360 MODULE_LICENSE("GPL");