1 /*
2 * Copyright 2011 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
25 #include <subdev/bios.h>
26 #include <subdev/bios/gpio.h>
27
28 #include "priv.h"
29
30 static int
nouveau_gpio_drive(struct nouveau_gpio * gpio,int idx,int line,int dir,int out)31 nouveau_gpio_drive(struct nouveau_gpio *gpio,
32 int idx, int line, int dir, int out)
33 {
34 const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
35 return impl->drive ? impl->drive(gpio, line, dir, out) : -ENODEV;
36 }
37
38 static int
nouveau_gpio_sense(struct nouveau_gpio * gpio,int idx,int line)39 nouveau_gpio_sense(struct nouveau_gpio *gpio, int idx, int line)
40 {
41 const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
42 return impl->sense ? impl->sense(gpio, line) : -ENODEV;
43 }
44
45 static int
nouveau_gpio_find(struct nouveau_gpio * gpio,int idx,u8 tag,u8 line,struct dcb_gpio_func * func)46 nouveau_gpio_find(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line,
47 struct dcb_gpio_func *func)
48 {
49 struct nouveau_bios *bios = nouveau_bios(gpio);
50 u8 ver, len;
51 u16 data;
52
53 if (line == 0xff && tag == 0xff)
54 return -EINVAL;
55
56 data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
57 if (data)
58 return 0;
59
60 /* Apple iMac G4 NV18 */
61 if (nv_device_match(nv_object(gpio), 0x0189, 0x10de, 0x0010)) {
62 if (tag == DCB_GPIO_TVDAC0) {
63 *func = (struct dcb_gpio_func) {
64 .func = DCB_GPIO_TVDAC0,
65 .line = 4,
66 .log[0] = 0,
67 .log[1] = 1,
68 };
69 return 0;
70 }
71 }
72
73 return -ENOENT;
74 }
75
76 static int
nouveau_gpio_set(struct nouveau_gpio * gpio,int idx,u8 tag,u8 line,int state)77 nouveau_gpio_set(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line, int state)
78 {
79 struct dcb_gpio_func func;
80 int ret;
81
82 ret = nouveau_gpio_find(gpio, idx, tag, line, &func);
83 if (ret == 0) {
84 int dir = !!(func.log[state] & 0x02);
85 int out = !!(func.log[state] & 0x01);
86 ret = nouveau_gpio_drive(gpio, idx, func.line, dir, out);
87 }
88
89 return ret;
90 }
91
92 static int
nouveau_gpio_get(struct nouveau_gpio * gpio,int idx,u8 tag,u8 line)93 nouveau_gpio_get(struct nouveau_gpio *gpio, int idx, u8 tag, u8 line)
94 {
95 struct dcb_gpio_func func;
96 int ret;
97
98 ret = nouveau_gpio_find(gpio, idx, tag, line, &func);
99 if (ret == 0) {
100 ret = nouveau_gpio_sense(gpio, idx, func.line);
101 if (ret >= 0)
102 ret = (ret == (func.log[1] & 1));
103 }
104
105 return ret;
106 }
107
108 static void
nouveau_gpio_intr_fini(struct nvkm_event * event,int type,int index)109 nouveau_gpio_intr_fini(struct nvkm_event *event, int type, int index)
110 {
111 struct nouveau_gpio *gpio = container_of(event, typeof(*gpio), event);
112 const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
113 impl->intr_mask(gpio, type, 1 << index, 0);
114 }
115
116 static void
nouveau_gpio_intr_init(struct nvkm_event * event,int type,int index)117 nouveau_gpio_intr_init(struct nvkm_event *event, int type, int index)
118 {
119 struct nouveau_gpio *gpio = container_of(event, typeof(*gpio), event);
120 const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
121 impl->intr_mask(gpio, type, 1 << index, 1 << index);
122 }
123
124 static int
nouveau_gpio_intr_ctor(struct nouveau_object * object,void * data,u32 size,struct nvkm_notify * notify)125 nouveau_gpio_intr_ctor(struct nouveau_object *object, void *data, u32 size,
126 struct nvkm_notify *notify)
127 {
128 struct nvkm_gpio_ntfy_req *req = data;
129 if (!WARN_ON(size != sizeof(*req))) {
130 notify->size = sizeof(struct nvkm_gpio_ntfy_rep);
131 notify->types = req->mask;
132 notify->index = req->line;
133 return 0;
134 }
135 return -EINVAL;
136 }
137
138 static void
nouveau_gpio_intr(struct nouveau_subdev * subdev)139 nouveau_gpio_intr(struct nouveau_subdev *subdev)
140 {
141 struct nouveau_gpio *gpio = nouveau_gpio(subdev);
142 const struct nouveau_gpio_impl *impl = (void *)nv_object(gpio)->oclass;
143 u32 hi, lo, i;
144
145 impl->intr_stat(gpio, &hi, &lo);
146
147 for (i = 0; (hi | lo) && i < impl->lines; i++) {
148 struct nvkm_gpio_ntfy_rep rep = {
149 .mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) |
150 (NVKM_GPIO_LO * !!(lo & (1 << i))),
151 };
152 nvkm_event_send(&gpio->event, rep.mask, i, &rep, sizeof(rep));
153 }
154 }
155
156 static const struct nvkm_event_func
157 nouveau_gpio_intr_func = {
158 .ctor = nouveau_gpio_intr_ctor,
159 .init = nouveau_gpio_intr_init,
160 .fini = nouveau_gpio_intr_fini,
161 };
162
163 int
_nouveau_gpio_fini(struct nouveau_object * object,bool suspend)164 _nouveau_gpio_fini(struct nouveau_object *object, bool suspend)
165 {
166 const struct nouveau_gpio_impl *impl = (void *)object->oclass;
167 struct nouveau_gpio *gpio = nouveau_gpio(object);
168 u32 mask = (1 << impl->lines) - 1;
169
170 impl->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
171 impl->intr_stat(gpio, &mask, &mask);
172
173 return nouveau_subdev_fini(&gpio->base, suspend);
174 }
175
176 static struct dmi_system_id gpio_reset_ids[] = {
177 {
178 .ident = "Apple Macbook 10,1",
179 .matches = {
180 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
181 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
182 }
183 },
184 { }
185 };
186
187 int
_nouveau_gpio_init(struct nouveau_object * object)188 _nouveau_gpio_init(struct nouveau_object *object)
189 {
190 struct nouveau_gpio *gpio = nouveau_gpio(object);
191 int ret;
192
193 ret = nouveau_subdev_init(&gpio->base);
194 if (ret)
195 return ret;
196
197 if (gpio->reset && dmi_check_system(gpio_reset_ids))
198 gpio->reset(gpio, DCB_GPIO_UNUSED);
199
200 return ret;
201 }
202
203 void
_nouveau_gpio_dtor(struct nouveau_object * object)204 _nouveau_gpio_dtor(struct nouveau_object *object)
205 {
206 struct nouveau_gpio *gpio = (void *)object;
207 nvkm_event_fini(&gpio->event);
208 nouveau_subdev_destroy(&gpio->base);
209 }
210
211 int
nouveau_gpio_create_(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,int length,void ** pobject)212 nouveau_gpio_create_(struct nouveau_object *parent,
213 struct nouveau_object *engine,
214 struct nouveau_oclass *oclass,
215 int length, void **pobject)
216 {
217 const struct nouveau_gpio_impl *impl = (void *)oclass;
218 struct nouveau_gpio *gpio;
219 int ret;
220
221 ret = nouveau_subdev_create_(parent, engine, oclass, 0, "GPIO", "gpio",
222 length, pobject);
223 gpio = *pobject;
224 if (ret)
225 return ret;
226
227 gpio->find = nouveau_gpio_find;
228 gpio->set = nouveau_gpio_set;
229 gpio->get = nouveau_gpio_get;
230 gpio->reset = impl->reset;
231
232 ret = nvkm_event_init(&nouveau_gpio_intr_func, 2, impl->lines,
233 &gpio->event);
234 if (ret)
235 return ret;
236
237 nv_subdev(gpio)->intr = nouveau_gpio_intr;
238 return 0;
239 }
240
241 int
_nouveau_gpio_ctor(struct nouveau_object * parent,struct nouveau_object * engine,struct nouveau_oclass * oclass,void * data,u32 size,struct nouveau_object ** pobject)242 _nouveau_gpio_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
243 struct nouveau_oclass *oclass, void *data, u32 size,
244 struct nouveau_object **pobject)
245 {
246 struct nouveau_gpio *gpio;
247 int ret;
248
249 ret = nouveau_gpio_create(parent, engine, oclass, &gpio);
250 *pobject = nv_object(gpio);
251 if (ret)
252 return ret;
253
254 return 0;
255 }
256