sunxi_dwhdmi.c revision 1.3.4.2 1 1.3.4.2 christos /* $NetBSD: sunxi_dwhdmi.c,v 1.3.4.2 2019/06/10 22:05:56 christos Exp $ */
2 1.3.4.2 christos
3 1.3.4.2 christos /*-
4 1.3.4.2 christos * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.3.4.2 christos * All rights reserved.
6 1.3.4.2 christos *
7 1.3.4.2 christos * Redistribution and use in source and binary forms, with or without
8 1.3.4.2 christos * modification, are permitted provided that the following conditions
9 1.3.4.2 christos * are met:
10 1.3.4.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.3.4.2 christos * notice, this list of conditions and the following disclaimer.
12 1.3.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.4.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.3.4.2 christos * documentation and/or other materials provided with the distribution.
15 1.3.4.2 christos *
16 1.3.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.3.4.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.3.4.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.3.4.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.3.4.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.3.4.2 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.3.4.2 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.3.4.2 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.3.4.2 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.3.4.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.3.4.2 christos * SUCH DAMAGE.
27 1.3.4.2 christos */
28 1.3.4.2 christos
29 1.3.4.2 christos #include <sys/cdefs.h>
30 1.3.4.2 christos __KERNEL_RCSID(0, "$NetBSD: sunxi_dwhdmi.c,v 1.3.4.2 2019/06/10 22:05:56 christos Exp $");
31 1.3.4.2 christos
32 1.3.4.2 christos #include <sys/param.h>
33 1.3.4.2 christos #include <sys/bus.h>
34 1.3.4.2 christos #include <sys/device.h>
35 1.3.4.2 christos #include <sys/intr.h>
36 1.3.4.2 christos #include <sys/systm.h>
37 1.3.4.2 christos #include <sys/kernel.h>
38 1.3.4.2 christos #include <sys/conf.h>
39 1.3.4.2 christos
40 1.3.4.2 christos #include <drm/drmP.h>
41 1.3.4.2 christos
42 1.3.4.2 christos #include <dev/fdt/fdtvar.h>
43 1.3.4.2 christos #include <dev/fdt/fdt_port.h>
44 1.3.4.2 christos
45 1.3.4.2 christos #include <dev/ic/dw_hdmi.h>
46 1.3.4.2 christos
47 1.3.4.2 christos #include <arm/sunxi/sunxi_hdmiphy.h>
48 1.3.4.2 christos
49 1.3.4.2 christos enum {
50 1.3.4.2 christos DWHDMI_PORT_INPUT = 0,
51 1.3.4.2 christos DWHDMI_PORT_OUTPUT = 1,
52 1.3.4.2 christos };
53 1.3.4.2 christos
54 1.3.4.2 christos static const char * const compatible[] = {
55 1.3.4.2 christos "allwinner,sun8i-h3-dw-hdmi",
56 1.3.4.2 christos "allwinner,sun50i-a64-dw-hdmi",
57 1.3.4.2 christos NULL
58 1.3.4.2 christos };
59 1.3.4.2 christos
60 1.3.4.2 christos struct sunxi_dwhdmi_softc {
61 1.3.4.2 christos struct dwhdmi_softc sc_base;
62 1.3.4.2 christos int sc_phandle;
63 1.3.4.2 christos struct fdtbus_phy *sc_phy;
64 1.3.4.2 christos struct fdtbus_regulator *sc_regulator;
65 1.3.4.2 christos
66 1.3.4.2 christos struct fdt_device_ports sc_ports;
67 1.3.4.2 christos struct drm_display_mode sc_curmode;
68 1.3.4.2 christos };
69 1.3.4.2 christos
70 1.3.4.2 christos #define to_sunxi_dwhdmi_softc(x) container_of(x, struct sunxi_dwhdmi_softc, sc_base)
71 1.3.4.2 christos
72 1.3.4.2 christos static int
73 1.3.4.2 christos sunxi_dwhdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
74 1.3.4.2 christos {
75 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = device_private(dev);
76 1.3.4.2 christos struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
77 1.3.4.2 christos struct fdt_endpoint *out_ep, *out_rep;
78 1.3.4.2 christos struct drm_encoder *encoder;
79 1.3.4.2 christos struct drm_bridge *bridge;
80 1.3.4.2 christos int error;
81 1.3.4.2 christos
82 1.3.4.2 christos if (!activate)
83 1.3.4.2 christos return EINVAL;
84 1.3.4.2 christos
85 1.3.4.2 christos if (fdt_endpoint_port_index(ep) != DWHDMI_PORT_INPUT)
86 1.3.4.2 christos return EINVAL;
87 1.3.4.2 christos
88 1.3.4.2 christos switch (fdt_endpoint_type(in_ep)) {
89 1.3.4.2 christos case EP_DRM_ENCODER:
90 1.3.4.2 christos encoder = fdt_endpoint_get_data(in_ep);
91 1.3.4.2 christos break;
92 1.3.4.2 christos case EP_DRM_BRIDGE:
93 1.3.4.2 christos bridge = fdt_endpoint_get_data(in_ep);
94 1.3.4.2 christos encoder = bridge->encoder;
95 1.3.4.2 christos break;
96 1.3.4.2 christos default:
97 1.3.4.2 christos encoder = NULL;
98 1.3.4.2 christos break;
99 1.3.4.2 christos }
100 1.3.4.2 christos
101 1.3.4.2 christos if (encoder == NULL)
102 1.3.4.2 christos return EINVAL;
103 1.3.4.2 christos
104 1.3.4.2 christos sc->sc_phy = fdtbus_phy_get(sc->sc_phandle, "hdmi-phy");
105 1.3.4.2 christos if (sc->sc_phy == NULL) {
106 1.3.4.2 christos device_printf(dev, "couldn't find hdmi-phy\n");
107 1.3.4.2 christos return ENXIO;
108 1.3.4.2 christos }
109 1.3.4.2 christos
110 1.3.4.2 christos sc->sc_regulator = fdtbus_regulator_acquire(sc->sc_phandle, "hvcc-supply");
111 1.3.4.2 christos if (sc->sc_regulator != NULL) {
112 1.3.4.2 christos error = fdtbus_regulator_enable(sc->sc_regulator);
113 1.3.4.2 christos if (error != 0) {
114 1.3.4.2 christos device_printf(dev, "couldn't enable supply\n");
115 1.3.4.2 christos return error;
116 1.3.4.2 christos }
117 1.3.4.2 christos }
118 1.3.4.2 christos
119 1.3.4.2 christos error = dwhdmi_bind(&sc->sc_base, encoder);
120 1.3.4.2 christos if (error != 0)
121 1.3.4.2 christos return error;
122 1.3.4.2 christos
123 1.3.4.2 christos out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, DWHDMI_PORT_OUTPUT, 0);
124 1.3.4.2 christos if (out_ep != NULL) {
125 1.3.4.2 christos /* Ignore downstream connectors, we have our own. */
126 1.3.4.2 christos out_rep = fdt_endpoint_remote(out_ep);
127 1.3.4.2 christos if (out_rep != NULL && fdt_endpoint_type(out_rep) == EP_DRM_CONNECTOR)
128 1.3.4.2 christos return 0;
129 1.3.4.2 christos
130 1.3.4.2 christos error = fdt_endpoint_activate(out_ep, activate);
131 1.3.4.2 christos if (error != 0)
132 1.3.4.2 christos return error;
133 1.3.4.2 christos }
134 1.3.4.2 christos
135 1.3.4.2 christos return 0;
136 1.3.4.2 christos }
137 1.3.4.2 christos
138 1.3.4.2 christos static void *
139 1.3.4.2 christos sunxi_dwhdmi_ep_get_data(device_t dev, struct fdt_endpoint *ep)
140 1.3.4.2 christos {
141 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = device_private(dev);
142 1.3.4.2 christos
143 1.3.4.2 christos return &sc->sc_base.sc_bridge;
144 1.3.4.2 christos }
145 1.3.4.2 christos
146 1.3.4.2 christos static enum drm_connector_status
147 1.3.4.2 christos sunxi_dwhdmi_detect(struct dwhdmi_softc *dsc, bool force)
148 1.3.4.2 christos {
149 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
150 1.3.4.2 christos
151 1.3.4.2 christos KASSERT(sc->sc_phy != NULL);
152 1.3.4.2 christos
153 1.3.4.2 christos if (sunxi_hdmiphy_detect(sc->sc_phy, force))
154 1.3.4.2 christos return connector_status_connected;
155 1.3.4.2 christos else
156 1.3.4.2 christos return connector_status_disconnected;
157 1.3.4.2 christos }
158 1.3.4.2 christos
159 1.3.4.2 christos static void
160 1.3.4.2 christos sunxi_dwhdmi_enable(struct dwhdmi_softc *dsc)
161 1.3.4.2 christos {
162 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
163 1.3.4.2 christos int error;
164 1.3.4.2 christos
165 1.3.4.2 christos KASSERT(sc->sc_phy != NULL);
166 1.3.4.2 christos
167 1.3.4.2 christos error = fdtbus_phy_enable(sc->sc_phy, true);
168 1.3.4.2 christos if (error != 0) {
169 1.3.4.2 christos device_printf(dsc->sc_dev, "failed to enable phy: %d\n", error);
170 1.3.4.2 christos return;
171 1.3.4.2 christos }
172 1.3.4.2 christos
173 1.3.4.2 christos error = sunxi_hdmiphy_config(sc->sc_phy, &sc->sc_curmode);
174 1.3.4.2 christos if (error != 0)
175 1.3.4.2 christos device_printf(dsc->sc_dev, "failed to configure phy: %d\n", error);
176 1.3.4.2 christos }
177 1.3.4.2 christos
178 1.3.4.2 christos static void
179 1.3.4.2 christos sunxi_dwhdmi_disable(struct dwhdmi_softc *dsc)
180 1.3.4.2 christos {
181 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
182 1.3.4.2 christos int error;
183 1.3.4.2 christos
184 1.3.4.2 christos KASSERT(sc->sc_phy != NULL);
185 1.3.4.2 christos
186 1.3.4.2 christos error = fdtbus_phy_enable(sc->sc_phy, false);
187 1.3.4.2 christos if (error != 0)
188 1.3.4.2 christos device_printf(dsc->sc_dev, "failed to disable phy\n");
189 1.3.4.2 christos }
190 1.3.4.2 christos
191 1.3.4.2 christos static void
192 1.3.4.2 christos sunxi_dwhdmi_mode_set(struct dwhdmi_softc *dsc, struct drm_display_mode *mode,
193 1.3.4.2 christos struct drm_display_mode *adjusted_mode)
194 1.3.4.2 christos {
195 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = to_sunxi_dwhdmi_softc(dsc);
196 1.3.4.2 christos
197 1.3.4.2 christos sc->sc_curmode = *adjusted_mode;
198 1.3.4.2 christos }
199 1.3.4.2 christos
200 1.3.4.2 christos static int
201 1.3.4.2 christos sunxi_dwhdmi_match(device_t parent, cfdata_t cf, void *aux)
202 1.3.4.2 christos {
203 1.3.4.2 christos struct fdt_attach_args * const faa = aux;
204 1.3.4.2 christos
205 1.3.4.2 christos return of_match_compatible(faa->faa_phandle, compatible);
206 1.3.4.2 christos }
207 1.3.4.2 christos
208 1.3.4.2 christos static void
209 1.3.4.2 christos sunxi_dwhdmi_attach(device_t parent, device_t self, void *aux)
210 1.3.4.2 christos {
211 1.3.4.2 christos struct sunxi_dwhdmi_softc * const sc = device_private(self);
212 1.3.4.2 christos struct fdt_attach_args * const faa = aux;
213 1.3.4.2 christos const int phandle = faa->faa_phandle;
214 1.3.4.2 christos struct clk *clk_iahb, *clk_isfr;
215 1.3.4.2 christos struct fdtbus_reset *rst;
216 1.3.4.2 christos bus_addr_t addr;
217 1.3.4.2 christos bus_size_t size;
218 1.3.4.2 christos
219 1.3.4.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
220 1.3.4.2 christos aprint_error(": couldn't get registers\n");
221 1.3.4.2 christos return;
222 1.3.4.2 christos }
223 1.3.4.2 christos
224 1.3.4.2 christos rst = fdtbus_reset_get(phandle, "ctrl");
225 1.3.4.2 christos if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
226 1.3.4.2 christos aprint_error(": couldn't de-assert reset\n");
227 1.3.4.2 christos return;
228 1.3.4.2 christos }
229 1.3.4.2 christos
230 1.3.4.2 christos clk_iahb = fdtbus_clock_get(phandle, "iahb");
231 1.3.4.2 christos if (clk_iahb == NULL || clk_enable(clk_iahb) != 0) {
232 1.3.4.2 christos aprint_error(": couldn't enable iahb clock\n");
233 1.3.4.2 christos return;
234 1.3.4.2 christos }
235 1.3.4.2 christos
236 1.3.4.2 christos clk_isfr = fdtbus_clock_get(phandle, "isfr");
237 1.3.4.2 christos if (clk_isfr == NULL || clk_enable(clk_isfr) != 0) {
238 1.3.4.2 christos aprint_error(": couldn't enable isfr clock\n");
239 1.3.4.2 christos return;
240 1.3.4.2 christos }
241 1.3.4.2 christos
242 1.3.4.2 christos sc->sc_base.sc_dev = self;
243 1.3.4.2 christos sc->sc_base.sc_reg_width = 1;
244 1.3.4.2 christos sc->sc_base.sc_bst = faa->faa_bst;
245 1.3.4.2 christos if (bus_space_map(sc->sc_base.sc_bst, addr, size, 0, &sc->sc_base.sc_bsh) != 0) {
246 1.3.4.2 christos aprint_error(": couldn't map registers\n");
247 1.3.4.2 christos return;
248 1.3.4.2 christos }
249 1.3.4.2 christos sc->sc_base.sc_detect = sunxi_dwhdmi_detect;
250 1.3.4.2 christos sc->sc_base.sc_enable = sunxi_dwhdmi_enable;
251 1.3.4.2 christos sc->sc_base.sc_disable = sunxi_dwhdmi_disable;
252 1.3.4.2 christos sc->sc_base.sc_mode_set = sunxi_dwhdmi_mode_set;
253 1.3.4.2 christos sc->sc_phandle = faa->faa_phandle;
254 1.3.4.2 christos
255 1.3.4.2 christos aprint_naive("\n");
256 1.3.4.2 christos aprint_normal(": HDMI TX\n");
257 1.3.4.2 christos
258 1.3.4.2 christos if (dwhdmi_attach(&sc->sc_base) != 0) {
259 1.3.4.2 christos aprint_error_dev(self, "failed to attach driver\n");
260 1.3.4.2 christos return;
261 1.3.4.2 christos }
262 1.3.4.2 christos
263 1.3.4.2 christos sc->sc_ports.dp_ep_activate = sunxi_dwhdmi_ep_activate;
264 1.3.4.2 christos sc->sc_ports.dp_ep_get_data = sunxi_dwhdmi_ep_get_data;
265 1.3.4.2 christos fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_BRIDGE);
266 1.3.4.2 christos }
267 1.3.4.2 christos
268 1.3.4.2 christos CFATTACH_DECL_NEW(sunxi_dwhdmi, sizeof(struct sunxi_dwhdmi_softc),
269 1.3.4.2 christos sunxi_dwhdmi_match, sunxi_dwhdmi_attach, NULL, NULL);
270