rk_dwhdmi.c revision 1.4.8.2 1 1.4.8.2 martin /* $NetBSD: rk_dwhdmi.c,v 1.4.8.2 2020/04/13 08:03:37 martin Exp $ */
2 1.4.8.2 martin
3 1.4.8.2 martin /*-
4 1.4.8.2 martin * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.4.8.2 martin * All rights reserved.
6 1.4.8.2 martin *
7 1.4.8.2 martin * Redistribution and use in source and binary forms, with or without
8 1.4.8.2 martin * modification, are permitted provided that the following conditions
9 1.4.8.2 martin * are met:
10 1.4.8.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.4.8.2 martin * notice, this list of conditions and the following disclaimer.
12 1.4.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.8.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.4.8.2 martin * documentation and/or other materials provided with the distribution.
15 1.4.8.2 martin *
16 1.4.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.4.8.2 martin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.4.8.2 martin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.4.8.2 martin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.4.8.2 martin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.4.8.2 martin * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.4.8.2 martin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.4.8.2 martin * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.4.8.2 martin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.4.8.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.4.8.2 martin * SUCH DAMAGE.
27 1.4.8.2 martin */
28 1.4.8.2 martin
29 1.4.8.2 martin #include <sys/cdefs.h>
30 1.4.8.2 martin __KERNEL_RCSID(0, "$NetBSD: rk_dwhdmi.c,v 1.4.8.2 2020/04/13 08:03:37 martin Exp $");
31 1.4.8.2 martin
32 1.4.8.2 martin #include <sys/param.h>
33 1.4.8.2 martin #include <sys/bus.h>
34 1.4.8.2 martin #include <sys/device.h>
35 1.4.8.2 martin #include <sys/intr.h>
36 1.4.8.2 martin #include <sys/systm.h>
37 1.4.8.2 martin #include <sys/kernel.h>
38 1.4.8.2 martin #include <sys/conf.h>
39 1.4.8.2 martin
40 1.4.8.2 martin #include <drm/drmP.h>
41 1.4.8.2 martin #include <drm/drm_crtc_helper.h>
42 1.4.8.2 martin
43 1.4.8.2 martin #include <dev/fdt/fdtvar.h>
44 1.4.8.2 martin #include <dev/fdt/fdt_port.h>
45 1.4.8.2 martin #include <dev/fdt/syscon.h>
46 1.4.8.2 martin
47 1.4.8.2 martin #include <dev/ic/dw_hdmi.h>
48 1.4.8.2 martin
49 1.4.8.2 martin #define RK3399_GRF_SOC_CON20 0x6250
50 1.4.8.2 martin #define HDMI_LCDC_SEL __BIT(6)
51 1.4.8.2 martin
52 1.4.8.2 martin static const struct dwhdmi_mpll_config rk_dwhdmi_mpll_config[] = {
53 1.4.8.2 martin { 40000, 0x00b3, 0x0000, 0x0018 },
54 1.4.8.2 martin { 65000, 0x0072, 0x0001, 0x0028 },
55 1.4.8.2 martin { 66000, 0x013e, 0x0003, 0x0038 },
56 1.4.8.2 martin { 83500, 0x0072, 0x0001, 0x0028 },
57 1.4.8.2 martin { 146250, 0x0051, 0x0002, 0x0038 },
58 1.4.8.2 martin { 148500, 0x0051, 0x0003, 0x0000 },
59 1.4.8.2 martin { 272000, 0x0040, 0x0003, 0x0000 },
60 1.4.8.2 martin { 340000, 0x0040, 0x0003, 0x0000 },
61 1.4.8.2 martin { 0, 0x0051, 0x0003, 0x0000 },
62 1.4.8.2 martin };
63 1.4.8.2 martin
64 1.4.8.2 martin static const struct dwhdmi_phy_config rk_dwhdmi_phy_config[] = {
65 1.4.8.2 martin { 74250, 0x8009, 0x0004, 0x0272 },
66 1.4.8.2 martin { 148500, 0x802b, 0x0004, 0x028d },
67 1.4.8.2 martin { 297000, 0x8039, 0x0005, 0x028d },
68 1.4.8.2 martin { 594000, 0x8039, 0x0000, 0x019d },
69 1.4.8.2 martin { 0, 0x0000, 0x0000, 0x0000 }
70 1.4.8.2 martin };
71 1.4.8.2 martin
72 1.4.8.2 martin enum {
73 1.4.8.2 martin DWHDMI_PORT_INPUT = 0,
74 1.4.8.2 martin DWHDMI_PORT_OUTPUT = 1,
75 1.4.8.2 martin };
76 1.4.8.2 martin
77 1.4.8.2 martin static const char * const compatible[] = {
78 1.4.8.2 martin "rockchip,rk3399-dw-hdmi",
79 1.4.8.2 martin NULL
80 1.4.8.2 martin };
81 1.4.8.2 martin
82 1.4.8.2 martin struct rk_dwhdmi_softc {
83 1.4.8.2 martin struct dwhdmi_softc sc_base;
84 1.4.8.2 martin int sc_phandle;
85 1.4.8.2 martin struct clk *sc_clk_vpll;
86 1.4.8.2 martin
87 1.4.8.2 martin struct fdt_device_ports sc_ports;
88 1.4.8.2 martin struct drm_display_mode sc_curmode;
89 1.4.8.2 martin struct drm_encoder sc_encoder;
90 1.4.8.2 martin struct syscon *sc_grf;
91 1.4.8.2 martin
92 1.4.8.2 martin bool sc_activated;
93 1.4.8.2 martin };
94 1.4.8.2 martin
95 1.4.8.2 martin #define to_rk_dwhdmi_softc(x) container_of(x, struct rk_dwhdmi_softc, sc_base)
96 1.4.8.2 martin #define to_rk_dwhdmi_encoder(x) container_of(x, struct rk_dwhdmi_softc, sc_encoder)
97 1.4.8.2 martin
98 1.4.8.2 martin static void
99 1.4.8.2 martin rk_dwhdmi_select_input(struct rk_dwhdmi_softc *sc, u_int crtc_index)
100 1.4.8.2 martin {
101 1.4.8.2 martin const uint32_t write_mask = HDMI_LCDC_SEL << 16;
102 1.4.8.2 martin const uint32_t write_val = crtc_index == 0 ? HDMI_LCDC_SEL : 0;
103 1.4.8.2 martin
104 1.4.8.2 martin syscon_lock(sc->sc_grf);
105 1.4.8.2 martin syscon_write_4(sc->sc_grf, RK3399_GRF_SOC_CON20, write_mask | write_val);
106 1.4.8.2 martin syscon_unlock(sc->sc_grf);
107 1.4.8.2 martin }
108 1.4.8.2 martin
109 1.4.8.2 martin static bool
110 1.4.8.2 martin rk_dwhdmi_encoder_mode_fixup(struct drm_encoder *encoder,
111 1.4.8.2 martin const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
112 1.4.8.2 martin {
113 1.4.8.2 martin return true;
114 1.4.8.2 martin }
115 1.4.8.2 martin
116 1.4.8.2 martin static void
117 1.4.8.2 martin rk_dwhdmi_encoder_mode_set(struct drm_encoder *encoder,
118 1.4.8.2 martin struct drm_display_mode *mode, struct drm_display_mode *adjusted)
119 1.4.8.2 martin {
120 1.4.8.2 martin }
121 1.4.8.2 martin
122 1.4.8.2 martin static void
123 1.4.8.2 martin rk_dwhdmi_encoder_enable(struct drm_encoder *encoder)
124 1.4.8.2 martin {
125 1.4.8.2 martin }
126 1.4.8.2 martin
127 1.4.8.2 martin static void
128 1.4.8.2 martin rk_dwhdmi_encoder_disable(struct drm_encoder *encoder)
129 1.4.8.2 martin {
130 1.4.8.2 martin }
131 1.4.8.2 martin
132 1.4.8.2 martin static void
133 1.4.8.2 martin rk_dwhdmi_encoder_prepare(struct drm_encoder *encoder)
134 1.4.8.2 martin {
135 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = to_rk_dwhdmi_encoder(encoder);
136 1.4.8.2 martin const u_int crtc_index = drm_crtc_index(encoder->crtc);
137 1.4.8.2 martin
138 1.4.8.2 martin rk_dwhdmi_select_input(sc, crtc_index);
139 1.4.8.2 martin }
140 1.4.8.2 martin
141 1.4.8.2 martin static void
142 1.4.8.2 martin rk_dwhdmi_encoder_commit(struct drm_encoder *encoder)
143 1.4.8.2 martin {
144 1.4.8.2 martin }
145 1.4.8.2 martin
146 1.4.8.2 martin static const struct drm_encoder_funcs rk_dwhdmi_encoder_funcs = {
147 1.4.8.2 martin .destroy = drm_encoder_cleanup,
148 1.4.8.2 martin };
149 1.4.8.2 martin
150 1.4.8.2 martin static const struct drm_encoder_helper_funcs rk_dwhdmi_encoder_helper_funcs = {
151 1.4.8.2 martin .prepare = rk_dwhdmi_encoder_prepare,
152 1.4.8.2 martin .mode_fixup = rk_dwhdmi_encoder_mode_fixup,
153 1.4.8.2 martin .mode_set = rk_dwhdmi_encoder_mode_set,
154 1.4.8.2 martin .enable = rk_dwhdmi_encoder_enable,
155 1.4.8.2 martin .disable = rk_dwhdmi_encoder_disable,
156 1.4.8.2 martin .commit = rk_dwhdmi_encoder_commit,
157 1.4.8.2 martin };
158 1.4.8.2 martin
159 1.4.8.2 martin static int
160 1.4.8.2 martin rk_dwhdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
161 1.4.8.2 martin {
162 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = device_private(dev);
163 1.4.8.2 martin struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
164 1.4.8.2 martin struct fdt_endpoint *out_ep, *out_rep;
165 1.4.8.2 martin struct drm_crtc *crtc;
166 1.4.8.2 martin int error;
167 1.4.8.2 martin
168 1.4.8.2 martin if (sc->sc_activated != false) {
169 1.4.8.2 martin return 0;
170 1.4.8.2 martin }
171 1.4.8.2 martin
172 1.4.8.2 martin if (!activate)
173 1.4.8.2 martin return EINVAL;
174 1.4.8.2 martin
175 1.4.8.2 martin if (fdt_endpoint_port_index(ep) != DWHDMI_PORT_INPUT)
176 1.4.8.2 martin return EINVAL;
177 1.4.8.2 martin
178 1.4.8.2 martin switch (fdt_endpoint_type(in_ep)) {
179 1.4.8.2 martin case EP_DRM_CRTC:
180 1.4.8.2 martin crtc = fdt_endpoint_get_data(in_ep);
181 1.4.8.2 martin break;
182 1.4.8.2 martin default:
183 1.4.8.2 martin crtc = NULL;
184 1.4.8.2 martin break;
185 1.4.8.2 martin }
186 1.4.8.2 martin
187 1.4.8.2 martin if (crtc == NULL)
188 1.4.8.2 martin return EINVAL;
189 1.4.8.2 martin
190 1.4.8.2 martin sc->sc_encoder.possible_crtcs = 3; // 1U << drm_crtc_index(crtc); /* XXX */
191 1.4.8.2 martin drm_encoder_init(crtc->dev, &sc->sc_encoder, &rk_dwhdmi_encoder_funcs,
192 1.4.8.2 martin DRM_MODE_ENCODER_TMDS);
193 1.4.8.2 martin drm_encoder_helper_add(&sc->sc_encoder, &rk_dwhdmi_encoder_helper_funcs);
194 1.4.8.2 martin
195 1.4.8.2 martin sc->sc_base.sc_connector.base.connector_type = DRM_MODE_CONNECTOR_HDMIA;
196 1.4.8.2 martin error = dwhdmi_bind(&sc->sc_base, &sc->sc_encoder);
197 1.4.8.2 martin if (error != 0)
198 1.4.8.2 martin return error;
199 1.4.8.2 martin sc->sc_activated = true;
200 1.4.8.2 martin
201 1.4.8.2 martin out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, DWHDMI_PORT_OUTPUT, 0);
202 1.4.8.2 martin if (out_ep != NULL) {
203 1.4.8.2 martin /* Ignore downstream connectors, we have our own. */
204 1.4.8.2 martin out_rep = fdt_endpoint_remote(out_ep);
205 1.4.8.2 martin if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR)
206 1.4.8.2 martin return 0;
207 1.4.8.2 martin
208 1.4.8.2 martin error = fdt_endpoint_activate(out_ep, activate);
209 1.4.8.2 martin if (error != 0)
210 1.4.8.2 martin return error;
211 1.4.8.2 martin }
212 1.4.8.2 martin
213 1.4.8.2 martin return 0;
214 1.4.8.2 martin }
215 1.4.8.2 martin
216 1.4.8.2 martin static void *
217 1.4.8.2 martin rk_dwhdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep)
218 1.4.8.2 martin {
219 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = device_private(dev);
220 1.4.8.2 martin
221 1.4.8.2 martin return &sc->sc_encoder;
222 1.4.8.2 martin }
223 1.4.8.2 martin
224 1.4.8.2 martin static void
225 1.4.8.2 martin rk_dwhdmi_enable(struct dwhdmi_softc *dsc)
226 1.4.8.2 martin {
227 1.4.8.2 martin
228 1.4.8.2 martin dwhdmi_phy_enable(dsc);
229 1.4.8.2 martin }
230 1.4.8.2 martin
231 1.4.8.2 martin static void
232 1.4.8.2 martin rk_dwhdmi_mode_set(struct dwhdmi_softc *dsc,
233 1.4.8.2 martin struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
234 1.4.8.2 martin {
235 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = to_rk_dwhdmi_softc(dsc);
236 1.4.8.2 martin int error;
237 1.4.8.2 martin
238 1.4.8.2 martin if (sc->sc_clk_vpll != NULL) {
239 1.4.8.2 martin error = clk_set_rate(sc->sc_clk_vpll, adjusted_mode->clock * 1000);
240 1.4.8.2 martin if (error != 0)
241 1.4.8.2 martin device_printf(dsc->sc_dev, "couldn't set pixel clock to %u Hz: %d\n",
242 1.4.8.2 martin adjusted_mode->clock * 1000, error);
243 1.4.8.2 martin }
244 1.4.8.2 martin
245 1.4.8.2 martin dwhdmi_phy_mode_set(dsc, mode, adjusted_mode);
246 1.4.8.2 martin }
247 1.4.8.2 martin
248 1.4.8.2 martin static audio_dai_tag_t
249 1.4.8.2 martin rk_dwhdmi_dai_get_tag(device_t dev, const void *data, size_t len)
250 1.4.8.2 martin {
251 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = device_private(dev);
252 1.4.8.2 martin
253 1.4.8.2 martin if (len != 4)
254 1.4.8.2 martin return NULL;
255 1.4.8.2 martin
256 1.4.8.2 martin return &sc->sc_base.sc_dai;
257 1.4.8.2 martin }
258 1.4.8.2 martin
259 1.4.8.2 martin static struct fdtbus_dai_controller_func rk_dwhdmi_dai_funcs = {
260 1.4.8.2 martin .get_tag = rk_dwhdmi_dai_get_tag
261 1.4.8.2 martin };
262 1.4.8.2 martin
263 1.4.8.2 martin static int
264 1.4.8.2 martin rk_dwhdmi_match(device_t parent, cfdata_t cf, void *aux)
265 1.4.8.2 martin {
266 1.4.8.2 martin struct fdt_attach_args * const faa = aux;
267 1.4.8.2 martin
268 1.4.8.2 martin return of_match_compatible(faa->faa_phandle, compatible);
269 1.4.8.2 martin }
270 1.4.8.2 martin
271 1.4.8.2 martin static void
272 1.4.8.2 martin rk_dwhdmi_attach(device_t parent, device_t self, void *aux)
273 1.4.8.2 martin {
274 1.4.8.2 martin struct rk_dwhdmi_softc * const sc = device_private(self);
275 1.4.8.2 martin struct fdt_attach_args * const faa = aux;
276 1.4.8.2 martin const int phandle = faa->faa_phandle;
277 1.4.8.2 martin bus_addr_t addr;
278 1.4.8.2 martin bus_size_t size;
279 1.4.8.2 martin
280 1.4.8.2 martin if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
281 1.4.8.2 martin aprint_error(": couldn't get registers\n");
282 1.4.8.2 martin return;
283 1.4.8.2 martin }
284 1.4.8.2 martin
285 1.4.8.2 martin /* Required */
286 1.4.8.2 martin if (fdtbus_clock_enable(phandle, "iahb", true) != 0) {
287 1.4.8.2 martin aprint_error(": couldn't enable iahb clock\n");
288 1.4.8.2 martin return;
289 1.4.8.2 martin }
290 1.4.8.2 martin
291 1.4.8.2 martin /* Required */
292 1.4.8.2 martin if (fdtbus_clock_enable(phandle, "isfr", true) != 0) {
293 1.4.8.2 martin aprint_error(": couldn't enable isfr clock\n");
294 1.4.8.2 martin return;
295 1.4.8.2 martin }
296 1.4.8.2 martin
297 1.4.8.2 martin /* Optional */
298 1.4.8.2 martin sc->sc_clk_vpll = fdtbus_clock_get(phandle, "vpll");
299 1.4.8.2 martin if (sc->sc_clk_vpll != NULL && clk_enable(sc->sc_clk_vpll) != 0) {
300 1.4.8.2 martin aprint_error(": couldn't enable vpll clock\n");
301 1.4.8.2 martin return;
302 1.4.8.2 martin }
303 1.4.8.2 martin
304 1.4.8.2 martin /* Optional */
305 1.4.8.2 martin if (fdtbus_clock_enable(phandle, "grf", false) != 0) {
306 1.4.8.2 martin aprint_error(": couldn't enable grf clock\n");
307 1.4.8.2 martin return;
308 1.4.8.2 martin }
309 1.4.8.2 martin
310 1.4.8.2 martin /* Optional */
311 1.4.8.2 martin if (fdtbus_clock_enable(phandle, "cec", false) != 0) {
312 1.4.8.2 martin aprint_error(": couldn't enable cec clock\n");
313 1.4.8.2 martin return;
314 1.4.8.2 martin }
315 1.4.8.2 martin
316 1.4.8.2 martin sc->sc_base.sc_dev = self;
317 1.4.8.2 martin if (of_getprop_uint32(phandle, "reg-io-width", &sc->sc_base.sc_reg_width) != 0)
318 1.4.8.2 martin sc->sc_base.sc_reg_width = 4;
319 1.4.8.2 martin sc->sc_base.sc_bst = faa->faa_bst;
320 1.4.8.2 martin if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) {
321 1.4.8.2 martin aprint_error(": couldn't map registers\n");
322 1.4.8.2 martin return;
323 1.4.8.2 martin }
324 1.4.8.2 martin sc->sc_phandle = faa->faa_phandle;
325 1.4.8.2 martin sc->sc_grf = fdtbus_syscon_acquire(phandle, "rockchip,grf");
326 1.4.8.2 martin if (sc->sc_grf == NULL) {
327 1.4.8.2 martin aprint_error(": couldn't get grf syscon\n");
328 1.4.8.2 martin return;
329 1.4.8.2 martin }
330 1.4.8.2 martin
331 1.4.8.2 martin aprint_naive("\n");
332 1.4.8.2 martin aprint_normal(": HDMI TX\n");
333 1.4.8.2 martin
334 1.4.8.2 martin sc->sc_base.sc_ic = fdtbus_i2c_acquire(phandle, "ddc-i2c-bus");
335 1.4.8.2 martin if (of_hasprop(phandle, "ddc-i2c-bus") && sc->sc_base.sc_ic == NULL) {
336 1.4.8.2 martin aprint_error_dev(self, "couldn't find external I2C master\n");
337 1.4.8.2 martin return;
338 1.4.8.2 martin }
339 1.4.8.2 martin
340 1.4.8.2 martin sc->sc_base.sc_flags |= DWHDMI_USE_INTERNAL_PHY;
341 1.4.8.2 martin sc->sc_base.sc_detect = dwhdmi_phy_detect;
342 1.4.8.2 martin sc->sc_base.sc_enable = rk_dwhdmi_enable;
343 1.4.8.2 martin sc->sc_base.sc_disable = dwhdmi_phy_disable;
344 1.4.8.2 martin sc->sc_base.sc_mode_set = rk_dwhdmi_mode_set;
345 1.4.8.2 martin sc->sc_base.sc_mpll_config = rk_dwhdmi_mpll_config;
346 1.4.8.2 martin sc->sc_base.sc_phy_config = rk_dwhdmi_phy_config;
347 1.4.8.2 martin
348 1.4.8.2 martin if (dwhdmi_attach(&sc->sc_base) != 0) {
349 1.4.8.2 martin aprint_error_dev(self, "failed to attach driver\n");
350 1.4.8.2 martin return;
351 1.4.8.2 martin }
352 1.4.8.2 martin
353 1.4.8.2 martin sc->sc_ports.dp_ep_activate = rk_dwhdmi_ep_activate;
354 1.4.8.2 martin sc->sc_ports.dp_ep_get_data = rk_dwhdmi_ep_get_data;
355 1.4.8.2 martin fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
356 1.4.8.2 martin
357 1.4.8.2 martin fdtbus_register_dai_controller(self, phandle, &rk_dwhdmi_dai_funcs);
358 1.4.8.2 martin }
359 1.4.8.2 martin
360 1.4.8.2 martin CFATTACH_DECL_NEW(rk_dwhdmi, sizeof(struct rk_dwhdmi_softc),
361 1.4.8.2 martin rk_dwhdmi_match, rk_dwhdmi_attach, NULL, NULL);
362