drm/nouveau/subdev: rename some functions to avoid upcoming conflicts
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / volt / base.c
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 <subdev/volt.h>
25 #include <subdev/bios.h>
26 #include <subdev/bios/vmap.h>
27 #include <subdev/bios/volt.h>
28
29 static int
30 nvkm_volt_get(struct nvkm_volt *volt)
31 {
32         if (volt->vid_get) {
33                 int ret = volt->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         return -ENODEV;
44 }
45
46 static int
47 nvkm_volt_set(struct nvkm_volt *volt, u32 uv)
48 {
49         struct nvkm_subdev *subdev = &volt->subdev;
50         if (volt->vid_set) {
51                 int i, ret = -EINVAL;
52                 for (i = 0; i < volt->vid_nr; i++) {
53                         if (volt->vid[i].uv == uv) {
54                                 ret = volt->vid_set(volt, volt->vid[i].vid);
55                                 nvkm_debug(subdev, "set %duv: %d\n", uv, ret);
56                                 break;
57                         }
58                 }
59                 return ret;
60         }
61         return -ENODEV;
62 }
63
64 static int
65 nvkm_volt_map(struct nvkm_volt *volt, u8 id)
66 {
67         struct nvkm_bios *bios = nvkm_bios(volt);
68         struct nvbios_vmap_entry info;
69         u8  ver, len;
70         u16 vmap;
71
72         vmap = nvbios_vmap_entry_parse(bios, id, &ver, &len, &info);
73         if (vmap) {
74                 if (info.link != 0xff) {
75                         int ret = nvkm_volt_map(volt, info.link);
76                         if (ret < 0)
77                                 return ret;
78                         info.min += ret;
79                 }
80                 return info.min;
81         }
82
83         return id ? id * 10000 : -ENODEV;
84 }
85
86 static int
87 nvkm_volt_set_id(struct nvkm_volt *volt, u8 id, int condition)
88 {
89         int 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 int
139 _nvkm_volt_init(struct nvkm_object *object)
140 {
141         struct nvkm_volt *volt = (void *)object;
142         struct nvkm_subdev *subdev = &volt->subdev;
143         int ret;
144
145         ret = nvkm_subdev_init_old(&volt->subdev);
146         if (ret)
147                 return ret;
148
149         ret = volt->get(volt);
150         if (ret < 0) {
151                 if (ret != -ENODEV)
152                         nvkm_debug(subdev, "current voltage unknown\n");
153                 return 0;
154         }
155
156         nvkm_debug(subdev, "current voltage: %duv\n", ret);
157         return 0;
158 }
159
160 void
161 _nvkm_volt_dtor(struct nvkm_object *object)
162 {
163         struct nvkm_volt *volt = (void *)object;
164         nvkm_subdev_destroy(&volt->subdev);
165 }
166
167 int
168 nvkm_volt_create_(struct nvkm_object *parent, struct nvkm_object *engine,
169                   struct nvkm_oclass *oclass, int length, void **pobject)
170 {
171         struct nvkm_device *device = (void *)parent;
172         struct nvkm_bios *bios = device->bios;
173         struct nvkm_volt *volt;
174         int ret, i;
175
176         ret = nvkm_subdev_create_(parent, engine, oclass, 0, "VOLT",
177                                   "voltage", length, pobject);
178         volt = *pobject;
179         if (ret)
180                 return ret;
181
182         volt->get = nvkm_volt_get;
183         volt->set = nvkm_volt_set;
184         volt->set_id = nvkm_volt_set_id;
185
186         /* Assuming the non-bios device should build the voltage table later */
187         if (bios)
188                 nvkm_volt_parse_bios(bios, volt);
189
190         if (volt->vid_nr) {
191                 for (i = 0; i < volt->vid_nr; i++) {
192                         nvkm_debug(&volt->subdev, "VID %02x: %duv\n",
193                                    volt->vid[i].vid, volt->vid[i].uv);
194                 }
195
196                 /*XXX: this is an assumption.. there probably exists boards
197                  * out there with i2c-connected voltage controllers too..
198                  */
199                 ret = nvkm_voltgpio_init(volt);
200                 if (ret == 0) {
201                         volt->vid_get = nvkm_voltgpio_get;
202                         volt->vid_set = nvkm_voltgpio_set;
203                 }
204         }
205
206         return ret;
207 }