1/*	$NetBSD: nouveau_nvkm_engine_disp_conn.c,v 1.3 2021/12/18 23:45:35 riastradh Exp $	*/
2
3/*
4 * Copyright 2014 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_engine_disp_conn.c,v 1.3 2021/12/18 23:45:35 riastradh Exp $");
28
29#include "conn.h"
30#include "outp.h"
31#include "priv.h"
32
33#include <subdev/gpio.h>
34
35#include <nvif/event.h>
36
37static int
38nvkm_conn_hpd(struct nvkm_notify *notify)
39{
40	struct nvkm_conn *conn = container_of(notify, typeof(*conn), hpd);
41	struct nvkm_disp *disp = conn->disp;
42	struct nvkm_gpio *gpio = disp->engine.subdev.device->gpio;
43	const struct nvkm_gpio_ntfy_rep *line = notify->data;
44	struct nvif_notify_conn_rep_v0 rep;
45	int index = conn->index;
46
47	CONN_DBG(conn, "HPD: %d", line->mask);
48
49	if (!nvkm_gpio_get(gpio, 0, DCB_GPIO_UNUSED, conn->hpd.index))
50		rep.mask = NVIF_NOTIFY_CONN_V0_UNPLUG;
51	else
52		rep.mask = NVIF_NOTIFY_CONN_V0_PLUG;
53	rep.version = 0;
54
55	nvkm_event_send(&disp->hpd, rep.mask, index, &rep, sizeof(rep));
56	return NVKM_NOTIFY_KEEP;
57}
58
59void
60nvkm_conn_fini(struct nvkm_conn *conn)
61{
62	nvkm_notify_put(&conn->hpd);
63}
64
65void
66nvkm_conn_init(struct nvkm_conn *conn)
67{
68	nvkm_notify_get(&conn->hpd);
69}
70
71void
72nvkm_conn_del(struct nvkm_conn **pconn)
73{
74	struct nvkm_conn *conn = *pconn;
75	if (conn) {
76		nvkm_notify_fini(&conn->hpd);
77		kfree(*pconn);
78		*pconn = NULL;
79	}
80}
81
82static void
83nvkm_conn_ctor(struct nvkm_disp *disp, int index, struct nvbios_connE *info,
84	       struct nvkm_conn *conn)
85{
86	static const u8 hpd[] = { 0x07, 0x08, 0x51, 0x52, 0x5e, 0x5f, 0x60 };
87	struct nvkm_gpio *gpio = disp->engine.subdev.device->gpio;
88	struct dcb_gpio_func func;
89	int ret;
90
91	conn->disp = disp;
92	conn->index = index;
93	conn->info = *info;
94
95	CONN_DBG(conn, "type %02x loc %d hpd %02x dp %x di %x sr %x lcdid %x",
96		 info->type, info->location, info->hpd, info->dp,
97		 info->di, info->sr, info->lcdid);
98
99	if ((info->hpd = ffs(info->hpd))) {
100		if (--info->hpd >= ARRAY_SIZE(hpd)) {
101			CONN_ERR(conn, "hpd %02x unknown", info->hpd);
102			return;
103		}
104		info->hpd = hpd[info->hpd];
105
106		ret = nvkm_gpio_find(gpio, 0, info->hpd, DCB_GPIO_UNUSED, &func);
107		if (ret) {
108			CONN_ERR(conn, "func %02x lookup failed, %d",
109				 info->hpd, ret);
110			return;
111		}
112
113		ret = nvkm_notify_init(NULL, &gpio->event, nvkm_conn_hpd,
114				       true, &(struct nvkm_gpio_ntfy_req) {
115					.mask = NVKM_GPIO_TOGGLED,
116					.line = func.line,
117				       },
118				       sizeof(struct nvkm_gpio_ntfy_req),
119				       sizeof(struct nvkm_gpio_ntfy_rep),
120				       &conn->hpd);
121		if (ret) {
122			CONN_ERR(conn, "func %02x failed, %d", info->hpd, ret);
123		} else {
124			CONN_DBG(conn, "func %02x (HPD)", info->hpd);
125		}
126	}
127}
128
129int
130nvkm_conn_new(struct nvkm_disp *disp, int index, struct nvbios_connE *info,
131	      struct nvkm_conn **pconn)
132{
133	if (!(*pconn = kzalloc(sizeof(**pconn), GFP_KERNEL)))
134		return -ENOMEM;
135	nvkm_conn_ctor(disp, index, info, *pconn);
136	return 0;
137}
138