10015d9a2SAmit Kucheria // SPDX-License-Identifier: GPL-2.0-only
20015d9a2SAmit Kucheria /*
30015d9a2SAmit Kucheria  *  user_space.c - A simple user space Thermal events notifier
40015d9a2SAmit Kucheria  *
50015d9a2SAmit Kucheria  *  Copyright (C) 2012 Intel Corp
60015d9a2SAmit Kucheria  *  Copyright (C) 2012 Durgadoss R <durgadoss.r@intel.com>
70015d9a2SAmit Kucheria  *
80015d9a2SAmit Kucheria  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90015d9a2SAmit Kucheria  *
100015d9a2SAmit Kucheria  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110015d9a2SAmit Kucheria  */
120015d9a2SAmit Kucheria 
130015d9a2SAmit Kucheria #include <linux/slab.h>
140015d9a2SAmit Kucheria #include <linux/thermal.h>
150015d9a2SAmit Kucheria 
160015d9a2SAmit Kucheria #include "thermal_core.h"
170015d9a2SAmit Kucheria 
user_space_bind(struct thermal_zone_device * tz)180275c9fbSDaniel Lezcano static int user_space_bind(struct thermal_zone_device *tz)
190275c9fbSDaniel Lezcano {
20fa1ef24aSDaniel Lezcano 	pr_info_once("Consider using thermal netlink events interface\n");
210275c9fbSDaniel Lezcano 
220275c9fbSDaniel Lezcano 	return 0;
230275c9fbSDaniel Lezcano }
240275c9fbSDaniel Lezcano 
250015d9a2SAmit Kucheria /**
260015d9a2SAmit Kucheria  * notify_user_space - Notifies user space about thermal events
270015d9a2SAmit Kucheria  * @tz: thermal_zone_device
280015d9a2SAmit Kucheria  * @trip: trip point index
290015d9a2SAmit Kucheria  *
300015d9a2SAmit Kucheria  * This function notifies the user space through UEvents.
310015d9a2SAmit Kucheria  */
notify_user_space(struct thermal_zone_device * tz,int trip)320015d9a2SAmit Kucheria static int notify_user_space(struct thermal_zone_device *tz, int trip)
330015d9a2SAmit Kucheria {
340015d9a2SAmit Kucheria 	char *thermal_prop[5];
350015d9a2SAmit Kucheria 	int i;
360015d9a2SAmit Kucheria 
37*72c976fcSRafael J. Wysocki 	lockdep_assert_held(&tz->lock);
38*72c976fcSRafael J. Wysocki 
390015d9a2SAmit Kucheria 	thermal_prop[0] = kasprintf(GFP_KERNEL, "NAME=%s", tz->type);
400015d9a2SAmit Kucheria 	thermal_prop[1] = kasprintf(GFP_KERNEL, "TEMP=%d", tz->temperature);
410015d9a2SAmit Kucheria 	thermal_prop[2] = kasprintf(GFP_KERNEL, "TRIP=%d", trip);
420015d9a2SAmit Kucheria 	thermal_prop[3] = kasprintf(GFP_KERNEL, "EVENT=%d", tz->notify_event);
430015d9a2SAmit Kucheria 	thermal_prop[4] = NULL;
440015d9a2SAmit Kucheria 	kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, thermal_prop);
450015d9a2SAmit Kucheria 	for (i = 0; i < 4; ++i)
460015d9a2SAmit Kucheria 		kfree(thermal_prop[i]);
47*72c976fcSRafael J. Wysocki 
480015d9a2SAmit Kucheria 	return 0;
490015d9a2SAmit Kucheria }
500015d9a2SAmit Kucheria 
510015d9a2SAmit Kucheria static struct thermal_governor thermal_gov_user_space = {
520015d9a2SAmit Kucheria 	.name		= "user_space",
530015d9a2SAmit Kucheria 	.throttle	= notify_user_space,
540275c9fbSDaniel Lezcano 	.bind_to_tz	= user_space_bind,
550015d9a2SAmit Kucheria };
560015d9a2SAmit Kucheria THERMAL_GOVERNOR_DECLARE(thermal_gov_user_space);
57