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_update_trip(struct nvkm_therm
*obj
)
29 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
30 struct nvbios_therm_trip_point
*trip
= therm
->fan
->bios
.trip
,
32 *last_trip
= therm
->last_trip
;
33 u8 temp
= therm
->base
.temp_get(&therm
->base
);
36 /* look for the trip point corresponding to the current temperature */
38 for (i
= 0; i
< therm
->fan
->bios
.nr_fan_trip
; i
++) {
39 if (temp
>= trip
[i
].temp
)
43 /* account for the hysteresis cycle */
44 if (last_trip
&& temp
<= (last_trip
->temp
) &&
45 temp
> (last_trip
->temp
- last_trip
->hysteresis
))
49 duty
= cur_trip
->fan_duty
;
50 therm
->last_trip
= cur_trip
;
53 therm
->last_trip
= NULL
;
60 nvkm_therm_update_linear(struct nvkm_therm
*obj
)
62 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
63 u8 linear_min_temp
= therm
->fan
->bios
.linear_min_temp
;
64 u8 linear_max_temp
= therm
->fan
->bios
.linear_max_temp
;
65 u8 temp
= therm
->base
.temp_get(&therm
->base
);
68 /* handle the non-linear part first */
69 if (temp
< linear_min_temp
)
70 return therm
->fan
->bios
.min_duty
;
71 else if (temp
> linear_max_temp
)
72 return therm
->fan
->bios
.max_duty
;
74 /* we are in the linear zone */
75 duty
= (temp
- linear_min_temp
);
76 duty
*= (therm
->fan
->bios
.max_duty
- therm
->fan
->bios
.min_duty
);
77 duty
/= (linear_max_temp
- linear_min_temp
);
78 duty
+= therm
->fan
->bios
.min_duty
;
83 nvkm_therm_update(struct nvkm_therm
*obj
, int mode
)
85 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
86 struct nvkm_timer
*tmr
= nvkm_timer(therm
);
92 spin_lock_irqsave(&therm
->lock
, flags
);
98 case NVKM_THERM_CTRL_MANUAL
:
99 tmr
->alarm_cancel(tmr
, &therm
->alarm
);
100 duty
= nvkm_therm_fan_get(&therm
->base
);
105 case NVKM_THERM_CTRL_AUTO
:
106 switch(therm
->fan
->bios
.fan_mode
) {
107 case NVBIOS_THERM_FAN_TRIP
:
108 duty
= nvkm_therm_update_trip(&therm
->base
);
110 case NVBIOS_THERM_FAN_LINEAR
:
111 duty
= nvkm_therm_update_linear(&therm
->base
);
113 case NVBIOS_THERM_FAN_OTHER
:
115 duty
= therm
->cstate
;
121 case NVKM_THERM_CTRL_NONE
:
123 tmr
->alarm_cancel(tmr
, &therm
->alarm
);
127 if (list_empty(&therm
->alarm
.head
) && poll
)
128 tmr
->alarm(tmr
, 1000000000ULL, &therm
->alarm
);
129 spin_unlock_irqrestore(&therm
->lock
, flags
);
132 nv_debug(therm
, "FAN target request: %d%%\n", duty
);
133 nvkm_therm_fan_set(&therm
->base
, immd
, duty
);
138 nvkm_therm_cstate(struct nvkm_therm
*obj
, int fan
, int dir
)
140 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
141 if (!dir
|| (dir
< 0 && fan
< therm
->cstate
) ||
142 (dir
> 0 && fan
> therm
->cstate
)) {
143 nv_debug(therm
, "default fan speed -> %d%%\n", fan
);
145 nvkm_therm_update(&therm
->base
, -1);
151 nvkm_therm_alarm(struct nvkm_alarm
*alarm
)
153 struct nvkm_therm_priv
*therm
=
154 container_of(alarm
, struct nvkm_therm_priv
, alarm
);
155 nvkm_therm_update(&therm
->base
, -1);
159 nvkm_therm_fan_mode(struct nvkm_therm
*obj
, int mode
)
161 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
162 struct nvkm_device
*device
= nv_device(therm
);
163 static const char *name
[] = {
169 /* The default PPWR ucode on fermi interferes with fan management */
170 if ((mode
>= ARRAY_SIZE(name
)) ||
171 (mode
!= NVKM_THERM_CTRL_NONE
&& device
->card_type
>= NV_C0
&&
172 !nvkm_subdev(device
, NVDEV_SUBDEV_PMU
)))
175 /* do not allow automatic fan management if the thermal sensor is
177 if (mode
== NVKM_THERM_CTRL_AUTO
&&
178 therm
->base
.temp_get(&therm
->base
) < 0)
181 if (therm
->mode
== mode
)
184 nv_info(therm
, "fan management: %s\n", name
[mode
]);
185 nvkm_therm_update(&therm
->base
, mode
);
190 nvkm_therm_attr_get(struct nvkm_therm
*obj
, enum nvkm_therm_attr_type type
)
192 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
195 case NVKM_THERM_ATTR_FAN_MIN_DUTY
:
196 return therm
->fan
->bios
.min_duty
;
197 case NVKM_THERM_ATTR_FAN_MAX_DUTY
:
198 return therm
->fan
->bios
.max_duty
;
199 case NVKM_THERM_ATTR_FAN_MODE
:
201 case NVKM_THERM_ATTR_THRS_FAN_BOOST
:
202 return therm
->bios_sensor
.thrs_fan_boost
.temp
;
203 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST
:
204 return therm
->bios_sensor
.thrs_fan_boost
.hysteresis
;
205 case NVKM_THERM_ATTR_THRS_DOWN_CLK
:
206 return therm
->bios_sensor
.thrs_down_clock
.temp
;
207 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST
:
208 return therm
->bios_sensor
.thrs_down_clock
.hysteresis
;
209 case NVKM_THERM_ATTR_THRS_CRITICAL
:
210 return therm
->bios_sensor
.thrs_critical
.temp
;
211 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST
:
212 return therm
->bios_sensor
.thrs_critical
.hysteresis
;
213 case NVKM_THERM_ATTR_THRS_SHUTDOWN
:
214 return therm
->bios_sensor
.thrs_shutdown
.temp
;
215 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST
:
216 return therm
->bios_sensor
.thrs_shutdown
.hysteresis
;
223 nvkm_therm_attr_set(struct nvkm_therm
*obj
,
224 enum nvkm_therm_attr_type type
, int value
)
226 struct nvkm_therm_priv
*therm
= container_of(obj
, typeof(*therm
), base
);
229 case NVKM_THERM_ATTR_FAN_MIN_DUTY
:
232 if (value
> therm
->fan
->bios
.max_duty
)
233 value
= therm
->fan
->bios
.max_duty
;
234 therm
->fan
->bios
.min_duty
= value
;
236 case NVKM_THERM_ATTR_FAN_MAX_DUTY
:
239 if (value
< therm
->fan
->bios
.min_duty
)
240 value
= therm
->fan
->bios
.min_duty
;
241 therm
->fan
->bios
.max_duty
= value
;
243 case NVKM_THERM_ATTR_FAN_MODE
:
244 return nvkm_therm_fan_mode(&therm
->base
, value
);
245 case NVKM_THERM_ATTR_THRS_FAN_BOOST
:
246 therm
->bios_sensor
.thrs_fan_boost
.temp
= value
;
247 therm
->sensor
.program_alarms(&therm
->base
);
249 case NVKM_THERM_ATTR_THRS_FAN_BOOST_HYST
:
250 therm
->bios_sensor
.thrs_fan_boost
.hysteresis
= value
;
251 therm
->sensor
.program_alarms(&therm
->base
);
253 case NVKM_THERM_ATTR_THRS_DOWN_CLK
:
254 therm
->bios_sensor
.thrs_down_clock
.temp
= value
;
255 therm
->sensor
.program_alarms(&therm
->base
);
257 case NVKM_THERM_ATTR_THRS_DOWN_CLK_HYST
:
258 therm
->bios_sensor
.thrs_down_clock
.hysteresis
= value
;
259 therm
->sensor
.program_alarms(&therm
->base
);
261 case NVKM_THERM_ATTR_THRS_CRITICAL
:
262 therm
->bios_sensor
.thrs_critical
.temp
= value
;
263 therm
->sensor
.program_alarms(&therm
->base
);
265 case NVKM_THERM_ATTR_THRS_CRITICAL_HYST
:
266 therm
->bios_sensor
.thrs_critical
.hysteresis
= value
;
267 therm
->sensor
.program_alarms(&therm
->base
);
269 case NVKM_THERM_ATTR_THRS_SHUTDOWN
:
270 therm
->bios_sensor
.thrs_shutdown
.temp
= value
;
271 therm
->sensor
.program_alarms(&therm
->base
);
273 case NVKM_THERM_ATTR_THRS_SHUTDOWN_HYST
:
274 therm
->bios_sensor
.thrs_shutdown
.hysteresis
= value
;
275 therm
->sensor
.program_alarms(&therm
->base
);
283 _nvkm_therm_init(struct nvkm_object
*object
)
285 struct nvkm_therm_priv
*therm
= (void *)object
;
288 ret
= nvkm_subdev_init(&therm
->base
.subdev
);
292 if (therm
->suspend
>= 0) {
293 /* restore the pwm value only when on manual or auto mode */
294 if (therm
->suspend
> 0)
295 nvkm_therm_fan_set(&therm
->base
, true, therm
->fan
->percent
);
297 nvkm_therm_fan_mode(&therm
->base
, therm
->suspend
);
299 nvkm_therm_sensor_init(&therm
->base
);
300 nvkm_therm_fan_init(&therm
->base
);
305 _nvkm_therm_fini(struct nvkm_object
*object
, bool suspend
)
307 struct nvkm_therm_priv
*therm
= (void *)object
;
309 nvkm_therm_fan_fini(&therm
->base
, suspend
);
310 nvkm_therm_sensor_fini(&therm
->base
, suspend
);
312 therm
->suspend
= therm
->mode
;
313 therm
->mode
= NVKM_THERM_CTRL_NONE
;
316 return nvkm_subdev_fini(&therm
->base
.subdev
, suspend
);
320 nvkm_therm_create_(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
321 struct nvkm_oclass
*oclass
, int length
, void **pobject
)
323 struct nvkm_therm_priv
*therm
;
326 ret
= nvkm_subdev_create_(parent
, engine
, oclass
, 0, "PTHERM",
327 "therm", length
, pobject
);
332 nvkm_alarm_init(&therm
->alarm
, nvkm_therm_alarm
);
333 spin_lock_init(&therm
->lock
);
334 spin_lock_init(&therm
->sensor
.alarm_program_lock
);
336 therm
->base
.fan_get
= nvkm_therm_fan_user_get
;
337 therm
->base
.fan_set
= nvkm_therm_fan_user_set
;
338 therm
->base
.fan_sense
= nvkm_therm_fan_sense
;
339 therm
->base
.attr_get
= nvkm_therm_attr_get
;
340 therm
->base
.attr_set
= nvkm_therm_attr_set
;
341 therm
->mode
= therm
->suspend
= -1; /* undefined */
346 nvkm_therm_preinit(struct nvkm_therm
*therm
)
348 nvkm_therm_sensor_ctor(therm
);
349 nvkm_therm_ic_ctor(therm
);
350 nvkm_therm_fan_ctor(therm
);
352 nvkm_therm_fan_mode(therm
, NVKM_THERM_CTRL_AUTO
);
353 nvkm_therm_sensor_preinit(therm
);
358 _nvkm_therm_dtor(struct nvkm_object
*object
)
360 struct nvkm_therm_priv
*therm
= (void *)object
;
362 nvkm_subdev_destroy(&therm
->base
.subdev
);