Lines Matching full:tz
67 * @tz: a valid pointer to a struct thermal_zone_device
73 static void bind_previous_governor(struct thermal_zone_device *tz,
76 if (tz->governor && tz->governor->bind_to_tz) {
77 if (tz->governor->bind_to_tz(tz)) {
78 dev_err(&tz->device,
80 failed_gov_name, tz->governor->name, tz->type);
81 tz->governor = NULL;
88 * @tz: a valid pointer to a struct thermal_zone_device
91 * Change the governor of thermal zone @tz.
95 static int thermal_set_governor(struct thermal_zone_device *tz,
100 if (tz->governor && tz->governor->unbind_from_tz)
101 tz->governor->unbind_from_tz(tz);
104 ret = new_gov->bind_to_tz(tz);
106 bind_previous_governor(tz, new_gov->name);
112 tz->governor = new_gov;
146 * only thermal zones with specified tz->tzp->governor_name
147 * may run with tz->govenor unset
191 int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
198 guard(thermal_zone)(tz);
202 ret = thermal_set_governor(tz, gov);
204 thermal_notify_tz_gov_change(tz, policy);
262 static int __thermal_zone_device_set_mode(struct thermal_zone_device *tz,
265 if (tz->ops.change_mode) {
268 ret = tz->ops.change_mode(tz, mode);
273 tz->mode = mode;
278 static void thermal_zone_broken_disable(struct thermal_zone_device *tz)
282 dev_err(&tz->device, "Unable to get temperature, disabling!\n");
287 __thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
288 thermal_notify_tz_disable(tz);
290 for_each_trip_desc(tz, td) {
293 dev_crit(&tz->device,
310 static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
316 mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, delay);
319 static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
322 thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY);
331 if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY)
332 dev_info(&tz->device, "Temperature check failed (%d)\n", error);
334 thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies);
336 tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL);
337 if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) {
338 thermal_zone_broken_disable(tz);
343 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
347 static void monitor_thermal_zone(struct thermal_zone_device *tz)
349 if (tz->passive > 0 && tz->passive_delay_jiffies)
350 thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
351 else if (tz->polling_delay_jiffies)
352 thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
355 static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
357 if (tz->governor)
358 return tz->governor;
363 void thermal_governor_update_tz(struct thermal_zone_device *tz,
366 if (!tz->governor || !tz->governor->update_tz)
369 tz->governor->update_tz(tz, reason);
372 static void thermal_zone_device_halt(struct thermal_zone_device *tz,
382 dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
387 void thermal_zone_device_critical(struct thermal_zone_device *tz)
389 thermal_zone_device_halt(tz, HWPROT_ACT_DEFAULT);
393 void thermal_zone_device_critical_shutdown(struct thermal_zone_device *tz)
395 thermal_zone_device_halt(tz, HWPROT_ACT_SHUTDOWN);
398 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
400 thermal_zone_device_halt(tz, HWPROT_ACT_REBOOT);
403 static void handle_critical_trips(struct thermal_zone_device *tz,
406 trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
409 tz->ops.critical(tz);
410 else if (tz->ops.hot)
411 tz->ops.hot(tz);
435 static void move_to_trips_high(struct thermal_zone_device *tz,
439 move_trip_to_sorted_list(td, &tz->trips_high);
442 static void move_to_trips_reached(struct thermal_zone_device *tz,
446 move_trip_to_sorted_list(td, &tz->trips_reached);
449 static void move_to_trips_invalid(struct thermal_zone_device *tz,
453 list_move(&td->list_node, &tz->trips_invalid);
457 struct thermal_zone_device *tz,
465 governor->trip_crossed(tz, trip, upward);
468 static void thermal_trip_crossed(struct thermal_zone_device *tz,
477 tz->passive++;
480 handle_critical_trips(tz, trip);
482 thermal_notify_tz_trip_up(tz, trip);
483 thermal_debug_tz_trip_up(tz, trip);
486 tz->passive--;
487 WARN_ON(tz->passive < 0);
489 thermal_notify_tz_trip_down(tz, trip);
490 thermal_debug_tz_trip_down(tz, trip);
492 thermal_governor_trip_crossed(governor, tz, trip, upward);
495 void thermal_zone_set_trip_hyst(struct thermal_zone_device *tz,
501 thermal_notify_tz_trip_change(tz, trip);
508 if (tz->temperature >= td->threshold)
509 move_to_trips_reached(tz, td);
512 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
522 thermal_notify_tz_trip_change(tz, trip);
532 move_to_trips_high(tz, td);
542 if (tz->temperature >= td->threshold)
543 thermal_trip_crossed(tz, td, thermal_get_tz_governor(tz), false);
545 move_to_trips_invalid(tz, td);
554 if (tz->temperature >= td->threshold)
555 move_to_trips_reached(tz, td);
557 move_to_trips_high(tz, td);
561 static void thermal_zone_handle_trips(struct thermal_zone_device *tz,
569 list_for_each_entry_safe_reverse(td, next, &tz->trips_reached, list_node) {
570 if (td->threshold <= tz->temperature)
573 thermal_trip_crossed(tz, td, governor, false);
581 list_for_each_entry_safe(td, next, &tz->trips_high, list_node) {
582 if (td->threshold > tz->temperature)
585 thermal_trip_crossed(tz, td, governor, true);
586 move_to_trips_reached(tz, td);
590 move_to_trips_high(tz, td);
592 if (!list_empty(&tz->trips_reached)) {
593 td = list_last_entry(&tz->trips_reached,
602 if (!list_empty(&tz->trips_high)) {
603 td = list_first_entry(&tz->trips_high,
609 void __thermal_zone_device_update(struct thermal_zone_device *tz,
612 struct thermal_governor *governor = thermal_get_tz_governor(tz);
616 if (tz->state != TZ_STATE_READY || tz->mode != THERMAL_DEVICE_ENABLED)
619 ret = __thermal_zone_get_temp(tz, &temp);
621 thermal_zone_recheck(tz, ret);
633 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
635 tz->last_temperature = tz->temperature;
636 tz->temperature = temp;
638 trace_thermal_temperature(tz);
640 thermal_genl_sampling_temp(tz->id, temp);
642 tz->notify_event = event;
644 thermal_zone_handle_trips(tz, governor, &low, &high);
646 thermal_thresholds_handle(tz, &low, &high);
648 thermal_zone_set_trips(tz, low, high);
651 governor->manage(tz);
653 thermal_debug_update_trip_stats(tz);
656 monitor_thermal_zone(tz);
659 static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
664 guard(thermal_zone)(tz);
667 if (mode == tz->mode)
670 ret = __thermal_zone_device_set_mode(tz, mode);
674 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
677 thermal_notify_tz_enable(tz);
679 thermal_notify_tz_disable(tz);
684 int thermal_zone_device_enable(struct thermal_zone_device *tz)
686 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
690 int thermal_zone_device_disable(struct thermal_zone_device *tz)
692 return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
696 static bool thermal_zone_is_present(struct thermal_zone_device *tz)
698 return !list_empty(&tz->node);
701 void thermal_zone_device_update(struct thermal_zone_device *tz,
704 guard(thermal_zone)(tz);
706 if (thermal_zone_is_present(tz))
707 __thermal_zone_device_update(tz, event);
750 struct thermal_zone_device *tz;
754 list_for_each_entry(tz, &thermal_tz_list, node) {
757 ret = cb(tz, data);
767 struct thermal_zone_device *tz;
771 list_for_each_entry(tz, &thermal_tz_list, node) {
772 if (tz->id == id) {
773 get_device(&tz->device);
774 return tz;
813 * @tz: pointer to struct thermal_zone_device
824 static int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
859 result = ida_alloc(&tz->ida, GFP_KERNEL);
866 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
876 result = device_create_file(&tz->device, &dev->attr);
887 result = device_create_file(&tz->device, &dev->weight_attr);
895 thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
900 device_remove_file(&tz->device, &dev->weight_attr);
902 device_remove_file(&tz->device, &dev->attr);
904 sysfs_remove_link(&tz->device.kobj, dev->name);
906 ida_free(&tz->ida, dev->id);
923 * @tz: pointer to a struct thermal_zone_device.
931 static void thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
947 thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
949 device_remove_file(&tz->device, &pos->weight_attr);
950 device_remove_file(&tz->device, &pos->attr);
951 sysfs_remove_link(&tz->device.kobj, pos->name);
952 ida_free(&tz->ida, pos->id);
958 struct thermal_zone_device *tz;
963 tz = to_thermal_zone(dev);
964 thermal_zone_destroy_device_groups(tz);
965 mutex_destroy(&tz->lock);
966 complete(&tz->removal);
980 void print_bind_err_msg(struct thermal_zone_device *tz,
984 dev_err(&tz->device, "binding cdev %s to trip %d failed: %d\n",
985 cdev->type, thermal_zone_trip_id(tz, &td->trip), ret);
988 static bool __thermal_zone_cdev_bind(struct thermal_zone_device *tz,
994 if (!tz->ops.should_bind)
997 for_each_trip_desc(tz, td) {
1005 if (!tz->ops.should_bind(tz, &td->trip, cdev, &c))
1008 ret = thermal_bind_cdev_to_trip(tz, td, cdev, &c);
1010 print_bind_err_msg(tz, td, cdev, ret);
1020 static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
1023 guard(thermal_zone)(tz);
1025 if (__thermal_zone_cdev_bind(tz, cdev))
1026 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1031 struct thermal_zone_device *tz;
1037 list_for_each_entry(tz, &thermal_tz_list, node)
1038 thermal_zone_cdev_bind(tz, cdev);
1317 static void __thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
1322 for_each_trip_desc(tz, td)
1323 thermal_unbind_cdev_from_trip(tz, td, cdev);
1326 static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
1329 guard(thermal_zone)(tz);
1331 __thermal_zone_cdev_unbind(tz, cdev);
1336 struct thermal_zone_device *tz;
1345 list_for_each_entry(tz, &thermal_tz_list, node)
1346 thermal_zone_cdev_unbind(tz, cdev);
1367 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
1372 if (tz->ops.get_crit_temp)
1373 return tz->ops.get_crit_temp(tz, temp);
1375 guard(thermal_zone)(tz);
1377 for_each_trip_desc(tz, td) {
1393 struct thermal_zone_device *tz = container_of(work, struct
1396 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1399 static void thermal_zone_device_init(struct thermal_zone_device *tz)
1403 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
1405 tz->temperature = THERMAL_TEMP_INIT;
1406 tz->passive = 0;
1407 tz->prev_low_trip = -INT_MAX;
1408 tz->prev_high_trip = INT_MAX;
1409 for_each_trip_desc(tz, td) {
1419 list_for_each_entry_safe(td, next, &tz->trips_invalid, list_node) {
1421 move_to_trips_high(tz, td);
1424 list_for_each_entry_safe(td, next, &tz->trips_reached, list_node) {
1426 move_to_trips_invalid(tz, td);
1428 move_to_trips_high(tz, td);
1432 static int thermal_zone_init_governor(struct thermal_zone_device *tz)
1438 if (tz->tzp)
1439 governor = __find_governor(tz->tzp->governor_name);
1443 return thermal_set_governor(tz, governor);
1446 static void thermal_zone_init_complete(struct thermal_zone_device *tz)
1452 list_add_tail(&tz->node, &thermal_tz_list);
1454 guard(thermal_zone)(tz);
1458 __thermal_zone_cdev_bind(tz, cdev);
1460 tz->state &= ~TZ_STATE_FLAG_INIT;
1467 tz->state |= TZ_STATE_FLAG_SUSPENDED;
1469 __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1506 struct thermal_zone_device *tz;
1541 tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
1542 if (!tz)
1546 tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
1547 if (!tz->tzp) {
1553 INIT_LIST_HEAD(&tz->node);
1554 INIT_LIST_HEAD(&tz->trips_high);
1555 INIT_LIST_HEAD(&tz->trips_reached);
1556 INIT_LIST_HEAD(&tz->trips_invalid);
1557 ida_init(&tz->ida);
1558 mutex_init(&tz->lock);
1559 init_completion(&tz->removal);
1560 init_completion(&tz->resume);
1567 tz->id = id;
1568 strscpy(tz->type, type, sizeof(tz->type));
1570 tz->ops = *ops;
1571 if (!tz->ops.critical)
1572 tz->ops.critical = thermal_zone_device_critical;
1574 tz->device.class = thermal_class;
1575 tz->devdata = devdata;
1576 tz->num_trips = num_trips;
1577 for_each_trip_desc(tz, td) {
1586 move_to_trips_invalid(tz, td);
1589 tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay);
1590 tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay);
1591 tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
1593 tz->state = TZ_STATE_FLAG_INIT;
1595 result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
1599 thermal_zone_device_init(tz);
1601 result = thermal_zone_init_governor(tz);
1607 result = thermal_zone_create_device_groups(tz);
1611 result = device_register(&tz->device);
1615 if (!tz->tzp || !tz->tzp->no_hwmon) {
1616 result = thermal_add_hwmon_sysfs(tz);
1621 result = thermal_thresholds_init(tz);
1625 thermal_zone_init_complete(tz);
1627 thermal_notify_tz_create(tz);
1629 thermal_debug_tz_add(tz);
1631 return tz;
1634 thermal_remove_hwmon_sysfs(tz);
1636 device_del(&tz->device);
1638 put_device(&tz->device);
1642 kfree(tz->tzp);
1644 kfree(tz);
1684 static bool thermal_zone_exit(struct thermal_zone_device *tz)
1690 if (list_empty(&tz->node))
1693 guard(thermal_zone)(tz);
1695 tz->state |= TZ_STATE_FLAG_EXIT;
1696 list_del_init(&tz->node);
1700 __thermal_zone_cdev_unbind(tz, cdev);
1707 * @tz: the thermal zone device to remove
1709 void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1711 if (!tz)
1714 thermal_debug_tz_remove(tz);
1716 if (!thermal_zone_exit(tz))
1719 cancel_delayed_work_sync(&tz->poll_queue);
1721 thermal_set_governor(tz, NULL);
1723 thermal_thresholds_exit(tz);
1724 thermal_remove_hwmon_sysfs(tz);
1725 ida_free(&thermal_tz_ida, tz->id);
1726 ida_destroy(&tz->ida);
1728 device_del(&tz->device);
1729 put_device(&tz->device);
1731 thermal_notify_tz_delete(tz);
1733 wait_for_completion(&tz->removal);
1734 kfree(tz->tzp);
1735 kfree(tz);
1778 struct thermal_zone_device *tz;
1780 tz = container_of(work, struct thermal_zone_device, poll_queue.work);
1782 guard(thermal_zone)(tz);
1784 tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING);
1786 thermal_debug_tz_resume(tz);
1787 thermal_zone_device_init(tz);
1788 thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
1789 __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
1791 complete(&tz->resume);
1794 static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
1796 guard(thermal_zone)(tz);
1798 if (tz->state & TZ_STATE_FLAG_RESUMING) {
1804 scoped_guard(thermal_zone_reverse, tz) {
1805 wait_for_completion(&tz->resume);
1809 tz->state |= TZ_STATE_FLAG_SUSPENDED;
1814 struct thermal_zone_device *tz;
1820 list_for_each_entry(tz, &thermal_tz_list, node)
1821 thermal_zone_pm_prepare(tz);
1824 static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
1826 guard(thermal_zone)(tz);
1828 cancel_delayed_work(&tz->poll_queue);
1830 reinit_completion(&tz->resume);
1831 tz->state |= TZ_STATE_FLAG_RESUMING;
1837 INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume);
1839 mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);
1844 struct thermal_zone_device *tz;
1850 list_for_each_entry(tz, &thermal_tz_list, node)
1851 thermal_zone_pm_complete(tz);