hwmon: (ibmpowernv) add a helper routine create_hwmon_attr
[linux-2.6-block.git] / drivers / hwmon / ibmpowernv.c
CommitLineData
24c1aa85
NG
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
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"
40
41/*
42 * Enumerates all the types of sensors in the POWERNV platform and does index
43 * into 'struct sensor_group'
44 */
45enum sensors {
46 FAN,
96124610 47 TEMP,
24c1aa85
NG
48 POWER_SUPPLY,
49 POWER_INPUT,
50 MAX_SENSOR_TYPE,
51};
52
53static struct sensor_group {
54 const char *name;
55 const char *compatible;
56 struct attribute_group group;
57 u32 attr_count;
fcaf57b6 58 u32 hwmon_index;
24c1aa85
NG
59} sensor_groups[] = {
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"}
64};
65
66struct sensor_data {
67 u32 id; /* An opaque id of the firmware for each sensor */
fcaf57b6
CLG
68 u32 hwmon_index;
69 u32 opal_index;
24c1aa85
NG
70 enum sensors type;
71 char name[MAX_ATTR_LEN];
72 struct device_attribute dev_attr;
73};
74
75struct platform_data {
76 const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
77 u32 sensors_count; /* Total count of sensors from each group */
78};
79
24c1aa85
NG
80static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
81 char *buf)
82{
83 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
84 dev_attr);
85 ssize_t ret;
86 u32 x;
87
88 ret = opal_get_sensor_data(sdata->id, &x);
89 if (ret)
90 return ret;
91
92 /* Convert temperature to milli-degrees */
96124610 93 if (sdata->type == TEMP)
24c1aa85
NG
94 x *= 1000;
95 /* Convert power to micro-watts */
96 else if (sdata->type == POWER_INPUT)
97 x *= 1000000;
98
99 return sprintf(buf, "%u\n", x);
100}
101
8de303ba 102static int get_sensor_index_attr(const char *name, u32 *index,
24c1aa85
NG
103 char *attr)
104{
105 char *hash_pos = strchr(name, '#');
106 char buf[8] = { 0 };
107 char *dash_pos;
108 u32 copy_len;
109 int err;
110
111 if (!hash_pos)
112 return -EINVAL;
113
114 dash_pos = strchr(hash_pos, '-');
115 if (!dash_pos)
116 return -EINVAL;
117
118 copy_len = dash_pos - hash_pos - 1;
119 if (copy_len >= sizeof(buf))
120 return -EINVAL;
121
122 strncpy(buf, hash_pos + 1, copy_len);
123
124 err = kstrtou32(buf, 10, index);
125 if (err)
126 return err;
127
128 strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
129
130 return 0;
131}
132
ccc9ac6c
CLG
133static const char *convert_opal_attr_name(enum sensors type,
134 const char *opal_attr)
135{
136 const char *attr_name = NULL;
137
138 if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
139 attr_name = "fault";
140 } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
141 attr_name = "input";
142 } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
143 if (type == TEMP)
144 attr_name = "max";
145 else if (type == FAN)
146 attr_name = "min";
147 }
148
149 return attr_name;
150}
151
24c1aa85
NG
152/*
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.
157 */
f9f54f16
CLG
158static const char *parse_opal_node_name(const char *node_name,
159 enum sensors type, u32 *index)
24c1aa85
NG
160{
161 char attr_suffix[MAX_ATTR_LEN];
ccc9ac6c 162 const char *attr_name;
24c1aa85
NG
163 int err;
164
f9f54f16
CLG
165 err = get_sensor_index_attr(node_name, index, attr_suffix);
166 if (err)
167 return ERR_PTR(err);
24c1aa85 168
ccc9ac6c
CLG
169 attr_name = convert_opal_attr_name(type, attr_suffix);
170 if (!attr_name)
f9f54f16 171 return ERR_PTR(-ENOENT);
24c1aa85 172
f9f54f16 173 return attr_name;
24c1aa85
NG
174}
175
c4ad4720
CLG
176static int get_sensor_type(struct device_node *np)
177{
178 enum sensors type;
179
180 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
181 if (of_device_is_compatible(np, sensor_groups[type].compatible))
182 return type;
183 }
184 return MAX_SENSOR_TYPE;
185}
186
fcaf57b6
CLG
187static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
188 struct sensor_data *sdata_table, int count)
189{
190 int i;
191
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;
196
197 return ++sensor_groups[sdata->type].hwmon_index;
198}
199
8de303ba 200static int populate_attr_groups(struct platform_device *pdev)
24c1aa85
NG
201{
202 struct platform_data *pdata = platform_get_drvdata(pdev);
203 const struct attribute_group **pgroups = pdata->attr_groups;
204 struct device_node *opal, *np;
205 enum sensors type;
206
207 opal = of_find_node_by_path("/ibm,opal/sensors");
24c1aa85
NG
208 for_each_child_of_node(opal, np) {
209 if (np->name == NULL)
210 continue;
211
c4ad4720
CLG
212 type = get_sensor_type(np);
213 if (type != MAX_SENSOR_TYPE)
214 sensor_groups[type].attr_count++;
24c1aa85
NG
215 }
216
217 of_node_put(opal);
218
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),
223 GFP_KERNEL);
224 if (!sensor_groups[type].group.attrs)
225 return -ENOMEM;
226
227 pgroups[type] = &sensor_groups[type].group;
228 pdata->sensors_count += sensor_groups[type].attr_count;
229 sensor_groups[type].attr_count = 0;
230 }
231
232 return 0;
233}
234
9e4f74b1
CLG
235static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
236 ssize_t (*show)(struct device *dev,
237 struct device_attribute *attr,
238 char *buf))
239{
240 snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
241 sensor_groups[sdata->type].name, sdata->hwmon_index,
242 attr_name);
243
244 sysfs_attr_init(&sdata->dev_attr.attr);
245 sdata->dev_attr.attr.name = sdata->name;
246 sdata->dev_attr.attr.mode = S_IRUGO;
247 sdata->dev_attr.show = show;
248}
249
24c1aa85
NG
250/*
251 * Iterate through the device tree for each child of 'sensors' node, create
252 * a sysfs attribute file, the file is named by translating the DT node name
253 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
254 * etc..
255 */
8de303ba 256static int create_device_attrs(struct platform_device *pdev)
24c1aa85
NG
257{
258 struct platform_data *pdata = platform_get_drvdata(pdev);
259 const struct attribute_group **pgroups = pdata->attr_groups;
260 struct device_node *opal, *np;
261 struct sensor_data *sdata;
18d03f3c 262 u32 sensor_id;
24c1aa85
NG
263 enum sensors type;
264 u32 count = 0;
265 int err = 0;
266
267 opal = of_find_node_by_path("/ibm,opal/sensors");
268 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
269 GFP_KERNEL);
270 if (!sdata) {
271 err = -ENOMEM;
272 goto exit_put_node;
273 }
274
275 for_each_child_of_node(opal, np) {
f9f54f16
CLG
276 const char *attr_name;
277 u32 opal_index;
278
24c1aa85
NG
279 if (np->name == NULL)
280 continue;
281
c4ad4720 282 type = get_sensor_type(np);
24c1aa85
NG
283 if (type == MAX_SENSOR_TYPE)
284 continue;
285
18d03f3c 286 if (of_property_read_u32(np, "sensor-id", &sensor_id)) {
24c1aa85
NG
287 dev_info(&pdev->dev,
288 "'sensor-id' missing in the node '%s'\n",
289 np->name);
290 continue;
291 }
292
18d03f3c 293 sdata[count].id = sensor_id;
24c1aa85 294 sdata[count].type = type;
f9f54f16
CLG
295
296 attr_name = parse_opal_node_name(np->name, type, &opal_index);
297 if (IS_ERR(attr_name)) {
298 dev_err(&pdev->dev, "Sensor device node name '%s' is invalid\n",
299 np->name);
300 err = PTR_ERR(attr_name);
24c1aa85 301 goto exit_put_node;
f9f54f16
CLG
302 }
303
fcaf57b6
CLG
304 sdata[count].opal_index = opal_index;
305 sdata[count].hwmon_index =
306 get_sensor_hwmon_index(&sdata[count], sdata, count);
307
9e4f74b1 308 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
24c1aa85
NG
309
310 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
311 &sdata[count++].dev_attr.attr;
312 }
313
314exit_put_node:
315 of_node_put(opal);
316 return err;
317}
318
8de303ba 319static int ibmpowernv_probe(struct platform_device *pdev)
24c1aa85
NG
320{
321 struct platform_data *pdata;
322 struct device *hwmon_dev;
323 int err;
324
325 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
326 if (!pdata)
327 return -ENOMEM;
328
329 platform_set_drvdata(pdev, pdata);
330 pdata->sensors_count = 0;
331 err = populate_attr_groups(pdev);
332 if (err)
333 return err;
334
335 /* Create sysfs attribute data for each sensor found in the DT */
336 err = create_device_attrs(pdev);
337 if (err)
338 return err;
339
340 /* Finally, register with hwmon */
341 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
342 pdata,
343 pdata->attr_groups);
344
345 return PTR_ERR_OR_ZERO(hwmon_dev);
346}
347
8de303ba
NG
348static const struct platform_device_id opal_sensor_driver_ids[] = {
349 {
350 .name = "opal-sensor",
351 },
352 { }
353};
354MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
355
24c1aa85 356static struct platform_driver ibmpowernv_driver = {
8de303ba
NG
357 .probe = ibmpowernv_probe,
358 .id_table = opal_sensor_driver_ids,
359 .driver = {
8de303ba 360 .name = DRVNAME,
24c1aa85
NG
361 },
362};
363
3bdec670 364module_platform_driver(ibmpowernv_driver);
24c1aa85
NG
365
366MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
367MODULE_DESCRIPTION("IBM POWERNV platform sensors");
368MODULE_LICENSE("GPL");