2 * Copyright 2012 The Nouveau community
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: Martin Peres
26 #include <subdev/gpio.h>
27 #include <subdev/timer.h>
31 struct nvkm_alarm alarm;
35 struct dcb_gpio_func func;
39 nvkm_fantog_update(struct nvkm_fantog *fan, int percent)
41 struct nvkm_therm_priv *therm = (void *)fan->base.parent;
42 struct nvkm_timer *tmr = nvkm_timer(therm);
43 struct nvkm_gpio *gpio = nvkm_gpio(therm);
47 spin_lock_irqsave(&fan->lock, flags);
49 percent = fan->percent;
50 fan->percent = percent;
52 duty = !gpio->get(gpio, 0, DCB_GPIO_FAN, 0xff);
53 gpio->set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
55 if (list_empty(&fan->alarm.head) && percent != (duty * 100)) {
56 u64 next_change = (percent * fan->period_us) / 100;
58 next_change = fan->period_us - next_change;
59 tmr->alarm(tmr, next_change * 1000, &fan->alarm);
61 spin_unlock_irqrestore(&fan->lock, flags);
65 nvkm_fantog_alarm(struct nvkm_alarm *alarm)
67 struct nvkm_fantog *fan =
68 container_of(alarm, struct nvkm_fantog, alarm);
69 nvkm_fantog_update(fan, -1);
73 nvkm_fantog_get(struct nvkm_therm *obj)
75 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
76 struct nvkm_fantog *fan = (void *)therm->fan;
81 nvkm_fantog_set(struct nvkm_therm *obj, int percent)
83 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
84 struct nvkm_fantog *fan = (void *)therm->fan;
85 if (therm->base.pwm_ctrl)
86 therm->base.pwm_ctrl(&therm->base, fan->func.line, false);
87 nvkm_fantog_update(fan, percent);
92 nvkm_fantog_create(struct nvkm_therm *obj, struct dcb_gpio_func *func)
94 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
95 struct nvkm_fantog *fan;
98 if (therm->base.pwm_ctrl) {
99 ret = therm->base.pwm_ctrl(&therm->base, func->line, false);
104 fan = kzalloc(sizeof(*fan), GFP_KERNEL);
105 therm->fan = &fan->base;
109 fan->base.type = "toggle";
110 fan->base.get = nvkm_fantog_get;
111 fan->base.set = nvkm_fantog_set;
112 nvkm_alarm_init(&fan->alarm, nvkm_fantog_alarm);
113 fan->period_us = 100000; /* 10Hz */
116 spin_lock_init(&fan->lock);