1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
33struct 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
42static void
43nouveau_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
68static void
69nouveau_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
76static int
77nouveau_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
84static int
85nouveau_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
95int
96nouveau_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
101 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
102 tpriv->fan = &priv->base;
103 if (!priv)
104 return -ENOMEM;
105
106 priv->base.type = "toggle";
107 priv->base.get = nouveau_fantog_get;
108 priv->base.set = nouveau_fantog_set;
109 nouveau_alarm_init(&priv->alarm, nouveau_fantog_alarm);
110 priv->period_us = 100000;
111 priv->percent = 100;
112 priv->func = *func;
113 spin_lock_init(&priv->lock);
114 return 0;
115}
116