1c39f472eSBen Skeggs /*
2c39f472eSBen Skeggs * Copyright 2011 Red Hat Inc.
3c39f472eSBen Skeggs *
4c39f472eSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
5c39f472eSBen Skeggs * copy of this software and associated documentation files (the "Software"),
6c39f472eSBen Skeggs * to deal in the Software without restriction, including without limitation
7c39f472eSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c39f472eSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
9c39f472eSBen Skeggs * Software is furnished to do so, subject to the following conditions:
10c39f472eSBen Skeggs *
11c39f472eSBen Skeggs * The above copyright notice and this permission notice shall be included in
12c39f472eSBen Skeggs * all copies or substantial portions of the Software.
13c39f472eSBen Skeggs *
14c39f472eSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15c39f472eSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16c39f472eSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17c39f472eSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18c39f472eSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19c39f472eSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20c39f472eSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
21c39f472eSBen Skeggs *
22c39f472eSBen Skeggs * Authors: Ben Skeggs
23c39f472eSBen Skeggs */
24c39f472eSBen Skeggs #include "priv.h"
25c39f472eSBen Skeggs
2672251facSMark Menzynski #include <core/option.h>
274e7659fcSBen Skeggs
28c39f472eSBen Skeggs static int
nvkm_gpio_drive(struct nvkm_gpio * gpio,int idx,int line,int dir,int out)294e7659fcSBen Skeggs nvkm_gpio_drive(struct nvkm_gpio *gpio, int idx, int line, int dir, int out)
30c39f472eSBen Skeggs {
312ea7249fSBen Skeggs return gpio->func->drive(gpio, line, dir, out);
32c39f472eSBen Skeggs }
33c39f472eSBen Skeggs
34c39f472eSBen Skeggs static int
nvkm_gpio_sense(struct nvkm_gpio * gpio,int idx,int line)354e7659fcSBen Skeggs nvkm_gpio_sense(struct nvkm_gpio *gpio, int idx, int line)
36c39f472eSBen Skeggs {
372ea7249fSBen Skeggs return gpio->func->sense(gpio, line);
38c39f472eSBen Skeggs }
39c39f472eSBen Skeggs
402ea7249fSBen Skeggs void
nvkm_gpio_reset(struct nvkm_gpio * gpio,u8 func)412ea7249fSBen Skeggs nvkm_gpio_reset(struct nvkm_gpio *gpio, u8 func)
422ea7249fSBen Skeggs {
432ea7249fSBen Skeggs if (gpio->func->reset)
442ea7249fSBen Skeggs gpio->func->reset(gpio, func);
452ea7249fSBen Skeggs }
462ea7249fSBen Skeggs
472ea7249fSBen Skeggs int
nvkm_gpio_find(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line,struct dcb_gpio_func * func)484e7659fcSBen Skeggs nvkm_gpio_find(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line,
49c39f472eSBen Skeggs struct dcb_gpio_func *func)
50c39f472eSBen Skeggs {
51e7d65181SBen Skeggs struct nvkm_device *device = gpio->subdev.device;
5246484438SBen Skeggs struct nvkm_bios *bios = device->bios;
53c39f472eSBen Skeggs u8 ver, len;
54c39f472eSBen Skeggs u16 data;
55c39f472eSBen Skeggs
56c39f472eSBen Skeggs if (line == 0xff && tag == 0xff)
57c39f472eSBen Skeggs return -EINVAL;
58c39f472eSBen Skeggs
59c39f472eSBen Skeggs data = dcb_gpio_match(bios, idx, tag, line, &ver, &len, func);
60c39f472eSBen Skeggs if (data)
61c39f472eSBen Skeggs return 0;
62c39f472eSBen Skeggs
63c39f472eSBen Skeggs /* Apple iMac G4 NV18 */
64c7af0ff0SBen Skeggs if (device->quirk && device->quirk->tv_gpio) {
65c39f472eSBen Skeggs if (tag == DCB_GPIO_TVDAC0) {
66c39f472eSBen Skeggs *func = (struct dcb_gpio_func) {
67c39f472eSBen Skeggs .func = DCB_GPIO_TVDAC0,
68c7af0ff0SBen Skeggs .line = device->quirk->tv_gpio,
69c39f472eSBen Skeggs .log[0] = 0,
70c39f472eSBen Skeggs .log[1] = 1,
71c39f472eSBen Skeggs };
72c39f472eSBen Skeggs return 0;
73c39f472eSBen Skeggs }
74c39f472eSBen Skeggs }
75c39f472eSBen Skeggs
76c39f472eSBen Skeggs return -ENOENT;
77c39f472eSBen Skeggs }
78c39f472eSBen Skeggs
792ea7249fSBen Skeggs int
nvkm_gpio_set(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line,int state)804e7659fcSBen Skeggs nvkm_gpio_set(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line, int state)
81c39f472eSBen Skeggs {
82c39f472eSBen Skeggs struct dcb_gpio_func func;
83c39f472eSBen Skeggs int ret;
84c39f472eSBen Skeggs
854e7659fcSBen Skeggs ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
86c39f472eSBen Skeggs if (ret == 0) {
87c39f472eSBen Skeggs int dir = !!(func.log[state] & 0x02);
88c39f472eSBen Skeggs int out = !!(func.log[state] & 0x01);
894e7659fcSBen Skeggs ret = nvkm_gpio_drive(gpio, idx, func.line, dir, out);
90c39f472eSBen Skeggs }
91c39f472eSBen Skeggs
92c39f472eSBen Skeggs return ret;
93c39f472eSBen Skeggs }
94c39f472eSBen Skeggs
952ea7249fSBen Skeggs int
nvkm_gpio_get(struct nvkm_gpio * gpio,int idx,u8 tag,u8 line)964e7659fcSBen Skeggs nvkm_gpio_get(struct nvkm_gpio *gpio, int idx, u8 tag, u8 line)
97c39f472eSBen Skeggs {
98c39f472eSBen Skeggs struct dcb_gpio_func func;
99c39f472eSBen Skeggs int ret;
100c39f472eSBen Skeggs
1014e7659fcSBen Skeggs ret = nvkm_gpio_find(gpio, idx, tag, line, &func);
102c39f472eSBen Skeggs if (ret == 0) {
1034e7659fcSBen Skeggs ret = nvkm_gpio_sense(gpio, idx, func.line);
104c39f472eSBen Skeggs if (ret >= 0)
105c39f472eSBen Skeggs ret = (ret == (func.log[1] & 1));
106c39f472eSBen Skeggs }
107c39f472eSBen Skeggs
108c39f472eSBen Skeggs return ret;
109c39f472eSBen Skeggs }
110c39f472eSBen Skeggs
111c39f472eSBen Skeggs static void
nvkm_gpio_intr_fini(struct nvkm_event * event,int type,int index)1124e7659fcSBen Skeggs nvkm_gpio_intr_fini(struct nvkm_event *event, int type, int index)
113c39f472eSBen Skeggs {
1144e7659fcSBen Skeggs struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
1152ea7249fSBen Skeggs gpio->func->intr_mask(gpio, type, 1 << index, 0);
116c39f472eSBen Skeggs }
117c39f472eSBen Skeggs
118c39f472eSBen Skeggs static void
nvkm_gpio_intr_init(struct nvkm_event * event,int type,int index)1194e7659fcSBen Skeggs nvkm_gpio_intr_init(struct nvkm_event *event, int type, int index)
120c39f472eSBen Skeggs {
1214e7659fcSBen Skeggs struct nvkm_gpio *gpio = container_of(event, typeof(*gpio), event);
1222ea7249fSBen Skeggs gpio->func->intr_mask(gpio, type, 1 << index, 1 << index);
123c39f472eSBen Skeggs }
124c39f472eSBen Skeggs
1252ea7249fSBen Skeggs static const struct nvkm_event_func
1262ea7249fSBen Skeggs nvkm_gpio_intr_func = {
1272ea7249fSBen Skeggs .init = nvkm_gpio_intr_init,
1282ea7249fSBen Skeggs .fini = nvkm_gpio_intr_fini,
1292ea7249fSBen Skeggs };
1302ea7249fSBen Skeggs
131c39f472eSBen Skeggs static void
nvkm_gpio_intr(struct nvkm_subdev * subdev)1324e7659fcSBen Skeggs nvkm_gpio_intr(struct nvkm_subdev *subdev)
133c39f472eSBen Skeggs {
1344e7659fcSBen Skeggs struct nvkm_gpio *gpio = nvkm_gpio(subdev);
135c39f472eSBen Skeggs u32 hi, lo, i;
136c39f472eSBen Skeggs
1372ea7249fSBen Skeggs gpio->func->intr_stat(gpio, &hi, &lo);
138c39f472eSBen Skeggs
1392ea7249fSBen Skeggs for (i = 0; (hi | lo) && i < gpio->func->lines; i++) {
140773eb04dSBen Skeggs u32 mask = (NVKM_GPIO_HI * !!(hi & (1 << i))) |
141773eb04dSBen Skeggs (NVKM_GPIO_LO * !!(lo & (1 << i)));
142*99d0701aSBen Skeggs nvkm_event_ntfy(&gpio->event, i, mask);
143c39f472eSBen Skeggs }
144c39f472eSBen Skeggs }
145c39f472eSBen Skeggs
1462ea7249fSBen Skeggs static int
nvkm_gpio_fini(struct nvkm_subdev * subdev,bool suspend)1472ea7249fSBen Skeggs nvkm_gpio_fini(struct nvkm_subdev *subdev, bool suspend)
148c39f472eSBen Skeggs {
1492ea7249fSBen Skeggs struct nvkm_gpio *gpio = nvkm_gpio(subdev);
15099a97a8bSAdam Borowski u32 mask = (1ULL << gpio->func->lines) - 1;
151c39f472eSBen Skeggs
1522ea7249fSBen Skeggs gpio->func->intr_mask(gpio, NVKM_GPIO_TOGGLED, mask, 0);
1532ea7249fSBen Skeggs gpio->func->intr_stat(gpio, &mask, &mask);
1542ea7249fSBen Skeggs return 0;
155c39f472eSBen Skeggs }
156c39f472eSBen Skeggs
1576faadbbbSChristoph Hellwig static const struct dmi_system_id gpio_reset_ids[] = {
158c39f472eSBen Skeggs {
159c39f472eSBen Skeggs .ident = "Apple Macbook 10,1",
160c39f472eSBen Skeggs .matches = {
161c39f472eSBen Skeggs DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
162c39f472eSBen Skeggs DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro10,1"),
163c39f472eSBen Skeggs }
164c39f472eSBen Skeggs },
165c39f472eSBen Skeggs { }
166c39f472eSBen Skeggs };
167c39f472eSBen Skeggs
16872251facSMark Menzynski static enum dcb_gpio_func_name power_checks[] = {
169940794b3SMark Menzynski DCB_GPIO_THERM_EXT_POWER_EVENT,
1703c978f73SMark Menzynski DCB_GPIO_POWER_ALERT,
17172251facSMark Menzynski DCB_GPIO_EXT_POWER_LOW,
17272251facSMark Menzynski };
17372251facSMark Menzynski
1742ea7249fSBen Skeggs static int
nvkm_gpio_init(struct nvkm_subdev * subdev)1752ea7249fSBen Skeggs nvkm_gpio_init(struct nvkm_subdev *subdev)
176c39f472eSBen Skeggs {
1772ea7249fSBen Skeggs struct nvkm_gpio *gpio = nvkm_gpio(subdev);
17872251facSMark Menzynski struct dcb_gpio_func func;
17972251facSMark Menzynski int ret;
18072251facSMark Menzynski int i;
18172251facSMark Menzynski
1822ea7249fSBen Skeggs if (dmi_check_system(gpio_reset_ids))
1832ea7249fSBen Skeggs nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
18472251facSMark Menzynski
18572251facSMark Menzynski if (nvkm_boolopt(subdev->device->cfgopt, "NvPowerChecks", true)) {
18672251facSMark Menzynski for (i = 0; i < ARRAY_SIZE(power_checks); ++i) {
18772251facSMark Menzynski ret = nvkm_gpio_find(gpio, 0, power_checks[i],
18872251facSMark Menzynski DCB_GPIO_UNUSED, &func);
18972251facSMark Menzynski if (ret)
19072251facSMark Menzynski continue;
19172251facSMark Menzynski
19272251facSMark Menzynski ret = nvkm_gpio_get(gpio, 0, func.func, func.line);
19372251facSMark Menzynski if (!ret)
19472251facSMark Menzynski continue;
19572251facSMark Menzynski
19672251facSMark Menzynski nvkm_error(&gpio->subdev,
19772251facSMark Menzynski "GPU is missing power, check its power "
19872251facSMark Menzynski "cables. Boot with "
19972251facSMark Menzynski "nouveau.config=NvPowerChecks=0 to "
20072251facSMark Menzynski "disable.\n");
20172251facSMark Menzynski return -EINVAL;
20272251facSMark Menzynski }
20372251facSMark Menzynski }
20472251facSMark Menzynski
2052ea7249fSBen Skeggs return 0;
206c39f472eSBen Skeggs }
207c39f472eSBen Skeggs
2082ea7249fSBen Skeggs static void *
nvkm_gpio_dtor(struct nvkm_subdev * subdev)2092ea7249fSBen Skeggs nvkm_gpio_dtor(struct nvkm_subdev *subdev)
210c39f472eSBen Skeggs {
2112ea7249fSBen Skeggs struct nvkm_gpio *gpio = nvkm_gpio(subdev);
212c39f472eSBen Skeggs nvkm_event_fini(&gpio->event);
2132ea7249fSBen Skeggs return gpio;
214c39f472eSBen Skeggs }
215c39f472eSBen Skeggs
2162ea7249fSBen Skeggs static const struct nvkm_subdev_func
2172ea7249fSBen Skeggs nvkm_gpio = {
2182ea7249fSBen Skeggs .dtor = nvkm_gpio_dtor,
2192ea7249fSBen Skeggs .init = nvkm_gpio_init,
2202ea7249fSBen Skeggs .fini = nvkm_gpio_fini,
2212ea7249fSBen Skeggs .intr = nvkm_gpio_intr,
2222ea7249fSBen Skeggs };
2232ea7249fSBen Skeggs
224c39f472eSBen Skeggs int
nvkm_gpio_new_(const struct nvkm_gpio_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_gpio ** pgpio)2252ea7249fSBen Skeggs nvkm_gpio_new_(const struct nvkm_gpio_func *func, struct nvkm_device *device,
22601055c01SBen Skeggs enum nvkm_subdev_type type, int inst, struct nvkm_gpio **pgpio)
227c39f472eSBen Skeggs {
2284e7659fcSBen Skeggs struct nvkm_gpio *gpio;
229c39f472eSBen Skeggs
2302ea7249fSBen Skeggs if (!(gpio = *pgpio = kzalloc(sizeof(*gpio), GFP_KERNEL)))
2312ea7249fSBen Skeggs return -ENOMEM;
232c39f472eSBen Skeggs
23301055c01SBen Skeggs nvkm_subdev_ctor(&nvkm_gpio, device, type, inst, &gpio->subdev);
2342ea7249fSBen Skeggs gpio->func = func;
235c39f472eSBen Skeggs
236f43e47c0SBen Skeggs return nvkm_event_init(&nvkm_gpio_intr_func, &gpio->subdev, 2, func->lines, &gpio->event);
237c39f472eSBen Skeggs }
238