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_connector_hpd(struct nvkm_notify *notify)
34 {
35 	struct nvkm_connector *conn = container_of(notify, typeof(*conn), hpd);
36 	struct nvkm_disp *disp = nvkm_disp(conn);
37 	struct nvkm_gpio *gpio = nvkm_gpio(conn);
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 	DBG("HPD: %d\n", line->mask);
43 
44 	if (!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 int
55 _nvkm_connector_fini(struct nvkm_object *object, bool suspend)
56 {
57 	struct nvkm_connector *conn = (void *)object;
58 	nvkm_notify_put(&conn->hpd);
59 	return nvkm_object_fini(&conn->base, suspend);
60 }
61 
62 int
63 _nvkm_connector_init(struct nvkm_object *object)
64 {
65 	struct nvkm_connector *conn = (void *)object;
66 	int ret = nvkm_object_init(&conn->base);
67 	if (ret == 0)
68 		nvkm_notify_get(&conn->hpd);
69 	return ret;
70 }
71 
72 void
73 _nvkm_connector_dtor(struct nvkm_object *object)
74 {
75 	struct nvkm_connector *conn = (void *)object;
76 	nvkm_notify_fini(&conn->hpd);
77 	nvkm_object_destroy(&conn->base);
78 }
79 
80 int
81 nvkm_connector_create_(struct nvkm_object *parent,
82 		       struct nvkm_object *engine,
83 		       struct nvkm_oclass *oclass,
84 		       struct nvbios_connE *info, int index,
85 		       int length, void **pobject)
86 {
87 	static const u8 hpd[] = { 0x07, 0x08, 0x51, 0x52, 0x5e, 0x5f, 0x60 };
88 	struct nvkm_disp *disp = nvkm_disp(parent);
89 	struct nvkm_gpio *gpio = nvkm_gpio(parent);
90 	struct nvkm_connector *conn;
91 	struct nvkm_output *outp;
92 	struct dcb_gpio_func func;
93 	int ret;
94 
95 	list_for_each_entry(outp, &disp->outp, head) {
96 		if (outp->conn && outp->conn->index == index) {
97 			atomic_inc(&nv_object(outp->conn)->refcount);
98 			*pobject = outp->conn;
99 			return 1;
100 		}
101 	}
102 
103 	ret = nvkm_object_create_(parent, engine, oclass, 0, length, pobject);
104 	conn = *pobject;
105 	if (ret)
106 		return ret;
107 
108 	conn->info = *info;
109 	conn->index = index;
110 
111 	DBG("type %02x loc %d hpd %02x dp %x di %x sr %x lcdid %x\n",
112 	    info->type, info->location, info->hpd, info->dp,
113 	    info->di, info->sr, info->lcdid);
114 
115 	if ((info->hpd = ffs(info->hpd))) {
116 		if (--info->hpd >= ARRAY_SIZE(hpd)) {
117 			ERR("hpd %02x unknown\n", info->hpd);
118 			return 0;
119 		}
120 		info->hpd = hpd[info->hpd];
121 
122 		ret = gpio->find(gpio, 0, info->hpd, DCB_GPIO_UNUSED, &func);
123 		if (ret) {
124 			ERR("func %02x lookup failed, %d\n", info->hpd, ret);
125 			return 0;
126 		}
127 
128 		ret = nvkm_notify_init(NULL, &gpio->event, nvkm_connector_hpd,
129 				       true, &(struct nvkm_gpio_ntfy_req) {
130 					.mask = NVKM_GPIO_TOGGLED,
131 					.line = func.line,
132 				       },
133 				       sizeof(struct nvkm_gpio_ntfy_req),
134 				       sizeof(struct nvkm_gpio_ntfy_rep),
135 				       &conn->hpd);
136 		if (ret) {
137 			ERR("func %02x failed, %d\n", info->hpd, ret);
138 		} else {
139 			DBG("func %02x (HPD)\n", info->hpd);
140 		}
141 	}
142 
143 	return 0;
144 }
145 
146 int
147 _nvkm_connector_ctor(struct nvkm_object *parent,
148 		     struct nvkm_object *engine,
149 		     struct nvkm_oclass *oclass, void *info, u32 index,
150 		     struct nvkm_object **pobject)
151 {
152 	struct nvkm_connector *conn;
153 	int ret;
154 
155 	ret = nvkm_connector_create(parent, engine, oclass, info, index, &conn);
156 	*pobject = nv_object(conn);
157 	if (ret)
158 		return ret;
159 
160 	return 0;
161 }
162 
163 struct nvkm_oclass *
164 nvkm_connector_oclass = &(struct nvkm_connector_impl) {
165 	.base = {
166 		.handle = 0,
167 		.ofuncs = &(struct nvkm_ofuncs) {
168 			.ctor = _nvkm_connector_ctor,
169 			.dtor = _nvkm_connector_dtor,
170 			.init = _nvkm_connector_init,
171 			.fini = _nvkm_connector_fini,
172 		},
173 	},
174 }.base;
175