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