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 
25 #include <subdev/gpio.h>
26 
27 #include <nvif/if0011.h>
28 
29 static int
30 nvkm_uconn_mthd_hpd_status(struct nvkm_conn *conn, void *argv, u32 argc)
31 {
32 	struct nvkm_gpio *gpio = conn->disp->engine.subdev.device->gpio;
33 	union nvif_conn_hpd_status_args *args = argv;
34 
35 	if (argc != sizeof(args->v0) || args->v0.version != 0)
36 		return -ENOSYS;
37 
38 	args->v0.support = gpio && conn->info.hpd != DCB_GPIO_UNUSED;
39 	args->v0.present = 0;
40 
41 	if (args->v0.support) {
42 		int ret = nvkm_gpio_get(gpio, 0, DCB_GPIO_UNUSED, conn->info.hpd);
43 
44 		if (WARN_ON(ret < 0)) {
45 			args->v0.support = false;
46 			return 0;
47 		}
48 
49 		args->v0.present = ret;
50 	}
51 
52 	return 0;
53 }
54 
55 static int
56 nvkm_uconn_mthd(struct nvkm_object *object, u32 mthd, void *argv, u32 argc)
57 {
58 	struct nvkm_conn *conn = nvkm_uconn(object);
59 
60 	switch (mthd) {
61 	case NVIF_CONN_V0_HPD_STATUS: return nvkm_uconn_mthd_hpd_status(conn, argv, argc);
62 	default:
63 		break;
64 	}
65 
66 	return -EINVAL;
67 }
68 
69 static void *
70 nvkm_uconn_dtor(struct nvkm_object *object)
71 {
72 	struct nvkm_conn *conn = nvkm_uconn(object);
73 	struct nvkm_disp *disp = conn->disp;
74 
75 	spin_lock(&disp->client.lock);
76 	conn->object.func = NULL;
77 	spin_unlock(&disp->client.lock);
78 	return NULL;
79 }
80 
81 static const struct nvkm_object_func
82 nvkm_uconn = {
83 	.dtor = nvkm_uconn_dtor,
84 	.mthd = nvkm_uconn_mthd,
85 };
86 
87 int
88 nvkm_uconn_new(const struct nvkm_oclass *oclass, void *argv, u32 argc, struct nvkm_object **pobject)
89 {
90 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
91 	struct nvkm_conn *cont, *conn = NULL;
92 	union nvif_conn_args *args = argv;
93 	int ret;
94 
95 	if (argc != sizeof(args->v0) || args->v0.version != 0)
96 		return -ENOSYS;
97 
98 	list_for_each_entry(cont, &disp->conns, head) {
99 		if (cont->index == args->v0.id) {
100 			conn = cont;
101 			break;
102 		}
103 	}
104 
105 	if (!conn)
106 		return -EINVAL;
107 
108 	ret = -EBUSY;
109 	spin_lock(&disp->client.lock);
110 	if (!conn->object.func) {
111 		nvkm_object_ctor(&nvkm_uconn, oclass, &conn->object);
112 		*pobject = &conn->object;
113 		ret = 0;
114 	}
115 	spin_unlock(&disp->client.lock);
116 	return ret;
117 }
118