2012-12-05 16:21:59 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright 2012 Red Hat Inc.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Authors: Ben Skeggs
|
|
|
|
|
* Martin Peres
|
|
|
|
|
*/
|
2015-01-14 15:11:48 +10:00
|
|
|
#include "priv.h"
|
2012-12-05 16:21:59 +10:00
|
|
|
|
2015-01-14 15:12:11 +10:00
|
|
|
#include <core/device.h>
|
2012-12-05 16:21:59 +10:00
|
|
|
#include <core/option.h>
|
2014-08-17 17:33:07 +02:00
|
|
|
#include <subdev/bios.h>
|
|
|
|
|
#include <subdev/bios/fan.h>
|
2015-01-14 15:11:48 +10:00
|
|
|
#include <subdev/gpio.h>
|
2012-12-05 16:21:59 +10:00
|
|
|
|
2015-01-14 15:11:48 +10:00
|
|
|
struct nvkm_fanpwm_priv {
|
|
|
|
|
struct nvkm_fan base;
|
2012-12-05 16:21:59 +10:00
|
|
|
struct dcb_gpio_func func;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int
|
2015-01-14 15:11:48 +10:00
|
|
|
nvkm_fanpwm_get(struct nvkm_therm *therm)
|
2012-12-05 16:21:59 +10:00
|
|
|
{
|
2015-01-14 15:11:48 +10:00
|
|
|
struct nvkm_therm_priv *tpriv = (void *)therm;
|
|
|
|
|
struct nvkm_fanpwm_priv *priv = (void *)tpriv->fan;
|
|
|
|
|
struct nvkm_gpio *gpio = nvkm_gpio(therm);
|
2012-12-05 16:21:59 +10:00
|
|
|
int card_type = nv_device(therm)->card_type;
|
|
|
|
|
u32 divs, duty;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = therm->pwm_get(therm, priv->func.line, &divs, &duty);
|
|
|
|
|
if (ret == 0 && divs) {
|
|
|
|
|
divs = max(divs, duty);
|
|
|
|
|
if (card_type <= NV_40 || (priv->func.log[0] & 1))
|
|
|
|
|
duty = divs - duty;
|
|
|
|
|
return (duty * 100) / divs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gpio->get(gpio, 0, priv->func.func, priv->func.line) * 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2015-01-14 15:11:48 +10:00
|
|
|
nvkm_fanpwm_set(struct nvkm_therm *therm, int percent)
|
2012-12-05 16:21:59 +10:00
|
|
|
{
|
2015-01-14 15:11:48 +10:00
|
|
|
struct nvkm_therm_priv *tpriv = (void *)therm;
|
|
|
|
|
struct nvkm_fanpwm_priv *priv = (void *)tpriv->fan;
|
2012-12-05 16:21:59 +10:00
|
|
|
int card_type = nv_device(therm)->card_type;
|
|
|
|
|
u32 divs, duty;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
divs = priv->base.perf.pwm_divisor;
|
|
|
|
|
if (priv->base.bios.pwm_freq) {
|
|
|
|
|
divs = 1;
|
|
|
|
|
if (therm->pwm_clock)
|
2014-02-18 03:56:11 +01:00
|
|
|
divs = therm->pwm_clock(therm, priv->func.line);
|
2012-12-05 16:21:59 +10:00
|
|
|
divs /= priv->base.bios.pwm_freq;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duty = ((divs * percent) + 99) / 100;
|
|
|
|
|
if (card_type <= NV_40 || (priv->func.log[0] & 1))
|
|
|
|
|
duty = divs - duty;
|
|
|
|
|
|
|
|
|
|
ret = therm->pwm_set(therm, priv->func.line, divs, duty);
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
ret = therm->pwm_ctrl(therm, priv->func.line, true);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2015-01-14 15:11:48 +10:00
|
|
|
nvkm_fanpwm_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
|
2012-12-05 16:21:59 +10:00
|
|
|
{
|
2015-01-14 15:11:48 +10:00
|
|
|
struct nvkm_device *device = nv_device(therm);
|
|
|
|
|
struct nvkm_therm_priv *tpriv = (void *)therm;
|
|
|
|
|
struct nvkm_bios *bios = nvkm_bios(therm);
|
|
|
|
|
struct nvkm_fanpwm_priv *priv;
|
2014-08-17 17:33:07 +02:00
|
|
|
struct nvbios_therm_fan fan;
|
2012-12-05 16:21:59 +10:00
|
|
|
u32 divs, duty;
|
|
|
|
|
|
2014-08-17 17:33:07 +02:00
|
|
|
nvbios_fan_parse(bios, &fan);
|
|
|
|
|
|
2015-01-14 15:11:48 +10:00
|
|
|
if (!nvkm_boolopt(device->cfgopt, "NvFanPWM", func->param) ||
|
2014-08-17 17:33:07 +02:00
|
|
|
!therm->pwm_ctrl || fan.type == NVBIOS_THERM_FAN_TOGGLE ||
|
2012-12-05 16:21:59 +10:00
|
|
|
therm->pwm_get(therm, func->line, &divs, &duty) == -ENODEV)
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
|
|
|
|
tpriv->fan = &priv->base;
|
|
|
|
|
if (!priv)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
priv->base.type = "PWM";
|
2015-01-14 15:11:48 +10:00
|
|
|
priv->base.get = nvkm_fanpwm_get;
|
|
|
|
|
priv->base.set = nvkm_fanpwm_set;
|
2012-12-05 16:21:59 +10:00
|
|
|
priv->func = *func;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|