xref: /netbsd-src/sys/dev/fdt/hdmi_connector.c (revision dd47db3e83a68b8e7f55e1a33bd6e94970c2de7e)
1 /* $NetBSD: hdmi_connector.c,v 1.4 2021/12/19 11:01:10 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: hdmi_connector.c,v 1.4 2021/12/19 11:01:10 riastradh Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/gpio.h>
36 #include <sys/systm.h>
37 
38 #include <dev/fdt/fdt_port.h>
39 #include <dev/fdt/fdtvar.h>
40 
41 #include <dev/i2c/ddcvar.h>
42 
43 #include <drm/drm_crtc.h>
44 #include <drm/drm_crtc_helper.h>
45 #include <drm/drm_drv.h>
46 #include <drm/drm_edid.h>
47 #include <drm/drm_probe_helper.h>
48 
49 static const struct device_compatible_entry compat_data[] = {
50 	{ .compat = "hdmi-connector" },
51 	DEVICE_COMPAT_EOL
52 };
53 
54 struct dispcon_hdmi_connector {
55 	struct drm_connector		base;
56 	struct fdtbus_gpio_pin		*hpd;
57 	i2c_tag_t			ddc;
58 
59 	int				type;	/* DRM_MODE_CONNECTOR_* */
60 };
61 
62 struct dispcon_hdmi_softc {
63 	struct fdt_device_ports		sc_ports;
64 	struct dispcon_hdmi_connector	sc_connector;
65 };
66 
67 #define	to_dispcon_hdmi_connector(x)	container_of(x, struct dispcon_hdmi_connector, base)
68 
69 static enum drm_connector_status
dispcon_hdmi_connector_detect(struct drm_connector * connector,bool force)70 dispcon_hdmi_connector_detect(struct drm_connector *connector, bool force)
71 {
72 	struct dispcon_hdmi_connector *hdmi_connector = to_dispcon_hdmi_connector(connector);
73 	bool con;
74 
75 	if (hdmi_connector->hpd == NULL) {
76 		/*
77 		 * No hotplug detect pin available. Assume that we are connected.
78 		 */
79 		return connector_status_connected;
80 	}
81 
82 	/*
83 	 * Read connect status from hotplug detect pin.
84 	 */
85 	con = fdtbus_gpio_read(hdmi_connector->hpd);
86 	if (con) {
87 		return connector_status_connected;
88 	} else {
89 		return connector_status_disconnected;
90 	}
91 }
92 
93 static void
dispcon_hdmi_connector_destroy(struct drm_connector * connector)94 dispcon_hdmi_connector_destroy(struct drm_connector *connector)
95 {
96 	drm_connector_unregister(connector);
97 	drm_connector_cleanup(connector);
98 }
99 
100 static const struct drm_connector_funcs dispcon_hdmi_connector_funcs = {
101 	.dpms = drm_helper_connector_dpms,
102 	.detect = dispcon_hdmi_connector_detect,
103 	.fill_modes = drm_helper_probe_single_connector_modes,
104 	.destroy = dispcon_hdmi_connector_destroy,
105 };
106 
107 static int
dispcon_hdmi_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)108 dispcon_hdmi_connector_mode_valid(struct drm_connector *connector, struct drm_display_mode *mode)
109 {
110 	return MODE_OK;
111 }
112 
113 static int
dispcon_hdmi_connector_get_modes(struct drm_connector * connector)114 dispcon_hdmi_connector_get_modes(struct drm_connector *connector)
115 {
116 	struct dispcon_hdmi_connector *hdmi_connector = to_dispcon_hdmi_connector(connector);
117 	char edid[EDID_LENGTH * 4];
118 	struct edid *pedid = NULL;
119 	int error, block;
120 
121 	if (hdmi_connector->ddc != NULL) {
122 		memset(edid, 0, sizeof(edid));
123 		for (block = 0; block < 4; block++) {
124 			error = ddc_read_edid_block(hdmi_connector->ddc,
125 			    &edid[block * EDID_LENGTH], EDID_LENGTH, block);
126 			if (error)
127 				break;
128 			if (block == 0) {
129 				pedid = (struct edid *)edid;
130 				if (edid[0x7e] == 0)
131 					break;
132 			}
133 		}
134 	}
135 
136 	drm_connector_update_edid_property(connector, pedid);
137 	if (pedid == NULL)
138 		return 0;
139 
140 	return drm_add_edid_modes(connector, pedid);
141 }
142 
143 static const struct drm_connector_helper_funcs dispcon_hdmi_connector_helper_funcs = {
144 	.mode_valid = dispcon_hdmi_connector_mode_valid,
145 	.get_modes = dispcon_hdmi_connector_get_modes,
146 };
147 
148 static int
dispcon_hdmi_ep_activate(device_t dev,struct fdt_endpoint * ep,bool activate)149 dispcon_hdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
150 {
151 	struct drm_connector *connector = fdt_endpoint_get_data(ep);
152 	struct dispcon_hdmi_connector *hdmi_connector = to_dispcon_hdmi_connector(connector);
153 	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
154 	struct drm_encoder *encoder;
155 
156 	if (fdt_endpoint_port_index(ep) != 0)
157 		return EINVAL;
158 
159 	if (fdt_endpoint_type(rep) != EP_DRM_ENCODER)
160 		return EINVAL;
161 
162 	if (activate) {
163 		encoder = fdt_endpoint_get_data(rep);
164 
165 		connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
166 		connector->interlace_allowed = 0;
167 		connector->doublescan_allowed = 0;
168 
169 		drm_connector_init(encoder->dev, connector, &dispcon_hdmi_connector_funcs,
170 		    hdmi_connector->type);
171 		drm_connector_helper_add(connector, &dispcon_hdmi_connector_helper_funcs);
172 		drm_connector_register(connector);
173 		drm_connector_attach_encoder(connector, encoder);
174 	}
175 
176 	return 0;
177 }
178 
179 static void *
dispcon_hdmi_ep_get_data(device_t dev,struct fdt_endpoint * ep)180 dispcon_hdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep)
181 {
182 	struct dispcon_hdmi_softc * const sc = device_private(dev);
183 
184 	return &sc->sc_connector.base;
185 }
186 
187 static int
dispcon_hdmi_match(device_t parent,cfdata_t cf,void * aux)188 dispcon_hdmi_match(device_t parent, cfdata_t cf, void *aux)
189 {
190 	struct fdt_attach_args * const faa = aux;
191 
192 	return of_compatible_match(faa->faa_phandle, compat_data);
193 }
194 
195 static void
dispcon_hdmi_attach(device_t parent,device_t self,void * aux)196 dispcon_hdmi_attach(device_t parent, device_t self, void *aux)
197 {
198 	struct dispcon_hdmi_softc * const sc = device_private(self);
199 	struct dispcon_hdmi_connector * const hdmi_connector = &sc->sc_connector;
200 	struct fdt_attach_args * const faa = aux;
201 	const int phandle = faa->faa_phandle;
202 
203 	aprint_naive("\n");
204 	aprint_normal(": HDMI connector\n");
205 
206 	hdmi_connector->type = DRM_MODE_CONNECTOR_HDMIA;
207 	hdmi_connector->hpd = fdtbus_gpio_acquire(phandle, "hpd-gpios", GPIO_PIN_INPUT);
208 	hdmi_connector->ddc = fdtbus_i2c_acquire(phandle, "ddc-i2c-bus");
209 
210 	sc->sc_ports.dp_ep_activate = dispcon_hdmi_ep_activate;
211 	sc->sc_ports.dp_ep_get_data = dispcon_hdmi_ep_get_data;
212 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_CONNECTOR);
213 }
214 
215 CFATTACH_DECL_NEW(dispcon_hdmi, sizeof(struct dispcon_hdmi_softc),
216 	dispcon_hdmi_match, dispcon_hdmi_attach, NULL, NULL);
217