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