xref: /netbsd-src/sys/arch/arm/nxp/imx6_dwhdmi.c (revision f1f3024106cd914aeb0e36f884f2a7b47b611483)
1 /* $NetBSD: imx6_dwhdmi.c,v 1.4 2021/12/19 11:25:39 riastradh Exp $ */
2 /*-
3  * Copyright (c) 2020 Genetec Corporation.  All rights reserved.
4  * Written by Hashimoto Kenichi for Genetec Corporation.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: imx6_dwhdmi.c,v 1.4 2021/12/19 11:25:39 riastradh Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/device.h>
34 #include <sys/intr.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 
39 #include <drm/drm_crtc_helper.h>
40 
41 #include <dev/fdt/fdtvar.h>
42 #include <dev/fdt/fdt_port.h>
43 #include <dev/fdt/syscon.h>
44 
45 #include <dev/ic/dw_hdmi.h>
46 
47 static const struct dwhdmi_mpll_config imx6_dwhdmi_mpll_config[] = {
48 	{ 45250,	0x01e0, 0x0000, 0x091c },
49 	{ 92500,	0x0072, 0x0001, 0x06dc },
50 	{ 148500,	0x0051, 0x0002, 0x091c },
51 	{ 216000,	0x00a0, 0x000a, 0x06dc },
52 	{ 0,		0x0000, 0x0000, 0x0000 },
53 };
54 
55 static const struct dwhdmi_phy_config imx6_dwhdmi_phy_config[] = {
56 	{ 216000,	0x800d, 0x000a, 0x01ad },
57 	{ 0,		0x0000, 0x0000, 0x0000 }
58 };
59 
60 enum {
61 	DWHDMI_PORT_INPUT = 0,
62 	DWHDMI_PORT_OUTPUT = 1,
63 };
64 
65 static const struct device_compatible_entry compat_data[] = {
66 	{ .compat = "fsl,imx6dl-hdmi" },
67 	{ .compat = "fsl,imx6q-hdmi" },
68 	DEVICE_COMPAT_EOL
69 };
70 
71 struct imx6_dwhdmi_softc {
72 	struct dwhdmi_softc	sc_base;
73 	int			sc_phandle;
74 
75 	struct fdt_device_ports	sc_ports;
76 	struct drm_display_mode	sc_curmode;
77 	struct drm_encoder	sc_encoder;
78 
79 	bool			sc_activated;
80 };
81 
82 #define	to_imx6_dwhdmi_softc(x)	container_of(x, struct imx6_dwhdmi_softc, sc_base)
83 #define	to_imx6_dwhdmi_encoder(x)	container_of(x, struct imx6_dwhdmi_softc, sc_encoder)
84 
85 static bool
imx6_dwhdmi_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)86 imx6_dwhdmi_encoder_mode_fixup(struct drm_encoder *encoder,
87     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
88 {
89 	return true;
90 }
91 
92 static void
imx6_dwhdmi_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted)93 imx6_dwhdmi_encoder_mode_set(struct drm_encoder *encoder,
94     struct drm_display_mode *mode, struct drm_display_mode *adjusted)
95 {
96 }
97 
98 static void
imx6_dwhdmi_encoder_enable(struct drm_encoder * encoder)99 imx6_dwhdmi_encoder_enable(struct drm_encoder *encoder)
100 {
101 }
102 
103 static void
imx6_dwhdmi_encoder_disable(struct drm_encoder * encoder)104 imx6_dwhdmi_encoder_disable(struct drm_encoder *encoder)
105 {
106 }
107 
108 static void
imx6_dwhdmi_encoder_prepare(struct drm_encoder * encoder)109 imx6_dwhdmi_encoder_prepare(struct drm_encoder *encoder)
110 {
111 }
112 
113 static void
imx6_dwhdmi_encoder_commit(struct drm_encoder * encoder)114 imx6_dwhdmi_encoder_commit(struct drm_encoder *encoder)
115 {
116 }
117 
118 static const struct drm_encoder_funcs imx6_dwhdmi_encoder_funcs = {
119 	.destroy = drm_encoder_cleanup,
120 };
121 
122 static const struct drm_encoder_helper_funcs imx6_dwhdmi_encoder_helper_funcs = {
123 	.prepare = imx6_dwhdmi_encoder_prepare,
124 	.mode_fixup = imx6_dwhdmi_encoder_mode_fixup,
125 	.mode_set = imx6_dwhdmi_encoder_mode_set,
126 	.enable = imx6_dwhdmi_encoder_enable,
127 	.disable = imx6_dwhdmi_encoder_disable,
128 	.commit = imx6_dwhdmi_encoder_commit,
129 };
130 
131 static int
imx6_dwhdmi_ep_activate(device_t dev,struct fdt_endpoint * ep,bool activate)132 imx6_dwhdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
133 {
134 	struct imx6_dwhdmi_softc * const sc = device_private(dev);
135 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
136 	struct fdt_endpoint *out_ep, *out_rep;
137 	struct drm_crtc *crtc;
138 	int error;
139 
140 	if (sc->sc_activated != false) {
141 		return 0;
142 	}
143 
144 	if (!activate)
145 		return EINVAL;
146 
147 	if (fdt_endpoint_port_index(ep) != DWHDMI_PORT_INPUT)
148 		return EINVAL;
149 
150 	switch (fdt_endpoint_type(in_ep)) {
151 	case EP_DRM_CRTC:
152 		crtc = fdt_endpoint_get_data(in_ep);
153 		break;
154 	default:
155 		crtc = NULL;
156 		break;
157 	}
158 
159 	if (crtc == NULL)
160 		return EINVAL;
161 
162 	sc->sc_encoder.possible_crtcs = 3; // 1U << drm_crtc_index(crtc); /* XXX */
163 	drm_encoder_init(crtc->dev, &sc->sc_encoder, &imx6_dwhdmi_encoder_funcs,
164 	    DRM_MODE_ENCODER_TMDS, NULL);
165 	drm_encoder_helper_add(&sc->sc_encoder, &imx6_dwhdmi_encoder_helper_funcs);
166 
167 	sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_HDMIA;
168 	error = dwhdmi_bind(&sc->sc_base, &sc->sc_encoder);
169 	if (error != 0)
170 		return error;
171 	sc->sc_activated = true;
172 
173 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, DWHDMI_PORT_OUTPUT, 0);
174 	if (out_ep != NULL) {
175 		/* Ignore downstream connectors, we have our own. */
176 		out_rep = fdt_endpoint_remote(out_ep);
177 		if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR)
178 			return 0;
179 
180 		error = fdt_endpoint_activate(out_ep, activate);
181 		if (error != 0)
182 			return error;
183 	}
184 
185 	return 0;
186 }
187 
188 static void *
imx6_dwhdmi_ep_get_data(device_t dev,struct fdt_endpoint * ep)189 imx6_dwhdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep)
190 {
191 	struct imx6_dwhdmi_softc * const sc = device_private(dev);
192 
193 	return &sc->sc_encoder;
194 }
195 
196 static audio_dai_tag_t
imx6_dwhdmi_dai_get_tag(device_t dev,const void * data,size_t len)197 imx6_dwhdmi_dai_get_tag(device_t dev, const void *data, size_t len)
198 {
199 	struct imx6_dwhdmi_softc * const sc = device_private(dev);
200 
201 	if (len != 4)
202 		return NULL;
203 
204 	return &sc->sc_base.sc_dai;
205 }
206 
207 static struct fdtbus_dai_controller_func imx6_dwhdmi_dai_funcs = {
208 	.get_tag = imx6_dwhdmi_dai_get_tag
209 };
210 
211 static int
imx6_dwhdmi_match(device_t parent,cfdata_t cf,void * aux)212 imx6_dwhdmi_match(device_t parent, cfdata_t cf, void *aux)
213 {
214 	struct fdt_attach_args * const faa = aux;
215 
216 	return of_compatible_match(faa->faa_phandle, compat_data);
217 }
218 
219 static void
imx6_dwhdmi_attach(device_t parent,device_t self,void * aux)220 imx6_dwhdmi_attach(device_t parent, device_t self, void *aux)
221 {
222 	struct imx6_dwhdmi_softc * const sc = device_private(self);
223 	struct fdt_attach_args * const faa = aux;
224 	const int phandle = faa->faa_phandle;
225 	bus_addr_t addr;
226 	bus_size_t size;
227 
228 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
229 		aprint_error(": couldn't get registers\n");
230 		return;
231 	}
232 
233 	/* Required */
234 	if (fdtbus_clock_enable(phandle, "iahb", true) != 0) {
235 		aprint_error(": couldn't enable iahb clock\n");
236 		return;
237 	}
238 
239 	/* Required */
240 	if (fdtbus_clock_enable(phandle, "isfr", true) != 0) {
241 		aprint_error(": couldn't enable isfr clock\n");
242 		return;
243 	}
244 
245 	sc->sc_base.sc_dev = self;
246 	sc->sc_base.sc_reg_width = 1;
247 	sc->sc_base.sc_bst = faa->faa_bst;
248 	if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) {
249 		aprint_error(": couldn't map registers\n");
250 		return;
251 	}
252 	sc->sc_phandle = faa->faa_phandle;
253 
254 	aprint_naive("\n");
255 	aprint_normal(": HDMI TX\n");
256 
257 	sc->sc_base.sc_ic = fdtbus_i2c_acquire(phandle, "ddc-i2c-bus");
258 	if (of_hasprop(phandle, "ddc-i2c-bus") && sc->sc_base.sc_ic == NULL) {
259 		aprint_error_dev(self, "couldn't find external I2C master\n");
260 		return;
261 	}
262 
263 	sc->sc_base.sc_flags |= DWHDMI_USE_INTERNAL_PHY;
264 	sc->sc_base.sc_detect = dwhdmi_phy_detect;
265 	sc->sc_base.sc_enable = dwhdmi_phy_enable;
266 	sc->sc_base.sc_disable = dwhdmi_phy_disable;
267 	sc->sc_base.sc_mode_set = dwhdmi_phy_mode_set;
268 	sc->sc_base.sc_mpll_config = imx6_dwhdmi_mpll_config;
269 	sc->sc_base.sc_phy_config = imx6_dwhdmi_phy_config;
270 
271 	if (dwhdmi_attach(&sc->sc_base) != 0) {
272 		aprint_error_dev(self, "failed to attach driver\n");
273 		return;
274 	}
275 
276 	sc->sc_ports.dp_ep_activate = imx6_dwhdmi_ep_activate;
277 	sc->sc_ports.dp_ep_get_data = imx6_dwhdmi_ep_get_data;
278 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
279 
280 	fdtbus_register_dai_controller(self, phandle, &imx6_dwhdmi_dai_funcs);
281 }
282 
283 CFATTACH_DECL_NEW(imx6_dwhdmi, sizeof(struct imx6_dwhdmi_softc),
284 	imx6_dwhdmi_match, imx6_dwhdmi_attach, NULL, NULL);
285