rtc: pcf8523: don't return invalid date when battery is low
[linux-2.6-block.git] / drivers / thermal / thermal_core.c
CommitLineData
7e3c0381 1// SPDX-License-Identifier: GPL-2.0
203d3d4a
ZR
2/*
3 * thermal.c - Generic Thermal Management Sysfs support.
4 *
5 * Copyright (C) 2008 Intel Corp
6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
203d3d4a
ZR
8 */
9
c5a01dd5
JP
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
203d3d4a
ZR
12#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/err.h>
5a0e3ad6 15#include <linux/slab.h>
203d3d4a
ZR
16#include <linux/kdev_t.h>
17#include <linux/idr.h>
18#include <linux/thermal.h>
b1569e99 19#include <linux/reboot.h>
42a5bf50 20#include <linux/string.h>
a116b5d4 21#include <linux/of.h>
4cb18728
D
22#include <net/netlink.h>
23#include <net/genetlink.h>
ff140fea 24#include <linux/suspend.h>
203d3d4a 25
100a8fdb
PA
26#define CREATE_TRACE_POINTS
27#include <trace/events/thermal.h>
28
71350db4 29#include "thermal_core.h"
0dd88793 30#include "thermal_hwmon.h"
71350db4 31
63c4ec90 32MODULE_AUTHOR("Zhang Rui");
203d3d4a 33MODULE_DESCRIPTION("Generic thermal management sysfs support");
6d8d4974 34MODULE_LICENSE("GPL v2");
203d3d4a 35
b31ef828
MW
36static DEFINE_IDA(thermal_tz_ida);
37static DEFINE_IDA(thermal_cdev_ida);
203d3d4a
ZR
38
39static LIST_HEAD(thermal_tz_list);
40static LIST_HEAD(thermal_cdev_list);
a4a15485
D
41static LIST_HEAD(thermal_governor_list);
42
203d3d4a 43static DEFINE_MUTEX(thermal_list_lock);
a4a15485 44static DEFINE_MUTEX(thermal_governor_lock);
e441fd68 45static DEFINE_MUTEX(poweroff_lock);
a4a15485 46
ff140fea 47static atomic_t in_suspend;
e441fd68 48static bool power_off_triggered;
ff140fea 49
f2234bcd
ZR
50static struct thermal_governor *def_governor;
51
1b4f4849
EV
52/*
53 * Governor section: set of functions to handle thermal governors
54 *
55 * Functions to help in the life cycle of thermal governors within
56 * the thermal core and by the thermal governor code.
57 */
58
a4a15485
D
59static struct thermal_governor *__find_governor(const char *name)
60{
61 struct thermal_governor *pos;
62
f2234bcd
ZR
63 if (!name || !name[0])
64 return def_governor;
65
a4a15485 66 list_for_each_entry(pos, &thermal_governor_list, governor_list)
484ac2f3 67 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
a4a15485
D
68 return pos;
69
70 return NULL;
71}
72
e33df1d2
JM
73/**
74 * bind_previous_governor() - bind the previous governor of the thermal zone
75 * @tz: a valid pointer to a struct thermal_zone_device
76 * @failed_gov_name: the name of the governor that failed to register
77 *
78 * Register the previous governor of the thermal zone after a new
79 * governor has failed to be bound.
80 */
81static void bind_previous_governor(struct thermal_zone_device *tz,
82 const char *failed_gov_name)
83{
84 if (tz->governor && tz->governor->bind_to_tz) {
85 if (tz->governor->bind_to_tz(tz)) {
86 dev_err(&tz->device,
87 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
88 failed_gov_name, tz->governor->name, tz->type);
89 tz->governor = NULL;
90 }
91 }
92}
93
94/**
95 * thermal_set_governor() - Switch to another governor
96 * @tz: a valid pointer to a struct thermal_zone_device
97 * @new_gov: pointer to the new governor
98 *
99 * Change the governor of thermal zone @tz.
100 *
101 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
102 */
103static int thermal_set_governor(struct thermal_zone_device *tz,
104 struct thermal_governor *new_gov)
105{
106 int ret = 0;
107
108 if (tz->governor && tz->governor->unbind_from_tz)
109 tz->governor->unbind_from_tz(tz);
110
111 if (new_gov && new_gov->bind_to_tz) {
112 ret = new_gov->bind_to_tz(tz);
113 if (ret) {
114 bind_previous_governor(tz, new_gov->name);
115
116 return ret;
117 }
118 }
119
120 tz->governor = new_gov;
121
122 return ret;
123}
124
a4a15485
D
125int thermal_register_governor(struct thermal_governor *governor)
126{
127 int err;
128 const char *name;
129 struct thermal_zone_device *pos;
130
131 if (!governor)
132 return -EINVAL;
133
134 mutex_lock(&thermal_governor_lock);
135
136 err = -EBUSY;
5027ba36 137 if (!__find_governor(governor->name)) {
eb7be329
EV
138 bool match_default;
139
a4a15485
D
140 err = 0;
141 list_add(&governor->governor_list, &thermal_governor_list);
eb7be329
EV
142 match_default = !strncmp(governor->name,
143 DEFAULT_THERMAL_GOVERNOR,
144 THERMAL_NAME_LENGTH);
145
146 if (!def_governor && match_default)
f2234bcd 147 def_governor = governor;
a4a15485
D
148 }
149
150 mutex_lock(&thermal_list_lock);
151
152 list_for_each_entry(pos, &thermal_tz_list, node) {
f2234bcd
ZR
153 /*
154 * only thermal zones with specified tz->tzp->governor_name
155 * may run with tz->govenor unset
156 */
a4a15485
D
157 if (pos->governor)
158 continue;
f2234bcd
ZR
159
160 name = pos->tzp->governor_name;
161
e33df1d2
JM
162 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
163 int ret;
164
165 ret = thermal_set_governor(pos, governor);
166 if (ret)
167 dev_err(&pos->device,
168 "Failed to set governor %s for thermal zone %s: %d\n",
169 governor->name, pos->type, ret);
170 }
a4a15485
D
171 }
172
173 mutex_unlock(&thermal_list_lock);
174 mutex_unlock(&thermal_governor_lock);
175
176 return err;
177}
a4a15485
D
178
179void thermal_unregister_governor(struct thermal_governor *governor)
180{
181 struct thermal_zone_device *pos;
182
183 if (!governor)
184 return;
185
186 mutex_lock(&thermal_governor_lock);
187
5027ba36 188 if (!__find_governor(governor->name))
a4a15485
D
189 goto exit;
190
191 mutex_lock(&thermal_list_lock);
192
193 list_for_each_entry(pos, &thermal_tz_list, node) {
484ac2f3 194 if (!strncasecmp(pos->governor->name, governor->name,
eb7be329 195 THERMAL_NAME_LENGTH))
e33df1d2 196 thermal_set_governor(pos, NULL);
a4a15485
D
197 }
198
199 mutex_unlock(&thermal_list_lock);
200 list_del(&governor->governor_list);
201exit:
202 mutex_unlock(&thermal_governor_lock);
9b4298a0 203}
9b4298a0 204
1b4f4849
EV
205int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
206 char *policy)
9b4298a0 207{
1b4f4849
EV
208 struct thermal_governor *gov;
209 int ret = -EINVAL;
9b4298a0 210
1b4f4849 211 mutex_lock(&thermal_governor_lock);
9b4298a0 212 mutex_lock(&tz->lock);
9b4298a0 213
1b4f4849
EV
214 gov = __find_governor(strim(policy));
215 if (!gov)
216 goto exit;
9b4298a0 217
1b4f4849 218 ret = thermal_set_governor(tz, gov);
9b4298a0 219
1b4f4849
EV
220exit:
221 mutex_unlock(&tz->lock);
222 mutex_unlock(&thermal_governor_lock);
9b4298a0 223
1b4f4849 224 return ret;
7e8ee1e9
D
225}
226
1b4f4849 227int thermal_build_list_of_policies(char *buf)
7e8ee1e9 228{
1b4f4849
EV
229 struct thermal_governor *pos;
230 ssize_t count = 0;
231 ssize_t size = PAGE_SIZE;
7e8ee1e9 232
1b4f4849 233 mutex_lock(&thermal_governor_lock);
a8892d83 234
1b4f4849
EV
235 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
236 size = PAGE_SIZE - count;
237 count += scnprintf(buf + count, size, "%s ", pos->name);
7e8ee1e9 238 }
1b4f4849 239 count += scnprintf(buf + count, size, "\n");
7e8ee1e9 240
1b4f4849 241 mutex_unlock(&thermal_governor_lock);
7e8ee1e9 242
1b4f4849 243 return count;
7e8ee1e9
D
244}
245
1b4f4849 246static int __init thermal_register_governors(void)
7e8ee1e9 247{
1b4f4849 248 int result;
7e8ee1e9 249
1b4f4849
EV
250 result = thermal_gov_step_wise_register();
251 if (result)
252 return result;
7e8ee1e9 253
1b4f4849
EV
254 result = thermal_gov_fair_share_register();
255 if (result)
256 return result;
7e8ee1e9 257
1b4f4849
EV
258 result = thermal_gov_bang_bang_register();
259 if (result)
260 return result;
7e8ee1e9 261
1b4f4849
EV
262 result = thermal_gov_user_space_register();
263 if (result)
264 return result;
7e8ee1e9 265
1b4f4849 266 return thermal_gov_power_allocator_register();
7e8ee1e9
D
267}
268
1b4f4849 269static void thermal_unregister_governors(void)
7e8ee1e9 270{
1b4f4849
EV
271 thermal_gov_step_wise_unregister();
272 thermal_gov_fair_share_unregister();
273 thermal_gov_bang_bang_unregister();
274 thermal_gov_user_space_unregister();
275 thermal_gov_power_allocator_unregister();
7e8ee1e9
D
276}
277
8772e185
EV
278/*
279 * Zone update section: main control loop applied to each zone while monitoring
280 *
281 * in polling mode. The monitoring is done using a workqueue.
282 * Same update may be done on a zone by calling thermal_zone_device_update().
283 *
284 * An update means:
285 * - Non-critical trips will invoke the governor responsible for that zone;
286 * - Hot trips will produce a notification to userspace;
287 * - Critical trip point will cause a system shutdown.
288 */
0c01ebbf
D
289static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
290 int delay)
291{
292 if (delay > 1000)
c2b59d27
JG
293 mod_delayed_work(system_freezable_power_efficient_wq,
294 &tz->poll_queue,
0c01ebbf
D
295 round_jiffies(msecs_to_jiffies(delay)));
296 else if (delay)
c2b59d27
JG
297 mod_delayed_work(system_freezable_power_efficient_wq,
298 &tz->poll_queue,
0c01ebbf
D
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,
eb7be329
EV
319 int trip,
320 enum thermal_trip_type trip_type)
0c01ebbf 321{
f2234bcd
ZR
322 tz->governor ? tz->governor->throttle(tz, trip) :
323 def_governor->throttle(tz, trip);
0c01ebbf
D
324}
325
ef1d87e0
K
326/**
327 * thermal_emergency_poweroff_func - emergency poweroff work after a known delay
328 * @work: work_struct associated with the emergency poweroff function
329 *
330 * This function is called in very critical situations to force
331 * a kernel poweroff after a configurable timeout value.
332 */
333static void thermal_emergency_poweroff_func(struct work_struct *work)
334{
335 /*
336 * We have reached here after the emergency thermal shutdown
337 * Waiting period has expired. This means orderly_poweroff has
338 * not been able to shut off the system for some reason.
339 * Try to shut down the system immediately using kernel_power_off
340 * if populated
341 */
342 WARN(1, "Attempting kernel_power_off: Temperature too high\n");
343 kernel_power_off();
344
345 /*
346 * Worst of the worst case trigger emergency restart
347 */
348 WARN(1, "Attempting emergency_restart: Temperature too high\n");
349 emergency_restart();
350}
351
352static DECLARE_DELAYED_WORK(thermal_emergency_poweroff_work,
353 thermal_emergency_poweroff_func);
354
355/**
356 * thermal_emergency_poweroff - Trigger an emergency system poweroff
357 *
358 * This may be called from any critical situation to trigger a system shutdown
359 * after a known period of time. By default this is not scheduled.
360 */
c4b379d0 361static void thermal_emergency_poweroff(void)
ef1d87e0
K
362{
363 int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
364 /*
365 * poweroff_delay_ms must be a carefully profiled positive value.
366 * Its a must for thermal_emergency_poweroff_work to be scheduled
367 */
368 if (poweroff_delay_ms <= 0)
369 return;
370 schedule_delayed_work(&thermal_emergency_poweroff_work,
371 msecs_to_jiffies(poweroff_delay_ms));
372}
373
0c01ebbf 374static void handle_critical_trips(struct thermal_zone_device *tz,
eb7be329 375 int trip, enum thermal_trip_type trip_type)
0c01ebbf 376{
17e8351a 377 int trip_temp;
0c01ebbf
D
378
379 tz->ops->get_trip_temp(tz, trip, &trip_temp);
380
381 /* If we have not crossed the trip_temp, we do not care. */
84ffe3ec 382 if (trip_temp <= 0 || tz->temperature < trip_temp)
0c01ebbf
D
383 return;
384
208cd822
PA
385 trace_thermal_zone_trip(tz, trip, trip_type);
386
0c01ebbf
D
387 if (tz->ops->notify)
388 tz->ops->notify(tz, trip, trip_type);
389
390 if (trip_type == THERMAL_TRIP_CRITICAL) {
923e0b1e 391 dev_emerg(&tz->device,
039f6cf5 392 "critical temperature reached (%d C), shutting down\n",
923e0b1e 393 tz->temperature / 1000);
e441fd68
K
394 mutex_lock(&poweroff_lock);
395 if (!power_off_triggered) {
ef1d87e0
K
396 /*
397 * Queue a backup emergency shutdown in the event of
398 * orderly_poweroff failure
399 */
400 thermal_emergency_poweroff();
e441fd68
K
401 orderly_poweroff(true);
402 power_off_triggered = true;
403 }
404 mutex_unlock(&poweroff_lock);
0c01ebbf
D
405 }
406}
407
408static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
409{
410 enum thermal_trip_type type;
411
81ad4276
ZR
412 /* Ignore disabled trip points */
413 if (test_bit(trip, &tz->trips_disabled))
414 return;
415
0c01ebbf
D
416 tz->ops->get_trip_type(tz, trip, &type);
417
418 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
419 handle_critical_trips(tz, trip, type);
420 else
421 handle_non_critical_trips(tz, trip, type);
422 /*
423 * Alright, we handled this trip successfully.
424 * So, start monitoring again.
425 */
426 monitor_thermal_zone(tz);
427}
428
429static void update_temperature(struct thermal_zone_device *tz)
430{
17e8351a 431 int temp, ret;
0c01ebbf 432
e6e238c3 433 ret = thermal_zone_get_temp(tz, &temp);
0c01ebbf 434 if (ret) {
7e497a73
HG
435 if (ret != -EAGAIN)
436 dev_warn(&tz->device,
437 "failed to read out thermal zone (%d)\n",
438 ret);
e6e238c3 439 return;
0c01ebbf
D
440 }
441
e6e238c3 442 mutex_lock(&tz->lock);
0c01ebbf
D
443 tz->last_temperature = tz->temperature;
444 tz->temperature = temp;
0c01ebbf 445 mutex_unlock(&tz->lock);
06475b55 446
100a8fdb 447 trace_thermal_temperature(tz);
bb431ba2
ZR
448 if (tz->last_temperature == THERMAL_TEMP_INVALID)
449 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
450 tz->temperature);
451 else
452 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
453 tz->last_temperature, tz->temperature);
454}
455
456static void thermal_zone_device_reset(struct thermal_zone_device *tz)
457{
458 struct thermal_instance *pos;
459
460 tz->temperature = THERMAL_TEMP_INVALID;
461 tz->passive = 0;
462 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
463 pos->initialized = false;
0c01ebbf
D
464}
465
0e70f466
SP
466void thermal_zone_device_update(struct thermal_zone_device *tz,
467 enum thermal_notify_event event)
0c01ebbf
D
468{
469 int count;
470
ff140fea
ZR
471 if (atomic_read(&in_suspend))
472 return;
473
81bd4e1c
EV
474 if (!tz->ops->get_temp)
475 return;
476
0c01ebbf
D
477 update_temperature(tz);
478
060c034a
SH
479 thermal_zone_set_trips(tz);
480
0e70f466
SP
481 tz->notify_event = event;
482
0c01ebbf
D
483 for (count = 0; count < tz->trips; count++)
484 handle_thermal_trip(tz, count);
485}
910cb1e3 486EXPORT_SYMBOL_GPL(thermal_zone_device_update);
0c01ebbf 487
106339ab
EV
488/**
489 * thermal_notify_framework - Sensor drivers use this API to notify framework
490 * @tz: thermal zone device
491 * @trip: indicates which trip point has been crossed
492 *
493 * This function handles the trip events from sensor drivers. It starts
494 * throttling the cooling devices according to the policy configured.
495 * For CRITICAL and HOT trip points, this notifies the respective drivers,
496 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
497 * The throttling policy is based on the configured platform data; if no
498 * platform data is provided, this uses the step_wise throttling policy.
499 */
500void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
501{
502 handle_thermal_trip(tz, trip);
503}
504EXPORT_SYMBOL_GPL(thermal_notify_framework);
505
0c01ebbf
D
506static void thermal_zone_device_check(struct work_struct *work)
507{
508 struct thermal_zone_device *tz = container_of(work, struct
509 thermal_zone_device,
510 poll_queue.work);
0e70f466 511 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
0c01ebbf
D
512}
513
712afbdf
EV
514/*
515 * Power actor section: interface to power actors to estimate power
516 *
517 * Set of functions used to interact to cooling devices that know
518 * how to estimate their devices power consumption.
519 */
9f38271c 520
35b11d2e
JM
521/**
522 * power_actor_get_max_power() - get the maximum power that a cdev can consume
523 * @cdev: pointer to &thermal_cooling_device
524 * @tz: a valid thermal zone device pointer
525 * @max_power: pointer in which to store the maximum power
526 *
527 * Calculate the maximum power consumption in milliwats that the
528 * cooling device can currently consume and store it in @max_power.
529 *
530 * Return: 0 on success, -EINVAL if @cdev doesn't support the
531 * power_actor API or -E* on other error.
532 */
533int power_actor_get_max_power(struct thermal_cooling_device *cdev,
534 struct thermal_zone_device *tz, u32 *max_power)
535{
536 if (!cdev_is_power_actor(cdev))
537 return -EINVAL;
538
539 return cdev->ops->state2power(cdev, tz, 0, max_power);
540}
541
c973c3bc
JM
542/**
543 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
544 * @cdev: pointer to &thermal_cooling_device
545 * @tz: a valid thermal zone device pointer
546 * @min_power: pointer in which to store the minimum power
547 *
548 * Calculate the minimum power consumption in milliwatts that the
549 * cooling device can currently consume and store it in @min_power.
550 *
551 * Return: 0 on success, -EINVAL if @cdev doesn't support the
552 * power_actor API or -E* on other error.
553 */
554int power_actor_get_min_power(struct thermal_cooling_device *cdev,
555 struct thermal_zone_device *tz, u32 *min_power)
556{
557 unsigned long max_state;
558 int ret;
559
560 if (!cdev_is_power_actor(cdev))
561 return -EINVAL;
562
563 ret = cdev->ops->get_max_state(cdev, &max_state);
564 if (ret)
565 return ret;
566
567 return cdev->ops->state2power(cdev, tz, max_state, min_power);
568}
569
35b11d2e 570/**
1a7e7cc0 571 * power_actor_set_power() - limit the maximum power a cooling device consumes
35b11d2e
JM
572 * @cdev: pointer to &thermal_cooling_device
573 * @instance: thermal instance to update
574 * @power: the power in milliwatts
575 *
1a7e7cc0
EV
576 * Set the cooling device to consume at most @power milliwatts. The limit is
577 * expected to be a cap at the maximum power consumption.
35b11d2e
JM
578 *
579 * Return: 0 on success, -EINVAL if the cooling device does not
580 * implement the power actor API or -E* for other failures.
581 */
582int power_actor_set_power(struct thermal_cooling_device *cdev,
583 struct thermal_instance *instance, u32 power)
584{
585 unsigned long state;
586 int ret;
587
588 if (!cdev_is_power_actor(cdev))
589 return -EINVAL;
590
591 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
592 if (ret)
593 return ret;
594
595 instance->target = state;
d0b7306d 596 mutex_lock(&cdev->lock);
35b11d2e 597 cdev->updated = false;
d0b7306d 598 mutex_unlock(&cdev->lock);
35b11d2e
JM
599 thermal_cdev_update(cdev);
600
601 return 0;
602}
603
3d0055d2
EV
604void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
605 const char *cdev_type, size_t size)
203d3d4a 606{
3d0055d2 607 struct thermal_cooling_device *cdev = NULL;
203d3d4a 608
3d0055d2
EV
609 mutex_lock(&thermal_list_lock);
610 list_for_each_entry(cdev, &thermal_cdev_list, node) {
611 /* skip non matching cdevs */
612 if (strncmp(cdev_type, cdev->type, size))
613 continue;
614
615 /* re binding the exception matching the type pattern */
616 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
617 THERMAL_NO_LIMIT,
618 THERMAL_NO_LIMIT,
619 THERMAL_WEIGHT_DEFAULT);
620 }
621 mutex_unlock(&thermal_list_lock);
203d3d4a
ZR
622}
623
3d0055d2
EV
624void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
625 const char *cdev_type, size_t size)
203d3d4a 626{
3d0055d2 627 struct thermal_cooling_device *cdev = NULL;
203d3d4a 628
3d0055d2
EV
629 mutex_lock(&thermal_list_lock);
630 list_for_each_entry(cdev, &thermal_cdev_list, node) {
631 /* skip non matching cdevs */
632 if (strncmp(cdev_type, cdev->type, size))
633 continue;
634 /* unbinding the exception matching the type pattern */
635 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
636 cdev);
637 }
638 mutex_unlock(&thermal_list_lock);
203d3d4a
ZR
639}
640
81193e2e
EV
641/*
642 * Device management section: cooling devices, zones devices, and binding
643 *
644 * Set of functions provided by the thermal core for:
645 * - cooling devices lifecycle: registration, unregistration,
646 * binding, and unbinding.
647 * - thermal zone devices lifecycle: registration, unregistration,
648 * binding, and unbinding.
649 */
203d3d4a
ZR
650
651/**
d2e4eb83
EV
652 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
653 * @tz: pointer to struct thermal_zone_device
203d3d4a
ZR
654 * @trip: indicates which trip point the cooling devices is
655 * associated with in this thermal zone.
d2e4eb83
EV
656 * @cdev: pointer to struct thermal_cooling_device
657 * @upper: the Maximum cooling state for this trip point.
658 * THERMAL_NO_LIMIT means no upper limit,
659 * and the cooling device can be in max_state.
660 * @lower: the Minimum cooling state can be used for this trip point.
661 * THERMAL_NO_LIMIT means no lower limit,
662 * and the cooling device can be in cooling state 0.
6cd9e9f6
KS
663 * @weight: The weight of the cooling device to be bound to the
664 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
665 * default value
543a9561 666 *
d2e4eb83
EV
667 * This interface function bind a thermal cooling device to the certain trip
668 * point of a thermal zone device.
543a9561 669 * This function is usually called in the thermal zone device .bind callback.
d2e4eb83
EV
670 *
671 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
672 */
673int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
674 int trip,
9d99842f 675 struct thermal_cooling_device *cdev,
6cd9e9f6
KS
676 unsigned long upper, unsigned long lower,
677 unsigned int weight)
203d3d4a 678{
b81b6ba3
ZR
679 struct thermal_instance *dev;
680 struct thermal_instance *pos;
c7516709
TS
681 struct thermal_zone_device *pos1;
682 struct thermal_cooling_device *pos2;
74051ba5 683 unsigned long max_state;
9a3031dc 684 int result, ret;
203d3d4a 685
543a9561 686 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
687 return -EINVAL;
688
c7516709
TS
689 list_for_each_entry(pos1, &thermal_tz_list, node) {
690 if (pos1 == tz)
691 break;
692 }
693 list_for_each_entry(pos2, &thermal_cdev_list, node) {
694 if (pos2 == cdev)
695 break;
696 }
697
698 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
699 return -EINVAL;
700
9a3031dc
LM
701 ret = cdev->ops->get_max_state(cdev, &max_state);
702 if (ret)
703 return ret;
9d99842f
ZR
704
705 /* lower default 0, upper default max_state */
706 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
707 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
708
709 if (lower > upper || upper > max_state)
710 return -EINVAL;
711
95e3ed15 712 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
203d3d4a
ZR
713 if (!dev)
714 return -ENOMEM;
715 dev->tz = tz;
716 dev->cdev = cdev;
717 dev->trip = trip;
9d99842f
ZR
718 dev->upper = upper;
719 dev->lower = lower;
ce119f83 720 dev->target = THERMAL_NO_TARGET;
6cd9e9f6 721 dev->weight = weight;
74051ba5 722
b31ef828
MW
723 result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL);
724 if (result < 0)
203d3d4a
ZR
725 goto free_mem;
726
b31ef828 727 dev->id = result;
203d3d4a
ZR
728 sprintf(dev->name, "cdev%d", dev->id);
729 result =
730 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
731 if (result)
b31ef828 732 goto release_ida;
203d3d4a
ZR
733
734 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 735 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
736 dev->attr.attr.name = dev->attr_name;
737 dev->attr.attr.mode = 0444;
33e678d4 738 dev->attr.show = trip_point_show;
203d3d4a
ZR
739 result = device_create_file(&tz->device, &dev->attr);
740 if (result)
741 goto remove_symbol_link;
742
db916513
JM
743 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
744 sysfs_attr_init(&dev->weight_attr.attr);
745 dev->weight_attr.attr.name = dev->weight_attr_name;
746 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
33e678d4
VK
747 dev->weight_attr.show = weight_show;
748 dev->weight_attr.store = weight_store;
db916513
JM
749 result = device_create_file(&tz->device, &dev->weight_attr);
750 if (result)
751 goto remove_trip_file;
752
203d3d4a 753 mutex_lock(&tz->lock);
f4a821ce 754 mutex_lock(&cdev->lock);
cddf31b3 755 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
b659a30d
EV
756 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
757 result = -EEXIST;
758 break;
759 }
b5e4ae62 760 if (!result) {
cddf31b3 761 list_add_tail(&dev->tz_node, &tz->thermal_instances);
b5e4ae62 762 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
4511f716 763 atomic_set(&tz->need_update, 1);
b5e4ae62 764 }
f4a821ce 765 mutex_unlock(&cdev->lock);
203d3d4a
ZR
766 mutex_unlock(&tz->lock);
767
768 if (!result)
769 return 0;
770
db916513
JM
771 device_remove_file(&tz->device, &dev->weight_attr);
772remove_trip_file:
203d3d4a 773 device_remove_file(&tz->device, &dev->attr);
caca8b80 774remove_symbol_link:
203d3d4a 775 sysfs_remove_link(&tz->device.kobj, dev->name);
b31ef828
MW
776release_ida:
777 ida_simple_remove(&tz->ida, dev->id);
caca8b80 778free_mem:
203d3d4a
ZR
779 kfree(dev);
780 return result;
781}
910cb1e3 782EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
203d3d4a
ZR
783
784/**
9892e5dc
EV
785 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
786 * thermal zone.
787 * @tz: pointer to a struct thermal_zone_device.
203d3d4a
ZR
788 * @trip: indicates which trip point the cooling devices is
789 * associated with in this thermal zone.
9892e5dc 790 * @cdev: pointer to a struct thermal_cooling_device.
543a9561 791 *
9892e5dc
EV
792 * This interface function unbind a thermal cooling device from the certain
793 * trip point of a thermal zone device.
543a9561 794 * This function is usually called in the thermal zone device .unbind callback.
9892e5dc
EV
795 *
796 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
797 */
798int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
799 int trip,
800 struct thermal_cooling_device *cdev)
801{
b81b6ba3 802 struct thermal_instance *pos, *next;
203d3d4a
ZR
803
804 mutex_lock(&tz->lock);
f4a821ce 805 mutex_lock(&cdev->lock);
cddf31b3 806 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
543a9561 807 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
cddf31b3 808 list_del(&pos->tz_node);
b5e4ae62 809 list_del(&pos->cdev_node);
f4a821ce 810 mutex_unlock(&cdev->lock);
203d3d4a
ZR
811 mutex_unlock(&tz->lock);
812 goto unbind;
813 }
814 }
f4a821ce 815 mutex_unlock(&cdev->lock);
203d3d4a
ZR
816 mutex_unlock(&tz->lock);
817
818 return -ENODEV;
819
caca8b80 820unbind:
528464ea 821 device_remove_file(&tz->device, &pos->weight_attr);
203d3d4a
ZR
822 device_remove_file(&tz->device, &pos->attr);
823 sysfs_remove_link(&tz->device.kobj, pos->name);
b31ef828 824 ida_simple_remove(&tz->ida, pos->id);
203d3d4a
ZR
825 kfree(pos);
826 return 0;
827}
910cb1e3 828EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
203d3d4a
ZR
829
830static void thermal_release(struct device *dev)
831{
832 struct thermal_zone_device *tz;
833 struct thermal_cooling_device *cdev;
834
caca8b80
JP
835 if (!strncmp(dev_name(dev), "thermal_zone",
836 sizeof("thermal_zone") - 1)) {
203d3d4a 837 tz = to_thermal_zone(dev);
6a6cd25b 838 thermal_zone_destroy_device_groups(tz);
203d3d4a 839 kfree(tz);
b659a30d
EV
840 } else if (!strncmp(dev_name(dev), "cooling_device",
841 sizeof("cooling_device") - 1)) {
203d3d4a
ZR
842 cdev = to_cooling_device(dev);
843 kfree(cdev);
844 }
845}
846
847static struct class thermal_class = {
848 .name = "thermal",
849 .dev_release = thermal_release,
850};
851
4b0d3c2d
EV
852static inline
853void print_bind_err_msg(struct thermal_zone_device *tz,
854 struct thermal_cooling_device *cdev, int ret)
f502ab84
EV
855{
856 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
857 tz->type, cdev->type, ret);
858}
859
860static void __bind(struct thermal_zone_device *tz, int mask,
861 struct thermal_cooling_device *cdev,
862 unsigned long *limits,
863 unsigned int weight)
864{
865 int i, ret;
866
867 for (i = 0; i < tz->trips; i++) {
868 if (mask & (1 << i)) {
869 unsigned long upper, lower;
870
871 upper = THERMAL_NO_LIMIT;
872 lower = THERMAL_NO_LIMIT;
873 if (limits) {
874 lower = limits[i * 2];
875 upper = limits[i * 2 + 1];
876 }
877 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
878 upper, lower,
879 weight);
880 if (ret)
881 print_bind_err_msg(tz, cdev, ret);
882 }
883 }
884}
885
949aad83
EV
886static void bind_cdev(struct thermal_cooling_device *cdev)
887{
888 int i, ret;
889 const struct thermal_zone_params *tzp;
890 struct thermal_zone_device *pos = NULL;
891
892 mutex_lock(&thermal_list_lock);
893
894 list_for_each_entry(pos, &thermal_tz_list, node) {
895 if (!pos->tzp && !pos->ops->bind)
896 continue;
897
898 if (pos->ops->bind) {
899 ret = pos->ops->bind(pos, cdev);
900 if (ret)
901 print_bind_err_msg(pos, cdev, ret);
902 continue;
903 }
904
905 tzp = pos->tzp;
906 if (!tzp || !tzp->tbp)
907 continue;
908
909 for (i = 0; i < tzp->num_tbps; i++) {
910 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
911 continue;
912 if (tzp->tbp[i].match(pos, cdev))
913 continue;
914 tzp->tbp[i].cdev = cdev;
915 __bind(pos, tzp->tbp[i].trip_mask, cdev,
916 tzp->tbp[i].binding_limits,
917 tzp->tbp[i].weight);
918 }
919 }
920
921 mutex_unlock(&thermal_list_lock);
922}
923
203d3d4a 924/**
a116b5d4
EV
925 * __thermal_cooling_device_register() - register a new thermal cooling device
926 * @np: a pointer to a device tree node.
203d3d4a
ZR
927 * @type: the thermal cooling device type.
928 * @devdata: device private data.
929 * @ops: standard thermal cooling devices callbacks.
3a6eccb3
EV
930 *
931 * This interface function adds a new thermal cooling device (fan/processor/...)
932 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
933 * to all the thermal zone devices registered at the same time.
a116b5d4
EV
934 * It also gives the opportunity to link the cooling device to a device tree
935 * node, so that it can be bound to a thermal zone created out of device tree.
3a6eccb3
EV
936 *
937 * Return: a pointer to the created struct thermal_cooling_device or an
938 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
203d3d4a 939 */
a116b5d4
EV
940static struct thermal_cooling_device *
941__thermal_cooling_device_register(struct device_node *np,
942 char *type, void *devdata,
943 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
944{
945 struct thermal_cooling_device *cdev;
4511f716 946 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
947 int result;
948
204dd1d3 949 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 950 return ERR_PTR(-EINVAL);
203d3d4a
ZR
951
952 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 953 !ops->set_cur_state)
3e6fda5c 954 return ERR_PTR(-EINVAL);
203d3d4a 955
95e3ed15 956 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
203d3d4a 957 if (!cdev)
3e6fda5c 958 return ERR_PTR(-ENOMEM);
203d3d4a 959
b31ef828
MW
960 result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL);
961 if (result < 0) {
203d3d4a 962 kfree(cdev);
3e6fda5c 963 return ERR_PTR(result);
203d3d4a
ZR
964 }
965
b31ef828 966 cdev->id = result;
c7a8b9d9 967 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
f4a821ce 968 mutex_init(&cdev->lock);
b5e4ae62 969 INIT_LIST_HEAD(&cdev->thermal_instances);
a116b5d4 970 cdev->np = np;
203d3d4a 971 cdev->ops = ops;
5ca0cce5 972 cdev->updated = false;
203d3d4a
ZR
973 cdev->device.class = &thermal_class;
974 cdev->devdata = devdata;
8ea22951 975 thermal_cooling_device_setup_sysfs(cdev);
354655ea 976 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
977 result = device_register(&cdev->device);
978 if (result) {
b31ef828 979 ida_simple_remove(&thermal_cdev_ida, cdev->id);
203d3d4a 980 kfree(cdev);
3e6fda5c 981 return ERR_PTR(result);
203d3d4a
ZR
982 }
983
7e8ee1e9 984 /* Add 'this' new cdev to the global cdev list */
203d3d4a
ZR
985 mutex_lock(&thermal_list_lock);
986 list_add(&cdev->node, &thermal_cdev_list);
203d3d4a
ZR
987 mutex_unlock(&thermal_list_lock);
988
7e8ee1e9
D
989 /* Update binding information for 'this' new cdev */
990 bind_cdev(cdev);
991
4511f716
CY
992 mutex_lock(&thermal_list_lock);
993 list_for_each_entry(pos, &thermal_tz_list, node)
994 if (atomic_cmpxchg(&pos->need_update, 1, 0))
0e70f466
SP
995 thermal_zone_device_update(pos,
996 THERMAL_EVENT_UNSPECIFIED);
4511f716
CY
997 mutex_unlock(&thermal_list_lock);
998
7e8ee1e9 999 return cdev;
203d3d4a 1000}
a116b5d4
EV
1001
1002/**
1003 * thermal_cooling_device_register() - register a new thermal cooling device
1004 * @type: the thermal cooling device type.
1005 * @devdata: device private data.
1006 * @ops: standard thermal cooling devices callbacks.
1007 *
1008 * This interface function adds a new thermal cooling device (fan/processor/...)
1009 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1010 * to all the thermal zone devices registered at the same time.
1011 *
1012 * Return: a pointer to the created struct thermal_cooling_device or an
1013 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1014 */
1015struct thermal_cooling_device *
1016thermal_cooling_device_register(char *type, void *devdata,
1017 const struct thermal_cooling_device_ops *ops)
1018{
1019 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1020}
910cb1e3 1021EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
203d3d4a 1022
a116b5d4
EV
1023/**
1024 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1025 * @np: a pointer to a device tree node.
1026 * @type: the thermal cooling device type.
1027 * @devdata: device private data.
1028 * @ops: standard thermal cooling devices callbacks.
1029 *
1030 * This function will register a cooling device with device tree node reference.
1031 * This interface function adds a new thermal cooling device (fan/processor/...)
1032 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1033 * to all the thermal zone devices registered at the same time.
1034 *
1035 * Return: a pointer to the created struct thermal_cooling_device or an
1036 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1037 */
1038struct thermal_cooling_device *
1039thermal_of_cooling_device_register(struct device_node *np,
1040 char *type, void *devdata,
1041 const struct thermal_cooling_device_ops *ops)
1042{
1043 return __thermal_cooling_device_register(np, type, devdata, ops);
1044}
1045EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1046
f11997fa
EV
1047static void __unbind(struct thermal_zone_device *tz, int mask,
1048 struct thermal_cooling_device *cdev)
1049{
1050 int i;
1051
1052 for (i = 0; i < tz->trips; i++)
1053 if (mask & (1 << i))
1054 thermal_zone_unbind_cooling_device(tz, i, cdev);
1055}
1056
203d3d4a 1057/**
38e7b549 1058 * thermal_cooling_device_unregister - removes a thermal cooling device
203d3d4a
ZR
1059 * @cdev: the thermal cooling device to remove.
1060 *
38e7b549
EV
1061 * thermal_cooling_device_unregister() must be called when a registered
1062 * thermal cooling device is no longer needed.
203d3d4a 1063 */
7e8ee1e9 1064void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
203d3d4a 1065{
7e8ee1e9
D
1066 int i;
1067 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1068 struct thermal_zone_device *tz;
1069 struct thermal_cooling_device *pos = NULL;
1070
1071 if (!cdev)
1072 return;
1073
1074 mutex_lock(&thermal_list_lock);
1075 list_for_each_entry(pos, &thermal_cdev_list, node)
b659a30d
EV
1076 if (pos == cdev)
1077 break;
203d3d4a
ZR
1078 if (pos != cdev) {
1079 /* thermal cooling device not found */
1080 mutex_unlock(&thermal_list_lock);
1081 return;
1082 }
1083 list_del(&cdev->node);
7e8ee1e9
D
1084
1085 /* Unbind all thermal zones associated with 'this' cdev */
203d3d4a 1086 list_for_each_entry(tz, &thermal_tz_list, node) {
7e8ee1e9
D
1087 if (tz->ops->unbind) {
1088 tz->ops->unbind(tz, cdev);
1089 continue;
1090 }
1091
1092 if (!tz->tzp || !tz->tzp->tbp)
203d3d4a 1093 continue;
7e8ee1e9
D
1094
1095 tzp = tz->tzp;
1096 for (i = 0; i < tzp->num_tbps; i++) {
1097 if (tzp->tbp[i].cdev == cdev) {
1098 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1099 tzp->tbp[i].cdev = NULL;
1100 }
1101 }
203d3d4a 1102 }
7e8ee1e9 1103
203d3d4a 1104 mutex_unlock(&thermal_list_lock);
7e8ee1e9 1105
b31ef828 1106 ida_simple_remove(&thermal_cdev_ida, cdev->id);
3c587768 1107 device_del(&cdev->device);
8ea22951 1108 thermal_cooling_device_destroy_sysfs(cdev);
3c587768 1109 put_device(&cdev->device);
203d3d4a 1110}
910cb1e3 1111EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
203d3d4a 1112
90f5b5bb 1113static void bind_tz(struct thermal_zone_device *tz)
ce119f83 1114{
90f5b5bb
EV
1115 int i, ret;
1116 struct thermal_cooling_device *pos = NULL;
1117 const struct thermal_zone_params *tzp = tz->tzp;
ce119f83 1118
90f5b5bb 1119 if (!tzp && !tz->ops->bind)
ce119f83 1120 return;
c56f5c03 1121
90f5b5bb 1122 mutex_lock(&thermal_list_lock);
c56f5c03 1123
90f5b5bb
EV
1124 /* If there is ops->bind, try to use ops->bind */
1125 if (tz->ops->bind) {
1126 list_for_each_entry(pos, &thermal_cdev_list, node) {
1127 ret = tz->ops->bind(tz, pos);
1128 if (ret)
1129 print_bind_err_msg(tz, pos, ret);
27365a6c 1130 }
90f5b5bb 1131 goto exit;
27365a6c 1132 }
c56f5c03 1133
90f5b5bb
EV
1134 if (!tzp || !tzp->tbp)
1135 goto exit;
27365a6c 1136
90f5b5bb
EV
1137 list_for_each_entry(pos, &thermal_cdev_list, node) {
1138 for (i = 0; i < tzp->num_tbps; i++) {
1139 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1140 continue;
1141 if (tzp->tbp[i].match(tz, pos))
1142 continue;
1143 tzp->tbp[i].cdev = pos;
1144 __bind(tz, tzp->tbp[i].trip_mask, pos,
1145 tzp->tbp[i].binding_limits,
1146 tzp->tbp[i].weight);
27365a6c 1147 }
c56f5c03 1148 }
90f5b5bb
EV
1149exit:
1150 mutex_unlock(&thermal_list_lock);
c56f5c03
D
1151}
1152
203d3d4a 1153/**
a00e55f9 1154 * thermal_zone_device_register() - register a new thermal zone device
203d3d4a
ZR
1155 * @type: the thermal zone device type
1156 * @trips: the number of trip points the thermal zone support
c56f5c03 1157 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1158 * @devdata: private device data
1159 * @ops: standard thermal zone device callbacks
50125a9b 1160 * @tzp: thermal zone platform parameters
b1569e99
MG
1161 * @passive_delay: number of milliseconds to wait between polls when
1162 * performing passive cooling
1163 * @polling_delay: number of milliseconds to wait between polls when checking
1164 * whether trip points have been crossed (0 for interrupt
1165 * driven systems)
203d3d4a 1166 *
a00e55f9
EV
1167 * This interface function adds a new thermal zone device (sensor) to
1168 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1169 * thermal cooling devices registered at the same time.
203d3d4a 1170 * thermal_zone_device_unregister() must be called when the device is no
1b7ddb84 1171 * longer needed. The passive cooling depends on the .get_trend() return value.
a00e55f9
EV
1172 *
1173 * Return: a pointer to the created struct thermal_zone_device or an
1174 * in case of error, an ERR_PTR. Caller must check return value with
1175 * IS_ERR*() helpers.
203d3d4a 1176 */
eb7be329
EV
1177struct thermal_zone_device *
1178thermal_zone_device_register(const char *type, int trips, int mask,
1179 void *devdata, struct thermal_zone_device_ops *ops,
1180 struct thermal_zone_params *tzp, int passive_delay,
1181 int polling_delay)
203d3d4a
ZR
1182{
1183 struct thermal_zone_device *tz;
03a971a2 1184 enum thermal_trip_type trip_type;
81ad4276 1185 int trip_temp;
203d3d4a
ZR
1186 int result;
1187 int count;
e33df1d2 1188 struct thermal_governor *governor;
203d3d4a 1189
54fa38cc
EV
1190 if (!type || strlen(type) == 0)
1191 return ERR_PTR(-EINVAL);
1192
204dd1d3 1193 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1194 return ERR_PTR(-EINVAL);
203d3d4a 1195
c56f5c03 1196 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
3e6fda5c 1197 return ERR_PTR(-EINVAL);
203d3d4a 1198
81bd4e1c 1199 if (!ops)
3e6fda5c 1200 return ERR_PTR(-EINVAL);
203d3d4a 1201
83720d0b 1202 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
6b2aa51d
EV
1203 return ERR_PTR(-EINVAL);
1204
95e3ed15 1205 tz = kzalloc(sizeof(*tz), GFP_KERNEL);
203d3d4a 1206 if (!tz)
3e6fda5c 1207 return ERR_PTR(-ENOMEM);
203d3d4a 1208
2d374139 1209 INIT_LIST_HEAD(&tz->thermal_instances);
b31ef828 1210 ida_init(&tz->ida);
203d3d4a 1211 mutex_init(&tz->lock);
b31ef828 1212 result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL);
9d9ca1f9
CJ
1213 if (result < 0)
1214 goto free_tz;
203d3d4a 1215
b31ef828 1216 tz->id = result;
54fa38cc 1217 strlcpy(tz->type, type, sizeof(tz->type));
203d3d4a 1218 tz->ops = ops;
50125a9b 1219 tz->tzp = tzp;
203d3d4a
ZR
1220 tz->device.class = &thermal_class;
1221 tz->devdata = devdata;
1222 tz->trips = trips;
b1569e99
MG
1223 tz->passive_delay = passive_delay;
1224 tz->polling_delay = polling_delay;
1c600861 1225
4d0fe749 1226 /* sys I/F */
1c600861 1227 /* Add nodes that are always present via .groups */
4d0fe749
EV
1228 result = thermal_zone_create_device_groups(tz, mask);
1229 if (result)
9d9ca1f9 1230 goto remove_id;
4d0fe749 1231
4511f716
CY
1232 /* A new thermal zone needs to be updated anyway. */
1233 atomic_set(&tz->need_update, 1);
b1569e99 1234
354655ea 1235 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a 1236 result = device_register(&tz->device);
9d9ca1f9
CJ
1237 if (result)
1238 goto remove_device_groups;
203d3d4a 1239
203d3d4a 1240 for (count = 0; count < trips; count++) {
81ad4276
ZR
1241 if (tz->ops->get_trip_type(tz, count, &trip_type))
1242 set_bit(count, &tz->trips_disabled);
81ad4276
ZR
1243 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1244 set_bit(count, &tz->trips_disabled);
1245 /* Check for bogus trip points */
1246 if (trip_temp == 0)
1247 set_bit(count, &tz->trips_disabled);
203d3d4a
ZR
1248 }
1249
a4a15485
D
1250 /* Update 'this' zone's governor information */
1251 mutex_lock(&thermal_governor_lock);
1252
1253 if (tz->tzp)
e33df1d2 1254 governor = __find_governor(tz->tzp->governor_name);
a4a15485 1255 else
e33df1d2
JM
1256 governor = def_governor;
1257
1258 result = thermal_set_governor(tz, governor);
1259 if (result) {
1260 mutex_unlock(&thermal_governor_lock);
1261 goto unregister;
1262 }
a4a15485
D
1263
1264 mutex_unlock(&thermal_governor_lock);
1265
ccba4ffd
EV
1266 if (!tz->tzp || !tz->tzp->no_hwmon) {
1267 result = thermal_add_hwmon_sysfs(tz);
1268 if (result)
1269 goto unregister;
1270 }
e68b16ab 1271
203d3d4a
ZR
1272 mutex_lock(&thermal_list_lock);
1273 list_add_tail(&tz->node, &thermal_tz_list);
203d3d4a
ZR
1274 mutex_unlock(&thermal_list_lock);
1275
7e8ee1e9
D
1276 /* Bind cooling devices for this zone */
1277 bind_tz(tz);
1278
b659a30d 1279 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
b1569e99 1280
bb431ba2 1281 thermal_zone_device_reset(tz);
4511f716
CY
1282 /* Update the new thermal zone and mark it as already updated. */
1283 if (atomic_cmpxchg(&tz->need_update, 1, 0))
0e70f466 1284 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
b1569e99 1285
14015860 1286 return tz;
203d3d4a 1287
caca8b80 1288unregister:
b31ef828 1289 ida_simple_remove(&thermal_tz_ida, tz->id);
203d3d4a 1290 device_unregister(&tz->device);
3e6fda5c 1291 return ERR_PTR(result);
9d9ca1f9
CJ
1292
1293remove_device_groups:
1294 thermal_zone_destroy_device_groups(tz);
1295remove_id:
1296 ida_simple_remove(&thermal_tz_ida, tz->id);
1297free_tz:
1298 kfree(tz);
1299 return ERR_PTR(result);
203d3d4a 1300}
910cb1e3 1301EXPORT_SYMBOL_GPL(thermal_zone_device_register);
203d3d4a
ZR
1302
1303/**
1304 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1305 * @tz: the thermal zone device to remove
1306 */
1307void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1308{
7e8ee1e9
D
1309 int i;
1310 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1311 struct thermal_cooling_device *cdev;
1312 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1313
1314 if (!tz)
1315 return;
1316
7e8ee1e9
D
1317 tzp = tz->tzp;
1318
203d3d4a
ZR
1319 mutex_lock(&thermal_list_lock);
1320 list_for_each_entry(pos, &thermal_tz_list, node)
b659a30d
EV
1321 if (pos == tz)
1322 break;
203d3d4a
ZR
1323 if (pos != tz) {
1324 /* thermal zone device not found */
1325 mutex_unlock(&thermal_list_lock);
1326 return;
1327 }
1328 list_del(&tz->node);
7e8ee1e9
D
1329
1330 /* Unbind all cdevs associated with 'this' thermal zone */
1331 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1332 if (tz->ops->unbind) {
1333 tz->ops->unbind(tz, cdev);
1334 continue;
1335 }
1336
1337 if (!tzp || !tzp->tbp)
1338 break;
1339
1340 for (i = 0; i < tzp->num_tbps; i++) {
1341 if (tzp->tbp[i].cdev == cdev) {
1342 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1343 tzp->tbp[i].cdev = NULL;
1344 }
1345 }
1346 }
1347
203d3d4a
ZR
1348 mutex_unlock(&thermal_list_lock);
1349
b1569e99
MG
1350 thermal_zone_device_set_polling(tz, 0);
1351
e33df1d2 1352 thermal_set_governor(tz, NULL);
203d3d4a 1353
e68b16ab 1354 thermal_remove_hwmon_sysfs(tz);
b31ef828
MW
1355 ida_simple_remove(&thermal_tz_ida, tz->id);
1356 ida_destroy(&tz->ida);
203d3d4a
ZR
1357 mutex_destroy(&tz->lock);
1358 device_unregister(&tz->device);
203d3d4a 1359}
910cb1e3 1360EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
203d3d4a 1361
63c4d919
EV
1362/**
1363 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1364 * @name: thermal zone name to fetch the temperature
1365 *
1366 * When only one zone is found with the passed name, returns a reference to it.
1367 *
1368 * Return: On success returns a reference to an unique thermal zone with
1369 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1370 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1371 */
1372struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1373{
1374 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1375 unsigned int found = 0;
1376
1377 if (!name)
1378 goto exit;
1379
1380 mutex_lock(&thermal_list_lock);
1381 list_for_each_entry(pos, &thermal_tz_list, node)
484ac2f3 1382 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
63c4d919
EV
1383 found++;
1384 ref = pos;
1385 }
1386 mutex_unlock(&thermal_list_lock);
1387
1388 /* nothing has been found, thus an error code for it */
1389 if (found == 0)
1390 ref = ERR_PTR(-ENODEV);
1391 else if (found > 1)
1392 /* Success only when an unique zone is found */
1393 ref = ERR_PTR(-EEXIST);
1394
1395exit:
1396 return ref;
1397}
1398EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1399
af06216a 1400#ifdef CONFIG_NET
2a94fe48
JB
1401static const struct genl_multicast_group thermal_event_mcgrps[] = {
1402 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1403};
1404
56989f6d 1405static struct genl_family thermal_event_genl_family __ro_after_init = {
489111e5 1406 .module = THIS_MODULE,
af06216a
RW
1407 .name = THERMAL_GENL_FAMILY_NAME,
1408 .version = THERMAL_GENL_VERSION,
1409 .maxattr = THERMAL_GENL_ATTR_MAX,
2a94fe48
JB
1410 .mcgrps = thermal_event_mcgrps,
1411 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
af06216a
RW
1412};
1413
8ab3e6a0 1414int thermal_generate_netlink_event(struct thermal_zone_device *tz,
eb7be329 1415 enum events event)
4cb18728
D
1416{
1417 struct sk_buff *skb;
1418 struct nlattr *attr;
1419 struct thermal_genl_event *thermal_event;
1420 void *msg_header;
1421 int size;
1422 int result;
b11de07c 1423 static unsigned int thermal_event_seqnum;
4cb18728 1424
8ab3e6a0
EV
1425 if (!tz)
1426 return -EINVAL;
1427
4cb18728 1428 /* allocate memory */
886ee546
JP
1429 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1430 nla_total_size(0);
4cb18728
D
1431
1432 skb = genlmsg_new(size, GFP_ATOMIC);
1433 if (!skb)
1434 return -ENOMEM;
1435
1436 /* add the genetlink message header */
1437 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1438 &thermal_event_genl_family, 0,
1439 THERMAL_GENL_CMD_EVENT);
1440 if (!msg_header) {
1441 nlmsg_free(skb);
1442 return -ENOMEM;
1443 }
1444
1445 /* fill the data */
886ee546
JP
1446 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1447 sizeof(struct thermal_genl_event));
4cb18728
D
1448
1449 if (!attr) {
1450 nlmsg_free(skb);
1451 return -EINVAL;
1452 }
1453
1454 thermal_event = nla_data(attr);
1455 if (!thermal_event) {
1456 nlmsg_free(skb);
1457 return -EINVAL;
1458 }
1459
1460 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1461
8ab3e6a0 1462 thermal_event->orig = tz->id;
4cb18728
D
1463 thermal_event->event = event;
1464
1465 /* send multicast genetlink message */
053c095a 1466 genlmsg_end(skb, msg_header);
4cb18728 1467
68eb5503 1468 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2a94fe48 1469 0, GFP_ATOMIC);
4cb18728 1470 if (result)
923e0b1e 1471 dev_err(&tz->device, "Failed to send netlink event:%d", result);
4cb18728
D
1472
1473 return result;
1474}
910cb1e3 1475EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
4cb18728 1476
56989f6d 1477static int __init genetlink_init(void)
4cb18728 1478{
2a94fe48 1479 return genl_register_family(&thermal_event_genl_family);
4cb18728
D
1480}
1481
af06216a
RW
1482static void genetlink_exit(void)
1483{
1484 genl_unregister_family(&thermal_event_genl_family);
1485}
1486#else /* !CONFIG_NET */
1487static inline int genetlink_init(void) { return 0; }
1488static inline void genetlink_exit(void) {}
1489#endif /* !CONFIG_NET */
1490
ff140fea 1491static int thermal_pm_notify(struct notifier_block *nb,
eb7be329 1492 unsigned long mode, void *_unused)
ff140fea
ZR
1493{
1494 struct thermal_zone_device *tz;
1495
1496 switch (mode) {
1497 case PM_HIBERNATION_PREPARE:
1498 case PM_RESTORE_PREPARE:
1499 case PM_SUSPEND_PREPARE:
1500 atomic_set(&in_suspend, 1);
1501 break;
1502 case PM_POST_HIBERNATION:
1503 case PM_POST_RESTORE:
1504 case PM_POST_SUSPEND:
1505 atomic_set(&in_suspend, 0);
1506 list_for_each_entry(tz, &thermal_tz_list, node) {
1507 thermal_zone_device_reset(tz);
0e70f466
SP
1508 thermal_zone_device_update(tz,
1509 THERMAL_EVENT_UNSPECIFIED);
ff140fea
ZR
1510 }
1511 break;
1512 default:
1513 break;
1514 }
1515 return 0;
1516}
1517
1518static struct notifier_block thermal_pm_nb = {
1519 .notifier_call = thermal_pm_notify,
1520};
1521
203d3d4a
ZR
1522static int __init thermal_init(void)
1523{
80a26a5c
ZR
1524 int result;
1525
e441fd68 1526 mutex_init(&poweroff_lock);
80a26a5c
ZR
1527 result = thermal_register_governors();
1528 if (result)
1529 goto error;
203d3d4a
ZR
1530
1531 result = class_register(&thermal_class);
80a26a5c
ZR
1532 if (result)
1533 goto unregister_governors;
1534
4cb18728 1535 result = genetlink_init();
80a26a5c
ZR
1536 if (result)
1537 goto unregister_class;
1538
4e5e4705
EV
1539 result = of_parse_thermal_zones();
1540 if (result)
1541 goto exit_netlink;
1542
ff140fea
ZR
1543 result = register_pm_notifier(&thermal_pm_nb);
1544 if (result)
1545 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1546 result);
1547
80a26a5c
ZR
1548 return 0;
1549
4e5e4705
EV
1550exit_netlink:
1551 genetlink_exit();
80a26a5c
ZR
1552unregister_class:
1553 class_unregister(&thermal_class);
9d367e5e
LH
1554unregister_governors:
1555 thermal_unregister_governors();
80a26a5c 1556error:
b31ef828
MW
1557 ida_destroy(&thermal_tz_ida);
1558 ida_destroy(&thermal_cdev_ida);
80a26a5c
ZR
1559 mutex_destroy(&thermal_list_lock);
1560 mutex_destroy(&thermal_governor_lock);
e441fd68 1561 mutex_destroy(&poweroff_lock);
203d3d4a
ZR
1562 return result;
1563}
1564
1565static void __exit thermal_exit(void)
1566{
ff140fea 1567 unregister_pm_notifier(&thermal_pm_nb);
4e5e4705 1568 of_thermal_destroy_zones();
80a26a5c 1569 genetlink_exit();
203d3d4a 1570 class_unregister(&thermal_class);
80a26a5c 1571 thermal_unregister_governors();
b31ef828
MW
1572 ida_destroy(&thermal_tz_ida);
1573 ida_destroy(&thermal_cdev_ida);
203d3d4a 1574 mutex_destroy(&thermal_list_lock);
80a26a5c 1575 mutex_destroy(&thermal_governor_lock);
203d3d4a
ZR
1576}
1577
4cb18728 1578fs_initcall(thermal_init);
203d3d4a 1579module_exit(thermal_exit);