Linux Audio

Check our new training course

Linux kernel drivers training

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