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 | */ | |
4e7659fc | 24 | #include "priv.h" |
a0b25635 | 25 | |
72251fac | 26 | #include <core/option.h> |
7356859a | 27 | |
e0996aea | 28 | static int |
4e7659fc | 29 | nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out) |
a0b25635 | 30 | { |
2ea7249f | 31 | return gpio->func->drive(gpio, line, dir, out); |
a0b25635 BS |
32 | } |
33 | ||
e0996aea | 34 | static int |
4e7659fc | 35 | nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line) |
a0b25635 | 36 | { |
2ea7249f | 37 | return gpio->func->sense(gpio, line); |
a0b25635 BS |
38 | } |
39 | ||
2ea7249f BS |
40 | void |
41 | nvkm_gpio_reset(struct nvkm_gpio *gpio, u8 func) | |
42 | { | |
43 | if (gpio->func->reset) | |
44 | gpio->func->reset(gpio, func); | |
45 | } | |
46 | ||
47 | int | |
4e7659fc BS |
48 | nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, |
49 | struct dcb_gpio_func *func) | |
a0b25635 | 50 | { |
e7d65181 | 51 | struct nvkm_device *device = gpio->subdev.device; |
46484438 | 52 | struct nvkm_bios *bios = device->bios; |
d2bcea68 BS |
53 | u8 ver, len; |
54 | u16 data; | |
55 | ||
e0996aea | 56 | if (line == 0xff && tag == 0xff) |
a0b25635 BS |
57 | return -EINVAL; |
58 | ||
d2bcea68 BS |
59 | data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func); |
60 | if (data) | |
e0996aea | 61 | return 0; |
a0b25635 BS |
62 | |
63 | /* Apple iMac G4 NV18 */ | |
c7af0ff0 | 64 | if (device->quirk && device->quirk->tv_gpio) { |
e0996aea BS |
65 | if (tag == DCB_GPIO_TVDAC0) { |
66 | *func = (struct dcb_gpio_func) { | |
a0b25635 | 67 | .func = DCB_GPIO_TVDAC0, |
c7af0ff0 | 68 | .line = device->quirk->tv_gpio, |
a0b25635 BS |
69 | .log[0] = 0, |
70 | .log[1] = 1, | |
71 | }; | |
72 | return 0; | |
73 | } | |
74 | } | |
75 | ||
2d976e3d | 76 | return -ENOENT; |
a0b25635 BS |
77 | } |
78 | ||
2ea7249f | 79 | int |
4e7659fc | 80 | nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state) |
a0b25635 | 81 | { |
e0996aea | 82 | struct dcb_gpio_func func; |
a0b25635 BS |
83 | int ret; |
84 | ||
4e7659fc | 85 | ret = nvkm_gpio_find(gpio, idx, tag, line, &func); |
a0b25635 | 86 | if (ret == 0) { |
e0996aea BS |
87 | int dir = !!(func.log[state] & 0x02); |
88 | int out = !!(func.log[state] & 0x01); | |
4e7659fc | 89 | ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out); |
a0b25635 BS |
90 | } |
91 | ||
92 | return ret; | |
93 | } | |
94 | ||
2ea7249f | 95 | int |
4e7659fc | 96 | nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line) |
a0b25635 | 97 | { |
e0996aea | 98 | struct dcb_gpio_func func; |
a0b25635 BS |
99 | int ret; |
100 | ||
4e7659fc | 101 | ret = nvkm_gpio_find(gpio, idx, tag, line, &func); |
a0b25635 | 102 | if (ret == 0) { |
4e7659fc | 103 | ret = nvkm_gpio_sense(gpio, idx, func.line); |
a0b25635 | 104 | if (ret >= 0) |
e0996aea | 105 | ret = (ret == (func.log[1] & 1)); |
a0b25635 BS |
106 | } |
107 | ||
108 | return ret; | |
109 | } | |
110 | ||
5693c0f2 | 111 | static void |
4e7659fc | 112 | nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index) |
5693c0f2 | 113 | { |
4e7659fc | 114 | struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event); |
2ea7249f | 115 | gpio->func->intr_mask(gpio, type, 1 << index, 0); |
5693c0f2 BS |
116 | } |
117 | ||
118 | static void | |
4e7659fc | 119 | nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index) |
5693c0f2 | 120 | { |
4e7659fc | 121 | struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event); |
2ea7249f | 122 | gpio->func->intr_mask(gpio, type, 1 << index, 1 << index); |
5693c0f2 BS |
123 | } |
124 | ||
2ea7249f BS |
125 | static const struct nvkm_event_func |
126 | nvkm_gpio_intr_func = { | |
2ea7249f BS |
127 | .init = nvkm_gpio_intr_init, |
128 | .fini = nvkm_gpio_intr_fini, | |
129 | }; | |
130 | ||
5693c0f2 | 131 | static void |
4e7659fc | 132 | nvkm_gpio_intr(struct nvkm_subdev *subdev) |
5693c0f2 | 133 | { |
4e7659fc | 134 | struct nvkm_gpio *gpio = nvkm_gpio(subdev); |
79ca2770 | 135 | u32 hi, lo, i; |
5693c0f2 | 136 | |
2ea7249f | 137 | gpio->func->intr_stat(gpio, &hi, &lo); |
5693c0f2 | 138 | |
2ea7249f | 139 | for (i = 0; (hi | lo) && i < gpio->func->lines; i++) { |
773eb04d BS |
140 | u32 mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) | |
141 | (NVKM_GPIO_LO * !!(lo & (1 << i))); | |
142 | nvkm_event_send(&gpio->event, mask, i, NULL, 0); | |
5693c0f2 BS |
143 | } |
144 | } | |
145 | ||
2ea7249f BS |
146 | static int |
147 | nvkm_gpio_fini(struct nvkm_subdev *subdev, bool suspend) | |
f4277a0e | 148 | { |
2ea7249f | 149 | struct nvkm_gpio *gpio = nvkm_gpio(subdev); |
99a97a8b | 150 | u32 mask = (1ULL << gpio->func->lines) - 1; |
f4277a0e | 151 | |
2ea7249f BS |
152 | gpio->func->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0); |
153 | gpio->func->intr_stat(gpio, &mask, &mask); | |
154 | return 0; | |
f4277a0e BS |
155 | } |
156 | ||
6faadbbb | 157 | static const struct dmi_system_id gpio_reset_ids[] = { |
f4277a0e BS |
158 | { |
159 | .ident = "Apple Macbook 10,1", | |
160 | .matches = { | |
161 | DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), | |
162 | DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"), | |
163 | } | |
164 | }, | |
165 | { } | |
166 | }; | |
167 | ||
72251fac | 168 | static enum dcb_gpio_func_name power_checks[] = { |
940794b3 | 169 | DCB_GPIO_THERM_EXT_POWER_EVENT, |
3c978f73 | 170 | DCB_GPIO_POWER_ALERT, |
72251fac MM |
171 | DCB_GPIO_EXT_POWER_LOW, |
172 | }; | |
173 | ||
2ea7249f BS |
174 | static int |
175 | nvkm_gpio_init(struct nvkm_subdev *subdev) | |
f4277a0e | 176 | { |
2ea7249f | 177 | struct nvkm_gpio *gpio = nvkm_gpio(subdev); |
72251fac MM |
178 | struct dcb_gpio_func func; |
179 | int ret; | |
180 | int i; | |
181 | ||
2ea7249f BS |
182 | if (dmi_check_system(gpio_reset_ids)) |
183 | nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED); | |
72251fac MM |
184 | |
185 | if (nvkm_boolopt(subdev->device->cfgopt, "NvPowerChecks", true)) { | |
186 | for (i = 0; i < ARRAY_SIZE(power_checks); ++i) { | |
187 | ret = nvkm_gpio_find(gpio, 0, power_checks[i], | |
188 | DCB_GPIO_UNUSED, &func); | |
189 | if (ret) | |
190 | continue; | |
191 | ||
192 | ret = nvkm_gpio_get(gpio, 0, func.func, func.line); | |
193 | if (!ret) | |
194 | continue; | |
195 | ||
196 | nvkm_error(&gpio->subdev, | |
197 | "GPU is missing power, check its power " | |
198 | "cables. Boot with " | |
199 | "nouveau.config=NvPowerChecks=0 to " | |
200 | "disable.\n"); | |
201 | return -EINVAL; | |
202 | } | |
203 | } | |
204 | ||
2ea7249f | 205 | return 0; |
f4277a0e BS |
206 | } |
207 | ||
2ea7249f BS |
208 | static void * |
209 | nvkm_gpio_dtor(struct nvkm_subdev *subdev) | |
a0b25635 | 210 | { |
2ea7249f | 211 | struct nvkm_gpio *gpio = nvkm_gpio(subdev); |
79ca2770 | 212 | nvkm_event_fini(&gpio->event); |
2ea7249f | 213 | return gpio; |
a0b25635 BS |
214 | } |
215 | ||
2ea7249f BS |
216 | static const struct nvkm_subdev_func |
217 | nvkm_gpio = { | |
218 | .dtor = nvkm_gpio_dtor, | |
219 | .init = nvkm_gpio_init, | |
220 | .fini = nvkm_gpio_fini, | |
221 | .intr = nvkm_gpio_intr, | |
222 | }; | |
a0b25635 | 223 | |
a0b25635 | 224 | int |
2ea7249f | 225 | nvkm_gpio_new_(const struct nvkm_gpio_func *func, struct nvkm_device *device, |
01055c01 | 226 | enum nvkm_subdev_type type, int inst, struct nvkm_gpio **pgpio) |
a0b25635 | 227 | { |
4e7659fc | 228 | struct nvkm_gpio *gpio; |
f4277a0e | 229 | |
2ea7249f BS |
230 | if (!(gpio = *pgpio = kzalloc(sizeof(*gpio), GFP_KERNEL))) |
231 | return -ENOMEM; | |
f4277a0e | 232 | |
01055c01 | 233 | nvkm_subdev_ctor(&nvkm_gpio, device, type, inst, &gpio->subdev); |
2ea7249f BS |
234 | gpio->func = func; |
235 | ||
f43e47c0 | 236 | return nvkm_event_init(&nvkm_gpio_intr_func, &gpio->subdev, 2, func->lines, &gpio->event); |
a0b25635 | 237 | } |