2 * Copyright 2013 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.
26 #include <subdev/bios.h>
27 #include <subdev/bios/vmap.h>
28 #include <subdev/bios/volt.h>
31 nvkm_volt_get(struct nvkm_volt *volt)
35 if (volt->func->volt_get)
36 return volt->func->volt_get(volt);
38 ret = volt->func->vid_get(volt);
40 for (i = 0; i < volt->vid_nr; i++) {
41 if (volt->vid[i].vid == ret)
42 return volt->vid[i].uv;
50 nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
52 struct nvkm_subdev *subdev = &volt->subdev;
55 if (volt->func->volt_set)
56 return volt->func->volt_set(volt, uv);
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);
69 nvkm_volt_map(struct nvkm_volt *volt, u8 id)
71 struct nvkm_bios *bios = volt->subdev.device->bios;
72 struct nvbios_vmap_entry info;
76 vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
78 if (info.link != 0xff) {
79 int ret = nvkm_volt_map(volt, info.link);
87 return id ? id * 10000 : -ENODEV;
91 nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
95 if (volt->func->set_id)
96 return volt->func->set_id(volt, id, condition);
98 ret = nvkm_volt_map(volt, id);
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);
113 nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
115 struct nvbios_volt_entry ivid;
116 struct nvbios_volt info;
117 u8 ver, hdr, cnt, len;
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;
132 info.base += info.step;
134 volt->vid_mask = info.vidmask;
135 } else if (data && info.vidmask) {
136 volt->min_uv = 0xffffffff;
138 for (i = 0; i < cnt; i++) {
139 data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
142 volt->vid[volt->vid_nr].uv = ivid.voltage;
143 volt->vid[volt->vid_nr].vid = ivid.vid;
145 volt->min_uv = min(volt->min_uv, ivid.voltage);
146 volt->max_uv = max(volt->max_uv, ivid.voltage);
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;
157 nvkm_volt_init(struct nvkm_subdev *subdev)
159 struct nvkm_volt *volt = nvkm_volt(subdev);
160 int ret = nvkm_volt_get(volt);
163 nvkm_debug(subdev, "current voltage unknown\n");
166 nvkm_debug(subdev, "current voltage: %duv\n", ret);
171 nvkm_volt_dtor(struct nvkm_subdev *subdev)
173 return nvkm_volt(subdev);
176 static const struct nvkm_subdev_func
178 .dtor = nvkm_volt_dtor,
179 .init = nvkm_volt_init,
183 nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
184 int index, struct nvkm_volt *volt)
186 struct nvkm_bios *bios = device->bios;
189 nvkm_subdev_ctor(&nvkm_volt, device, index, &volt->subdev);
192 /* Assuming the non-bios device should build the voltage table later */
194 nvkm_volt_parse_bios(bios, volt);
195 nvkm_debug(&volt->subdev, "min: %iuv max: %iuv\n",
196 volt->min_uv, volt->max_uv);
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);
208 nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
209 int index, struct nvkm_volt **pvolt)
211 if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
213 nvkm_volt_ctor(func, device, index, *pvolt);