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