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