4747
4848#define ACPI_THERMAL_TRIP_PASSIVE (-1)
4949
50+ #define ACPI_THERMAL_MAX_NR_TRIPS (ACPI_THERMAL_MAX_ACTIVE + 3)
51+
5052/*
5153 * This exception is thrown out in two cases:
5254 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
@@ -112,7 +114,6 @@ struct acpi_thermal {
112114 unsigned long polling_frequency ;
113115 volatile u8 zombie ;
114116 struct acpi_thermal_trips trips ;
115- struct thermal_trip * trip_table ;
116117 struct thermal_zone_device * thermal_zone ;
117118 int kelvin_offset ; /* in millidegrees */
118119 struct work_struct thermal_check_work ;
@@ -167,11 +168,17 @@ static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
167168
168169static int acpi_thermal_temp (struct acpi_thermal * tz , int temp_deci_k )
169170{
171+ int temp ;
172+
170173 if (temp_deci_k == THERMAL_TEMP_INVALID )
171174 return THERMAL_TEMP_INVALID ;
172175
173- return deci_kelvin_to_millicelsius_with_offset (temp_deci_k ,
176+ temp = deci_kelvin_to_millicelsius_with_offset (temp_deci_k ,
174177 tz -> kelvin_offset );
178+ if (temp <= 0 )
179+ return THERMAL_TEMP_INVALID ;
180+
181+ return temp ;
175182}
176183
177184static bool acpi_thermal_trip_valid (struct acpi_thermal_trip * acpi_trip )
@@ -451,26 +458,19 @@ static bool acpi_thermal_init_trip(struct acpi_thermal *tz, int index)
451458 return false;
452459}
453460
454- static int acpi_thermal_get_trip_points (struct acpi_thermal * tz )
461+ static void acpi_thermal_get_trip_points (struct acpi_thermal * tz )
455462{
456- unsigned int count = 0 ;
457463 int i ;
458464
459- if (acpi_thermal_init_trip (tz , ACPI_THERMAL_TRIP_PASSIVE ))
460- count ++ ;
465+ acpi_thermal_init_trip (tz , ACPI_THERMAL_TRIP_PASSIVE );
461466
462467 for (i = 0 ; i < ACPI_THERMAL_MAX_ACTIVE ; i ++ ) {
463- if (acpi_thermal_init_trip (tz , i ))
464- count ++ ;
465- else
468+ if (!acpi_thermal_init_trip (tz , i ))
466469 break ;
467-
468470 }
469471
470472 while (++ i < ACPI_THERMAL_MAX_ACTIVE )
471473 tz -> trips .active [i ].trip .temp_dk = THERMAL_TEMP_INVALID ;
472-
473- return count ;
474474}
475475
476476/* sys I/F for generic thermal sysfs support */
@@ -558,77 +558,31 @@ static void acpi_thermal_zone_device_critical(struct thermal_zone_device *therma
558558 thermal_zone_device_critical (thermal );
559559}
560560
561- struct acpi_thermal_bind_data {
562- struct thermal_zone_device * thermal ;
563- struct thermal_cooling_device * cdev ;
564- bool bind ;
565- };
566-
567- static int bind_unbind_cdev_cb (struct thermal_trip * trip , void * arg )
561+ static bool acpi_thermal_should_bind_cdev (struct thermal_zone_device * thermal ,
562+ const struct thermal_trip * trip ,
563+ struct thermal_cooling_device * cdev ,
564+ struct cooling_spec * c )
568565{
569566 struct acpi_thermal_trip * acpi_trip = trip -> priv ;
570- struct acpi_thermal_bind_data * bd = arg ;
571- struct thermal_zone_device * thermal = bd -> thermal ;
572- struct thermal_cooling_device * cdev = bd -> cdev ;
573567 struct acpi_device * cdev_adev = cdev -> devdata ;
574568 int i ;
575569
576570 /* Skip critical and hot trips. */
577571 if (!acpi_trip )
578- return 0 ;
572+ return false ;
579573
580574 for (i = 0 ; i < acpi_trip -> devices .count ; i ++ ) {
581575 acpi_handle handle = acpi_trip -> devices .handles [i ];
582- struct acpi_device * adev = acpi_fetch_acpi_dev (handle );
583-
584- if (adev != cdev_adev )
585- continue ;
586-
587- if (bd -> bind ) {
588- int ret ;
589-
590- ret = thermal_bind_cdev_to_trip (thermal , trip , cdev ,
591- THERMAL_NO_LIMIT ,
592- THERMAL_NO_LIMIT ,
593- THERMAL_WEIGHT_DEFAULT );
594- if (ret )
595- return ret ;
596- } else {
597- thermal_unbind_cdev_from_trip (thermal , trip , cdev );
598- }
599- }
600-
601- return 0 ;
602- }
603-
604- static int acpi_thermal_bind_unbind_cdev (struct thermal_zone_device * thermal ,
605- struct thermal_cooling_device * cdev ,
606- bool bind )
607- {
608- struct acpi_thermal_bind_data bd = {
609- .thermal = thermal , .cdev = cdev , .bind = bind
610- };
611-
612- return for_each_thermal_trip (thermal , bind_unbind_cdev_cb , & bd );
613- }
614576
615- static int
616- acpi_thermal_bind_cooling_device (struct thermal_zone_device * thermal ,
617- struct thermal_cooling_device * cdev )
618- {
619- return acpi_thermal_bind_unbind_cdev (thermal , cdev , true);
620- }
577+ if (acpi_fetch_acpi_dev (handle ) == cdev_adev )
578+ return true;
579+ }
621580
622- static int
623- acpi_thermal_unbind_cooling_device (struct thermal_zone_device * thermal ,
624- struct thermal_cooling_device * cdev )
625- {
626- return acpi_thermal_bind_unbind_cdev (thermal , cdev , false);
581+ return false;
627582}
628583
629- static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
630- .bind = acpi_thermal_bind_cooling_device ,
631- .unbind = acpi_thermal_unbind_cooling_device ,
584+ static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
585+ .should_bind = acpi_thermal_should_bind_cdev ,
632586 .get_temp = thermal_get_temp ,
633587 .get_trend = thermal_get_trend ,
634588 .hot = acpi_thermal_zone_device_hot ,
@@ -662,14 +616,15 @@ static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
662616}
663617
664618static int acpi_thermal_register_thermal_zone (struct acpi_thermal * tz ,
619+ const struct thermal_trip * trip_table ,
665620 unsigned int trip_count ,
666621 int passive_delay )
667622{
668623 int result ;
669624
670625 if (trip_count )
671626 tz -> thermal_zone = thermal_zone_device_register_with_trips (
672- "acpitz" , tz -> trip_table , trip_count , 0 , tz ,
627+ "acpitz" , trip_table , trip_count , tz ,
673628 & acpi_thermal_zone_ops , NULL , passive_delay ,
674629 tz -> polling_frequency * 100 );
675630 else
@@ -824,10 +779,10 @@ static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)
824779
825780static int acpi_thermal_add (struct acpi_device * device )
826781{
782+ struct thermal_trip trip_table [ACPI_THERMAL_MAX_NR_TRIPS ] = { 0 };
827783 struct acpi_thermal_trip * acpi_trip ;
828784 struct thermal_trip * trip ;
829785 struct acpi_thermal * tz ;
830- unsigned int trip_count ;
831786 int crit_temp , hot_temp ;
832787 int passive_delay = 0 ;
833788 int result ;
@@ -849,21 +804,10 @@ static int acpi_thermal_add(struct acpi_device *device)
849804 acpi_thermal_aml_dependency_fix (tz );
850805
851806 /* Get trip points [_CRT, _PSV, etc.] (required). */
852- trip_count = acpi_thermal_get_trip_points (tz );
807+ acpi_thermal_get_trip_points (tz );
853808
854809 crit_temp = acpi_thermal_get_critical_trip (tz );
855- if (crit_temp != THERMAL_TEMP_INVALID )
856- trip_count ++ ;
857-
858810 hot_temp = acpi_thermal_get_hot_trip (tz );
859- if (hot_temp != THERMAL_TEMP_INVALID )
860- trip_count ++ ;
861-
862- if (!trip_count ) {
863- pr_warn (FW_BUG "No valid trip points!\n" );
864- result = - ENODEV ;
865- goto free_memory ;
866- }
867811
868812 /* Get temperature [_TMP] (required). */
869813 result = acpi_thermal_get_temperature (tz );
@@ -882,13 +826,7 @@ static int acpi_thermal_add(struct acpi_device *device)
882826
883827 acpi_thermal_guess_offset (tz , crit_temp );
884828
885- trip = kcalloc (trip_count , sizeof (* trip ), GFP_KERNEL );
886- if (!trip ) {
887- result = - ENOMEM ;
888- goto free_memory ;
889- }
890-
891- tz -> trip_table = trip ;
829+ trip = trip_table ;
892830
893831 if (crit_temp != THERMAL_TEMP_INVALID ) {
894832 trip -> type = THERMAL_TRIP_CRITICAL ;
@@ -924,12 +862,17 @@ static int acpi_thermal_add(struct acpi_device *device)
924862 trip ++ ;
925863 }
926864
927- if (trip == tz -> trip_table )
865+ if (trip == trip_table ) {
928866 pr_warn (FW_BUG "No valid trip points!\n" );
867+ result = - ENODEV ;
868+ goto free_memory ;
869+ }
929870
930- result = acpi_thermal_register_thermal_zone (tz , trip_count , passive_delay );
871+ result = acpi_thermal_register_thermal_zone (tz , trip_table ,
872+ trip - trip_table ,
873+ passive_delay );
931874 if (result )
932- goto free_trips ;
875+ goto free_memory ;
933876
934877 refcount_set (& tz -> thermal_check_count , 3 );
935878 mutex_init (& tz -> thermal_check_lock );
@@ -948,8 +891,6 @@ static int acpi_thermal_add(struct acpi_device *device)
948891flush_wq :
949892 flush_workqueue (acpi_thermal_pm_queue );
950893 acpi_thermal_unregister_thermal_zone (tz );
951- free_trips :
952- kfree (tz -> trip_table );
953894free_memory :
954895 acpi_thermal_free_thermal_zone (tz );
955896
@@ -970,8 +911,7 @@ static void acpi_thermal_remove(struct acpi_device *device)
970911
971912 flush_workqueue (acpi_thermal_pm_queue );
972913 acpi_thermal_unregister_thermal_zone (tz );
973- kfree (tz -> trip_table );
974- kfree (tz );
914+ acpi_thermal_free_thermal_zone (tz );
975915}
976916
977917#ifdef CONFIG_PM_SLEEP
0 commit comments