Home | History | Annotate | Line # | Download | only in sunxi
sunxi_mixer.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: sunxi_mixer.c,v 1.3 2019/02/04 12:10:13 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_mixer.c,v 1.3 2019/02/04 12:10:13 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.h>
     42  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     43  1.1  jmcneill #include <drm/drm_plane_helper.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46  1.1  jmcneill #include <dev/fdt/fdt_port.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #include <arm/sunxi/sunxi_drm.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #define	SUNXI_MIXER_FREQ	432000000
     51  1.1  jmcneill 
     52  1.1  jmcneill #define	GLB_BASE		0x00000
     53  1.1  jmcneill #define	BLD_BASE		0x01000
     54  1.1  jmcneill #define	OVL_BASE(n)		(0x02000 + (n) * 0x1000)
     55  1.3  jmcneill #define	OVL_V_BASE		OVL_BASE(0)
     56  1.1  jmcneill #define	OVL_UI_BASE		OVL_BASE(1)
     57  1.1  jmcneill 
     58  1.1  jmcneill /* GLB registers */
     59  1.1  jmcneill #define	GLB_CTL			0x000
     60  1.1  jmcneill #define	 GLB_CTL_EN				__BIT(0)
     61  1.1  jmcneill #define	GLB_STS			0x004
     62  1.1  jmcneill #define	GLB_DBUFFER		0x008
     63  1.1  jmcneill #define	 GLB_DBUFFER_DOUBLE_BUFFER_RDY		__BIT(0)
     64  1.1  jmcneill #define	GLB_SIZE		0x00c
     65  1.1  jmcneill 
     66  1.1  jmcneill /* BLD registers */
     67  1.1  jmcneill #define	BLD_FILL_COLOR_CTL	0x000
     68  1.3  jmcneill #define	 BLD_FILL_COLOR_CTL_P1_EN		__BIT(9)
     69  1.1  jmcneill #define	 BLD_FILL_COLOR_CTL_P0_EN		__BIT(8)
     70  1.1  jmcneill #define	BLD_CH_ISIZE(n)		(0x008 + (n) * 0x10)
     71  1.1  jmcneill #define	BLD_CH_OFFSET(n)	(0x00c + (n) * 0x10)
     72  1.1  jmcneill #define	BLD_CH_RTCTL		0x080
     73  1.3  jmcneill #define	 BLD_CH_RTCTL_P1			__BITS(7,4)
     74  1.1  jmcneill #define	 BLD_CH_RTCTL_P0			__BITS(3,0)
     75  1.1  jmcneill #define	BLD_SIZE		0x08c
     76  1.1  jmcneill #define	BLD_CTL(n)		(0x090 + (n) * 0x04)
     77  1.1  jmcneill 
     78  1.3  jmcneill /* OVL_V registers */
     79  1.3  jmcneill #define	OVL_V_ATTCTL(n)		(0x000 + (n) * 0x30)
     80  1.3  jmcneill #define	 OVL_V_ATTCTL_VIDEO_UI_SEL		__BIT(15)
     81  1.3  jmcneill #define	 OVL_V_ATTCTL_LAY_FBFMT			__BITS(12,8)
     82  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_VYUY		0x00
     83  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_YVYU		0x01
     84  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_UYVY		0x02
     85  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_YUYV		0x03
     86  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_YUV422		0x06
     87  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_YUV420		0x0a
     88  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_YUV411		0x0e
     89  1.3  jmcneill #define	  OVL_V_ATTCTL_LAY_FBFMT_XRGB_8888	0x04
     90  1.3  jmcneill #define	 OVL_V_ATTCTL_LAY0_EN			__BIT(0)
     91  1.3  jmcneill #define	OVL_V_MBSIZE(n)		(0x004 + (n) * 0x30)
     92  1.3  jmcneill #define	OVL_V_COOR(n)		(0x008 + (n) * 0x30)
     93  1.3  jmcneill #define	OVL_V_PITCH0(n)		(0x00c + (n) * 0x30)
     94  1.3  jmcneill #define	OVL_V_PITCH1(n)		(0x010 + (n) * 0x30)
     95  1.3  jmcneill #define	OVL_V_PITCH2(n)		(0x014 + (n) * 0x30)
     96  1.3  jmcneill #define	OVL_V_TOP_LADD0(n)	(0x018 + (n) * 0x30)
     97  1.3  jmcneill #define	OVL_V_TOP_LADD1(n)	(0x01c + (n) * 0x30)
     98  1.3  jmcneill #define	OVL_V_TOP_LADD2(n)	(0x020 + (n) * 0x30)
     99  1.3  jmcneill #define	OVL_V_FILL_COLOR(n)	(0x0c0 + (n) * 0x4)
    100  1.3  jmcneill #define	OVL_V_TOP_HADD0		0x0d0
    101  1.3  jmcneill #define	OVL_V_TOP_HADD1		0x0d4
    102  1.3  jmcneill #define	OVL_V_TOP_HADD2		0x0d8
    103  1.3  jmcneill #define	 OVL_V_TOP_HADD_LAYER0	__BITS(7,0)
    104  1.3  jmcneill #define	OVL_V_SIZE		0x0e8
    105  1.3  jmcneill #define	OVL_V_HDS_CTL0		0x0f0
    106  1.3  jmcneill #define	OVL_V_HDS_CTL1		0x0f4
    107  1.3  jmcneill #define	OVL_V_VDS_CTL0		0x0f8
    108  1.3  jmcneill #define	OVL_V_VDS_CTL1		0x0fc
    109  1.3  jmcneill 
    110  1.1  jmcneill /* OVL_UI registers */
    111  1.1  jmcneill #define	OVL_UI_ATTR_CTL(n)	(0x000 + (n) * 0x20)
    112  1.1  jmcneill #define	 OVL_UI_ATTR_CTL_LAY_FBFMT		__BITS(12,8)
    113  1.1  jmcneill #define	  OVL_UI_ATTR_CTL_LAY_FBFMT_XRGB_8888	0x04
    114  1.1  jmcneill #define	 OVL_UI_ATTR_CTL_LAY_EN			__BIT(0)
    115  1.1  jmcneill #define	OVL_UI_MBSIZE(n)	(0x004 + (n) * 0x20)
    116  1.1  jmcneill #define	OVL_UI_COOR(n)		(0x008 + (n) * 0x20)
    117  1.1  jmcneill #define	OVL_UI_PITCH(n)		(0x00c + (n) * 0x20)
    118  1.1  jmcneill #define	OVL_UI_TOP_LADD(n)	(0x010 + (n) * 0x20)
    119  1.1  jmcneill #define	OVL_UI_TOP_HADD		0x080
    120  1.1  jmcneill #define	 OVL_UI_TOP_HADD_LAYER0	__BITS(7,0)
    121  1.1  jmcneill #define	OVL_UI_SIZE		0x088
    122  1.1  jmcneill 
    123  1.1  jmcneill enum {
    124  1.1  jmcneill 	MIXER_PORT_OUTPUT = 1,
    125  1.1  jmcneill };
    126  1.1  jmcneill 
    127  1.1  jmcneill static const char * const compatible[] = {
    128  1.2  jmcneill 	"allwinner,sun8i-h3-de2-mixer-0",
    129  1.1  jmcneill 	"allwinner,sun50i-a64-de2-mixer-0",
    130  1.1  jmcneill 	"allwinner,sun50i-a64-de2-mixer-1",
    131  1.1  jmcneill 	NULL
    132  1.1  jmcneill };
    133  1.1  jmcneill 
    134  1.1  jmcneill struct sunxi_mixer_softc;
    135  1.1  jmcneill 
    136  1.1  jmcneill struct sunxi_mixer_crtc {
    137  1.1  jmcneill 	struct drm_crtc		base;
    138  1.1  jmcneill 	struct sunxi_mixer_softc *sc;
    139  1.1  jmcneill };
    140  1.1  jmcneill 
    141  1.3  jmcneill struct sunxi_mixer_overlay {
    142  1.3  jmcneill 	struct drm_plane	base;
    143  1.3  jmcneill 	struct sunxi_mixer_softc *sc;
    144  1.3  jmcneill };
    145  1.3  jmcneill 
    146  1.1  jmcneill struct sunxi_mixer_softc {
    147  1.1  jmcneill 	device_t		sc_dev;
    148  1.1  jmcneill 	bus_space_tag_t		sc_bst;
    149  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    150  1.1  jmcneill 	int			sc_phandle;
    151  1.1  jmcneill 
    152  1.1  jmcneill 	struct sunxi_mixer_crtc	sc_crtc;
    153  1.3  jmcneill 	struct sunxi_mixer_overlay sc_overlay;
    154  1.1  jmcneill 
    155  1.1  jmcneill 	struct fdt_device_ports	sc_ports;
    156  1.1  jmcneill };
    157  1.1  jmcneill 
    158  1.1  jmcneill #define	GLB_READ(sc, reg)				\
    159  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, GLB_BASE + (reg))
    160  1.1  jmcneill #define	GLB_WRITE(sc, reg, val)				\
    161  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, GLB_BASE + (reg), (val))
    162  1.1  jmcneill 
    163  1.1  jmcneill #define	BLD_READ(sc, reg)				\
    164  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, BLD_BASE + (reg))
    165  1.1  jmcneill #define	BLD_WRITE(sc, reg, val)				\
    166  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, BLD_BASE + (reg), (val))
    167  1.1  jmcneill 
    168  1.3  jmcneill #define	OVL_V_READ(sc, reg)				\
    169  1.3  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, OVL_V_BASE + (reg))
    170  1.3  jmcneill #define	OVL_V_WRITE(sc, reg, val)			\
    171  1.3  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, OVL_V_BASE + (reg), (val))
    172  1.3  jmcneill 
    173  1.1  jmcneill #define	OVL_UI_READ(sc, reg)				\
    174  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, OVL_UI_BASE + (reg))
    175  1.1  jmcneill #define	OVL_UI_WRITE(sc, reg, val)			\
    176  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, OVL_UI_BASE + (reg), (val))
    177  1.1  jmcneill 
    178  1.3  jmcneill #define	to_sunxi_mixer_crtc(x)		container_of(x, struct sunxi_mixer_crtc, base)
    179  1.3  jmcneill #define	to_sunxi_mixer_overlay(x)	container_of(x, struct sunxi_mixer_overlay, base)
    180  1.1  jmcneill 
    181  1.1  jmcneill static void
    182  1.1  jmcneill sunxi_mixer_destroy(struct drm_crtc *crtc)
    183  1.1  jmcneill {
    184  1.1  jmcneill 	drm_crtc_cleanup(crtc);
    185  1.1  jmcneill }
    186  1.1  jmcneill 
    187  1.1  jmcneill static const struct drm_crtc_funcs sunxi_mixer_crtc_funcs = {
    188  1.1  jmcneill 	.set_config = drm_crtc_helper_set_config,
    189  1.1  jmcneill 	.destroy = sunxi_mixer_destroy,
    190  1.1  jmcneill };
    191  1.1  jmcneill 
    192  1.1  jmcneill static void
    193  1.1  jmcneill sunxi_mixer_dpms(struct drm_crtc *crtc, int mode)
    194  1.1  jmcneill {
    195  1.1  jmcneill }
    196  1.1  jmcneill 
    197  1.1  jmcneill static bool
    198  1.1  jmcneill sunxi_mixer_mode_fixup(struct drm_crtc *crtc,
    199  1.1  jmcneill     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    200  1.1  jmcneill {
    201  1.1  jmcneill 	return true;
    202  1.1  jmcneill }
    203  1.1  jmcneill 
    204  1.1  jmcneill static int
    205  1.1  jmcneill sunxi_mixer_mode_do_set_base(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    206  1.1  jmcneill     int x, int y, int atomic)
    207  1.1  jmcneill {
    208  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    209  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    210  1.1  jmcneill 	struct sunxi_drm_framebuffer *sfb = atomic?
    211  1.1  jmcneill 	    to_sunxi_drm_framebuffer(fb) :
    212  1.1  jmcneill 	    to_sunxi_drm_framebuffer(crtc->primary->fb);
    213  1.1  jmcneill 
    214  1.1  jmcneill 	uint64_t paddr = (uint64_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
    215  1.1  jmcneill 
    216  1.1  jmcneill 	uint32_t haddr = (paddr >> 32) & OVL_UI_TOP_HADD_LAYER0;
    217  1.1  jmcneill 	uint32_t laddr = paddr & 0xffffffff;
    218  1.1  jmcneill 
    219  1.1  jmcneill 	/* Framebuffer start address */
    220  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_TOP_HADD, haddr);
    221  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_TOP_LADD(0), laddr);
    222  1.1  jmcneill 
    223  1.1  jmcneill 	return 0;
    224  1.1  jmcneill }
    225  1.1  jmcneill 
    226  1.1  jmcneill static int
    227  1.1  jmcneill sunxi_mixer_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
    228  1.1  jmcneill     struct drm_display_mode *adjusted_mode, int x, int y,
    229  1.1  jmcneill     struct drm_framebuffer *old_fb)
    230  1.1  jmcneill {
    231  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    232  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    233  1.1  jmcneill 	uint32_t val;
    234  1.1  jmcneill 
    235  1.1  jmcneill 	const uint32_t size = ((adjusted_mode->vdisplay - 1) << 16) |
    236  1.1  jmcneill 			      (adjusted_mode->hdisplay - 1);
    237  1.1  jmcneill 	const uint32_t offset = (y << 16) | x;
    238  1.1  jmcneill 
    239  1.1  jmcneill 	/* Set global size */
    240  1.1  jmcneill 	GLB_WRITE(sc, GLB_SIZE, size);
    241  1.1  jmcneill 
    242  1.1  jmcneill 	/* Enable pipe 0 */
    243  1.3  jmcneill 	val = BLD_READ(sc, BLD_FILL_COLOR_CTL);
    244  1.3  jmcneill 	val |= BLD_FILL_COLOR_CTL_P0_EN;
    245  1.3  jmcneill 	BLD_WRITE(sc, BLD_FILL_COLOR_CTL, val);
    246  1.1  jmcneill 
    247  1.1  jmcneill 	/* Set blender 0 input size */
    248  1.1  jmcneill 	BLD_WRITE(sc, BLD_CH_ISIZE(0), size);
    249  1.1  jmcneill 	/* Set blender 0 offset */
    250  1.1  jmcneill 	BLD_WRITE(sc, BLD_CH_OFFSET(0), offset);
    251  1.1  jmcneill 	/* Route channel 1 to pipe 0 */
    252  1.3  jmcneill 	val = BLD_READ(sc, BLD_CH_RTCTL);
    253  1.3  jmcneill 	val &= ~BLD_CH_RTCTL_P0;
    254  1.3  jmcneill 	val |= __SHIFTIN(1, BLD_CH_RTCTL_P0);
    255  1.3  jmcneill 	BLD_WRITE(sc, BLD_CH_RTCTL, val);
    256  1.1  jmcneill 	/* Set blender output size */
    257  1.1  jmcneill 	BLD_WRITE(sc, BLD_SIZE, size);
    258  1.1  jmcneill 
    259  1.1  jmcneill 	/* Enable UI overlay in XRGB8888 mode */
    260  1.1  jmcneill 	val = OVL_UI_ATTR_CTL_LAY_EN |
    261  1.1  jmcneill 	      __SHIFTIN(OVL_UI_ATTR_CTL_LAY_FBFMT_XRGB_8888, OVL_UI_ATTR_CTL_LAY_FBFMT);
    262  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_ATTR_CTL(0), val);
    263  1.1  jmcneill 	/* Set UI overlay layer size */
    264  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_MBSIZE(0), size);
    265  1.1  jmcneill 	/* Set UI overlay offset */
    266  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_COOR(0), offset);
    267  1.1  jmcneill 	/* Set UI overlay line size */
    268  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_PITCH(0), adjusted_mode->hdisplay * 4);
    269  1.1  jmcneill 	/* Set UI overlay window size */
    270  1.1  jmcneill 	OVL_UI_WRITE(sc, OVL_UI_SIZE, size);
    271  1.1  jmcneill 
    272  1.1  jmcneill 	sunxi_mixer_mode_do_set_base(crtc, old_fb, x, y, 0);
    273  1.1  jmcneill 
    274  1.1  jmcneill 	return 0;
    275  1.1  jmcneill }
    276  1.1  jmcneill 
    277  1.1  jmcneill static int
    278  1.1  jmcneill sunxi_mixer_mode_set_base(struct drm_crtc *crtc, int x, int y,
    279  1.1  jmcneill     struct drm_framebuffer *old_fb)
    280  1.1  jmcneill {
    281  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    282  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    283  1.1  jmcneill 
    284  1.1  jmcneill 	sunxi_mixer_mode_do_set_base(crtc, old_fb, x, y, 0);
    285  1.1  jmcneill 
    286  1.1  jmcneill 	/* Commit settings */
    287  1.1  jmcneill 	GLB_WRITE(sc, GLB_DBUFFER, GLB_DBUFFER_DOUBLE_BUFFER_RDY);
    288  1.1  jmcneill 
    289  1.1  jmcneill 	return 0;
    290  1.1  jmcneill }
    291  1.1  jmcneill 
    292  1.1  jmcneill static int
    293  1.1  jmcneill sunxi_mixer_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    294  1.1  jmcneill     int x, int y, enum mode_set_atomic state)
    295  1.1  jmcneill {
    296  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    297  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    298  1.1  jmcneill 
    299  1.1  jmcneill 	sunxi_mixer_mode_do_set_base(crtc, fb, x, y, 1);
    300  1.1  jmcneill 
    301  1.1  jmcneill 	/* Commit settings */
    302  1.1  jmcneill 	GLB_WRITE(sc, GLB_DBUFFER, GLB_DBUFFER_DOUBLE_BUFFER_RDY);
    303  1.1  jmcneill 
    304  1.1  jmcneill 	return 0;
    305  1.1  jmcneill }
    306  1.1  jmcneill 
    307  1.1  jmcneill static void
    308  1.1  jmcneill sunxi_mixer_disable(struct drm_crtc *crtc)
    309  1.1  jmcneill {
    310  1.1  jmcneill }
    311  1.1  jmcneill 
    312  1.1  jmcneill static void
    313  1.1  jmcneill sunxi_mixer_prepare(struct drm_crtc *crtc)
    314  1.1  jmcneill {
    315  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    316  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    317  1.1  jmcneill 
    318  1.1  jmcneill 	/* RT enable */
    319  1.1  jmcneill 	GLB_WRITE(sc, GLB_CTL, GLB_CTL_EN);
    320  1.1  jmcneill }
    321  1.1  jmcneill 
    322  1.1  jmcneill static void
    323  1.1  jmcneill sunxi_mixer_commit(struct drm_crtc *crtc)
    324  1.1  jmcneill {
    325  1.1  jmcneill 	struct sunxi_mixer_crtc *mixer_crtc = to_sunxi_mixer_crtc(crtc);
    326  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = mixer_crtc->sc;
    327  1.1  jmcneill 
    328  1.1  jmcneill 	/* Commit settings */
    329  1.1  jmcneill 	GLB_WRITE(sc, GLB_DBUFFER, GLB_DBUFFER_DOUBLE_BUFFER_RDY);
    330  1.1  jmcneill }
    331  1.1  jmcneill 
    332  1.1  jmcneill static const struct drm_crtc_helper_funcs sunxi_mixer_crtc_helper_funcs = {
    333  1.1  jmcneill 	.dpms = sunxi_mixer_dpms,
    334  1.1  jmcneill 	.mode_fixup = sunxi_mixer_mode_fixup,
    335  1.1  jmcneill 	.mode_set = sunxi_mixer_mode_set,
    336  1.1  jmcneill 	.mode_set_base = sunxi_mixer_mode_set_base,
    337  1.1  jmcneill 	.mode_set_base_atomic = sunxi_mixer_mode_set_base_atomic,
    338  1.1  jmcneill 	.disable = sunxi_mixer_disable,
    339  1.1  jmcneill 	.prepare = sunxi_mixer_prepare,
    340  1.1  jmcneill 	.commit = sunxi_mixer_commit,
    341  1.1  jmcneill };
    342  1.1  jmcneill 
    343  1.3  jmcneill static void
    344  1.3  jmcneill sunxi_mixer_overlay_destroy(struct drm_plane *plane)
    345  1.3  jmcneill {
    346  1.3  jmcneill }
    347  1.3  jmcneill 
    348  1.3  jmcneill static bool
    349  1.3  jmcneill sunxi_mixer_overlay_ui(uint32_t drm_format)
    350  1.3  jmcneill {
    351  1.3  jmcneill 	switch (drm_format) {
    352  1.3  jmcneill 	case DRM_FORMAT_XRGB8888:
    353  1.3  jmcneill 		return true;
    354  1.3  jmcneill 	default:
    355  1.3  jmcneill 		return false;
    356  1.3  jmcneill 	}
    357  1.3  jmcneill }
    358  1.3  jmcneill 
    359  1.3  jmcneill static u_int
    360  1.3  jmcneill sunxi_mixer_overlay_format(uint32_t drm_format)
    361  1.3  jmcneill {
    362  1.3  jmcneill 	switch (drm_format) {
    363  1.3  jmcneill 	case DRM_FORMAT_XRGB8888:	return OVL_V_ATTCTL_LAY_FBFMT_XRGB_8888;
    364  1.3  jmcneill 	case DRM_FORMAT_VYUY:		return OVL_V_ATTCTL_LAY_FBFMT_VYUY;
    365  1.3  jmcneill 	case DRM_FORMAT_YVYU:		return OVL_V_ATTCTL_LAY_FBFMT_YVYU;
    366  1.3  jmcneill 	case DRM_FORMAT_UYVY:		return OVL_V_ATTCTL_LAY_FBFMT_UYVY;
    367  1.3  jmcneill 	case DRM_FORMAT_YUYV:		return OVL_V_ATTCTL_LAY_FBFMT_YUYV;
    368  1.3  jmcneill 	case DRM_FORMAT_YUV422:		return OVL_V_ATTCTL_LAY_FBFMT_YUV422;
    369  1.3  jmcneill 	case DRM_FORMAT_YUV420:		return OVL_V_ATTCTL_LAY_FBFMT_YUV420;
    370  1.3  jmcneill 	case DRM_FORMAT_YUV411:		return OVL_V_ATTCTL_LAY_FBFMT_YUV411;
    371  1.3  jmcneill 	default:			return 0;	/* shouldn't happen */
    372  1.3  jmcneill 	}
    373  1.3  jmcneill }
    374  1.3  jmcneill 
    375  1.3  jmcneill static int
    376  1.3  jmcneill sunxi_mixer_overlay_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
    377  1.3  jmcneill     struct drm_framebuffer *fb, int crtc_x, int crtc_y, u_int crtc_w, u_int crtc_h,
    378  1.3  jmcneill     uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h)
    379  1.3  jmcneill {
    380  1.3  jmcneill 	struct sunxi_mixer_overlay *overlay = to_sunxi_mixer_overlay(plane);
    381  1.3  jmcneill 	struct sunxi_mixer_softc * const sc = overlay->sc;
    382  1.3  jmcneill 	struct sunxi_drm_framebuffer *sfb = to_sunxi_drm_framebuffer(fb);
    383  1.3  jmcneill 	uint32_t val;
    384  1.3  jmcneill 
    385  1.3  jmcneill 	const u_int fbfmt = sunxi_mixer_overlay_format(fb->pixel_format);
    386  1.3  jmcneill 	const uint64_t paddr = (uint64_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
    387  1.3  jmcneill 
    388  1.3  jmcneill 	const uint32_t input_size = (((src_h >> 16) - 1) << 16) | ((src_w >> 16) - 1);
    389  1.3  jmcneill 	const uint32_t input_pos = ((src_y >> 16) << 16) | (src_x >> 16);
    390  1.3  jmcneill 
    391  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_MBSIZE(0), input_size);
    392  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_COOR(0), input_pos);
    393  1.3  jmcneill 
    394  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_PITCH0(0), fb->pitches[0]);
    395  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_PITCH1(0), fb->pitches[1]);
    396  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_PITCH2(0), fb->pitches[2]);
    397  1.3  jmcneill 
    398  1.3  jmcneill 	const uint64_t paddr0 = paddr + fb->offsets[0] +
    399  1.3  jmcneill 	    (src_x >> 16) * drm_format_plane_cpp(fb->pixel_format, 0) +
    400  1.3  jmcneill 	    (src_y >> 16) * fb->pitches[0];
    401  1.3  jmcneill 	const uint64_t paddr1 = paddr + fb->offsets[1] +
    402  1.3  jmcneill 	    (src_x >> 16) * drm_format_plane_cpp(fb->pixel_format, 1) +
    403  1.3  jmcneill 	    (src_y >> 16) * fb->pitches[1];
    404  1.3  jmcneill 	const uint64_t paddr2 = paddr + fb->offsets[2] +
    405  1.3  jmcneill 	    (src_x >> 16) * drm_format_plane_cpp(fb->pixel_format, 2) +
    406  1.3  jmcneill 	    (src_y >> 16) * fb->pitches[2];
    407  1.3  jmcneill 
    408  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_HADD0, (paddr0 >> 32) & OVL_V_TOP_HADD_LAYER0);
    409  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_HADD1, (paddr1 >> 32) & OVL_V_TOP_HADD_LAYER0);
    410  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_HADD2, (paddr2 >> 32) & OVL_V_TOP_HADD_LAYER0);
    411  1.3  jmcneill 
    412  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_LADD0(0), paddr0 & 0xffffffff);
    413  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_LADD1(0), paddr1 & 0xffffffff);
    414  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_TOP_LADD2(0), paddr2 & 0xffffffff);
    415  1.3  jmcneill 
    416  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_SIZE, input_size);
    417  1.3  jmcneill 
    418  1.3  jmcneill 	val = OVL_V_ATTCTL_LAY0_EN;
    419  1.3  jmcneill 	val |= __SHIFTIN(fbfmt, OVL_V_ATTCTL_LAY_FBFMT);
    420  1.3  jmcneill 	if (sunxi_mixer_overlay_ui(fb->pixel_format) == true)
    421  1.3  jmcneill 		val |= OVL_V_ATTCTL_VIDEO_UI_SEL;
    422  1.3  jmcneill 	OVL_V_WRITE(sc, OVL_V_ATTCTL(0), val);
    423  1.3  jmcneill 
    424  1.3  jmcneill 	/* Set blender 1 input size */
    425  1.3  jmcneill 	BLD_WRITE(sc, BLD_CH_ISIZE(1), input_size);
    426  1.3  jmcneill 	/* Set blender 1 offset */
    427  1.3  jmcneill 	BLD_WRITE(sc, BLD_CH_OFFSET(1), (crtc_y << 16) | crtc_x);
    428  1.3  jmcneill 	/* Route channel 0 to pipe 1 */
    429  1.3  jmcneill 	val = BLD_READ(sc, BLD_CH_RTCTL);
    430  1.3  jmcneill 	val &= ~BLD_CH_RTCTL_P1;
    431  1.3  jmcneill 	val |= __SHIFTIN(0, BLD_CH_RTCTL_P1);
    432  1.3  jmcneill 	BLD_WRITE(sc, BLD_CH_RTCTL, val);
    433  1.3  jmcneill 
    434  1.3  jmcneill         /* Enable pipe 1 */
    435  1.3  jmcneill 	val = BLD_READ(sc, BLD_FILL_COLOR_CTL);
    436  1.3  jmcneill 	val |= BLD_FILL_COLOR_CTL_P1_EN;
    437  1.3  jmcneill 	BLD_WRITE(sc, BLD_FILL_COLOR_CTL, val);
    438  1.3  jmcneill 
    439  1.3  jmcneill 	/* Commit settings */
    440  1.3  jmcneill 	GLB_WRITE(sc, GLB_DBUFFER, GLB_DBUFFER_DOUBLE_BUFFER_RDY);
    441  1.3  jmcneill 
    442  1.3  jmcneill 	return 0;
    443  1.3  jmcneill }
    444  1.3  jmcneill 
    445  1.3  jmcneill static int
    446  1.3  jmcneill sunxi_mixer_overlay_disable_plane(struct drm_plane *plane)
    447  1.3  jmcneill {
    448  1.3  jmcneill 	struct sunxi_mixer_overlay *overlay = to_sunxi_mixer_overlay(plane);
    449  1.3  jmcneill 	struct sunxi_mixer_softc * const sc = overlay->sc;
    450  1.3  jmcneill 	uint32_t val;
    451  1.3  jmcneill 
    452  1.3  jmcneill 	val = BLD_READ(sc, BLD_FILL_COLOR_CTL);
    453  1.3  jmcneill 	val &= ~BLD_FILL_COLOR_CTL_P1_EN;
    454  1.3  jmcneill 	BLD_WRITE(sc, BLD_FILL_COLOR_CTL, val);
    455  1.3  jmcneill 
    456  1.3  jmcneill 	/* Commit settings */
    457  1.3  jmcneill 	GLB_WRITE(sc, GLB_DBUFFER, GLB_DBUFFER_DOUBLE_BUFFER_RDY);
    458  1.3  jmcneill 
    459  1.3  jmcneill 	return 0;
    460  1.3  jmcneill }
    461  1.3  jmcneill 
    462  1.3  jmcneill static const struct drm_plane_funcs sunxi_mixer_overlay_funcs = {
    463  1.3  jmcneill 	.update_plane = sunxi_mixer_overlay_update_plane,
    464  1.3  jmcneill 	.disable_plane = sunxi_mixer_overlay_disable_plane,
    465  1.3  jmcneill 	.destroy = sunxi_mixer_overlay_destroy,
    466  1.3  jmcneill };
    467  1.3  jmcneill 
    468  1.3  jmcneill static uint32_t sunxi_mixer_overlay_formats[] = {
    469  1.3  jmcneill 	DRM_FORMAT_XRGB8888,
    470  1.3  jmcneill #if notyet
    471  1.3  jmcneill 	DRM_FORMAT_VYUY,
    472  1.3  jmcneill 	DRM_FORMAT_YVYU,
    473  1.3  jmcneill 	DRM_FORMAT_UYVY,
    474  1.3  jmcneill 	DRM_FORMAT_YUYV,
    475  1.3  jmcneill 	DRM_FORMAT_YUV422,
    476  1.3  jmcneill 	DRM_FORMAT_YUV420,
    477  1.3  jmcneill 	DRM_FORMAT_YUV411,
    478  1.3  jmcneill #endif
    479  1.3  jmcneill };
    480  1.3  jmcneill 
    481  1.1  jmcneill static int
    482  1.1  jmcneill sunxi_mixer_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    483  1.1  jmcneill {
    484  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = device_private(dev);
    485  1.1  jmcneill 	struct drm_device *ddev;
    486  1.1  jmcneill 
    487  1.1  jmcneill 	if (!activate)
    488  1.1  jmcneill 		return EINVAL;
    489  1.1  jmcneill 
    490  1.1  jmcneill 	ddev = sunxi_drm_endpoint_device(ep);
    491  1.1  jmcneill 	if (ddev == NULL) {
    492  1.1  jmcneill 		DRM_ERROR("couldn't find DRM device\n");
    493  1.1  jmcneill 		return ENXIO;
    494  1.1  jmcneill 	}
    495  1.1  jmcneill 
    496  1.1  jmcneill 	sc->sc_crtc.sc = sc;
    497  1.3  jmcneill 	sc->sc_overlay.sc = sc;
    498  1.1  jmcneill 
    499  1.1  jmcneill 	drm_crtc_init(ddev, &sc->sc_crtc.base, &sunxi_mixer_crtc_funcs);
    500  1.1  jmcneill 	drm_crtc_helper_add(&sc->sc_crtc.base, &sunxi_mixer_crtc_helper_funcs);
    501  1.1  jmcneill 
    502  1.3  jmcneill 	drm_universal_plane_init(ddev, &sc->sc_overlay.base,
    503  1.3  jmcneill 	    1 << drm_crtc_index(&sc->sc_crtc.base), &sunxi_mixer_overlay_funcs,
    504  1.3  jmcneill 	    sunxi_mixer_overlay_formats, __arraycount(sunxi_mixer_overlay_formats),
    505  1.3  jmcneill 	    DRM_PLANE_TYPE_OVERLAY);
    506  1.3  jmcneill 
    507  1.1  jmcneill 	return fdt_endpoint_activate(ep, activate);
    508  1.1  jmcneill }
    509  1.1  jmcneill 
    510  1.1  jmcneill static void *
    511  1.1  jmcneill sunxi_mixer_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    512  1.1  jmcneill {
    513  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = device_private(dev);
    514  1.1  jmcneill 
    515  1.1  jmcneill 	return &sc->sc_crtc;
    516  1.1  jmcneill }
    517  1.1  jmcneill 
    518  1.1  jmcneill static int
    519  1.1  jmcneill sunxi_mixer_match(device_t parent, cfdata_t cf, void *aux)
    520  1.1  jmcneill {
    521  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    522  1.1  jmcneill 
    523  1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
    524  1.1  jmcneill }
    525  1.1  jmcneill 
    526  1.1  jmcneill static void
    527  1.1  jmcneill sunxi_mixer_attach(device_t parent, device_t self, void *aux)
    528  1.1  jmcneill {
    529  1.1  jmcneill 	struct sunxi_mixer_softc * const sc = device_private(self);
    530  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    531  1.1  jmcneill 	struct fdt_endpoint *out_ep;
    532  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    533  1.1  jmcneill 	struct clk *clk_bus, *clk_mod;
    534  1.1  jmcneill 	struct fdtbus_reset *rst;
    535  1.1  jmcneill 	bus_addr_t addr;
    536  1.1  jmcneill 	bus_size_t size;
    537  1.1  jmcneill 
    538  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    539  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    540  1.1  jmcneill 		return;
    541  1.1  jmcneill 	}
    542  1.1  jmcneill 
    543  1.1  jmcneill 	rst = fdtbus_reset_get_index(phandle, 0);
    544  1.1  jmcneill 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    545  1.1  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    546  1.1  jmcneill 		return;
    547  1.1  jmcneill 	}
    548  1.1  jmcneill 
    549  1.1  jmcneill 	clk_bus = fdtbus_clock_get(phandle, "bus");
    550  1.1  jmcneill 	if (clk_bus == NULL || clk_enable(clk_bus) != 0) {
    551  1.1  jmcneill 		aprint_error(": couldn't enable bus clock\n");
    552  1.1  jmcneill 		return;
    553  1.1  jmcneill 	}
    554  1.1  jmcneill 
    555  1.1  jmcneill 	clk_mod = fdtbus_clock_get(phandle, "mod");
    556  1.1  jmcneill 	if (clk_mod == NULL ||
    557  1.1  jmcneill 	    clk_set_rate(clk_mod, SUNXI_MIXER_FREQ) != 0 ||
    558  1.1  jmcneill 	    clk_enable(clk_mod) != 0) {
    559  1.1  jmcneill 		aprint_error(": couldn't enable mod clock\n");
    560  1.1  jmcneill 		return;
    561  1.1  jmcneill 	}
    562  1.1  jmcneill 
    563  1.1  jmcneill 	sc->sc_dev = self;
    564  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    565  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    566  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    567  1.1  jmcneill 		return;
    568  1.1  jmcneill 	}
    569  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    570  1.1  jmcneill 
    571  1.1  jmcneill 	aprint_naive("\n");
    572  1.1  jmcneill 	aprint_normal(": Display Engine Mixer\n");
    573  1.1  jmcneill 
    574  1.1  jmcneill 	sc->sc_ports.dp_ep_activate = sunxi_mixer_ep_activate;
    575  1.1  jmcneill 	sc->sc_ports.dp_ep_get_data = sunxi_mixer_ep_get_data;
    576  1.1  jmcneill 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_CRTC);
    577  1.1  jmcneill 
    578  1.1  jmcneill 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, MIXER_PORT_OUTPUT, 0);
    579  1.1  jmcneill 	if (out_ep != NULL)
    580  1.1  jmcneill 		sunxi_drm_register_endpoint(phandle, out_ep);
    581  1.1  jmcneill }
    582  1.1  jmcneill 
    583  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_mixer, sizeof(struct sunxi_mixer_softc),
    584  1.1  jmcneill 	sunxi_mixer_match, sunxi_mixer_attach, NULL, NULL);
    585