1/* $NetBSD: nouveau_subdev_therm_fantog.c,v 1.1.1.1 2014/08/06 12:36:32 riastradh Exp $ */
2
3/*
4 * Copyright 2012 The Nouveau community
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Martin Peres
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_therm_fantog.c,v 1.1.1.1 2014/08/06 12:36:32 riastradh Exp $");
29
30#include "priv.h"
31
32#include <core/object.h>
33#include <core/device.h>
34
35#include <subdev/gpio.h>
36#include <subdev/timer.h>
37
38struct nouveau_fantog_priv {
39 struct nouveau_fan base;
40 struct nouveau_alarm alarm;
41 spinlock_t lock;
42 u32 period_us;
43 u32 percent;
44 struct dcb_gpio_func func;
45};
46
47static void
48nouveau_fantog_update(struct nouveau_fantog_priv *priv, int percent)
49{
50 struct nouveau_therm_priv *tpriv = (void *)priv->base.parent;
51 struct nouveau_timer *ptimer = nouveau_timer(tpriv);
52 struct nouveau_gpio *gpio = nouveau_gpio(tpriv);
53 unsigned long flags;
54 int duty;
55
56 spin_lock_irqsave(&priv->lock, flags);
57 if (percent < 0)
58 percent = priv->percent;
59 priv->percent = percent;
60
61 duty = !gpio->get(gpio, 0, DCB_GPIO_FAN, 0xff);
62 gpio->set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
63
64 if (list_empty(&priv->alarm.head) && percent != (duty * 100)) {
65 u64 next_change = (percent * priv->period_us) / 100;
66 if (!duty)
67 next_change = priv->period_us - next_change;
68 ptimer->alarm(ptimer, next_change * 1000, &priv->alarm);
69 }
70 spin_unlock_irqrestore(&priv->lock, flags);
71}
72
73static void
74nouveau_fantog_alarm(struct nouveau_alarm *alarm)
75{
76 struct nouveau_fantog_priv *priv =
77 container_of(alarm, struct nouveau_fantog_priv, alarm);
78 nouveau_fantog_update(priv, -1);
79}
80
81static int
82nouveau_fantog_get(struct nouveau_therm *therm)
83{
84 struct nouveau_therm_priv *tpriv = (void *)therm;
85 struct nouveau_fantog_priv *priv = (void *)tpriv->fan;
86 return priv->percent;
87}
88
89static int
90nouveau_fantog_set(struct nouveau_therm *therm, int percent)
91{
92 struct nouveau_therm_priv *tpriv = (void *)therm;
93 struct nouveau_fantog_priv *priv = (void *)tpriv->fan;
94 if (therm->pwm_ctrl)
95 therm->pwm_ctrl(therm, priv->func.line, false);
96 nouveau_fantog_update(priv, percent);
97 return 0;
98}
99
100int
101nouveau_fantog_create(struct nouveau_therm *therm, struct dcb_gpio_func *func)
102{
103 struct nouveau_therm_priv *tpriv = (void *)therm;
104 struct nouveau_fantog_priv *priv;
105 int ret;
106
107 if (therm->pwm_ctrl) {
108 ret = therm->pwm_ctrl(therm, func->line, false);
109 if (ret)
110 return ret;
111 }
112
113 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
114 tpriv->fan = &priv->base;
115 if (!priv)
116 return -ENOMEM;
117
118 priv->base.type = "toggle";
119 priv->base.get = nouveau_fantog_get;
120 priv->base.set = nouveau_fantog_set;
121 nouveau_alarm_init(&priv->alarm, nouveau_fantog_alarm);
122 priv->period_us = 100000; /* 10Hz */
123 priv->percent = 100;
124 priv->func = *func;
125 spin_lock_init(&priv->lock);
126 return 0;
127}
128