ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nvkm / subdev / therm / fan.c
blob434fa745ca40f420d24273fcd0e2e3e391c75cc7
1 /*
2 * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23 * Martin Peres
25 #include "priv.h"
27 #include <subdev/bios/fan.h>
28 #include <subdev/gpio.h>
29 #include <subdev/timer.h>
31 static int
32 nvkm_fan_update(struct nvkm_fan *fan, bool immediate, int target)
34 struct nvkm_therm *therm = fan->parent;
35 struct nvkm_therm_priv *priv = (void *)therm;
36 struct nvkm_timer *ptimer = nvkm_timer(priv);
37 unsigned long flags;
38 int ret = 0;
39 int duty;
41 /* update target fan speed, restricting to allowed range */
42 spin_lock_irqsave(&fan->lock, flags);
43 if (target < 0)
44 target = fan->percent;
45 target = max_t(u8, target, fan->bios.min_duty);
46 target = min_t(u8, target, fan->bios.max_duty);
47 if (fan->percent != target) {
48 nv_debug(therm, "FAN target: %d\n", target);
49 fan->percent = target;
52 /* check that we're not already at the target duty cycle */
53 duty = fan->get(therm);
54 if (duty == target) {
55 spin_unlock_irqrestore(&fan->lock, flags);
56 return 0;
59 /* smooth out the fanspeed increase/decrease */
60 if (!immediate && duty >= 0) {
61 /* the constant "3" is a rough approximation taken from
62 * nvidia's behaviour.
63 * it is meant to bump the fan speed more incrementally
65 if (duty < target)
66 duty = min(duty + 3, target);
67 else if (duty > target)
68 duty = max(duty - 3, target);
69 } else {
70 duty = target;
73 nv_debug(therm, "FAN update: %d\n", duty);
74 ret = fan->set(therm, duty);
75 if (ret) {
76 spin_unlock_irqrestore(&fan->lock, flags);
77 return ret;
80 /* fan speed updated, drop the fan lock before grabbing the
81 * alarm-scheduling lock and risking a deadlock
83 spin_unlock_irqrestore(&fan->lock, flags);
85 /* schedule next fan update, if not at target speed already */
86 if (list_empty(&fan->alarm.head) && target != duty) {
87 u16 bump_period = fan->bios.bump_period;
88 u16 slow_down_period = fan->bios.slow_down_period;
89 u64 delay;
91 if (duty > target)
92 delay = slow_down_period;
93 else if (duty == target)
94 delay = min(bump_period, slow_down_period) ;
95 else
96 delay = bump_period;
98 ptimer->alarm(ptimer, delay * 1000 * 1000, &fan->alarm);
101 return ret;
104 static void
105 nvkm_fan_alarm(struct nvkm_alarm *alarm)
107 struct nvkm_fan *fan = container_of(alarm, struct nvkm_fan, alarm);
108 nvkm_fan_update(fan, false, -1);
112 nvkm_therm_fan_get(struct nvkm_therm *therm)
114 struct nvkm_therm_priv *priv = (void *)therm;
115 return priv->fan->get(therm);
119 nvkm_therm_fan_set(struct nvkm_therm *therm, bool immediate, int percent)
121 struct nvkm_therm_priv *priv = (void *)therm;
122 return nvkm_fan_update(priv->fan, immediate, percent);
126 nvkm_therm_fan_sense(struct nvkm_therm *therm)
128 struct nvkm_therm_priv *priv = (void *)therm;
129 struct nvkm_timer *ptimer = nvkm_timer(therm);
130 struct nvkm_gpio *gpio = nvkm_gpio(therm);
131 u32 cycles, cur, prev;
132 u64 start, end, tach;
134 if (priv->fan->tach.func == DCB_GPIO_UNUSED)
135 return -ENODEV;
137 /* Time a complete rotation and extrapolate to RPM:
138 * When the fan spins, it changes the value of GPIO FAN_SENSE.
139 * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
141 start = ptimer->read(ptimer);
142 prev = gpio->get(gpio, 0, priv->fan->tach.func, priv->fan->tach.line);
143 cycles = 0;
144 do {
145 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
147 cur = gpio->get(gpio, 0, priv->fan->tach.func, priv->fan->tach.line);
148 if (prev != cur) {
149 if (!start)
150 start = ptimer->read(ptimer);
151 cycles++;
152 prev = cur;
154 } while (cycles < 5 && ptimer->read(ptimer) - start < 250000000);
155 end = ptimer->read(ptimer);
157 if (cycles == 5) {
158 tach = (u64)60000000000ULL;
159 do_div(tach, (end - start));
160 return tach;
161 } else
162 return 0;
166 nvkm_therm_fan_user_get(struct nvkm_therm *therm)
168 return nvkm_therm_fan_get(therm);
172 nvkm_therm_fan_user_set(struct nvkm_therm *therm, int percent)
174 struct nvkm_therm_priv *priv = (void *)therm;
176 if (priv->mode != NVKM_THERM_CTRL_MANUAL)
177 return -EINVAL;
179 return nvkm_therm_fan_set(therm, true, percent);
182 static void
183 nvkm_therm_fan_set_defaults(struct nvkm_therm *therm)
185 struct nvkm_therm_priv *priv = (void *)therm;
187 priv->fan->bios.pwm_freq = 0;
188 priv->fan->bios.min_duty = 0;
189 priv->fan->bios.max_duty = 100;
190 priv->fan->bios.bump_period = 500;
191 priv->fan->bios.slow_down_period = 2000;
192 priv->fan->bios.linear_min_temp = 40;
193 priv->fan->bios.linear_max_temp = 85;
196 static void
197 nvkm_therm_fan_safety_checks(struct nvkm_therm *therm)
199 struct nvkm_therm_priv *priv = (void *)therm;
201 if (priv->fan->bios.min_duty > 100)
202 priv->fan->bios.min_duty = 100;
203 if (priv->fan->bios.max_duty > 100)
204 priv->fan->bios.max_duty = 100;
206 if (priv->fan->bios.min_duty > priv->fan->bios.max_duty)
207 priv->fan->bios.min_duty = priv->fan->bios.max_duty;
211 nvkm_therm_fan_init(struct nvkm_therm *therm)
213 return 0;
217 nvkm_therm_fan_fini(struct nvkm_therm *therm, bool suspend)
219 struct nvkm_therm_priv *priv = (void *)therm;
220 struct nvkm_timer *ptimer = nvkm_timer(therm);
222 if (suspend)
223 ptimer->alarm_cancel(ptimer, &priv->fan->alarm);
224 return 0;
228 nvkm_therm_fan_ctor(struct nvkm_therm *therm)
230 struct nvkm_therm_priv *priv = (void *)therm;
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;
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, &func);
245 if (ret != 0)
246 ret = nvkm_fantog_create(therm, &func);
250 /* no controllable fan found, create a dummy fan module */
251 if (ret != 0) {
252 ret = nvkm_fannil_create(therm);
253 if (ret)
254 return ret;
257 nv_info(therm, "FAN control: %s\n", priv->fan->type);
259 /* read the current speed, it is useful when resuming */
260 priv->fan->percent = nvkm_therm_fan_get(therm);
262 /* attempt to detect a tachometer connection */
263 ret = gpio->find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff, &priv->fan->tach);
264 if (ret)
265 priv->fan->tach.func = DCB_GPIO_UNUSED;
267 /* initialise fan bump/slow update handling */
268 priv->fan->parent = therm;
269 nvkm_alarm_init(&priv->fan->alarm, nvkm_fan_alarm);
270 spin_lock_init(&priv->fan->lock);
272 /* other random init... */
273 nvkm_therm_fan_set_defaults(therm);
274 nvbios_perf_fan_parse(bios, &priv->fan->perf);
275 if (!nvbios_fan_parse(bios, &priv->fan->bios)) {
276 nv_debug(therm, "parsing the fan table failed\n");
277 if (nvbios_therm_fan_parse(bios, &priv->fan->bios))
278 nv_error(therm, "parsing both fan tables failed\n");
280 nvkm_therm_fan_safety_checks(therm);
281 return 0;