1 /* 2 * Copyright 2013 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/bios.h> 27 #include <subdev/bios/vmap.h> 28 #include <subdev/bios/volt.h> 29 30 int 31 nvkm_volt_get(struct nvkm_volt *volt) 32 { 33 int ret, i; 34 35 if (volt->func->volt_get) 36 return volt->func->volt_get(volt); 37 38 ret = volt->func->vid_get(volt); 39 if (ret >= 0) { 40 for (i = 0; i < volt->vid_nr; i++) { 41 if (volt->vid[i].vid == ret) 42 return volt->vid[i].uv; 43 } 44 ret = -EINVAL; 45 } 46 return ret; 47 } 48 49 static int 50 nvkm_volt_set(struct nvkm_volt *volt, u32 uv) 51 { 52 struct nvkm_subdev *subdev = &volt->subdev; 53 int i, ret = -EINVAL; 54 55 if (volt->func->volt_set) 56 return volt->func->volt_set(volt, uv); 57 58 for (i = 0; i < volt->vid_nr; i++) { 59 if (volt->vid[i].uv == uv) { 60 ret = volt->func->vid_set(volt, volt->vid[i].vid); 61 nvkm_debug(subdev, "set %duv: %d\n", uv, ret); 62 break; 63 } 64 } 65 return ret; 66 } 67 68 static int 69 nvkm_volt_map(struct nvkm_volt *volt, u8 id) 70 { 71 struct nvkm_bios *bios = volt->subdev.device->bios; 72 struct nvbios_vmap_entry info; 73 u8 ver, len; 74 u16 vmap; 75 76 vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info); 77 if (vmap) { 78 if (info.link != 0xff) { 79 int ret = nvkm_volt_map(volt, info.link); 80 if (ret < 0) 81 return ret; 82 info.min += ret; 83 } 84 return info.min; 85 } 86 87 return id ? id * 10000 : -ENODEV; 88 } 89 90 int 91 nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition) 92 { 93 int ret; 94 95 if (volt->func->set_id) 96 return volt->func->set_id(volt, id, condition); 97 98 ret = nvkm_volt_map(volt, id); 99 if (ret >= 0) { 100 int prev = nvkm_volt_get(volt); 101 if (!condition || prev < 0 || 102 (condition < 0 && ret < prev) || 103 (condition > 0 && ret > prev)) { 104 ret = nvkm_volt_set(volt, ret); 105 } else { 106 ret = 0; 107 } 108 } 109 return ret; 110 } 111 112 static void 113 nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt) 114 { 115 struct nvbios_volt_entry ivid; 116 struct nvbios_volt info; 117 u8 ver, hdr, cnt, len; 118 u16 data; 119 int i; 120 121 data = nvbios_volt_parse(bios, &ver, &hdr, &cnt, &len, &info); 122 if (data && info.vidmask && info.base && info.step) { 123 volt->min_uv = info.min; 124 volt->max_uv = info.max; 125 for (i = 0; i < info.vidmask + 1; i++) { 126 if (info.base >= info.min && 127 info.base <= info.max) { 128 volt->vid[volt->vid_nr].uv = info.base; 129 volt->vid[volt->vid_nr].vid = i; 130 volt->vid_nr++; 131 } 132 info.base += info.step; 133 } 134 volt->vid_mask = info.vidmask; 135 } else if (data && info.vidmask) { 136 volt->min_uv = 0xffffffff; 137 volt->max_uv = 0; 138 for (i = 0; i < cnt; i++) { 139 data = nvbios_volt_entry_parse(bios, i, &ver, &hdr, 140 &ivid); 141 if (data) { 142 volt->vid[volt->vid_nr].uv = ivid.voltage; 143 volt->vid[volt->vid_nr].vid = ivid.vid; 144 volt->vid_nr++; 145 volt->min_uv = min(volt->min_uv, ivid.voltage); 146 volt->max_uv = max(volt->max_uv, ivid.voltage); 147 } 148 } 149 volt->vid_mask = info.vidmask; 150 } else if (data && info.type == NVBIOS_VOLT_PWM) { 151 volt->min_uv = info.base; 152 volt->max_uv = info.base + info.pwm_range; 153 } 154 } 155 156 static int 157 nvkm_volt_init(struct nvkm_subdev *subdev) 158 { 159 struct nvkm_volt *volt = nvkm_volt(subdev); 160 int ret = nvkm_volt_get(volt); 161 if (ret < 0) { 162 if (ret != -ENODEV) 163 nvkm_debug(subdev, "current voltage unknown\n"); 164 return 0; 165 } 166 nvkm_debug(subdev, "current voltage: %duv\n", ret); 167 return 0; 168 } 169 170 static void * 171 nvkm_volt_dtor(struct nvkm_subdev *subdev) 172 { 173 return nvkm_volt(subdev); 174 } 175 176 static const struct nvkm_subdev_func 177 nvkm_volt = { 178 .dtor = nvkm_volt_dtor, 179 .init = nvkm_volt_init, 180 }; 181 182 void 183 nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device, 184 int index, struct nvkm_volt *volt) 185 { 186 struct nvkm_bios *bios = device->bios; 187 int i; 188 189 nvkm_subdev_ctor(&nvkm_volt, device, index, &volt->subdev); 190 volt->func = func; 191 192 /* Assuming the non-bios device should build the voltage table later */ 193 if (bios) { 194 nvkm_volt_parse_bios(bios, volt); 195 nvkm_debug(&volt->subdev, "min: %iuv max: %iuv\n", 196 volt->min_uv, volt->max_uv); 197 } 198 199 if (volt->vid_nr) { 200 for (i = 0; i < volt->vid_nr; i++) { 201 nvkm_debug(&volt->subdev, "VID %02x: %duv\n", 202 volt->vid[i].vid, volt->vid[i].uv); 203 } 204 } 205 } 206 207 int 208 nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device, 209 int index, struct nvkm_volt **pvolt) 210 { 211 if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL))) 212 return -ENOMEM; 213 nvkm_volt_ctor(func, device, index, *pvolt); 214 return 0; 215 } 216