2 * Copyright 2011 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 <core/notify.h>
29 nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out)
31 return gpio->func->drive(gpio, line, dir, out);
35 nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line)
37 return gpio->func->sense(gpio, line);
41 nvkm_gpio_reset(struct nvkm_gpio *gpio, u8 func)
43 if (gpio->func->reset)
44 gpio->func->reset(gpio, func);
48 nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line,
49 struct dcb_gpio_func *func)
51 struct nvkm_device *device = gpio->subdev.device;
52 struct nvkm_bios *bios = device->bios;
56 if (line == 0xff && tag == 0xff)
59 data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
63 /* Apple iMac G4 NV18 */
64 if (device->quirk && device->quirk->tv_gpio) {
65 if (tag == DCB_GPIO_TVDAC0) {
66 *func = (struct dcb_gpio_func) {
67 .func = DCB_GPIO_TVDAC0,
68 .line = device->quirk->tv_gpio,
80 nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state)
82 struct dcb_gpio_func func;
85 ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
87 int dir = !!(func.log[state] & 0x02);
88 int out = !!(func.log[state] & 0x01);
89 ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out);
96 nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line)
98 struct dcb_gpio_func func;
101 ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
103 ret = nvkm_gpio_sense(gpio, idx, func.line);
105 ret = (ret == (func.log[1] & 1));
112 nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index)
114 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
115 gpio->func->intr_mask(gpio, type, 1 << index, 0);
119 nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index)
121 struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
122 gpio->func->intr_mask(gpio, type, 1 << index, 1 << index);
126 nvkm_gpio_intr_ctor(struct nvkm_object *object, void *data, u32 size,
127 struct nvkm_notify *notify)
129 struct nvkm_gpio_ntfy_req *req = data;
130 if (!WARN_ON(size != sizeof(*req))) {
131 notify->size = sizeof(struct nvkm_gpio_ntfy_rep);
132 notify->types = req->mask;
133 notify->index = req->line;
139 static const struct nvkm_event_func
140 nvkm_gpio_intr_func = {
141 .ctor = nvkm_gpio_intr_ctor,
142 .init = nvkm_gpio_intr_init,
143 .fini = nvkm_gpio_intr_fini,
147 nvkm_gpio_intr(struct nvkm_subdev *subdev)
149 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
152 gpio->func->intr_stat(gpio, &hi, &lo);
154 for (i = 0; (hi | lo) && i < gpio->func->lines; i++) {
155 struct nvkm_gpio_ntfy_rep rep = {
156 .mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) |
157 (NVKM_GPIO_LO * !!(lo & (1 << i))),
159 nvkm_event_send(&gpio->event, rep.mask, i, &rep, sizeof(rep));
164 nvkm_gpio_fini(struct nvkm_subdev *subdev, bool suspend)
166 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
167 u32 mask = (1 << gpio->func->lines) - 1;
169 gpio->func->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
170 gpio->func->intr_stat(gpio, &mask, &mask);
174 static struct dmi_system_id gpio_reset_ids[] = {
176 .ident = "Apple Macbook 10,1",
178 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
179 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
186 nvkm_gpio_init(struct nvkm_subdev *subdev)
188 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
189 if (dmi_check_system(gpio_reset_ids))
190 nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
195 nvkm_gpio_dtor(struct nvkm_subdev *subdev)
197 struct nvkm_gpio *gpio = nvkm_gpio(subdev);
198 nvkm_event_fini(&gpio->event);
202 static const struct nvkm_subdev_func
204 .dtor = nvkm_gpio_dtor,
205 .init = nvkm_gpio_init,
206 .fini = nvkm_gpio_fini,
207 .intr = nvkm_gpio_intr,
211 nvkm_gpio_new_(const struct nvkm_gpio_func *func, struct nvkm_device *device,
212 int index, struct nvkm_gpio **pgpio)
214 struct nvkm_gpio *gpio;
216 if (!(gpio = *pgpio = kzalloc(sizeof(*gpio), GFP_KERNEL)))
219 nvkm_subdev_ctor(&nvkm_gpio, device, index, 0, &gpio->subdev);
222 return nvkm_event_init(&nvkm_gpio_intr_func, 2, func->lines,