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