blob: 655591316a881274a6d152801ffd19138a0dc34b [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
2 * thermal.c - Generic Thermal Management Sysfs support.
3 *
4 * Copyright (C) 2008 Intel Corp
5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
7 *
Zhang Rui203d3d42008-01-17 15:51:08 +08008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
Zhang Rui203d3d42008-01-17 15:51:08 +080011 */
12
Joe Perchesc5a01dd2012-03-21 12:55:02 -070013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Zhang Rui203d3d42008-01-17 15:51:08 +080015#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080019#include <linux/kdev_t.h>
20#include <linux/idr.h>
21#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000022#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000023#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040024#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053025#include <net/netlink.h>
26#include <net/genetlink.h>
Zhang Ruiff140fe2015-10-30 16:31:58 +080027#include <linux/suspend.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080028
Punit Agrawal100a8fd2014-07-29 11:50:48 +010029#define CREATE_TRACE_POINTS
30#include <trace/events/thermal.h>
31
Durgadoss R71350db2012-09-18 11:04:53 +053032#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040033#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053034
Zhang Rui63c4ec92008-04-21 16:07:13 +080035MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080036MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000037MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080038
Zhang Rui203d3d42008-01-17 15:51:08 +080039static DEFINE_IDR(thermal_tz_idr);
40static DEFINE_IDR(thermal_cdev_idr);
41static DEFINE_MUTEX(thermal_idr_lock);
42
43static LIST_HEAD(thermal_tz_list);
44static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053045static LIST_HEAD(thermal_governor_list);
46
Zhang Rui203d3d42008-01-17 15:51:08 +080047static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053048static DEFINE_MUTEX(thermal_governor_lock);
49
Zhang Ruiff140fe2015-10-30 16:31:58 +080050static atomic_t in_suspend;
51
Zhang Ruif2234bc2014-01-24 10:23:19 +080052static struct thermal_governor *def_governor;
53
Eduardo Valentin1b4f4842016-11-07 21:09:05 -080054/*
55 * Governor section: set of functions to handle thermal governors
56 *
57 * Functions to help in the life cycle of thermal governors within
58 * the thermal core and by the thermal governor code.
59 */
60
Durgadoss Ra4a15482012-09-18 11:04:57 +053061static struct thermal_governor *__find_governor(const char *name)
62{
63 struct thermal_governor *pos;
64
Zhang Ruif2234bc2014-01-24 10:23:19 +080065 if (!name || !name[0])
66 return def_governor;
67
Durgadoss Ra4a15482012-09-18 11:04:57 +053068 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070069 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053070 return pos;
71
72 return NULL;
73}
74
Javi Merinoe33df1d2015-02-26 19:00:27 +000075/**
76 * bind_previous_governor() - bind the previous governor of the thermal zone
77 * @tz: a valid pointer to a struct thermal_zone_device
78 * @failed_gov_name: the name of the governor that failed to register
79 *
80 * Register the previous governor of the thermal zone after a new
81 * governor has failed to be bound.
82 */
83static void bind_previous_governor(struct thermal_zone_device *tz,
84 const char *failed_gov_name)
85{
86 if (tz->governor && tz->governor->bind_to_tz) {
87 if (tz->governor->bind_to_tz(tz)) {
88 dev_err(&tz->device,
89 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
90 failed_gov_name, tz->governor->name, tz->type);
91 tz->governor = NULL;
92 }
93 }
94}
95
96/**
97 * thermal_set_governor() - Switch to another governor
98 * @tz: a valid pointer to a struct thermal_zone_device
99 * @new_gov: pointer to the new governor
100 *
101 * Change the governor of thermal zone @tz.
102 *
103 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
104 */
105static int thermal_set_governor(struct thermal_zone_device *tz,
106 struct thermal_governor *new_gov)
107{
108 int ret = 0;
109
110 if (tz->governor && tz->governor->unbind_from_tz)
111 tz->governor->unbind_from_tz(tz);
112
113 if (new_gov && new_gov->bind_to_tz) {
114 ret = new_gov->bind_to_tz(tz);
115 if (ret) {
116 bind_previous_governor(tz, new_gov->name);
117
118 return ret;
119 }
120 }
121
122 tz->governor = new_gov;
123
124 return ret;
125}
126
Durgadoss Ra4a15482012-09-18 11:04:57 +0530127int thermal_register_governor(struct thermal_governor *governor)
128{
129 int err;
130 const char *name;
131 struct thermal_zone_device *pos;
132
133 if (!governor)
134 return -EINVAL;
135
136 mutex_lock(&thermal_governor_lock);
137
138 err = -EBUSY;
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800139 if (!__find_governor(governor->name)) {
Eduardo Valentineb7be322016-11-07 21:09:21 -0800140 bool match_default;
141
Durgadoss Ra4a15482012-09-18 11:04:57 +0530142 err = 0;
143 list_add(&governor->governor_list, &thermal_governor_list);
Eduardo Valentineb7be322016-11-07 21:09:21 -0800144 match_default = !strncmp(governor->name,
145 DEFAULT_THERMAL_GOVERNOR,
146 THERMAL_NAME_LENGTH);
147
148 if (!def_governor && match_default)
Zhang Ruif2234bc2014-01-24 10:23:19 +0800149 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530150 }
151
152 mutex_lock(&thermal_list_lock);
153
154 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800155 /*
156 * only thermal zones with specified tz->tzp->governor_name
157 * may run with tz->govenor unset
158 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530159 if (pos->governor)
160 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800161
162 name = pos->tzp->governor_name;
163
Javi Merinoe33df1d2015-02-26 19:00:27 +0000164 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
165 int ret;
166
167 ret = thermal_set_governor(pos, governor);
168 if (ret)
169 dev_err(&pos->device,
170 "Failed to set governor %s for thermal zone %s: %d\n",
171 governor->name, pos->type, ret);
172 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530173 }
174
175 mutex_unlock(&thermal_list_lock);
176 mutex_unlock(&thermal_governor_lock);
177
178 return err;
179}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530180
181void thermal_unregister_governor(struct thermal_governor *governor)
182{
183 struct thermal_zone_device *pos;
184
185 if (!governor)
186 return;
187
188 mutex_lock(&thermal_governor_lock);
189
Eduardo Valentin5027ba32016-11-07 21:09:20 -0800190 if (!__find_governor(governor->name))
Durgadoss Ra4a15482012-09-18 11:04:57 +0530191 goto exit;
192
193 mutex_lock(&thermal_list_lock);
194
195 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700196 if (!strncasecmp(pos->governor->name, governor->name,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800197 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000198 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530199 }
200
201 mutex_unlock(&thermal_list_lock);
202 list_del(&governor->governor_list);
203exit:
204 mutex_unlock(&thermal_governor_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530205}
Zhang Rui203d3d42008-01-17 15:51:08 +0800206
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800207int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
208 char *policy)
Zhang Rui203d3d42008-01-17 15:51:08 +0800209{
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800210 struct thermal_governor *gov;
211 int ret = -EINVAL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800212
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800213 mutex_lock(&thermal_governor_lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530214 mutex_lock(&tz->lock);
Durgadoss R9b4298a2012-09-18 11:04:54 +0530215
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800216 gov = __find_governor(strim(policy));
217 if (!gov)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530218 goto exit;
219
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800220 ret = thermal_set_governor(tz, gov);
221
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530222exit:
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800223 mutex_unlock(&tz->lock);
224 mutex_unlock(&thermal_governor_lock);
225
226 return ret;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530227}
228
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800229int thermal_build_list_of_policies(char *buf)
230{
231 struct thermal_governor *pos;
232 ssize_t count = 0;
233 ssize_t size = PAGE_SIZE;
234
235 mutex_lock(&thermal_governor_lock);
236
237 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
238 size = PAGE_SIZE - count;
239 count += scnprintf(buf + count, size, "%s ", pos->name);
240 }
241 count += scnprintf(buf + count, size, "\n");
242
243 mutex_unlock(&thermal_governor_lock);
244
245 return count;
246}
247
248static int __init thermal_register_governors(void)
249{
250 int result;
251
252 result = thermal_gov_step_wise_register();
253 if (result)
254 return result;
255
256 result = thermal_gov_fair_share_register();
257 if (result)
258 return result;
259
260 result = thermal_gov_bang_bang_register();
261 if (result)
262 return result;
263
264 result = thermal_gov_user_space_register();
265 if (result)
266 return result;
267
268 return thermal_gov_power_allocator_register();
269}
270
271static void thermal_unregister_governors(void)
272{
273 thermal_gov_step_wise_unregister();
274 thermal_gov_fair_share_unregister();
275 thermal_gov_bang_bang_unregister();
276 thermal_gov_user_space_unregister();
277 thermal_gov_power_allocator_unregister();
278}
279
Eduardo Valentin8772e182016-11-07 21:09:15 -0800280/*
281 * Zone update section: main control loop applied to each zone while monitoring
282 *
283 * in polling mode. The monitoring is done using a workqueue.
284 * Same update may be done on a zone by calling thermal_zone_device_update().
285 *
286 * An update means:
287 * - Non-critical trips will invoke the governor responsible for that zone;
288 * - Hot trips will produce a notification to userspace;
289 * - Critical trip point will cause a system shutdown.
290 */
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530291static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
292 int delay)
293{
294 if (delay > 1000)
295 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
296 round_jiffies(msecs_to_jiffies(delay)));
297 else if (delay)
298 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
299 msecs_to_jiffies(delay));
300 else
301 cancel_delayed_work(&tz->poll_queue);
302}
303
304static void monitor_thermal_zone(struct thermal_zone_device *tz)
305{
306 mutex_lock(&tz->lock);
307
308 if (tz->passive)
309 thermal_zone_device_set_polling(tz, tz->passive_delay);
310 else if (tz->polling_delay)
311 thermal_zone_device_set_polling(tz, tz->polling_delay);
312 else
313 thermal_zone_device_set_polling(tz, 0);
314
315 mutex_unlock(&tz->lock);
316}
317
318static void handle_non_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800319 int trip,
320 enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530321{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800322 tz->governor ? tz->governor->throttle(tz, trip) :
323 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530324}
325
326static void handle_critical_trips(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -0800327 int trip, enum thermal_trip_type trip_type)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530328{
Sascha Hauer17e83512015-07-24 08:12:54 +0200329 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530330
331 tz->ops->get_trip_temp(tz, trip, &trip_temp);
332
333 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800334 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530335 return;
336
Punit Agrawal208cd822014-07-29 11:50:50 +0100337 trace_thermal_zone_trip(tz, trip, trip_type);
338
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530339 if (tz->ops->notify)
340 tz->ops->notify(tz, trip, trip_type);
341
342 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000343 dev_emerg(&tz->device,
344 "critical temperature reached(%d C),shutting down\n",
345 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530346 orderly_poweroff(true);
347 }
348}
349
350static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
351{
352 enum thermal_trip_type type;
353
Zhang Rui81ad4272016-03-18 10:03:24 +0800354 /* Ignore disabled trip points */
355 if (test_bit(trip, &tz->trips_disabled))
356 return;
357
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530358 tz->ops->get_trip_type(tz, trip, &type);
359
360 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
361 handle_critical_trips(tz, trip, type);
362 else
363 handle_non_critical_trips(tz, trip, type);
364 /*
365 * Alright, we handled this trip successfully.
366 * So, start monitoring again.
367 */
368 monitor_thermal_zone(tz);
369}
370
371static void update_temperature(struct thermal_zone_device *tz)
372{
Sascha Hauer17e83512015-07-24 08:12:54 +0200373 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530374
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000375 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530376 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100377 if (ret != -EAGAIN)
378 dev_warn(&tz->device,
379 "failed to read out thermal zone (%d)\n",
380 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000381 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530382 }
383
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000384 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530385 tz->last_temperature = tz->temperature;
386 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530387 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800388
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100389 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800390 if (tz->last_temperature == THERMAL_TEMP_INVALID)
391 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
392 tz->temperature);
393 else
394 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
395 tz->last_temperature, tz->temperature);
396}
397
398static void thermal_zone_device_reset(struct thermal_zone_device *tz)
399{
400 struct thermal_instance *pos;
401
402 tz->temperature = THERMAL_TEMP_INVALID;
403 tz->passive = 0;
404 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
405 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530406}
407
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700408void thermal_zone_device_update(struct thermal_zone_device *tz,
409 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530410{
411 int count;
412
Zhang Ruiff140fe2015-10-30 16:31:58 +0800413 if (atomic_read(&in_suspend))
414 return;
415
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400416 if (!tz->ops->get_temp)
417 return;
418
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530419 update_temperature(tz);
420
Sascha Hauer060c0342016-06-22 16:42:01 +0800421 thermal_zone_set_trips(tz);
422
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700423 tz->notify_event = event;
424
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530425 for (count = 0; count < tz->trips; count++)
426 handle_thermal_trip(tz, count);
427}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000428EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530429
Eduardo Valentin106339a2016-11-07 21:09:14 -0800430/**
431 * thermal_notify_framework - Sensor drivers use this API to notify framework
432 * @tz: thermal zone device
433 * @trip: indicates which trip point has been crossed
434 *
435 * This function handles the trip events from sensor drivers. It starts
436 * throttling the cooling devices according to the policy configured.
437 * For CRITICAL and HOT trip points, this notifies the respective drivers,
438 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
439 * The throttling policy is based on the configured platform data; if no
440 * platform data is provided, this uses the step_wise throttling policy.
441 */
442void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
443{
444 handle_thermal_trip(tz, trip);
445}
446EXPORT_SYMBOL_GPL(thermal_notify_framework);
447
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530448static void thermal_zone_device_check(struct work_struct *work)
449{
450 struct thermal_zone_device *tz = container_of(work, struct
451 thermal_zone_device,
452 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700453 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530454}
455
Eduardo Valentin712afbd2016-11-07 21:09:16 -0800456/*
457 * Power actor section: interface to power actors to estimate power
458 *
459 * Set of functions used to interact to cooling devices that know
460 * how to estimate their devices power consumption.
461 */
Javi Merino9f382712015-03-26 15:53:02 +0000462
Javi Merino35b11d22015-02-26 19:00:28 +0000463/**
464 * power_actor_get_max_power() - get the maximum power that a cdev can consume
465 * @cdev: pointer to &thermal_cooling_device
466 * @tz: a valid thermal zone device pointer
467 * @max_power: pointer in which to store the maximum power
468 *
469 * Calculate the maximum power consumption in milliwats that the
470 * cooling device can currently consume and store it in @max_power.
471 *
472 * Return: 0 on success, -EINVAL if @cdev doesn't support the
473 * power_actor API or -E* on other error.
474 */
475int power_actor_get_max_power(struct thermal_cooling_device *cdev,
476 struct thermal_zone_device *tz, u32 *max_power)
477{
478 if (!cdev_is_power_actor(cdev))
479 return -EINVAL;
480
481 return cdev->ops->state2power(cdev, tz, 0, max_power);
482}
483
484/**
Javi Merinoc973c3b2015-09-14 14:23:50 +0100485 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
486 * @cdev: pointer to &thermal_cooling_device
487 * @tz: a valid thermal zone device pointer
488 * @min_power: pointer in which to store the minimum power
489 *
490 * Calculate the minimum power consumption in milliwatts that the
491 * cooling device can currently consume and store it in @min_power.
492 *
493 * Return: 0 on success, -EINVAL if @cdev doesn't support the
494 * power_actor API or -E* on other error.
495 */
496int power_actor_get_min_power(struct thermal_cooling_device *cdev,
497 struct thermal_zone_device *tz, u32 *min_power)
498{
499 unsigned long max_state;
500 int ret;
501
502 if (!cdev_is_power_actor(cdev))
503 return -EINVAL;
504
505 ret = cdev->ops->get_max_state(cdev, &max_state);
506 if (ret)
507 return ret;
508
509 return cdev->ops->state2power(cdev, tz, max_state, min_power);
510}
511
512/**
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800513 * power_actor_set_power() - limit the maximum power a cooling device consumes
Javi Merino35b11d22015-02-26 19:00:28 +0000514 * @cdev: pointer to &thermal_cooling_device
515 * @instance: thermal instance to update
516 * @power: the power in milliwatts
517 *
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800518 * Set the cooling device to consume at most @power milliwatts. The limit is
519 * expected to be a cap at the maximum power consumption.
Javi Merino35b11d22015-02-26 19:00:28 +0000520 *
521 * Return: 0 on success, -EINVAL if the cooling device does not
522 * implement the power actor API or -E* for other failures.
523 */
524int power_actor_set_power(struct thermal_cooling_device *cdev,
525 struct thermal_instance *instance, u32 power)
526{
527 unsigned long state;
528 int ret;
529
530 if (!cdev_is_power_actor(cdev))
531 return -EINVAL;
532
533 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
534 if (ret)
535 return ret;
536
537 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100538 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000539 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +0100540 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +0000541 thermal_cdev_update(cdev);
542
543 return 0;
544}
545
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800546void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
547 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800548{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800549 struct thermal_cooling_device *cdev = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800550
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800551 mutex_lock(&thermal_list_lock);
552 list_for_each_entry(cdev, &thermal_cdev_list, node) {
553 /* skip non matching cdevs */
554 if (strncmp(cdev_type, cdev->type, size))
555 continue;
556
557 /* re binding the exception matching the type pattern */
558 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
559 THERMAL_NO_LIMIT,
560 THERMAL_NO_LIMIT,
561 THERMAL_WEIGHT_DEFAULT);
562 }
563 mutex_unlock(&thermal_list_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800564}
565
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800566void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
567 const char *cdev_type, size_t size)
Zhang Rui203d3d42008-01-17 15:51:08 +0800568{
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800569 struct thermal_cooling_device *cdev = NULL;
570
571 mutex_lock(&thermal_list_lock);
572 list_for_each_entry(cdev, &thermal_cdev_list, node) {
573 /* skip non matching cdevs */
574 if (strncmp(cdev_type, cdev->type, size))
575 continue;
576 /* unbinding the exception matching the type pattern */
577 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
578 cdev);
579 }
580 mutex_unlock(&thermal_list_lock);
581}
582
Eduardo Valentin81193e22016-11-07 21:09:17 -0800583/*
584 * Device management section: cooling devices, zones devices, and binding
585 *
586 * Set of functions provided by the thermal core for:
587 * - cooling devices lifecycle: registration, unregistration,
588 * binding, and unbinding.
589 * - thermal zone devices lifecycle: registration, unregistration,
590 * binding, and unbinding.
591 */
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800592static int get_idr(struct idr *idr, struct mutex *lock, int *id)
593{
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000594 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800595
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800596 if (lock)
597 mutex_lock(lock);
598 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
599 if (lock)
600 mutex_unlock(lock);
601 if (unlikely(ret < 0))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000602 return ret;
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800603 *id = ret;
604 return 0;
Zhang Rui203d3d42008-01-17 15:51:08 +0800605}
606
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800607static void release_idr(struct idr *idr, struct mutex *lock, int id)
Zhang Rui203d3d42008-01-17 15:51:08 +0800608{
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800609 if (lock)
610 mutex_lock(lock);
611 idr_remove(idr, id);
612 if (lock)
613 mutex_unlock(lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800614}
615
Zhang Rui203d3d42008-01-17 15:51:08 +0800616/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000617 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
618 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800619 * @trip: indicates which trip point the cooling devices is
620 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000621 * @cdev: pointer to struct thermal_cooling_device
622 * @upper: the Maximum cooling state for this trip point.
623 * THERMAL_NO_LIMIT means no upper limit,
624 * and the cooling device can be in max_state.
625 * @lower: the Minimum cooling state can be used for this trip point.
626 * THERMAL_NO_LIMIT means no lower limit,
627 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000628 * @weight: The weight of the cooling device to be bound to the
629 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
630 * default value
Len Brown543a9562008-02-07 16:55:08 -0500631 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000632 * This interface function bind a thermal cooling device to the certain trip
633 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500634 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000635 *
636 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800637 */
638int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
639 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800640 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000641 unsigned long upper, unsigned long lower,
642 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800643{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800644 struct thermal_instance *dev;
645 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500646 struct thermal_zone_device *pos1;
647 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800648 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100649 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800650
Len Brown543a9562008-02-07 16:55:08 -0500651 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800652 return -EINVAL;
653
Thomas Sujithc7516702008-02-15 00:58:50 -0500654 list_for_each_entry(pos1, &thermal_tz_list, node) {
655 if (pos1 == tz)
656 break;
657 }
658 list_for_each_entry(pos2, &thermal_cdev_list, node) {
659 if (pos2 == cdev)
660 break;
661 }
662
663 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800664 return -EINVAL;
665
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100666 ret = cdev->ops->get_max_state(cdev, &max_state);
667 if (ret)
668 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800669
670 /* lower default 0, upper default max_state */
671 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
672 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
673
674 if (lower > upper || upper > max_state)
675 return -EINVAL;
676
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800677 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800678 if (!dev)
679 return -ENOMEM;
680 dev->tz = tz;
681 dev->cdev = cdev;
682 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800683 dev->upper = upper;
684 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800685 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000686 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800687
Zhang Rui203d3d42008-01-17 15:51:08 +0800688 result = get_idr(&tz->idr, &tz->lock, &dev->id);
689 if (result)
690 goto free_mem;
691
692 sprintf(dev->name, "cdev%d", dev->id);
693 result =
694 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
695 if (result)
696 goto release_idr;
697
698 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700699 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800700 dev->attr.attr.name = dev->attr_name;
701 dev->attr.attr.mode = 0444;
702 dev->attr.show = thermal_cooling_device_trip_point_show;
703 result = device_create_file(&tz->device, &dev->attr);
704 if (result)
705 goto remove_symbol_link;
706
Javi Merinodb916512015-02-18 16:04:24 +0000707 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
708 sysfs_attr_init(&dev->weight_attr.attr);
709 dev->weight_attr.attr.name = dev->weight_attr_name;
710 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
711 dev->weight_attr.show = thermal_cooling_device_weight_show;
712 dev->weight_attr.store = thermal_cooling_device_weight_store;
713 result = device_create_file(&tz->device, &dev->weight_attr);
714 if (result)
715 goto remove_trip_file;
716
Zhang Rui203d3d42008-01-17 15:51:08 +0800717 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800718 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800719 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Eduardo Valentinb659a302016-11-07 21:09:23 -0800720 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
721 result = -EEXIST;
722 break;
723 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800724 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800725 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800726 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800727 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800728 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800729 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800730 mutex_unlock(&tz->lock);
731
732 if (!result)
733 return 0;
734
Javi Merinodb916512015-02-18 16:04:24 +0000735 device_remove_file(&tz->device, &dev->weight_attr);
736remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800737 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b802012-03-21 12:55:02 -0700738remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800739 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b802012-03-21 12:55:02 -0700740release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800741 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b802012-03-21 12:55:02 -0700742free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800743 kfree(dev);
744 return result;
745}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000746EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800747
748/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000749 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
750 * thermal zone.
751 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800752 * @trip: indicates which trip point the cooling devices is
753 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000754 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500755 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000756 * This interface function unbind a thermal cooling device from the certain
757 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500758 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000759 *
760 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800761 */
762int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
763 int trip,
764 struct thermal_cooling_device *cdev)
765{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800766 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800767
768 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800769 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800770 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500771 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800772 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800773 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800774 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800775 mutex_unlock(&tz->lock);
776 goto unbind;
777 }
778 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800779 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800780 mutex_unlock(&tz->lock);
781
782 return -ENODEV;
783
Joe Perchescaca8b802012-03-21 12:55:02 -0700784unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530785 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800786 device_remove_file(&tz->device, &pos->attr);
787 sysfs_remove_link(&tz->device.kobj, pos->name);
788 release_idr(&tz->idr, &tz->lock, pos->id);
789 kfree(pos);
790 return 0;
791}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000792EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800793
794static void thermal_release(struct device *dev)
795{
796 struct thermal_zone_device *tz;
797 struct thermal_cooling_device *cdev;
798
Joe Perchescaca8b802012-03-21 12:55:02 -0700799 if (!strncmp(dev_name(dev), "thermal_zone",
800 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800801 tz = to_thermal_zone(dev);
Jacob von Chorusf53345e2016-12-30 14:07:52 -0500802 kfree(tz->trip_type_attrs);
803 kfree(tz->trip_temp_attrs);
804 kfree(tz->trip_hyst_attrs);
805 kfree(tz->trips_attribute_group.attrs);
806 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +0800807 kfree(tz);
Eduardo Valentinb659a302016-11-07 21:09:23 -0800808 } else if (!strncmp(dev_name(dev), "cooling_device",
809 sizeof("cooling_device") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800810 cdev = to_cooling_device(dev);
811 kfree(cdev);
812 }
813}
814
815static struct class thermal_class = {
816 .name = "thermal",
817 .dev_release = thermal_release,
818};
819
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800820static inline
821void print_bind_err_msg(struct thermal_zone_device *tz,
822 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800823{
824 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
825 tz->type, cdev->type, ret);
826}
827
828static void __bind(struct thermal_zone_device *tz, int mask,
829 struct thermal_cooling_device *cdev,
830 unsigned long *limits,
831 unsigned int weight)
832{
833 int i, ret;
834
835 for (i = 0; i < tz->trips; i++) {
836 if (mask & (1 << i)) {
837 unsigned long upper, lower;
838
839 upper = THERMAL_NO_LIMIT;
840 lower = THERMAL_NO_LIMIT;
841 if (limits) {
842 lower = limits[i * 2];
843 upper = limits[i * 2 + 1];
844 }
845 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
846 upper, lower,
847 weight);
848 if (ret)
849 print_bind_err_msg(tz, cdev, ret);
850 }
851 }
852}
853
Eduardo Valentin949aad82016-11-07 21:09:09 -0800854static void bind_cdev(struct thermal_cooling_device *cdev)
855{
856 int i, ret;
857 const struct thermal_zone_params *tzp;
858 struct thermal_zone_device *pos = NULL;
859
860 mutex_lock(&thermal_list_lock);
861
862 list_for_each_entry(pos, &thermal_tz_list, node) {
863 if (!pos->tzp && !pos->ops->bind)
864 continue;
865
866 if (pos->ops->bind) {
867 ret = pos->ops->bind(pos, cdev);
868 if (ret)
869 print_bind_err_msg(pos, cdev, ret);
870 continue;
871 }
872
873 tzp = pos->tzp;
874 if (!tzp || !tzp->tbp)
875 continue;
876
877 for (i = 0; i < tzp->num_tbps; i++) {
878 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
879 continue;
880 if (tzp->tbp[i].match(pos, cdev))
881 continue;
882 tzp->tbp[i].cdev = cdev;
883 __bind(pos, tzp->tbp[i].trip_mask, cdev,
884 tzp->tbp[i].binding_limits,
885 tzp->tbp[i].weight);
886 }
887 }
888
889 mutex_unlock(&thermal_list_lock);
890}
891
Zhang Rui203d3d42008-01-17 15:51:08 +0800892/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400893 * __thermal_cooling_device_register() - register a new thermal cooling device
894 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800895 * @type: the thermal cooling device type.
896 * @devdata: device private data.
897 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000898 *
899 * This interface function adds a new thermal cooling device (fan/processor/...)
900 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
901 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400902 * It also gives the opportunity to link the cooling device to a device tree
903 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000904 *
905 * Return: a pointer to the created struct thermal_cooling_device or an
906 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800907 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400908static struct thermal_cooling_device *
909__thermal_cooling_device_register(struct device_node *np,
910 char *type, void *devdata,
911 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800912{
913 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800914 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800915 int result;
916
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700917 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500918 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800919
920 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500921 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500922 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800923
Eduardo Valentin95e3ed12016-11-07 21:09:25 -0800924 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800925 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500926 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800927
928 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
929 if (result) {
930 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500931 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 }
933
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000934 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800935 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800936 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400937 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800938 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800939 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800940 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800941 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800943 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 result = device_register(&cdev->device);
945 if (result) {
946 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
947 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500948 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800949 }
950
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530951 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800952 mutex_lock(&thermal_list_lock);
953 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800954 mutex_unlock(&thermal_list_lock);
955
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530956 /* Update binding information for 'this' new cdev */
957 bind_cdev(cdev);
958
Chen Yu4511f712015-10-30 16:32:10 +0800959 mutex_lock(&thermal_list_lock);
960 list_for_each_entry(pos, &thermal_tz_list, node)
961 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700962 thermal_zone_device_update(pos,
963 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800964 mutex_unlock(&thermal_list_lock);
965
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530966 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800967}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400968
969/**
970 * thermal_cooling_device_register() - register a new thermal cooling device
971 * @type: the thermal cooling device type.
972 * @devdata: device private data.
973 * @ops: standard thermal cooling devices callbacks.
974 *
975 * This interface function adds a new thermal cooling device (fan/processor/...)
976 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
977 * to all the thermal zone devices registered at the same time.
978 *
979 * Return: a pointer to the created struct thermal_cooling_device or an
980 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
981 */
982struct thermal_cooling_device *
983thermal_cooling_device_register(char *type, void *devdata,
984 const struct thermal_cooling_device_ops *ops)
985{
986 return __thermal_cooling_device_register(NULL, type, devdata, ops);
987}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000988EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800989
990/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400991 * thermal_of_cooling_device_register() - register an OF thermal cooling device
992 * @np: a pointer to a device tree node.
993 * @type: the thermal cooling device type.
994 * @devdata: device private data.
995 * @ops: standard thermal cooling devices callbacks.
996 *
997 * This function will register a cooling device with device tree node reference.
998 * This interface function adds a new thermal cooling device (fan/processor/...)
999 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1000 * to all the thermal zone devices registered at the same time.
1001 *
1002 * Return: a pointer to the created struct thermal_cooling_device or an
1003 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1004 */
1005struct thermal_cooling_device *
1006thermal_of_cooling_device_register(struct device_node *np,
1007 char *type, void *devdata,
1008 const struct thermal_cooling_device_ops *ops)
1009{
1010 return __thermal_cooling_device_register(np, type, devdata, ops);
1011}
1012EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1013
Eduardo Valentinf11997f2016-11-07 21:09:08 -08001014static void __unbind(struct thermal_zone_device *tz, int mask,
1015 struct thermal_cooling_device *cdev)
1016{
1017 int i;
1018
1019 for (i = 0; i < tz->trips; i++)
1020 if (mask & (1 << i))
1021 thermal_zone_unbind_cooling_device(tz, i, cdev);
1022}
1023
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001024/**
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001025 * thermal_cooling_device_unregister - removes a thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 * @cdev: the thermal cooling device to remove.
1027 *
Eduardo Valentin38e7b542016-11-07 21:09:24 -08001028 * thermal_cooling_device_unregister() must be called when a registered
1029 * thermal cooling device is no longer needed.
Zhang Rui203d3d42008-01-17 15:51:08 +08001030 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301031void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001032{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301033 int i;
1034 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001035 struct thermal_zone_device *tz;
1036 struct thermal_cooling_device *pos = NULL;
1037
1038 if (!cdev)
1039 return;
1040
1041 mutex_lock(&thermal_list_lock);
1042 list_for_each_entry(pos, &thermal_cdev_list, node)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001043 if (pos == cdev)
1044 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001045 if (pos != cdev) {
1046 /* thermal cooling device not found */
1047 mutex_unlock(&thermal_list_lock);
1048 return;
1049 }
1050 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301051
1052 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001053 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301054 if (tz->ops->unbind) {
1055 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301057 }
1058
1059 if (!tz->tzp || !tz->tzp->tbp)
1060 continue;
1061
1062 tzp = tz->tzp;
1063 for (i = 0; i < tzp->num_tbps; i++) {
1064 if (tzp->tbp[i].cdev == cdev) {
1065 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1066 tzp->tbp[i].cdev = NULL;
1067 }
1068 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301070
Zhang Rui203d3d42008-01-17 15:51:08 +08001071 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301072
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1074 device_unregister(&cdev->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001075}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001076EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001077
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001078static void bind_tz(struct thermal_zone_device *tz)
Zhang Ruice119f82012-06-27 14:13:04 +08001079{
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001080 int i, ret;
1081 struct thermal_cooling_device *pos = NULL;
1082 const struct thermal_zone_params *tzp = tz->tzp;
Zhang Ruice119f82012-06-27 14:13:04 +08001083
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001084 if (!tzp && !tz->ops->bind)
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001085 return;
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001086
1087 mutex_lock(&thermal_list_lock);
1088
1089 /* If there is ops->bind, try to use ops->bind */
1090 if (tz->ops->bind) {
1091 list_for_each_entry(pos, &thermal_cdev_list, node) {
1092 ret = tz->ops->bind(tz, pos);
1093 if (ret)
1094 print_bind_err_msg(tz, pos, ret);
1095 }
1096 goto exit;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001097 }
1098
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001099 if (!tzp || !tzp->tbp)
1100 goto exit;
Zhang Ruice119f82012-06-27 14:13:04 +08001101
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001102 list_for_each_entry(pos, &thermal_cdev_list, node) {
1103 for (i = 0; i < tzp->num_tbps; i++) {
1104 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1105 continue;
1106 if (tzp->tbp[i].match(tz, pos))
1107 continue;
1108 tzp->tbp[i].cdev = pos;
1109 __bind(tz, tzp->tbp[i].trip_mask, pos,
1110 tzp->tbp[i].binding_limits,
1111 tzp->tbp[i].weight);
Durgadoss R27365a62012-07-25 10:10:59 +08001112 }
1113 }
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001114exit:
1115 mutex_unlock(&thermal_list_lock);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001116}
1117
1118/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001119 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001120 * @type: the thermal zone device type
1121 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001122 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001123 * @devdata: private device data
1124 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301125 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001126 * @passive_delay: number of milliseconds to wait between polls when
1127 * performing passive cooling
1128 * @polling_delay: number of milliseconds to wait between polls when checking
1129 * whether trip points have been crossed (0 for interrupt
1130 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001131 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001132 * This interface function adds a new thermal zone device (sensor) to
1133 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1134 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001135 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001136 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001137 *
1138 * Return: a pointer to the created struct thermal_zone_device or an
1139 * in case of error, an ERR_PTR. Caller must check return value with
1140 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001141 */
Eduardo Valentineb7be322016-11-07 21:09:21 -08001142struct thermal_zone_device *
1143thermal_zone_device_register(const char *type, int trips, int mask,
1144 void *devdata, struct thermal_zone_device_ops *ops,
1145 struct thermal_zone_params *tzp, int passive_delay,
1146 int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001147{
1148 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001149 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001150 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001151 int result;
1152 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001153 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001154
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001155 if (!type || strlen(type) == 0)
1156 return ERR_PTR(-EINVAL);
1157
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001158 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001159 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001160
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001161 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001162 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001163
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001164 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001165 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001166
Jonghwa Lee83720d02013-05-18 09:50:26 +00001167 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001168 return ERR_PTR(-EINVAL);
1169
Eduardo Valentin95e3ed12016-11-07 21:09:25 -08001170 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001171 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001172 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001173
Zhang Rui2d374132012-06-27 10:09:00 +08001174 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 idr_init(&tz->idr);
1176 mutex_init(&tz->lock);
1177 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1178 if (result) {
1179 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001180 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 }
1182
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001183 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001184 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301185 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 tz->device.class = &thermal_class;
1187 tz->devdata = devdata;
1188 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001189 tz->passive_delay = passive_delay;
1190 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001191
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001192 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001193 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001194 result = thermal_zone_create_device_groups(tz, mask);
1195 if (result)
1196 goto unregister;
1197
Chen Yu4511f712015-10-30 16:32:10 +08001198 /* A new thermal zone needs to be updated anyway. */
1199 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001200
Kay Sievers354655e2009-01-06 10:44:37 -08001201 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001202 result = device_register(&tz->device);
1203 if (result) {
1204 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1205 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001206 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001207 }
1208
Zhang Rui203d3d42008-01-17 15:51:08 +08001209 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001210 if (tz->ops->get_trip_type(tz, count, &trip_type))
1211 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001212 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1213 set_bit(count, &tz->trips_disabled);
1214 /* Check for bogus trip points */
1215 if (trip_temp == 0)
1216 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001217 }
1218
Durgadoss Ra4a15482012-09-18 11:04:57 +05301219 /* Update 'this' zone's governor information */
1220 mutex_lock(&thermal_governor_lock);
1221
1222 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001223 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301224 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001225 governor = def_governor;
1226
1227 result = thermal_set_governor(tz, governor);
1228 if (result) {
1229 mutex_unlock(&thermal_governor_lock);
1230 goto unregister;
1231 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301232
1233 mutex_unlock(&thermal_governor_lock);
1234
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001235 if (!tz->tzp || !tz->tzp->no_hwmon) {
1236 result = thermal_add_hwmon_sysfs(tz);
1237 if (result)
1238 goto unregister;
1239 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001240
Zhang Rui203d3d42008-01-17 15:51:08 +08001241 mutex_lock(&thermal_list_lock);
1242 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001243 mutex_unlock(&thermal_list_lock);
1244
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301245 /* Bind cooling devices for this zone */
1246 bind_tz(tz);
1247
Eduardo Valentinb659a302016-11-07 21:09:23 -08001248 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001249
Zhang Ruibb431ba2015-10-30 16:31:47 +08001250 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001251 /* Update the new thermal zone and mark it as already updated. */
1252 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001253 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001254
Yao Dongdong14015862014-10-28 07:40:25 +00001255 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001256
Joe Perchescaca8b802012-03-21 12:55:02 -07001257unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001258 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1259 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001260 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001261}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001262EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001263
1264/**
1265 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001266 * @tz: the thermal zone device to remove
1267 */
1268void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1269{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301270 int i;
1271 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001272 struct thermal_cooling_device *cdev;
1273 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001274
1275 if (!tz)
1276 return;
1277
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301278 tzp = tz->tzp;
1279
Zhang Rui203d3d42008-01-17 15:51:08 +08001280 mutex_lock(&thermal_list_lock);
1281 list_for_each_entry(pos, &thermal_tz_list, node)
Eduardo Valentinb659a302016-11-07 21:09:23 -08001282 if (pos == tz)
1283 break;
Zhang Rui203d3d42008-01-17 15:51:08 +08001284 if (pos != tz) {
1285 /* thermal zone device not found */
1286 mutex_unlock(&thermal_list_lock);
1287 return;
1288 }
1289 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301290
1291 /* Unbind all cdevs associated with 'this' thermal zone */
1292 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1293 if (tz->ops->unbind) {
1294 tz->ops->unbind(tz, cdev);
1295 continue;
1296 }
1297
1298 if (!tzp || !tzp->tbp)
1299 break;
1300
1301 for (i = 0; i < tzp->num_tbps; i++) {
1302 if (tzp->tbp[i].cdev == cdev) {
1303 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1304 tzp->tbp[i].cdev = NULL;
1305 }
1306 }
1307 }
1308
Zhang Rui203d3d42008-01-17 15:51:08 +08001309 mutex_unlock(&thermal_list_lock);
1310
Matthew Garrettb1569e92008-12-03 17:55:32 +00001311 thermal_zone_device_set_polling(tz, 0);
1312
Javi Merinoe33df1d2015-02-26 19:00:27 +00001313 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001314
Zhang Ruie68b16a2008-04-21 16:07:52 +08001315 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001316 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1317 idr_destroy(&tz->idr);
1318 mutex_destroy(&tz->lock);
1319 device_unregister(&tz->device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001320}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001321EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001322
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001323/**
1324 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1325 * @name: thermal zone name to fetch the temperature
1326 *
1327 * When only one zone is found with the passed name, returns a reference to it.
1328 *
1329 * Return: On success returns a reference to an unique thermal zone with
1330 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1331 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1332 */
1333struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1334{
1335 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1336 unsigned int found = 0;
1337
1338 if (!name)
1339 goto exit;
1340
1341 mutex_lock(&thermal_list_lock);
1342 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001343 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001344 found++;
1345 ref = pos;
1346 }
1347 mutex_unlock(&thermal_list_lock);
1348
1349 /* nothing has been found, thus an error code for it */
1350 if (found == 0)
1351 ref = ERR_PTR(-ENODEV);
1352 else if (found > 1)
1353 /* Success only when an unique zone is found */
1354 ref = ERR_PTR(-EEXIST);
1355
1356exit:
1357 return ref;
1358}
1359EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1360
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001361#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001362static const struct genl_multicast_group thermal_event_mcgrps[] = {
1363 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1364};
1365
Johannes Berg56989f62016-10-24 14:40:05 +02001366static struct genl_family thermal_event_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001367 .module = THIS_MODULE,
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001368 .name = THERMAL_GENL_FAMILY_NAME,
1369 .version = THERMAL_GENL_VERSION,
1370 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001371 .mcgrps = thermal_event_mcgrps,
1372 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001373};
1374
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001375int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001376 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301377{
1378 struct sk_buff *skb;
1379 struct nlattr *attr;
1380 struct thermal_genl_event *thermal_event;
1381 void *msg_header;
1382 int size;
1383 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001384 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301385
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001386 if (!tz)
1387 return -EINVAL;
1388
R.Durgadoss4cb18722010-10-27 03:33:29 +05301389 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001390 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1391 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301392
1393 skb = genlmsg_new(size, GFP_ATOMIC);
1394 if (!skb)
1395 return -ENOMEM;
1396
1397 /* add the genetlink message header */
1398 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1399 &thermal_event_genl_family, 0,
1400 THERMAL_GENL_CMD_EVENT);
1401 if (!msg_header) {
1402 nlmsg_free(skb);
1403 return -ENOMEM;
1404 }
1405
1406 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001407 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1408 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301409
1410 if (!attr) {
1411 nlmsg_free(skb);
1412 return -EINVAL;
1413 }
1414
1415 thermal_event = nla_data(attr);
1416 if (!thermal_event) {
1417 nlmsg_free(skb);
1418 return -EINVAL;
1419 }
1420
1421 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1422
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001423 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301424 thermal_event->event = event;
1425
1426 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001427 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301428
Johannes Berg68eb5502013-11-19 15:19:38 +01001429 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001430 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301431 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001432 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301433
1434 return result;
1435}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001436EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301437
Johannes Berg56989f62016-10-24 14:40:05 +02001438static int __init genetlink_init(void)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301439{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001440 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301441}
1442
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001443static void genetlink_exit(void)
1444{
1445 genl_unregister_family(&thermal_event_genl_family);
1446}
1447#else /* !CONFIG_NET */
1448static inline int genetlink_init(void) { return 0; }
1449static inline void genetlink_exit(void) {}
1450#endif /* !CONFIG_NET */
1451
Zhang Ruiff140fe2015-10-30 16:31:58 +08001452static int thermal_pm_notify(struct notifier_block *nb,
Eduardo Valentineb7be322016-11-07 21:09:21 -08001453 unsigned long mode, void *_unused)
Zhang Ruiff140fe2015-10-30 16:31:58 +08001454{
1455 struct thermal_zone_device *tz;
1456
1457 switch (mode) {
1458 case PM_HIBERNATION_PREPARE:
1459 case PM_RESTORE_PREPARE:
1460 case PM_SUSPEND_PREPARE:
1461 atomic_set(&in_suspend, 1);
1462 break;
1463 case PM_POST_HIBERNATION:
1464 case PM_POST_RESTORE:
1465 case PM_POST_SUSPEND:
1466 atomic_set(&in_suspend, 0);
1467 list_for_each_entry(tz, &thermal_tz_list, node) {
1468 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001469 thermal_zone_device_update(tz,
1470 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001471 }
1472 break;
1473 default:
1474 break;
1475 }
1476 return 0;
1477}
1478
1479static struct notifier_block thermal_pm_nb = {
1480 .notifier_call = thermal_pm_notify,
1481};
1482
Zhang Rui203d3d42008-01-17 15:51:08 +08001483static int __init thermal_init(void)
1484{
Zhang Rui80a26a52013-03-26 16:38:29 +08001485 int result;
1486
1487 result = thermal_register_governors();
1488 if (result)
1489 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001490
1491 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001492 if (result)
1493 goto unregister_governors;
1494
R.Durgadoss4cb18722010-10-27 03:33:29 +05301495 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001496 if (result)
1497 goto unregister_class;
1498
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001499 result = of_parse_thermal_zones();
1500 if (result)
1501 goto exit_netlink;
1502
Zhang Ruiff140fe2015-10-30 16:31:58 +08001503 result = register_pm_notifier(&thermal_pm_nb);
1504 if (result)
1505 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1506 result);
1507
Zhang Rui80a26a52013-03-26 16:38:29 +08001508 return 0;
1509
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001510exit_netlink:
1511 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001512unregister_class:
1513 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001514unregister_governors:
1515 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001516error:
1517 idr_destroy(&thermal_tz_idr);
1518 idr_destroy(&thermal_cdev_idr);
1519 mutex_destroy(&thermal_idr_lock);
1520 mutex_destroy(&thermal_list_lock);
1521 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001522 return result;
1523}
1524
1525static void __exit thermal_exit(void)
1526{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001527 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001528 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001529 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001531 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001532 idr_destroy(&thermal_tz_idr);
1533 idr_destroy(&thermal_cdev_idr);
1534 mutex_destroy(&thermal_idr_lock);
1535 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001536 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001537}
1538
R.Durgadoss4cb18722010-10-27 03:33:29 +05301539fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001540module_exit(thermal_exit);