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#include "priv.h"
26
27#include <core/device.h>
28#include <core/option.h>
29#include <subdev/bios.h>
30#include <subdev/bios/fan.h>
31#include <subdev/gpio.h>
32
33struct nvkm_fanpwm_priv {
34	struct nvkm_fan base;
35	struct dcb_gpio_func func;
36};
37
38static int
39nvkm_fanpwm_get(struct nvkm_therm *therm)
40{
41	struct nvkm_therm_priv *tpriv = (void *)therm;
42	struct nvkm_fanpwm_priv *priv = (void *)tpriv->fan;
43	struct nvkm_gpio *gpio = nvkm_gpio(therm);
44	int card_type = nv_device(therm)->card_type;
45	u32 divs, duty;
46	int ret;
47
48	ret = therm->pwm_get(therm, priv->func.line, &divs, &duty);
49	if (ret == 0 && divs) {
50		divs = max(divs, duty);
51		if (card_type <= NV_40 || (priv->func.log[0] & 1))
52			duty = divs - duty;
53		return (duty * 100) / divs;
54	}
55
56	return gpio->get(gpio, 0, priv->func.func, priv->func.line) * 100;
57}
58
59static int
60nvkm_fanpwm_set(struct nvkm_therm *therm, int percent)
61{
62	struct nvkm_therm_priv *tpriv = (void *)therm;
63	struct nvkm_fanpwm_priv *priv = (void *)tpriv->fan;
64	int card_type = nv_device(therm)->card_type;
65	u32 divs, duty;
66	int ret;
67
68	divs = priv->base.perf.pwm_divisor;
69	if (priv->base.bios.pwm_freq) {
70		divs = 1;
71		if (therm->pwm_clock)
72			divs = therm->pwm_clock(therm, priv->func.line);
73		divs /= priv->base.bios.pwm_freq;
74	}
75
76	duty = ((divs * percent) + 99) / 100;
77	if (card_type <= NV_40 || (priv->func.log[0] & 1))
78		duty = divs - duty;
79
80	ret = therm->pwm_set(therm, priv->func.line, divs, duty);
81	if (ret == 0)
82		ret = therm->pwm_ctrl(therm, priv->func.line, true);
83	return ret;
84}
85
86int
87nvkm_fanpwm_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
88{
89	struct nvkm_device *device = nv_device(therm);
90	struct nvkm_therm_priv *tpriv = (void *)therm;
91	struct nvkm_bios *bios = nvkm_bios(therm);
92	struct nvkm_fanpwm_priv *priv;
93	struct nvbios_therm_fan fan;
94	u32 divs, duty;
95
96	nvbios_fan_parse(bios, &fan);
97
98	if (!nvkm_boolopt(device->cfgopt, "NvFanPWM", func->param) ||
99	    !therm->pwm_ctrl || fan.type == NVBIOS_THERM_FAN_TOGGLE ||
100	     therm->pwm_get(therm, func->line, &divs, &duty) == -ENODEV)
101		return -ENODEV;
102
103	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
104	tpriv->fan = &priv->base;
105	if (!priv)
106		return -ENOMEM;
107
108	priv->base.type = "PWM";
109	priv->base.get = nvkm_fanpwm_get;
110	priv->base.set = nvkm_fanpwm_set;
111	priv->func = *func;
112	return 0;
113}
114