]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/hwmon/ibmpowernv.c
hwmon: (ibmpowernv) do not use the OPAL index for hwmon attribute names
[mirror_ubuntu-bionic-kernel.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
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 */
45 enum sensors {
46 FAN,
47 TEMP,
48 POWER_SUPPLY,
49 POWER_INPUT,
50 MAX_SENSOR_TYPE,
51 };
52
53 static struct sensor_group {
54 const char *name;
55 const char *compatible;
56 struct attribute_group group;
57 u32 attr_count;
58 u32 hwmon_index;
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
66 struct sensor_data {
67 u32 id; /* An opaque id of the firmware for each sensor */
68 u32 hwmon_index;
69 u32 opal_index;
70 enum sensors type;
71 char name[MAX_ATTR_LEN];
72 struct device_attribute dev_attr;
73 };
74
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 */
78 };
79
80 static 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 */
93 if (sdata->type == TEMP)
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
102 static int get_sensor_index_attr(const char *name, u32 *index,
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
133 static 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
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 */
158 static const char *parse_opal_node_name(const char *node_name,
159 enum sensors type, u32 *index)
160 {
161 char attr_suffix[MAX_ATTR_LEN];
162 const char *attr_name;
163 int err;
164
165 err = get_sensor_index_attr(node_name, index, attr_suffix);
166 if (err)
167 return ERR_PTR(err);
168
169 attr_name = convert_opal_attr_name(type, attr_suffix);
170 if (!attr_name)
171 return ERR_PTR(-ENOENT);
172
173 return attr_name;
174 }
175
176 static 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
187 static 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
200 static int populate_attr_groups(struct platform_device *pdev)
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");
208 for_each_child_of_node(opal, np) {
209 if (np->name == NULL)
210 continue;
211
212 type = get_sensor_type(np);
213 if (type != MAX_SENSOR_TYPE)
214 sensor_groups[type].attr_count++;
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
235 /*
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
239 * etc..
240 */
241 static int create_device_attrs(struct platform_device *pdev)
242 {
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;
247 u32 sensor_id;
248 enum sensors type;
249 u32 count = 0;
250 int err = 0;
251
252 opal = of_find_node_by_path("/ibm,opal/sensors");
253 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
254 GFP_KERNEL);
255 if (!sdata) {
256 err = -ENOMEM;
257 goto exit_put_node;
258 }
259
260 for_each_child_of_node(opal, np) {
261 const char *attr_name;
262 u32 opal_index;
263
264 if (np->name == NULL)
265 continue;
266
267 type = get_sensor_type(np);
268 if (type == MAX_SENSOR_TYPE)
269 continue;
270
271 if (of_property_read_u32(np, "sensor-id", &sensor_id)) {
272 dev_info(&pdev->dev,
273 "'sensor-id' missing in the node '%s'\n",
274 np->name);
275 continue;
276 }
277
278 sdata[count].id = sensor_id;
279 sdata[count].type = type;
280
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",
284 np->name);
285 err = PTR_ERR(attr_name);
286 goto exit_put_node;
287 }
288
289 sdata[count].opal_index = opal_index;
290 sdata[count].hwmon_index =
291 get_sensor_hwmon_index(&sdata[count], sdata, count);
292
293 snprintf(sdata[count].name, MAX_ATTR_LEN, "%s%d_%s",
294 sensor_groups[type].name, sdata[count].hwmon_index,
295 attr_name);
296
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;
301
302 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
303 &sdata[count++].dev_attr.attr;
304 }
305
306 exit_put_node:
307 of_node_put(opal);
308 return err;
309 }
310
311 static int ibmpowernv_probe(struct platform_device *pdev)
312 {
313 struct platform_data *pdata;
314 struct device *hwmon_dev;
315 int err;
316
317 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
318 if (!pdata)
319 return -ENOMEM;
320
321 platform_set_drvdata(pdev, pdata);
322 pdata->sensors_count = 0;
323 err = populate_attr_groups(pdev);
324 if (err)
325 return err;
326
327 /* Create sysfs attribute data for each sensor found in the DT */
328 err = create_device_attrs(pdev);
329 if (err)
330 return err;
331
332 /* Finally, register with hwmon */
333 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
334 pdata,
335 pdata->attr_groups);
336
337 return PTR_ERR_OR_ZERO(hwmon_dev);
338 }
339
340 static const struct platform_device_id opal_sensor_driver_ids[] = {
341 {
342 .name = "opal-sensor",
343 },
344 { }
345 };
346 MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
347
348 static struct platform_driver ibmpowernv_driver = {
349 .probe = ibmpowernv_probe,
350 .id_table = opal_sensor_driver_ids,
351 .driver = {
352 .name = DRVNAME,
353 },
354 };
355
356 module_platform_driver(ibmpowernv_driver);
357
358 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
359 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
360 MODULE_LICENSE("GPL");