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.
27 #include <subdev/bios/fan.h>
28 #include <subdev/gpio.h>
29 #include <subdev/timer.h>
32 nvkm_fan_update(struct nvkm_fan *fan, bool immediate, int target)
34 struct nvkm_therm_priv *therm = (void *)fan->parent;
35 struct nvkm_subdev *subdev = &therm->base.subdev;
36 struct nvkm_timer *tmr = subdev->device->timer;
41 /* update target fan speed, restricting to allowed range */
42 spin_lock_irqsave(&fan->lock, flags);
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 nvkm_debug(subdev, "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->base);
55 spin_unlock_irqrestore(&fan->lock, flags);
59 /* smooth out the fanspeed increase/decrease */
60 if (!immediate && duty >= 0) {
61 /* the constant "3" is a rough approximation taken from
63 * it is meant to bump the fan speed more incrementally
66 duty = min(duty + 3, target);
67 else if (duty > target)
68 duty = max(duty - 3, target);
73 nvkm_debug(subdev, "FAN update: %d\n", duty);
74 ret = fan->set(&therm->base, duty);
76 spin_unlock_irqrestore(&fan->lock, flags);
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;
92 delay = slow_down_period;
93 else if (duty == target)
94 delay = min(bump_period, slow_down_period) ;
98 tmr->alarm(tmr, delay * 1000 * 1000, &fan->alarm);
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 *obj)
114 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
115 return therm->fan->get(&therm->base);
119 nvkm_therm_fan_set(struct nvkm_therm *obj, bool immediate, int percent)
121 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
122 return nvkm_fan_update(therm->fan, immediate, percent);
126 nvkm_therm_fan_sense(struct nvkm_therm *obj)
128 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
129 struct nvkm_device *device = therm->base.subdev.device;
130 struct nvkm_timer *tmr = device->timer;
131 struct nvkm_gpio *gpio = device->gpio;
132 u32 cycles, cur, prev;
133 u64 start, end, tach;
135 if (therm->fan->tach.func == DCB_GPIO_UNUSED)
138 /* Time a complete rotation and extrapolate to RPM:
139 * When the fan spins, it changes the value of GPIO FAN_SENSE.
140 * We get 4 changes (0 -> 1 -> 0 -> 1) per complete rotation.
142 start = tmr->read(tmr);
143 prev = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
144 therm->fan->tach.line);
147 usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
149 cur = nvkm_gpio_get(gpio, 0, therm->fan->tach.func,
150 therm->fan->tach.line);
153 start = tmr->read(tmr);
157 } while (cycles < 5 && tmr->read(tmr) - start < 250000000);
158 end = tmr->read(tmr);
161 tach = (u64)60000000000ULL;
162 do_div(tach, (end - start));
169 nvkm_therm_fan_user_get(struct nvkm_therm *obj)
171 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
172 return nvkm_therm_fan_get(&therm->base);
176 nvkm_therm_fan_user_set(struct nvkm_therm *obj, int percent)
178 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
180 if (therm->mode != NVKM_THERM_CTRL_MANUAL)
183 return nvkm_therm_fan_set(&therm->base, true, percent);
187 nvkm_therm_fan_set_defaults(struct nvkm_therm *obj)
189 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
191 therm->fan->bios.pwm_freq = 0;
192 therm->fan->bios.min_duty = 0;
193 therm->fan->bios.max_duty = 100;
194 therm->fan->bios.bump_period = 500;
195 therm->fan->bios.slow_down_period = 2000;
196 therm->fan->bios.linear_min_temp = 40;
197 therm->fan->bios.linear_max_temp = 85;
201 nvkm_therm_fan_safety_checks(struct nvkm_therm *obj)
203 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
205 if (therm->fan->bios.min_duty > 100)
206 therm->fan->bios.min_duty = 100;
207 if (therm->fan->bios.max_duty > 100)
208 therm->fan->bios.max_duty = 100;
210 if (therm->fan->bios.min_duty > therm->fan->bios.max_duty)
211 therm->fan->bios.min_duty = therm->fan->bios.max_duty;
215 nvkm_therm_fan_init(struct nvkm_therm *obj)
221 nvkm_therm_fan_fini(struct nvkm_therm *obj, bool suspend)
223 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
224 struct nvkm_timer *tmr = nvkm_timer(therm);
227 tmr->alarm_cancel(tmr, &therm->fan->alarm);
232 nvkm_therm_fan_ctor(struct nvkm_therm *obj)
234 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
235 struct nvkm_subdev *subdev = &therm->base.subdev;
236 struct nvkm_device *device = subdev->device;
237 struct nvkm_gpio *gpio = device->gpio;
238 struct nvkm_bios *bios = device->bios;
239 struct dcb_gpio_func func;
242 /* attempt to locate a drivable fan, and determine control method */
243 ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN, 0xff, &func);
245 /* FIXME: is this really the place to perform such checks ? */
246 if (func.line != 16 && func.log[0] & DCB_GPIO_LOG_DIR_IN) {
247 nvkm_debug(subdev, "GPIO_FAN is in input mode\n");
250 ret = nvkm_fanpwm_create(&therm->base, &func);
252 ret = nvkm_fantog_create(&therm->base, &func);
256 /* no controllable fan found, create a dummy fan module */
258 ret = nvkm_fannil_create(&therm->base);
263 nvkm_debug(subdev, "FAN control: %s\n", therm->fan->type);
265 /* read the current speed, it is useful when resuming */
266 therm->fan->percent = nvkm_therm_fan_get(&therm->base);
268 /* attempt to detect a tachometer connection */
269 ret = nvkm_gpio_find(gpio, 0, DCB_GPIO_FAN_SENSE, 0xff,
272 therm->fan->tach.func = DCB_GPIO_UNUSED;
274 /* initialise fan bump/slow update handling */
275 therm->fan->parent = &therm->base;
276 nvkm_alarm_init(&therm->fan->alarm, nvkm_fan_alarm);
277 spin_lock_init(&therm->fan->lock);
279 /* other random init... */
280 nvkm_therm_fan_set_defaults(&therm->base);
281 nvbios_perf_fan_parse(bios, &therm->fan->perf);
282 if (!nvbios_fan_parse(bios, &therm->fan->bios)) {
283 nvkm_debug(subdev, "parsing the fan table failed\n");
284 if (nvbios_therm_fan_parse(bios, &therm->fan->bios))
285 nvkm_error(subdev, "parsing both fan tables failed\n");
287 nvkm_therm_fan_safety_checks(&therm->base);