]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/ibmpowernv.c
hwmon: (ibmpowernv) add support for the new device tree
[mirror_ubuntu-bionic-kernel.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
14681637
CLG
53#define INVALID_INDEX (-1U)
54
24c1aa85
NG
55static struct sensor_group {
56 const char *name;
57 const char *compatible;
58 struct attribute_group group;
59 u32 attr_count;
fcaf57b6 60 u32 hwmon_index;
24c1aa85
NG
61} sensor_groups[] = {
62 {"fan", "ibm,opal-sensor-cooling-fan"},
63 {"temp", "ibm,opal-sensor-amb-temp"},
64 {"in", "ibm,opal-sensor-power-supply"},
65 {"power", "ibm,opal-sensor-power"}
66};
67
68struct sensor_data {
69 u32 id; /* An opaque id of the firmware for each sensor */
fcaf57b6
CLG
70 u32 hwmon_index;
71 u32 opal_index;
24c1aa85
NG
72 enum sensors type;
73 char name[MAX_ATTR_LEN];
74 struct device_attribute dev_attr;
75};
76
77struct platform_data {
78 const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
79 u32 sensors_count; /* Total count of sensors from each group */
80};
81
24c1aa85
NG
82static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
83 char *buf)
84{
85 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
86 dev_attr);
87 ssize_t ret;
88 u32 x;
89
90 ret = opal_get_sensor_data(sdata->id, &x);
91 if (ret)
92 return ret;
93
94 /* Convert temperature to milli-degrees */
96124610 95 if (sdata->type == TEMP)
24c1aa85
NG
96 x *= 1000;
97 /* Convert power to micro-watts */
98 else if (sdata->type == POWER_INPUT)
99 x *= 1000000;
100
101 return sprintf(buf, "%u\n", x);
102}
103
8de303ba 104static int get_sensor_index_attr(const char *name, u32 *index,
24c1aa85
NG
105 char *attr)
106{
107 char *hash_pos = strchr(name, '#');
108 char buf[8] = { 0 };
109 char *dash_pos;
110 u32 copy_len;
111 int err;
112
113 if (!hash_pos)
114 return -EINVAL;
115
116 dash_pos = strchr(hash_pos, '-');
117 if (!dash_pos)
118 return -EINVAL;
119
120 copy_len = dash_pos - hash_pos - 1;
121 if (copy_len >= sizeof(buf))
122 return -EINVAL;
123
124 strncpy(buf, hash_pos + 1, copy_len);
125
126 err = kstrtou32(buf, 10, index);
127 if (err)
128 return err;
129
130 strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
131
132 return 0;
133}
134
ccc9ac6c
CLG
135static const char *convert_opal_attr_name(enum sensors type,
136 const char *opal_attr)
137{
138 const char *attr_name = NULL;
139
140 if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
141 attr_name = "fault";
142 } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
143 attr_name = "input";
144 } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
145 if (type == TEMP)
146 attr_name = "max";
147 else if (type == FAN)
148 attr_name = "min";
149 }
150
151 return attr_name;
152}
153
24c1aa85
NG
154/*
155 * This function translates the DT node name into the 'hwmon' attribute name.
156 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
157 * which need to be mapped as fan2_input, temp1_max respectively before
158 * populating them inside hwmon device class.
159 */
f9f54f16
CLG
160static const char *parse_opal_node_name(const char *node_name,
161 enum sensors type, u32 *index)
24c1aa85
NG
162{
163 char attr_suffix[MAX_ATTR_LEN];
ccc9ac6c 164 const char *attr_name;
24c1aa85
NG
165 int err;
166
f9f54f16
CLG
167 err = get_sensor_index_attr(node_name, index, attr_suffix);
168 if (err)
169 return ERR_PTR(err);
24c1aa85 170
ccc9ac6c
CLG
171 attr_name = convert_opal_attr_name(type, attr_suffix);
172 if (!attr_name)
f9f54f16 173 return ERR_PTR(-ENOENT);
24c1aa85 174
f9f54f16 175 return attr_name;
24c1aa85
NG
176}
177
c4ad4720
CLG
178static int get_sensor_type(struct device_node *np)
179{
180 enum sensors type;
14681637 181 const char *str;
c4ad4720
CLG
182
183 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
184 if (of_device_is_compatible(np, sensor_groups[type].compatible))
185 return type;
186 }
14681637
CLG
187
188 /*
189 * Let's check if we have a newer device tree
190 */
191 if (!of_device_is_compatible(np, "ibm,opal-sensor"))
192 return MAX_SENSOR_TYPE;
193
194 if (of_property_read_string(np, "sensor-type", &str))
195 return MAX_SENSOR_TYPE;
196
197 for (type = 0; type < MAX_SENSOR_TYPE; type++)
198 if (!strcmp(str, sensor_groups[type].name))
199 return type;
200
c4ad4720
CLG
201 return MAX_SENSOR_TYPE;
202}
203
fcaf57b6
CLG
204static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
205 struct sensor_data *sdata_table, int count)
206{
207 int i;
208
14681637
CLG
209 /*
210 * We don't use the OPAL index on newer device trees
211 */
212 if (sdata->opal_index != INVALID_INDEX) {
213 for (i = 0; i < count; i++)
214 if (sdata_table[i].opal_index == sdata->opal_index &&
215 sdata_table[i].type == sdata->type)
216 return sdata_table[i].hwmon_index;
217 }
fcaf57b6
CLG
218 return ++sensor_groups[sdata->type].hwmon_index;
219}
220
8de303ba 221static int populate_attr_groups(struct platform_device *pdev)
24c1aa85
NG
222{
223 struct platform_data *pdata = platform_get_drvdata(pdev);
224 const struct attribute_group **pgroups = pdata->attr_groups;
225 struct device_node *opal, *np;
226 enum sensors type;
227
228 opal = of_find_node_by_path("/ibm,opal/sensors");
24c1aa85
NG
229 for_each_child_of_node(opal, np) {
230 if (np->name == NULL)
231 continue;
232
c4ad4720
CLG
233 type = get_sensor_type(np);
234 if (type != MAX_SENSOR_TYPE)
235 sensor_groups[type].attr_count++;
24c1aa85
NG
236 }
237
238 of_node_put(opal);
239
240 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
241 sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
242 sizeof(struct attribute *) *
243 (sensor_groups[type].attr_count + 1),
244 GFP_KERNEL);
245 if (!sensor_groups[type].group.attrs)
246 return -ENOMEM;
247
248 pgroups[type] = &sensor_groups[type].group;
249 pdata->sensors_count += sensor_groups[type].attr_count;
250 sensor_groups[type].attr_count = 0;
251 }
252
253 return 0;
254}
255
9e4f74b1
CLG
256static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
257 ssize_t (*show)(struct device *dev,
258 struct device_attribute *attr,
259 char *buf))
260{
261 snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
262 sensor_groups[sdata->type].name, sdata->hwmon_index,
263 attr_name);
264
265 sysfs_attr_init(&sdata->dev_attr.attr);
266 sdata->dev_attr.attr.name = sdata->name;
267 sdata->dev_attr.attr.mode = S_IRUGO;
268 sdata->dev_attr.show = show;
269}
270
24c1aa85
NG
271/*
272 * Iterate through the device tree for each child of 'sensors' node, create
273 * a sysfs attribute file, the file is named by translating the DT node name
274 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
275 * etc..
276 */
8de303ba 277static int create_device_attrs(struct platform_device *pdev)
24c1aa85
NG
278{
279 struct platform_data *pdata = platform_get_drvdata(pdev);
280 const struct attribute_group **pgroups = pdata->attr_groups;
281 struct device_node *opal, *np;
282 struct sensor_data *sdata;
18d03f3c 283 u32 sensor_id;
24c1aa85
NG
284 enum sensors type;
285 u32 count = 0;
286 int err = 0;
287
288 opal = of_find_node_by_path("/ibm,opal/sensors");
289 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
290 GFP_KERNEL);
291 if (!sdata) {
292 err = -ENOMEM;
293 goto exit_put_node;
294 }
295
296 for_each_child_of_node(opal, np) {
f9f54f16
CLG
297 const char *attr_name;
298 u32 opal_index;
299
24c1aa85
NG
300 if (np->name == NULL)
301 continue;
302
c4ad4720 303 type = get_sensor_type(np);
24c1aa85
NG
304 if (type == MAX_SENSOR_TYPE)
305 continue;
306
14681637
CLG
307 /*
308 * Newer device trees use a "sensor-data" property
309 * name for input.
310 */
311 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
312 of_property_read_u32(np, "sensor-data", &sensor_id)) {
24c1aa85
NG
313 dev_info(&pdev->dev,
314 "'sensor-id' missing in the node '%s'\n",
315 np->name);
316 continue;
317 }
318
18d03f3c 319 sdata[count].id = sensor_id;
24c1aa85 320 sdata[count].type = type;
f9f54f16 321
14681637
CLG
322 /*
323 * If we can not parse the node name, it means we are
324 * running on a newer device tree. We can just forget
325 * about the OPAL index and use a defaut value for the
326 * hwmon attribute name
327 */
f9f54f16
CLG
328 attr_name = parse_opal_node_name(np->name, type, &opal_index);
329 if (IS_ERR(attr_name)) {
14681637
CLG
330 attr_name = "input";
331 opal_index = INVALID_INDEX;
f9f54f16
CLG
332 }
333
fcaf57b6
CLG
334 sdata[count].opal_index = opal_index;
335 sdata[count].hwmon_index =
336 get_sensor_hwmon_index(&sdata[count], sdata, count);
337
9e4f74b1 338 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
24c1aa85
NG
339
340 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
341 &sdata[count++].dev_attr.attr;
342 }
343
344exit_put_node:
345 of_node_put(opal);
346 return err;
347}
348
8de303ba 349static int ibmpowernv_probe(struct platform_device *pdev)
24c1aa85
NG
350{
351 struct platform_data *pdata;
352 struct device *hwmon_dev;
353 int err;
354
355 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
356 if (!pdata)
357 return -ENOMEM;
358
359 platform_set_drvdata(pdev, pdata);
360 pdata->sensors_count = 0;
361 err = populate_attr_groups(pdev);
362 if (err)
363 return err;
364
365 /* Create sysfs attribute data for each sensor found in the DT */
366 err = create_device_attrs(pdev);
367 if (err)
368 return err;
369
370 /* Finally, register with hwmon */
371 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
372 pdata,
373 pdata->attr_groups);
374
375 return PTR_ERR_OR_ZERO(hwmon_dev);
376}
377
8de303ba
NG
378static const struct platform_device_id opal_sensor_driver_ids[] = {
379 {
380 .name = "opal-sensor",
381 },
382 { }
383};
384MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
385
24c1aa85 386static struct platform_driver ibmpowernv_driver = {
8de303ba
NG
387 .probe = ibmpowernv_probe,
388 .id_table = opal_sensor_driver_ids,
389 .driver = {
8de303ba 390 .name = DRVNAME,
24c1aa85
NG
391 },
392};
393
3bdec670 394module_platform_driver(ibmpowernv_driver);
24c1aa85
NG
395
396MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
397MODULE_DESCRIPTION("IBM POWERNV platform sensors");
398MODULE_LICENSE("GPL");