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
nvkm_uconn_uevent_aux(struct nvkm_object * object,u64 token,u32 bits)34 nvkm_uconn_uevent_aux(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_I2C_PLUG)
41 		args.v0.types |= NVIF_CONN_EVENT_V0_PLUG;
42 	if (bits & NVKM_I2C_UNPLUG)
43 		args.v0.types |= NVIF_CONN_EVENT_V0_UNPLUG;
44 	if (bits & NVKM_I2C_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
nvkm_uconn_uevent_gpio(struct nvkm_object * object,u64 token,u32 bits)51 nvkm_uconn_uevent_gpio(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_GPIO_HI)
58 		args.v0.types |= NVIF_CONN_EVENT_V0_PLUG;
59 	if (bits & NVKM_GPIO_LO)
60 		args.v0.types |= NVIF_CONN_EVENT_V0_UNPLUG;
61 
62 	return object->client->event(token, &args, sizeof(args.v0));
63 }
64 
65 static bool
nvkm_connector_is_dp_dms(u8 type)66 nvkm_connector_is_dp_dms(u8 type)
67 {
68 	switch (type) {
69 	case DCB_CONNECTOR_DMS59_DP0:
70 	case DCB_CONNECTOR_DMS59_DP1:
71 		return true;
72 	default:
73 		return false;
74 	}
75 }
76 
77 static int
nvkm_uconn_uevent(struct nvkm_object * object,void * argv,u32 argc,struct nvkm_uevent * uevent)78 nvkm_uconn_uevent(struct nvkm_object *object, void *argv, u32 argc, struct nvkm_uevent *uevent)
79 {
80 	struct nvkm_conn *conn = nvkm_uconn(object);
81 	struct nvkm_device *device = conn->disp->engine.subdev.device;
82 	struct nvkm_outp *outp;
83 	union nvif_conn_event_args *args = argv;
84 	u64 bits = 0;
85 
86 	if (!uevent) {
87 		if (conn->info.hpd == DCB_GPIO_UNUSED)
88 			return -ENOSYS;
89 		return 0;
90 	}
91 
92 	if (argc != sizeof(args->v0) || args->v0.version != 0)
93 		return -ENOSYS;
94 
95 	list_for_each_entry(outp, &conn->disp->outps, head) {
96 		if (outp->info.connector == conn->index)
97 			break;
98 	}
99 
100 	if (&outp->head == &conn->disp->outps)
101 		return -EINVAL;
102 
103 	if (outp->dp.aux && !outp->info.location) {
104 		if (args->v0.types & NVIF_CONN_EVENT_V0_PLUG  ) bits |= NVKM_I2C_PLUG;
105 		if (args->v0.types & NVIF_CONN_EVENT_V0_UNPLUG) bits |= NVKM_I2C_UNPLUG;
106 		if (args->v0.types & NVIF_CONN_EVENT_V0_IRQ   ) bits |= NVKM_I2C_IRQ;
107 
108 		return nvkm_uevent_add(uevent, &device->i2c->event, outp->dp.aux->id, bits,
109 				       nvkm_uconn_uevent_aux);
110 	}
111 
112 	if (args->v0.types & NVIF_CONN_EVENT_V0_PLUG  ) bits |= NVKM_GPIO_HI;
113 	if (args->v0.types & NVIF_CONN_EVENT_V0_UNPLUG) bits |= NVKM_GPIO_LO;
114 	if (args->v0.types & NVIF_CONN_EVENT_V0_IRQ) {
115 		/* TODO: support DP IRQ on ANX9805 and remove this hack. */
116 		if (!outp->info.location && !nvkm_connector_is_dp_dms(conn->info.type))
117 			return -EINVAL;
118 	}
119 
120 	return nvkm_uevent_add(uevent, &device->gpio->event, conn->info.hpd, bits,
121 			       nvkm_uconn_uevent_gpio);
122 }
123 
124 static int
nvkm_uconn_mthd_hpd_status(struct nvkm_conn * conn,void * argv,u32 argc)125 nvkm_uconn_mthd_hpd_status(struct nvkm_conn *conn, void *argv, u32 argc)
126 {
127 	struct nvkm_gpio *gpio = conn->disp->engine.subdev.device->gpio;
128 	union nvif_conn_hpd_status_args *args = argv;
129 
130 	if (argc != sizeof(args->v0) || args->v0.version != 0)
131 		return -ENOSYS;
132 
133 	args->v0.support = gpio && conn->info.hpd != DCB_GPIO_UNUSED;
134 	args->v0.present = 0;
135 
136 	if (args->v0.support) {
137 		int ret = nvkm_gpio_get(gpio, 0, DCB_GPIO_UNUSED, conn->info.hpd);
138 
139 		if (WARN_ON(ret < 0)) {
140 			args->v0.support = false;
141 			return 0;
142 		}
143 
144 		args->v0.present = ret;
145 	}
146 
147 	return 0;
148 }
149 
150 static int
nvkm_uconn_mthd(struct nvkm_object * object,u32 mthd,void * argv,u32 argc)151 nvkm_uconn_mthd(struct nvkm_object *object, u32 mthd, void *argv, u32 argc)
152 {
153 	struct nvkm_conn *conn = nvkm_uconn(object);
154 
155 	switch (mthd) {
156 	case NVIF_CONN_V0_HPD_STATUS: return nvkm_uconn_mthd_hpd_status(conn, argv, argc);
157 	default:
158 		break;
159 	}
160 
161 	return -EINVAL;
162 }
163 
164 static void *
nvkm_uconn_dtor(struct nvkm_object * object)165 nvkm_uconn_dtor(struct nvkm_object *object)
166 {
167 	struct nvkm_conn *conn = nvkm_uconn(object);
168 	struct nvkm_disp *disp = conn->disp;
169 
170 	spin_lock(&disp->client.lock);
171 	conn->object.func = NULL;
172 	spin_unlock(&disp->client.lock);
173 	return NULL;
174 }
175 
176 static const struct nvkm_object_func
177 nvkm_uconn = {
178 	.dtor = nvkm_uconn_dtor,
179 	.mthd = nvkm_uconn_mthd,
180 	.uevent = nvkm_uconn_uevent,
181 };
182 
183 int
nvkm_uconn_new(const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)184 nvkm_uconn_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
185 {
186 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
187 	struct nvkm_conn *cont, *conn = NULL;
188 	union nvif_conn_args *args = argv;
189 	int ret;
190 
191 	if (argc != sizeof(args->v0) || args->v0.version != 0)
192 		return -ENOSYS;
193 
194 	list_for_each_entry(cont, &disp->conns, head) {
195 		if (cont->index == args->v0.id) {
196 			conn = cont;
197 			break;
198 		}
199 	}
200 
201 	if (!conn)
202 		return -EINVAL;
203 
204 	ret = -EBUSY;
205 	spin_lock(&disp->client.lock);
206 	if (!conn->object.func) {
207 		nvkm_object_ctor(&nvkm_uconn, oclass, &conn->object);
208 		*pobject = &conn->object;
209 		ret = 0;
210 	}
211 	spin_unlock(&disp->client.lock);
212 	return ret;
213 }
214