1 /*
2  * Copyright 2014 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 #include "conn.h"
25 #include "outp.h"
26 #include "priv.h"
27 
28 #include <subdev/gpio.h>
29 
30 #include <nvif/event.h>
31 
32 static int
33 nvkm_conn_hpd(struct nvkm_notify *notify)
34 {
35 	struct nvkm_conn *conn = container_of(notify, typeof(*conn), hpd);
36 	struct nvkm_disp *disp = conn->disp;
37 	struct nvkm_gpio *gpio = disp->engine.subdev.device->gpio;
38 	const struct nvkm_gpio_ntfy_rep *line = notify->data;
39 	struct nvif_notify_conn_rep_v0 rep;
40 	int index = conn->index;
41 
42 	CONN_DBG(conn, "HPD: %d", line->mask);
43 
44 	if (!nvkm_gpio_get(gpio, 0, DCB_GPIO_UNUSED, conn->hpd.index))
45 		rep.mask = NVIF_NOTIFY_CONN_V0_UNPLUG;
46 	else
47 		rep.mask = NVIF_NOTIFY_CONN_V0_PLUG;
48 	rep.version = 0;
49 
50 	nvkm_event_send(&disp->hpd, rep.mask, index, &rep, sizeof(rep));
51 	return NVKM_NOTIFY_KEEP;
52 }
53 
54 void
55 nvkm_conn_fini(struct nvkm_conn *conn)
56 {
57 	nvkm_notify_put(&conn->hpd);
58 }
59 
60 void
61 nvkm_conn_init(struct nvkm_conn *conn)
62 {
63 	nvkm_notify_get(&conn->hpd);
64 }
65 
66 void
67 nvkm_conn_del(struct nvkm_conn **pconn)
68 {
69 	struct nvkm_conn *conn = *pconn;
70 	if (conn) {
71 		nvkm_notify_fini(&conn->hpd);
72 		kfree(*pconn);
73 		*pconn = NULL;
74 	}
75 }
76 
77 static void
78 nvkm_conn_ctor(struct nvkm_disp *disp, int index, struct nvbios_connE *info,
79 	       struct nvkm_conn *conn)
80 {
81 	static const u8 hpd[] = { 0x07, 0x08, 0x51, 0x52, 0x5e, 0x5f, 0x60 };
82 	struct nvkm_gpio *gpio = disp->engine.subdev.device->gpio;
83 	struct dcb_gpio_func func;
84 	int ret;
85 
86 	conn->disp = disp;
87 	conn->index = index;
88 	conn->info = *info;
89 	conn->info.hpd = DCB_GPIO_UNUSED;
90 
91 	CONN_DBG(conn, "type %02x loc %d hpd %02x dp %x di %x sr %x lcdid %x",
92 		 info->type, info->location, info->hpd, info->dp,
93 		 info->di, info->sr, info->lcdid);
94 
95 	if ((info->hpd = ffs(info->hpd))) {
96 		if (--info->hpd >= ARRAY_SIZE(hpd)) {
97 			CONN_ERR(conn, "hpd %02x unknown", info->hpd);
98 			return;
99 		}
100 		info->hpd = hpd[info->hpd];
101 
102 		ret = nvkm_gpio_find(gpio, 0, info->hpd, DCB_GPIO_UNUSED, &func);
103 		if (ret) {
104 			CONN_ERR(conn, "func %02x lookup failed, %d", info->hpd, ret);
105 			return;
106 		}
107 
108 		conn->info.hpd = func.line;
109 
110 		ret = nvkm_notify_init(NULL, &gpio->event, nvkm_conn_hpd,
111 				       true, &(struct nvkm_gpio_ntfy_req) {
112 					.mask = NVKM_GPIO_TOGGLED,
113 					.line = func.line,
114 				       },
115 				       sizeof(struct nvkm_gpio_ntfy_req),
116 				       sizeof(struct nvkm_gpio_ntfy_rep),
117 				       &conn->hpd);
118 		if (ret) {
119 			CONN_ERR(conn, "func %02x failed, %d", info->hpd, ret);
120 		} else {
121 			CONN_DBG(conn, "func %02x (HPD)", info->hpd);
122 		}
123 	}
124 }
125 
126 int
127 nvkm_conn_new(struct nvkm_disp *disp, int index, struct nvbios_connE *info,
128 	      struct nvkm_conn **pconn)
129 {
130 	if (!(*pconn = kzalloc(sizeof(**pconn), GFP_KERNEL)))
131 		return -ENOMEM;
132 	nvkm_conn_ctor(disp, index, info, *pconn);
133 	return 0;
134 }
135