2 * Copyright 2012 The Nouveau community
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Martin Peres
27 nvkm_therm_temp_get(struct nvkm_therm *therm)
29 if (therm->func->temp_get)
30 return therm->func->temp_get(therm);
35 nvkm_therm_update_trip(struct nvkm_therm *therm)
37 struct nvbios_therm_trip_point *trip = therm->fan->bios.trip,
39 *last_trip = therm->last_trip;
40 u8 temp = therm->func->temp_get(therm);
43 /* look for the trip point corresponding to the current temperature */
45 for (i = 0; i < therm->fan->bios.nr_fan_trip; i++) {
46 if (temp >= trip[i].temp)
50 /* account for the hysteresis cycle */
51 if (last_trip && temp <= (last_trip->temp) &&
52 temp > (last_trip->temp - last_trip->hysteresis))
56 duty = cur_trip->fan_duty;
57 therm->last_trip = cur_trip;
60 therm->last_trip = NULL;
67 nvkm_therm_update_linear(struct nvkm_therm *therm)
69 u8 linear_min_temp = therm->fan->bios.linear_min_temp;
70 u8 linear_max_temp = therm->fan->bios.linear_max_temp;
71 u8 temp = therm->func->temp_get(therm);
74 /* handle the non-linear part first */
75 if (temp < linear_min_temp)
76 return therm->fan->bios.min_duty;
77 else if (temp > linear_max_temp)
78 return therm->fan->bios.max_duty;
80 /* we are in the linear zone */
81 duty = (temp - linear_min_temp);
82 duty *= (therm->fan->bios.max_duty - therm->fan->bios.min_duty);
83 duty /= (linear_max_temp - linear_min_temp);
84 duty += therm->fan->bios.min_duty;
89 nvkm_therm_update(struct nvkm_therm *therm, int mode)
91 struct nvkm_subdev *subdev = &therm->subdev;
92 struct nvkm_timer *tmr = subdev->device->timer;
98 spin_lock_irqsave(&therm->lock, flags);
104 case NVKM_THERM_CTRL_MANUAL:
105 tmr->alarm_cancel(tmr, &therm->alarm);
106 duty = nvkm_therm_fan_get(therm);
111 case NVKM_THERM_CTRL_AUTO:
112 switch(therm->fan->bios.fan_mode) {
113 case NVBIOS_THERM_FAN_TRIP:
114 duty = nvkm_therm_update_trip(therm);
116 case NVBIOS_THERM_FAN_LINEAR:
117 duty = nvkm_therm_update_linear(therm);
119 case NVBIOS_THERM_FAN_OTHER:
121 duty = therm->cstate;
127 case NVKM_THERM_CTRL_NONE:
129 tmr->alarm_cancel(tmr, &therm->alarm);
133 if (list_empty(&therm->alarm.head) && poll)
134 tmr->alarm(tmr, 1000000000ULL, &therm->alarm);
135 spin_unlock_irqrestore(&therm->lock, flags);
138 nvkm_debug(subdev, "FAN target request: %d%%\n", duty);
139 nvkm_therm_fan_set(therm, immd, duty);
144 nvkm_therm_cstate(struct nvkm_therm *therm, int fan, int dir)
146 struct nvkm_subdev *subdev = &therm->subdev;
147 if (!dir || (dir < 0 && fan < therm->cstate) ||
148 (dir > 0 && fan > therm->cstate)) {
149 nvkm_debug(subdev, "default fan speed -> %d%%\n", fan);
151 nvkm_therm_update(therm, -1);
157 nvkm_therm_alarm(struct nvkm_alarm *alarm)
159 struct nvkm_therm *therm =
160 container_of(alarm, struct nvkm_therm, alarm);
161 nvkm_therm_update(therm, -1);
165 nvkm_therm_fan_mode(struct nvkm_therm *therm, int mode)
167 struct nvkm_subdev *subdev = &therm->subdev;
168 struct nvkm_device *device = subdev->device;
169 static const char *name[] = {
175 /* The default PPWR ucode on fermi interferes with fan management */
176 if ((mode >= ARRAY_SIZE(name)) ||
177 (mode != NVKM_THERM_CTRL_NONE && device->card_type >= NV_C0 &&
181 /* do not allow automatic fan management if the thermal sensor is
183 if (mode == NVKM_THERM_CTRL_AUTO &&
184 therm->func->temp_get(therm) < 0)
187 if (therm->mode == mode)
190 nvkm_debug(subdev, "fan management: %s\n", name[mode]);
191 nvkm_therm_update(therm, mode);
196 nvkm_therm_attr_get(struct nvkm_therm *therm, enum nvkm_therm_attr_type type)
199 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
200 return therm->fan->bios.min_duty;
201 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
202 return therm->fan->bios.max_duty;
203 case NVKM_THERM_ATTR_FAN_MODE:
205 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
206 return therm->bios_sensor.thrs_fan_boost.temp;
207 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
208 return therm->bios_sensor.thrs_fan_boost.hysteresis;
209 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
210 return therm->bios_sensor.thrs_down_clock.temp;
211 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
212 return therm->bios_sensor.thrs_down_clock.hysteresis;
213 case NVKM_THERM_ATTR_THRS_CRITICAL:
214 return therm->bios_sensor.thrs_critical.temp;
215 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
216 return therm->bios_sensor.thrs_critical.hysteresis;
217 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
218 return therm->bios_sensor.thrs_shutdown.temp;
219 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
220 return therm->bios_sensor.thrs_shutdown.hysteresis;
227 nvkm_therm_attr_set(struct nvkm_therm *therm,
228 enum nvkm_therm_attr_type type, int value)
231 case NVKM_THERM_ATTR_FAN_MIN_DUTY:
234 if (value > therm->fan->bios.max_duty)
235 value = therm->fan->bios.max_duty;
236 therm->fan->bios.min_duty = value;
238 case NVKM_THERM_ATTR_FAN_MAX_DUTY:
241 if (value < therm->fan->bios.min_duty)
242 value = therm->fan->bios.min_duty;
243 therm->fan->bios.max_duty = value;
245 case NVKM_THERM_ATTR_FAN_MODE:
246 return nvkm_therm_fan_mode(therm, value);
247 case NVKM_THERM_ATTR_THRS_FAN_BOOST:
248 therm->bios_sensor.thrs_fan_boost.temp = value;
249 therm->func->program_alarms(therm);
251 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST:
252 therm->bios_sensor.thrs_fan_boost.hysteresis = value;
253 therm->func->program_alarms(therm);
255 case NVKM_THERM_ATTR_THRS_DOWN_CLK:
256 therm->bios_sensor.thrs_down_clock.temp = value;
257 therm->func->program_alarms(therm);
259 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST:
260 therm->bios_sensor.thrs_down_clock.hysteresis = value;
261 therm->func->program_alarms(therm);
263 case NVKM_THERM_ATTR_THRS_CRITICAL:
264 therm->bios_sensor.thrs_critical.temp = value;
265 therm->func->program_alarms(therm);
267 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST:
268 therm->bios_sensor.thrs_critical.hysteresis = value;
269 therm->func->program_alarms(therm);
271 case NVKM_THERM_ATTR_THRS_SHUTDOWN:
272 therm->bios_sensor.thrs_shutdown.temp = value;
273 therm->func->program_alarms(therm);
275 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST:
276 therm->bios_sensor.thrs_shutdown.hysteresis = value;
277 therm->func->program_alarms(therm);
285 nvkm_therm_intr(struct nvkm_subdev *subdev)
287 struct nvkm_therm *therm = nvkm_therm(subdev);
288 if (therm->func->intr)
289 therm->func->intr(therm);
293 nvkm_therm_fini(struct nvkm_subdev *subdev, bool suspend)
295 struct nvkm_therm *therm = nvkm_therm(subdev);
297 if (therm->func->fini)
298 therm->func->fini(therm);
300 nvkm_therm_fan_fini(therm, suspend);
301 nvkm_therm_sensor_fini(therm, suspend);
304 therm->suspend = therm->mode;
305 therm->mode = NVKM_THERM_CTRL_NONE;
312 nvkm_therm_oneinit(struct nvkm_subdev *subdev)
314 struct nvkm_therm *therm = nvkm_therm(subdev);
315 nvkm_therm_sensor_ctor(therm);
316 nvkm_therm_ic_ctor(therm);
317 nvkm_therm_fan_ctor(therm);
318 nvkm_therm_fan_mode(therm, NVKM_THERM_CTRL_AUTO);
319 nvkm_therm_sensor_preinit(therm);
324 nvkm_therm_init(struct nvkm_subdev *subdev)
326 struct nvkm_therm *therm = nvkm_therm(subdev);
328 therm->func->init(therm);
330 if (therm->suspend >= 0) {
331 /* restore the pwm value only when on manual or auto mode */
332 if (therm->suspend > 0)
333 nvkm_therm_fan_set(therm, true, therm->fan->percent);
335 nvkm_therm_fan_mode(therm, therm->suspend);
338 nvkm_therm_sensor_init(therm);
339 nvkm_therm_fan_init(therm);
344 nvkm_therm_dtor(struct nvkm_subdev *subdev)
346 struct nvkm_therm *therm = nvkm_therm(subdev);
351 static const struct nvkm_subdev_func
353 .dtor = nvkm_therm_dtor,
354 .oneinit = nvkm_therm_oneinit,
355 .init = nvkm_therm_init,
356 .fini = nvkm_therm_fini,
357 .intr = nvkm_therm_intr,
361 nvkm_therm_new_(const struct nvkm_therm_func *func, struct nvkm_device *device,
362 int index, struct nvkm_therm **ptherm)
364 struct nvkm_therm *therm;
366 if (!(therm = *ptherm = kzalloc(sizeof(*therm), GFP_KERNEL)))
369 nvkm_subdev_ctor(&nvkm_therm, device, index, 0, &therm->subdev);
372 nvkm_alarm_init(&therm->alarm, nvkm_therm_alarm);
373 spin_lock_init(&therm->lock);
374 spin_lock_init(&therm->sensor.alarm_program_lock);
376 therm->fan_get = nvkm_therm_fan_user_get;
377 therm->fan_set = nvkm_therm_fan_user_set;
378 therm->attr_get = nvkm_therm_attr_get;
379 therm->attr_set = nvkm_therm_attr_set;
380 therm->mode = therm->suspend = -1; /* undefined */