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