xref: /linux/drivers/gpu/drm/nouveau/nvkm/engine/disp/uconn.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
1 /*
2  * Copyright 2021 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 #define nvkm_uconn(p) container_of((p), struct nvkm_conn, object)
23 #include "conn.h"
24 #include "outp.h"
25 
26 #include <core/client.h>
27 #include <core/event.h>
28 #include <subdev/gpio.h>
29 #include <subdev/i2c.h>
30 
31 #include <nvif/if0011.h>
32 
33 static int
34 nvkm_uconn_uevent_gsp(struct nvkm_object *object, u64 token, u32 bits)
35 {
36 	union nvif_conn_event_args args;
37 
38 	args.v0.version = 0;
39 	args.v0.types = 0;
40 	if (bits & NVKM_DPYID_PLUG)
41 		args.v0.types |= NVIF_CONN_EVENT_V0_PLUG;
42 	if (bits & NVKM_DPYID_UNPLUG)
43 		args.v0.types |= NVIF_CONN_EVENT_V0_UNPLUG;
44 	if (bits & NVKM_DPYID_IRQ)
45 		args.v0.types |= NVIF_CONN_EVENT_V0_IRQ;
46 
47 	return object->client->event(token, &args, sizeof(args.v0));
48 }
49 
50 static int
51 nvkm_uconn_uevent_aux(struct nvkm_object *object, u64 token, u32 bits)
52 {
53 	union nvif_conn_event_args args;
54 
55 	args.v0.version = 0;
56 	args.v0.types = 0;
57 	if (bits & NVKM_I2C_PLUG)
58 		args.v0.types |= NVIF_CONN_EVENT_V0_PLUG;
59 	if (bits & NVKM_I2C_UNPLUG)
60 		args.v0.types |= NVIF_CONN_EVENT_V0_UNPLUG;
61 	if (bits & NVKM_I2C_IRQ)
62 		args.v0.types |= NVIF_CONN_EVENT_V0_IRQ;
63 
64 	return object->client->event(token, &args, sizeof(args.v0));
65 }
66 
67 static int
68 nvkm_uconn_uevent_gpio(struct nvkm_object *object, u64 token, u32 bits)
69 {
70 	union nvif_conn_event_args args;
71 
72 	args.v0.version = 0;
73 	args.v0.types = 0;
74 	if (bits & NVKM_GPIO_HI)
75 		args.v0.types |= NVIF_CONN_EVENT_V0_PLUG;
76 	if (bits & NVKM_GPIO_LO)
77 		args.v0.types |= NVIF_CONN_EVENT_V0_UNPLUG;
78 
79 	return object->client->event(token, &args, sizeof(args.v0));
80 }
81 
82 static bool
83 nvkm_connector_is_dp_dms(u8 type)
84 {
85 	switch (type) {
86 	case DCB_CONNECTOR_DMS59_DP0:
87 	case DCB_CONNECTOR_DMS59_DP1:
88 		return true;
89 	default:
90 		return false;
91 	}
92 }
93 
94 static int
95 nvkm_uconn_uevent(struct nvkm_object *object, void *argv, u32 argc, struct nvkm_uevent *uevent)
96 {
97 	struct nvkm_conn *conn = nvkm_uconn(object);
98 	struct nvkm_disp *disp = conn->disp;
99 	struct nvkm_device *device = disp->engine.subdev.device;
100 	struct nvkm_outp *outp;
101 	union nvif_conn_event_args *args = argv;
102 	u64 bits = 0;
103 
104 	if (!uevent) {
105 		if (!disp->rm.client.gsp && conn->info.hpd == DCB_GPIO_UNUSED)
106 			return -ENOSYS;
107 		return 0;
108 	}
109 
110 	if (argc != sizeof(args->v0) || args->v0.version != 0)
111 		return -ENOSYS;
112 
113 	list_for_each_entry(outp, &conn->disp->outps, head) {
114 		if (outp->info.connector == conn->index)
115 			break;
116 	}
117 
118 	if (&outp->head == &conn->disp->outps)
119 		return -EINVAL;
120 
121 	if (disp->rm.client.gsp) {
122 		if (args->v0.types & NVIF_CONN_EVENT_V0_PLUG  ) bits |= NVKM_DPYID_PLUG;
123 		if (args->v0.types & NVIF_CONN_EVENT_V0_UNPLUG) bits |= NVKM_DPYID_UNPLUG;
124 		if (args->v0.types & NVIF_CONN_EVENT_V0_IRQ   ) bits |= NVKM_DPYID_IRQ;
125 
126 		return nvkm_uevent_add(uevent, &disp->rm.event, outp->index, bits,
127 				       nvkm_uconn_uevent_gsp);
128 	}
129 
130 	if (outp->dp.aux && !outp->info.location) {
131 		if (args->v0.types & NVIF_CONN_EVENT_V0_PLUG  ) bits |= NVKM_I2C_PLUG;
132 		if (args->v0.types & NVIF_CONN_EVENT_V0_UNPLUG) bits |= NVKM_I2C_UNPLUG;
133 		if (args->v0.types & NVIF_CONN_EVENT_V0_IRQ   ) bits |= NVKM_I2C_IRQ;
134 
135 		return nvkm_uevent_add(uevent, &device->i2c->event, outp->dp.aux->id, bits,
136 				       nvkm_uconn_uevent_aux);
137 	}
138 
139 	if (args->v0.types & NVIF_CONN_EVENT_V0_PLUG  ) bits |= NVKM_GPIO_HI;
140 	if (args->v0.types & NVIF_CONN_EVENT_V0_UNPLUG) bits |= NVKM_GPIO_LO;
141 	if (args->v0.types & NVIF_CONN_EVENT_V0_IRQ) {
142 		/* TODO: support DP IRQ on ANX9805 and remove this hack. */
143 		if (!outp->info.location && !nvkm_connector_is_dp_dms(conn->info.type))
144 			return -EINVAL;
145 	}
146 
147 	return nvkm_uevent_add(uevent, &device->gpio->event, conn->info.hpd, bits,
148 			       nvkm_uconn_uevent_gpio);
149 }
150 
151 static void *
152 nvkm_uconn_dtor(struct nvkm_object *object)
153 {
154 	struct nvkm_conn *conn = nvkm_uconn(object);
155 	struct nvkm_disp *disp = conn->disp;
156 
157 	spin_lock(&disp->client.lock);
158 	conn->object.func = NULL;
159 	spin_unlock(&disp->client.lock);
160 	return NULL;
161 }
162 
163 static const struct nvkm_object_func
164 nvkm_uconn = {
165 	.dtor = nvkm_uconn_dtor,
166 	.uevent = nvkm_uconn_uevent,
167 };
168 
169 int
170 nvkm_uconn_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
171 {
172 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
173 	struct nvkm_conn *cont, *conn = NULL;
174 	union nvif_conn_args *args = argv;
175 	int ret;
176 
177 	if (argc != sizeof(args->v0) || args->v0.version != 0)
178 		return -ENOSYS;
179 
180 	list_for_each_entry(cont, &disp->conns, head) {
181 		if (cont->index == args->v0.id) {
182 			conn = cont;
183 			break;
184 		}
185 	}
186 
187 	if (!conn)
188 		return -EINVAL;
189 
190 	ret = -EBUSY;
191 	spin_lock(&disp->client.lock);
192 	if (!conn->object.func) {
193 		switch (conn->info.type) {
194 		case DCB_CONNECTOR_VGA      : args->v0.type = NVIF_CONN_V0_VGA; break;
195 		case DCB_CONNECTOR_TV_0     :
196 		case DCB_CONNECTOR_TV_1     :
197 		case DCB_CONNECTOR_TV_3     : args->v0.type = NVIF_CONN_V0_TV; break;
198 		case DCB_CONNECTOR_DMS59_0  :
199 		case DCB_CONNECTOR_DMS59_1  :
200 		case DCB_CONNECTOR_DVI_I    : args->v0.type = NVIF_CONN_V0_DVI_I; break;
201 		case DCB_CONNECTOR_DVI_D    : args->v0.type = NVIF_CONN_V0_DVI_D; break;
202 		case DCB_CONNECTOR_LVDS     : args->v0.type = NVIF_CONN_V0_LVDS; break;
203 		case DCB_CONNECTOR_LVDS_SPWG: args->v0.type = NVIF_CONN_V0_LVDS_SPWG; break;
204 		case DCB_CONNECTOR_DMS59_DP0:
205 		case DCB_CONNECTOR_DMS59_DP1:
206 		case DCB_CONNECTOR_DP       :
207 		case DCB_CONNECTOR_mDP      :
208 		case DCB_CONNECTOR_USB_C    : args->v0.type = NVIF_CONN_V0_DP; break;
209 		case DCB_CONNECTOR_eDP      : args->v0.type = NVIF_CONN_V0_EDP; break;
210 		case DCB_CONNECTOR_HDMI_0   :
211 		case DCB_CONNECTOR_HDMI_1   :
212 		case DCB_CONNECTOR_HDMI_C   : args->v0.type = NVIF_CONN_V0_HDMI; break;
213 		default:
214 			WARN_ON(1);
215 			ret = -EINVAL;
216 			break;
217 		}
218 
219 		nvkm_object_ctor(&nvkm_uconn, oclass, &conn->object);
220 		*pobject = &conn->object;
221 		ret = 0;
222 	}
223 	spin_unlock(&disp->client.lock);
224 	return ret;
225 }
226