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  */
24 #include "priv.h"
25 
26 #include <subdev/gpio.h>
27 
28 int
29 gt215_therm_fan_sense(struct nvkm_therm *therm)
30 {
31 	struct nvkm_device *device = therm->subdev.device;
32 	u32 tach = nvkm_rd32(device, 0x00e728) & 0x0000ffff;
33 	u32 ctrl = nvkm_rd32(device, 0x00e720);
34 	if (ctrl & 0x00000001)
35 		return tach * 60 / 2;
36 	return -ENODEV;
37 }
38 
39 static int
40 gt215_therm_init(struct nvkm_object *object)
41 {
42 	struct nvkm_therm_priv *therm = (void *)object;
43 	struct nvkm_device *device = therm->base.subdev.device;
44 	struct dcb_gpio_func *tach = &therm->fan->tach;
45 	int ret;
46 
47 	ret = nvkm_therm_init(&therm->base);
48 	if (ret)
49 		return ret;
50 
51 	g84_sensor_setup(&therm->base);
52 
53 	/* enable fan tach, count revolutions per-second */
54 	nvkm_mask(device, 0x00e720, 0x00000003, 0x00000002);
55 	if (tach->func != DCB_GPIO_UNUSED) {
56 		nvkm_wr32(device, 0x00e724, nv_device(therm)->crystal * 1000);
57 		nvkm_mask(device, 0x00e720, 0x001f0000, tach->line << 16);
58 		nvkm_mask(device, 0x00e720, 0x00000001, 0x00000001);
59 	}
60 	nvkm_mask(device, 0x00e720, 0x00000002, 0x00000000);
61 
62 	return 0;
63 }
64 
65 static int
66 gt215_therm_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
67 		 struct nvkm_oclass *oclass, void *data, u32 size,
68 		 struct nvkm_object **pobject)
69 {
70 	struct nvkm_therm_priv *therm;
71 	int ret;
72 
73 	ret = nvkm_therm_create(parent, engine, oclass, &therm);
74 	*pobject = nv_object(therm);
75 	if (ret)
76 		return ret;
77 
78 	therm->base.pwm_ctrl = nv50_fan_pwm_ctrl;
79 	therm->base.pwm_get = nv50_fan_pwm_get;
80 	therm->base.pwm_set = nv50_fan_pwm_set;
81 	therm->base.pwm_clock = nv50_fan_pwm_clock;
82 	therm->base.temp_get = g84_temp_get;
83 	therm->base.fan_sense = gt215_therm_fan_sense;
84 	therm->sensor.program_alarms = nvkm_therm_program_alarms_polling;
85 	return nvkm_therm_preinit(&therm->base);
86 }
87 
88 struct nvkm_oclass
89 gt215_therm_oclass = {
90 	.handle = NV_SUBDEV(THERM, 0xa3),
91 	.ofuncs = &(struct nvkm_ofuncs) {
92 		.ctor = gt215_therm_ctor,
93 		.dtor = _nvkm_therm_dtor,
94 		.init = gt215_therm_init,
95 		.fini = g84_therm_fini,
96 	},
97 };
98