]>
Commit | Line | Data |
---|---|---|
0cbf83bb 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 | */ | |
0cbf83bb MP |
24 | #include "priv.h" |
25 | ||
0cbf83bb MP |
26 | #include <subdev/gpio.h> |
27 | #include <subdev/timer.h> | |
28 | ||
da06b46b | 29 | struct nvkm_fantog { |
e1404611 BS |
30 | struct nvkm_fan base; |
31 | struct nvkm_alarm alarm; | |
0cbf83bb MP |
32 | spinlock_t lock; |
33 | u32 period_us; | |
34 | u32 percent; | |
35 | struct dcb_gpio_func func; | |
36 | }; | |
37 | ||
38 | static void | |
da06b46b | 39 | nvkm_fantog_update(struct nvkm_fantog *fan, int percent) |
0cbf83bb | 40 | { |
57113c01 BS |
41 | struct nvkm_therm *therm = fan->base.parent; |
42 | struct nvkm_device *device = therm->subdev.device; | |
2ea7249f BS |
43 | struct nvkm_timer *tmr = device->timer; |
44 | struct nvkm_gpio *gpio = device->gpio; | |
0cbf83bb MP |
45 | unsigned long flags; |
46 | int duty; | |
47 | ||
da06b46b | 48 | spin_lock_irqsave(&fan->lock, flags); |
0cbf83bb | 49 | if (percent < 0) |
da06b46b BS |
50 | percent = fan->percent; |
51 | fan->percent = percent; | |
0cbf83bb | 52 | |
2ea7249f BS |
53 | duty = !nvkm_gpio_get(gpio, 0, DCB_GPIO_FAN, 0xff); |
54 | nvkm_gpio_set(gpio, 0, DCB_GPIO_FAN, 0xff, duty); | |
0cbf83bb | 55 | |
e4311ee5 | 56 | if (percent != (duty * 100)) { |
da06b46b | 57 | u64 next_change = (percent * fan->period_us) / 100; |
0cbf83bb | 58 | if (!duty) |
da06b46b | 59 | next_change = fan->period_us - next_change; |
31649ecf | 60 | nvkm_timer_alarm(tmr, next_change * 1000, &fan->alarm); |
0cbf83bb | 61 | } |
da06b46b | 62 | spin_unlock_irqrestore(&fan->lock, flags); |
0cbf83bb MP |
63 | } |
64 | ||
65 | static void | |
e1404611 | 66 | nvkm_fantog_alarm(struct nvkm_alarm *alarm) |
0cbf83bb | 67 | { |
da06b46b BS |
68 | struct nvkm_fantog *fan = |
69 | container_of(alarm, struct nvkm_fantog, alarm); | |
70 | nvkm_fantog_update(fan, -1); | |
0cbf83bb MP |
71 | } |
72 | ||
73 | static int | |
57113c01 | 74 | nvkm_fantog_get(struct nvkm_therm *therm) |
0cbf83bb | 75 | { |
da06b46b BS |
76 | struct nvkm_fantog *fan = (void *)therm->fan; |
77 | return fan->percent; | |
0cbf83bb MP |
78 | } |
79 | ||
cd897837 | 80 | static int |
57113c01 | 81 | nvkm_fantog_set(struct nvkm_therm *therm, int percent) |
0cbf83bb | 82 | { |
da06b46b | 83 | struct nvkm_fantog *fan = (void *)therm->fan; |
57113c01 BS |
84 | if (therm->func->pwm_ctrl) |
85 | therm->func->pwm_ctrl(therm, fan->func.line, false); | |
da06b46b | 86 | nvkm_fantog_update(fan, percent); |
0cbf83bb MP |
87 | return 0; |
88 | } | |
89 | ||
90 | int | |
57113c01 | 91 | nvkm_fantog_create(struct nvkm_therm *therm, struct dcb_gpio_func *func) |
0cbf83bb | 92 | { |
da06b46b | 93 | struct nvkm_fantog *fan; |
21b1ed10 BS |
94 | int ret; |
95 | ||
57113c01 BS |
96 | if (therm->func->pwm_ctrl) { |
97 | ret = therm->func->pwm_ctrl(therm, func->line, false); | |
21b1ed10 BS |
98 | if (ret) |
99 | return ret; | |
100 | } | |
0cbf83bb | 101 | |
da06b46b BS |
102 | fan = kzalloc(sizeof(*fan), GFP_KERNEL); |
103 | therm->fan = &fan->base; | |
104 | if (!fan) | |
0cbf83bb MP |
105 | return -ENOMEM; |
106 | ||
da06b46b BS |
107 | fan->base.type = "toggle"; |
108 | fan->base.get = nvkm_fantog_get; | |
109 | fan->base.set = nvkm_fantog_set; | |
110 | nvkm_alarm_init(&fan->alarm, nvkm_fantog_alarm); | |
111 | fan->period_us = 100000; /* 10Hz */ | |
112 | fan->percent = 100; | |
113 | fan->func = *func; | |
114 | spin_lock_init(&fan->lock); | |
0cbf83bb MP |
115 | return 0; |
116 | } |