xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/therm/nouveau_nvkm_subdev_therm_gf119.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_therm_gf119.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_therm_gf119.c,v 1.3 2021/12/18 23:45:41 riastradh Exp $");
28 
29 #include "priv.h"
30 
31 static int
pwm_info(struct nvkm_therm * therm,int line)32 pwm_info(struct nvkm_therm *therm, int line)
33 {
34 	struct nvkm_subdev *subdev = &therm->subdev;
35 	struct nvkm_device *device = subdev->device;
36 	u32 gpio = nvkm_rd32(device, 0x00d610 + (line * 0x04));
37 
38 	switch (gpio & 0x000000c0) {
39 	case 0x00000000: /* normal mode, possibly pwm forced off by us */
40 	case 0x00000040: /* nvio special */
41 		switch (gpio & 0x0000001f) {
42 		case 0x00: return 2;
43 		case 0x19: return 1;
44 		case 0x1c: return 0;
45 		case 0x1e: return 2;
46 		default:
47 			break;
48 		}
49 	default:
50 		break;
51 	}
52 
53 	nvkm_error(subdev, "GPIO %d unknown PWM: %08x\n", line, gpio);
54 	return -ENODEV;
55 }
56 
57 int
gf119_fan_pwm_ctrl(struct nvkm_therm * therm,int line,bool enable)58 gf119_fan_pwm_ctrl(struct nvkm_therm *therm, int line, bool enable)
59 {
60 	struct nvkm_device *device = therm->subdev.device;
61 	u32 data = enable ? 0x00000040 : 0x00000000;
62 	int indx = pwm_info(therm, line);
63 	if (indx < 0)
64 		return indx;
65 	else if (indx < 2)
66 		nvkm_mask(device, 0x00d610 + (line * 0x04), 0x000000c0, data);
67 	/* nothing to do for indx == 2, it seems hardwired to PTHERM */
68 	return 0;
69 }
70 
71 int
gf119_fan_pwm_get(struct nvkm_therm * therm,int line,u32 * divs,u32 * duty)72 gf119_fan_pwm_get(struct nvkm_therm *therm, int line, u32 *divs, u32 *duty)
73 {
74 	struct nvkm_device *device = therm->subdev.device;
75 	int indx = pwm_info(therm, line);
76 	if (indx < 0)
77 		return indx;
78 	else if (indx < 2) {
79 		if (nvkm_rd32(device, 0x00d610 + (line * 0x04)) & 0x00000040) {
80 			*divs = nvkm_rd32(device, 0x00e114 + (indx * 8));
81 			*duty = nvkm_rd32(device, 0x00e118 + (indx * 8));
82 			return 0;
83 		}
84 	} else if (indx == 2) {
85 		*divs = nvkm_rd32(device, 0x0200d8) & 0x1fff;
86 		*duty = nvkm_rd32(device, 0x0200dc) & 0x1fff;
87 		return 0;
88 	}
89 
90 	return -EINVAL;
91 }
92 
93 int
gf119_fan_pwm_set(struct nvkm_therm * therm,int line,u32 divs,u32 duty)94 gf119_fan_pwm_set(struct nvkm_therm *therm, int line, u32 divs, u32 duty)
95 {
96 	struct nvkm_device *device = therm->subdev.device;
97 	int indx = pwm_info(therm, line);
98 	if (indx < 0)
99 		return indx;
100 	else if (indx < 2) {
101 		nvkm_wr32(device, 0x00e114 + (indx * 8), divs);
102 		nvkm_wr32(device, 0x00e118 + (indx * 8), duty | 0x80000000);
103 	} else if (indx == 2) {
104 		nvkm_mask(device, 0x0200d8, 0x1fff, divs); /* keep the high bits */
105 		nvkm_wr32(device, 0x0200dc, duty | 0x40000000);
106 	}
107 	return 0;
108 }
109 
110 int
gf119_fan_pwm_clock(struct nvkm_therm * therm,int line)111 gf119_fan_pwm_clock(struct nvkm_therm *therm, int line)
112 {
113 	struct nvkm_device *device = therm->subdev.device;
114 	int indx = pwm_info(therm, line);
115 	if (indx < 0)
116 		return 0;
117 	else if (indx < 2)
118 		return (device->crystal * 1000) / 20;
119 	else
120 		return device->crystal * 1000 / 10;
121 }
122 
123 void
gf119_therm_init(struct nvkm_therm * therm)124 gf119_therm_init(struct nvkm_therm *therm)
125 {
126 	struct nvkm_device *device = therm->subdev.device;
127 
128 	g84_sensor_setup(therm);
129 
130 	/* enable fan tach, count revolutions per-second */
131 	nvkm_mask(device, 0x00e720, 0x00000003, 0x00000002);
132 	if (therm->fan->tach.func != DCB_GPIO_UNUSED) {
133 		nvkm_mask(device, 0x00d79c, 0x000000ff, therm->fan->tach.line);
134 		nvkm_wr32(device, 0x00e724, device->crystal * 1000);
135 		nvkm_mask(device, 0x00e720, 0x00000001, 0x00000001);
136 	}
137 	nvkm_mask(device, 0x00e720, 0x00000002, 0x00000000);
138 }
139 
140 static const struct nvkm_therm_func
141 gf119_therm = {
142 	.init = gf119_therm_init,
143 	.fini = g84_therm_fini,
144 	.pwm_ctrl = gf119_fan_pwm_ctrl,
145 	.pwm_get = gf119_fan_pwm_get,
146 	.pwm_set = gf119_fan_pwm_set,
147 	.pwm_clock = gf119_fan_pwm_clock,
148 	.temp_get = g84_temp_get,
149 	.fan_sense = gt215_therm_fan_sense,
150 	.program_alarms = nvkm_therm_program_alarms_polling,
151 };
152 
153 int
gf119_therm_new(struct nvkm_device * device,int index,struct nvkm_therm ** ptherm)154 gf119_therm_new(struct nvkm_device *device, int index,
155 	       struct nvkm_therm **ptherm)
156 {
157 	return nvkm_therm_new_(&gf119_therm, device, index, ptherm);
158 }
159