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