]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/gpu/drm/nouveau/core/subdev/therm/fan.c
Merge tag 'squashfs-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/pkl/squas...
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / nouveau / core / subdev / therm / fan.c
1 /*
2 * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23 * Martin Peres
24 */
25
26 #include "priv.h"
27
28 #include <core/object.h>
29 #include <core/device.h>
30
31 #include <subdev/gpio.h>
32 #include <subdev/timer.h>
33
34 static int
35 nouveau_fan_update(struct nouveau_fan *fan, bool immediate, int target)
36 {
37 struct nouveau_therm *therm = fan->parent;
38 struct nouveau_therm_priv *priv = (void *)therm;
39 struct nouveau_timer *ptimer = nouveau_timer(priv);
40 unsigned long flags;
41 int ret = 0;
42 int duty;
43
44 /* update target fan speed, restricting to allowed range */
45 spin_lock_irqsave(&fan->lock, flags);
46 if (target < 0)
47 target = fan->percent;
48 target = max_t(u8, target, fan->bios.min_duty);
49 target = min_t(u8, target, fan->bios.max_duty);
50 if (fan->percent != target) {
51 nv_debug(therm, "FAN target: %d\n", target);
52 fan->percent = target;
53 }
54
55 /* check that we're not already at the target duty cycle */
56 duty = fan->get(therm);
57 if (duty == target)
58 goto done;
59
60 /* smooth out the fanspeed increase/decrease */
61 if (!immediate && duty >= 0) {
62 /* the constant "3" is a rough approximation taken from
63 * nvidia's behaviour.
64 * it is meant to bump the fan speed more incrementally
65 */
66 if (duty < target)
67 duty = min(duty + 3, target);
68 else if (duty > target)
69 duty = max(duty - 3, target);
70 } else {
71 duty = target;
72 }
73
74 nv_debug(therm, "FAN update: %d\n", duty);
75 ret = fan->set(therm, duty);
76 if (ret)
77 goto done;
78
79 /* schedule next fan update, if not at target speed already */
80 if (list_empty(&fan->alarm.head) && target != duty) {
81 u16 bump_period = fan->bios.bump_period;
82 u16 slow_down_period = fan->bios.slow_down_period;
83 u64 delay;
84
85 if (duty > target)
86 delay = slow_down_period;
87 else if (duty == target)
88 delay = min(bump_period, slow_down_period) ;
89 else
90 delay = bump_period;
91
92 ptimer->alarm(ptimer, delay * 1000 * 1000, &fan->alarm);
93 }
94
95 done:
96 spin_unlock_irqrestore(&fan->lock, flags);
97 return ret;
98 }
99
100 static void
101 nouveau_fan_alarm(struct nouveau_alarm *alarm)
102 {
103 struct nouveau_fan *fan = container_of(alarm, struct nouveau_fan, alarm);
104 nouveau_fan_update(fan, false, -1);
105 }
106
107 int
108 nouveau_therm_fan_get(struct nouveau_therm *therm)
109 {
110 struct nouveau_therm_priv *priv = (void *)therm;
111 return priv->fan->get(therm);
112 }
113
114 int
115 nouveau_therm_fan_set(struct nouveau_therm *therm, bool immediate, int percent)
116 {
117 struct nouveau_therm_priv *priv = (void *)therm;
118 return nouveau_fan_update(priv->fan, immediate, percent);
119 }
120
121 int
122 nouveau_therm_fan_sense(struct nouveau_therm *therm)
123 {
124 struct nouveau_therm_priv *priv = (void *)therm;
125 struct nouveau_timer *ptimer = nouveau_timer(therm);
126 struct nouveau_gpio *gpio = nouveau_gpio(therm);
127 u32 cycles, cur, prev;
128 u64 start, end, tach;
129
130 if (priv->fan->tach.func == DCB_GPIO_UNUSED)
131 return -ENODEV;
132
133 /* Time a complete rotation and extrapolate to RPM:
134 * When the fan spins, it changes the value of GPIO FAN_SENSE.
135 * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
136 */
137 start = ptimer->read(ptimer);
138 prev = gpio->get(gpio, 0, priv->fan->tach.func, priv->fan->tach.line);
139 cycles = 0;
140 do {
141 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
142
143 cur = gpio->get(gpio, 0, priv->fan->tach.func, priv->fan->tach.line);
144 if (prev != cur) {
145 if (!start)
146 start = ptimer->read(ptimer);
147 cycles++;
148 prev = cur;
149 }
150 } while (cycles < 5 && ptimer->read(ptimer) - start < 250000000);
151 end = ptimer->read(ptimer);
152
153 if (cycles == 5) {
154 tach = (u64)60000000000ULL;
155 do_div(tach, (end - start));
156 return tach;
157 } else
158 return 0;
159 }
160
161 int
162 nouveau_therm_fan_user_get(struct nouveau_therm *therm)
163 {
164 return nouveau_therm_fan_get(therm);
165 }
166
167 int
168 nouveau_therm_fan_user_set(struct nouveau_therm *therm, int percent)
169 {
170 struct nouveau_therm_priv *priv = (void *)therm;
171
172 if (priv->mode != NOUVEAU_THERM_CTRL_MANUAL)
173 return -EINVAL;
174
175 return nouveau_therm_fan_set(therm, true, percent);
176 }
177
178 static void
179 nouveau_therm_fan_set_defaults(struct nouveau_therm *therm)
180 {
181 struct nouveau_therm_priv *priv = (void *)therm;
182
183 priv->fan->bios.pwm_freq = 0;
184 priv->fan->bios.min_duty = 0;
185 priv->fan->bios.max_duty = 100;
186 priv->fan->bios.bump_period = 500;
187 priv->fan->bios.slow_down_period = 2000;
188 /*XXX: talk to mupuf */
189 #if 0
190 priv->fan->bios.linear_min_temp = 40;
191 priv->fan->bios.linear_max_temp = 85;
192 #endif
193 }
194
195 static void
196 nouveau_therm_fan_safety_checks(struct nouveau_therm *therm)
197 {
198 struct nouveau_therm_priv *priv = (void *)therm;
199
200 if (priv->fan->bios.min_duty > 100)
201 priv->fan->bios.min_duty = 100;
202 if (priv->fan->bios.max_duty > 100)
203 priv->fan->bios.max_duty = 100;
204
205 if (priv->fan->bios.min_duty > priv->fan->bios.max_duty)
206 priv->fan->bios.min_duty = priv->fan->bios.max_duty;
207 }
208
209 int
210 nouveau_therm_fan_init(struct nouveau_therm *therm)
211 {
212 return 0;
213 }
214
215 int
216 nouveau_therm_fan_fini(struct nouveau_therm *therm, bool suspend)
217 {
218 struct nouveau_therm_priv *priv = (void *)therm;
219 struct nouveau_timer *ptimer = nouveau_timer(therm);
220
221 if (suspend)
222 ptimer->alarm_cancel(ptimer, &priv->fan->alarm);
223 return 0;
224 }
225
226 int
227 nouveau_therm_fan_ctor(struct nouveau_therm *therm)
228 {
229 struct nouveau_therm_priv *priv = (void *)therm;
230 struct nouveau_gpio *gpio = nouveau_gpio(therm);
231 struct nouveau_bios *bios = nouveau_bios(therm);
232 struct dcb_gpio_func func;
233 int ret;
234
235 /* attempt to locate a drivable fan, and determine control method */
236 ret = gpio->find(gpio, 0, DCB_GPIO_FAN, 0xff, &func);
237 if (ret == 0) {
238 if (func.log[0] & DCB_GPIO_LOG_DIR_IN) {
239 nv_debug(therm, "GPIO_FAN is in input mode\n");
240 ret = -EINVAL;
241 } else {
242 ret = nouveau_fanpwm_create(therm, &func);
243 if (ret != 0)
244 ret = nouveau_fantog_create(therm, &func);
245 }
246 }
247
248 /* no controllable fan found, create a dummy fan module */
249 if (ret != 0) {
250 ret = nouveau_fannil_create(therm);
251 if (ret)
252 return ret;
253 }
254
255 nv_info(therm, "FAN control: %s\n", priv->fan->type);
256
257 /* read the current speed, it is useful when resuming */
258 priv->fan->percent = nouveau_therm_fan_get(therm);
259
260 /* attempt to detect a tachometer connection */
261 ret = gpio->find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff, &priv->fan->tach);
262 if (ret)
263 priv->fan->tach.func = DCB_GPIO_UNUSED;
264
265 /* initialise fan bump/slow update handling */
266 priv->fan->parent = therm;
267 nouveau_alarm_init(&priv->fan->alarm, nouveau_fan_alarm);
268 spin_lock_init(&priv->fan->lock);
269
270 /* other random init... */
271 nouveau_therm_fan_set_defaults(therm);
272 nvbios_perf_fan_parse(bios, &priv->fan->perf);
273 if (nvbios_therm_fan_parse(bios, &priv->fan->bios))
274 nv_error(therm, "parsing the thermal table failed\n");
275 nouveau_therm_fan_safety_checks(therm);
276 return 0;
277 }