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 #include "priv.h"
25 
26 #include <subdev/gpio.h>
27 #include <subdev/timer.h>
28 
29 struct nvkm_fantog {
30 	struct nvkm_fan base;
31 	struct nvkm_alarm alarm;
32 	spinlock_t lock;
33 	u32 period_us;
34 	u32 percent;
35 	struct dcb_gpio_func func;
36 };
37 
38 static void
39 nvkm_fantog_update(struct nvkm_fantog *fan, int percent)
40 {
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);
44 	unsigned long flags;
45 	int duty;
46 
47 	spin_lock_irqsave(&fan->lock, flags);
48 	if (percent < 0)
49 		percent = fan->percent;
50 	fan->percent = percent;
51 
52 	duty = !gpio->get(gpio, 0, DCB_GPIO_FAN, 0xff);
53 	gpio->set(gpio, 0, DCB_GPIO_FAN, 0xff, duty);
54 
55 	if (list_empty(&fan->alarm.head) && percent != (duty * 100)) {
56 		u64 next_change = (percent * fan->period_us) / 100;
57 		if (!duty)
58 			next_change = fan->period_us - next_change;
59 		tmr->alarm(tmr, next_change * 1000, &fan->alarm);
60 	}
61 	spin_unlock_irqrestore(&fan->lock, flags);
62 }
63 
64 static void
65 nvkm_fantog_alarm(struct nvkm_alarm *alarm)
66 {
67 	struct nvkm_fantog *fan =
68 	       container_of(alarm, struct nvkm_fantog, alarm);
69 	nvkm_fantog_update(fan, -1);
70 }
71 
72 static int
73 nvkm_fantog_get(struct nvkm_therm *obj)
74 {
75 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
76 	struct nvkm_fantog *fan = (void *)therm->fan;
77 	return fan->percent;
78 }
79 
80 static int
81 nvkm_fantog_set(struct nvkm_therm *obj, int percent)
82 {
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);
88 	return 0;
89 }
90 
91 int
92 nvkm_fantog_create(struct nvkm_therm *obj, struct dcb_gpio_func *func)
93 {
94 	struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base);
95 	struct nvkm_fantog *fan;
96 	int ret;
97 
98 	if (therm->base.pwm_ctrl) {
99 		ret = therm->base.pwm_ctrl(&therm->base, func->line, false);
100 		if (ret)
101 			return ret;
102 	}
103 
104 	fan = kzalloc(sizeof(*fan), GFP_KERNEL);
105 	therm->fan = &fan->base;
106 	if (!fan)
107 		return -ENOMEM;
108 
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 */
114 	fan->percent = 100;
115 	fan->func = *func;
116 	spin_lock_init(&fan->lock);
117 	return 0;
118 }
119