thermal: exynos: Clean up non-DT remnants
[deliverable/linux.git] / drivers / thermal / thermal_core.c
CommitLineData
203d3d4a
ZR
1/*
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 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
c5a01dd5
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
203d3d4a
ZR
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
5a0e3ad6 31#include <linux/slab.h>
203d3d4a
ZR
32#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
b1569e99 35#include <linux/reboot.h>
42a5bf50 36#include <linux/string.h>
4cb18728
D
37#include <net/netlink.h>
38#include <net/genetlink.h>
203d3d4a 39
71350db4
D
40#include "thermal_core.h"
41
63c4ec90 42MODULE_AUTHOR("Zhang Rui");
203d3d4a 43MODULE_DESCRIPTION("Generic thermal management sysfs support");
6d8d4974 44MODULE_LICENSE("GPL v2");
203d3d4a 45
203d3d4a
ZR
46static DEFINE_IDR(thermal_tz_idr);
47static DEFINE_IDR(thermal_cdev_idr);
48static DEFINE_MUTEX(thermal_idr_lock);
49
50static LIST_HEAD(thermal_tz_list);
51static LIST_HEAD(thermal_cdev_list);
a4a15485
D
52static LIST_HEAD(thermal_governor_list);
53
203d3d4a 54static DEFINE_MUTEX(thermal_list_lock);
a4a15485
D
55static DEFINE_MUTEX(thermal_governor_lock);
56
57static struct thermal_governor *__find_governor(const char *name)
58{
59 struct thermal_governor *pos;
60
61 list_for_each_entry(pos, &thermal_governor_list, governor_list)
62 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
63 return pos;
64
65 return NULL;
66}
67
68int thermal_register_governor(struct thermal_governor *governor)
69{
70 int err;
71 const char *name;
72 struct thermal_zone_device *pos;
73
74 if (!governor)
75 return -EINVAL;
76
77 mutex_lock(&thermal_governor_lock);
78
79 err = -EBUSY;
80 if (__find_governor(governor->name) == NULL) {
81 err = 0;
82 list_add(&governor->governor_list, &thermal_governor_list);
83 }
84
85 mutex_lock(&thermal_list_lock);
86
87 list_for_each_entry(pos, &thermal_tz_list, node) {
88 if (pos->governor)
89 continue;
90 if (pos->tzp)
91 name = pos->tzp->governor_name;
92 else
93 name = DEFAULT_THERMAL_GOVERNOR;
94 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
95 pos->governor = governor;
96 }
97
98 mutex_unlock(&thermal_list_lock);
99 mutex_unlock(&thermal_governor_lock);
100
101 return err;
102}
a4a15485
D
103
104void thermal_unregister_governor(struct thermal_governor *governor)
105{
106 struct thermal_zone_device *pos;
107
108 if (!governor)
109 return;
110
111 mutex_lock(&thermal_governor_lock);
112
113 if (__find_governor(governor->name) == NULL)
114 goto exit;
115
116 mutex_lock(&thermal_list_lock);
117
118 list_for_each_entry(pos, &thermal_tz_list, node) {
119 if (!strnicmp(pos->governor->name, governor->name,
120 THERMAL_NAME_LENGTH))
121 pos->governor = NULL;
122 }
123
124 mutex_unlock(&thermal_list_lock);
125 list_del(&governor->governor_list);
126exit:
127 mutex_unlock(&thermal_governor_lock);
128 return;
129}
203d3d4a
ZR
130
131static int get_idr(struct idr *idr, struct mutex *lock, int *id)
132{
6deb69fa 133 int ret;
203d3d4a
ZR
134
135 if (lock)
136 mutex_lock(lock);
6deb69fa 137 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
203d3d4a
ZR
138 if (lock)
139 mutex_unlock(lock);
6deb69fa
TH
140 if (unlikely(ret < 0))
141 return ret;
142 *id = ret;
203d3d4a
ZR
143 return 0;
144}
145
146static void release_idr(struct idr *idr, struct mutex *lock, int id)
147{
148 if (lock)
149 mutex_lock(lock);
150 idr_remove(idr, id);
151 if (lock)
152 mutex_unlock(lock);
153}
154
9b4298a0
D
155int get_tz_trend(struct thermal_zone_device *tz, int trip)
156{
157 enum thermal_trend trend;
158
0c872507
EV
159 if (tz->emul_temperature || !tz->ops->get_trend ||
160 tz->ops->get_trend(tz, trip, &trend)) {
9b4298a0
D
161 if (tz->temperature > tz->last_temperature)
162 trend = THERMAL_TREND_RAISING;
163 else if (tz->temperature < tz->last_temperature)
164 trend = THERMAL_TREND_DROPPING;
165 else
166 trend = THERMAL_TREND_STABLE;
167 }
168
169 return trend;
170}
171EXPORT_SYMBOL(get_tz_trend);
172
173struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
174 struct thermal_cooling_device *cdev, int trip)
175{
176 struct thermal_instance *pos = NULL;
177 struct thermal_instance *target_instance = NULL;
178
179 mutex_lock(&tz->lock);
180 mutex_lock(&cdev->lock);
181
182 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
183 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
184 target_instance = pos;
185 break;
186 }
187 }
188
189 mutex_unlock(&cdev->lock);
190 mutex_unlock(&tz->lock);
191
192 return target_instance;
193}
194EXPORT_SYMBOL(get_thermal_instance);
195
7e8ee1e9
D
196static void print_bind_err_msg(struct thermal_zone_device *tz,
197 struct thermal_cooling_device *cdev, int ret)
198{
199 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
200 tz->type, cdev->type, ret);
201}
202
203static void __bind(struct thermal_zone_device *tz, int mask,
204 struct thermal_cooling_device *cdev)
205{
206 int i, ret;
207
208 for (i = 0; i < tz->trips; i++) {
209 if (mask & (1 << i)) {
210 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
211 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
212 if (ret)
213 print_bind_err_msg(tz, cdev, ret);
214 }
215 }
216}
217
218static void __unbind(struct thermal_zone_device *tz, int mask,
219 struct thermal_cooling_device *cdev)
220{
221 int i;
222
223 for (i = 0; i < tz->trips; i++)
224 if (mask & (1 << i))
225 thermal_zone_unbind_cooling_device(tz, i, cdev);
226}
227
228static void bind_cdev(struct thermal_cooling_device *cdev)
229{
230 int i, ret;
231 const struct thermal_zone_params *tzp;
232 struct thermal_zone_device *pos = NULL;
233
234 mutex_lock(&thermal_list_lock);
235
236 list_for_each_entry(pos, &thermal_tz_list, node) {
237 if (!pos->tzp && !pos->ops->bind)
238 continue;
239
240 if (!pos->tzp && pos->ops->bind) {
241 ret = pos->ops->bind(pos, cdev);
242 if (ret)
243 print_bind_err_msg(pos, cdev, ret);
244 }
245
246 tzp = pos->tzp;
791700cd
HD
247 if (!tzp || !tzp->tbp)
248 continue;
7e8ee1e9
D
249
250 for (i = 0; i < tzp->num_tbps; i++) {
251 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
252 continue;
253 if (tzp->tbp[i].match(pos, cdev))
254 continue;
255 tzp->tbp[i].cdev = cdev;
256 __bind(pos, tzp->tbp[i].trip_mask, cdev);
257 }
258 }
259
260 mutex_unlock(&thermal_list_lock);
261}
262
263static void bind_tz(struct thermal_zone_device *tz)
264{
265 int i, ret;
266 struct thermal_cooling_device *pos = NULL;
267 const struct thermal_zone_params *tzp = tz->tzp;
268
269 if (!tzp && !tz->ops->bind)
270 return;
271
272 mutex_lock(&thermal_list_lock);
273
274 /* If there is no platform data, try to use ops->bind */
275 if (!tzp && tz->ops->bind) {
276 list_for_each_entry(pos, &thermal_cdev_list, node) {
277 ret = tz->ops->bind(tz, pos);
278 if (ret)
279 print_bind_err_msg(tz, pos, ret);
280 }
281 goto exit;
282 }
283
791700cd 284 if (!tzp || !tzp->tbp)
7e8ee1e9
D
285 goto exit;
286
287 list_for_each_entry(pos, &thermal_cdev_list, node) {
288 for (i = 0; i < tzp->num_tbps; i++) {
289 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
290 continue;
291 if (tzp->tbp[i].match(tz, pos))
292 continue;
293 tzp->tbp[i].cdev = pos;
294 __bind(tz, tzp->tbp[i].trip_mask, pos);
295 }
296 }
297exit:
298 mutex_unlock(&thermal_list_lock);
299}
300
0c01ebbf
D
301static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
302 int delay)
303{
304 if (delay > 1000)
305 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
306 round_jiffies(msecs_to_jiffies(delay)));
307 else if (delay)
308 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
309 msecs_to_jiffies(delay));
310 else
311 cancel_delayed_work(&tz->poll_queue);
312}
313
314static void monitor_thermal_zone(struct thermal_zone_device *tz)
315{
316 mutex_lock(&tz->lock);
317
318 if (tz->passive)
319 thermal_zone_device_set_polling(tz, tz->passive_delay);
320 else if (tz->polling_delay)
321 thermal_zone_device_set_polling(tz, tz->polling_delay);
322 else
323 thermal_zone_device_set_polling(tz, 0);
324
325 mutex_unlock(&tz->lock);
326}
327
328static void handle_non_critical_trips(struct thermal_zone_device *tz,
329 int trip, enum thermal_trip_type trip_type)
330{
d567c686
ZR
331 if (tz->governor)
332 tz->governor->throttle(tz, trip);
0c01ebbf
D
333}
334
335static void handle_critical_trips(struct thermal_zone_device *tz,
336 int trip, enum thermal_trip_type trip_type)
337{
338 long trip_temp;
339
340 tz->ops->get_trip_temp(tz, trip, &trip_temp);
341
342 /* If we have not crossed the trip_temp, we do not care. */
343 if (tz->temperature < trip_temp)
344 return;
345
346 if (tz->ops->notify)
347 tz->ops->notify(tz, trip, trip_type);
348
349 if (trip_type == THERMAL_TRIP_CRITICAL) {
923e0b1e
EV
350 dev_emerg(&tz->device,
351 "critical temperature reached(%d C),shutting down\n",
352 tz->temperature / 1000);
0c01ebbf
D
353 orderly_poweroff(true);
354 }
355}
356
357static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
358{
359 enum thermal_trip_type type;
360
361 tz->ops->get_trip_type(tz, trip, &type);
362
363 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
364 handle_critical_trips(tz, trip, type);
365 else
366 handle_non_critical_trips(tz, trip, type);
367 /*
368 * Alright, we handled this trip successfully.
369 * So, start monitoring again.
370 */
371 monitor_thermal_zone(tz);
372}
373
837b26bb
EV
374/**
375 * thermal_zone_get_temp() - returns its the temperature of thermal zone
376 * @tz: a valid pointer to a struct thermal_zone_device
377 * @temp: a valid pointer to where to store the resulting temperature.
378 *
379 * When a valid thermal zone reference is passed, it will fetch its
380 * temperature and fill @temp.
381 *
382 * Return: On success returns 0, an error code otherwise
383 */
384int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
e6e238c3 385{
837b26bb 386 int ret = -EINVAL;
5e20b2e5
ZR
387#ifdef CONFIG_THERMAL_EMULATION
388 int count;
e6e238c3
ADK
389 unsigned long crit_temp = -1UL;
390 enum thermal_trip_type type;
5e20b2e5 391#endif
e6e238c3 392
9b19ec39 393 if (!tz || IS_ERR(tz))
837b26bb
EV
394 goto exit;
395
e6e238c3
ADK
396 mutex_lock(&tz->lock);
397
398 ret = tz->ops->get_temp(tz, temp);
399#ifdef CONFIG_THERMAL_EMULATION
400 if (!tz->emul_temperature)
401 goto skip_emul;
402
403 for (count = 0; count < tz->trips; count++) {
404 ret = tz->ops->get_trip_type(tz, count, &type);
405 if (!ret && type == THERMAL_TRIP_CRITICAL) {
406 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
407 break;
408 }
409 }
410
411 if (ret)
412 goto skip_emul;
413
414 if (*temp < crit_temp)
415 *temp = tz->emul_temperature;
416skip_emul:
417#endif
418 mutex_unlock(&tz->lock);
837b26bb 419exit:
e6e238c3
ADK
420 return ret;
421}
837b26bb 422EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
e6e238c3 423
0c01ebbf
D
424static void update_temperature(struct thermal_zone_device *tz)
425{
426 long temp;
427 int ret;
428
e6e238c3 429 ret = thermal_zone_get_temp(tz, &temp);
0c01ebbf 430 if (ret) {
923e0b1e
EV
431 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
432 tz->id);
e6e238c3 433 return;
0c01ebbf
D
434 }
435
e6e238c3 436 mutex_lock(&tz->lock);
0c01ebbf
D
437 tz->last_temperature = tz->temperature;
438 tz->temperature = temp;
0c01ebbf
D
439 mutex_unlock(&tz->lock);
440}
441
442void thermal_zone_device_update(struct thermal_zone_device *tz)
443{
444 int count;
445
446 update_temperature(tz);
447
448 for (count = 0; count < tz->trips; count++)
449 handle_thermal_trip(tz, count);
450}
910cb1e3 451EXPORT_SYMBOL_GPL(thermal_zone_device_update);
0c01ebbf
D
452
453static void thermal_zone_device_check(struct work_struct *work)
454{
455 struct thermal_zone_device *tz = container_of(work, struct
456 thermal_zone_device,
457 poll_queue.work);
458 thermal_zone_device_update(tz);
459}
460
203d3d4a
ZR
461/* sys I/F for thermal zone */
462
463#define to_thermal_zone(_dev) \
464 container_of(_dev, struct thermal_zone_device, device)
465
466static ssize_t
467type_show(struct device *dev, struct device_attribute *attr, char *buf)
468{
469 struct thermal_zone_device *tz = to_thermal_zone(dev);
470
471 return sprintf(buf, "%s\n", tz->type);
472}
473
474static ssize_t
475temp_show(struct device *dev, struct device_attribute *attr, char *buf)
476{
477 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
478 long temperature;
479 int ret;
203d3d4a 480
e6e238c3 481 ret = thermal_zone_get_temp(tz, &temperature);
6503e5df
MG
482
483 if (ret)
484 return ret;
485
486 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
487}
488
489static ssize_t
490mode_show(struct device *dev, struct device_attribute *attr, char *buf)
491{
492 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
493 enum thermal_device_mode mode;
494 int result;
203d3d4a
ZR
495
496 if (!tz->ops->get_mode)
497 return -EPERM;
498
6503e5df
MG
499 result = tz->ops->get_mode(tz, &mode);
500 if (result)
501 return result;
502
503 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
504 : "disabled");
203d3d4a
ZR
505}
506
507static ssize_t
508mode_store(struct device *dev, struct device_attribute *attr,
509 const char *buf, size_t count)
510{
511 struct thermal_zone_device *tz = to_thermal_zone(dev);
512 int result;
513
514 if (!tz->ops->set_mode)
515 return -EPERM;
516
f1f0e2ac 517 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
6503e5df 518 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
f1f0e2ac 519 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
6503e5df
MG
520 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
521 else
522 result = -EINVAL;
523
203d3d4a
ZR
524 if (result)
525 return result;
526
527 return count;
528}
529
530static ssize_t
531trip_point_type_show(struct device *dev, struct device_attribute *attr,
532 char *buf)
533{
534 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
535 enum thermal_trip_type type;
536 int trip, result;
203d3d4a
ZR
537
538 if (!tz->ops->get_trip_type)
539 return -EPERM;
540
541 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
542 return -EINVAL;
543
6503e5df
MG
544 result = tz->ops->get_trip_type(tz, trip, &type);
545 if (result)
546 return result;
547
548 switch (type) {
549 case THERMAL_TRIP_CRITICAL:
625120a4 550 return sprintf(buf, "critical\n");
6503e5df 551 case THERMAL_TRIP_HOT:
625120a4 552 return sprintf(buf, "hot\n");
6503e5df 553 case THERMAL_TRIP_PASSIVE:
625120a4 554 return sprintf(buf, "passive\n");
6503e5df 555 case THERMAL_TRIP_ACTIVE:
625120a4 556 return sprintf(buf, "active\n");
6503e5df 557 default:
625120a4 558 return sprintf(buf, "unknown\n");
6503e5df 559 }
203d3d4a
ZR
560}
561
c56f5c03
D
562static ssize_t
563trip_point_temp_store(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
565{
566 struct thermal_zone_device *tz = to_thermal_zone(dev);
567 int trip, ret;
568 unsigned long temperature;
569
570 if (!tz->ops->set_trip_temp)
571 return -EPERM;
572
573 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
574 return -EINVAL;
575
576 if (kstrtoul(buf, 10, &temperature))
577 return -EINVAL;
578
579 ret = tz->ops->set_trip_temp(tz, trip, temperature);
580
581 return ret ? ret : count;
582}
583
203d3d4a
ZR
584static ssize_t
585trip_point_temp_show(struct device *dev, struct device_attribute *attr,
586 char *buf)
587{
588 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
589 int trip, ret;
590 long temperature;
203d3d4a
ZR
591
592 if (!tz->ops->get_trip_temp)
593 return -EPERM;
594
595 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
596 return -EINVAL;
597
6503e5df
MG
598 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
599
600 if (ret)
601 return ret;
602
603 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
604}
605
27365a6c
D
606static ssize_t
607trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
609{
610 struct thermal_zone_device *tz = to_thermal_zone(dev);
611 int trip, ret;
612 unsigned long temperature;
613
614 if (!tz->ops->set_trip_hyst)
615 return -EPERM;
616
617 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
618 return -EINVAL;
619
620 if (kstrtoul(buf, 10, &temperature))
621 return -EINVAL;
622
623 /*
624 * We are not doing any check on the 'temperature' value
625 * here. The driver implementing 'set_trip_hyst' has to
626 * take care of this.
627 */
628 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
629
630 return ret ? ret : count;
631}
632
633static ssize_t
634trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
635 char *buf)
636{
637 struct thermal_zone_device *tz = to_thermal_zone(dev);
638 int trip, ret;
639 unsigned long temperature;
640
641 if (!tz->ops->get_trip_hyst)
642 return -EPERM;
643
644 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
645 return -EINVAL;
646
647 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
648
649 return ret ? ret : sprintf(buf, "%ld\n", temperature);
650}
651
03a971a2
MG
652static ssize_t
653passive_store(struct device *dev, struct device_attribute *attr,
654 const char *buf, size_t count)
655{
656 struct thermal_zone_device *tz = to_thermal_zone(dev);
657 struct thermal_cooling_device *cdev = NULL;
658 int state;
659
660 if (!sscanf(buf, "%d\n", &state))
661 return -EINVAL;
662
3d8e3ad8
FP
663 /* sanity check: values below 1000 millicelcius don't make sense
664 * and can cause the system to go into a thermal heart attack
665 */
666 if (state && state < 1000)
667 return -EINVAL;
668
03a971a2
MG
669 if (state && !tz->forced_passive) {
670 mutex_lock(&thermal_list_lock);
671 list_for_each_entry(cdev, &thermal_cdev_list, node) {
672 if (!strncmp("Processor", cdev->type,
673 sizeof("Processor")))
674 thermal_zone_bind_cooling_device(tz,
9d99842f
ZR
675 THERMAL_TRIPS_NONE, cdev,
676 THERMAL_NO_LIMIT,
677 THERMAL_NO_LIMIT);
03a971a2
MG
678 }
679 mutex_unlock(&thermal_list_lock);
e4143b03
FP
680 if (!tz->passive_delay)
681 tz->passive_delay = 1000;
03a971a2
MG
682 } else if (!state && tz->forced_passive) {
683 mutex_lock(&thermal_list_lock);
684 list_for_each_entry(cdev, &thermal_cdev_list, node) {
685 if (!strncmp("Processor", cdev->type,
686 sizeof("Processor")))
687 thermal_zone_unbind_cooling_device(tz,
688 THERMAL_TRIPS_NONE,
689 cdev);
690 }
691 mutex_unlock(&thermal_list_lock);
e4143b03 692 tz->passive_delay = 0;
03a971a2
MG
693 }
694
03a971a2
MG
695 tz->forced_passive = state;
696
697 thermal_zone_device_update(tz);
698
699 return count;
700}
701
702static ssize_t
703passive_show(struct device *dev, struct device_attribute *attr,
704 char *buf)
705{
706 struct thermal_zone_device *tz = to_thermal_zone(dev);
707
708 return sprintf(buf, "%d\n", tz->forced_passive);
709}
710
5a2c090b
D
711static ssize_t
712policy_store(struct device *dev, struct device_attribute *attr,
713 const char *buf, size_t count)
714{
715 int ret = -EINVAL;
716 struct thermal_zone_device *tz = to_thermal_zone(dev);
717 struct thermal_governor *gov;
42a5bf50
AS
718 char name[THERMAL_NAME_LENGTH];
719
720 snprintf(name, sizeof(name), "%s", buf);
5a2c090b
D
721
722 mutex_lock(&thermal_governor_lock);
723
42a5bf50 724 gov = __find_governor(strim(name));
5a2c090b
D
725 if (!gov)
726 goto exit;
727
728 tz->governor = gov;
729 ret = count;
730
731exit:
732 mutex_unlock(&thermal_governor_lock);
733 return ret;
734}
735
736static ssize_t
737policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
738{
739 struct thermal_zone_device *tz = to_thermal_zone(dev);
740
741 return sprintf(buf, "%s\n", tz->governor->name);
742}
743
e6e238c3
ADK
744#ifdef CONFIG_THERMAL_EMULATION
745static ssize_t
746emul_temp_store(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count)
748{
749 struct thermal_zone_device *tz = to_thermal_zone(dev);
750 int ret = 0;
751 unsigned long temperature;
752
753 if (kstrtoul(buf, 10, &temperature))
754 return -EINVAL;
755
756 if (!tz->ops->set_emul_temp) {
757 mutex_lock(&tz->lock);
758 tz->emul_temperature = temperature;
759 mutex_unlock(&tz->lock);
760 } else {
761 ret = tz->ops->set_emul_temp(tz, temperature);
762 }
763
764 return ret ? ret : count;
765}
766static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
767#endif/*CONFIG_THERMAL_EMULATION*/
768
203d3d4a
ZR
769static DEVICE_ATTR(type, 0444, type_show, NULL);
770static DEVICE_ATTR(temp, 0444, temp_show, NULL);
771static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
886ee546 772static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
5a2c090b 773static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
203d3d4a 774
203d3d4a
ZR
775/* sys I/F for cooling device */
776#define to_cooling_device(_dev) \
777 container_of(_dev, struct thermal_cooling_device, device)
778
779static ssize_t
780thermal_cooling_device_type_show(struct device *dev,
781 struct device_attribute *attr, char *buf)
782{
783 struct thermal_cooling_device *cdev = to_cooling_device(dev);
784
785 return sprintf(buf, "%s\n", cdev->type);
786}
787
788static ssize_t
789thermal_cooling_device_max_state_show(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
792 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
793 unsigned long state;
794 int ret;
203d3d4a 795
6503e5df
MG
796 ret = cdev->ops->get_max_state(cdev, &state);
797 if (ret)
798 return ret;
799 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
800}
801
802static ssize_t
803thermal_cooling_device_cur_state_show(struct device *dev,
804 struct device_attribute *attr, char *buf)
805{
806 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
807 unsigned long state;
808 int ret;
203d3d4a 809
6503e5df
MG
810 ret = cdev->ops->get_cur_state(cdev, &state);
811 if (ret)
812 return ret;
813 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
814}
815
816static ssize_t
817thermal_cooling_device_cur_state_store(struct device *dev,
818 struct device_attribute *attr,
819 const char *buf, size_t count)
820{
821 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df 822 unsigned long state;
203d3d4a
ZR
823 int result;
824
6503e5df 825 if (!sscanf(buf, "%ld\n", &state))
203d3d4a
ZR
826 return -EINVAL;
827
edb94918 828 if ((long)state < 0)
203d3d4a
ZR
829 return -EINVAL;
830
831 result = cdev->ops->set_cur_state(cdev, state);
832 if (result)
833 return result;
834 return count;
835}
836
837static struct device_attribute dev_attr_cdev_type =
543a9561 838__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
203d3d4a
ZR
839static DEVICE_ATTR(max_state, 0444,
840 thermal_cooling_device_max_state_show, NULL);
841static DEVICE_ATTR(cur_state, 0644,
842 thermal_cooling_device_cur_state_show,
843 thermal_cooling_device_cur_state_store);
844
845static ssize_t
846thermal_cooling_device_trip_point_show(struct device *dev,
543a9561 847 struct device_attribute *attr, char *buf)
203d3d4a 848{
b81b6ba3 849 struct thermal_instance *instance;
203d3d4a
ZR
850
851 instance =
b81b6ba3 852 container_of(attr, struct thermal_instance, attr);
203d3d4a
ZR
853
854 if (instance->trip == THERMAL_TRIPS_NONE)
855 return sprintf(buf, "-1\n");
856 else
857 return sprintf(buf, "%d\n", instance->trip);
858}
859
860/* Device management */
861
16d75239
RH
862#if defined(CONFIG_THERMAL_HWMON)
863
e68b16ab
ZR
864/* hwmon sys I/F */
865#include <linux/hwmon.h>
31f5396a
JD
866
867/* thermal zone devices with the same type share one hwmon device */
868struct thermal_hwmon_device {
869 char type[THERMAL_NAME_LENGTH];
870 struct device *device;
871 int count;
872 struct list_head tz_list;
873 struct list_head node;
874};
875
876struct thermal_hwmon_attr {
877 struct device_attribute attr;
878 char name[16];
879};
880
881/* one temperature input for each thermal zone */
882struct thermal_hwmon_temp {
883 struct list_head hwmon_node;
884 struct thermal_zone_device *tz;
885 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
886 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
887};
888
e68b16ab
ZR
889static LIST_HEAD(thermal_hwmon_list);
890
891static ssize_t
892name_show(struct device *dev, struct device_attribute *attr, char *buf)
893{
0e968a3b 894 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
e68b16ab
ZR
895 return sprintf(buf, "%s\n", hwmon->type);
896}
897static DEVICE_ATTR(name, 0444, name_show, NULL);
898
899static ssize_t
900temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
901{
6503e5df
MG
902 long temperature;
903 int ret;
e68b16ab
ZR
904 struct thermal_hwmon_attr *hwmon_attr
905 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
906 struct thermal_hwmon_temp *temp
907 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 908 temp_input);
31f5396a 909 struct thermal_zone_device *tz = temp->tz;
e68b16ab 910
e6e238c3 911 ret = thermal_zone_get_temp(tz, &temperature);
6503e5df
MG
912
913 if (ret)
914 return ret;
915
916 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
917}
918
919static ssize_t
920temp_crit_show(struct device *dev, struct device_attribute *attr,
921 char *buf)
922{
923 struct thermal_hwmon_attr *hwmon_attr
924 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
925 struct thermal_hwmon_temp *temp
926 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 927 temp_crit);
31f5396a 928 struct thermal_zone_device *tz = temp->tz;
6503e5df
MG
929 long temperature;
930 int ret;
931
932 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
933 if (ret)
934 return ret;
e68b16ab 935
6503e5df 936 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
937}
938
939
0d97d7a4
JD
940static struct thermal_hwmon_device *
941thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
e68b16ab
ZR
942{
943 struct thermal_hwmon_device *hwmon;
e68b16ab
ZR
944
945 mutex_lock(&thermal_list_lock);
946 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
947 if (!strcmp(hwmon->type, tz->type)) {
e68b16ab 948 mutex_unlock(&thermal_list_lock);
0d97d7a4 949 return hwmon;
e68b16ab
ZR
950 }
951 mutex_unlock(&thermal_list_lock);
952
0d97d7a4
JD
953 return NULL;
954}
955
31f5396a
JD
956/* Find the temperature input matching a given thermal zone */
957static struct thermal_hwmon_temp *
958thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
959 const struct thermal_zone_device *tz)
960{
961 struct thermal_hwmon_temp *temp;
962
963 mutex_lock(&thermal_list_lock);
964 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
965 if (temp->tz == tz) {
966 mutex_unlock(&thermal_list_lock);
967 return temp;
968 }
969 mutex_unlock(&thermal_list_lock);
970
971 return NULL;
972}
973
0d97d7a4
JD
974static int
975thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
976{
977 struct thermal_hwmon_device *hwmon;
31f5396a 978 struct thermal_hwmon_temp *temp;
0d97d7a4
JD
979 int new_hwmon_device = 1;
980 int result;
981
982 hwmon = thermal_hwmon_lookup_by_type(tz);
983 if (hwmon) {
984 new_hwmon_device = 0;
985 goto register_sys_interface;
986 }
987
e68b16ab
ZR
988 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
989 if (!hwmon)
990 return -ENOMEM;
991
992 INIT_LIST_HEAD(&hwmon->tz_list);
993 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
994 hwmon->device = hwmon_device_register(NULL);
995 if (IS_ERR(hwmon->device)) {
996 result = PTR_ERR(hwmon->device);
997 goto free_mem;
998 }
0e968a3b 999 dev_set_drvdata(hwmon->device, hwmon);
e68b16ab
ZR
1000 result = device_create_file(hwmon->device, &dev_attr_name);
1001 if (result)
b299eb5c 1002 goto free_mem;
e68b16ab
ZR
1003
1004 register_sys_interface:
31f5396a
JD
1005 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
1006 if (!temp) {
1007 result = -ENOMEM;
1008 goto unregister_name;
1009 }
1010
1011 temp->tz = tz;
e68b16ab
ZR
1012 hwmon->count++;
1013
79a49168 1014 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
e68b16ab 1015 "temp%d_input", hwmon->count);
31f5396a
JD
1016 temp->temp_input.attr.attr.name = temp->temp_input.name;
1017 temp->temp_input.attr.attr.mode = 0444;
1018 temp->temp_input.attr.show = temp_input_show;
1019 sysfs_attr_init(&temp->temp_input.attr.attr);
1020 result = device_create_file(hwmon->device, &temp->temp_input.attr);
e68b16ab 1021 if (result)
31f5396a 1022 goto free_temp_mem;
e68b16ab
ZR
1023
1024 if (tz->ops->get_crit_temp) {
1025 unsigned long temperature;
1026 if (!tz->ops->get_crit_temp(tz, &temperature)) {
79a49168
GR
1027 snprintf(temp->temp_crit.name,
1028 sizeof(temp->temp_crit.name),
e68b16ab 1029 "temp%d_crit", hwmon->count);
31f5396a
JD
1030 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
1031 temp->temp_crit.attr.attr.mode = 0444;
1032 temp->temp_crit.attr.show = temp_crit_show;
1033 sysfs_attr_init(&temp->temp_crit.attr.attr);
e68b16ab 1034 result = device_create_file(hwmon->device,
31f5396a 1035 &temp->temp_crit.attr);
e68b16ab 1036 if (result)
b299eb5c 1037 goto unregister_input;
e68b16ab
ZR
1038 }
1039 }
1040
1041 mutex_lock(&thermal_list_lock);
1042 if (new_hwmon_device)
1043 list_add_tail(&hwmon->node, &thermal_hwmon_list);
31f5396a 1044 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
e68b16ab
ZR
1045 mutex_unlock(&thermal_list_lock);
1046
1047 return 0;
1048
b299eb5c 1049 unregister_input:
31f5396a
JD
1050 device_remove_file(hwmon->device, &temp->temp_input.attr);
1051 free_temp_mem:
1052 kfree(temp);
b299eb5c 1053 unregister_name:
e68b16ab
ZR
1054 if (new_hwmon_device) {
1055 device_remove_file(hwmon->device, &dev_attr_name);
1056 hwmon_device_unregister(hwmon->device);
1057 }
1058 free_mem:
1059 if (new_hwmon_device)
1060 kfree(hwmon);
1061
1062 return result;
1063}
1064
1065static void
1066thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1067{
31f5396a
JD
1068 struct thermal_hwmon_device *hwmon;
1069 struct thermal_hwmon_temp *temp;
1070
1071 hwmon = thermal_hwmon_lookup_by_type(tz);
1072 if (unlikely(!hwmon)) {
1073 /* Should never happen... */
1074 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
1075 return;
1076 }
1077
1078 temp = thermal_hwmon_lookup_temp(hwmon, tz);
1079 if (unlikely(!temp)) {
1080 /* Should never happen... */
1081 dev_dbg(&tz->device, "temperature input lookup failed!\n");
1082 return;
1083 }
e68b16ab 1084
31f5396a 1085 device_remove_file(hwmon->device, &temp->temp_input.attr);
b299eb5c 1086 if (tz->ops->get_crit_temp)
31f5396a 1087 device_remove_file(hwmon->device, &temp->temp_crit.attr);
e68b16ab
ZR
1088
1089 mutex_lock(&thermal_list_lock);
31f5396a
JD
1090 list_del(&temp->hwmon_node);
1091 kfree(temp);
e68b16ab
ZR
1092 if (!list_empty(&hwmon->tz_list)) {
1093 mutex_unlock(&thermal_list_lock);
1094 return;
1095 }
1096 list_del(&hwmon->node);
1097 mutex_unlock(&thermal_list_lock);
1098
1099 device_remove_file(hwmon->device, &dev_attr_name);
1100 hwmon_device_unregister(hwmon->device);
1101 kfree(hwmon);
1102}
1103#else
1104static int
1105thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
1106{
1107 return 0;
1108}
1109
1110static void
1111thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
1112{
1113}
1114#endif
1115
203d3d4a 1116/**
d2e4eb83
EV
1117 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1118 * @tz: pointer to struct thermal_zone_device
203d3d4a
ZR
1119 * @trip: indicates which trip point the cooling devices is
1120 * associated with in this thermal zone.
d2e4eb83
EV
1121 * @cdev: pointer to struct thermal_cooling_device
1122 * @upper: the Maximum cooling state for this trip point.
1123 * THERMAL_NO_LIMIT means no upper limit,
1124 * and the cooling device can be in max_state.
1125 * @lower: the Minimum cooling state can be used for this trip point.
1126 * THERMAL_NO_LIMIT means no lower limit,
1127 * and the cooling device can be in cooling state 0.
543a9561 1128 *
d2e4eb83
EV
1129 * This interface function bind a thermal cooling device to the certain trip
1130 * point of a thermal zone device.
543a9561 1131 * This function is usually called in the thermal zone device .bind callback.
d2e4eb83
EV
1132 *
1133 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
1134 */
1135int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1136 int trip,
9d99842f
ZR
1137 struct thermal_cooling_device *cdev,
1138 unsigned long upper, unsigned long lower)
203d3d4a 1139{
b81b6ba3
ZR
1140 struct thermal_instance *dev;
1141 struct thermal_instance *pos;
c7516709
TS
1142 struct thermal_zone_device *pos1;
1143 struct thermal_cooling_device *pos2;
74051ba5 1144 unsigned long max_state;
203d3d4a
ZR
1145 int result;
1146
543a9561 1147 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
1148 return -EINVAL;
1149
c7516709
TS
1150 list_for_each_entry(pos1, &thermal_tz_list, node) {
1151 if (pos1 == tz)
1152 break;
1153 }
1154 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1155 if (pos2 == cdev)
1156 break;
1157 }
1158
1159 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
1160 return -EINVAL;
1161
9d99842f
ZR
1162 cdev->ops->get_max_state(cdev, &max_state);
1163
1164 /* lower default 0, upper default max_state */
1165 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1166 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1167
1168 if (lower > upper || upper > max_state)
1169 return -EINVAL;
1170
203d3d4a 1171 dev =
b81b6ba3 1172 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
203d3d4a
ZR
1173 if (!dev)
1174 return -ENOMEM;
1175 dev->tz = tz;
1176 dev->cdev = cdev;
1177 dev->trip = trip;
9d99842f
ZR
1178 dev->upper = upper;
1179 dev->lower = lower;
ce119f83 1180 dev->target = THERMAL_NO_TARGET;
74051ba5 1181
203d3d4a
ZR
1182 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1183 if (result)
1184 goto free_mem;
1185
1186 sprintf(dev->name, "cdev%d", dev->id);
1187 result =
1188 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1189 if (result)
1190 goto release_idr;
1191
1192 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 1193 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
1194 dev->attr.attr.name = dev->attr_name;
1195 dev->attr.attr.mode = 0444;
1196 dev->attr.show = thermal_cooling_device_trip_point_show;
1197 result = device_create_file(&tz->device, &dev->attr);
1198 if (result)
1199 goto remove_symbol_link;
1200
1201 mutex_lock(&tz->lock);
f4a821ce 1202 mutex_lock(&cdev->lock);
cddf31b3 1203 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
203d3d4a
ZR
1204 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1205 result = -EEXIST;
1206 break;
1207 }
b5e4ae62 1208 if (!result) {
cddf31b3 1209 list_add_tail(&dev->tz_node, &tz->thermal_instances);
b5e4ae62
ZR
1210 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1211 }
f4a821ce 1212 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1213 mutex_unlock(&tz->lock);
1214
1215 if (!result)
1216 return 0;
1217
1218 device_remove_file(&tz->device, &dev->attr);
caca8b80 1219remove_symbol_link:
203d3d4a 1220 sysfs_remove_link(&tz->device.kobj, dev->name);
caca8b80 1221release_idr:
203d3d4a 1222 release_idr(&tz->idr, &tz->lock, dev->id);
caca8b80 1223free_mem:
203d3d4a
ZR
1224 kfree(dev);
1225 return result;
1226}
910cb1e3 1227EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
203d3d4a
ZR
1228
1229/**
9892e5dc
EV
1230 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1231 * thermal zone.
1232 * @tz: pointer to a struct thermal_zone_device.
203d3d4a
ZR
1233 * @trip: indicates which trip point the cooling devices is
1234 * associated with in this thermal zone.
9892e5dc 1235 * @cdev: pointer to a struct thermal_cooling_device.
543a9561 1236 *
9892e5dc
EV
1237 * This interface function unbind a thermal cooling device from the certain
1238 * trip point of a thermal zone device.
543a9561 1239 * This function is usually called in the thermal zone device .unbind callback.
9892e5dc
EV
1240 *
1241 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
1242 */
1243int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1244 int trip,
1245 struct thermal_cooling_device *cdev)
1246{
b81b6ba3 1247 struct thermal_instance *pos, *next;
203d3d4a
ZR
1248
1249 mutex_lock(&tz->lock);
f4a821ce 1250 mutex_lock(&cdev->lock);
cddf31b3 1251 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
543a9561 1252 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
cddf31b3 1253 list_del(&pos->tz_node);
b5e4ae62 1254 list_del(&pos->cdev_node);
f4a821ce 1255 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1256 mutex_unlock(&tz->lock);
1257 goto unbind;
1258 }
1259 }
f4a821ce 1260 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1261 mutex_unlock(&tz->lock);
1262
1263 return -ENODEV;
1264
caca8b80 1265unbind:
203d3d4a
ZR
1266 device_remove_file(&tz->device, &pos->attr);
1267 sysfs_remove_link(&tz->device.kobj, pos->name);
1268 release_idr(&tz->idr, &tz->lock, pos->id);
1269 kfree(pos);
1270 return 0;
1271}
910cb1e3 1272EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
203d3d4a
ZR
1273
1274static void thermal_release(struct device *dev)
1275{
1276 struct thermal_zone_device *tz;
1277 struct thermal_cooling_device *cdev;
1278
caca8b80
JP
1279 if (!strncmp(dev_name(dev), "thermal_zone",
1280 sizeof("thermal_zone") - 1)) {
203d3d4a
ZR
1281 tz = to_thermal_zone(dev);
1282 kfree(tz);
1283 } else {
1284 cdev = to_cooling_device(dev);
1285 kfree(cdev);
1286 }
1287}
1288
1289static struct class thermal_class = {
1290 .name = "thermal",
1291 .dev_release = thermal_release,
1292};
1293
1294/**
3a6eccb3 1295 * thermal_cooling_device_register() - register a new thermal cooling device
203d3d4a
ZR
1296 * @type: the thermal cooling device type.
1297 * @devdata: device private data.
1298 * @ops: standard thermal cooling devices callbacks.
3a6eccb3
EV
1299 *
1300 * This interface function adds a new thermal cooling device (fan/processor/...)
1301 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1302 * to all the thermal zone devices registered at the same time.
1303 *
1304 * Return: a pointer to the created struct thermal_cooling_device or an
1305 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
203d3d4a 1306 */
caca8b80
JP
1307struct thermal_cooling_device *
1308thermal_cooling_device_register(char *type, void *devdata,
1309 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
1310{
1311 struct thermal_cooling_device *cdev;
203d3d4a
ZR
1312 int result;
1313
204dd1d3 1314 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1315 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1316
1317 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 1318 !ops->set_cur_state)
3e6fda5c 1319 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1320
1321 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1322 if (!cdev)
3e6fda5c 1323 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
1324
1325 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1326 if (result) {
1327 kfree(cdev);
3e6fda5c 1328 return ERR_PTR(result);
203d3d4a
ZR
1329 }
1330
c7a8b9d9 1331 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
f4a821ce 1332 mutex_init(&cdev->lock);
b5e4ae62 1333 INIT_LIST_HEAD(&cdev->thermal_instances);
203d3d4a 1334 cdev->ops = ops;
ce119f83 1335 cdev->updated = true;
203d3d4a
ZR
1336 cdev->device.class = &thermal_class;
1337 cdev->devdata = devdata;
354655ea 1338 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
1339 result = device_register(&cdev->device);
1340 if (result) {
1341 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1342 kfree(cdev);
3e6fda5c 1343 return ERR_PTR(result);
203d3d4a
ZR
1344 }
1345
1346 /* sys I/F */
1347 if (type) {
543a9561 1348 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1349 if (result)
1350 goto unregister;
1351 }
1352
1353 result = device_create_file(&cdev->device, &dev_attr_max_state);
1354 if (result)
1355 goto unregister;
1356
1357 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1358 if (result)
1359 goto unregister;
1360
7e8ee1e9 1361 /* Add 'this' new cdev to the global cdev list */
203d3d4a
ZR
1362 mutex_lock(&thermal_list_lock);
1363 list_add(&cdev->node, &thermal_cdev_list);
203d3d4a
ZR
1364 mutex_unlock(&thermal_list_lock);
1365
7e8ee1e9
D
1366 /* Update binding information for 'this' new cdev */
1367 bind_cdev(cdev);
1368
1369 return cdev;
203d3d4a 1370
caca8b80 1371unregister:
203d3d4a
ZR
1372 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1373 device_unregister(&cdev->device);
3e6fda5c 1374 return ERR_PTR(result);
203d3d4a 1375}
910cb1e3 1376EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
203d3d4a
ZR
1377
1378/**
1379 * thermal_cooling_device_unregister - removes the registered thermal cooling device
203d3d4a
ZR
1380 * @cdev: the thermal cooling device to remove.
1381 *
1382 * thermal_cooling_device_unregister() must be called when the device is no
1383 * longer needed.
1384 */
7e8ee1e9 1385void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
203d3d4a 1386{
7e8ee1e9
D
1387 int i;
1388 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1389 struct thermal_zone_device *tz;
1390 struct thermal_cooling_device *pos = NULL;
1391
1392 if (!cdev)
1393 return;
1394
1395 mutex_lock(&thermal_list_lock);
1396 list_for_each_entry(pos, &thermal_cdev_list, node)
1397 if (pos == cdev)
1398 break;
1399 if (pos != cdev) {
1400 /* thermal cooling device not found */
1401 mutex_unlock(&thermal_list_lock);
1402 return;
1403 }
1404 list_del(&cdev->node);
7e8ee1e9
D
1405
1406 /* Unbind all thermal zones associated with 'this' cdev */
203d3d4a 1407 list_for_each_entry(tz, &thermal_tz_list, node) {
7e8ee1e9
D
1408 if (tz->ops->unbind) {
1409 tz->ops->unbind(tz, cdev);
1410 continue;
1411 }
1412
1413 if (!tz->tzp || !tz->tzp->tbp)
203d3d4a 1414 continue;
7e8ee1e9
D
1415
1416 tzp = tz->tzp;
1417 for (i = 0; i < tzp->num_tbps; i++) {
1418 if (tzp->tbp[i].cdev == cdev) {
1419 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1420 tzp->tbp[i].cdev = NULL;
1421 }
1422 }
203d3d4a 1423 }
7e8ee1e9 1424
203d3d4a 1425 mutex_unlock(&thermal_list_lock);
7e8ee1e9 1426
203d3d4a 1427 if (cdev->type[0])
543a9561 1428 device_remove_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1429 device_remove_file(&cdev->device, &dev_attr_max_state);
1430 device_remove_file(&cdev->device, &dev_attr_cur_state);
1431
1432 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1433 device_unregister(&cdev->device);
1434 return;
1435}
910cb1e3 1436EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
203d3d4a 1437
dc765482 1438void thermal_cdev_update(struct thermal_cooling_device *cdev)
ce119f83
ZR
1439{
1440 struct thermal_instance *instance;
1441 unsigned long target = 0;
1442
1443 /* cooling device is updated*/
1444 if (cdev->updated)
1445 return;
1446
f4a821ce 1447 mutex_lock(&cdev->lock);
ce119f83
ZR
1448 /* Make sure cdev enters the deepest cooling state */
1449 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
1450 if (instance->target == THERMAL_NO_TARGET)
1451 continue;
1452 if (instance->target > target)
1453 target = instance->target;
1454 }
f4a821ce 1455 mutex_unlock(&cdev->lock);
ce119f83
ZR
1456 cdev->ops->set_cur_state(cdev, target);
1457 cdev->updated = true;
1458}
dc765482 1459EXPORT_SYMBOL(thermal_cdev_update);
ce119f83 1460
f2b4caaf 1461/**
7b73c993 1462 * thermal_notify_framework - Sensor drivers use this API to notify framework
f2b4caaf
D
1463 * @tz: thermal zone device
1464 * @trip: indicates which trip point has been crossed
1465 *
1466 * This function handles the trip events from sensor drivers. It starts
1467 * throttling the cooling devices according to the policy configured.
1468 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1469 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1470 * The throttling policy is based on the configured platform data; if no
1471 * platform data is provided, this uses the step_wise throttling policy.
1472 */
7b73c993 1473void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
f2b4caaf
D
1474{
1475 handle_thermal_trip(tz, trip);
1476}
910cb1e3 1477EXPORT_SYMBOL_GPL(thermal_notify_framework);
f2b4caaf 1478
c56f5c03 1479/**
269c174f 1480 * create_trip_attrs() - create attributes for trip points
c56f5c03
D
1481 * @tz: the thermal zone device
1482 * @mask: Writeable trip point bitmap.
269c174f
EV
1483 *
1484 * helper function to instantiate sysfs entries for every trip
1485 * point and its properties of a struct thermal_zone_device.
1486 *
1487 * Return: 0 on success, the proper error value otherwise.
c56f5c03
D
1488 */
1489static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1490{
1491 int indx;
27365a6c 1492 int size = sizeof(struct thermal_attr) * tz->trips;
c56f5c03 1493
27365a6c 1494 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1495 if (!tz->trip_type_attrs)
1496 return -ENOMEM;
1497
27365a6c 1498 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1499 if (!tz->trip_temp_attrs) {
1500 kfree(tz->trip_type_attrs);
1501 return -ENOMEM;
1502 }
1503
27365a6c
D
1504 if (tz->ops->get_trip_hyst) {
1505 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1506 if (!tz->trip_hyst_attrs) {
1507 kfree(tz->trip_type_attrs);
1508 kfree(tz->trip_temp_attrs);
1509 return -ENOMEM;
1510 }
1511 }
c56f5c03 1512
27365a6c
D
1513
1514 for (indx = 0; indx < tz->trips; indx++) {
c56f5c03
D
1515 /* create trip type attribute */
1516 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1517 "trip_point_%d_type", indx);
1518
1519 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1520 tz->trip_type_attrs[indx].attr.attr.name =
1521 tz->trip_type_attrs[indx].name;
1522 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1523 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1524
1525 device_create_file(&tz->device,
1526 &tz->trip_type_attrs[indx].attr);
1527
1528 /* create trip temp attribute */
1529 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1530 "trip_point_%d_temp", indx);
1531
1532 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1533 tz->trip_temp_attrs[indx].attr.attr.name =
1534 tz->trip_temp_attrs[indx].name;
1535 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1536 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1537 if (mask & (1 << indx)) {
1538 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1539 tz->trip_temp_attrs[indx].attr.store =
1540 trip_point_temp_store;
1541 }
1542
1543 device_create_file(&tz->device,
1544 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1545
1546 /* create Optional trip hyst attribute */
1547 if (!tz->ops->get_trip_hyst)
1548 continue;
1549 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1550 "trip_point_%d_hyst", indx);
1551
1552 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1553 tz->trip_hyst_attrs[indx].attr.attr.name =
1554 tz->trip_hyst_attrs[indx].name;
1555 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1556 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1557 if (tz->ops->set_trip_hyst) {
1558 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1559 tz->trip_hyst_attrs[indx].attr.store =
1560 trip_point_hyst_store;
1561 }
1562
1563 device_create_file(&tz->device,
1564 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1565 }
1566 return 0;
1567}
1568
1569static void remove_trip_attrs(struct thermal_zone_device *tz)
1570{
1571 int indx;
1572
1573 for (indx = 0; indx < tz->trips; indx++) {
1574 device_remove_file(&tz->device,
1575 &tz->trip_type_attrs[indx].attr);
1576 device_remove_file(&tz->device,
1577 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1578 if (tz->ops->get_trip_hyst)
1579 device_remove_file(&tz->device,
1580 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1581 }
1582 kfree(tz->trip_type_attrs);
1583 kfree(tz->trip_temp_attrs);
27365a6c 1584 kfree(tz->trip_hyst_attrs);
c56f5c03
D
1585}
1586
203d3d4a 1587/**
a00e55f9 1588 * thermal_zone_device_register() - register a new thermal zone device
203d3d4a
ZR
1589 * @type: the thermal zone device type
1590 * @trips: the number of trip points the thermal zone support
c56f5c03 1591 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1592 * @devdata: private device data
1593 * @ops: standard thermal zone device callbacks
50125a9b 1594 * @tzp: thermal zone platform parameters
b1569e99
MG
1595 * @passive_delay: number of milliseconds to wait between polls when
1596 * performing passive cooling
1597 * @polling_delay: number of milliseconds to wait between polls when checking
1598 * whether trip points have been crossed (0 for interrupt
1599 * driven systems)
203d3d4a 1600 *
a00e55f9
EV
1601 * This interface function adds a new thermal zone device (sensor) to
1602 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1603 * thermal cooling devices registered at the same time.
203d3d4a 1604 * thermal_zone_device_unregister() must be called when the device is no
1b7ddb84 1605 * longer needed. The passive cooling depends on the .get_trend() return value.
a00e55f9
EV
1606 *
1607 * Return: a pointer to the created struct thermal_zone_device or an
1608 * in case of error, an ERR_PTR. Caller must check return value with
1609 * IS_ERR*() helpers.
203d3d4a 1610 */
4b1bf587 1611struct thermal_zone_device *thermal_zone_device_register(const char *type,
c56f5c03 1612 int trips, int mask, void *devdata,
5b275ce2 1613 const struct thermal_zone_device_ops *ops,
50125a9b 1614 const struct thermal_zone_params *tzp,
1b7ddb84 1615 int passive_delay, int polling_delay)
203d3d4a
ZR
1616{
1617 struct thermal_zone_device *tz;
03a971a2 1618 enum thermal_trip_type trip_type;
203d3d4a
ZR
1619 int result;
1620 int count;
03a971a2 1621 int passive = 0;
203d3d4a 1622
204dd1d3 1623 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1624 return ERR_PTR(-EINVAL);
203d3d4a 1625
c56f5c03 1626 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
3e6fda5c 1627 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1628
1629 if (!ops || !ops->get_temp)
3e6fda5c 1630 return ERR_PTR(-EINVAL);
203d3d4a 1631
83720d0b 1632 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
6b2aa51d
EV
1633 return ERR_PTR(-EINVAL);
1634
203d3d4a
ZR
1635 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1636 if (!tz)
3e6fda5c 1637 return ERR_PTR(-ENOMEM);
203d3d4a 1638
2d374139 1639 INIT_LIST_HEAD(&tz->thermal_instances);
203d3d4a
ZR
1640 idr_init(&tz->idr);
1641 mutex_init(&tz->lock);
1642 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1643 if (result) {
1644 kfree(tz);
3e6fda5c 1645 return ERR_PTR(result);
203d3d4a
ZR
1646 }
1647
c7a8b9d9 1648 strlcpy(tz->type, type ? : "", sizeof(tz->type));
203d3d4a 1649 tz->ops = ops;
50125a9b 1650 tz->tzp = tzp;
203d3d4a
ZR
1651 tz->device.class = &thermal_class;
1652 tz->devdata = devdata;
1653 tz->trips = trips;
b1569e99
MG
1654 tz->passive_delay = passive_delay;
1655 tz->polling_delay = polling_delay;
1656
354655ea 1657 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a
ZR
1658 result = device_register(&tz->device);
1659 if (result) {
1660 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1661 kfree(tz);
3e6fda5c 1662 return ERR_PTR(result);
203d3d4a
ZR
1663 }
1664
1665 /* sys I/F */
1666 if (type) {
1667 result = device_create_file(&tz->device, &dev_attr_type);
1668 if (result)
1669 goto unregister;
1670 }
1671
1672 result = device_create_file(&tz->device, &dev_attr_temp);
1673 if (result)
1674 goto unregister;
1675
1676 if (ops->get_mode) {
1677 result = device_create_file(&tz->device, &dev_attr_mode);
1678 if (result)
1679 goto unregister;
1680 }
1681
c56f5c03
D
1682 result = create_trip_attrs(tz, mask);
1683 if (result)
1684 goto unregister;
1685
203d3d4a 1686 for (count = 0; count < trips; count++) {
03a971a2
MG
1687 tz->ops->get_trip_type(tz, count, &trip_type);
1688 if (trip_type == THERMAL_TRIP_PASSIVE)
1689 passive = 1;
203d3d4a
ZR
1690 }
1691
5a2c090b
D
1692 if (!passive) {
1693 result = device_create_file(&tz->device, &dev_attr_passive);
1694 if (result)
1695 goto unregister;
1696 }
03a971a2 1697
e6e238c3
ADK
1698#ifdef CONFIG_THERMAL_EMULATION
1699 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1700 if (result)
1701 goto unregister;
1702#endif
5a2c090b
D
1703 /* Create policy attribute */
1704 result = device_create_file(&tz->device, &dev_attr_policy);
03a971a2
MG
1705 if (result)
1706 goto unregister;
1707
a4a15485
D
1708 /* Update 'this' zone's governor information */
1709 mutex_lock(&thermal_governor_lock);
1710
1711 if (tz->tzp)
1712 tz->governor = __find_governor(tz->tzp->governor_name);
1713 else
1714 tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
1715
1716 mutex_unlock(&thermal_governor_lock);
1717
e68b16ab
ZR
1718 result = thermal_add_hwmon_sysfs(tz);
1719 if (result)
1720 goto unregister;
1721
203d3d4a
ZR
1722 mutex_lock(&thermal_list_lock);
1723 list_add_tail(&tz->node, &thermal_tz_list);
203d3d4a
ZR
1724 mutex_unlock(&thermal_list_lock);
1725
7e8ee1e9
D
1726 /* Bind cooling devices for this zone */
1727 bind_tz(tz);
1728
b1569e99
MG
1729 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1730
1731 thermal_zone_device_update(tz);
1732
203d3d4a
ZR
1733 if (!result)
1734 return tz;
1735
caca8b80 1736unregister:
203d3d4a
ZR
1737 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1738 device_unregister(&tz->device);
3e6fda5c 1739 return ERR_PTR(result);
203d3d4a 1740}
910cb1e3 1741EXPORT_SYMBOL_GPL(thermal_zone_device_register);
203d3d4a
ZR
1742
1743/**
1744 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1745 * @tz: the thermal zone device to remove
1746 */
1747void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1748{
7e8ee1e9
D
1749 int i;
1750 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1751 struct thermal_cooling_device *cdev;
1752 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1753
1754 if (!tz)
1755 return;
1756
7e8ee1e9
D
1757 tzp = tz->tzp;
1758
203d3d4a
ZR
1759 mutex_lock(&thermal_list_lock);
1760 list_for_each_entry(pos, &thermal_tz_list, node)
1761 if (pos == tz)
1762 break;
1763 if (pos != tz) {
1764 /* thermal zone device not found */
1765 mutex_unlock(&thermal_list_lock);
1766 return;
1767 }
1768 list_del(&tz->node);
7e8ee1e9
D
1769
1770 /* Unbind all cdevs associated with 'this' thermal zone */
1771 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1772 if (tz->ops->unbind) {
1773 tz->ops->unbind(tz, cdev);
1774 continue;
1775 }
1776
1777 if (!tzp || !tzp->tbp)
1778 break;
1779
1780 for (i = 0; i < tzp->num_tbps; i++) {
1781 if (tzp->tbp[i].cdev == cdev) {
1782 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1783 tzp->tbp[i].cdev = NULL;
1784 }
1785 }
1786 }
1787
203d3d4a
ZR
1788 mutex_unlock(&thermal_list_lock);
1789
b1569e99
MG
1790 thermal_zone_device_set_polling(tz, 0);
1791
203d3d4a
ZR
1792 if (tz->type[0])
1793 device_remove_file(&tz->device, &dev_attr_type);
1794 device_remove_file(&tz->device, &dev_attr_temp);
1795 if (tz->ops->get_mode)
1796 device_remove_file(&tz->device, &dev_attr_mode);
5a2c090b 1797 device_remove_file(&tz->device, &dev_attr_policy);
c56f5c03 1798 remove_trip_attrs(tz);
a4a15485 1799 tz->governor = NULL;
203d3d4a 1800
e68b16ab 1801 thermal_remove_hwmon_sysfs(tz);
203d3d4a
ZR
1802 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1803 idr_destroy(&tz->idr);
1804 mutex_destroy(&tz->lock);
1805 device_unregister(&tz->device);
1806 return;
1807}
910cb1e3 1808EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
203d3d4a 1809
63c4d919
EV
1810/**
1811 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1812 * @name: thermal zone name to fetch the temperature
1813 *
1814 * When only one zone is found with the passed name, returns a reference to it.
1815 *
1816 * Return: On success returns a reference to an unique thermal zone with
1817 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1818 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1819 */
1820struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1821{
1822 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1823 unsigned int found = 0;
1824
1825 if (!name)
1826 goto exit;
1827
1828 mutex_lock(&thermal_list_lock);
1829 list_for_each_entry(pos, &thermal_tz_list, node)
1830 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1831 found++;
1832 ref = pos;
1833 }
1834 mutex_unlock(&thermal_list_lock);
1835
1836 /* nothing has been found, thus an error code for it */
1837 if (found == 0)
1838 ref = ERR_PTR(-ENODEV);
1839 else if (found > 1)
1840 /* Success only when an unique zone is found */
1841 ref = ERR_PTR(-EEXIST);
1842
1843exit:
1844 return ref;
1845}
1846EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1847
af06216a
RW
1848#ifdef CONFIG_NET
1849static struct genl_family thermal_event_genl_family = {
1850 .id = GENL_ID_GENERATE,
1851 .name = THERMAL_GENL_FAMILY_NAME,
1852 .version = THERMAL_GENL_VERSION,
1853 .maxattr = THERMAL_GENL_ATTR_MAX,
1854};
1855
1856static struct genl_multicast_group thermal_event_mcgrp = {
1857 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1858};
1859
8ab3e6a0
EV
1860int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1861 enum events event)
4cb18728
D
1862{
1863 struct sk_buff *skb;
1864 struct nlattr *attr;
1865 struct thermal_genl_event *thermal_event;
1866 void *msg_header;
1867 int size;
1868 int result;
b11de07c 1869 static unsigned int thermal_event_seqnum;
4cb18728 1870
8ab3e6a0
EV
1871 if (!tz)
1872 return -EINVAL;
1873
4cb18728 1874 /* allocate memory */
886ee546
JP
1875 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1876 nla_total_size(0);
4cb18728
D
1877
1878 skb = genlmsg_new(size, GFP_ATOMIC);
1879 if (!skb)
1880 return -ENOMEM;
1881
1882 /* add the genetlink message header */
1883 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1884 &thermal_event_genl_family, 0,
1885 THERMAL_GENL_CMD_EVENT);
1886 if (!msg_header) {
1887 nlmsg_free(skb);
1888 return -ENOMEM;
1889 }
1890
1891 /* fill the data */
886ee546
JP
1892 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1893 sizeof(struct thermal_genl_event));
4cb18728
D
1894
1895 if (!attr) {
1896 nlmsg_free(skb);
1897 return -EINVAL;
1898 }
1899
1900 thermal_event = nla_data(attr);
1901 if (!thermal_event) {
1902 nlmsg_free(skb);
1903 return -EINVAL;
1904 }
1905
1906 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1907
8ab3e6a0 1908 thermal_event->orig = tz->id;
4cb18728
D
1909 thermal_event->event = event;
1910
1911 /* send multicast genetlink message */
1912 result = genlmsg_end(skb, msg_header);
1913 if (result < 0) {
1914 nlmsg_free(skb);
1915 return result;
1916 }
1917
1918 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1919 if (result)
923e0b1e 1920 dev_err(&tz->device, "Failed to send netlink event:%d", result);
4cb18728
D
1921
1922 return result;
1923}
910cb1e3 1924EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
4cb18728
D
1925
1926static int genetlink_init(void)
1927{
1928 int result;
1929
1930 result = genl_register_family(&thermal_event_genl_family);
1931 if (result)
1932 return result;
1933
1934 result = genl_register_mc_group(&thermal_event_genl_family,
1935 &thermal_event_mcgrp);
1936 if (result)
1937 genl_unregister_family(&thermal_event_genl_family);
1938 return result;
1939}
1940
af06216a
RW
1941static void genetlink_exit(void)
1942{
1943 genl_unregister_family(&thermal_event_genl_family);
1944}
1945#else /* !CONFIG_NET */
1946static inline int genetlink_init(void) { return 0; }
1947static inline void genetlink_exit(void) {}
1948#endif /* !CONFIG_NET */
1949
80a26a5c
ZR
1950static int __init thermal_register_governors(void)
1951{
1952 int result;
1953
1954 result = thermal_gov_step_wise_register();
1955 if (result)
1956 return result;
1957
1958 result = thermal_gov_fair_share_register();
1959 if (result)
1960 return result;
1961
1962 return thermal_gov_user_space_register();
1963}
1964
1965static void thermal_unregister_governors(void)
1966{
1967 thermal_gov_step_wise_unregister();
1968 thermal_gov_fair_share_unregister();
1969 thermal_gov_user_space_unregister();
1970}
1971
203d3d4a
ZR
1972static int __init thermal_init(void)
1973{
80a26a5c
ZR
1974 int result;
1975
1976 result = thermal_register_governors();
1977 if (result)
1978 goto error;
203d3d4a
ZR
1979
1980 result = class_register(&thermal_class);
80a26a5c
ZR
1981 if (result)
1982 goto unregister_governors;
1983
4cb18728 1984 result = genetlink_init();
80a26a5c
ZR
1985 if (result)
1986 goto unregister_class;
1987
1988 return 0;
1989
1990unregister_governors:
1991 thermal_unregister_governors();
1992unregister_class:
1993 class_unregister(&thermal_class);
1994error:
1995 idr_destroy(&thermal_tz_idr);
1996 idr_destroy(&thermal_cdev_idr);
1997 mutex_destroy(&thermal_idr_lock);
1998 mutex_destroy(&thermal_list_lock);
1999 mutex_destroy(&thermal_governor_lock);
203d3d4a
ZR
2000 return result;
2001}
2002
2003static void __exit thermal_exit(void)
2004{
80a26a5c 2005 genetlink_exit();
203d3d4a 2006 class_unregister(&thermal_class);
80a26a5c 2007 thermal_unregister_governors();
203d3d4a
ZR
2008 idr_destroy(&thermal_tz_idr);
2009 idr_destroy(&thermal_cdev_idr);
2010 mutex_destroy(&thermal_idr_lock);
2011 mutex_destroy(&thermal_list_lock);
80a26a5c 2012 mutex_destroy(&thermal_governor_lock);
203d3d4a
ZR
2013}
2014
4cb18728 2015fs_initcall(thermal_init);
203d3d4a 2016module_exit(thermal_exit);
This page took 0.469216 seconds and 5 git commands to generate.