Commit | Line | Data |
---|---|---|
7e3c0381 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a369ee88 EV |
2 | /* |
3 | * thermal.c - sysfs interface of thermal devices | |
4 | * | |
5 | * Copyright (C) 2016 Eduardo Valentin <edubezval@gmail.com> | |
6 | * | |
7 | * Highly based on original thermal_core.c | |
8 | * Copyright (C) 2008 Intel Corp | |
9 | * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> | |
10 | * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> | |
a369ee88 EV |
11 | */ |
12 | ||
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
14 | ||
15 | #include <linux/sysfs.h> | |
16 | #include <linux/device.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/string.h> | |
8ea22951 | 20 | #include <linux/jiffies.h> |
a369ee88 EV |
21 | |
22 | #include "thermal_core.h" | |
23 | ||
24 | /* sys I/F for thermal zone */ | |
25 | ||
26 | static ssize_t | |
27 | type_show(struct device *dev, struct device_attribute *attr, char *buf) | |
28 | { | |
29 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
30 | ||
31 | return sprintf(buf, "%s\n", tz->type); | |
32 | } | |
33 | ||
34 | static ssize_t | |
35 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) | |
36 | { | |
37 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
38 | int temperature, ret; | |
39 | ||
40 | ret = thermal_zone_get_temp(tz, &temperature); | |
41 | ||
42 | if (ret) | |
43 | return ret; | |
44 | ||
45 | return sprintf(buf, "%d\n", temperature); | |
46 | } | |
47 | ||
48 | static ssize_t | |
49 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) | |
50 | { | |
51 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
a369ee88 | 52 | |
1ee14820 AP |
53 | return sprintf(buf, "%s\n", tz->mode == THERMAL_DEVICE_ENABLED ? |
54 | "enabled" : "disabled"); | |
a369ee88 EV |
55 | } |
56 | ||
57 | static ssize_t | |
58 | mode_store(struct device *dev, struct device_attribute *attr, | |
59 | const char *buf, size_t count) | |
60 | { | |
61 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
62 | int result; | |
63 | ||
64 | if (!tz->ops->set_mode) | |
65 | return -EPERM; | |
66 | ||
67 | if (!strncmp(buf, "enabled", sizeof("enabled") - 1)) | |
68 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); | |
69 | else if (!strncmp(buf, "disabled", sizeof("disabled") - 1)) | |
70 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); | |
71 | else | |
72 | result = -EINVAL; | |
73 | ||
74 | if (result) | |
75 | return result; | |
76 | ||
77 | return count; | |
78 | } | |
79 | ||
80 | static ssize_t | |
81 | trip_point_type_show(struct device *dev, struct device_attribute *attr, | |
82 | char *buf) | |
83 | { | |
84 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
85 | enum thermal_trip_type type; | |
86 | int trip, result; | |
87 | ||
88 | if (!tz->ops->get_trip_type) | |
89 | return -EPERM; | |
90 | ||
91 | if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1) | |
92 | return -EINVAL; | |
93 | ||
94 | result = tz->ops->get_trip_type(tz, trip, &type); | |
95 | if (result) | |
96 | return result; | |
97 | ||
98 | switch (type) { | |
99 | case THERMAL_TRIP_CRITICAL: | |
100 | return sprintf(buf, "critical\n"); | |
101 | case THERMAL_TRIP_HOT: | |
102 | return sprintf(buf, "hot\n"); | |
103 | case THERMAL_TRIP_PASSIVE: | |
104 | return sprintf(buf, "passive\n"); | |
105 | case THERMAL_TRIP_ACTIVE: | |
106 | return sprintf(buf, "active\n"); | |
107 | default: | |
108 | return sprintf(buf, "unknown\n"); | |
109 | } | |
110 | } | |
111 | ||
112 | static ssize_t | |
113 | trip_point_temp_store(struct device *dev, struct device_attribute *attr, | |
114 | const char *buf, size_t count) | |
115 | { | |
116 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
117 | int trip, ret; | |
118 | int temperature; | |
119 | ||
120 | if (!tz->ops->set_trip_temp) | |
121 | return -EPERM; | |
122 | ||
123 | if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) | |
124 | return -EINVAL; | |
125 | ||
126 | if (kstrtoint(buf, 10, &temperature)) | |
127 | return -EINVAL; | |
128 | ||
129 | ret = tz->ops->set_trip_temp(tz, trip, temperature); | |
130 | if (ret) | |
131 | return ret; | |
132 | ||
133 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); | |
134 | ||
135 | return count; | |
136 | } | |
137 | ||
138 | static ssize_t | |
139 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, | |
140 | char *buf) | |
141 | { | |
142 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
143 | int trip, ret; | |
144 | int temperature; | |
145 | ||
146 | if (!tz->ops->get_trip_temp) | |
147 | return -EPERM; | |
148 | ||
149 | if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) | |
150 | return -EINVAL; | |
151 | ||
152 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); | |
153 | ||
154 | if (ret) | |
155 | return ret; | |
156 | ||
157 | return sprintf(buf, "%d\n", temperature); | |
158 | } | |
159 | ||
160 | static ssize_t | |
161 | trip_point_hyst_store(struct device *dev, struct device_attribute *attr, | |
162 | const char *buf, size_t count) | |
163 | { | |
164 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
165 | int trip, ret; | |
166 | int temperature; | |
167 | ||
168 | if (!tz->ops->set_trip_hyst) | |
169 | return -EPERM; | |
170 | ||
171 | if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) | |
172 | return -EINVAL; | |
173 | ||
174 | if (kstrtoint(buf, 10, &temperature)) | |
175 | return -EINVAL; | |
176 | ||
177 | /* | |
178 | * We are not doing any check on the 'temperature' value | |
179 | * here. The driver implementing 'set_trip_hyst' has to | |
180 | * take care of this. | |
181 | */ | |
182 | ret = tz->ops->set_trip_hyst(tz, trip, temperature); | |
183 | ||
184 | if (!ret) | |
185 | thermal_zone_set_trips(tz); | |
186 | ||
187 | return ret ? ret : count; | |
188 | } | |
189 | ||
190 | static ssize_t | |
191 | trip_point_hyst_show(struct device *dev, struct device_attribute *attr, | |
192 | char *buf) | |
193 | { | |
194 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
195 | int trip, ret; | |
196 | int temperature; | |
197 | ||
198 | if (!tz->ops->get_trip_hyst) | |
199 | return -EPERM; | |
200 | ||
201 | if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) | |
202 | return -EINVAL; | |
203 | ||
204 | ret = tz->ops->get_trip_hyst(tz, trip, &temperature); | |
205 | ||
206 | return ret ? ret : sprintf(buf, "%d\n", temperature); | |
207 | } | |
208 | ||
209 | static ssize_t | |
210 | passive_store(struct device *dev, struct device_attribute *attr, | |
211 | const char *buf, size_t count) | |
212 | { | |
213 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
214 | int state; | |
215 | ||
216 | if (sscanf(buf, "%d\n", &state) != 1) | |
217 | return -EINVAL; | |
218 | ||
219 | /* sanity check: values below 1000 millicelcius don't make sense | |
220 | * and can cause the system to go into a thermal heart attack | |
221 | */ | |
222 | if (state && state < 1000) | |
223 | return -EINVAL; | |
224 | ||
225 | if (state && !tz->forced_passive) { | |
226 | if (!tz->passive_delay) | |
227 | tz->passive_delay = 1000; | |
228 | thermal_zone_device_rebind_exception(tz, "Processor", | |
229 | sizeof("Processor")); | |
230 | } else if (!state && tz->forced_passive) { | |
231 | tz->passive_delay = 0; | |
232 | thermal_zone_device_unbind_exception(tz, "Processor", | |
233 | sizeof("Processor")); | |
234 | } | |
235 | ||
236 | tz->forced_passive = state; | |
237 | ||
238 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); | |
239 | ||
240 | return count; | |
241 | } | |
242 | ||
243 | static ssize_t | |
244 | passive_show(struct device *dev, struct device_attribute *attr, | |
245 | char *buf) | |
246 | { | |
247 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
248 | ||
249 | return sprintf(buf, "%d\n", tz->forced_passive); | |
250 | } | |
251 | ||
252 | static ssize_t | |
253 | policy_store(struct device *dev, struct device_attribute *attr, | |
254 | const char *buf, size_t count) | |
255 | { | |
256 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
257 | char name[THERMAL_NAME_LENGTH]; | |
258 | int ret; | |
259 | ||
260 | snprintf(name, sizeof(name), "%s", buf); | |
261 | ||
262 | ret = thermal_zone_device_set_policy(tz, name); | |
263 | if (!ret) | |
264 | ret = count; | |
265 | ||
266 | return ret; | |
267 | } | |
268 | ||
269 | static ssize_t | |
270 | policy_show(struct device *dev, struct device_attribute *devattr, char *buf) | |
271 | { | |
272 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
273 | ||
274 | return sprintf(buf, "%s\n", tz->governor->name); | |
275 | } | |
276 | ||
277 | static ssize_t | |
278 | available_policies_show(struct device *dev, struct device_attribute *devattr, | |
279 | char *buf) | |
280 | { | |
281 | return thermal_build_list_of_policies(buf); | |
282 | } | |
283 | ||
698db4fd | 284 | #if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) |
a369ee88 EV |
285 | static ssize_t |
286 | emul_temp_store(struct device *dev, struct device_attribute *attr, | |
287 | const char *buf, size_t count) | |
288 | { | |
289 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
290 | int ret = 0; | |
291 | int temperature; | |
292 | ||
293 | if (kstrtoint(buf, 10, &temperature)) | |
294 | return -EINVAL; | |
295 | ||
296 | if (!tz->ops->set_emul_temp) { | |
297 | mutex_lock(&tz->lock); | |
298 | tz->emul_temperature = temperature; | |
299 | mutex_unlock(&tz->lock); | |
300 | } else { | |
301 | ret = tz->ops->set_emul_temp(tz, temperature); | |
302 | } | |
303 | ||
304 | if (!ret) | |
305 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); | |
306 | ||
307 | return ret ? ret : count; | |
308 | } | |
6cbaefb4 | 309 | static DEVICE_ATTR_WO(emul_temp); |
698db4fd | 310 | #endif |
a369ee88 EV |
311 | |
312 | static ssize_t | |
313 | sustainable_power_show(struct device *dev, struct device_attribute *devattr, | |
314 | char *buf) | |
315 | { | |
316 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
317 | ||
318 | if (tz->tzp) | |
319 | return sprintf(buf, "%u\n", tz->tzp->sustainable_power); | |
320 | else | |
321 | return -EIO; | |
322 | } | |
323 | ||
324 | static ssize_t | |
325 | sustainable_power_store(struct device *dev, struct device_attribute *devattr, | |
326 | const char *buf, size_t count) | |
327 | { | |
328 | struct thermal_zone_device *tz = to_thermal_zone(dev); | |
329 | u32 sustainable_power; | |
330 | ||
331 | if (!tz->tzp) | |
332 | return -EIO; | |
333 | ||
334 | if (kstrtou32(buf, 10, &sustainable_power)) | |
335 | return -EINVAL; | |
336 | ||
337 | tz->tzp->sustainable_power = sustainable_power; | |
338 | ||
339 | return count; | |
340 | } | |
341 | ||
342 | #define create_s32_tzp_attr(name) \ | |
343 | static ssize_t \ | |
344 | name##_show(struct device *dev, struct device_attribute *devattr, \ | |
345 | char *buf) \ | |
346 | { \ | |
347 | struct thermal_zone_device *tz = to_thermal_zone(dev); \ | |
348 | \ | |
349 | if (tz->tzp) \ | |
350 | return sprintf(buf, "%d\n", tz->tzp->name); \ | |
351 | else \ | |
352 | return -EIO; \ | |
353 | } \ | |
354 | \ | |
355 | static ssize_t \ | |
356 | name##_store(struct device *dev, struct device_attribute *devattr, \ | |
357 | const char *buf, size_t count) \ | |
358 | { \ | |
359 | struct thermal_zone_device *tz = to_thermal_zone(dev); \ | |
360 | s32 value; \ | |
361 | \ | |
362 | if (!tz->tzp) \ | |
363 | return -EIO; \ | |
364 | \ | |
365 | if (kstrtos32(buf, 10, &value)) \ | |
366 | return -EINVAL; \ | |
367 | \ | |
368 | tz->tzp->name = value; \ | |
369 | \ | |
370 | return count; \ | |
371 | } \ | |
e76a4386 | 372 | static DEVICE_ATTR_RW(name) |
a369ee88 EV |
373 | |
374 | create_s32_tzp_attr(k_po); | |
375 | create_s32_tzp_attr(k_pu); | |
376 | create_s32_tzp_attr(k_i); | |
377 | create_s32_tzp_attr(k_d); | |
378 | create_s32_tzp_attr(integral_cutoff); | |
379 | create_s32_tzp_attr(slope); | |
380 | create_s32_tzp_attr(offset); | |
381 | #undef create_s32_tzp_attr | |
382 | ||
383 | /* | |
384 | * These are thermal zone device attributes that will always be present. | |
385 | * All the attributes created for tzp (create_s32_tzp_attr) also are always | |
386 | * present on the sysfs interface. | |
387 | */ | |
c828a892 JP |
388 | static DEVICE_ATTR_RO(type); |
389 | static DEVICE_ATTR_RO(temp); | |
b6b996b6 | 390 | static DEVICE_ATTR_RW(policy); |
c828a892 | 391 | static DEVICE_ATTR_RO(available_policies); |
b6b996b6 | 392 | static DEVICE_ATTR_RW(sustainable_power); |
a369ee88 EV |
393 | |
394 | /* These thermal zone device attributes are created based on conditions */ | |
b6b996b6 JP |
395 | static DEVICE_ATTR_RW(mode); |
396 | static DEVICE_ATTR_RW(passive); | |
a369ee88 EV |
397 | |
398 | /* These attributes are unconditionally added to a thermal zone */ | |
399 | static struct attribute *thermal_zone_dev_attrs[] = { | |
400 | &dev_attr_type.attr, | |
401 | &dev_attr_temp.attr, | |
402 | #if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) | |
403 | &dev_attr_emul_temp.attr, | |
404 | #endif | |
405 | &dev_attr_policy.attr, | |
406 | &dev_attr_available_policies.attr, | |
407 | &dev_attr_sustainable_power.attr, | |
408 | &dev_attr_k_po.attr, | |
409 | &dev_attr_k_pu.attr, | |
410 | &dev_attr_k_i.attr, | |
411 | &dev_attr_k_d.attr, | |
412 | &dev_attr_integral_cutoff.attr, | |
413 | &dev_attr_slope.attr, | |
414 | &dev_attr_offset.attr, | |
415 | NULL, | |
416 | }; | |
417 | ||
418 | static struct attribute_group thermal_zone_attribute_group = { | |
419 | .attrs = thermal_zone_dev_attrs, | |
420 | }; | |
421 | ||
a369ee88 EV |
422 | static struct attribute *thermal_zone_mode_attrs[] = { |
423 | &dev_attr_mode.attr, | |
424 | NULL, | |
425 | }; | |
426 | ||
a369ee88 EV |
427 | static struct attribute_group thermal_zone_mode_attribute_group = { |
428 | .attrs = thermal_zone_mode_attrs, | |
a369ee88 EV |
429 | }; |
430 | ||
431 | /* We expose passive only if passive trips are present */ | |
432 | static struct attribute *thermal_zone_passive_attrs[] = { | |
433 | &dev_attr_passive.attr, | |
434 | NULL, | |
435 | }; | |
436 | ||
437 | static umode_t thermal_zone_passive_is_visible(struct kobject *kobj, | |
438 | struct attribute *attr, | |
439 | int attrno) | |
440 | { | |
441 | struct device *dev = container_of(kobj, struct device, kobj); | |
442 | struct thermal_zone_device *tz; | |
443 | enum thermal_trip_type trip_type; | |
444 | int count, passive = 0; | |
445 | ||
446 | tz = container_of(dev, struct thermal_zone_device, device); | |
447 | ||
448 | for (count = 0; count < tz->trips && !passive; count++) { | |
449 | tz->ops->get_trip_type(tz, count, &trip_type); | |
450 | ||
451 | if (trip_type == THERMAL_TRIP_PASSIVE) | |
452 | passive = 1; | |
453 | } | |
454 | ||
455 | if (!passive) | |
456 | return attr->mode; | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | static struct attribute_group thermal_zone_passive_attribute_group = { | |
462 | .attrs = thermal_zone_passive_attrs, | |
463 | .is_visible = thermal_zone_passive_is_visible, | |
464 | }; | |
465 | ||
466 | static const struct attribute_group *thermal_zone_attribute_groups[] = { | |
467 | &thermal_zone_attribute_group, | |
468 | &thermal_zone_mode_attribute_group, | |
469 | &thermal_zone_passive_attribute_group, | |
470 | /* This is not NULL terminated as we create the group dynamically */ | |
471 | }; | |
472 | ||
473 | /** | |
474 | * create_trip_attrs() - create attributes for trip points | |
475 | * @tz: the thermal zone device | |
476 | * @mask: Writeable trip point bitmap. | |
477 | * | |
478 | * helper function to instantiate sysfs entries for every trip | |
479 | * point and its properties of a struct thermal_zone_device. | |
480 | * | |
481 | * Return: 0 on success, the proper error value otherwise. | |
482 | */ | |
483 | static int create_trip_attrs(struct thermal_zone_device *tz, int mask) | |
484 | { | |
a369ee88 EV |
485 | struct attribute **attrs; |
486 | int indx; | |
487 | ||
488 | /* This function works only for zones with at least one trip */ | |
489 | if (tz->trips <= 0) | |
490 | return -EINVAL; | |
491 | ||
b819dc9e EV |
492 | tz->trip_type_attrs = kcalloc(tz->trips, sizeof(*tz->trip_type_attrs), |
493 | GFP_KERNEL); | |
a369ee88 EV |
494 | if (!tz->trip_type_attrs) |
495 | return -ENOMEM; | |
496 | ||
b819dc9e EV |
497 | tz->trip_temp_attrs = kcalloc(tz->trips, sizeof(*tz->trip_temp_attrs), |
498 | GFP_KERNEL); | |
a369ee88 EV |
499 | if (!tz->trip_temp_attrs) { |
500 | kfree(tz->trip_type_attrs); | |
501 | return -ENOMEM; | |
502 | } | |
503 | ||
504 | if (tz->ops->get_trip_hyst) { | |
b819dc9e EV |
505 | tz->trip_hyst_attrs = kcalloc(tz->trips, |
506 | sizeof(*tz->trip_hyst_attrs), | |
507 | GFP_KERNEL); | |
a369ee88 EV |
508 | if (!tz->trip_hyst_attrs) { |
509 | kfree(tz->trip_type_attrs); | |
510 | kfree(tz->trip_temp_attrs); | |
511 | return -ENOMEM; | |
512 | } | |
513 | } | |
514 | ||
b819dc9e | 515 | attrs = kcalloc(tz->trips * 3 + 1, sizeof(*attrs), GFP_KERNEL); |
a369ee88 EV |
516 | if (!attrs) { |
517 | kfree(tz->trip_type_attrs); | |
518 | kfree(tz->trip_temp_attrs); | |
519 | if (tz->ops->get_trip_hyst) | |
520 | kfree(tz->trip_hyst_attrs); | |
521 | return -ENOMEM; | |
522 | } | |
523 | ||
524 | for (indx = 0; indx < tz->trips; indx++) { | |
525 | /* create trip type attribute */ | |
526 | snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH, | |
527 | "trip_point_%d_type", indx); | |
528 | ||
529 | sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr); | |
530 | tz->trip_type_attrs[indx].attr.attr.name = | |
531 | tz->trip_type_attrs[indx].name; | |
532 | tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO; | |
533 | tz->trip_type_attrs[indx].attr.show = trip_point_type_show; | |
534 | attrs[indx] = &tz->trip_type_attrs[indx].attr.attr; | |
535 | ||
536 | /* create trip temp attribute */ | |
537 | snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH, | |
538 | "trip_point_%d_temp", indx); | |
539 | ||
540 | sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr); | |
541 | tz->trip_temp_attrs[indx].attr.attr.name = | |
542 | tz->trip_temp_attrs[indx].name; | |
543 | tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO; | |
544 | tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show; | |
545 | if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) && | |
546 | mask & (1 << indx)) { | |
547 | tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR; | |
548 | tz->trip_temp_attrs[indx].attr.store = | |
549 | trip_point_temp_store; | |
550 | } | |
551 | attrs[indx + tz->trips] = &tz->trip_temp_attrs[indx].attr.attr; | |
552 | ||
553 | /* create Optional trip hyst attribute */ | |
554 | if (!tz->ops->get_trip_hyst) | |
555 | continue; | |
556 | snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH, | |
557 | "trip_point_%d_hyst", indx); | |
558 | ||
559 | sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr); | |
560 | tz->trip_hyst_attrs[indx].attr.attr.name = | |
561 | tz->trip_hyst_attrs[indx].name; | |
562 | tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO; | |
563 | tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show; | |
564 | if (tz->ops->set_trip_hyst) { | |
565 | tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR; | |
566 | tz->trip_hyst_attrs[indx].attr.store = | |
567 | trip_point_hyst_store; | |
568 | } | |
569 | attrs[indx + tz->trips * 2] = | |
570 | &tz->trip_hyst_attrs[indx].attr.attr; | |
571 | } | |
572 | attrs[tz->trips * 3] = NULL; | |
573 | ||
574 | tz->trips_attribute_group.attrs = attrs; | |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
32fa5ba3 CJ |
579 | /** |
580 | * destroy_trip_attrs() - destroy attributes for trip points | |
581 | * @tz: the thermal zone device | |
582 | * | |
583 | * helper function to free resources allocated by create_trip_attrs() | |
584 | */ | |
585 | static void destroy_trip_attrs(struct thermal_zone_device *tz) | |
586 | { | |
587 | if (!tz) | |
588 | return; | |
589 | ||
590 | kfree(tz->trip_type_attrs); | |
591 | kfree(tz->trip_temp_attrs); | |
592 | if (tz->ops->get_trip_hyst) | |
593 | kfree(tz->trip_hyst_attrs); | |
594 | kfree(tz->trips_attribute_group.attrs); | |
595 | } | |
596 | ||
a369ee88 EV |
597 | int thermal_zone_create_device_groups(struct thermal_zone_device *tz, |
598 | int mask) | |
599 | { | |
600 | const struct attribute_group **groups; | |
601 | int i, size, result; | |
602 | ||
603 | /* we need one extra for trips and the NULL to terminate the array */ | |
604 | size = ARRAY_SIZE(thermal_zone_attribute_groups) + 2; | |
605 | /* This also takes care of API requirement to be NULL terminated */ | |
606 | groups = kcalloc(size, sizeof(*groups), GFP_KERNEL); | |
607 | if (!groups) | |
608 | return -ENOMEM; | |
609 | ||
610 | for (i = 0; i < size - 2; i++) | |
611 | groups[i] = thermal_zone_attribute_groups[i]; | |
612 | ||
613 | if (tz->trips) { | |
614 | result = create_trip_attrs(tz, mask); | |
615 | if (result) { | |
616 | kfree(groups); | |
617 | ||
618 | return result; | |
619 | } | |
620 | ||
621 | groups[size - 2] = &tz->trips_attribute_group; | |
622 | } | |
623 | ||
624 | tz->device.groups = groups; | |
625 | ||
626 | return 0; | |
627 | } | |
45cf2ec9 | 628 | |
32fa5ba3 CJ |
629 | void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz) |
630 | { | |
631 | if (!tz) | |
632 | return; | |
633 | ||
634 | if (tz->trips) | |
635 | destroy_trip_attrs(tz); | |
636 | ||
637 | kfree(tz->device.groups); | |
638 | } | |
639 | ||
45cf2ec9 EV |
640 | /* sys I/F for cooling device */ |
641 | static ssize_t | |
33e678d4 | 642 | cdev_type_show(struct device *dev, struct device_attribute *attr, char *buf) |
45cf2ec9 EV |
643 | { |
644 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
645 | ||
646 | return sprintf(buf, "%s\n", cdev->type); | |
647 | } | |
648 | ||
33e678d4 VK |
649 | static ssize_t max_state_show(struct device *dev, struct device_attribute *attr, |
650 | char *buf) | |
45cf2ec9 EV |
651 | { |
652 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
653 | unsigned long state; | |
654 | int ret; | |
655 | ||
656 | ret = cdev->ops->get_max_state(cdev, &state); | |
657 | if (ret) | |
658 | return ret; | |
659 | return sprintf(buf, "%ld\n", state); | |
660 | } | |
661 | ||
33e678d4 VK |
662 | static ssize_t cur_state_show(struct device *dev, struct device_attribute *attr, |
663 | char *buf) | |
45cf2ec9 EV |
664 | { |
665 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
666 | unsigned long state; | |
667 | int ret; | |
668 | ||
669 | ret = cdev->ops->get_cur_state(cdev, &state); | |
670 | if (ret) | |
671 | return ret; | |
672 | return sprintf(buf, "%ld\n", state); | |
673 | } | |
674 | ||
675 | static ssize_t | |
33e678d4 VK |
676 | cur_state_store(struct device *dev, struct device_attribute *attr, |
677 | const char *buf, size_t count) | |
45cf2ec9 EV |
678 | { |
679 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
680 | unsigned long state; | |
681 | int result; | |
682 | ||
683 | if (sscanf(buf, "%ld\n", &state) != 1) | |
684 | return -EINVAL; | |
685 | ||
686 | if ((long)state < 0) | |
687 | return -EINVAL; | |
688 | ||
68000a0d TG |
689 | mutex_lock(&cdev->lock); |
690 | ||
45cf2ec9 | 691 | result = cdev->ops->set_cur_state(cdev, state); |
68000a0d TG |
692 | if (!result) |
693 | thermal_cooling_device_stats_update(cdev, state); | |
694 | ||
695 | mutex_unlock(&cdev->lock); | |
696 | return result ? result : count; | |
45cf2ec9 EV |
697 | } |
698 | ||
33e678d4 VK |
699 | static struct device_attribute |
700 | dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL); | |
e76a4386 VK |
701 | static DEVICE_ATTR_RO(max_state); |
702 | static DEVICE_ATTR_RW(cur_state); | |
45cf2ec9 EV |
703 | |
704 | static struct attribute *cooling_device_attrs[] = { | |
705 | &dev_attr_cdev_type.attr, | |
706 | &dev_attr_max_state.attr, | |
707 | &dev_attr_cur_state.attr, | |
708 | NULL, | |
709 | }; | |
710 | ||
711 | static const struct attribute_group cooling_device_attr_group = { | |
712 | .attrs = cooling_device_attrs, | |
713 | }; | |
714 | ||
715 | static const struct attribute_group *cooling_device_attr_groups[] = { | |
716 | &cooling_device_attr_group, | |
8ea22951 | 717 | NULL, /* Space allocated for cooling_device_stats_attr_group */ |
45cf2ec9 EV |
718 | NULL, |
719 | }; | |
720 | ||
8ea22951 VK |
721 | #ifdef CONFIG_THERMAL_STATISTICS |
722 | struct cooling_dev_stats { | |
723 | spinlock_t lock; | |
724 | unsigned int total_trans; | |
725 | unsigned long state; | |
726 | unsigned long max_states; | |
727 | ktime_t last_time; | |
728 | ktime_t *time_in_state; | |
729 | unsigned int *trans_table; | |
730 | }; | |
731 | ||
732 | static void update_time_in_state(struct cooling_dev_stats *stats) | |
733 | { | |
734 | ktime_t now = ktime_get(), delta; | |
735 | ||
736 | delta = ktime_sub(now, stats->last_time); | |
737 | stats->time_in_state[stats->state] = | |
738 | ktime_add(stats->time_in_state[stats->state], delta); | |
739 | stats->last_time = now; | |
740 | } | |
741 | ||
742 | void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, | |
743 | unsigned long new_state) | |
744 | { | |
745 | struct cooling_dev_stats *stats = cdev->stats; | |
746 | ||
747 | spin_lock(&stats->lock); | |
748 | ||
749 | if (stats->state == new_state) | |
750 | goto unlock; | |
751 | ||
752 | update_time_in_state(stats); | |
753 | stats->trans_table[stats->state * stats->max_states + new_state]++; | |
754 | stats->state = new_state; | |
755 | stats->total_trans++; | |
756 | ||
757 | unlock: | |
758 | spin_unlock(&stats->lock); | |
759 | } | |
760 | ||
33e678d4 VK |
761 | static ssize_t total_trans_show(struct device *dev, |
762 | struct device_attribute *attr, char *buf) | |
8ea22951 VK |
763 | { |
764 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
765 | struct cooling_dev_stats *stats = cdev->stats; | |
766 | int ret; | |
767 | ||
768 | spin_lock(&stats->lock); | |
769 | ret = sprintf(buf, "%u\n", stats->total_trans); | |
770 | spin_unlock(&stats->lock); | |
771 | ||
772 | return ret; | |
773 | } | |
774 | ||
775 | static ssize_t | |
33e678d4 VK |
776 | time_in_state_ms_show(struct device *dev, struct device_attribute *attr, |
777 | char *buf) | |
8ea22951 VK |
778 | { |
779 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
780 | struct cooling_dev_stats *stats = cdev->stats; | |
781 | ssize_t len = 0; | |
782 | int i; | |
783 | ||
784 | spin_lock(&stats->lock); | |
785 | update_time_in_state(stats); | |
786 | ||
787 | for (i = 0; i < stats->max_states; i++) { | |
788 | len += sprintf(buf + len, "state%u\t%llu\n", i, | |
789 | ktime_to_ms(stats->time_in_state[i])); | |
790 | } | |
791 | spin_unlock(&stats->lock); | |
792 | ||
793 | return len; | |
794 | } | |
795 | ||
796 | static ssize_t | |
33e678d4 VK |
797 | reset_store(struct device *dev, struct device_attribute *attr, const char *buf, |
798 | size_t count) | |
8ea22951 VK |
799 | { |
800 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
801 | struct cooling_dev_stats *stats = cdev->stats; | |
802 | int i, states = stats->max_states; | |
803 | ||
804 | spin_lock(&stats->lock); | |
805 | ||
806 | stats->total_trans = 0; | |
807 | stats->last_time = ktime_get(); | |
808 | memset(stats->trans_table, 0, | |
809 | states * states * sizeof(*stats->trans_table)); | |
810 | ||
811 | for (i = 0; i < stats->max_states; i++) | |
812 | stats->time_in_state[i] = ktime_set(0, 0); | |
813 | ||
814 | spin_unlock(&stats->lock); | |
815 | ||
816 | return count; | |
817 | } | |
818 | ||
33e678d4 VK |
819 | static ssize_t trans_table_show(struct device *dev, |
820 | struct device_attribute *attr, char *buf) | |
8ea22951 VK |
821 | { |
822 | struct thermal_cooling_device *cdev = to_cooling_device(dev); | |
823 | struct cooling_dev_stats *stats = cdev->stats; | |
824 | ssize_t len = 0; | |
825 | int i, j; | |
826 | ||
827 | len += snprintf(buf + len, PAGE_SIZE - len, " From : To\n"); | |
828 | len += snprintf(buf + len, PAGE_SIZE - len, " : "); | |
829 | for (i = 0; i < stats->max_states; i++) { | |
830 | if (len >= PAGE_SIZE) | |
831 | break; | |
832 | len += snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i); | |
833 | } | |
834 | if (len >= PAGE_SIZE) | |
835 | return PAGE_SIZE; | |
836 | ||
837 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); | |
838 | ||
839 | for (i = 0; i < stats->max_states; i++) { | |
840 | if (len >= PAGE_SIZE) | |
841 | break; | |
842 | ||
843 | len += snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i); | |
844 | ||
845 | for (j = 0; j < stats->max_states; j++) { | |
846 | if (len >= PAGE_SIZE) | |
847 | break; | |
848 | len += snprintf(buf + len, PAGE_SIZE - len, "%8u ", | |
849 | stats->trans_table[i * stats->max_states + j]); | |
850 | } | |
851 | if (len >= PAGE_SIZE) | |
852 | break; | |
853 | len += snprintf(buf + len, PAGE_SIZE - len, "\n"); | |
854 | } | |
855 | ||
856 | if (len >= PAGE_SIZE) { | |
857 | pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n"); | |
858 | return -EFBIG; | |
859 | } | |
860 | return len; | |
861 | } | |
862 | ||
e76a4386 VK |
863 | static DEVICE_ATTR_RO(total_trans); |
864 | static DEVICE_ATTR_RO(time_in_state_ms); | |
865 | static DEVICE_ATTR_WO(reset); | |
866 | static DEVICE_ATTR_RO(trans_table); | |
8ea22951 VK |
867 | |
868 | static struct attribute *cooling_device_stats_attrs[] = { | |
869 | &dev_attr_total_trans.attr, | |
870 | &dev_attr_time_in_state_ms.attr, | |
871 | &dev_attr_reset.attr, | |
872 | &dev_attr_trans_table.attr, | |
873 | NULL | |
874 | }; | |
875 | ||
876 | static const struct attribute_group cooling_device_stats_attr_group = { | |
877 | .attrs = cooling_device_stats_attrs, | |
878 | .name = "stats" | |
879 | }; | |
880 | ||
881 | static void cooling_device_stats_setup(struct thermal_cooling_device *cdev) | |
882 | { | |
883 | struct cooling_dev_stats *stats; | |
884 | unsigned long states; | |
885 | int var; | |
886 | ||
887 | if (cdev->ops->get_max_state(cdev, &states)) | |
888 | return; | |
889 | ||
890 | states++; /* Total number of states is highest state + 1 */ | |
891 | ||
892 | var = sizeof(*stats); | |
893 | var += sizeof(*stats->time_in_state) * states; | |
894 | var += sizeof(*stats->trans_table) * states * states; | |
895 | ||
896 | stats = kzalloc(var, GFP_KERNEL); | |
897 | if (!stats) | |
898 | return; | |
899 | ||
900 | stats->time_in_state = (ktime_t *)(stats + 1); | |
901 | stats->trans_table = (unsigned int *)(stats->time_in_state + states); | |
902 | cdev->stats = stats; | |
903 | stats->last_time = ktime_get(); | |
904 | stats->max_states = states; | |
905 | ||
906 | spin_lock_init(&stats->lock); | |
907 | ||
908 | /* Fill the empty slot left in cooling_device_attr_groups */ | |
909 | var = ARRAY_SIZE(cooling_device_attr_groups) - 2; | |
910 | cooling_device_attr_groups[var] = &cooling_device_stats_attr_group; | |
911 | } | |
912 | ||
913 | static void cooling_device_stats_destroy(struct thermal_cooling_device *cdev) | |
914 | { | |
915 | kfree(cdev->stats); | |
916 | cdev->stats = NULL; | |
917 | } | |
918 | ||
919 | #else | |
920 | ||
921 | static inline void | |
922 | cooling_device_stats_setup(struct thermal_cooling_device *cdev) {} | |
923 | static inline void | |
924 | cooling_device_stats_destroy(struct thermal_cooling_device *cdev) {} | |
925 | ||
926 | #endif /* CONFIG_THERMAL_STATISTICS */ | |
927 | ||
45cf2ec9 EV |
928 | void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev) |
929 | { | |
8ea22951 | 930 | cooling_device_stats_setup(cdev); |
45cf2ec9 EV |
931 | cdev->device.groups = cooling_device_attr_groups; |
932 | } | |
933 | ||
8ea22951 VK |
934 | void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev) |
935 | { | |
936 | cooling_device_stats_destroy(cdev); | |
937 | } | |
938 | ||
45cf2ec9 EV |
939 | /* these helper will be used only at the time of bindig */ |
940 | ssize_t | |
33e678d4 | 941 | trip_point_show(struct device *dev, struct device_attribute *attr, char *buf) |
45cf2ec9 EV |
942 | { |
943 | struct thermal_instance *instance; | |
944 | ||
945 | instance = | |
946 | container_of(attr, struct thermal_instance, attr); | |
947 | ||
948 | if (instance->trip == THERMAL_TRIPS_NONE) | |
949 | return sprintf(buf, "-1\n"); | |
950 | else | |
951 | return sprintf(buf, "%d\n", instance->trip); | |
952 | } | |
953 | ||
954 | ssize_t | |
33e678d4 | 955 | weight_show(struct device *dev, struct device_attribute *attr, char *buf) |
45cf2ec9 EV |
956 | { |
957 | struct thermal_instance *instance; | |
958 | ||
959 | instance = container_of(attr, struct thermal_instance, weight_attr); | |
960 | ||
961 | return sprintf(buf, "%d\n", instance->weight); | |
962 | } | |
963 | ||
33e678d4 VK |
964 | ssize_t weight_store(struct device *dev, struct device_attribute *attr, |
965 | const char *buf, size_t count) | |
45cf2ec9 EV |
966 | { |
967 | struct thermal_instance *instance; | |
968 | int ret, weight; | |
969 | ||
970 | ret = kstrtoint(buf, 0, &weight); | |
971 | if (ret) | |
972 | return ret; | |
973 | ||
974 | instance = container_of(attr, struct thermal_instance, weight_attr); | |
975 | instance->weight = weight; | |
976 | ||
977 | return count; | |
978 | } |