OSDN Git Service

Input: gtco - fix endpoint sanity check
[sagit-ice-cold/kernel_xiaomi_msm8998.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         MAX_SENSOR_TYPE,
54 };
55
56 #define INVALID_INDEX (-1U)
57
58 static struct sensor_group {
59         const char *name;
60         const char *compatible;
61         struct attribute_group group;
62         u32 attr_count;
63         u32 hwmon_index;
64 } 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
71 struct sensor_data {
72         u32 id; /* An opaque id of the firmware for each sensor */
73         u32 hwmon_index;
74         u32 opal_index;
75         enum sensors type;
76         char label[MAX_LABEL_LEN];
77         char name[MAX_ATTR_LEN];
78         struct device_attribute dev_attr;
79 };
80
81 struct 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
86 static 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 */
99         if (sdata->type == TEMP)
100                 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
108 static 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
117 static int get_logical_cpu(int hwcpu)
118 {
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
128 static void make_sensor_label(struct device_node *np,
129                               struct sensor_data *sdata, const char *label)
130 {
131         u32 id;
132         size_t n;
133
134         n = snprintf(sdata->label, sizeof(sdata->label), "%s", label);
135
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
145                          * with a core. Let's print out the range of
146                          * cpu ids corresponding to the hardware
147                          * threads of the core.
148                          */
149                         n += snprintf(sdata->label + n,
150                                       sizeof(sdata->label) - n, " %d-%d",
151                                       cpuid, cpuid + threads_per_core - 1);
152                 else
153                         n += snprintf(sdata->label + n,
154                                       sizeof(sdata->label) - n, " phy%d", id);
155         }
156
157         /*
158          * Membuffer pretty print
159          */
160         if (!of_property_read_u32(np, "ibm,chip-id", &id))
161                 n += snprintf(sdata->label + n, sizeof(sdata->label) - n,
162                               " %d", id & 0xffff);
163 }
164
165 static int get_sensor_index_attr(const char *name, u32 *index, char *attr)
166 {
167         char *hash_pos = strchr(name, '#');
168         char buf[8] = { 0 };
169         char *dash_pos;
170         u32 copy_len;
171         int err;
172
173         if (!hash_pos)
174                 return -EINVAL;
175
176         dash_pos = strchr(hash_pos, '-');
177         if (!dash_pos)
178                 return -EINVAL;
179
180         copy_len = dash_pos - hash_pos - 1;
181         if (copy_len >= sizeof(buf))
182                 return -EINVAL;
183
184         strncpy(buf, hash_pos + 1, copy_len);
185
186         err = kstrtou32(buf, 10, index);
187         if (err)
188                 return err;
189
190         strncpy(attr, dash_pos + 1, MAX_ATTR_LEN);
191
192         return 0;
193 }
194
195 static const char *convert_opal_attr_name(enum sensors type,
196                                           const char *opal_attr)
197 {
198         const char *attr_name = NULL;
199
200         if (!strcmp(opal_attr, DT_FAULT_ATTR_SUFFIX)) {
201                 attr_name = "fault";
202         } else if (!strcmp(opal_attr, DT_DATA_ATTR_SUFFIX)) {
203                 attr_name = "input";
204         } else if (!strcmp(opal_attr, DT_THRESHOLD_ATTR_SUFFIX)) {
205                 if (type == TEMP)
206                         attr_name = "max";
207                 else if (type == FAN)
208                         attr_name = "min";
209         }
210
211         return attr_name;
212 }
213
214 /*
215  * This function translates the DT node name into the 'hwmon' attribute name.
216  * IBMPOWERNV device node appear like cooling-fan#2-data, amb-temp#1-thrs etc.
217  * which need to be mapped as fan2_input, temp1_max respectively before
218  * populating them inside hwmon device class.
219  */
220 static const char *parse_opal_node_name(const char *node_name,
221                                         enum sensors type, u32 *index)
222 {
223         char attr_suffix[MAX_ATTR_LEN];
224         const char *attr_name;
225         int err;
226
227         err = get_sensor_index_attr(node_name, index, attr_suffix);
228         if (err)
229                 return ERR_PTR(err);
230
231         attr_name = convert_opal_attr_name(type, attr_suffix);
232         if (!attr_name)
233                 return ERR_PTR(-ENOENT);
234
235         return attr_name;
236 }
237
238 static int get_sensor_type(struct device_node *np)
239 {
240         enum sensors type;
241         const char *str;
242
243         for (type = 0; type < MAX_SENSOR_TYPE; type++) {
244                 if (of_device_is_compatible(np, sensor_groups[type].compatible))
245                         return type;
246         }
247
248         /*
249          * Let's check if we have a newer device tree
250          */
251         if (!of_device_is_compatible(np, "ibm,opal-sensor"))
252                 return MAX_SENSOR_TYPE;
253
254         if (of_property_read_string(np, "sensor-type", &str))
255                 return MAX_SENSOR_TYPE;
256
257         for (type = 0; type < MAX_SENSOR_TYPE; type++)
258                 if (!strcmp(str, sensor_groups[type].name))
259                         return type;
260
261         return MAX_SENSOR_TYPE;
262 }
263
264 static u32 get_sensor_hwmon_index(struct sensor_data *sdata,
265                                   struct sensor_data *sdata_table, int count)
266 {
267         int i;
268
269         /*
270          * We don't use the OPAL index on newer device trees
271          */
272         if (sdata->opal_index != INVALID_INDEX) {
273                 for (i = 0; i < count; i++)
274                         if (sdata_table[i].opal_index == sdata->opal_index &&
275                             sdata_table[i].type == sdata->type)
276                                 return sdata_table[i].hwmon_index;
277         }
278         return ++sensor_groups[sdata->type].hwmon_index;
279 }
280
281 static int populate_attr_groups(struct platform_device *pdev)
282 {
283         struct platform_data *pdata = platform_get_drvdata(pdev);
284         const struct attribute_group **pgroups = pdata->attr_groups;
285         struct device_node *opal, *np;
286         enum sensors type;
287
288         opal = of_find_node_by_path("/ibm,opal/sensors");
289         for_each_child_of_node(opal, np) {
290                 const char *label;
291
292                 if (np->name == NULL)
293                         continue;
294
295                 type = get_sensor_type(np);
296                 if (type == MAX_SENSOR_TYPE)
297                         continue;
298
299                 sensor_groups[type].attr_count++;
300
301                 /*
302                  * add a new attribute for labels
303                  */
304                 if (!of_property_read_string(np, "label", &label))
305                         sensor_groups[type].attr_count++;
306         }
307
308         of_node_put(opal);
309
310         for (type = 0; type < MAX_SENSOR_TYPE; type++) {
311                 sensor_groups[type].group.attrs = devm_kzalloc(&pdev->dev,
312                                         sizeof(struct attribute *) *
313                                         (sensor_groups[type].attr_count + 1),
314                                         GFP_KERNEL);
315                 if (!sensor_groups[type].group.attrs)
316                         return -ENOMEM;
317
318                 pgroups[type] = &sensor_groups[type].group;
319                 pdata->sensors_count += sensor_groups[type].attr_count;
320                 sensor_groups[type].attr_count = 0;
321         }
322
323         return 0;
324 }
325
326 static void create_hwmon_attr(struct sensor_data *sdata, const char *attr_name,
327                               ssize_t (*show)(struct device *dev,
328                                               struct device_attribute *attr,
329                                               char *buf))
330 {
331         snprintf(sdata->name, MAX_ATTR_LEN, "%s%d_%s",
332                  sensor_groups[sdata->type].name, sdata->hwmon_index,
333                  attr_name);
334
335         sysfs_attr_init(&sdata->dev_attr.attr);
336         sdata->dev_attr.attr.name = sdata->name;
337         sdata->dev_attr.attr.mode = S_IRUGO;
338         sdata->dev_attr.show = show;
339 }
340
341 /*
342  * Iterate through the device tree for each child of 'sensors' node, create
343  * a sysfs attribute file, the file is named by translating the DT node name
344  * to the name required by the higher 'hwmon' driver like fan1_input, temp1_max
345  * etc..
346  */
347 static int create_device_attrs(struct platform_device *pdev)
348 {
349         struct platform_data *pdata = platform_get_drvdata(pdev);
350         const struct attribute_group **pgroups = pdata->attr_groups;
351         struct device_node *opal, *np;
352         struct sensor_data *sdata;
353         u32 sensor_id;
354         enum sensors type;
355         u32 count = 0;
356         int err = 0;
357
358         opal = of_find_node_by_path("/ibm,opal/sensors");
359         sdata = devm_kzalloc(&pdev->dev, pdata->sensors_count * sizeof(*sdata),
360                              GFP_KERNEL);
361         if (!sdata) {
362                 err = -ENOMEM;
363                 goto exit_put_node;
364         }
365
366         for_each_child_of_node(opal, np) {
367                 const char *attr_name;
368                 u32 opal_index;
369                 const char *label;
370
371                 if (np->name == NULL)
372                         continue;
373
374                 type = get_sensor_type(np);
375                 if (type == MAX_SENSOR_TYPE)
376                         continue;
377
378                 /*
379                  * Newer device trees use a "sensor-data" property
380                  * name for input.
381                  */
382                 if (of_property_read_u32(np, "sensor-id", &sensor_id) &&
383                     of_property_read_u32(np, "sensor-data", &sensor_id)) {
384                         dev_info(&pdev->dev,
385                                  "'sensor-id' missing in the node '%s'\n",
386                                  np->name);
387                         continue;
388                 }
389
390                 sdata[count].id = sensor_id;
391                 sdata[count].type = type;
392
393                 /*
394                  * If we can not parse the node name, it means we are
395                  * running on a newer device tree. We can just forget
396                  * about the OPAL index and use a defaut value for the
397                  * hwmon attribute name
398                  */
399                 attr_name = parse_opal_node_name(np->name, type, &opal_index);
400                 if (IS_ERR(attr_name)) {
401                         attr_name = "input";
402                         opal_index = INVALID_INDEX;
403                 }
404
405                 sdata[count].opal_index = opal_index;
406                 sdata[count].hwmon_index =
407                         get_sensor_hwmon_index(&sdata[count], sdata, count);
408
409                 create_hwmon_attr(&sdata[count], attr_name, show_sensor);
410
411                 pgroups[type]->attrs[sensor_groups[type].attr_count++] =
412                                 &sdata[count++].dev_attr.attr;
413
414                 if (!of_property_read_string(np, "label", &label)) {
415                         /*
416                          * For the label attribute, we can reuse the
417                          * "properties" of the previous "input"
418                          * attribute. They are related to the same
419                          * sensor.
420                          */
421                         sdata[count].type = type;
422                         sdata[count].opal_index = sdata[count - 1].opal_index;
423                         sdata[count].hwmon_index = sdata[count - 1].hwmon_index;
424
425                         make_sensor_label(np, &sdata[count], label);
426
427                         create_hwmon_attr(&sdata[count], "label", show_label);
428
429                         pgroups[type]->attrs[sensor_groups[type].attr_count++] =
430                                 &sdata[count++].dev_attr.attr;
431                 }
432         }
433
434 exit_put_node:
435         of_node_put(opal);
436         return err;
437 }
438
439 static int ibmpowernv_probe(struct platform_device *pdev)
440 {
441         struct platform_data *pdata;
442         struct device *hwmon_dev;
443         int err;
444
445         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
446         if (!pdata)
447                 return -ENOMEM;
448
449         platform_set_drvdata(pdev, pdata);
450         pdata->sensors_count = 0;
451         err = populate_attr_groups(pdev);
452         if (err)
453                 return err;
454
455         /* Create sysfs attribute data for each sensor found in the DT */
456         err = create_device_attrs(pdev);
457         if (err)
458                 return err;
459
460         /* Finally, register with hwmon */
461         hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
462                                                            pdata,
463                                                            pdata->attr_groups);
464
465         return PTR_ERR_OR_ZERO(hwmon_dev);
466 }
467
468 static const struct platform_device_id opal_sensor_driver_ids[] = {
469         {
470                 .name = "opal-sensor",
471         },
472         { }
473 };
474 MODULE_DEVICE_TABLE(platform, opal_sensor_driver_ids);
475
476 static const struct of_device_id opal_sensor_match[] = {
477         { .compatible   = "ibm,opal-sensor" },
478         { },
479 };
480 MODULE_DEVICE_TABLE(of, opal_sensor_match);
481
482 static struct platform_driver ibmpowernv_driver = {
483         .probe          = ibmpowernv_probe,
484         .id_table       = opal_sensor_driver_ids,
485         .driver         = {
486                 .name   = DRVNAME,
487                 .of_match_table = opal_sensor_match,
488         },
489 };
490
491 module_platform_driver(ibmpowernv_driver);
492
493 MODULE_AUTHOR("Neelesh Gupta <neelegup@linux.vnet.ibm.com>");
494 MODULE_DESCRIPTION("IBM POWERNV platform sensors");
495 MODULE_LICENSE("GPL");