83ebf366a221ae5a786ec4388b0387b602751f00
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvkm / 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 #include "priv.h"
26
27 #include <subdev/bios/fan.h>
28 #include <subdev/gpio.h>
29 #include <subdev/timer.h>
30
31 static int
32 nvkm_fan_update(struct nvkm_fan *fan, bool immediate, int target)
33 {
34         struct nvkm_therm_priv *therm = (void *)fan->parent;
35         struct nvkm_timer *tmr = nvkm_timer(therm);
36         unsigned long flags;
37         int ret = 0;
38         int duty;
39
40         /* update target fan speed, restricting to allowed range */
41         spin_lock_irqsave(&fan->lock, flags);
42         if (target < 0)
43                 target = fan->percent;
44         target = max_t(u8, target, fan->bios.min_duty);
45         target = min_t(u8, target, fan->bios.max_duty);
46         if (fan->percent != target) {
47                 nv_debug(therm, "FAN target: %d\n", target);
48                 fan->percent = target;
49         }
50
51         /* check that we're not already at the target duty cycle */
52         duty = fan->get(&therm->base);
53         if (duty == target) {
54                 spin_unlock_irqrestore(&fan->lock, flags);
55                 return 0;
56         }
57
58         /* smooth out the fanspeed increase/decrease */
59         if (!immediate && duty >= 0) {
60                 /* the constant "3" is a rough approximation taken from
61                  * nvidia's behaviour.
62                  * it is meant to bump the fan speed more incrementally
63                  */
64                 if (duty < target)
65                         duty = min(duty + 3, target);
66                 else if (duty > target)
67                         duty = max(duty - 3, target);
68         } else {
69                 duty = target;
70         }
71
72         nv_debug(therm, "FAN update: %d\n", duty);
73         ret = fan->set(&therm->base, duty);
74         if (ret) {
75                 spin_unlock_irqrestore(&fan->lock, flags);
76                 return ret;
77         }
78
79         /* fan speed updated, drop the fan lock before grabbing the
80          * alarm-scheduling lock and risking a deadlock
81          */
82         spin_unlock_irqrestore(&fan->lock, flags);
83
84         /* schedule next fan update, if not at target speed already */
85         if (list_empty(&fan->alarm.head) && target != duty) {
86                 u16 bump_period = fan->bios.bump_period;
87                 u16 slow_down_period = fan->bios.slow_down_period;
88                 u64 delay;
89
90                 if (duty > target)
91                         delay = slow_down_period;
92                 else if (duty == target)
93                         delay = min(bump_period, slow_down_period) ;
94                 else
95                         delay = bump_period;
96
97                 tmr->alarm(tmr, delay * 1000 * 1000, &fan->alarm);
98         }
99
100         return ret;
101 }
102
103 static void
104 nvkm_fan_alarm(struct nvkm_alarm *alarm)
105 {
106         struct nvkm_fan *fan = container_of(alarm, struct nvkm_fan, alarm);
107         nvkm_fan_update(fan, false, -1);
108 }
109
110 int
111 nvkm_therm_fan_get(struct nvkm_therm *obj)
112 {
113         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
114         return therm->fan->get(&therm->base);
115 }
116
117 int
118 nvkm_therm_fan_set(struct nvkm_therm *obj, bool immediate, int percent)
119 {
120         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
121         return nvkm_fan_update(therm->fan, immediate, percent);
122 }
123
124 int
125 nvkm_therm_fan_sense(struct nvkm_therm *obj)
126 {
127         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
128         struct nvkm_timer *tmr = nvkm_timer(therm);
129         struct nvkm_gpio *gpio = nvkm_gpio(therm);
130         u32 cycles, cur, prev;
131         u64 start, end, tach;
132
133         if (therm->fan->tach.func == DCB_GPIO_UNUSED)
134                 return -ENODEV;
135
136         /* Time a complete rotation and extrapolate to RPM:
137          * When the fan spins, it changes the value of GPIO FAN_SENSE.
138          * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
139          */
140         start = tmr->read(tmr);
141         prev = gpio->get(gpio, 0, therm->fan->tach.func, therm->fan->tach.line);
142         cycles = 0;
143         do {
144                 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
145
146                 cur = gpio->get(gpio, 0, therm->fan->tach.func, therm->fan->tach.line);
147                 if (prev != cur) {
148                         if (!start)
149                                 start = tmr->read(tmr);
150                         cycles++;
151                         prev = cur;
152                 }
153         } while (cycles < 5 && tmr->read(tmr) - start < 250000000);
154         end = tmr->read(tmr);
155
156         if (cycles == 5) {
157                 tach = (u64)60000000000ULL;
158                 do_div(tach, (end - start));
159                 return tach;
160         } else
161                 return 0;
162 }
163
164 int
165 nvkm_therm_fan_user_get(struct nvkm_therm *obj)
166 {
167         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
168         return nvkm_therm_fan_get(&therm->base);
169 }
170
171 int
172 nvkm_therm_fan_user_set(struct nvkm_therm *obj, int percent)
173 {
174         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
175
176         if (therm->mode != NVKM_THERM_CTRL_MANUAL)
177                 return -EINVAL;
178
179         return nvkm_therm_fan_set(&therm->base, true, percent);
180 }
181
182 static void
183 nvkm_therm_fan_set_defaults(struct nvkm_therm *obj)
184 {
185         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
186
187         therm->fan->bios.pwm_freq = 0;
188         therm->fan->bios.min_duty = 0;
189         therm->fan->bios.max_duty = 100;
190         therm->fan->bios.bump_period = 500;
191         therm->fan->bios.slow_down_period = 2000;
192         therm->fan->bios.linear_min_temp = 40;
193         therm->fan->bios.linear_max_temp = 85;
194 }
195
196 static void
197 nvkm_therm_fan_safety_checks(struct nvkm_therm *obj)
198 {
199         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
200
201         if (therm->fan->bios.min_duty > 100)
202                 therm->fan->bios.min_duty = 100;
203         if (therm->fan->bios.max_duty > 100)
204                 therm->fan->bios.max_duty = 100;
205
206         if (therm->fan->bios.min_duty > therm->fan->bios.max_duty)
207                 therm->fan->bios.min_duty = therm->fan->bios.max_duty;
208 }
209
210 int
211 nvkm_therm_fan_init(struct nvkm_therm *obj)
212 {
213         return 0;
214 }
215
216 int
217 nvkm_therm_fan_fini(struct nvkm_therm *obj, bool suspend)
218 {
219         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
220         struct nvkm_timer *tmr = nvkm_timer(therm);
221
222         if (suspend)
223                 tmr->alarm_cancel(tmr, &therm->fan->alarm);
224         return 0;
225 }
226
227 int
228 nvkm_therm_fan_ctor(struct nvkm_therm *obj)
229 {
230         struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
231         struct nvkm_gpio *gpio = nvkm_gpio(therm);
232         struct nvkm_bios *bios = nvkm_bios(therm);
233         struct dcb_gpio_func func;
234         int ret;
235
236         /* attempt to locate a drivable fan, and determine control method */
237         ret = gpio->find(gpio, 0, DCB_GPIO_FAN, 0xff, &func);
238         if (ret == 0) {
239                 /* FIXME: is this really the place to perform such checks ? */
240                 if (func.line != 16 && func.log[0] & DCB_GPIO_LOG_DIR_IN) {
241                         nv_debug(therm, "GPIO_FAN is in input mode\n");
242                         ret = -EINVAL;
243                 } else {
244                         ret = nvkm_fanpwm_create(&therm->base, &func);
245                         if (ret != 0)
246                                 ret = nvkm_fantog_create(&therm->base, &func);
247                 }
248         }
249
250         /* no controllable fan found, create a dummy fan module */
251         if (ret != 0) {
252                 ret = nvkm_fannil_create(&therm->base);
253                 if (ret)
254                         return ret;
255         }
256
257         nv_info(therm, "FAN control: %s\n", therm->fan->type);
258
259         /* read the current speed, it is useful when resuming */
260         therm->fan->percent = nvkm_therm_fan_get(&therm->base);
261
262         /* attempt to detect a tachometer connection */
263         ret = gpio->find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff, &therm->fan->tach);
264         if (ret)
265                 therm->fan->tach.func = DCB_GPIO_UNUSED;
266
267         /* initialise fan bump/slow update handling */
268         therm->fan->parent = &therm->base;
269         nvkm_alarm_init(&therm->fan->alarm, nvkm_fan_alarm);
270         spin_lock_init(&therm->fan->lock);
271
272         /* other random init... */
273         nvkm_therm_fan_set_defaults(&therm->base);
274         nvbios_perf_fan_parse(bios, &therm->fan->perf);
275         if (!nvbios_fan_parse(bios, &therm->fan->bios)) {
276                 nv_debug(therm, "parsing the fan table failed\n");
277                 if (nvbios_therm_fan_parse(bios, &therm->fan->bios))
278                         nv_error(therm, "parsing both fan tables failed\n");
279         }
280         nvkm_therm_fan_safety_checks(&therm->base);
281         return 0;
282 }