xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_fan.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_bios_fan.c,v 1.4 2021/12/18 23:45:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Martin Peres
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: Martin Peres
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_fan.c,v 1.4 2021/12/18 23:45:38 riastradh Exp $");
28 
29 #include <subdev/bios.h>
30 #include <subdev/bios/bit.h>
31 #include <subdev/bios/fan.h>
32 
33 static u32
nvbios_fan_table(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)34 nvbios_fan_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
35 {
36 	struct bit_entry bit_P;
37 	u32 fan = 0;
38 
39 	if (!bit_entry(bios, 'P', &bit_P)) {
40 		if (bit_P.version == 2 && bit_P.length >= 0x5c)
41 			fan = nvbios_rd32(bios, bit_P.offset + 0x58);
42 
43 		if (fan) {
44 			*ver = nvbios_rd08(bios, fan + 0);
45 			switch (*ver) {
46 			case 0x10:
47 				*hdr = nvbios_rd08(bios, fan + 1);
48 				*len = nvbios_rd08(bios, fan + 2);
49 				*cnt = nvbios_rd08(bios, fan + 3);
50 				return fan;
51 			default:
52 				break;
53 			}
54 		}
55 	}
56 
57 	return 0;
58 }
59 
60 static u32
nvbios_fan_entry(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)61 nvbios_fan_entry(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
62 		 u8 *cnt, u8 *len)
63 {
64 	u32 data = nvbios_fan_table(bios, ver, hdr, cnt, len);
65 	if (data && idx < *cnt)
66 		return data + *hdr + (idx * (*len));
67 	return 0;
68 }
69 
70 u32
nvbios_fan_parse(struct nvkm_bios * bios,struct nvbios_therm_fan * fan)71 nvbios_fan_parse(struct nvkm_bios *bios, struct nvbios_therm_fan *fan)
72 {
73 	u8 ver, hdr, cnt, len;
74 
75 	u32 data = nvbios_fan_entry(bios, 0, &ver, &hdr, &cnt, &len);
76 	if (data) {
77 		u8 type = nvbios_rd08(bios, data + 0x00);
78 		switch (type) {
79 		case 0:
80 			fan->type = NVBIOS_THERM_FAN_TOGGLE;
81 			break;
82 		case 1:
83 		case 2:
84 			/* TODO: Understand the difference between the two! */
85 			fan->type = NVBIOS_THERM_FAN_PWM;
86 			break;
87 		default:
88 			fan->type = NVBIOS_THERM_FAN_UNK;
89 		}
90 
91 		fan->fan_mode = NVBIOS_THERM_FAN_LINEAR;
92 		fan->min_duty = nvbios_rd08(bios, data + 0x02);
93 		fan->max_duty = nvbios_rd08(bios, data + 0x03);
94 
95 		fan->pwm_freq = nvbios_rd32(bios, data + 0x0b) & 0xffffff;
96 	}
97 
98 	return data;
99 }
100