2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 enum nv40_sensor_style
{ INVALID_STYLE
= -1, OLD_STYLE
= 0, NEW_STYLE
= 1 };
29 static enum nv40_sensor_style
30 nv40_sensor_style(struct nvkm_therm
*therm
)
32 switch (therm
->subdev
.device
->chipset
) {
53 nv40_sensor_setup(struct nvkm_therm
*therm
)
55 struct nvkm_device
*device
= therm
->subdev
.device
;
56 enum nv40_sensor_style style
= nv40_sensor_style(therm
);
58 /* enable ADC readout and disable the ALARM threshold */
59 if (style
== NEW_STYLE
) {
60 nvkm_mask(device
, 0x15b8, 0x80000000, 0);
61 nvkm_wr32(device
, 0x15b0, 0x80003fff);
62 mdelay(20); /* wait for the temperature to stabilize */
63 return nvkm_rd32(device
, 0x15b4) & 0x3fff;
64 } else if (style
== OLD_STYLE
) {
65 nvkm_wr32(device
, 0x15b0, 0xff);
66 mdelay(20); /* wait for the temperature to stabilize */
67 return nvkm_rd32(device
, 0x15b4) & 0xff;
73 nv40_temp_get(struct nvkm_therm
*therm
)
75 struct nvkm_device
*device
= therm
->subdev
.device
;
76 struct nvbios_therm_sensor
*sensor
= &therm
->bios_sensor
;
77 enum nv40_sensor_style style
= nv40_sensor_style(therm
);
80 if (style
== NEW_STYLE
) {
81 nvkm_wr32(device
, 0x15b0, 0x80003fff);
82 core_temp
= nvkm_rd32(device
, 0x15b4) & 0x3fff;
83 } else if (style
== OLD_STYLE
) {
84 nvkm_wr32(device
, 0x15b0, 0xff);
85 core_temp
= nvkm_rd32(device
, 0x15b4) & 0xff;
89 /* if the slope or the offset is unset, do no use the sensor */
90 if (!sensor
->slope_div
|| !sensor
->slope_mult
||
91 !sensor
->offset_num
|| !sensor
->offset_den
)
94 core_temp
= core_temp
* sensor
->slope_mult
/ sensor
->slope_div
;
95 core_temp
= core_temp
+ sensor
->offset_num
/ sensor
->offset_den
;
96 core_temp
= core_temp
+ sensor
->offset_constant
- 8;
98 /* reserve negative temperatures for errors */
106 nv40_fan_pwm_ctrl(struct nvkm_therm
*therm
, int line
, bool enable
)
108 struct nvkm_subdev
*subdev
= &therm
->subdev
;
109 struct nvkm_device
*device
= subdev
->device
;
110 u32 mask
= enable
? 0x80000000 : 0x00000000;
111 if (line
== 2) nvkm_mask(device
, 0x0010f0, 0x80000000, mask
);
112 else if (line
== 9) nvkm_mask(device
, 0x0015f4, 0x80000000, mask
);
114 nvkm_error(subdev
, "unknown pwm ctrl for gpio %d\n", line
);
121 nv40_fan_pwm_get(struct nvkm_therm
*therm
, int line
, u32
*divs
, u32
*duty
)
123 struct nvkm_subdev
*subdev
= &therm
->subdev
;
124 struct nvkm_device
*device
= subdev
->device
;
126 u32 reg
= nvkm_rd32(device
, 0x0010f0);
127 if (reg
& 0x80000000) {
128 *duty
= (reg
& 0x7fff0000) >> 16;
129 *divs
= (reg
& 0x00007fff);
134 u32 reg
= nvkm_rd32(device
, 0x0015f4);
135 if (reg
& 0x80000000) {
136 *divs
= nvkm_rd32(device
, 0x0015f8);
137 *duty
= (reg
& 0x7fffffff);
141 nvkm_error(subdev
, "unknown pwm ctrl for gpio %d\n", line
);
149 nv40_fan_pwm_set(struct nvkm_therm
*therm
, int line
, u32 divs
, u32 duty
)
151 struct nvkm_subdev
*subdev
= &therm
->subdev
;
152 struct nvkm_device
*device
= subdev
->device
;
154 nvkm_mask(device
, 0x0010f0, 0x7fff7fff, (duty
<< 16) | divs
);
157 nvkm_wr32(device
, 0x0015f8, divs
);
158 nvkm_mask(device
, 0x0015f4, 0x7fffffff, duty
);
160 nvkm_error(subdev
, "unknown pwm ctrl for gpio %d\n", line
);
168 nv40_therm_intr(struct nvkm_therm
*therm
)
170 struct nvkm_subdev
*subdev
= &therm
->subdev
;
171 struct nvkm_device
*device
= subdev
->device
;
172 uint32_t stat
= nvkm_rd32(device
, 0x1100);
177 nvkm_wr32(device
, 0x1100, 0x70000);
179 nvkm_error(subdev
, "THERM received an IRQ: stat = %x\n", stat
);
183 nv40_therm_init(struct nvkm_therm
*therm
)
185 nv40_sensor_setup(therm
);
188 static const struct nvkm_therm_func
190 .init
= nv40_therm_init
,
191 .intr
= nv40_therm_intr
,
192 .pwm_ctrl
= nv40_fan_pwm_ctrl
,
193 .pwm_get
= nv40_fan_pwm_get
,
194 .pwm_set
= nv40_fan_pwm_set
,
195 .temp_get
= nv40_temp_get
,
196 .program_alarms
= nvkm_therm_program_alarms_polling
,
200 nv40_therm_new(struct nvkm_device
*device
, int index
,
201 struct nvkm_therm
**ptherm
)
203 return nvkm_therm_new_(&nv40_therm
, device
, index
, ptherm
);