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 */
24
25 #include "priv.h"
26
27 #include <core/object.h>
28 #include <core/device.h>
29
30 #include <subdev/gpio.h>
31 #include <subdev/timer.h>
32
33 struct nouveau_fantog_priv {
34 struct nouveau_fan base;
35 struct nouveau_alarm alarm;
36 spinlock_t lock;
37 u32 period_us;
38 u32 percent;
39 struct dcb_gpio_func func;
40 };
41
42 static void
nouveau_fantog_update(struct nouveau_fantog_priv * priv,int percent)43 nouveau_fantog_update(struct nouveau_fantog_priv *priv, int percent)
44 {
45 struct nouveau_therm_priv *tpriv = (void *)priv->base.parent;
46 struct nouveau_timer *ptimer = nouveau_timer(tpriv);
47 struct nouveau_gpio *gpio = nouveau_gpio(tpriv);
48 unsigned long flags;
49 int duty;
50
51 spin_lock_irqsave(&priv->lock, flags);
52 if (percent < 0)
53 percent = priv->percent;
54 priv->percent = percent;
55
56 duty = !gpio->get(gpio, 0, DCB_GPIO_FAN, 0xff);
57 gpio->set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
58
59 if (list_empty(&priv->alarm.head) && percent != (duty * 100)) {
60 u64 next_change = (percent * priv->period_us) / 100;
61 if (!duty)
62 next_change = priv->period_us - next_change;
63 ptimer->alarm(ptimer, next_change * 1000, &priv->alarm);
64 }
65 spin_unlock_irqrestore(&priv->lock, flags);
66 }
67
68 static void
nouveau_fantog_alarm(struct nouveau_alarm * alarm)69 nouveau_fantog_alarm(struct nouveau_alarm *alarm)
70 {
71 struct nouveau_fantog_priv *priv =
72 container_of(alarm, struct nouveau_fantog_priv, alarm);
73 nouveau_fantog_update(priv, -1);
74 }
75
76 static int
nouveau_fantog_get(struct nouveau_therm * therm)77 nouveau_fantog_get(struct nouveau_therm *therm)
78 {
79 struct nouveau_therm_priv *tpriv = (void *)therm;
80 struct nouveau_fantog_priv *priv = (void *)tpriv->fan;
81 return priv->percent;
82 }
83
84 static int
nouveau_fantog_set(struct nouveau_therm * therm,int percent)85 nouveau_fantog_set(struct nouveau_therm *therm, int percent)
86 {
87 struct nouveau_therm_priv *tpriv = (void *)therm;
88 struct nouveau_fantog_priv *priv = (void *)tpriv->fan;
89 if (therm->pwm_ctrl)
90 therm->pwm_ctrl(therm, priv->func.line, false);
91 nouveau_fantog_update(priv, percent);
92 return 0;
93 }
94
95 int
nouveau_fantog_create(struct nouveau_therm * therm,struct dcb_gpio_func * func)96 nouveau_fantog_create(struct nouveau_therm *therm, struct dcb_gpio_func *func)
97 {
98 struct nouveau_therm_priv *tpriv = (void *)therm;
99 struct nouveau_fantog_priv *priv;
100 int ret;
101
102 if (therm->pwm_ctrl) {
103 ret = therm->pwm_ctrl(therm, func->line, false);
104 if (ret)
105 return ret;
106 }
107
108 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
109 tpriv->fan = &priv->base;
110 if (!priv)
111 return -ENOMEM;
112
113 priv->base.type = "toggle";
114 priv->base.get = nouveau_fantog_get;
115 priv->base.set = nouveau_fantog_set;
116 nouveau_alarm_init(&priv->alarm, nouveau_fantog_alarm);
117 priv->period_us = 100000; /* 10Hz */
118 priv->percent = 100;
119 priv->func = *func;
120 spin_lock_init(&priv->lock);
121 return 0;
122 }
123