hwmon: (ibmpowernv) add a label attribute
[deliverable/linux.git] / drivers / hwmon / ibmpowernv.c
1 /*
2 * IBM PowerNV platform sensors for temperature/fan/voltage/power
3 * Copyright (C) 2014 IBM
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 */
18
19 #define DRVNAME "ibmpowernv"
20 #define pr_fmt(fmt) DRVNAME ": " fmt
21
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>
27 #include <linux/of.h>
28 #include <linux/slab.h>
29
30 #include <linux/platform_device.h>
31 #include <asm/opal.h>
32 #include <linux/err.h>
33
34 #define MAX_ATTR_LEN 32
35 #define MAX_LABEL_LEN 64
36
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"
41
42 /*
43 * Enumerates all the types of sensors in the POWERNV platform and does index
44 * into 'struct sensor_group'
45 */
46 enum sensors {
47 FAN,
48 TEMP,
49 POWER_SUPPLY,
50 POWER_INPUT,
51 MAX_SENSOR_TYPE,
52 };
53
54 #define INVALID_INDEX (-1U)
55
56 static struct sensor_group {
57 const char *name;
58 const char *compatible;
59 struct attribute_group group;
60 u32 attr_count;
61 u32 hwmon_index;
62 } sensor_groups[] = {
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"}
67 };
68
69 struct sensor_data {
70 u32 id; /* An opaque id of the firmware for each sensor */
71 u32 hwmon_index;
72 u32 opal_index;
73 enum sensors type;
74 char label[MAX_LABEL_LEN];
75 char name[MAX_ATTR_LEN];
76 struct device_attribute dev_attr;
77 };
78
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 */
82 };
83
84 static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
85 char *buf)
86 {
87 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
88 dev_attr);
89 ssize_t ret;
90 u32 x;
91
92 ret = opal_get_sensor_data(sdata->id, &x);
93 if (ret)
94 return ret;
95
96 /* Convert temperature to milli-degrees */
97 if (sdata->type == TEMP)
98 x *= 1000;
99 /* Convert power to micro-watts */
100 else if (sdata->type == POWER_INPUT)
101 x *= 1000000;
102
103 return sprintf(buf, "%u\n", x);
104 }
105
106 static ssize_t show_label(struct device *dev, struct device_attribute *devattr,
107 char *buf)
108 {
109 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
110 dev_attr);
111
112 return sprintf(buf, "%s\n", sdata->label);
113 }
114
115 static void __init make_sensor_label(struct device_node *np,
116 struct sensor_data *sdata,
117 const char *label)
118 {
119 size_t n;
120
121 n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
122 }
123
124 static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
125 {
126 char *hash_pos = strchr(name, '#');
127 char buf[8] = { 0 };
128 char *dash_pos;
129 u32 copy_len;
130 int err;
131
132 if (!hash_pos)
133 return -EINVAL;
134
135 dash_pos = strchr(hash_pos, '-');
136 if (!dash_pos)
137 return -EINVAL;
138
139 copy_len = dash_pos - hash_pos - 1;
140 if (copy_len >= sizeof(buf))
141 return -EINVAL;
142
143 strncpy(buf, hash_pos + 1, copy_len);
144
145 err = kstrtou32(buf, 10, index);
146 if (err)
147 return err;
148
149 strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
150
151 return 0;
152 }
153
154 static const char *convert_opal_attr_name(enum sensors type,
155 const char *opal_attr)
156 {
157 const char *attr_name = NULL;
158
159 if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
160 attr_name = "fault";
161 } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
162 attr_name = "input";
163 } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
164 if (type == TEMP)
165 attr_name = "max";
166 else if (type == FAN)
167 attr_name = "min";
168 }
169
170 return attr_name;
171 }
172
173 /*
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.
178 */
179 static const char *parse_opal_node_name(const char *node_name,
180 enum sensors type, u32 *index)
181 {
182 char attr_suffix[MAX_ATTR_LEN];
183 const char *attr_name;
184 int err;
185
186 err = get_sensor_index_attr(node_name, index, attr_suffix);
187 if (err)
188 return ERR_PTR(err);
189
190 attr_name = convert_opal_attr_name(type, attr_suffix);
191 if (!attr_name)
192 return ERR_PTR(-ENOENT);
193
194 return attr_name;
195 }
196
197 static int get_sensor_type(struct device_node *np)
198 {
199 enum sensors type;
200 const char *str;
201
202 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
203 if (of_device_is_compatible(np, sensor_groups[type].compatible))
204 return type;
205 }
206
207 /*
208 * Let's check if we have a newer device tree
209 */
210 if (!of_device_is_compatible(np, "ibm,opal-sensor"))
211 return MAX_SENSOR_TYPE;
212
213 if (of_property_read_string(np, "sensor-type", &str))
214 return MAX_SENSOR_TYPE;
215
216 for (type = 0; type < MAX_SENSOR_TYPE; type++)
217 if (!strcmp(str, sensor_groups[type].name))
218 return type;
219
220 return MAX_SENSOR_TYPE;
221 }
222
223 static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
224 struct sensor_data *sdata_table, int count)
225 {
226 int i;
227
228 /*
229 * We don't use the OPAL index on newer device trees
230 */
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;
236 }
237 return ++sensor_groups[sdata->type].hwmon_index;
238 }
239
240 static int populate_attr_groups(struct platform_device *pdev)
241 {
242 struct platform_data *pdata = platform_get_drvdata(pdev);
243 const struct attribute_group **pgroups = pdata->attr_groups;
244 struct device_node *opal, *np;
245 enum sensors type;
246
247 opal = of_find_node_by_path("/ibm,opal/sensors");
248 for_each_child_of_node(opal, np) {
249 const char *label;
250
251 if (np->name == NULL)
252 continue;
253
254 type = get_sensor_type(np);
255 if (type == MAX_SENSOR_TYPE)
256 continue;
257
258 sensor_groups[type].attr_count++;
259
260 /*
261 * add a new attribute for labels
262 */
263 if (!of_property_read_string(np, "label", &label))
264 sensor_groups[type].attr_count++;
265 }
266
267 of_node_put(opal);
268
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),
273 GFP_KERNEL);
274 if (!sensor_groups[type].group.attrs)
275 return -ENOMEM;
276
277 pgroups[type] = &sensor_groups[type].group;
278 pdata->sensors_count += sensor_groups[type].attr_count;
279 sensor_groups[type].attr_count = 0;
280 }
281
282 return 0;
283 }
284
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,
288 char *buf))
289 {
290 snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
291 sensor_groups[sdata->type].name, sdata->hwmon_index,
292 attr_name);
293
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;
298 }
299
300 /*
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
304 * etc..
305 */
306 static int create_device_attrs(struct platform_device *pdev)
307 {
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;
312 u32 sensor_id;
313 enum sensors type;
314 u32 count = 0;
315 int err = 0;
316
317 opal = of_find_node_by_path("/ibm,opal/sensors");
318 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
319 GFP_KERNEL);
320 if (!sdata) {
321 err = -ENOMEM;
322 goto exit_put_node;
323 }
324
325 for_each_child_of_node(opal, np) {
326 const char *attr_name;
327 u32 opal_index;
328 const char *label;
329
330 if (np->name == NULL)
331 continue;
332
333 type = get_sensor_type(np);
334 if (type == MAX_SENSOR_TYPE)
335 continue;
336
337 /*
338 * Newer device trees use a "sensor-data" property
339 * name for input.
340 */
341 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
342 of_property_read_u32(np, "sensor-data", &sensor_id)) {
343 dev_info(&pdev->dev,
344 "'sensor-id' missing in the node '%s'\n",
345 np->name);
346 continue;
347 }
348
349 sdata[count].id = sensor_id;
350 sdata[count].type = type;
351
352 /*
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
357 */
358 attr_name = parse_opal_node_name(np->name, type, &opal_index);
359 if (IS_ERR(attr_name)) {
360 attr_name = "input";
361 opal_index = INVALID_INDEX;
362 }
363
364 sdata[count].opal_index = opal_index;
365 sdata[count].hwmon_index =
366 get_sensor_hwmon_index(&sdata[count], sdata, count);
367
368 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
369
370 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
371 &sdata[count++].dev_attr.attr;
372
373 if (!of_property_read_string(np, "label", &label)) {
374 /*
375 * For the label attribute, we can reuse the
376 * "properties" of the previous "input"
377 * attribute. They are related to the same
378 * sensor.
379 */
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;
383
384 make_sensor_label(np, &sdata[count], label);
385
386 create_hwmon_attr(&sdata[count], "label", show_label);
387
388 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
389 &sdata[count++].dev_attr.attr;
390 }
391 }
392
393 exit_put_node:
394 of_node_put(opal);
395 return err;
396 }
397
398 static int ibmpowernv_probe(struct platform_device *pdev)
399 {
400 struct platform_data *pdata;
401 struct device *hwmon_dev;
402 int err;
403
404 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
405 if (!pdata)
406 return -ENOMEM;
407
408 platform_set_drvdata(pdev, pdata);
409 pdata->sensors_count = 0;
410 err = populate_attr_groups(pdev);
411 if (err)
412 return err;
413
414 /* Create sysfs attribute data for each sensor found in the DT */
415 err = create_device_attrs(pdev);
416 if (err)
417 return err;
418
419 /* Finally, register with hwmon */
420 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
421 pdata,
422 pdata->attr_groups);
423
424 return PTR_ERR_OR_ZERO(hwmon_dev);
425 }
426
427 static const struct platform_device_id opal_sensor_driver_ids[] = {
428 {
429 .name = "opal-sensor",
430 },
431 { }
432 };
433 MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
434
435 static struct platform_driver ibmpowernv_driver = {
436 .probe = ibmpowernv_probe,
437 .id_table = opal_sensor_driver_ids,
438 .driver = {
439 .name = DRVNAME,
440 },
441 };
442
443 module_platform_driver(ibmpowernv_driver);
444
445 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
446 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
447 MODULE_LICENSE("GPL");
This page took 0.087294 seconds and 6 git commands to generate.