drm/nouveau/fan: restore pwm value on resume when in manual/auto mode
[deliverable/linux.git] / drivers / gpu / drm / nouveau / core / subdev / therm / base.c
CommitLineData
aa1b9b48
MP
1/*
2 * Copyright 2012 The Nouveau community
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Martin Peres
23 */
24
25#include <core/object.h>
26#include <core/device.h>
27
28#include <subdev/bios.h>
29
30#include "priv.h"
31
694472f4
MP
32static int
33nouveau_therm_update_trip(struct nouveau_therm *therm)
34{
35 struct nouveau_therm_priv *priv = (void *)therm;
36 struct nouveau_therm_trip_point *trip = priv->fan->bios.trip,
37 *cur_trip = NULL,
38 *last_trip = priv->last_trip;
39 u8 temp = therm->temp_get(therm);
40 u16 duty, i;
41
42 /* look for the trip point corresponding to the current temperature */
43 cur_trip = NULL;
44 for (i = 0; i < priv->fan->bios.nr_fan_trip; i++) {
45 if (temp >= trip[i].temp)
46 cur_trip = &trip[i];
47 }
48
49 /* account for the hysteresis cycle */
50 if (last_trip && temp <= (last_trip->temp) &&
51 temp > (last_trip->temp - last_trip->hysteresis))
52 cur_trip = last_trip;
53
54 if (cur_trip) {
55 duty = cur_trip->fan_duty;
56 priv->last_trip = cur_trip;
57 } else {
58 duty = 0;
59 priv->last_trip = NULL;
60 }
61
62 return duty;
63}
64
65static int
66nouveau_therm_update_linear(struct nouveau_therm *therm)
67{
68 struct nouveau_therm_priv *priv = (void *)therm;
69 u8 linear_min_temp = priv->fan->bios.linear_min_temp;
70 u8 linear_max_temp = priv->fan->bios.linear_max_temp;
71 u8 temp = therm->temp_get(therm);
72 u16 duty;
73
a624bafb
MP
74 /* handle the non-linear part first */
75 if (temp < linear_min_temp)
76 return priv->fan->bios.min_duty;
77 else if (temp > linear_max_temp)
78 return priv->fan->bios.max_duty;
79
80 /* we are in the linear zone */
694472f4
MP
81 duty = (temp - linear_min_temp);
82 duty *= (priv->fan->bios.max_duty - priv->fan->bios.min_duty);
83 duty /= (linear_max_temp - linear_min_temp);
84 duty += priv->fan->bios.min_duty;
85
86 return duty;
87}
88
89static void
90nouveau_therm_update(struct nouveau_therm *therm, int mode)
91{
92 struct nouveau_timer *ptimer = nouveau_timer(therm);
93 struct nouveau_therm_priv *priv = (void *)therm;
94 unsigned long flags;
95 int duty;
96
97 spin_lock_irqsave(&priv->lock, flags);
98 if (mode < 0)
99 mode = priv->mode;
100 priv->mode = mode;
101
102 switch (mode) {
1a22274b
BS
103 case NOUVEAU_THERM_CTRL_MANUAL:
104 duty = nouveau_therm_fan_get(therm);
105 if (duty < 0)
106 duty = 100;
694472f4 107 break;
1a22274b 108 case NOUVEAU_THERM_CTRL_AUTO:
694472f4
MP
109 if (priv->fan->bios.nr_fan_trip)
110 duty = nouveau_therm_update_trip(therm);
111 else
112 duty = nouveau_therm_update_linear(therm);
113 break;
1a22274b 114 case NOUVEAU_THERM_CTRL_NONE:
694472f4
MP
115 default:
116 goto done;
117 }
118
1a22274b
BS
119 nv_debug(therm, "FAN target request: %d%%\n", duty);
120 nouveau_therm_fan_set(therm, (mode != NOUVEAU_THERM_CTRL_AUTO), duty);
694472f4
MP
121
122done:
1a22274b 123 if (list_empty(&priv->alarm.head) && (mode == NOUVEAU_THERM_CTRL_AUTO))
694472f4
MP
124 ptimer->alarm(ptimer, 1000000000ULL, &priv->alarm);
125 spin_unlock_irqrestore(&priv->lock, flags);
126}
127
128static void
129nouveau_therm_alarm(struct nouveau_alarm *alarm)
130{
131 struct nouveau_therm_priv *priv =
132 container_of(alarm, struct nouveau_therm_priv, alarm);
133 nouveau_therm_update(&priv->base, -1);
134}
135
0083b91d 136int
c4ce9246 137nouveau_therm_fan_mode(struct nouveau_therm *therm, int mode)
694472f4
MP
138{
139 struct nouveau_therm_priv *priv = (void *)therm;
1a22274b
BS
140 struct nouveau_device *device = nv_device(therm);
141 static const char *name[] = {
142 "disabled",
143 "manual",
144 "automatic"
145 };
694472f4
MP
146
147 /* The default PDAEMON ucode interferes with fan management */
1a22274b
BS
148 if ((mode >= ARRAY_SIZE(name)) ||
149 (mode != NOUVEAU_THERM_CTRL_NONE && device->card_type >= NV_C0))
694472f4
MP
150 return -EINVAL;
151
98ee7c7c
MP
152 /* do not allow automatic fan management if the thermal sensor is
153 * not available */
154 if (priv->mode == 2 && therm->temp_get(therm) < 0)
155 return -EINVAL;
156
1a22274b
BS
157 if (priv->mode == mode)
158 return 0;
694472f4 159
c4ce9246 160 nv_info(therm, "fan management: %s\n", name[mode]);
694472f4
MP
161 nouveau_therm_update(therm, mode);
162 return 0;
163}
164
aa1b9b48
MP
165int
166nouveau_therm_attr_get(struct nouveau_therm *therm,
167 enum nouveau_therm_attr_type type)
168{
169 struct nouveau_therm_priv *priv = (void *)therm;
170
171 switch (type) {
172 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
9c3bd3a5 173 return priv->fan->bios.min_duty;
aa1b9b48 174 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
9c3bd3a5 175 return priv->fan->bios.max_duty;
2f951a5d 176 case NOUVEAU_THERM_ATTR_FAN_MODE:
694472f4 177 return priv->mode;
aa1b9b48
MP
178 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
179 return priv->bios_sensor.thrs_fan_boost.temp;
180 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
181 return priv->bios_sensor.thrs_fan_boost.hysteresis;
182 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
183 return priv->bios_sensor.thrs_down_clock.temp;
184 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
185 return priv->bios_sensor.thrs_down_clock.hysteresis;
186 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
187 return priv->bios_sensor.thrs_critical.temp;
188 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
189 return priv->bios_sensor.thrs_critical.hysteresis;
190 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
191 return priv->bios_sensor.thrs_shutdown.temp;
192 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
193 return priv->bios_sensor.thrs_shutdown.hysteresis;
194 }
195
196 return -EINVAL;
197}
198
199int
200nouveau_therm_attr_set(struct nouveau_therm *therm,
201 enum nouveau_therm_attr_type type, int value)
202{
203 struct nouveau_therm_priv *priv = (void *)therm;
204
205 switch (type) {
206 case NOUVEAU_THERM_ATTR_FAN_MIN_DUTY:
207 if (value < 0)
208 value = 0;
9c3bd3a5
BS
209 if (value > priv->fan->bios.max_duty)
210 value = priv->fan->bios.max_duty;
211 priv->fan->bios.min_duty = value;
aa1b9b48
MP
212 return 0;
213 case NOUVEAU_THERM_ATTR_FAN_MAX_DUTY:
214 if (value < 0)
215 value = 0;
9c3bd3a5
BS
216 if (value < priv->fan->bios.min_duty)
217 value = priv->fan->bios.min_duty;
218 priv->fan->bios.max_duty = value;
aa1b9b48 219 return 0;
2f951a5d 220 case NOUVEAU_THERM_ATTR_FAN_MODE:
c4ce9246 221 return nouveau_therm_fan_mode(therm, value);
aa1b9b48
MP
222 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST:
223 priv->bios_sensor.thrs_fan_boost.temp = value;
0083b91d 224 priv->sensor.program_alarms(therm);
aa1b9b48
MP
225 return 0;
226 case NOUVEAU_THERM_ATTR_THRS_FAN_BOOST_HYST:
227 priv->bios_sensor.thrs_fan_boost.hysteresis = value;
0083b91d 228 priv->sensor.program_alarms(therm);
aa1b9b48
MP
229 return 0;
230 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK:
231 priv->bios_sensor.thrs_down_clock.temp = value;
0083b91d 232 priv->sensor.program_alarms(therm);
aa1b9b48
MP
233 return 0;
234 case NOUVEAU_THERM_ATTR_THRS_DOWN_CLK_HYST:
235 priv->bios_sensor.thrs_down_clock.hysteresis = value;
0083b91d 236 priv->sensor.program_alarms(therm);
aa1b9b48
MP
237 return 0;
238 case NOUVEAU_THERM_ATTR_THRS_CRITICAL:
239 priv->bios_sensor.thrs_critical.temp = value;
0083b91d 240 priv->sensor.program_alarms(therm);
aa1b9b48
MP
241 return 0;
242 case NOUVEAU_THERM_ATTR_THRS_CRITICAL_HYST:
243 priv->bios_sensor.thrs_critical.hysteresis = value;
0083b91d 244 priv->sensor.program_alarms(therm);
aa1b9b48
MP
245 return 0;
246 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN:
247 priv->bios_sensor.thrs_shutdown.temp = value;
0083b91d 248 priv->sensor.program_alarms(therm);
aa1b9b48
MP
249 return 0;
250 case NOUVEAU_THERM_ATTR_THRS_SHUTDOWN_HYST:
251 priv->bios_sensor.thrs_shutdown.hysteresis = value;
0083b91d 252 priv->sensor.program_alarms(therm);
aa1b9b48
MP
253 return 0;
254 }
255
256 return -EINVAL;
257}
258
259int
5f066c32 260_nouveau_therm_init(struct nouveau_object *object)
aa1b9b48
MP
261{
262 struct nouveau_therm *therm = (void *)object;
263 struct nouveau_therm_priv *priv = (void *)therm;
264 int ret;
265
266 ret = nouveau_subdev_init(&therm->base);
267 if (ret)
268 return ret;
269
4cc00ad1
MP
270 if (priv->suspend >= 0) {
271 /* restore the pwm value only when on manual or auto mode */
272 if (priv->suspend > 0)
273 nouveau_therm_fan_set(therm, true, priv->fan->percent);
274
ffb8ea8a 275 nouveau_therm_fan_mode(therm, priv->suspend);
4cc00ad1 276 }
0083b91d 277 priv->sensor.program_alarms(therm);
aa1b9b48
MP
278 return 0;
279}
280
281int
5f066c32 282_nouveau_therm_fini(struct nouveau_object *object, bool suspend)
aa1b9b48
MP
283{
284 struct nouveau_therm *therm = (void *)object;
285 struct nouveau_therm_priv *priv = (void *)therm;
286
1a22274b
BS
287 if (suspend) {
288 priv->suspend = priv->mode;
289 priv->mode = NOUVEAU_THERM_CTRL_NONE;
290 }
aa1b9b48
MP
291
292 return nouveau_subdev_fini(&therm->base, suspend);
293}
5f066c32
BS
294
295int
296nouveau_therm_create_(struct nouveau_object *parent,
297 struct nouveau_object *engine,
298 struct nouveau_oclass *oclass,
299 int length, void **pobject)
300{
301 struct nouveau_therm_priv *priv;
302 int ret;
303
304 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "PTHERM",
305 "therm", length, pobject);
306 priv = *pobject;
307 if (ret)
308 return ret;
309
694472f4
MP
310 nouveau_alarm_init(&priv->alarm, nouveau_therm_alarm);
311 spin_lock_init(&priv->lock);
3969f05b 312 spin_lock_init(&priv->sensor.alarm_program_lock);
694472f4 313
5f066c32
BS
314 priv->base.fan_get = nouveau_therm_fan_user_get;
315 priv->base.fan_set = nouveau_therm_fan_user_set;
316 priv->base.fan_sense = nouveau_therm_fan_sense;
317 priv->base.attr_get = nouveau_therm_attr_get;
318 priv->base.attr_set = nouveau_therm_attr_set;
1a22274b 319 priv->mode = priv->suspend = -1; /* undefined */
5f066c32
BS
320 return 0;
321}
9c3bd3a5
BS
322
323int
324nouveau_therm_preinit(struct nouveau_therm *therm)
325{
9c3bd3a5 326 nouveau_therm_sensor_ctor(therm);
13506e2a 327 nouveau_therm_ic_ctor(therm);
9c3bd3a5 328 nouveau_therm_fan_ctor(therm);
1a22274b 329
c4ce9246 330 nouveau_therm_fan_mode(therm, NOUVEAU_THERM_CTRL_NONE);
0b3ee377 331 nouveau_therm_sensor_preinit(therm);
9c3bd3a5
BS
332 return 0;
333}
334
335void
336_nouveau_therm_dtor(struct nouveau_object *object)
337{
338 struct nouveau_therm_priv *priv = (void *)object;
339 kfree(priv->fan);
340 nouveau_subdev_destroy(&priv->base.base);
341}
This page took 0.139497 seconds and 5 git commands to generate.