1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * int340x_thermal_zone.c
4 * Copyright (c) 2015, Intel Corporation.
5 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/acpi.h>
10 #include <linux/thermal.h>
11 #include <linux/units.h>
12 #include "int340x_thermal_zone.h"
13
int340x_thermal_get_zone_temp(struct thermal_zone_device * zone,int * temp)14 static int int340x_thermal_get_zone_temp(struct thermal_zone_device *zone,
15 int *temp)
16 {
17 struct int34x_thermal_zone *d = thermal_zone_device_priv(zone);
18 unsigned long long tmp;
19 acpi_status status;
20
21 status = acpi_evaluate_integer(d->adev->handle, "_TMP", NULL, &tmp);
22 if (ACPI_FAILURE(status))
23 return -EIO;
24
25 if (d->lpat_table) {
26 int conv_temp;
27
28 conv_temp = acpi_lpat_raw_to_temp(d->lpat_table, (int)tmp);
29 if (conv_temp < 0)
30 return conv_temp;
31
32 *temp = conv_temp * 10;
33 } else {
34 /* _TMP returns the temperature in tenths of degrees Kelvin */
35 *temp = deci_kelvin_to_millicelsius(tmp);
36 }
37
38 return 0;
39 }
40
int340x_thermal_set_trip_temp(struct thermal_zone_device * zone,const struct thermal_trip * trip,int temp)41 static int int340x_thermal_set_trip_temp(struct thermal_zone_device *zone,
42 const struct thermal_trip *trip, int temp)
43 {
44 struct int34x_thermal_zone *d = thermal_zone_device_priv(zone);
45 unsigned int trip_index = THERMAL_TRIP_PRIV_TO_INT(trip->priv);
46 char name[] = {'P', 'A', 'T', '0' + trip_index, '\0'};
47 acpi_status status;
48
49 if (trip_index > 9)
50 return -EINVAL;
51
52 status = acpi_execute_simple_method(d->adev->handle, name,
53 millicelsius_to_deci_kelvin(temp));
54 if (ACPI_FAILURE(status))
55 return -EIO;
56
57 return 0;
58 }
59
int340x_thermal_critical(struct thermal_zone_device * zone)60 static void int340x_thermal_critical(struct thermal_zone_device *zone)
61 {
62 dev_dbg(thermal_zone_device(zone), "%s: critical temperature reached\n",
63 thermal_zone_device_type(zone));
64 }
65
int340x_thermal_read_trips(struct acpi_device * zone_adev,struct thermal_trip * zone_trips,int trip_cnt)66 static int int340x_thermal_read_trips(struct acpi_device *zone_adev,
67 struct thermal_trip *zone_trips,
68 int trip_cnt)
69 {
70 int i, ret;
71
72 ret = thermal_acpi_critical_trip_temp(zone_adev,
73 &zone_trips[trip_cnt].temperature);
74 if (!ret) {
75 zone_trips[trip_cnt].type = THERMAL_TRIP_CRITICAL;
76 trip_cnt++;
77 }
78
79 ret = thermal_acpi_hot_trip_temp(zone_adev,
80 &zone_trips[trip_cnt].temperature);
81 if (!ret) {
82 zone_trips[trip_cnt].type = THERMAL_TRIP_HOT;
83 trip_cnt++;
84 }
85
86 ret = thermal_acpi_passive_trip_temp(zone_adev,
87 &zone_trips[trip_cnt].temperature);
88 if (!ret) {
89 zone_trips[trip_cnt].type = THERMAL_TRIP_PASSIVE;
90 trip_cnt++;
91 }
92
93 for (i = 0; i < INT340X_THERMAL_MAX_ACT_TRIP_COUNT; i++) {
94 ret = thermal_acpi_active_trip_temp(zone_adev, i,
95 &zone_trips[trip_cnt].temperature);
96 if (ret)
97 break;
98
99 zone_trips[trip_cnt].type = THERMAL_TRIP_ACTIVE;
100 zone_trips[trip_cnt].priv = THERMAL_INT_TO_TRIP_PRIV(i);
101 trip_cnt++;
102 }
103
104 return trip_cnt;
105 }
106
107 static struct thermal_zone_params int340x_thermal_params = {
108 .no_hwmon = true,
109 };
110
int340x_thermal_zone_add(struct acpi_device * adev,int (* get_temp)(struct thermal_zone_device *,int *))111 struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev,
112 int (*get_temp) (struct thermal_zone_device *, int *))
113 {
114 const struct thermal_zone_device_ops zone_ops = {
115 .set_trip_temp = int340x_thermal_set_trip_temp,
116 .critical = int340x_thermal_critical,
117 .get_temp = get_temp ? get_temp : int340x_thermal_get_zone_temp,
118 };
119 struct int34x_thermal_zone *int34x_zone;
120 struct thermal_trip *zone_trips;
121 unsigned long long trip_cnt = 0;
122 unsigned long long hyst;
123 acpi_status status;
124 int i, ret;
125
126 int34x_zone = kzalloc(sizeof(*int34x_zone), GFP_KERNEL);
127 if (!int34x_zone)
128 return ERR_PTR(-ENOMEM);
129
130 int34x_zone->adev = adev;
131
132 status = acpi_evaluate_integer(adev->handle, "PATC", NULL, &trip_cnt);
133 if (ACPI_SUCCESS(status))
134 int34x_zone->aux_trip_nr = trip_cnt;
135
136 zone_trips = kzalloc(sizeof(*zone_trips) * (trip_cnt + INT340X_THERMAL_MAX_TRIP_COUNT),
137 GFP_KERNEL);
138 if (!zone_trips) {
139 ret = -ENOMEM;
140 goto err_trips_alloc;
141 }
142
143 for (i = 0; i < trip_cnt; i++) {
144 zone_trips[i].type = THERMAL_TRIP_PASSIVE;
145 zone_trips[i].temperature = THERMAL_TEMP_INVALID;
146 zone_trips[i].flags |= THERMAL_TRIP_FLAG_RW_TEMP;
147 zone_trips[i].priv = THERMAL_INT_TO_TRIP_PRIV(i);
148 }
149
150 trip_cnt = int340x_thermal_read_trips(adev, zone_trips, trip_cnt);
151
152 status = acpi_evaluate_integer(adev->handle, "GTSH", NULL, &hyst);
153 if (ACPI_SUCCESS(status))
154 hyst *= 100;
155 else
156 hyst = 0;
157
158 for (i = 0; i < trip_cnt; ++i)
159 zone_trips[i].hysteresis = hyst;
160
161 int34x_zone->lpat_table = acpi_lpat_get_conversion_table(adev->handle);
162
163 int34x_zone->zone = thermal_zone_device_register_with_trips(
164 acpi_device_bid(adev),
165 zone_trips, trip_cnt,
166 int34x_zone,
167 &zone_ops,
168 &int340x_thermal_params,
169 0, 0);
170 kfree(zone_trips);
171
172 if (IS_ERR(int34x_zone->zone)) {
173 ret = PTR_ERR(int34x_zone->zone);
174 goto err_thermal_zone;
175 }
176 ret = thermal_zone_device_enable(int34x_zone->zone);
177 if (ret)
178 goto err_enable;
179
180 return int34x_zone;
181
182 err_enable:
183 thermal_zone_device_unregister(int34x_zone->zone);
184 err_thermal_zone:
185 acpi_lpat_free_conversion_table(int34x_zone->lpat_table);
186 err_trips_alloc:
187 kfree(int34x_zone);
188 return ERR_PTR(ret);
189 }
190 EXPORT_SYMBOL_GPL(int340x_thermal_zone_add);
191
int340x_thermal_zone_remove(struct int34x_thermal_zone * int34x_zone)192 void int340x_thermal_zone_remove(struct int34x_thermal_zone *int34x_zone)
193 {
194 thermal_zone_device_unregister(int34x_zone->zone);
195 acpi_lpat_free_conversion_table(int34x_zone->lpat_table);
196 kfree(int34x_zone);
197 }
198 EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove);
199
int340x_update_one_trip(struct thermal_trip * trip,void * arg)200 static int int340x_update_one_trip(struct thermal_trip *trip, void *arg)
201 {
202 struct int34x_thermal_zone *int34x_zone = arg;
203 struct acpi_device *zone_adev = int34x_zone->adev;
204 int temp, err;
205
206 switch (trip->type) {
207 case THERMAL_TRIP_CRITICAL:
208 err = thermal_acpi_critical_trip_temp(zone_adev, &temp);
209 break;
210 case THERMAL_TRIP_HOT:
211 err = thermal_acpi_hot_trip_temp(zone_adev, &temp);
212 break;
213 case THERMAL_TRIP_PASSIVE:
214 err = thermal_acpi_passive_trip_temp(zone_adev, &temp);
215 break;
216 case THERMAL_TRIP_ACTIVE:
217 err = thermal_acpi_active_trip_temp(zone_adev,
218 THERMAL_TRIP_PRIV_TO_INT(trip->priv),
219 &temp);
220 break;
221 default:
222 err = -ENODEV;
223 }
224 if (err)
225 temp = THERMAL_TEMP_INVALID;
226
227 thermal_zone_set_trip_temp(int34x_zone->zone, trip, temp);
228
229 return 0;
230 }
231
int340x_thermal_update_trips(struct int34x_thermal_zone * int34x_zone)232 void int340x_thermal_update_trips(struct int34x_thermal_zone *int34x_zone)
233 {
234 thermal_zone_for_each_trip(int34x_zone->zone, int340x_update_one_trip,
235 int34x_zone);
236 }
237 EXPORT_SYMBOL_GPL(int340x_thermal_update_trips);
238
239 MODULE_AUTHOR("Aaron Lu <[email protected]>");
240 MODULE_AUTHOR("Srinivas Pandruvada <[email protected]>");
241 MODULE_DESCRIPTION("Intel INT340x common thermal zone handler");
242 MODULE_LICENSE("GPL v2");
243