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>
29 struct nvkm_fantog_priv {
31 struct nvkm_alarm alarm;
35 struct dcb_gpio_func func;
39 nvkm_fantog_update(struct nvkm_fantog_priv *priv, int percent)
41 struct nvkm_therm_priv *tpriv = (void *)priv->base.parent;
42 struct nvkm_timer *ptimer = nvkm_timer(tpriv);
43 struct nvkm_gpio *gpio = nvkm_gpio(tpriv);
47 spin_lock_irqsave(&priv->lock, flags);
49 percent = priv->percent;
50 priv->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(&priv->alarm.head) && percent != (duty * 100)) {
56 u64 next_change = (percent * priv->period_us) / 100;
58 next_change = priv->period_us - next_change;
59 ptimer->alarm(ptimer, next_change * 1000, &priv->alarm);
61 spin_unlock_irqrestore(&priv->lock, flags);
65 nvkm_fantog_alarm(struct nvkm_alarm *alarm)
67 struct nvkm_fantog_priv *priv =
68 container_of(alarm, struct nvkm_fantog_priv, alarm);
69 nvkm_fantog_update(priv, -1);
73 nvkm_fantog_get(struct nvkm_therm *therm)
75 struct nvkm_therm_priv *tpriv = (void *)therm;
76 struct nvkm_fantog_priv *priv = (void *)tpriv->fan;
81 nvkm_fantog_set(struct nvkm_therm *therm, int percent)
83 struct nvkm_therm_priv *tpriv = (void *)therm;
84 struct nvkm_fantog_priv *priv = (void *)tpriv->fan;
86 therm->pwm_ctrl(therm, priv->func.line, false);
87 nvkm_fantog_update(priv, percent);
92 nvkm_fantog_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
94 struct nvkm_therm_priv *tpriv = (void *)therm;
95 struct nvkm_fantog_priv *priv;
98 if (therm->pwm_ctrl) {
99 ret = therm->pwm_ctrl(therm, func->line, false);
104 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
105 tpriv->fan = &priv->base;
109 priv->base.type = "toggle";
110 priv->base.get = nvkm_fantog_get;
111 priv->base.set = nvkm_fantog_set;
112 nvkm_alarm_init(&priv->alarm, nvkm_fantog_alarm);
113 priv->period_us = 100000; /* 10Hz */
116 spin_lock_init(&priv->lock);