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)
33 int ret = volt->func->vid_get(volt), i;
35 for (i = 0; i < volt->vid_nr; i++) {
36 if (volt->vid[i].vid == ret)
37 return volt->vid[i].uv;
45 nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
47 struct nvkm_subdev *subdev = &volt->subdev;
49 for (i = 0; i < volt->vid_nr; i++) {
50 if (volt->vid[i].uv == uv) {
51 ret = volt->func->vid_set(volt, volt->vid[i].vid);
52 nvkm_debug(subdev, "set %duv: %d\n", uv, ret);
60 nvkm_volt_map(struct nvkm_volt *volt, u8 id)
62 struct nvkm_bios *bios = volt->subdev.device->bios;
63 struct nvbios_vmap_entry info;
67 vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
69 if (info.link != 0xff) {
70 int ret = nvkm_volt_map(volt, info.link);
78 return id ? id * 10000 : -ENODEV;
82 nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
86 if (volt->func->set_id)
87 return volt->func->set_id(volt, id, condition);
89 ret = nvkm_volt_map(volt, id);
91 int prev = nvkm_volt_get(volt);
92 if (!condition || prev < 0 ||
93 (condition < 0 && ret < prev) ||
94 (condition > 0 && ret > prev)) {
95 ret = nvkm_volt_set(volt, ret);
104 nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
106 struct nvbios_volt_entry ivid;
107 struct nvbios_volt info;
108 u8 ver, hdr, cnt, len;
112 data = nvbios_volt_parse(bios, &ver, &hdr, &cnt, &len, &info);
113 if (data && info.vidmask && info.base && info.step) {
114 for (i = 0; i < info.vidmask + 1; i++) {
115 if (info.base >= info.min &&
116 info.base <= info.max) {
117 volt->vid[volt->vid_nr].uv = info.base;
118 volt->vid[volt->vid_nr].vid = i;
121 info.base += info.step;
123 volt->vid_mask = info.vidmask;
124 } else if (data && info.vidmask) {
125 for (i = 0; i < cnt; i++) {
126 data = nvbios_volt_entry_parse(bios, i, &ver, &hdr,
129 volt->vid[volt->vid_nr].uv = ivid.voltage;
130 volt->vid[volt->vid_nr].vid = ivid.vid;
134 volt->vid_mask = info.vidmask;
139 nvkm_volt_init(struct nvkm_subdev *subdev)
141 struct nvkm_volt *volt = nvkm_volt(subdev);
142 int ret = nvkm_volt_get(volt);
145 nvkm_debug(subdev, "current voltage unknown\n");
148 nvkm_debug(subdev, "current voltage: %duv\n", ret);
153 nvkm_volt_dtor(struct nvkm_subdev *subdev)
155 return nvkm_volt(subdev);
158 static const struct nvkm_subdev_func
160 .dtor = nvkm_volt_dtor,
161 .init = nvkm_volt_init,
165 nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
166 int index, struct nvkm_volt *volt)
168 struct nvkm_bios *bios = device->bios;
171 nvkm_subdev_ctor(&nvkm_volt, device, index, 0, &volt->subdev);
174 /* Assuming the non-bios device should build the voltage table later */
176 nvkm_volt_parse_bios(bios, volt);
179 for (i = 0; i < volt->vid_nr; i++) {
180 nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
181 volt->vid[i].vid, volt->vid[i].uv);
187 nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
188 int index, struct nvkm_volt **pvolt)
190 if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
192 nvkm_volt_ctor(func, device, index, *pvolt);