• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23  * 	    Martin Peres
24  */
25 
26 #include <core/option.h>
27 #include <subdev/gpio.h>
28 
29 #include "priv.h"
30 
31 struct nouveau_fanpwm_priv {
32 	struct nouveau_fan base;
33 	struct dcb_gpio_func func;
34 };
35 
36 static int
nouveau_fanpwm_get(struct nouveau_therm * therm)37 nouveau_fanpwm_get(struct nouveau_therm *therm)
38 {
39 	struct nouveau_therm_priv *tpriv = (void *)therm;
40 	struct nouveau_fanpwm_priv *priv = (void *)tpriv->fan;
41 	struct nouveau_gpio *gpio = nouveau_gpio(therm);
42 	int card_type = nv_device(therm)->card_type;
43 	u32 divs, duty;
44 	int ret;
45 
46 	ret = therm->pwm_get(therm, priv->func.line, &divs, &duty);
47 	if (ret == 0 && divs) {
48 		divs = max(divs, duty);
49 		if (card_type <= NV_40 || (priv->func.log[0] & 1))
50 			duty = divs - duty;
51 		return (duty * 100) / divs;
52 	}
53 
54 	return gpio->get(gpio, 0, priv->func.func, priv->func.line) * 100;
55 }
56 
57 static int
nouveau_fanpwm_set(struct nouveau_therm * therm,int percent)58 nouveau_fanpwm_set(struct nouveau_therm *therm, int percent)
59 {
60 	struct nouveau_therm_priv *tpriv = (void *)therm;
61 	struct nouveau_fanpwm_priv *priv = (void *)tpriv->fan;
62 	int card_type = nv_device(therm)->card_type;
63 	u32 divs, duty;
64 	int ret;
65 
66 	divs = priv->base.perf.pwm_divisor;
67 	if (priv->base.bios.pwm_freq) {
68 		divs = 1;
69 		if (therm->pwm_clock)
70 			divs = therm->pwm_clock(therm);
71 		divs /= priv->base.bios.pwm_freq;
72 	}
73 
74 	duty = ((divs * percent) + 99) / 100;
75 	if (card_type <= NV_40 || (priv->func.log[0] & 1))
76 		duty = divs - duty;
77 
78 	ret = therm->pwm_set(therm, priv->func.line, divs, duty);
79 	if (ret == 0)
80 		ret = therm->pwm_ctrl(therm, priv->func.line, true);
81 	return ret;
82 }
83 
84 int
nouveau_fanpwm_create(struct nouveau_therm * therm,struct dcb_gpio_func * func)85 nouveau_fanpwm_create(struct nouveau_therm *therm, struct dcb_gpio_func *func)
86 {
87 	struct nouveau_device *device = nv_device(therm);
88 	struct nouveau_therm_priv *tpriv = (void *)therm;
89 	struct nouveau_fanpwm_priv *priv;
90 	u32 divs, duty;
91 
92 	if (!nouveau_boolopt(device->cfgopt, "NvFanPWM", func->param) ||
93 	    !therm->pwm_ctrl ||
94 	     therm->pwm_get(therm, func->line, &divs, &duty) == -ENODEV)
95 		return -ENODEV;
96 
97 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
98 	tpriv->fan = &priv->base;
99 	if (!priv)
100 		return -ENOMEM;
101 
102 	priv->base.type = "PWM";
103 	priv->base.get = nouveau_fanpwm_get;
104 	priv->base.set = nouveau_fanpwm_set;
105 	priv->func = *func;
106 	return 0;
107 }
108