• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include "priv.h"
27 
28 struct nv50_therm_priv {
29 	struct nouveau_therm_priv base;
30 };
31 
32 static int
pwm_info(struct nouveau_therm * therm,int * line,int * ctrl,int * indx)33 pwm_info(struct nouveau_therm *therm, int *line, int *ctrl, int *indx)
34 {
35 	if (*line == 0x04) {
36 		*ctrl = 0x00e100;
37 		*line = 4;
38 		*indx = 0;
39 	} else
40 	if (*line == 0x09) {
41 		*ctrl = 0x00e100;
42 		*line = 9;
43 		*indx = 1;
44 	} else
45 	if (*line == 0x10) {
46 		*ctrl = 0x00e28c;
47 		*line = 0;
48 		*indx = 0;
49 	} else {
50 		nv_error(therm, "unknown pwm ctrl for gpio %d\n", *line);
51 		return -ENODEV;
52 	}
53 
54 	return 0;
55 }
56 
57 int
nv50_fan_pwm_ctrl(struct nouveau_therm * therm,int line,bool enable)58 nv50_fan_pwm_ctrl(struct nouveau_therm *therm, int line, bool enable)
59 {
60 	u32 data = enable ? 0x00000001 : 0x00000000;
61 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
62 	if (ret == 0)
63 		nv_mask(therm, ctrl, 0x00010001 << line, data << line);
64 	return ret;
65 }
66 
67 int
nv50_fan_pwm_get(struct nouveau_therm * therm,int line,u32 * divs,u32 * duty)68 nv50_fan_pwm_get(struct nouveau_therm *therm, int line, u32 *divs, u32 *duty)
69 {
70 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
71 	if (ret)
72 		return ret;
73 
74 	if (nv_rd32(therm, ctrl) & (1 << line)) {
75 		*divs = nv_rd32(therm, 0x00e114 + (id * 8));
76 		*duty = nv_rd32(therm, 0x00e118 + (id * 8));
77 		return 0;
78 	}
79 
80 	return -EINVAL;
81 }
82 
83 int
nv50_fan_pwm_set(struct nouveau_therm * therm,int line,u32 divs,u32 duty)84 nv50_fan_pwm_set(struct nouveau_therm *therm, int line, u32 divs, u32 duty)
85 {
86 	int ctrl, id, ret = pwm_info(therm, &line, &ctrl, &id);
87 	if (ret)
88 		return ret;
89 
90 	nv_wr32(therm, 0x00e114 + (id * 8), divs);
91 	nv_wr32(therm, 0x00e118 + (id * 8), duty | 0x80000000);
92 	return 0;
93 }
94 
95 int
nv50_fan_pwm_clock(struct nouveau_therm * therm,int line)96 nv50_fan_pwm_clock(struct nouveau_therm *therm, int line)
97 {
98 	int chipset = nv_device(therm)->chipset;
99 	int crystal = nv_device(therm)->crystal;
100 	int pwm_clock;
101 
102 	/* determine the PWM source clock */
103 	if (chipset > 0x50 && chipset < 0x94) {
104 		u8 pwm_div = nv_rd32(therm, 0x410c);
105 		if (nv_rd32(therm, 0xc040) & 0x800000) {
106 			/* Use the HOST clock (100 MHz)
107 			* Where does this constant(2.4) comes from? */
108 			pwm_clock = (100000000 >> pwm_div) * 10 / 24;
109 		} else {
110 			/* Where does this constant(20) comes from? */
111 			pwm_clock = (crystal * 1000) >> pwm_div;
112 			pwm_clock /= 20;
113 		}
114 	} else {
115 		pwm_clock = (crystal * 1000) / 20;
116 	}
117 
118 	return pwm_clock;
119 }
120 
121 static void
nv50_sensor_setup(struct nouveau_therm * therm)122 nv50_sensor_setup(struct nouveau_therm *therm)
123 {
124 	nv_mask(therm, 0x20010, 0x40000000, 0x0);
125 	mdelay(20); /* wait for the temperature to stabilize */
126 }
127 
128 static int
nv50_temp_get(struct nouveau_therm * therm)129 nv50_temp_get(struct nouveau_therm *therm)
130 {
131 	struct nouveau_therm_priv *priv = (void *)therm;
132 	struct nvbios_therm_sensor *sensor = &priv->bios_sensor;
133 	int core_temp;
134 
135 	core_temp = nv_rd32(therm, 0x20014) & 0x3fff;
136 
137 	/* if the slope or the offset is unset, do no use the sensor */
138 	if (!sensor->slope_div || !sensor->slope_mult ||
139 	    !sensor->offset_num || !sensor->offset_den)
140 	    return -ENODEV;
141 
142 	core_temp = core_temp * sensor->slope_mult / sensor->slope_div;
143 	core_temp = core_temp + sensor->offset_num / sensor->offset_den;
144 	core_temp = core_temp + sensor->offset_constant - 8;
145 
146 	/* reserve negative temperatures for errors */
147 	if (core_temp < 0)
148 		core_temp = 0;
149 
150 	return core_temp;
151 }
152 
153 static int
nv50_therm_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)154 nv50_therm_ctor(struct nouveau_object *parent,
155 		struct nouveau_object *engine,
156 		struct nouveau_oclass *oclass, void *data, u32 size,
157 		struct nouveau_object **pobject)
158 {
159 	struct nv50_therm_priv *priv;
160 	int ret;
161 
162 	ret = nouveau_therm_create(parent, engine, oclass, &priv);
163 	*pobject = nv_object(priv);
164 	if (ret)
165 		return ret;
166 
167 	priv->base.base.pwm_ctrl = nv50_fan_pwm_ctrl;
168 	priv->base.base.pwm_get = nv50_fan_pwm_get;
169 	priv->base.base.pwm_set = nv50_fan_pwm_set;
170 	priv->base.base.pwm_clock = nv50_fan_pwm_clock;
171 	priv->base.base.temp_get = nv50_temp_get;
172 	priv->base.sensor.program_alarms = nouveau_therm_program_alarms_polling;
173 	nv_subdev(priv)->intr = nv40_therm_intr;
174 
175 	return nouveau_therm_preinit(&priv->base.base);
176 }
177 
178 static int
nv50_therm_init(struct nouveau_object * object)179 nv50_therm_init(struct nouveau_object *object)
180 {
181 	struct nouveau_therm *therm = (void *)object;
182 
183 	nv50_sensor_setup(therm);
184 
185 	return _nouveau_therm_init(object);
186 }
187 
188 struct nouveau_oclass
189 nv50_therm_oclass = {
190 	.handle = NV_SUBDEV(THERM, 0x50),
191 	.ofuncs = &(struct nouveau_ofuncs) {
192 		.ctor = nv50_therm_ctor,
193 		.dtor = _nouveau_therm_dtor,
194 		.init = nv50_therm_init,
195 		.fini = _nouveau_therm_fini,
196 	},
197 };
198