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 = volt->func->vid_get(volt), i;
34 	if (ret >= 0) {
35 		for (i = 0; i < volt->vid_nr; i++) {
36 			if (volt->vid[i].vid == ret)
37 				return volt->vid[i].uv;
38 		}
39 		ret = -EINVAL;
40 	}
41 	return ret;
42 }
43 
44 static int
45 nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
46 {
47 	struct nvkm_subdev *subdev = &volt->subdev;
48 	int i, ret = -EINVAL;
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);
53 			break;
54 		}
55 	}
56 	return ret;
57 }
58 
59 static int
60 nvkm_volt_map(struct nvkm_volt *volt, u8 id)
61 {
62 	struct nvkm_bios *bios = volt->subdev.device->bios;
63 	struct nvbios_vmap_entry info;
64 	u8  ver, len;
65 	u16 vmap;
66 
67 	vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
68 	if (vmap) {
69 		if (info.link != 0xff) {
70 			int ret = nvkm_volt_map(volt, info.link);
71 			if (ret < 0)
72 				return ret;
73 			info.min += ret;
74 		}
75 		return info.min;
76 	}
77 
78 	return id ? id * 10000 : -ENODEV;
79 }
80 
81 int
82 nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
83 {
84 	int ret;
85 
86 	if (volt->func->set_id)
87 		return volt->func->set_id(volt, id, condition);
88 
89 	ret = nvkm_volt_map(volt, id);
90 	if (ret >= 0) {
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);
96 		} else {
97 			ret = 0;
98 		}
99 	}
100 	return ret;
101 }
102 
103 static void
104 nvkm_volt_parse_bios(struct nvkm_bios *bios, struct nvkm_volt *volt)
105 {
106 	struct nvbios_volt_entry ivid;
107 	struct nvbios_volt info;
108 	u8  ver, hdr, cnt, len;
109 	u16 data;
110 	int i;
111 
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;
119 				volt->vid_nr++;
120 			}
121 			info.base += info.step;
122 		}
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,
127 						       &ivid);
128 			if (data) {
129 				volt->vid[volt->vid_nr].uv = ivid.voltage;
130 				volt->vid[volt->vid_nr].vid = ivid.vid;
131 				volt->vid_nr++;
132 			}
133 		}
134 		volt->vid_mask = info.vidmask;
135 	}
136 }
137 
138 static int
139 nvkm_volt_init(struct nvkm_subdev *subdev)
140 {
141 	struct nvkm_volt *volt = nvkm_volt(subdev);
142 	int ret = nvkm_volt_get(volt);
143 	if (ret < 0) {
144 		if (ret != -ENODEV)
145 			nvkm_debug(subdev, "current voltage unknown\n");
146 		return 0;
147 	}
148 	nvkm_debug(subdev, "current voltage: %duv\n", ret);
149 	return 0;
150 }
151 
152 static void *
153 nvkm_volt_dtor(struct nvkm_subdev *subdev)
154 {
155 	return nvkm_volt(subdev);
156 }
157 
158 static const struct nvkm_subdev_func
159 nvkm_volt = {
160 	.dtor = nvkm_volt_dtor,
161 	.init = nvkm_volt_init,
162 };
163 
164 void
165 nvkm_volt_ctor(const struct nvkm_volt_func *func, struct nvkm_device *device,
166 	       int index, struct nvkm_volt *volt)
167 {
168 	struct nvkm_bios *bios = device->bios;
169 	int i;
170 
171 	nvkm_subdev_ctor(&nvkm_volt, device, index, 0, &volt->subdev);
172 	volt->func = func;
173 
174 	/* Assuming the non-bios device should build the voltage table later */
175 	if (bios)
176 		nvkm_volt_parse_bios(bios, volt);
177 
178 	if (volt->vid_nr) {
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);
182 		}
183 	}
184 }
185 
186 int
187 nvkm_volt_new_(const struct nvkm_volt_func *func, struct nvkm_device *device,
188 	       int index, struct nvkm_volt **pvolt)
189 {
190 	if (!(*pvolt = kzalloc(sizeof(**pvolt), GFP_KERNEL)))
191 		return -ENOMEM;
192 	nvkm_volt_ctor(func, device, index, *pvolt);
193 	return 0;
194 }
195