Home | History | Annotate | Line # | Download | only in i2c
tda19988.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: tda19988.c,v 1.3 2019/11/04 10:02:39 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo (at) freebsd.org>
      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 AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  jmcneill  * LIABILITY, 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: tda19988.c,v 1.3 2019/11/04 10:02:39 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill /*
     33  1.1  jmcneill * NXP TDA19988 HDMI encoder
     34  1.1  jmcneill */
     35  1.1  jmcneill #include <sys/param.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/time.h>
     39  1.1  jmcneill #include <sys/bus.h>
     40  1.1  jmcneill #include <sys/types.h>
     41  1.1  jmcneill 
     42  1.1  jmcneill #include <dev/i2c/i2cvar.h>
     43  1.1  jmcneill #include <dev/i2c/ddcvar.h>
     44  1.1  jmcneill #include <dev/i2c/ddcreg.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     47  1.1  jmcneill #include <dev/fdt/fdt_port.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/videomode/videomode.h>
     50  1.1  jmcneill #include <dev/videomode/edidvar.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #include <drm/drmP.h>
     53  1.1  jmcneill #include <drm/drm_crtc.h>
     54  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     55  1.1  jmcneill #include <drm/drm_edid.h>
     56  1.1  jmcneill 
     57  1.1  jmcneill enum {
     58  1.1  jmcneill 	TDA19988_PORT_INPUT = 0
     59  1.1  jmcneill };
     60  1.1  jmcneill 
     61  1.1  jmcneill #define	MKREG(page, addr)	(((page) << 8) | (addr))
     62  1.1  jmcneill 
     63  1.1  jmcneill #define	REGPAGE(reg)		(((reg) >> 8) & 0xff)
     64  1.1  jmcneill #define	REGADDR(reg)		((reg) & 0xff)
     65  1.1  jmcneill 
     66  1.1  jmcneill #define TDA_VERSION		MKREG(0x00, 0x00)
     67  1.1  jmcneill #define TDA_MAIN_CNTRL0		MKREG(0x00, 0x01)
     68  1.1  jmcneill #define 	MAIN_CNTRL0_SR		(1 << 0)
     69  1.1  jmcneill #define TDA_VERSION_MSB		MKREG(0x00, 0x02)
     70  1.1  jmcneill #define	TDA_SOFTRESET		MKREG(0x00, 0x0a)
     71  1.1  jmcneill #define		SOFTRESET_I2C		(1 << 1)
     72  1.1  jmcneill #define		SOFTRESET_AUDIO		(1 << 0)
     73  1.1  jmcneill #define	TDA_DDC_CTRL		MKREG(0x00, 0x0b)
     74  1.1  jmcneill #define		DDC_ENABLE		0
     75  1.1  jmcneill #define	TDA_CCLK		MKREG(0x00, 0x0c)
     76  1.1  jmcneill #define		CCLK_ENABLE		1
     77  1.1  jmcneill #define	TDA_INT_FLAGS_2		MKREG(0x00, 0x11)
     78  1.1  jmcneill #define		INT_FLAGS_2_EDID_BLK_RD	(1 << 1)
     79  1.1  jmcneill 
     80  1.1  jmcneill #define	TDA_VIP_CNTRL_0		MKREG(0x00, 0x20)
     81  1.1  jmcneill #define	TDA_VIP_CNTRL_1		MKREG(0x00, 0x21)
     82  1.1  jmcneill #define	TDA_VIP_CNTRL_2		MKREG(0x00, 0x22)
     83  1.1  jmcneill #define	TDA_VIP_CNTRL_3		MKREG(0x00, 0x23)
     84  1.1  jmcneill #define		VIP_CNTRL_3_SYNC_HS	(2 << 4)
     85  1.1  jmcneill #define		VIP_CNTRL_3_V_TGL	(1 << 2)
     86  1.1  jmcneill #define		VIP_CNTRL_3_H_TGL	(1 << 1)
     87  1.1  jmcneill 
     88  1.1  jmcneill #define	TDA_VIP_CNTRL_4		MKREG(0x00, 0x24)
     89  1.1  jmcneill #define		VIP_CNTRL_4_BLANKIT_NDE		(0 << 2)
     90  1.1  jmcneill #define		VIP_CNTRL_4_BLANKIT_HS_VS	(1 << 2)
     91  1.1  jmcneill #define		VIP_CNTRL_4_BLANKIT_NHS_VS	(2 << 2)
     92  1.1  jmcneill #define		VIP_CNTRL_4_BLANKIT_HE_VE	(3 << 2)
     93  1.1  jmcneill #define		VIP_CNTRL_4_BLC_NONE		(0 << 0)
     94  1.1  jmcneill #define		VIP_CNTRL_4_BLC_RGB444		(1 << 0)
     95  1.1  jmcneill #define		VIP_CNTRL_4_BLC_YUV444		(2 << 0)
     96  1.1  jmcneill #define		VIP_CNTRL_4_BLC_YUV422		(3 << 0)
     97  1.1  jmcneill #define	TDA_VIP_CNTRL_5		MKREG(0x00, 0x25)
     98  1.1  jmcneill #define		VIP_CNTRL_5_SP_CNT(n)	(((n) & 3) << 1)
     99  1.1  jmcneill #define	TDA_MUX_VP_VIP_OUT	MKREG(0x00, 0x27)
    100  1.1  jmcneill #define TDA_MAT_CONTRL		MKREG(0x00, 0x80)
    101  1.1  jmcneill #define		MAT_CONTRL_MAT_BP	(1 << 2)
    102  1.1  jmcneill #define	TDA_VIDFORMAT		MKREG(0x00, 0xa0)
    103  1.1  jmcneill #define	TDA_REFPIX_MSB		MKREG(0x00, 0xa1)
    104  1.1  jmcneill #define	TDA_REFPIX_LSB		MKREG(0x00, 0xa2)
    105  1.1  jmcneill #define	TDA_REFLINE_MSB		MKREG(0x00, 0xa3)
    106  1.1  jmcneill #define	TDA_REFLINE_LSB		MKREG(0x00, 0xa4)
    107  1.1  jmcneill #define	TDA_NPIX_MSB		MKREG(0x00, 0xa5)
    108  1.1  jmcneill #define	TDA_NPIX_LSB		MKREG(0x00, 0xa6)
    109  1.1  jmcneill #define	TDA_NLINE_MSB		MKREG(0x00, 0xa7)
    110  1.1  jmcneill #define	TDA_NLINE_LSB		MKREG(0x00, 0xa8)
    111  1.1  jmcneill #define	TDA_VS_LINE_STRT_1_MSB	MKREG(0x00, 0xa9)
    112  1.1  jmcneill #define	TDA_VS_LINE_STRT_1_LSB	MKREG(0x00, 0xaa)
    113  1.1  jmcneill #define	TDA_VS_PIX_STRT_1_MSB	MKREG(0x00, 0xab)
    114  1.1  jmcneill #define	TDA_VS_PIX_STRT_1_LSB	MKREG(0x00, 0xac)
    115  1.1  jmcneill #define	TDA_VS_LINE_END_1_MSB	MKREG(0x00, 0xad)
    116  1.1  jmcneill #define	TDA_VS_LINE_END_1_LSB	MKREG(0x00, 0xae)
    117  1.1  jmcneill #define	TDA_VS_PIX_END_1_MSB	MKREG(0x00, 0xaf)
    118  1.1  jmcneill #define	TDA_VS_PIX_END_1_LSB	MKREG(0x00, 0xb0)
    119  1.1  jmcneill #define	TDA_VS_LINE_STRT_2_MSB	MKREG(0x00, 0xb1)
    120  1.1  jmcneill #define	TDA_VS_LINE_STRT_2_LSB	MKREG(0x00, 0xb2)
    121  1.1  jmcneill #define	TDA_VS_PIX_STRT_2_MSB	MKREG(0x00, 0xb3)
    122  1.1  jmcneill #define	TDA_VS_PIX_STRT_2_LSB	MKREG(0x00, 0xb4)
    123  1.1  jmcneill #define	TDA_VS_LINE_END_2_MSB	MKREG(0x00, 0xb5)
    124  1.1  jmcneill #define	TDA_VS_LINE_END_2_LSB	MKREG(0x00, 0xb6)
    125  1.1  jmcneill #define	TDA_VS_PIX_END_2_MSB	MKREG(0x00, 0xb7)
    126  1.1  jmcneill #define	TDA_VS_PIX_END_2_LSB	MKREG(0x00, 0xb8)
    127  1.1  jmcneill #define	TDA_HS_PIX_START_MSB	MKREG(0x00, 0xb9)
    128  1.1  jmcneill #define	TDA_HS_PIX_START_LSB	MKREG(0x00, 0xba)
    129  1.1  jmcneill #define	TDA_HS_PIX_STOP_MSB	MKREG(0x00, 0xbb)
    130  1.1  jmcneill #define	TDA_HS_PIX_STOP_LSB	MKREG(0x00, 0xbc)
    131  1.1  jmcneill #define	TDA_VWIN_START_1_MSB	MKREG(0x00, 0xbd)
    132  1.1  jmcneill #define	TDA_VWIN_START_1_LSB	MKREG(0x00, 0xbe)
    133  1.1  jmcneill #define	TDA_VWIN_END_1_MSB	MKREG(0x00, 0xbf)
    134  1.1  jmcneill #define	TDA_VWIN_END_1_LSB	MKREG(0x00, 0xc0)
    135  1.1  jmcneill #define	TDA_VWIN_START_2_MSB	MKREG(0x00, 0xc1)
    136  1.1  jmcneill #define	TDA_VWIN_START_2_LSB	MKREG(0x00, 0xc2)
    137  1.1  jmcneill #define	TDA_VWIN_END_2_MSB	MKREG(0x00, 0xc3)
    138  1.1  jmcneill #define	TDA_VWIN_END_2_LSB	MKREG(0x00, 0xc4)
    139  1.1  jmcneill #define	TDA_DE_START_MSB	MKREG(0x00, 0xc5)
    140  1.1  jmcneill #define	TDA_DE_START_LSB	MKREG(0x00, 0xc6)
    141  1.1  jmcneill #define	TDA_DE_STOP_MSB		MKREG(0x00, 0xc7)
    142  1.1  jmcneill #define	TDA_DE_STOP_LSB		MKREG(0x00, 0xc8)
    143  1.1  jmcneill 
    144  1.1  jmcneill #define	TDA_TBG_CNTRL_0		MKREG(0x00, 0xca)
    145  1.1  jmcneill #define		TBG_CNTRL_0_SYNC_ONCE	(1 << 7)
    146  1.1  jmcneill #define		TBG_CNTRL_0_SYNC_MTHD	(1 << 6)
    147  1.1  jmcneill 
    148  1.1  jmcneill #define	TDA_TBG_CNTRL_1		MKREG(0x00, 0xcb)
    149  1.1  jmcneill #define		TBG_CNTRL_1_DWIN_DIS	(1 << 6)
    150  1.1  jmcneill #define		TBG_CNTRL_1_TGL_EN	(1 << 2)
    151  1.1  jmcneill #define		TBG_CNTRL_1_V_TGL	(1 << 1)
    152  1.1  jmcneill #define		TBG_CNTRL_1_H_TGL	(1 << 0)
    153  1.1  jmcneill 
    154  1.1  jmcneill #define	TDA_HVF_CNTRL_0		MKREG(0x00, 0xe4)
    155  1.1  jmcneill #define		HVF_CNTRL_0_PREFIL_NONE		(0 << 2)
    156  1.1  jmcneill #define		HVF_CNTRL_0_INTPOL_BYPASS	(0 << 0)
    157  1.1  jmcneill #define	TDA_HVF_CNTRL_1		MKREG(0x00, 0xe5)
    158  1.1  jmcneill #define		HVF_CNTRL_1_VQR(x)	(((x) & 3) << 2)
    159  1.1  jmcneill #define		HVF_CNTRL_1_VQR_FULL	HVF_CNTRL_1_VQR(0)
    160  1.1  jmcneill #define	TDA_ENABLE_SPACE	MKREG(0x00, 0xd6)
    161  1.1  jmcneill #define	TDA_RPT_CNTRL		MKREG(0x00, 0xf0)
    162  1.1  jmcneill 
    163  1.1  jmcneill #define	TDA_PLL_SERIAL_1	MKREG(0x02, 0x00)
    164  1.1  jmcneill #define		PLL_SERIAL_1_SRL_MAN_IP	(1 << 6)
    165  1.1  jmcneill #define	TDA_PLL_SERIAL_2	MKREG(0x02, 0x01)
    166  1.1  jmcneill #define		PLL_SERIAL_2_SRL_PR(x)		(((x) & 0xf) << 4)
    167  1.1  jmcneill #define		PLL_SERIAL_2_SRL_NOSC(x)	(((x) & 0x3) << 0)
    168  1.1  jmcneill #define	TDA_PLL_SERIAL_3	MKREG(0x02, 0x02)
    169  1.1  jmcneill #define		PLL_SERIAL_3_SRL_PXIN_SEL	(1 << 4)
    170  1.1  jmcneill #define		PLL_SERIAL_3_SRL_DE		(1 << 2)
    171  1.1  jmcneill #define		PLL_SERIAL_3_SRL_CCIR		(1 << 0)
    172  1.1  jmcneill #define	TDA_SERIALIZER		MKREG(0x02, 0x03)
    173  1.1  jmcneill #define	TDA_BUFFER_OUT		MKREG(0x02, 0x04)
    174  1.1  jmcneill #define	TDA_PLL_SCG1		MKREG(0x02, 0x05)
    175  1.1  jmcneill #define	TDA_PLL_SCG2		MKREG(0x02, 0x06)
    176  1.1  jmcneill #define	TDA_PLL_SCGN1		MKREG(0x02, 0x07)
    177  1.1  jmcneill #define	TDA_PLL_SCGN2		MKREG(0x02, 0x08)
    178  1.1  jmcneill #define	TDA_PLL_SCGR1		MKREG(0x02, 0x09)
    179  1.1  jmcneill #define	TDA_PLL_SCGR2		MKREG(0x02, 0x0a)
    180  1.1  jmcneill 
    181  1.1  jmcneill #define	TDA_SEL_CLK		MKREG(0x02, 0x11)
    182  1.1  jmcneill #define		SEL_CLK_ENA_SC_CLK	(1 << 3)
    183  1.1  jmcneill #define		SEL_CLK_SEL_VRF_CLK(x)	(((x) & 3) << 1)
    184  1.1  jmcneill #define		SEL_CLK_SEL_CLK1	(1 << 0)
    185  1.1  jmcneill #define	TDA_ANA_GENERAL		MKREG(0x02, 0x12)
    186  1.1  jmcneill 
    187  1.1  jmcneill #define	TDA_EDID_DATA0		MKREG(0x09, 0x00)
    188  1.1  jmcneill #define	TDA_EDID_CTRL		MKREG(0x09, 0xfa)
    189  1.1  jmcneill #define	TDA_DDC_ADDR		MKREG(0x09, 0xfb)
    190  1.1  jmcneill #define	TDA_DDC_OFFS		MKREG(0x09, 0xfc)
    191  1.1  jmcneill #define	TDA_DDC_SEGM_ADDR	MKREG(0x09, 0xfd)
    192  1.1  jmcneill #define	TDA_DDC_SEGM		MKREG(0x09, 0xfe)
    193  1.1  jmcneill 
    194  1.1  jmcneill #define	TDA_IF_VSP		MKREG(0x10, 0x20)
    195  1.1  jmcneill #define	TDA_IF_AVI		MKREG(0x10, 0x40)
    196  1.1  jmcneill #define	TDA_IF_SPD		MKREG(0x10, 0x60)
    197  1.1  jmcneill #define	TDA_IF_AUD		MKREG(0x10, 0x80)
    198  1.1  jmcneill #define	TDA_IF_MPS		MKREG(0x10, 0xa0)
    199  1.1  jmcneill 
    200  1.1  jmcneill #define	TDA_ENC_CNTRL		MKREG(0x11, 0x0d)
    201  1.1  jmcneill #define		ENC_CNTRL_DVI_MODE	(0 << 2)
    202  1.1  jmcneill #define		ENC_CNTRL_HDMI_MODE	(1 << 2)
    203  1.1  jmcneill #define	TDA_DIP_IF_FLAGS	MKREG(0x11, 0x0f)
    204  1.1  jmcneill #define		DIP_IF_FLAGS_IF5	(1 << 5)
    205  1.1  jmcneill #define		DIP_IF_FLAGS_IF4	(1 << 4)
    206  1.1  jmcneill #define		DIP_IF_FLAGS_IF3	(1 << 3)
    207  1.1  jmcneill #define		DIP_IF_FLAGS_IF2	(1 << 2) /* AVI IF on page 10h */
    208  1.1  jmcneill #define		DIP_IF_FLAGS_IF1	(1 << 1)
    209  1.1  jmcneill 
    210  1.1  jmcneill #define	TDA_TX3			MKREG(0x12, 0x9a)
    211  1.1  jmcneill #define	TDA_TX4			MKREG(0x12, 0x9b)
    212  1.1  jmcneill #define		TX4_PD_RAM		(1 << 1)
    213  1.1  jmcneill #define	TDA_HDCP_TX33		MKREG(0x12, 0xb8)
    214  1.1  jmcneill #define		HDCP_TX33_HDMI		(1 << 1)
    215  1.1  jmcneill 
    216  1.1  jmcneill #define	TDA_CURPAGE_ADDR	0xff
    217  1.1  jmcneill 
    218  1.1  jmcneill #define	TDA_CEC_RXSHPDLEV	0xfe
    219  1.1  jmcneill #define		RXSHPDLEV_HPD	__BIT(1)
    220  1.1  jmcneill 
    221  1.1  jmcneill #define	TDA_CEC_ENAMODS		0xff
    222  1.1  jmcneill #define		ENAMODS_RXSENS		(1 << 2)
    223  1.1  jmcneill #define		ENAMODS_HDMI		(1 << 1)
    224  1.1  jmcneill #define	TDA_CEC_FRO_IM_CLK_CTRL	0xfb
    225  1.1  jmcneill #define		CEC_FRO_IM_CLK_CTRL_GHOST_DIS	(1 << 7)
    226  1.1  jmcneill #define		CEC_FRO_IM_CLK_CTRL_IMCLK_SEL	(1 << 1)
    227  1.1  jmcneill 
    228  1.1  jmcneill /* EDID reading */
    229  1.1  jmcneill #define	MAX_READ_ATTEMPTS	100
    230  1.1  jmcneill 
    231  1.1  jmcneill /* EDID fields */
    232  1.1  jmcneill #define	EDID_MODES0		35
    233  1.1  jmcneill #define	EDID_MODES1		36
    234  1.1  jmcneill #define	EDID_TIMING_START	38
    235  1.1  jmcneill #define	EDID_TIMING_END		54
    236  1.1  jmcneill #define	EDID_TIMING_X(v)	(((v) + 31) * 8)
    237  1.1  jmcneill #define	EDID_FREQ(v)		(((v) & 0x3f) + 60)
    238  1.1  jmcneill #define	EDID_RATIO(v)		(((v) >> 6) & 0x3)
    239  1.1  jmcneill #define	EDID_RATIO_10x16	0
    240  1.1  jmcneill #define	EDID_RATIO_3x4		1
    241  1.1  jmcneill #define	EDID_RATIO_4x5		2
    242  1.1  jmcneill #define	EDID_RATIO_9x16		3
    243  1.1  jmcneill 
    244  1.1  jmcneill #define	TDA19988		0x0301
    245  1.1  jmcneill 
    246  1.1  jmcneill static const struct device_compatible_entry compat_data[] = {
    247  1.1  jmcneill 	{ "nxp,tda998x",	1 },
    248  1.1  jmcneill 	{ NULL }
    249  1.1  jmcneill };
    250  1.1  jmcneill 
    251  1.1  jmcneill struct tda19988_softc;
    252  1.1  jmcneill 
    253  1.1  jmcneill struct tda19988_connector {
    254  1.1  jmcneill 	struct drm_connector	base;
    255  1.1  jmcneill 	struct tda19988_softc	*sc;
    256  1.1  jmcneill };
    257  1.1  jmcneill 
    258  1.1  jmcneill struct tda19988_softc {
    259  1.1  jmcneill 	device_t		sc_dev;
    260  1.3  jmcneill 	int			sc_phandle;
    261  1.1  jmcneill 	i2c_tag_t		sc_i2c;
    262  1.1  jmcneill 	i2c_addr_t		sc_addr;
    263  1.1  jmcneill 	uint32_t		sc_cec_addr;
    264  1.1  jmcneill 	uint16_t		sc_version;
    265  1.1  jmcneill 	int			sc_current_page;
    266  1.1  jmcneill 	uint8_t			*sc_edid;
    267  1.1  jmcneill 	uint32_t		sc_edid_len;
    268  1.3  jmcneill 	bool			sc_edid_valid;
    269  1.1  jmcneill 
    270  1.1  jmcneill 	struct drm_bridge	sc_bridge;
    271  1.1  jmcneill 	struct tda19988_connector sc_connector;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	struct fdt_device_ports	sc_ports;
    274  1.3  jmcneill 
    275  1.3  jmcneill 	enum drm_connector_status sc_last_status;
    276  1.1  jmcneill };
    277  1.1  jmcneill 
    278  1.1  jmcneill #define	to_tda_connector(x)	container_of(x, struct tda19988_connector, base)
    279  1.1  jmcneill 
    280  1.1  jmcneill static int
    281  1.1  jmcneill tda19988_set_page(struct tda19988_softc *sc, uint8_t page)
    282  1.1  jmcneill {
    283  1.1  jmcneill 	uint8_t buf[2] = { TDA_CURPAGE_ADDR, page };
    284  1.1  jmcneill 	int result;
    285  1.1  jmcneill 
    286  1.3  jmcneill 	result = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, buf, 2, NULL, 0,
    287  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    288  1.1  jmcneill 	if (result == 0)
    289  1.1  jmcneill 		sc->sc_current_page = page;
    290  1.1  jmcneill 
    291  1.1  jmcneill 	return result;
    292  1.1  jmcneill }
    293  1.1  jmcneill 
    294  1.1  jmcneill static int
    295  1.1  jmcneill tda19988_cec_read(struct tda19988_softc *sc, uint8_t addr, uint8_t *data)
    296  1.1  jmcneill {
    297  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_cec_addr, &addr, 1, data, 1,
    298  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    299  1.1  jmcneill }
    300  1.1  jmcneill 
    301  1.1  jmcneill static int
    302  1.1  jmcneill tda19988_cec_write(struct tda19988_softc *sc, uint8_t addr, uint8_t data)
    303  1.1  jmcneill {
    304  1.1  jmcneill 	uint8_t buf[2] = { addr, data };
    305  1.1  jmcneill 
    306  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_cec_addr, buf, 2, NULL, 0,
    307  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    308  1.1  jmcneill }
    309  1.1  jmcneill 
    310  1.1  jmcneill static int
    311  1.1  jmcneill tda19988_block_read(struct tda19988_softc *sc, uint16_t addr, uint8_t *data, int len)
    312  1.1  jmcneill {
    313  1.1  jmcneill 	uint8_t reg;
    314  1.1  jmcneill 
    315  1.1  jmcneill 	reg = REGADDR(addr);
    316  1.1  jmcneill 
    317  1.1  jmcneill 	if (sc->sc_current_page != REGPAGE(addr))
    318  1.1  jmcneill 		tda19988_set_page(sc, REGPAGE(addr));
    319  1.1  jmcneill 
    320  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1, data, len,
    321  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    322  1.1  jmcneill }
    323  1.1  jmcneill 
    324  1.1  jmcneill static int
    325  1.1  jmcneill tda19988_reg_read(struct tda19988_softc *sc, uint16_t addr, uint8_t *data)
    326  1.1  jmcneill {
    327  1.1  jmcneill 	uint8_t reg;
    328  1.1  jmcneill 
    329  1.1  jmcneill 	reg = REGADDR(addr);
    330  1.1  jmcneill 
    331  1.1  jmcneill 	if (sc->sc_current_page != REGPAGE(addr))
    332  1.1  jmcneill 		tda19988_set_page(sc, REGPAGE(addr));
    333  1.1  jmcneill 
    334  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, &reg, 1, data, 1,
    335  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    336  1.1  jmcneill }
    337  1.1  jmcneill 
    338  1.1  jmcneill static int
    339  1.1  jmcneill tda19988_reg_write(struct tda19988_softc *sc, uint16_t addr, uint8_t data)
    340  1.1  jmcneill {
    341  1.1  jmcneill 	uint8_t buf[2] = { REGADDR(addr), data };
    342  1.1  jmcneill 
    343  1.1  jmcneill 	if (sc->sc_current_page != REGPAGE(addr))
    344  1.1  jmcneill 		tda19988_set_page(sc, REGPAGE(addr));
    345  1.1  jmcneill 
    346  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, buf, 2, NULL, 0,
    347  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    348  1.1  jmcneill }
    349  1.1  jmcneill 
    350  1.1  jmcneill static int
    351  1.1  jmcneill tda19988_reg_write2(struct tda19988_softc *sc, uint16_t address, uint16_t data)
    352  1.1  jmcneill {
    353  1.1  jmcneill 	uint8_t buf[3];
    354  1.1  jmcneill 
    355  1.1  jmcneill 	buf[0] = REGADDR(address);
    356  1.1  jmcneill 	buf[1] = (data >> 8);
    357  1.1  jmcneill 	buf[2] = (data & 0xff);
    358  1.1  jmcneill 
    359  1.1  jmcneill 	if (sc->sc_current_page != REGPAGE(address))
    360  1.1  jmcneill 		tda19988_set_page(sc, REGPAGE(address));
    361  1.1  jmcneill 
    362  1.3  jmcneill 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, buf, 3, NULL, 0,
    363  1.3  jmcneill 	    cold ? I2C_F_POLL : 0);
    364  1.1  jmcneill }
    365  1.1  jmcneill 
    366  1.1  jmcneill static void
    367  1.1  jmcneill tda19988_reg_set(struct tda19988_softc *sc, uint16_t addr, uint8_t flags)
    368  1.1  jmcneill {
    369  1.1  jmcneill 	uint8_t data;
    370  1.1  jmcneill 
    371  1.1  jmcneill 	tda19988_reg_read(sc, addr, &data);
    372  1.1  jmcneill 	data |= flags;
    373  1.1  jmcneill 	tda19988_reg_write(sc, addr, data);
    374  1.1  jmcneill }
    375  1.1  jmcneill 
    376  1.1  jmcneill static void
    377  1.1  jmcneill tda19988_reg_clear(struct tda19988_softc *sc, uint16_t addr, uint8_t flags)
    378  1.1  jmcneill {
    379  1.1  jmcneill 	uint8_t data;
    380  1.1  jmcneill 
    381  1.1  jmcneill 	tda19988_reg_read(sc, addr, &data);
    382  1.1  jmcneill 	data &= ~flags;
    383  1.1  jmcneill 	tda19988_reg_write(sc, addr, data);
    384  1.1  jmcneill }
    385  1.1  jmcneill 
    386  1.1  jmcneill static int
    387  1.1  jmcneill tda19988_match(device_t parent, cfdata_t match, void *aux)
    388  1.1  jmcneill {
    389  1.1  jmcneill 	struct i2c_attach_args * const ia = aux;
    390  1.1  jmcneill 	int match_result;
    391  1.1  jmcneill 
    392  1.1  jmcneill 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    393  1.1  jmcneill 		return match_result;
    394  1.1  jmcneill 
    395  1.1  jmcneill 	return 0;
    396  1.1  jmcneill }
    397  1.1  jmcneill 
    398  1.1  jmcneill static void
    399  1.1  jmcneill tda19988_init_encoder(struct tda19988_softc *sc, const struct drm_display_mode *mode)
    400  1.1  jmcneill {
    401  1.1  jmcneill 	uint16_t ref_pix, ref_line, n_pix, n_line;
    402  1.1  jmcneill 	uint16_t hs_pix_start, hs_pix_stop;
    403  1.1  jmcneill 	uint16_t vs1_pix_start, vs1_pix_stop;
    404  1.1  jmcneill 	uint16_t vs1_line_start, vs1_line_end;
    405  1.1  jmcneill 	uint16_t vs2_pix_start, vs2_pix_stop;
    406  1.1  jmcneill 	uint16_t vs2_line_start, vs2_line_end;
    407  1.1  jmcneill 	uint16_t vwin1_line_start, vwin1_line_end;
    408  1.1  jmcneill 	uint16_t vwin2_line_start, vwin2_line_end;
    409  1.1  jmcneill 	uint16_t de_start, de_stop;
    410  1.1  jmcneill 	uint8_t reg, div;
    411  1.1  jmcneill 
    412  1.1  jmcneill 	n_pix = mode->crtc_htotal;
    413  1.1  jmcneill 	n_line = mode->crtc_vtotal;
    414  1.1  jmcneill 
    415  1.1  jmcneill 	hs_pix_stop = mode->crtc_hsync_end - mode->crtc_hdisplay;
    416  1.1  jmcneill 	hs_pix_start = mode->crtc_hsync_start - mode->crtc_hdisplay;
    417  1.1  jmcneill 
    418  1.1  jmcneill 	de_stop = mode->crtc_htotal;
    419  1.1  jmcneill 	de_start = mode->crtc_htotal - mode->crtc_hdisplay;
    420  1.1  jmcneill 	ref_pix = hs_pix_start + 3;
    421  1.1  jmcneill 
    422  1.2  jmcneill 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
    423  1.1  jmcneill 		ref_pix += mode->crtc_hskew;
    424  1.1  jmcneill 
    425  1.2  jmcneill 	if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
    426  1.1  jmcneill 		ref_line = 1 + mode->crtc_vsync_start - mode->crtc_vdisplay;
    427  1.1  jmcneill 		vwin1_line_start = mode->crtc_vtotal - mode->crtc_vdisplay - 1;
    428  1.1  jmcneill 		vwin1_line_end = vwin1_line_start + mode->crtc_vdisplay;
    429  1.1  jmcneill 
    430  1.1  jmcneill 		vs1_pix_start = vs1_pix_stop = hs_pix_start;
    431  1.1  jmcneill 		vs1_line_start = mode->crtc_vsync_start - mode->crtc_vdisplay;
    432  1.1  jmcneill 		vs1_line_end = vs1_line_start + mode->crtc_vsync_end - mode->crtc_vsync_start;
    433  1.1  jmcneill 
    434  1.1  jmcneill 		vwin2_line_start = vwin2_line_end = 0;
    435  1.1  jmcneill 		vs2_pix_start = vs2_pix_stop = 0;
    436  1.1  jmcneill 		vs2_line_start = vs2_line_end = 0;
    437  1.1  jmcneill 	} else {
    438  1.1  jmcneill 		ref_line = 1 + (mode->crtc_vsync_start - mode->crtc_vdisplay)/2;
    439  1.1  jmcneill 		vwin1_line_start = (mode->crtc_vtotal - mode->crtc_vdisplay)/2;
    440  1.1  jmcneill 		vwin1_line_end = vwin1_line_start + mode->crtc_vdisplay/2;
    441  1.1  jmcneill 
    442  1.1  jmcneill 		vs1_pix_start = vs1_pix_stop = hs_pix_start;
    443  1.1  jmcneill 		vs1_line_start = (mode->crtc_vsync_start - mode->crtc_vdisplay)/2;
    444  1.1  jmcneill 		vs1_line_end = vs1_line_start + (mode->crtc_vsync_end - mode->crtc_vsync_start)/2;
    445  1.1  jmcneill 
    446  1.1  jmcneill 		vwin2_line_start = vwin1_line_start + mode->crtc_vtotal/2;
    447  1.1  jmcneill 		vwin2_line_end = vwin2_line_start + mode->crtc_vdisplay/2;
    448  1.1  jmcneill 
    449  1.1  jmcneill 		vs2_pix_start = vs2_pix_stop = hs_pix_start + mode->crtc_htotal/2;
    450  1.1  jmcneill 		vs2_line_start = vs1_line_start + mode->crtc_vtotal/2 ;
    451  1.1  jmcneill 		vs2_line_end = vs2_line_start + (mode->crtc_vsync_end - mode->crtc_vsync_start)/2;
    452  1.1  jmcneill 	}
    453  1.1  jmcneill 
    454  1.1  jmcneill 	div = 148500 / mode->crtc_clock;
    455  1.1  jmcneill 	if (div != 0) {
    456  1.1  jmcneill 		div--;
    457  1.1  jmcneill 		if (div > 3)
    458  1.1  jmcneill 			div = 3;
    459  1.1  jmcneill 	}
    460  1.1  jmcneill 
    461  1.1  jmcneill 	/* set HDMI HDCP mode off */
    462  1.1  jmcneill 	tda19988_reg_set(sc, TDA_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
    463  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_HDCP_TX33, HDCP_TX33_HDMI);
    464  1.1  jmcneill 	tda19988_reg_write(sc, TDA_ENC_CNTRL, ENC_CNTRL_DVI_MODE);
    465  1.1  jmcneill 
    466  1.1  jmcneill 	/* no pre-filter or interpolator */
    467  1.1  jmcneill 	tda19988_reg_write(sc, TDA_HVF_CNTRL_0,
    468  1.1  jmcneill 	    HVF_CNTRL_0_INTPOL_BYPASS | HVF_CNTRL_0_PREFIL_NONE);
    469  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
    470  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_4,
    471  1.1  jmcneill 	    VIP_CNTRL_4_BLANKIT_NDE | VIP_CNTRL_4_BLC_NONE);
    472  1.1  jmcneill 
    473  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR);
    474  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IP);
    475  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_PLL_SERIAL_3, PLL_SERIAL_3_SRL_DE);
    476  1.1  jmcneill 	tda19988_reg_write(sc, TDA_SERIALIZER, 0);
    477  1.1  jmcneill 	tda19988_reg_write(sc, TDA_HVF_CNTRL_1, HVF_CNTRL_1_VQR_FULL);
    478  1.1  jmcneill 
    479  1.1  jmcneill 	tda19988_reg_write(sc, TDA_RPT_CNTRL, 0);
    480  1.1  jmcneill 	tda19988_reg_write(sc, TDA_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
    481  1.1  jmcneill 			SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
    482  1.1  jmcneill 
    483  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
    484  1.1  jmcneill 			PLL_SERIAL_2_SRL_PR(0));
    485  1.1  jmcneill 
    486  1.1  jmcneill 	tda19988_reg_set(sc, TDA_MAT_CONTRL, MAT_CONTRL_MAT_BP);
    487  1.1  jmcneill 
    488  1.1  jmcneill 	tda19988_reg_write(sc, TDA_ANA_GENERAL, 0x09);
    489  1.1  jmcneill 
    490  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_TBG_CNTRL_0, TBG_CNTRL_0_SYNC_MTHD);
    491  1.1  jmcneill 
    492  1.1  jmcneill 	/*
    493  1.1  jmcneill 	 * Sync on rising HSYNC/VSYNC
    494  1.1  jmcneill 	 */
    495  1.1  jmcneill 	reg = VIP_CNTRL_3_SYNC_HS;
    496  1.2  jmcneill 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
    497  1.1  jmcneill 		reg |= VIP_CNTRL_3_H_TGL;
    498  1.2  jmcneill 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
    499  1.1  jmcneill 		reg |= VIP_CNTRL_3_V_TGL;
    500  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_3, reg);
    501  1.1  jmcneill 
    502  1.1  jmcneill 	reg = TBG_CNTRL_1_TGL_EN;
    503  1.2  jmcneill 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
    504  1.1  jmcneill 		reg |= TBG_CNTRL_1_H_TGL;
    505  1.2  jmcneill 	if (mode->flags & DRM_MODE_FLAG_NVSYNC)
    506  1.1  jmcneill 		reg |= TBG_CNTRL_1_V_TGL;
    507  1.1  jmcneill 	tda19988_reg_write(sc, TDA_TBG_CNTRL_1, reg);
    508  1.1  jmcneill 
    509  1.1  jmcneill 	/* Program timing */
    510  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIDFORMAT, 0x00);
    511  1.1  jmcneill 
    512  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_REFPIX_MSB, ref_pix);
    513  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_REFLINE_MSB, ref_line);
    514  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_NPIX_MSB, n_pix);
    515  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_NLINE_MSB, n_line);
    516  1.1  jmcneill 
    517  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_LINE_STRT_1_MSB, vs1_line_start);
    518  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_PIX_STRT_1_MSB, vs1_pix_start);
    519  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_LINE_END_1_MSB, vs1_line_end);
    520  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_PIX_END_1_MSB, vs1_pix_stop);
    521  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_LINE_STRT_2_MSB, vs2_line_start);
    522  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_PIX_STRT_2_MSB, vs2_pix_start);
    523  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_LINE_END_2_MSB, vs2_line_end);
    524  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VS_PIX_END_2_MSB, vs2_pix_stop);
    525  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_HS_PIX_START_MSB, hs_pix_start);
    526  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_HS_PIX_STOP_MSB, hs_pix_stop);
    527  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VWIN_START_1_MSB, vwin1_line_start);
    528  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VWIN_END_1_MSB, vwin1_line_end);
    529  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VWIN_START_2_MSB, vwin2_line_start);
    530  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_VWIN_END_2_MSB, vwin2_line_end);
    531  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_DE_START_MSB, de_start);
    532  1.1  jmcneill 	tda19988_reg_write2(sc, TDA_DE_STOP_MSB, de_stop);
    533  1.1  jmcneill 
    534  1.1  jmcneill 	if (sc->sc_version == TDA19988)
    535  1.1  jmcneill 		tda19988_reg_write(sc, TDA_ENABLE_SPACE, 0x00);
    536  1.1  jmcneill 
    537  1.1  jmcneill 	/* must be last register set */
    538  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_TBG_CNTRL_0, TBG_CNTRL_0_SYNC_ONCE);
    539  1.1  jmcneill }
    540  1.1  jmcneill 
    541  1.1  jmcneill static int
    542  1.1  jmcneill tda19988_read_edid_block(struct tda19988_softc *sc, uint8_t *buf, int block)
    543  1.1  jmcneill {
    544  1.1  jmcneill 	int attempt, err;
    545  1.1  jmcneill 	uint8_t data;
    546  1.1  jmcneill 
    547  1.1  jmcneill 	err = 0;
    548  1.1  jmcneill 
    549  1.1  jmcneill 	tda19988_reg_set(sc, TDA_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
    550  1.1  jmcneill 
    551  1.1  jmcneill 	/* Block 0 */
    552  1.1  jmcneill 	tda19988_reg_write(sc, TDA_DDC_ADDR, 0xa0);
    553  1.1  jmcneill 	tda19988_reg_write(sc, TDA_DDC_OFFS, (block % 2) ? 128 : 0);
    554  1.1  jmcneill 	tda19988_reg_write(sc, TDA_DDC_SEGM_ADDR, 0x60);
    555  1.1  jmcneill 	tda19988_reg_write(sc, TDA_DDC_SEGM, block / 2);
    556  1.1  jmcneill 
    557  1.1  jmcneill 	tda19988_reg_write(sc, TDA_EDID_CTRL, 1);
    558  1.1  jmcneill 	tda19988_reg_write(sc, TDA_EDID_CTRL, 0);
    559  1.1  jmcneill 
    560  1.1  jmcneill 	data = 0;
    561  1.1  jmcneill 	for (attempt = 0; attempt < MAX_READ_ATTEMPTS; attempt++) {
    562  1.1  jmcneill 		tda19988_reg_read(sc, TDA_INT_FLAGS_2, &data);
    563  1.1  jmcneill 		if (data & INT_FLAGS_2_EDID_BLK_RD)
    564  1.1  jmcneill 			break;
    565  1.1  jmcneill 		delay(1000);
    566  1.1  jmcneill 	}
    567  1.1  jmcneill 
    568  1.1  jmcneill 	if (attempt == MAX_READ_ATTEMPTS) {
    569  1.1  jmcneill 		err = -1;
    570  1.1  jmcneill 		goto done;
    571  1.1  jmcneill 	}
    572  1.1  jmcneill 
    573  1.1  jmcneill 	if (tda19988_block_read(sc, TDA_EDID_DATA0, buf, EDID_LENGTH) != 0) {
    574  1.1  jmcneill 		err = -1;
    575  1.1  jmcneill 		goto done;
    576  1.1  jmcneill 	}
    577  1.1  jmcneill 
    578  1.1  jmcneill done:
    579  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
    580  1.1  jmcneill 
    581  1.1  jmcneill 	return (err);
    582  1.1  jmcneill }
    583  1.1  jmcneill 
    584  1.1  jmcneill static int
    585  1.1  jmcneill tda19988_read_edid(struct tda19988_softc *sc)
    586  1.1  jmcneill {
    587  1.1  jmcneill 	int err;
    588  1.1  jmcneill 	int blocks, i;
    589  1.1  jmcneill 	uint8_t *buf, *edid;
    590  1.1  jmcneill 
    591  1.1  jmcneill 	err = 0;
    592  1.1  jmcneill 	if (sc->sc_version == TDA19988)
    593  1.1  jmcneill 		tda19988_reg_clear(sc, TDA_TX4, TX4_PD_RAM);
    594  1.1  jmcneill 
    595  1.1  jmcneill 	err = tda19988_read_edid_block(sc, sc->sc_edid, 0);
    596  1.1  jmcneill 	if (err)
    597  1.1  jmcneill 		goto done;
    598  1.1  jmcneill 
    599  1.1  jmcneill 	blocks = sc->sc_edid[0x7e];
    600  1.1  jmcneill 	if (blocks > 0) {
    601  1.1  jmcneill 		if (sc->sc_edid_len != EDID_LENGTH*(blocks+1)) {
    602  1.1  jmcneill 			edid = kmem_zalloc(EDID_LENGTH*(blocks+1), KM_SLEEP);
    603  1.1  jmcneill 			memcpy(edid, sc->sc_edid, EDID_LENGTH);
    604  1.1  jmcneill 			kmem_free(sc->sc_edid, sc->sc_edid_len);
    605  1.1  jmcneill 			sc->sc_edid = edid;
    606  1.1  jmcneill 			sc->sc_edid_len = EDID_LENGTH*(blocks+1);
    607  1.1  jmcneill 		}
    608  1.1  jmcneill 		for (i = 0; i < blocks; i++) {
    609  1.1  jmcneill 			/* TODO: check validity */
    610  1.1  jmcneill 			buf = sc->sc_edid + EDID_LENGTH*(i+1);
    611  1.1  jmcneill 			err = tda19988_read_edid_block(sc, buf, i);
    612  1.1  jmcneill 			if (err)
    613  1.1  jmcneill 				goto done;
    614  1.1  jmcneill 		}
    615  1.1  jmcneill 	}
    616  1.1  jmcneill 
    617  1.1  jmcneill done:
    618  1.1  jmcneill 	if (sc->sc_version == TDA19988)
    619  1.1  jmcneill 		tda19988_reg_set(sc, TDA_TX4, TX4_PD_RAM);
    620  1.1  jmcneill 
    621  1.1  jmcneill 	return (err);
    622  1.1  jmcneill }
    623  1.1  jmcneill 
    624  1.1  jmcneill static void
    625  1.1  jmcneill tda19988_start(struct tda19988_softc *sc)
    626  1.1  jmcneill {
    627  1.1  jmcneill 	device_t dev;
    628  1.1  jmcneill 	uint8_t data;
    629  1.1  jmcneill 	uint16_t ver;
    630  1.1  jmcneill 
    631  1.1  jmcneill 	dev = sc->sc_dev;
    632  1.1  jmcneill 
    633  1.1  jmcneill 	tda19988_cec_write(sc, TDA_CEC_ENAMODS, ENAMODS_RXSENS | ENAMODS_HDMI);
    634  1.1  jmcneill 	DELAY(1000);
    635  1.1  jmcneill 	tda19988_cec_read(sc, TDA_CEC_RXSHPDLEV, &data);
    636  1.1  jmcneill 
    637  1.1  jmcneill 	/* Reset core */
    638  1.1  jmcneill 	tda19988_reg_set(sc, TDA_SOFTRESET, 3);
    639  1.1  jmcneill 	DELAY(100);
    640  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_SOFTRESET, 3);
    641  1.1  jmcneill 	DELAY(100);
    642  1.1  jmcneill 
    643  1.1  jmcneill 	/* reset transmitter: */
    644  1.1  jmcneill 	tda19988_reg_set(sc, TDA_MAIN_CNTRL0, MAIN_CNTRL0_SR);
    645  1.1  jmcneill 	tda19988_reg_clear(sc, TDA_MAIN_CNTRL0, MAIN_CNTRL0_SR);
    646  1.1  jmcneill 
    647  1.1  jmcneill 	/* PLL registers common configuration */
    648  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SERIAL_1, 0x00);
    649  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
    650  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SERIAL_3, 0x00);
    651  1.1  jmcneill 	tda19988_reg_write(sc, TDA_SERIALIZER, 0x00);
    652  1.1  jmcneill 	tda19988_reg_write(sc, TDA_BUFFER_OUT, 0x00);
    653  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCG1, 0x00);
    654  1.1  jmcneill 	tda19988_reg_write(sc, TDA_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
    655  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCGN1, 0xfa);
    656  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCGN2, 0x00);
    657  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCGR1, 0x5b);
    658  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCGR2, 0x00);
    659  1.1  jmcneill 	tda19988_reg_write(sc, TDA_PLL_SCG2, 0x10);
    660  1.1  jmcneill 
    661  1.1  jmcneill 	/* Write the default value MUX register */
    662  1.1  jmcneill 	tda19988_reg_write(sc, TDA_MUX_VP_VIP_OUT, 0x24);
    663  1.1  jmcneill 
    664  1.1  jmcneill 	ver = 0;
    665  1.1  jmcneill 	tda19988_reg_read(sc, TDA_VERSION, &data);
    666  1.1  jmcneill 	ver |= data;
    667  1.1  jmcneill 	tda19988_reg_read(sc, TDA_VERSION_MSB, &data);
    668  1.1  jmcneill 	ver |= (data << 8);
    669  1.1  jmcneill 
    670  1.1  jmcneill 	/* Clear feature bits */
    671  1.1  jmcneill 	sc->sc_version = ver & ~0x30;
    672  1.1  jmcneill 	switch (sc->sc_version) {
    673  1.1  jmcneill 		case TDA19988:
    674  1.1  jmcneill 			device_printf(dev, "TDA19988\n");
    675  1.1  jmcneill 			break;
    676  1.1  jmcneill 		default:
    677  1.1  jmcneill 			device_printf(dev, "Unknown device: %04x\n", sc->sc_version);
    678  1.1  jmcneill 			return;
    679  1.1  jmcneill 	}
    680  1.1  jmcneill 
    681  1.1  jmcneill 	tda19988_reg_write(sc, TDA_DDC_CTRL, DDC_ENABLE);
    682  1.1  jmcneill 	tda19988_reg_write(sc, TDA_TX3, 39);
    683  1.1  jmcneill 
    684  1.1  jmcneill     	tda19988_cec_write(sc, TDA_CEC_FRO_IM_CLK_CTRL,
    685  1.1  jmcneill             CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
    686  1.1  jmcneill 
    687  1.1  jmcneill 	/* Default values for RGB 4:4:4 mapping */
    688  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_0, 0x23);
    689  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_1, 0x01);
    690  1.1  jmcneill 	tda19988_reg_write(sc, TDA_VIP_CNTRL_2, 0x45);
    691  1.1  jmcneill }
    692  1.1  jmcneill 
    693  1.1  jmcneill static enum drm_connector_status
    694  1.1  jmcneill tda19988_connector_detect(struct drm_connector *connector, bool force)
    695  1.1  jmcneill {
    696  1.1  jmcneill 	struct tda19988_connector *tda_connector = to_tda_connector(connector);
    697  1.1  jmcneill 	struct tda19988_softc * const sc = tda_connector->sc;
    698  1.3  jmcneill 	enum drm_connector_status status;
    699  1.1  jmcneill 	uint8_t data = 0;
    700  1.1  jmcneill 
    701  1.3  jmcneill 	iic_acquire_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    702  1.1  jmcneill 	tda19988_cec_read(sc, TDA_CEC_RXSHPDLEV, &data);
    703  1.3  jmcneill 	iic_release_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    704  1.1  jmcneill 
    705  1.3  jmcneill 	status = (data & RXSHPDLEV_HPD) ?
    706  1.1  jmcneill 	    connector_status_connected :
    707  1.1  jmcneill 	    connector_status_disconnected;
    708  1.3  jmcneill 
    709  1.3  jmcneill 	/* On connect, invalidate the last EDID */
    710  1.3  jmcneill 	if (status == connector_status_connected &&
    711  1.3  jmcneill 	    sc->sc_last_status != connector_status_connected)
    712  1.3  jmcneill 		sc->sc_edid_valid = false;
    713  1.3  jmcneill 
    714  1.3  jmcneill 	sc->sc_last_status = status;
    715  1.3  jmcneill 
    716  1.3  jmcneill 	return status;
    717  1.1  jmcneill }
    718  1.1  jmcneill 
    719  1.1  jmcneill static void
    720  1.1  jmcneill tda19988_connector_destroy(struct drm_connector *connector)
    721  1.1  jmcneill {
    722  1.1  jmcneill 	drm_connector_unregister(connector);
    723  1.1  jmcneill 	drm_connector_cleanup(connector);
    724  1.1  jmcneill }
    725  1.1  jmcneill 
    726  1.1  jmcneill static const struct drm_connector_funcs tda19988_connector_funcs = {
    727  1.1  jmcneill 	.dpms = drm_helper_connector_dpms,
    728  1.1  jmcneill 	.detect = tda19988_connector_detect,
    729  1.1  jmcneill 	.fill_modes = drm_helper_probe_single_connector_modes,
    730  1.1  jmcneill 	.destroy = tda19988_connector_destroy,
    731  1.1  jmcneill };
    732  1.1  jmcneill 
    733  1.1  jmcneill static int
    734  1.1  jmcneill tda19988_connector_get_modes(struct drm_connector *connector)
    735  1.1  jmcneill {
    736  1.1  jmcneill 	struct tda19988_connector *tda_connector = to_tda_connector(connector);
    737  1.1  jmcneill 	struct tda19988_softc * const sc = tda_connector->sc;
    738  1.1  jmcneill 	struct edid *pedid = NULL;
    739  1.1  jmcneill 	int error;
    740  1.1  jmcneill 
    741  1.3  jmcneill 	if (sc->sc_edid_valid) {
    742  1.1  jmcneill 		pedid = (struct edid *)sc->sc_edid;
    743  1.3  jmcneill 	} else {
    744  1.3  jmcneill 		iic_acquire_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    745  1.3  jmcneill 		if (tda19988_read_edid(sc) == 0)
    746  1.3  jmcneill 			pedid = (struct edid *)sc->sc_edid;
    747  1.3  jmcneill 		iic_release_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    748  1.3  jmcneill 		sc->sc_edid_valid = true;
    749  1.3  jmcneill 	}
    750  1.1  jmcneill 
    751  1.1  jmcneill 	drm_mode_connector_update_edid_property(connector, pedid);
    752  1.1  jmcneill 	if (pedid == NULL)
    753  1.1  jmcneill 		return 0;
    754  1.1  jmcneill 
    755  1.1  jmcneill 	error = drm_add_edid_modes(connector, pedid);
    756  1.1  jmcneill 	drm_edid_to_eld(connector, pedid);
    757  1.1  jmcneill 
    758  1.1  jmcneill 	return error;
    759  1.1  jmcneill }
    760  1.1  jmcneill 
    761  1.1  jmcneill static struct drm_encoder *
    762  1.1  jmcneill tda19988_connector_best_encoder(struct drm_connector *connector)
    763  1.1  jmcneill {
    764  1.1  jmcneill 	int enc_id = connector->encoder_ids[0];
    765  1.1  jmcneill 	struct drm_mode_object *obj;
    766  1.1  jmcneill 	struct drm_encoder *encoder = NULL;
    767  1.1  jmcneill 
    768  1.1  jmcneill 	if (enc_id) {
    769  1.1  jmcneill 		obj = drm_mode_object_find(connector->dev, enc_id,
    770  1.1  jmcneill 		    DRM_MODE_OBJECT_ENCODER);
    771  1.1  jmcneill 		if (obj == NULL)
    772  1.1  jmcneill 			return NULL;
    773  1.1  jmcneill 		encoder = obj_to_encoder(obj);
    774  1.1  jmcneill 	}
    775  1.1  jmcneill 
    776  1.1  jmcneill 	return encoder;
    777  1.1  jmcneill }
    778  1.1  jmcneill 
    779  1.1  jmcneill static const struct drm_connector_helper_funcs tda19988_connector_helper_funcs = {
    780  1.1  jmcneill 	.get_modes = tda19988_connector_get_modes,
    781  1.1  jmcneill 	.best_encoder = tda19988_connector_best_encoder,
    782  1.1  jmcneill };
    783  1.1  jmcneill 
    784  1.1  jmcneill static int
    785  1.1  jmcneill tda19988_bridge_attach(struct drm_bridge *bridge)
    786  1.1  jmcneill {
    787  1.1  jmcneill 	struct tda19988_softc *sc = bridge->driver_private;
    788  1.1  jmcneill 	struct tda19988_connector *tda_connector = &sc->sc_connector;
    789  1.1  jmcneill 	struct drm_connector *connector = &tda_connector->base;
    790  1.1  jmcneill 	int error;
    791  1.1  jmcneill 
    792  1.1  jmcneill 	tda_connector->sc = sc;
    793  1.1  jmcneill 
    794  1.1  jmcneill 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
    795  1.1  jmcneill 	connector->interlace_allowed = 1;
    796  1.1  jmcneill 	connector->doublescan_allowed = 0;
    797  1.1  jmcneill 
    798  1.1  jmcneill 	drm_connector_init(bridge->dev, connector, &tda19988_connector_funcs,
    799  1.1  jmcneill 	    DRM_MODE_CONNECTOR_HDMIA);
    800  1.1  jmcneill 	drm_connector_helper_add(connector, &tda19988_connector_helper_funcs);
    801  1.1  jmcneill 
    802  1.1  jmcneill 	error = drm_mode_connector_attach_encoder(connector, bridge->encoder);
    803  1.1  jmcneill 	if (error != 0)
    804  1.1  jmcneill 		return error;
    805  1.1  jmcneill 
    806  1.1  jmcneill 	return drm_connector_register(connector);
    807  1.1  jmcneill }
    808  1.1  jmcneill 
    809  1.1  jmcneill static void
    810  1.1  jmcneill tda19988_bridge_enable(struct drm_bridge *bridge)
    811  1.1  jmcneill {
    812  1.1  jmcneill 	struct tda19988_softc * const sc = bridge->driver_private;
    813  1.1  jmcneill 
    814  1.3  jmcneill 	fdtbus_pinctrl_set_config(sc->sc_phandle, "default");
    815  1.1  jmcneill }
    816  1.1  jmcneill 
    817  1.1  jmcneill static void
    818  1.1  jmcneill tda19988_bridge_pre_enable(struct drm_bridge *bridge)
    819  1.1  jmcneill {
    820  1.1  jmcneill }
    821  1.1  jmcneill 
    822  1.1  jmcneill static void
    823  1.1  jmcneill tda19988_bridge_disable(struct drm_bridge *bridge)
    824  1.1  jmcneill {
    825  1.3  jmcneill 	struct tda19988_softc * const sc = bridge->driver_private;
    826  1.3  jmcneill 
    827  1.3  jmcneill 	fdtbus_pinctrl_set_config(sc->sc_phandle, "off");
    828  1.1  jmcneill }
    829  1.1  jmcneill 
    830  1.1  jmcneill static void
    831  1.1  jmcneill tda19988_bridge_post_disable(struct drm_bridge *bridge)
    832  1.1  jmcneill {
    833  1.1  jmcneill }
    834  1.1  jmcneill 
    835  1.1  jmcneill static void
    836  1.1  jmcneill tda19988_bridge_mode_set(struct drm_bridge *bridge,
    837  1.1  jmcneill     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    838  1.1  jmcneill {
    839  1.1  jmcneill 	struct tda19988_softc * const sc = bridge->driver_private;
    840  1.1  jmcneill 
    841  1.3  jmcneill 	iic_acquire_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    842  1.3  jmcneill 	tda19988_init_encoder(sc, adjusted_mode);
    843  1.3  jmcneill 	iic_release_bus(sc->sc_i2c, cold ? I2C_F_POLL : 0);
    844  1.1  jmcneill }
    845  1.1  jmcneill 
    846  1.1  jmcneill static bool
    847  1.1  jmcneill tda19988_bridge_mode_fixup(struct drm_bridge *bridge,
    848  1.1  jmcneill     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    849  1.1  jmcneill {
    850  1.1  jmcneill 	return true;
    851  1.1  jmcneill }
    852  1.1  jmcneill 
    853  1.1  jmcneill static const struct drm_bridge_funcs tda19988_bridge_funcs = {
    854  1.1  jmcneill 	.attach = tda19988_bridge_attach,
    855  1.1  jmcneill 	.enable = tda19988_bridge_enable,
    856  1.1  jmcneill 	.pre_enable = tda19988_bridge_pre_enable,
    857  1.1  jmcneill 	.disable = tda19988_bridge_disable,
    858  1.1  jmcneill 	.post_disable = tda19988_bridge_post_disable,
    859  1.1  jmcneill 	.mode_set = tda19988_bridge_mode_set,
    860  1.1  jmcneill 	.mode_fixup = tda19988_bridge_mode_fixup,
    861  1.1  jmcneill };
    862  1.1  jmcneill 
    863  1.1  jmcneill static int
    864  1.1  jmcneill tda19988_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    865  1.1  jmcneill {
    866  1.1  jmcneill 	struct tda19988_softc *sc = device_private(dev);
    867  1.1  jmcneill 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
    868  1.1  jmcneill 	struct drm_encoder *encoder;
    869  1.1  jmcneill 	int error;
    870  1.1  jmcneill 
    871  1.1  jmcneill 	if (!activate)
    872  1.1  jmcneill 		return EINVAL;
    873  1.1  jmcneill 
    874  1.1  jmcneill 	if (fdt_endpoint_port_index(ep) != TDA19988_PORT_INPUT)
    875  1.1  jmcneill 		return EINVAL;
    876  1.1  jmcneill 
    877  1.1  jmcneill 	switch (fdt_endpoint_type(in_ep)) {
    878  1.1  jmcneill 	case EP_DRM_ENCODER:
    879  1.1  jmcneill 		encoder = fdt_endpoint_get_data(in_ep);
    880  1.1  jmcneill 		break;
    881  1.1  jmcneill 	default:
    882  1.1  jmcneill 		encoder = NULL;
    883  1.1  jmcneill 		break;
    884  1.1  jmcneill 	}
    885  1.1  jmcneill 
    886  1.1  jmcneill 	if (encoder == NULL)
    887  1.1  jmcneill 		return EINVAL;
    888  1.1  jmcneill 
    889  1.1  jmcneill 	sc->sc_bridge.driver_private = sc;
    890  1.1  jmcneill 	sc->sc_bridge.funcs = &tda19988_bridge_funcs;
    891  1.1  jmcneill 	sc->sc_bridge.encoder = encoder;
    892  1.1  jmcneill 	error = drm_bridge_attach(encoder->dev, &sc->sc_bridge);
    893  1.1  jmcneill 	if (error != 0)
    894  1.1  jmcneill 		return EIO;
    895  1.1  jmcneill 
    896  1.1  jmcneill 	encoder->bridge = &sc->sc_bridge;
    897  1.1  jmcneill 
    898  1.1  jmcneill 	return 0;
    899  1.1  jmcneill }
    900  1.1  jmcneill 
    901  1.1  jmcneill static void *
    902  1.1  jmcneill tda19988_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    903  1.1  jmcneill {
    904  1.1  jmcneill 	struct tda19988_softc *sc = device_private(dev);
    905  1.1  jmcneill 
    906  1.1  jmcneill 	return &sc->sc_bridge;
    907  1.1  jmcneill }
    908  1.1  jmcneill 
    909  1.1  jmcneill static void
    910  1.1  jmcneill tda19988_attach(device_t parent, device_t self, void *aux)
    911  1.1  jmcneill {
    912  1.1  jmcneill 	struct tda19988_softc *sc = device_private(self);
    913  1.1  jmcneill 	struct i2c_attach_args * const ia = aux;
    914  1.1  jmcneill 	const int phandle = ia->ia_cookie;
    915  1.1  jmcneill 
    916  1.1  jmcneill 	sc->sc_dev = self;
    917  1.3  jmcneill 	sc->sc_phandle = phandle;
    918  1.1  jmcneill 	sc->sc_i2c = ia->ia_tag;
    919  1.1  jmcneill 	sc->sc_addr = ia->ia_addr;
    920  1.1  jmcneill 	sc->sc_cec_addr = 0x34; /* hardcoded */
    921  1.1  jmcneill 	sc->sc_current_page = 0xff;
    922  1.1  jmcneill 	sc->sc_edid = kmem_zalloc(EDID_LENGTH, KM_SLEEP);
    923  1.1  jmcneill 	sc->sc_edid_len = EDID_LENGTH;
    924  1.3  jmcneill 	sc->sc_edid_valid = false;
    925  1.3  jmcneill 	sc->sc_last_status = connector_status_unknown;
    926  1.1  jmcneill 
    927  1.1  jmcneill 	aprint_naive("\n");
    928  1.1  jmcneill 	aprint_normal(": NXP TDA19988 HDMI transmitter\n");
    929  1.1  jmcneill 
    930  1.1  jmcneill 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    931  1.1  jmcneill 	tda19988_start(sc);
    932  1.1  jmcneill 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    933  1.1  jmcneill 
    934  1.1  jmcneill 	sc->sc_ports.dp_ep_activate = tda19988_ep_activate;
    935  1.1  jmcneill 	sc->sc_ports.dp_ep_get_data = tda19988_ep_get_data;
    936  1.1  jmcneill 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    937  1.1  jmcneill }
    938  1.1  jmcneill 
    939  1.1  jmcneill CFATTACH_DECL_NEW(tdahdmi, sizeof(struct tda19988_softc),
    940  1.1  jmcneill     tda19988_match, tda19988_attach, NULL, NULL);
    941