]> Git Repo - linux.git/blob - drivers/hwmon/ibmpowernv.c
Merge branch 'akpm' (patches from Andrew)
[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 #include <asm/cputhreads.h>
34 #include <asm/smp.h>
35
36 #define MAX_ATTR_LEN    32
37 #define MAX_LABEL_LEN   64
38
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  */
48 enum sensors {
49         FAN,
50         TEMP,
51         POWER_SUPPLY,
52         POWER_INPUT,
53         CURRENT,
54         ENERGY,
55         MAX_SENSOR_TYPE,
56 };
57
58 #define INVALID_INDEX (-1U)
59
60 /*
61  * 'compatible' string properties for sensor types as defined in old
62  * PowerNV firmware (skiboot). These are ordered as 'enum sensors'.
63  */
64 static const char * const legacy_compatibles[] = {
65         "ibm,opal-sensor-cooling-fan",
66         "ibm,opal-sensor-amb-temp",
67         "ibm,opal-sensor-power-supply",
68         "ibm,opal-sensor-power"
69 };
70
71 static struct sensor_group {
72         const char *name; /* matches property 'sensor-type' */
73         struct attribute_group group;
74         u32 attr_count;
75         u32 hwmon_index;
76 } sensor_groups[] = {
77         { "fan"   },
78         { "temp"  },
79         { "in"    },
80         { "power" },
81         { "curr"  },
82         { "energy" },
83 };
84
85 struct sensor_data {
86         u32 id; /* An opaque id of the firmware for each sensor */
87         u32 hwmon_index;
88         u32 opal_index;
89         enum sensors type;
90         char label[MAX_LABEL_LEN];
91         char name[MAX_ATTR_LEN];
92         struct device_attribute dev_attr;
93         struct sensor_group_data *sgrp_data;
94 };
95
96 struct sensor_group_data {
97         struct mutex mutex;
98         u32 gid;
99         bool enable;
100 };
101
102 struct platform_data {
103         const struct attribute_group *attr_groups[MAX_SENSOR_TYPE + 1];
104         struct sensor_group_data *sgrp_data;
105         u32 sensors_count; /* Total count of sensors from each group */
106         u32 nr_sensor_groups; /* Total number of sensor groups */
107 };
108
109 static ssize_t show_sensor(struct device *dev, struct device_attribute *devattr,
110                            char *buf)
111 {
112         struct sensor_data *sdata = container_of(devattr, struct sensor_data,
113                                                  dev_attr);
114         ssize_t ret;
115         u64 x;
116
117         if (sdata->sgrp_data && !sdata->sgrp_data->enable)
118                 return -ENODATA;
119
120         ret =  opal_get_sensor_data_u64(sdata->id, &x);
121
122         if (ret)
123                 return ret;
124
125         /* Convert temperature to milli-degrees */
126         if (sdata->type == TEMP)
127                 x *= 1000;
128         /* Convert power to micro-watts */
129         else if (sdata->type == POWER_INPUT)
130                 x *= 1000000;
131
132         return sprintf(buf, "%llu\n", x);
133 }
134
135 static ssize_t show_enable(struct device *dev,
136                            struct device_attribute *devattr, char *buf)
137 {
138         struct sensor_data *sdata = container_of(devattr, struct sensor_data,
139                                                  dev_attr);
140
141         return sprintf(buf, "%u\n", sdata->sgrp_data->enable);
142 }
143
144 static ssize_t store_enable(struct device *dev,
145                             struct device_attribute *devattr,
146                             const char *buf, size_t count)
147 {
148         struct sensor_data *sdata = container_of(devattr, struct sensor_data,
149                                                  dev_attr);
150         struct sensor_group_data *sgrp_data = sdata->sgrp_data;
151         int ret;
152         bool data;
153
154         ret = kstrtobool(buf, &data);
155         if (ret)
156                 return ret;
157
158         ret = mutex_lock_interruptible(&sgrp_data->mutex);
159         if (ret)
160                 return ret;
161
162         if (data != sgrp_data->enable) {
163                 ret =  sensor_group_enable(sgrp_data->gid, data);
164                 if (!ret)
165                         sgrp_data->enable = data;
166         }
167
168         if (!ret)
169                 ret = count;
170
171         mutex_unlock(&sgrp_data->mutex);
172         return ret;
173 }
174
175 static ssize_t show_label(struct device *dev, struct device_attribute *devattr,
176                           char *buf)
177 {
178         struct sensor_data *sdata = container_of(devattr, struct sensor_data,
179                                                  dev_attr);
180
181         return sprintf(buf, "%s\n", sdata->label);
182 }
183
184 static int __init get_logical_cpu(int hwcpu)
185 {
186         int cpu;
187
188         for_each_possible_cpu(cpu)
189                 if (get_hard_smp_processor_id(cpu) == hwcpu)
190                         return cpu;
191
192         return -ENOENT;
193 }
194
195 static void __init make_sensor_label(struct device_node *np,
196                                      struct sensor_data *sdata,
197                                      const char *label)
198 {
199         u32 id;
200         size_t n;
201
202         n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
203
204         /*
205          * Core temp pretty print
206          */
207         if (!of_property_read_u32(np, "ibm,pir", &id)) {
208                 int cpuid = get_logical_cpu(id);
209
210                 if (cpuid >= 0)
211                         /*
212                          * The digital thermal sensors are associated
213                          * with a core.
214                          */
215                         n += snprintf(sdata->label + n,
216                                       sizeof(sdata->label) - n, " %d",
217                                       cpuid);
218                 else
219                         n += snprintf(sdata->label + n,
220                                       sizeof(sdata->label) - n, " phy%d", id);
221         }
222
223         /*
224          * Membuffer pretty print
225          */
226         if (!of_property_read_u32(np, "ibm,chip-id", &id))
227                 n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
228                               " %d", id & 0xffff);
229 }
230
231 static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
232 {
233         char *hash_pos = strchr(name, '#');
234         char buf[8] = { 0 };
235         char *dash_pos;
236         u32 copy_len;
237         int err;
238
239         if (!hash_pos)
240                 return -EINVAL;
241
242         dash_pos = strchr(hash_pos, '-');
243         if (!dash_pos)
244                 return -EINVAL;
245
246         copy_len = dash_pos - hash_pos - 1;
247         if (copy_len >= sizeof(buf))
248                 return -EINVAL;
249
250         strncpy(buf, hash_pos + 1, copy_len);
251
252         err = kstrtou32(buf, 10, index);
253         if (err)
254                 return err;
255
256         strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
257
258         return 0;
259 }
260
261 static const char *convert_opal_attr_name(enum sensors type,
262                                           const char *opal_attr)
263 {
264         const char *attr_name = NULL;
265
266         if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
267                 attr_name = "fault";
268         } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
269                 attr_name = "input";
270         } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
271                 if (type == TEMP)
272                         attr_name = "max";
273                 else if (type == FAN)
274                         attr_name = "min";
275         }
276
277         return attr_name;
278 }
279
280 /*
281  * This function translates the DT node name into the 'hwmon' attribute name.
282  * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
283  * which need to be mapped as fan2_input, temp1_max respectively before
284  * populating them inside hwmon device class.
285  */
286 static const char *parse_opal_node_name(const char *node_name,
287                                         enum sensors type, u32 *index)
288 {
289         char attr_suffix[MAX_ATTR_LEN];
290         const char *attr_name;
291         int err;
292
293         err = get_sensor_index_attr(node_name, index, attr_suffix);
294         if (err)
295                 return ERR_PTR(err);
296
297         attr_name = convert_opal_attr_name(type, attr_suffix);
298         if (!attr_name)
299                 return ERR_PTR(-ENOENT);
300
301         return attr_name;
302 }
303
304 static int get_sensor_type(struct device_node *np)
305 {
306         enum sensors type;
307         const char *str;
308
309         for (type = 0; type < ARRAY_SIZE(legacy_compatibles); type++) {
310                 if (of_device_is_compatible(np, legacy_compatibles[type]))
311                         return type;
312         }
313
314         /*
315          * Let's check if we have a newer device tree
316          */
317         if (!of_device_is_compatible(np, "ibm,opal-sensor"))
318                 return MAX_SENSOR_TYPE;
319
320         if (of_property_read_string(np, "sensor-type", &str))
321                 return MAX_SENSOR_TYPE;
322
323         for (type = 0; type < MAX_SENSOR_TYPE; type++)
324                 if (!strcmp(str, sensor_groups[type].name))
325                         return type;
326
327         return MAX_SENSOR_TYPE;
328 }
329
330 static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
331                                   struct sensor_data *sdata_table, int count)
332 {
333         int i;
334
335         /*
336          * We don't use the OPAL index on newer device trees
337          */
338         if (sdata->opal_index != INVALID_INDEX) {
339                 for (i = 0; i < count; i++)
340                         if (sdata_table[i].opal_index == sdata->opal_index &&
341                             sdata_table[i].type == sdata->type)
342                                 return sdata_table[i].hwmon_index;
343         }
344         return ++sensor_groups[sdata->type].hwmon_index;
345 }
346
347 static int init_sensor_group_data(struct platform_device *pdev,
348                                   struct platform_data *pdata)
349 {
350         struct sensor_group_data *sgrp_data;
351         struct device_node *groups, *sgrp;
352         int count = 0, ret = 0;
353         enum sensors type;
354
355         groups = of_find_compatible_node(NULL, NULL, "ibm,opal-sensor-group");
356         if (!groups)
357                 return ret;
358
359         for_each_child_of_node(groups, sgrp) {
360                 type = get_sensor_type(sgrp);
361                 if (type != MAX_SENSOR_TYPE)
362                         pdata->nr_sensor_groups++;
363         }
364
365         if (!pdata->nr_sensor_groups)
366                 goto out;
367
368         sgrp_data = devm_kcalloc(&pdev->dev, pdata->nr_sensor_groups,
369                                  sizeof(*sgrp_data), GFP_KERNEL);
370         if (!sgrp_data) {
371                 ret = -ENOMEM;
372                 goto out;
373         }
374
375         for_each_child_of_node(groups, sgrp) {
376                 u32 gid;
377
378                 type = get_sensor_type(sgrp);
379                 if (type == MAX_SENSOR_TYPE)
380                         continue;
381
382                 if (of_property_read_u32(sgrp, "sensor-group-id", &gid))
383                         continue;
384
385                 if (of_count_phandle_with_args(sgrp, "sensors", NULL) <= 0)
386                         continue;
387
388                 sensor_groups[type].attr_count++;
389                 sgrp_data[count].gid = gid;
390                 mutex_init(&sgrp_data[count].mutex);
391                 sgrp_data[count++].enable = false;
392         }
393
394         pdata->sgrp_data = sgrp_data;
395 out:
396         of_node_put(groups);
397         return ret;
398 }
399
400 static struct sensor_group_data *get_sensor_group(struct platform_data *pdata,
401                                                   struct device_node *node,
402                                                   enum sensors gtype)
403 {
404         struct sensor_group_data *sgrp_data = pdata->sgrp_data;
405         struct device_node *groups, *sgrp;
406
407         groups = of_find_compatible_node(NULL, NULL, "ibm,opal-sensor-group");
408         if (!groups)
409                 return NULL;
410
411         for_each_child_of_node(groups, sgrp) {
412                 struct of_phandle_iterator it;
413                 u32 gid;
414                 int rc, i;
415                 enum sensors type;
416
417                 type = get_sensor_type(sgrp);
418                 if (type != gtype)
419                         continue;
420
421                 if (of_property_read_u32(sgrp, "sensor-group-id", &gid))
422                         continue;
423
424                 of_for_each_phandle(&it, rc, sgrp, "sensors", NULL, 0)
425                         if (it.phandle == node->phandle) {
426                                 of_node_put(it.node);
427                                 break;
428                         }
429
430                 if (rc)
431                         continue;
432
433                 for (i = 0; i < pdata->nr_sensor_groups; i++)
434                         if (gid == sgrp_data[i].gid) {
435                                 of_node_put(sgrp);
436                                 of_node_put(groups);
437                                 return &sgrp_data[i];
438                         }
439         }
440
441         of_node_put(groups);
442         return NULL;
443 }
444
445 static int populate_attr_groups(struct platform_device *pdev)
446 {
447         struct platform_data *pdata = platform_get_drvdata(pdev);
448         const struct attribute_group **pgroups = pdata->attr_groups;
449         struct device_node *opal, *np;
450         enum sensors type;
451         int ret;
452
453         ret = init_sensor_group_data(pdev, pdata);
454         if (ret)
455                 return ret;
456
457         opal = of_find_node_by_path("/ibm,opal/sensors");
458         for_each_child_of_node(opal, np) {
459                 const char *label;
460
461                 if (np->name == NULL)
462                         continue;
463
464                 type = get_sensor_type(np);
465                 if (type == MAX_SENSOR_TYPE)
466                         continue;
467
468                 sensor_groups[type].attr_count++;
469
470                 /*
471                  * add attributes for labels, min and max
472                  */
473                 if (!of_property_read_string(np, "label", &label))
474                         sensor_groups[type].attr_count++;
475                 if (of_find_property(np, "sensor-data-min", NULL))
476                         sensor_groups[type].attr_count++;
477                 if (of_find_property(np, "sensor-data-max", NULL))
478                         sensor_groups[type].attr_count++;
479         }
480
481         of_node_put(opal);
482
483         for (type = 0; type < MAX_SENSOR_TYPE; type++) {
484                 sensor_groups[type].group.attrs = devm_kcalloc(&pdev->dev,
485                                         sensor_groups[type].attr_count + 1,
486                                         sizeof(struct attribute *),
487                                         GFP_KERNEL);
488                 if (!sensor_groups[type].group.attrs)
489                         return -ENOMEM;
490
491                 pgroups[type] = &sensor_groups[type].group;
492                 pdata->sensors_count += sensor_groups[type].attr_count;
493                 sensor_groups[type].attr_count = 0;
494         }
495
496         return 0;
497 }
498
499 static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
500                               ssize_t (*show)(struct device *dev,
501                                               struct device_attribute *attr,
502                                               char *buf),
503                             ssize_t (*store)(struct device *dev,
504                                              struct device_attribute *attr,
505                                              const char *buf, size_t count))
506 {
507         snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
508                  sensor_groups[sdata->type].name, sdata->hwmon_index,
509                  attr_name);
510
511         sysfs_attr_init(&sdata->dev_attr.attr);
512         sdata->dev_attr.attr.name = sdata->name;
513         sdata->dev_attr.show = show;
514         if (store) {
515                 sdata->dev_attr.store = store;
516                 sdata->dev_attr.attr.mode = 0664;
517         } else {
518                 sdata->dev_attr.attr.mode = 0444;
519         }
520 }
521
522 static void populate_sensor(struct sensor_data *sdata, int od, int hd, int sid,
523                             const char *attr_name, enum sensors type,
524                             const struct attribute_group *pgroup,
525                             struct sensor_group_data *sgrp_data,
526                             ssize_t (*show)(struct device *dev,
527                                             struct device_attribute *attr,
528                                             char *buf),
529                             ssize_t (*store)(struct device *dev,
530                                              struct device_attribute *attr,
531                                              const char *buf, size_t count))
532 {
533         sdata->id = sid;
534         sdata->type = type;
535         sdata->opal_index = od;
536         sdata->hwmon_index = hd;
537         create_hwmon_attr(sdata, attr_name, show, store);
538         pgroup->attrs[sensor_groups[type].attr_count++] = &sdata->dev_attr.attr;
539         sdata->sgrp_data = sgrp_data;
540 }
541
542 static char *get_max_attr(enum sensors type)
543 {
544         switch (type) {
545         case POWER_INPUT:
546                 return "input_highest";
547         default:
548                 return "highest";
549         }
550 }
551
552 static char *get_min_attr(enum sensors type)
553 {
554         switch (type) {
555         case POWER_INPUT:
556                 return "input_lowest";
557         default:
558                 return "lowest";
559         }
560 }
561
562 /*
563  * Iterate through the device tree for each child of 'sensors' node, create
564  * a sysfs attribute file, the file is named by translating the DT node name
565  * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
566  * etc..
567  */
568 static int create_device_attrs(struct platform_device *pdev)
569 {
570         struct platform_data *pdata = platform_get_drvdata(pdev);
571         const struct attribute_group **pgroups = pdata->attr_groups;
572         struct device_node *opal, *np;
573         struct sensor_data *sdata;
574         u32 count = 0;
575         u32 group_attr_id[MAX_SENSOR_TYPE] = {0};
576
577         sdata = devm_kcalloc(&pdev->dev,
578                              pdata->sensors_count, sizeof(*sdata),
579                              GFP_KERNEL);
580         if (!sdata)
581                 return -ENOMEM;
582
583         opal = of_find_node_by_path("/ibm,opal/sensors");
584         for_each_child_of_node(opal, np) {
585                 struct sensor_group_data *sgrp_data;
586                 const char *attr_name;
587                 u32 opal_index, hw_id;
588                 u32 sensor_id;
589                 const char *label;
590                 enum sensors type;
591
592                 if (np->name == NULL)
593                         continue;
594
595                 type = get_sensor_type(np);
596                 if (type == MAX_SENSOR_TYPE)
597                         continue;
598
599                 /*
600                  * Newer device trees use a "sensor-data" property
601                  * name for input.
602                  */
603                 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
604                     of_property_read_u32(np, "sensor-data", &sensor_id)) {
605                         dev_info(&pdev->dev,
606                                  "'sensor-id' missing in the node '%s'\n",
607                                  np->name);
608                         continue;
609                 }
610
611                 sdata[count].id = sensor_id;
612                 sdata[count].type = type;
613
614                 /*
615                  * If we can not parse the node name, it means we are
616                  * running on a newer device tree. We can just forget
617                  * about the OPAL index and use a defaut value for the
618                  * hwmon attribute name
619                  */
620                 attr_name = parse_opal_node_name(np->name, type, &opal_index);
621                 if (IS_ERR(attr_name)) {
622                         attr_name = "input";
623                         opal_index = INVALID_INDEX;
624                 }
625
626                 hw_id = get_sensor_hwmon_index(&sdata[count], sdata, count);
627                 sgrp_data = get_sensor_group(pdata, np, type);
628                 populate_sensor(&sdata[count], opal_index, hw_id, sensor_id,
629                                 attr_name, type, pgroups[type], sgrp_data,
630                                 show_sensor, NULL);
631                 count++;
632
633                 if (!of_property_read_string(np, "label", &label)) {
634                         /*
635                          * For the label attribute, we can reuse the
636                          * "properties" of the previous "input"
637                          * attribute. They are related to the same
638                          * sensor.
639                          */
640
641                         make_sensor_label(np, &sdata[count], label);
642                         populate_sensor(&sdata[count], opal_index, hw_id,
643                                         sensor_id, "label", type, pgroups[type],
644                                         NULL, show_label, NULL);
645                         count++;
646                 }
647
648                 if (!of_property_read_u32(np, "sensor-data-max", &sensor_id)) {
649                         attr_name = get_max_attr(type);
650                         populate_sensor(&sdata[count], opal_index, hw_id,
651                                         sensor_id, attr_name, type,
652                                         pgroups[type], sgrp_data, show_sensor,
653                                         NULL);
654                         count++;
655                 }
656
657                 if (!of_property_read_u32(np, "sensor-data-min", &sensor_id)) {
658                         attr_name = get_min_attr(type);
659                         populate_sensor(&sdata[count], opal_index, hw_id,
660                                         sensor_id, attr_name, type,
661                                         pgroups[type], sgrp_data, show_sensor,
662                                         NULL);
663                         count++;
664                 }
665
666                 if (sgrp_data && !sgrp_data->enable) {
667                         sgrp_data->enable = true;
668                         hw_id = ++group_attr_id[type];
669                         populate_sensor(&sdata[count], opal_index, hw_id,
670                                         sgrp_data->gid, "enable", type,
671                                         pgroups[type], sgrp_data, show_enable,
672                                         store_enable);
673                         count++;
674                 }
675         }
676
677         of_node_put(opal);
678         return 0;
679 }
680
681 static int ibmpowernv_probe(struct platform_device *pdev)
682 {
683         struct platform_data *pdata;
684         struct device *hwmon_dev;
685         int err;
686
687         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
688         if (!pdata)
689                 return -ENOMEM;
690
691         platform_set_drvdata(pdev, pdata);
692         pdata->sensors_count = 0;
693         pdata->nr_sensor_groups = 0;
694         err = populate_attr_groups(pdev);
695         if (err)
696                 return err;
697
698         /* Create sysfs attribute data for each sensor found in the DT */
699         err = create_device_attrs(pdev);
700         if (err)
701                 return err;
702
703         /* Finally, register with hwmon */
704         hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
705                                                            pdata,
706                                                            pdata->attr_groups);
707
708         return PTR_ERR_OR_ZERO(hwmon_dev);
709 }
710
711 static const struct platform_device_id opal_sensor_driver_ids[] = {
712         {
713                 .name = "opal-sensor",
714         },
715         { }
716 };
717 MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
718
719 static const struct of_device_id opal_sensor_match[] = {
720         { .compatible   = "ibm,opal-sensor" },
721         { },
722 };
723 MODULE_DEVICE_TABLE(of, opal_sensor_match);
724
725 static struct platform_driver ibmpowernv_driver = {
726         .probe          = ibmpowernv_probe,
727         .id_table       = opal_sensor_driver_ids,
728         .driver         = {
729                 .name   = DRVNAME,
730                 .of_match_table = opal_sensor_match,
731         },
732 };
733
734 module_platform_driver(ibmpowernv_driver);
735
736 MODULE_AUTHOR("Neelesh Gupta <[email protected]>");
737 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
738 MODULE_LICENSE("GPL");
This page took 0.077162 seconds and 4 git commands to generate.