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