1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
32struct nvkm_fanpwm {
33 struct nvkm_fan base;
34 struct dcb_gpio_func func;
35};
36
37static int
38nvkm_fanpwm_get(struct nvkm_therm *therm)
39{
40 struct nvkm_fanpwm *fan = (void *)therm->fan;
41 struct nvkm_device *device = therm->subdev.device;
42 struct nvkm_gpio *gpio = device->gpio;
43 int card_type = device->card_type;
44 u32 divs, duty;
45 int ret;
46
47 ret = therm->func->pwm_get(therm, 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 nvkm_gpio_get(gpio, 0, fan->func.func, fan->func.line) * 100;
56}
57
58static int
59nvkm_fanpwm_set(struct nvkm_therm *therm, int percent)
60{
61 struct nvkm_fanpwm *fan = (void *)therm->fan;
62 int card_type = therm->subdev.device->card_type;
63 u32 divs, duty;
64 int ret;
65
66 divs = fan->base.perf.pwm_divisor;
67 if (fan->base.bios.pwm_freq) {
68 divs = 1;
69 if (therm->func->pwm_clock)
70 divs = therm->func->pwm_clock(therm, fan->func.line);
71 divs /= fan->base.bios.pwm_freq;
72 }
73
74 duty = ((divs * percent) + 99) / 100;
75 if (card_type <= NV_40 || (fan->func.log[0] & 1))
76 duty = divs - duty;
77
78 ret = therm->func->pwm_set(therm, fan->func.line, divs, duty);
79 if (ret == 0)
80 ret = therm->func->pwm_ctrl(therm, fan->func.line, true);
81 return ret;
82}
83
84int
85nvkm_fanpwm_create(struct nvkm_therm *therm, struct dcb_gpio_func *func)
86{
87 struct nvkm_device *device = therm->subdev.device;
88 struct nvkm_bios *bios = device->bios;
89 struct nvkm_fanpwm *fan;
90 struct nvbios_therm_fan info = {};
91 u32 divs, duty;
92
93 nvbios_fan_parse(bios, &info);
94
95 if (!nvkm_boolopt(device->cfgopt, "NvFanPWM", func->param) ||
96 !therm->func->pwm_ctrl || info.type == NVBIOS_THERM_FAN_TOGGLE ||
97 therm->func->pwm_get(therm, func->line, &divs, &duty) == -ENODEV)
98 return -ENODEV;
99
100 fan = kzalloc(sizeof(*fan), GFP_KERNEL);
101 therm->fan = &fan->base;
102 if (!fan)
103 return -ENOMEM;
104
105 fan->base.type = "PWM";
106 fan->base.get = nvkm_fanpwm_get;
107 fan->base.set = nvkm_fanpwm_set;
108 fan->func = *func;
109 return 0;
110}
111