blob: 18b3c8f258bf16d5b141ed843c5691348355a16c [file] [log] [blame]
Neelesh Gupta24c1aa82014-07-08 14:38:38 +05301/*
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>
Cédric Le Goater3df2f592015-04-08 19:19:50 +020033#include <asm/cputhreads.h>
Guenter Roeck84169152015-04-08 21:02:47 -070034#include <asm/smp.h>
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053035
36#define MAX_ATTR_LEN 32
Cédric Le Goater2bcd3782015-04-08 19:19:49 +020037#define MAX_LABEL_LEN 64
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053038
39/* Sensor suffix name from DT */
40#define DT_FAULT_ATTR_SUFFIX "faulted"
41#define DT_DATA_ATTR_SUFFIX "data"
42#define DT_THRESHOLD_ATTR_SUFFIX "thrs"
43
44/*
45 * Enumerates all the types of sensors in the POWERNV platform and does index
46 * into 'struct sensor_group'
47 */
48enum sensors {
49 FAN,
Cédric Le Goater96124612015-03-19 18:44:41 +010050 TEMP,
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053051 POWER_SUPPLY,
52 POWER_INPUT,
53 MAX_SENSOR_TYPE,
54};
55
Cédric Le Goater14681632015-04-08 19:19:48 +020056#define INVALID_INDEX (-1U)
57
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053058static struct sensor_group {
59 const char *name;
60 const char *compatible;
61 struct attribute_group group;
62 u32 attr_count;
Cédric Le Goaterfcaf57b2015-03-19 18:44:45 +010063 u32 hwmon_index;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053064} sensor_groups[] = {
65 {"fan", "ibm,opal-sensor-cooling-fan"},
66 {"temp", "ibm,opal-sensor-amb-temp"},
67 {"in", "ibm,opal-sensor-power-supply"},
68 {"power", "ibm,opal-sensor-power"}
69};
70
71struct sensor_data {
72 u32 id; /* An opaque id of the firmware for each sensor */
Cédric Le Goaterfcaf57b2015-03-19 18:44:45 +010073 u32 hwmon_index;
74 u32 opal_index;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053075 enum sensors type;
Cédric Le Goater2bcd3782015-04-08 19:19:49 +020076 char label[MAX_LABEL_LEN];
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053077 char name[MAX_ATTR_LEN];
78 struct device_attribute dev_attr;
79};
80
81struct platform_data {
82 const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
83 u32 sensors_count; /* Total count of sensors from each group */
84};
85
Neelesh Gupta24c1aa82014-07-08 14:38:38 +053086static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
87 char *buf)
88{
89 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
90 dev_attr);
91 ssize_t ret;
92 u32 x;
93
94 ret = opal_get_sensor_data(sdata->id, &x);
95 if (ret)
96 return ret;
97
98 /* Convert temperature to milli-degrees */
Cédric Le Goater96124612015-03-19 18:44:41 +010099 if (sdata->type == TEMP)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530100 x *= 1000;
101 /* Convert power to micro-watts */
102 else if (sdata->type == POWER_INPUT)
103 x *= 1000000;
104
105 return sprintf(buf, "%u\n", x);
106}
107
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200108static ssize_t show_label(struct device *dev, struct device_attribute *devattr,
109 char *buf)
110{
111 struct sensor_data *sdata = container_of(devattr, struct sensor_data,
112 dev_attr);
113
114 return sprintf(buf, "%s\n", sdata->label);
115}
116
Geert Uytterhoevend75d4fd2018-10-28 18:16:51 +0100117static int get_logical_cpu(int hwcpu)
Cédric Le Goater3df2f592015-04-08 19:19:50 +0200118{
119 int cpu;
120
121 for_each_possible_cpu(cpu)
122 if (get_hard_smp_processor_id(cpu) == hwcpu)
123 return cpu;
124
125 return -ENOENT;
126}
127
Geert Uytterhoevend75d4fd2018-10-28 18:16:51 +0100128static void make_sensor_label(struct device_node *np,
129 struct sensor_data *sdata, const char *label)
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200130{
Cédric Le Goater3df2f592015-04-08 19:19:50 +0200131 u32 id;
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200132 size_t n;
133
134 n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
Cédric Le Goater3df2f592015-04-08 19:19:50 +0200135
136 /*
137 * Core temp pretty print
138 */
139 if (!of_property_read_u32(np, "ibm,pir", &id)) {
140 int cpuid = get_logical_cpu(id);
141
142 if (cpuid >= 0)
143 /*
144 * The digital thermal sensors are associated
Michael Neulingacf32962016-09-13 15:47:41 +1000145 * with a core.
Cédric Le Goater3df2f592015-04-08 19:19:50 +0200146 */
147 n += snprintf(sdata->label + n,
Michael Neulingacf32962016-09-13 15:47:41 +1000148 sizeof(sdata->label) - n, " %d",
149 cpuid);
Cédric Le Goater3df2f592015-04-08 19:19:50 +0200150 else
151 n += snprintf(sdata->label + n,
152 sizeof(sdata->label) - n, " phy%d", id);
153 }
154
155 /*
156 * Membuffer pretty print
157 */
158 if (!of_property_read_u32(np, "ibm,chip-id", &id))
159 n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
160 " %d", id & 0xffff);
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200161}
162
163static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530164{
165 char *hash_pos = strchr(name, '#');
166 char buf[8] = { 0 };
167 char *dash_pos;
168 u32 copy_len;
169 int err;
170
171 if (!hash_pos)
172 return -EINVAL;
173
174 dash_pos = strchr(hash_pos, '-');
175 if (!dash_pos)
176 return -EINVAL;
177
178 copy_len = dash_pos - hash_pos - 1;
179 if (copy_len >= sizeof(buf))
180 return -EINVAL;
181
182 strncpy(buf, hash_pos + 1, copy_len);
183
184 err = kstrtou32(buf, 10, index);
185 if (err)
186 return err;
187
188 strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
189
190 return 0;
191}
192
Cédric Le Goaterccc9ac62015-03-19 18:44:43 +0100193static const char *convert_opal_attr_name(enum sensors type,
194 const char *opal_attr)
195{
196 const char *attr_name = NULL;
197
198 if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
199 attr_name = "fault";
200 } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
201 attr_name = "input";
202 } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
203 if (type == TEMP)
204 attr_name = "max";
205 else if (type == FAN)
206 attr_name = "min";
207 }
208
209 return attr_name;
210}
211
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530212/*
213 * This function translates the DT node name into the 'hwmon' attribute name.
214 * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
215 * which need to be mapped as fan2_input, temp1_max respectively before
216 * populating them inside hwmon device class.
217 */
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100218static const char *parse_opal_node_name(const char *node_name,
219 enum sensors type, u32 *index)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530220{
221 char attr_suffix[MAX_ATTR_LEN];
Cédric Le Goaterccc9ac62015-03-19 18:44:43 +0100222 const char *attr_name;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530223 int err;
224
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100225 err = get_sensor_index_attr(node_name, index, attr_suffix);
226 if (err)
227 return ERR_PTR(err);
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530228
Cédric Le Goaterccc9ac62015-03-19 18:44:43 +0100229 attr_name = convert_opal_attr_name(type, attr_suffix);
230 if (!attr_name)
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100231 return ERR_PTR(-ENOENT);
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530232
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100233 return attr_name;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530234}
235
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100236static int get_sensor_type(struct device_node *np)
237{
238 enum sensors type;
Cédric Le Goater14681632015-04-08 19:19:48 +0200239 const char *str;
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100240
241 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
242 if (of_device_is_compatible(np, sensor_groups[type].compatible))
243 return type;
244 }
Cédric Le Goater14681632015-04-08 19:19:48 +0200245
246 /*
247 * Let's check if we have a newer device tree
248 */
249 if (!of_device_is_compatible(np, "ibm,opal-sensor"))
250 return MAX_SENSOR_TYPE;
251
252 if (of_property_read_string(np, "sensor-type", &str))
253 return MAX_SENSOR_TYPE;
254
255 for (type = 0; type < MAX_SENSOR_TYPE; type++)
256 if (!strcmp(str, sensor_groups[type].name))
257 return type;
258
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100259 return MAX_SENSOR_TYPE;
260}
261
Cédric Le Goaterfcaf57b2015-03-19 18:44:45 +0100262static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
263 struct sensor_data *sdata_table, int count)
264{
265 int i;
266
Cédric Le Goater14681632015-04-08 19:19:48 +0200267 /*
268 * We don't use the OPAL index on newer device trees
269 */
270 if (sdata->opal_index != INVALID_INDEX) {
271 for (i = 0; i < count; i++)
272 if (sdata_table[i].opal_index == sdata->opal_index &&
273 sdata_table[i].type == sdata->type)
274 return sdata_table[i].hwmon_index;
275 }
Cédric Le Goaterfcaf57b2015-03-19 18:44:45 +0100276 return ++sensor_groups[sdata->type].hwmon_index;
277}
278
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530279static int populate_attr_groups(struct platform_device *pdev)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530280{
281 struct platform_data *pdata = platform_get_drvdata(pdev);
282 const struct attribute_group **pgroups = pdata->attr_groups;
283 struct device_node *opal, *np;
284 enum sensors type;
285
286 opal = of_find_node_by_path("/ibm,opal/sensors");
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530287 for_each_child_of_node(opal, np) {
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200288 const char *label;
289
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530290 if (np->name == NULL)
291 continue;
292
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100293 type = get_sensor_type(np);
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200294 if (type == MAX_SENSOR_TYPE)
295 continue;
296
297 sensor_groups[type].attr_count++;
298
299 /*
300 * add a new attribute for labels
301 */
302 if (!of_property_read_string(np, "label", &label))
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100303 sensor_groups[type].attr_count++;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530304 }
305
306 of_node_put(opal);
307
308 for (type = 0; type < MAX_SENSOR_TYPE; type++) {
309 sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
310 sizeof(struct attribute *) *
311 (sensor_groups[type].attr_count + 1),
312 GFP_KERNEL);
313 if (!sensor_groups[type].group.attrs)
314 return -ENOMEM;
315
316 pgroups[type] = &sensor_groups[type].group;
317 pdata->sensors_count += sensor_groups[type].attr_count;
318 sensor_groups[type].attr_count = 0;
319 }
320
321 return 0;
322}
323
Cédric Le Goater9e4f74b2015-04-08 19:19:47 +0200324static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
325 ssize_t (*show)(struct device *dev,
326 struct device_attribute *attr,
327 char *buf))
328{
329 snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
330 sensor_groups[sdata->type].name, sdata->hwmon_index,
331 attr_name);
332
333 sysfs_attr_init(&sdata->dev_attr.attr);
334 sdata->dev_attr.attr.name = sdata->name;
335 sdata->dev_attr.attr.mode = S_IRUGO;
336 sdata->dev_attr.show = show;
337}
338
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530339/*
340 * Iterate through the device tree for each child of 'sensors' node, create
341 * a sysfs attribute file, the file is named by translating the DT node name
342 * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
343 * etc..
344 */
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530345static int create_device_attrs(struct platform_device *pdev)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530346{
347 struct platform_data *pdata = platform_get_drvdata(pdev);
348 const struct attribute_group **pgroups = pdata->attr_groups;
349 struct device_node *opal, *np;
350 struct sensor_data *sdata;
Axel Lin18d03f32014-08-01 12:38:47 +0800351 u32 sensor_id;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530352 enum sensors type;
353 u32 count = 0;
354 int err = 0;
355
356 opal = of_find_node_by_path("/ibm,opal/sensors");
357 sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
358 GFP_KERNEL);
359 if (!sdata) {
360 err = -ENOMEM;
361 goto exit_put_node;
362 }
363
364 for_each_child_of_node(opal, np) {
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100365 const char *attr_name;
366 u32 opal_index;
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200367 const char *label;
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100368
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530369 if (np->name == NULL)
370 continue;
371
Cédric Le Goaterc4ad4722015-03-19 18:44:42 +0100372 type = get_sensor_type(np);
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530373 if (type == MAX_SENSOR_TYPE)
374 continue;
375
Cédric Le Goater14681632015-04-08 19:19:48 +0200376 /*
377 * Newer device trees use a "sensor-data" property
378 * name for input.
379 */
380 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
381 of_property_read_u32(np, "sensor-data", &sensor_id)) {
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530382 dev_info(&pdev->dev,
383 "'sensor-id' missing in the node '%s'\n",
384 np->name);
385 continue;
386 }
387
Axel Lin18d03f32014-08-01 12:38:47 +0800388 sdata[count].id = sensor_id;
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530389 sdata[count].type = type;
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100390
Cédric Le Goater14681632015-04-08 19:19:48 +0200391 /*
392 * If we can not parse the node name, it means we are
393 * running on a newer device tree. We can just forget
394 * about the OPAL index and use a defaut value for the
395 * hwmon attribute name
396 */
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100397 attr_name = parse_opal_node_name(np->name, type, &opal_index);
398 if (IS_ERR(attr_name)) {
Cédric Le Goater14681632015-04-08 19:19:48 +0200399 attr_name = "input";
400 opal_index = INVALID_INDEX;
Cédric Le Goaterf9f54f12015-03-19 18:44:44 +0100401 }
402
Cédric Le Goaterfcaf57b2015-03-19 18:44:45 +0100403 sdata[count].opal_index = opal_index;
404 sdata[count].hwmon_index =
405 get_sensor_hwmon_index(&sdata[count], sdata, count);
406
Cédric Le Goater9e4f74b2015-04-08 19:19:47 +0200407 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530408
409 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
410 &sdata[count++].dev_attr.attr;
Cédric Le Goater2bcd3782015-04-08 19:19:49 +0200411
412 if (!of_property_read_string(np, "label", &label)) {
413 /*
414 * For the label attribute, we can reuse the
415 * "properties" of the previous "input"
416 * attribute. They are related to the same
417 * sensor.
418 */
419 sdata[count].type = type;
420 sdata[count].opal_index = sdata[count - 1].opal_index;
421 sdata[count].hwmon_index = sdata[count - 1].hwmon_index;
422
423 make_sensor_label(np, &sdata[count], label);
424
425 create_hwmon_attr(&sdata[count], "label", show_label);
426
427 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
428 &sdata[count++].dev_attr.attr;
429 }
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530430 }
431
432exit_put_node:
433 of_node_put(opal);
434 return err;
435}
436
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530437static int ibmpowernv_probe(struct platform_device *pdev)
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530438{
439 struct platform_data *pdata;
440 struct device *hwmon_dev;
441 int err;
442
443 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
444 if (!pdata)
445 return -ENOMEM;
446
447 platform_set_drvdata(pdev, pdata);
448 pdata->sensors_count = 0;
449 err = populate_attr_groups(pdev);
450 if (err)
451 return err;
452
453 /* Create sysfs attribute data for each sensor found in the DT */
454 err = create_device_attrs(pdev);
455 if (err)
456 return err;
457
458 /* Finally, register with hwmon */
459 hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
460 pdata,
461 pdata->attr_groups);
462
463 return PTR_ERR_OR_ZERO(hwmon_dev);
464}
465
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530466static const struct platform_device_id opal_sensor_driver_ids[] = {
467 {
468 .name = "opal-sensor",
469 },
470 { }
471};
472MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
473
Cédric Le Goater0b056b22015-09-23 14:44:48 +0200474static const struct of_device_id opal_sensor_match[] = {
475 { .compatible = "ibm,opal-sensor" },
476 { },
477};
478MODULE_DEVICE_TABLE(of, opal_sensor_match);
479
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530480static struct platform_driver ibmpowernv_driver = {
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530481 .probe = ibmpowernv_probe,
482 .id_table = opal_sensor_driver_ids,
483 .driver = {
Neelesh Gupta8de303ba2014-11-05 16:45:14 +0530484 .name = DRVNAME,
Cédric Le Goater0b056b22015-09-23 14:44:48 +0200485 .of_match_table = opal_sensor_match,
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530486 },
487};
488
Axel Lin3bdec672014-11-12 16:36:47 +0800489module_platform_driver(ibmpowernv_driver);
Neelesh Gupta24c1aa82014-07-08 14:38:38 +0530490
491MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
492MODULE_DESCRIPTION("IBM POWERNV platform sensors");
493MODULE_LICENSE("GPL");