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 #include "priv.h" 26 27 enum nv40_sensor_style { INVALID_STYLE = -1, OLD_STYLE = 0, NEW_STYLE = 1 }; 28 29 static enum nv40_sensor_style 30 nv40_sensor_style(struct nvkm_therm *therm) 31 { 32 struct nvkm_device *device = nv_device(therm); 33 34 switch (device->chipset) { 35 case 0x43: 36 case 0x44: 37 case 0x4a: 38 case 0x47: 39 return OLD_STYLE; 40 41 case 0x46: 42 case 0x49: 43 case 0x4b: 44 case 0x4e: 45 case 0x4c: 46 case 0x67: 47 case 0x68: 48 case 0x63: 49 return NEW_STYLE; 50 default: 51 return INVALID_STYLE; 52 } 53 } 54 55 static int 56 nv40_sensor_setup(struct nvkm_therm *therm) 57 { 58 struct nvkm_device *device = therm->subdev.device; 59 enum nv40_sensor_style style = nv40_sensor_style(therm); 60 61 /* enable ADC readout and disable the ALARM threshold */ 62 if (style == NEW_STYLE) { 63 nvkm_mask(device, 0x15b8, 0x80000000, 0); 64 nvkm_wr32(device, 0x15b0, 0x80003fff); 65 mdelay(20); /* wait for the temperature to stabilize */ 66 return nvkm_rd32(device, 0x15b4) & 0x3fff; 67 } else if (style == OLD_STYLE) { 68 nvkm_wr32(device, 0x15b0, 0xff); 69 mdelay(20); /* wait for the temperature to stabilize */ 70 return nvkm_rd32(device, 0x15b4) & 0xff; 71 } else 72 return -ENODEV; 73 } 74 75 static int 76 nv40_temp_get(struct nvkm_therm *obj) 77 { 78 struct nvkm_therm_priv *therm = container_of(obj, typeof(*therm), base); 79 struct nvkm_device *device = therm->base.subdev.device; 80 struct nvbios_therm_sensor *sensor = &therm->bios_sensor; 81 enum nv40_sensor_style style = nv40_sensor_style(&therm->base); 82 int core_temp; 83 84 if (style == NEW_STYLE) { 85 nvkm_wr32(device, 0x15b0, 0x80003fff); 86 core_temp = nvkm_rd32(device, 0x15b4) & 0x3fff; 87 } else if (style == OLD_STYLE) { 88 nvkm_wr32(device, 0x15b0, 0xff); 89 core_temp = nvkm_rd32(device, 0x15b4) & 0xff; 90 } else 91 return -ENODEV; 92 93 /* if the slope or the offset is unset, do no use the sensor */ 94 if (!sensor->slope_div || !sensor->slope_mult || 95 !sensor->offset_num || !sensor->offset_den) 96 return -ENODEV; 97 98 core_temp = core_temp * sensor->slope_mult / sensor->slope_div; 99 core_temp = core_temp + sensor->offset_num / sensor->offset_den; 100 core_temp = core_temp + sensor->offset_constant - 8; 101 102 /* reserve negative temperatures for errors */ 103 if (core_temp < 0) 104 core_temp = 0; 105 106 return core_temp; 107 } 108 109 static int 110 nv40_fan_pwm_ctrl(struct nvkm_therm *therm, int line, bool enable) 111 { 112 struct nvkm_subdev *subdev = &therm->subdev; 113 struct nvkm_device *device = subdev->device; 114 u32 mask = enable ? 0x80000000 : 0x0000000; 115 if (line == 2) nvkm_mask(device, 0x0010f0, 0x80000000, mask); 116 else if (line == 9) nvkm_mask(device, 0x0015f4, 0x80000000, mask); 117 else { 118 nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line); 119 return -ENODEV; 120 } 121 return 0; 122 } 123 124 static int 125 nv40_fan_pwm_get(struct nvkm_therm *therm, int line, u32 *divs, u32 *duty) 126 { 127 struct nvkm_subdev *subdev = &therm->subdev; 128 struct nvkm_device *device = subdev->device; 129 if (line == 2) { 130 u32 reg = nvkm_rd32(device, 0x0010f0); 131 if (reg & 0x80000000) { 132 *duty = (reg & 0x7fff0000) >> 16; 133 *divs = (reg & 0x00007fff); 134 return 0; 135 } 136 } else 137 if (line == 9) { 138 u32 reg = nvkm_rd32(device, 0x0015f4); 139 if (reg & 0x80000000) { 140 *divs = nvkm_rd32(device, 0x0015f8); 141 *duty = (reg & 0x7fffffff); 142 return 0; 143 } 144 } else { 145 nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line); 146 return -ENODEV; 147 } 148 149 return -EINVAL; 150 } 151 152 static int 153 nv40_fan_pwm_set(struct nvkm_therm *therm, int line, u32 divs, u32 duty) 154 { 155 struct nvkm_subdev *subdev = &therm->subdev; 156 struct nvkm_device *device = subdev->device; 157 if (line == 2) { 158 nvkm_mask(device, 0x0010f0, 0x7fff7fff, (duty << 16) | divs); 159 } else 160 if (line == 9) { 161 nvkm_wr32(device, 0x0015f8, divs); 162 nvkm_mask(device, 0x0015f4, 0x7fffffff, duty); 163 } else { 164 nvkm_error(subdev, "unknown pwm ctrl for gpio %d\n", line); 165 return -ENODEV; 166 } 167 168 return 0; 169 } 170 171 void 172 nv40_therm_intr(struct nvkm_subdev *subdev) 173 { 174 struct nvkm_therm *therm = nvkm_therm(subdev); 175 struct nvkm_device *device = therm->subdev.device; 176 uint32_t stat = nvkm_rd32(device, 0x1100); 177 178 /* traitement */ 179 180 /* ack all IRQs */ 181 nvkm_wr32(device, 0x1100, 0x70000); 182 183 nvkm_error(subdev, "THERM received an IRQ: stat = %x\n", stat); 184 } 185 186 static int 187 nv40_therm_ctor(struct nvkm_object *parent, 188 struct nvkm_object *engine, 189 struct nvkm_oclass *oclass, void *data, u32 size, 190 struct nvkm_object **pobject) 191 { 192 struct nvkm_therm_priv *therm; 193 int ret; 194 195 ret = nvkm_therm_create(parent, engine, oclass, &therm); 196 *pobject = nv_object(therm); 197 if (ret) 198 return ret; 199 200 therm->base.pwm_ctrl = nv40_fan_pwm_ctrl; 201 therm->base.pwm_get = nv40_fan_pwm_get; 202 therm->base.pwm_set = nv40_fan_pwm_set; 203 therm->base.temp_get = nv40_temp_get; 204 therm->sensor.program_alarms = nvkm_therm_program_alarms_polling; 205 nv_subdev(therm)->intr = nv40_therm_intr; 206 return nvkm_therm_preinit(&therm->base); 207 } 208 209 static int 210 nv40_therm_init(struct nvkm_object *object) 211 { 212 struct nvkm_therm *therm = (void *)object; 213 214 nv40_sensor_setup(therm); 215 216 return _nvkm_therm_init(object); 217 } 218 219 struct nvkm_oclass 220 nv40_therm_oclass = { 221 .handle = NV_SUBDEV(THERM, 0x40), 222 .ofuncs = &(struct nvkm_ofuncs) { 223 .ctor = nv40_therm_ctor, 224 .dtor = _nvkm_therm_dtor, 225 .init = nv40_therm_init, 226 .fini = _nvkm_therm_fini, 227 }, 228 }; 229