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