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 for (i = 0; i < info.vidmask + 1; i++) {
124 if (info.base >= info.min &&
125 info.base <= info.max) {
126 volt->vid[volt->vid_nr].uv = info.base;
127 volt->vid[volt->vid_nr].vid = i;
130 info.base += info.step;
132 volt->vid_mask = info.vidmask;
133 } else if (data && info.vidmask) {
134 for (i = 0; i < cnt; i++) {
135 data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
138 volt->vid[volt->vid_nr].uv = ivid.voltage;
139 volt->vid[volt->vid_nr].vid = ivid.vid;
143 volt->vid_mask = info.vidmask;
148 nvkm_volt_init(struct nvkm_subdev *subdev)
150 struct nvkm_volt *volt = nvkm_volt(subdev);
151 int ret = nvkm_volt_get(volt);
154 nvkm_debug(subdev, "current voltage unknown\n");
157 nvkm_debug(subdev, "current voltage: %duv\n", ret);
162 nvkm_volt_dtor(struct nvkm_subdev *subdev)
164 return nvkm_volt(subdev);
167 static const struct nvkm_subdev_func
169 .dtor = nvkm_volt_dtor,
170 .init = nvkm_volt_init,
174 nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
175 int index, struct nvkm_volt *volt)
177 struct nvkm_bios *bios = device->bios;
180 nvkm_subdev_ctor(&nvkm_volt, device, index, &volt->subdev);
183 /* Assuming the non-bios device should build the voltage table later */
185 nvkm_volt_parse_bios(bios, volt);
188 for (i = 0; i < volt->vid_nr; i++) {
189 nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
190 volt->vid[i].vid, volt->vid[i].uv);
196 nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
197 int index, struct nvkm_volt **pvolt)
199 if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
201 nvkm_volt_ctor(func, device, index, *pvolt);