sunxi_lcdc.c revision 1.1 1 1.1 jmcneill /* $NetBSD: sunxi_lcdc.c,v 1.1 2019/01/30 01:24:00 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_lcdc.c,v 1.1 2019/01/30 01:24:00 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/conf.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <drm/drmP.h>
41 1.1 jmcneill #include <drm/drm_crtc_helper.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/fdt/fdtvar.h>
44 1.1 jmcneill #include <dev/fdt/fdt_port.h>
45 1.1 jmcneill
46 1.1 jmcneill #define TCON_GCTL_REG 0x000
47 1.1 jmcneill #define TCON_GCTL_TCON_EN __BIT(31)
48 1.1 jmcneill #define TCON_GCTL_GAMMA_EN __BIT(30)
49 1.1 jmcneill #define TCON_GCTL_IO_MAP_SEL __BIT(0)
50 1.1 jmcneill
51 1.1 jmcneill #define TCON_GINT0_REG 0x004
52 1.1 jmcneill #define TCON_GINT1_REG 0x008
53 1.1 jmcneill #define TCON_GINT1_TCON1_LINE_INT_NUM __BITS(11,0)
54 1.1 jmcneill
55 1.1 jmcneill #define TCON1_CTL_REG 0x090
56 1.1 jmcneill #define TCON1_CTL_TCON1_EN __BIT(31)
57 1.1 jmcneill #define TCON1_CTL_START_DELAY __BITS(8,4)
58 1.1 jmcneill #define TCON1_CTL_TCON1_SRC_SEL __BIT(1)
59 1.1 jmcneill #define TCON1_BASIC0_REG 0x094
60 1.1 jmcneill #define TCON1_BASIC1_REG 0x098
61 1.1 jmcneill #define TCON1_BASIC2_REG 0x09c
62 1.1 jmcneill #define TCON1_BASIC3_REG 0x0a0
63 1.1 jmcneill #define TCON1_BASIC4_REG 0x0a4
64 1.1 jmcneill #define TCON1_BASIC5_REG 0x0a8
65 1.1 jmcneill
66 1.1 jmcneill #define TCON1_IO_POL_REG 0x0f0
67 1.1 jmcneill #define TCON1_IO_POL_IO3_INV __BIT(27)
68 1.1 jmcneill #define TCON1_IO_POL_IO2_INV __BIT(26)
69 1.1 jmcneill #define TCON1_IO_POL_IO1_INV __BIT(25)
70 1.1 jmcneill #define TCON1_IO_POL_IO0_INV __BIT(24)
71 1.1 jmcneill #define TCON1_IO_POL_DATA_INV __BITS(23,0)
72 1.1 jmcneill #define TCON1_IO_TRI_REG 0x0f4
73 1.1 jmcneill
74 1.1 jmcneill enum {
75 1.1 jmcneill MIXER_PORT_INPUT = 0,
76 1.1 jmcneill MIXER_PORT_OUTPUT = 1,
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.1 jmcneill static const char * const compatible[] = {
80 1.1 jmcneill "allwinner,sun50i-a64-tcon-lcd",
81 1.1 jmcneill "allwinner,sun50i-a64-tcon-tv",
82 1.1 jmcneill NULL
83 1.1 jmcneill };
84 1.1 jmcneill
85 1.1 jmcneill struct sunxi_lcdc_softc;
86 1.1 jmcneill
87 1.1 jmcneill struct sunxi_lcdc_encoder {
88 1.1 jmcneill struct drm_encoder base;
89 1.1 jmcneill struct sunxi_lcdc_softc *sc;
90 1.1 jmcneill struct drm_display_mode curmode;
91 1.1 jmcneill };
92 1.1 jmcneill
93 1.1 jmcneill struct sunxi_lcdc_softc {
94 1.1 jmcneill device_t sc_dev;
95 1.1 jmcneill bus_space_tag_t sc_bst;
96 1.1 jmcneill bus_space_handle_t sc_bsh;
97 1.1 jmcneill int sc_phandle;
98 1.1 jmcneill
99 1.1 jmcneill struct clk *sc_clk_ch[2];
100 1.1 jmcneill
101 1.1 jmcneill struct sunxi_lcdc_encoder sc_encoder;
102 1.1 jmcneill struct drm_connector sc_connector;
103 1.1 jmcneill
104 1.1 jmcneill struct fdt_device_ports sc_ports;
105 1.1 jmcneill };
106 1.1 jmcneill
107 1.1 jmcneill #define to_sunxi_lcdc_encoder(x) container_of(x, struct sunxi_lcdc_encoder, base)
108 1.1 jmcneill
109 1.1 jmcneill #define TCON_READ(sc, reg) \
110 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
111 1.1 jmcneill #define TCON_WRITE(sc, reg, val) \
112 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
113 1.1 jmcneill
114 1.1 jmcneill static void
115 1.1 jmcneill sunxi_lcdc_destroy(struct drm_encoder *encoder)
116 1.1 jmcneill {
117 1.1 jmcneill }
118 1.1 jmcneill
119 1.1 jmcneill static const struct drm_encoder_funcs sunxi_lcdc_funcs = {
120 1.1 jmcneill .destroy = sunxi_lcdc_destroy,
121 1.1 jmcneill };
122 1.1 jmcneill
123 1.1 jmcneill static void
124 1.1 jmcneill sunxi_lcdc_tcon1_dpms(struct drm_encoder *encoder, int mode)
125 1.1 jmcneill {
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill static bool
129 1.1 jmcneill sunxi_lcdc_tcon1_mode_fixup(struct drm_encoder *encoder,
130 1.1 jmcneill const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
131 1.1 jmcneill {
132 1.1 jmcneill return true;
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static void
136 1.1 jmcneill sunxi_lcdc_tcon1_mode_set(struct drm_encoder *encoder,
137 1.1 jmcneill struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
138 1.1 jmcneill {
139 1.1 jmcneill struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
140 1.1 jmcneill
141 1.1 jmcneill lcdc_encoder->curmode = *adjusted_mode;
142 1.1 jmcneill }
143 1.1 jmcneill
144 1.1 jmcneill static void
145 1.1 jmcneill sunxi_lcdc_tcon1_prepare(struct drm_encoder *encoder)
146 1.1 jmcneill {
147 1.1 jmcneill struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
148 1.1 jmcneill struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
149 1.1 jmcneill uint32_t val;
150 1.1 jmcneill
151 1.1 jmcneill val = TCON_READ(sc, TCON_GCTL_REG);
152 1.1 jmcneill val |= TCON_GCTL_TCON_EN;
153 1.1 jmcneill TCON_WRITE(sc, TCON_GCTL_REG, val);
154 1.1 jmcneill
155 1.1 jmcneill TCON_WRITE(sc, TCON1_IO_POL_REG, 0);
156 1.1 jmcneill TCON_WRITE(sc, TCON1_IO_TRI_REG, 0xffffffff);
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill static void
160 1.1 jmcneill sunxi_lcdc_tcon1_commit(struct drm_encoder *encoder)
161 1.1 jmcneill {
162 1.1 jmcneill struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
163 1.1 jmcneill struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
164 1.1 jmcneill struct drm_display_mode *mode = &lcdc_encoder->curmode;
165 1.1 jmcneill uint32_t val;
166 1.1 jmcneill int error;
167 1.1 jmcneill
168 1.1 jmcneill const u_int interlace_p = (mode->flags & DRM_MODE_FLAG_INTERLACE) != 0;
169 1.1 jmcneill const u_int hspw = mode->hsync_end - mode->hsync_start;
170 1.1 jmcneill const u_int hbp = mode->htotal - mode->hsync_start;
171 1.1 jmcneill const u_int vspw = mode->vsync_end - mode->vsync_start;
172 1.1 jmcneill const u_int vbp = mode->vtotal - mode->vsync_start;
173 1.1 jmcneill const u_int vblank_len =
174 1.1 jmcneill ((mode->vtotal << interlace_p) >> 1) - mode->vdisplay - 2;
175 1.1 jmcneill const u_int start_delay =
176 1.1 jmcneill vblank_len >= 32 ? 30 : vblank_len - 2;
177 1.1 jmcneill
178 1.1 jmcneill val = TCON1_CTL_TCON1_EN |
179 1.1 jmcneill __SHIFTIN(start_delay, TCON1_CTL_START_DELAY);
180 1.1 jmcneill TCON_WRITE(sc, TCON1_CTL_REG, val);
181 1.1 jmcneill
182 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC0_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
183 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC1_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
184 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC2_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
185 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC3_REG, ((mode->htotal - 1) << 16) | (hbp - 1));
186 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC4_REG, ((mode->vtotal * 2) << 16) | (vbp - 1));
187 1.1 jmcneill TCON_WRITE(sc, TCON1_BASIC5_REG, ((hspw - 1) << 16) | (vspw - 1));
188 1.1 jmcneill
189 1.1 jmcneill TCON_WRITE(sc, TCON_GINT1_REG,
190 1.1 jmcneill __SHIFTIN(start_delay + 2, TCON_GINT1_TCON1_LINE_INT_NUM));
191 1.1 jmcneill
192 1.1 jmcneill if (sc->sc_clk_ch[1] != NULL) {
193 1.1 jmcneill error = clk_set_rate(sc->sc_clk_ch[1], mode->crtc_clock * 1000);
194 1.1 jmcneill if (error != 0) {
195 1.1 jmcneill device_printf(sc->sc_dev, "failed to set CH1 PLL rate to %u Hz: %d\n",
196 1.1 jmcneill mode->crtc_clock * 1000, error);
197 1.1 jmcneill return;
198 1.1 jmcneill }
199 1.1 jmcneill error = clk_enable(sc->sc_clk_ch[1]);
200 1.1 jmcneill if (error != 0) {
201 1.1 jmcneill device_printf(sc->sc_dev, "failed to enable CH1 PLL: %d\n", error);
202 1.1 jmcneill return;
203 1.1 jmcneill }
204 1.1 jmcneill } else {
205 1.1 jmcneill device_printf(sc->sc_dev, "no CH1 PLL configured\n");
206 1.1 jmcneill }
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill static const struct drm_encoder_helper_funcs sunxi_lcdc_tcon1_helper_funcs = {
210 1.1 jmcneill .dpms = sunxi_lcdc_tcon1_dpms,
211 1.1 jmcneill .mode_fixup = sunxi_lcdc_tcon1_mode_fixup,
212 1.1 jmcneill .prepare = sunxi_lcdc_tcon1_prepare,
213 1.1 jmcneill .commit = sunxi_lcdc_tcon1_commit,
214 1.1 jmcneill .mode_set = sunxi_lcdc_tcon1_mode_set,
215 1.1 jmcneill };
216 1.1 jmcneill
217 1.1 jmcneill static int
218 1.1 jmcneill sunxi_lcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
219 1.1 jmcneill {
220 1.1 jmcneill struct sunxi_lcdc_softc * const sc = device_private(dev);
221 1.1 jmcneill struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
222 1.1 jmcneill struct fdt_endpoint *out_ep;
223 1.1 jmcneill struct drm_crtc *crtc;
224 1.1 jmcneill int error;
225 1.1 jmcneill
226 1.1 jmcneill if (!activate)
227 1.1 jmcneill return EINVAL;
228 1.1 jmcneill
229 1.1 jmcneill if (fdt_endpoint_port_index(ep) != MIXER_PORT_INPUT)
230 1.1 jmcneill return EINVAL;
231 1.1 jmcneill
232 1.1 jmcneill if (fdt_endpoint_type(in_ep) != EP_DRM_CRTC)
233 1.1 jmcneill return EINVAL;
234 1.1 jmcneill
235 1.1 jmcneill crtc = fdt_endpoint_get_data(in_ep);
236 1.1 jmcneill
237 1.1 jmcneill sc->sc_encoder.sc = sc;
238 1.1 jmcneill
239 1.1 jmcneill out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, MIXER_PORT_OUTPUT, 1);
240 1.1 jmcneill if (out_ep != NULL) {
241 1.1 jmcneill drm_encoder_init(crtc->dev, &sc->sc_encoder.base, &sunxi_lcdc_funcs,
242 1.1 jmcneill DRM_MODE_ENCODER_TMDS);
243 1.1 jmcneill drm_encoder_helper_add(&sc->sc_encoder.base, &sunxi_lcdc_tcon1_helper_funcs);
244 1.1 jmcneill
245 1.1 jmcneill error = fdt_endpoint_activate(out_ep, activate);
246 1.1 jmcneill if (error != 0)
247 1.1 jmcneill return error;
248 1.1 jmcneill sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(crtc);
249 1.1 jmcneill }
250 1.1 jmcneill
251 1.1 jmcneill return 0;
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill static void *
255 1.1 jmcneill sunxi_lcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
256 1.1 jmcneill {
257 1.1 jmcneill struct sunxi_lcdc_softc * const sc = device_private(dev);
258 1.1 jmcneill
259 1.1 jmcneill return &sc->sc_encoder;
260 1.1 jmcneill }
261 1.1 jmcneill
262 1.1 jmcneill static int
263 1.1 jmcneill sunxi_lcdc_match(device_t parent, cfdata_t cf, void *aux)
264 1.1 jmcneill {
265 1.1 jmcneill struct fdt_attach_args * const faa = aux;
266 1.1 jmcneill
267 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
268 1.1 jmcneill }
269 1.1 jmcneill
270 1.1 jmcneill static void
271 1.1 jmcneill sunxi_lcdc_attach(device_t parent, device_t self, void *aux)
272 1.1 jmcneill {
273 1.1 jmcneill struct sunxi_lcdc_softc * const sc = device_private(self);
274 1.1 jmcneill struct fdt_attach_args * const faa = aux;
275 1.1 jmcneill const int phandle = faa->faa_phandle;
276 1.1 jmcneill struct fdtbus_reset *rst;
277 1.1 jmcneill struct clk *clk;
278 1.1 jmcneill bus_addr_t addr;
279 1.1 jmcneill bus_size_t size;
280 1.1 jmcneill
281 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
282 1.1 jmcneill aprint_error(": couldn't get registers\n");
283 1.1 jmcneill return;
284 1.1 jmcneill }
285 1.1 jmcneill
286 1.1 jmcneill rst = fdtbus_reset_get(phandle, "lcd");
287 1.1 jmcneill if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
288 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
289 1.1 jmcneill return;
290 1.1 jmcneill }
291 1.1 jmcneill
292 1.1 jmcneill clk = fdtbus_clock_get(phandle, "ahb");
293 1.1 jmcneill if (clk == NULL || clk_enable(clk) != 0) {
294 1.1 jmcneill aprint_error(": couldn't enable bus clock\n");
295 1.1 jmcneill return;
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill sc->sc_dev = self;
299 1.1 jmcneill sc->sc_bst = faa->faa_bst;
300 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
301 1.1 jmcneill aprint_error(": couldn't map registers\n");
302 1.1 jmcneill return;
303 1.1 jmcneill }
304 1.1 jmcneill sc->sc_phandle = faa->faa_phandle;
305 1.1 jmcneill sc->sc_clk_ch[0] = fdtbus_clock_get(phandle, "tcon-ch0");
306 1.1 jmcneill sc->sc_clk_ch[1] = fdtbus_clock_get(phandle, "tcon-ch1");
307 1.1 jmcneill
308 1.1 jmcneill aprint_naive("\n");
309 1.1 jmcneill aprint_normal(": Timing Controller\n");
310 1.1 jmcneill
311 1.1 jmcneill sc->sc_ports.dp_ep_activate = sunxi_lcdc_ep_activate;
312 1.1 jmcneill sc->sc_ports.dp_ep_get_data = sunxi_lcdc_ep_get_data;
313 1.1 jmcneill fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_lcdc, sizeof(struct sunxi_lcdc_softc),
317 1.1 jmcneill sunxi_lcdc_match, sunxi_lcdc_attach, NULL, NULL);
318