blob: c9882246d81f6bc8d2fd0199f077414323bcc6f7 [file] [log] [blame]
Rob Clarke7792ce2013-01-08 19:21:02 -06001/*
2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19
Russell King893c3e52013-08-27 01:27:42 +010020#include <linux/hdmi.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060021#include <linux/module.h>
Jean-Francois Moine12473b72014-01-25 18:14:38 +010022#include <linux/irq.h>
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +010023#include <sound/asoundef.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060024
25#include <drm/drmP.h>
26#include <drm/drm_crtc_helper.h>
27#include <drm/drm_encoder_slave.h>
28#include <drm/drm_edid.h>
Russell Kingc4c11dd2013-08-14 21:43:30 +020029#include <drm/i2c/tda998x.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060030
31#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
32
33struct tda998x_priv {
34 struct i2c_client *cec;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +010035 struct i2c_client *hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -060036 uint16_t rev;
37 uint8_t current_page;
38 int dpms;
Russell Kingc4c11dd2013-08-14 21:43:30 +020039 bool is_hdmi_sink;
Russell King5e74c222013-08-14 21:43:29 +020040 u8 vip_cntrl_0;
41 u8 vip_cntrl_1;
42 u8 vip_cntrl_2;
Russell Kingc4c11dd2013-08-14 21:43:30 +020043 struct tda998x_encoder_params params;
Jean-Francois Moine12473b72014-01-25 18:14:38 +010044
45 wait_queue_head_t wq_edid;
46 volatile int wq_edid_wait;
47 struct drm_encoder *encoder;
Rob Clarke7792ce2013-01-08 19:21:02 -060048};
49
50#define to_tda998x_priv(x) ((struct tda998x_priv *)to_encoder_slave(x)->slave_priv)
51
52/* The TDA9988 series of devices use a paged register scheme.. to simplify
53 * things we encode the page # in upper bits of the register #. To read/
54 * write a given register, we need to make sure CURPAGE register is set
55 * appropriately. Which implies reads/writes are not atomic. Fun!
56 */
57
58#define REG(page, addr) (((page) << 8) | (addr))
59#define REG2ADDR(reg) ((reg) & 0xff)
60#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
61
62#define REG_CURPAGE 0xff /* write */
63
64
65/* Page 00h: General Control */
66#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
67#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
68# define MAIN_CNTRL0_SR (1 << 0)
69# define MAIN_CNTRL0_DECS (1 << 1)
70# define MAIN_CNTRL0_DEHS (1 << 2)
71# define MAIN_CNTRL0_CECS (1 << 3)
72# define MAIN_CNTRL0_CEHS (1 << 4)
73# define MAIN_CNTRL0_SCALER (1 << 7)
74#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
75#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
76# define SOFTRESET_AUDIO (1 << 0)
77# define SOFTRESET_I2C_MASTER (1 << 1)
78#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
79#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
80#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
81# define I2C_MASTER_DIS_MM (1 << 0)
82# define I2C_MASTER_DIS_FILT (1 << 1)
83# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020084#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
85# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060086#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
87#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
88#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
89# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +020090#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -060091#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
92#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
93#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
94#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
95#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
96# define VIP_CNTRL_0_MIRR_A (1 << 7)
97# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
98# define VIP_CNTRL_0_MIRR_B (1 << 3)
99# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
100#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
101# define VIP_CNTRL_1_MIRR_C (1 << 7)
102# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
103# define VIP_CNTRL_1_MIRR_D (1 << 3)
104# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
105#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
106# define VIP_CNTRL_2_MIRR_E (1 << 7)
107# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
108# define VIP_CNTRL_2_MIRR_F (1 << 3)
109# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
110#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
111# define VIP_CNTRL_3_X_TGL (1 << 0)
112# define VIP_CNTRL_3_H_TGL (1 << 1)
113# define VIP_CNTRL_3_V_TGL (1 << 2)
114# define VIP_CNTRL_3_EMB (1 << 3)
115# define VIP_CNTRL_3_SYNC_DE (1 << 4)
116# define VIP_CNTRL_3_SYNC_HS (1 << 5)
117# define VIP_CNTRL_3_DE_INT (1 << 6)
118# define VIP_CNTRL_3_EDGE (1 << 7)
119#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
120# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
121# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
122# define VIP_CNTRL_4_CCIR656 (1 << 4)
123# define VIP_CNTRL_4_656_ALT (1 << 5)
124# define VIP_CNTRL_4_TST_656 (1 << 6)
125# define VIP_CNTRL_4_TST_PAT (1 << 7)
126#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
127# define VIP_CNTRL_5_CKCASE (1 << 0)
128# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200129#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Russell Kingbcb24812013-08-14 21:43:27 +0200130#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600131#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
132# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
133# define MAT_CONTRL_MAT_BP (1 << 2)
134#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
135#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
136#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
137#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
138#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
139#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
140#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
141#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
142#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
143#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
144#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
145#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
146#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
147#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
148#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
149#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
150#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200151#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
152#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600153#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
154#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200155#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
156#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600157#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
158#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
159#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
160#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
161#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
162#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
163#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
164#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
165#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
166#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200167#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
168#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
169#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
170#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600171#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
172#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
173#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
174#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
175#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200176# define TBG_CNTRL_0_TOP_TGL (1 << 0)
177# define TBG_CNTRL_0_TOP_SEL (1 << 1)
178# define TBG_CNTRL_0_DE_EXT (1 << 2)
179# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600180# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
181# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
182# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
183#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200184# define TBG_CNTRL_1_H_TGL (1 << 0)
185# define TBG_CNTRL_1_V_TGL (1 << 1)
186# define TBG_CNTRL_1_TGL_EN (1 << 2)
187# define TBG_CNTRL_1_X_EXT (1 << 3)
188# define TBG_CNTRL_1_H_EXT (1 << 4)
189# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600190# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
191#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
192#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
193# define HVF_CNTRL_0_SM (1 << 7)
194# define HVF_CNTRL_0_RWB (1 << 6)
195# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
196# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
197#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
198# define HVF_CNTRL_1_FOR (1 << 0)
199# define HVF_CNTRL_1_YUVBLK (1 << 1)
200# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
201# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
202# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
203#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200204#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
205# define I2S_FORMAT(x) (((x) & 3) << 0)
206#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
207# define AIP_CLKSEL_FS(x) (((x) & 3) << 0)
208# define AIP_CLKSEL_CLK_POL(x) (((x) & 1) << 2)
209# define AIP_CLKSEL_AIP(x) (((x) & 7) << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600210
211
212/* Page 02h: PLL settings */
213#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
214# define PLL_SERIAL_1_SRL_FDN (1 << 0)
215# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
216# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
217#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100218# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600219# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
220#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
221# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
222# define PLL_SERIAL_3_SRL_DE (1 << 2)
223# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
224#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
225#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
226#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
227#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
228#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
229#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
230#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
231#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
232#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200233# define AUDIO_DIV_SERCLK_1 0
234# define AUDIO_DIV_SERCLK_2 1
235# define AUDIO_DIV_SERCLK_4 2
236# define AUDIO_DIV_SERCLK_8 3
237# define AUDIO_DIV_SERCLK_16 4
238# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600239#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
240# define SEL_CLK_SEL_CLK1 (1 << 0)
241# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
242# define SEL_CLK_ENA_SC_CLK (1 << 3)
243#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
244
245
246/* Page 09h: EDID Control */
247#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
248/* next 127 successive registers are the EDID block */
249#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
250#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
251#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
252#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
253#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
254
255
256/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200257#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
258#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
259#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
260#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
261#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600262
263
264/* Page 11h: audio settings and content info packets */
265#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
266# define AIP_CNTRL_0_RST_FIFO (1 << 0)
267# define AIP_CNTRL_0_SWAP (1 << 1)
268# define AIP_CNTRL_0_LAYOUT (1 << 2)
269# define AIP_CNTRL_0_ACR_MAN (1 << 5)
270# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200271#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
272# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
273# define CA_I2S_HBR_CHSTAT (1 << 6)
274#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
275#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
276#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
277#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
278#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
279#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
280#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
281#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
282# define CTS_N_K(x) (((x) & 7) << 0)
283# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600284#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
285# define ENC_CNTRL_RST_ENC (1 << 0)
286# define ENC_CNTRL_RST_SEL (1 << 1)
287# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200288#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
289# define DIP_FLAGS_ACR (1 << 0)
290# define DIP_FLAGS_GC (1 << 1)
291#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
292# define DIP_IF_FLAGS_IF1 (1 << 1)
293# define DIP_IF_FLAGS_IF2 (1 << 2)
294# define DIP_IF_FLAGS_IF3 (1 << 3)
295# define DIP_IF_FLAGS_IF4 (1 << 4)
296# define DIP_IF_FLAGS_IF5 (1 << 5)
297#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600298
299
300/* Page 12h: HDCP and OTP */
301#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200302#define REG_TX4 REG(0x12, 0x9b) /* read/write */
303# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600304#define REG_TX33 REG(0x12, 0xb8) /* read/write */
305# define TX33_HDMI (1 << 1)
306
307
308/* Page 13h: Gamut related metadata packets */
309
310
311
312/* CEC registers: (not paged)
313 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100314#define REG_CEC_INTSTATUS 0xee /* read */
315# define CEC_INTSTATUS_CEC (1 << 0)
316# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600317#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
318# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
319# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
320# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
321# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100322#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
323#define REG_CEC_RXSHPDINT 0xfd /* read */
Rob Clarke7792ce2013-01-08 19:21:02 -0600324#define REG_CEC_RXSHPDLEV 0xfe /* read */
325# define CEC_RXSHPDLEV_RXSENS (1 << 0)
326# define CEC_RXSHPDLEV_HPD (1 << 1)
327
328#define REG_CEC_ENAMODS 0xff /* read/write */
329# define CEC_ENAMODS_DIS_FRO (1 << 6)
330# define CEC_ENAMODS_DIS_CCLK (1 << 5)
331# define CEC_ENAMODS_EN_RXSENS (1 << 2)
332# define CEC_ENAMODS_EN_HDMI (1 << 1)
333# define CEC_ENAMODS_EN_CEC (1 << 0)
334
335
336/* Device versions: */
337#define TDA9989N2 0x0101
338#define TDA19989 0x0201
339#define TDA19989N2 0x0202
340#define TDA19988 0x0301
341
342static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100343cec_write(struct tda998x_priv *priv, uint16_t addr, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600344{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100345 struct i2c_client *client = priv->cec;
Rob Clarke7792ce2013-01-08 19:21:02 -0600346 uint8_t buf[] = {addr, val};
347 int ret;
348
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100349 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600350 if (ret < 0)
351 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
352}
353
354static uint8_t
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100355cec_read(struct tda998x_priv *priv, uint8_t addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600356{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100357 struct i2c_client *client = priv->cec;
Rob Clarke7792ce2013-01-08 19:21:02 -0600358 uint8_t val;
359 int ret;
360
361 ret = i2c_master_send(client, &addr, sizeof(addr));
362 if (ret < 0)
363 goto fail;
364
365 ret = i2c_master_recv(client, &val, sizeof(val));
366 if (ret < 0)
367 goto fail;
368
369 return val;
370
371fail:
372 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
373 return 0;
374}
375
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100376static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100377set_page(struct tda998x_priv *priv, uint16_t reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600378{
Rob Clarke7792ce2013-01-08 19:21:02 -0600379 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100380 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600381 uint8_t buf[] = {
382 REG_CURPAGE, REG2PAGE(reg)
383 };
384 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100385 if (ret < 0) {
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100386 dev_err(&client->dev, "setpage %04x err %d\n",
387 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100388 return ret;
389 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600390
391 priv->current_page = REG2PAGE(reg);
392 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100393 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600394}
395
396static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100397reg_read_range(struct tda998x_priv *priv, uint16_t reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600398{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100399 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600400 uint8_t addr = REG2ADDR(reg);
401 int ret;
402
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100403 ret = set_page(priv, reg);
404 if (ret < 0)
405 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600406
407 ret = i2c_master_send(client, &addr, sizeof(addr));
408 if (ret < 0)
409 goto fail;
410
411 ret = i2c_master_recv(client, buf, cnt);
412 if (ret < 0)
413 goto fail;
414
415 return ret;
416
417fail:
418 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
419 return ret;
420}
421
Russell Kingc4c11dd2013-08-14 21:43:30 +0200422static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100423reg_write_range(struct tda998x_priv *priv, uint16_t reg, uint8_t *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200424{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100425 struct i2c_client *client = priv->hdmi;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200426 uint8_t buf[cnt+1];
427 int ret;
428
429 buf[0] = REG2ADDR(reg);
430 memcpy(&buf[1], p, cnt);
431
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100432 ret = set_page(priv, reg);
433 if (ret < 0)
434 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200435
436 ret = i2c_master_send(client, buf, cnt + 1);
437 if (ret < 0)
438 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
439}
440
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100441static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100442reg_read(struct tda998x_priv *priv, uint16_t reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600443{
444 uint8_t val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100445 int ret;
446
447 ret = reg_read_range(priv, reg, &val, sizeof(val));
448 if (ret < 0)
449 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600450 return val;
451}
452
453static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100454reg_write(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600455{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100456 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600457 uint8_t buf[] = {REG2ADDR(reg), val};
458 int ret;
459
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100460 ret = set_page(priv, reg);
461 if (ret < 0)
462 return;
Rob Clarke7792ce2013-01-08 19:21:02 -0600463
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100464 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600465 if (ret < 0)
466 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
467}
468
469static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100470reg_write16(struct tda998x_priv *priv, uint16_t reg, uint16_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600471{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100472 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600473 uint8_t buf[] = {REG2ADDR(reg), val >> 8, val};
474 int ret;
475
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100476 ret = set_page(priv, reg);
477 if (ret < 0)
478 return;
Rob Clarke7792ce2013-01-08 19:21:02 -0600479
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100480 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600481 if (ret < 0)
482 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
483}
484
485static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100486reg_set(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600487{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100488 int old_val;
489
490 old_val = reg_read(priv, reg);
491 if (old_val >= 0)
492 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600493}
494
495static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100496reg_clear(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600497{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100498 int old_val;
499
500 old_val = reg_read(priv, reg);
501 if (old_val >= 0)
502 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600503}
504
505static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100506tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600507{
508 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100509 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600510 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100511 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600512 msleep(50);
513
514 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100515 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
516 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600517
518 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100519 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
520 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
521 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
522 reg_write(priv, REG_SERIALIZER, 0x00);
523 reg_write(priv, REG_BUFFER_OUT, 0x00);
524 reg_write(priv, REG_PLL_SCG1, 0x00);
525 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
526 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
527 reg_write(priv, REG_PLL_SCGN1, 0xfa);
528 reg_write(priv, REG_PLL_SCGN2, 0x00);
529 reg_write(priv, REG_PLL_SCGR1, 0x5b);
530 reg_write(priv, REG_PLL_SCGR2, 0x00);
531 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200532
533 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100534 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600535}
536
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100537/*
538 * only 2 interrupts may occur: screen plug/unplug and EDID read
539 */
540static irqreturn_t tda998x_irq_thread(int irq, void *data)
541{
542 struct tda998x_priv *priv = data;
543 u8 sta, cec, lvl, flag0, flag1, flag2;
544
545 if (!priv)
546 return IRQ_HANDLED;
547 sta = cec_read(priv, REG_CEC_INTSTATUS);
548 cec = cec_read(priv, REG_CEC_RXSHPDINT);
549 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
550 flag0 = reg_read(priv, REG_INT_FLAGS_0);
551 flag1 = reg_read(priv, REG_INT_FLAGS_1);
552 flag2 = reg_read(priv, REG_INT_FLAGS_2);
553 DRM_DEBUG_DRIVER(
554 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
555 sta, cec, lvl, flag0, flag1, flag2);
556 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
557 priv->wq_edid_wait = 0;
558 wake_up(&priv->wq_edid);
559 } else if (cec != 0) { /* HPD change */
560 if (priv->encoder && priv->encoder->dev)
561 drm_helper_hpd_irq_event(priv->encoder->dev);
562 }
563 return IRQ_HANDLED;
564}
565
Russell Kingc4c11dd2013-08-14 21:43:30 +0200566static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes)
567{
568 uint8_t sum = 0;
569
570 while (bytes--)
571 sum += *buf++;
572 return (255 - sum) + 1;
573}
574
575#define HB(x) (x)
576#define PB(x) (HB(2) + 1 + (x))
577
578static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100579tda998x_write_if(struct tda998x_priv *priv, uint8_t bit, uint16_t addr,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200580 uint8_t *buf, size_t size)
581{
582 buf[PB(0)] = tda998x_cksum(buf, size);
583
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100584 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
585 reg_write_range(priv, addr, buf, size);
586 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200587}
588
589static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100590tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200591{
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100592 u8 buf[PB(HDMI_AUDIO_INFOFRAME_SIZE) + 1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200593
Jean-Francois Moine7288ca02014-01-25 18:14:44 +0100594 memset(buf, 0, sizeof(buf));
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100595 buf[HB(0)] = HDMI_INFOFRAME_TYPE_AUDIO;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200596 buf[HB(1)] = 0x01;
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100597 buf[HB(2)] = HDMI_AUDIO_INFOFRAME_SIZE;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200598 buf[PB(1)] = p->audio_frame[1] & 0x07; /* CC */
599 buf[PB(2)] = p->audio_frame[2] & 0x1c; /* SF */
600 buf[PB(4)] = p->audio_frame[4];
601 buf[PB(5)] = p->audio_frame[5] & 0xf8; /* DM_INH + LSV */
602
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100603 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, buf,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200604 sizeof(buf));
605}
606
607static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100608tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200609{
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100610 u8 buf[PB(HDMI_AVI_INFOFRAME_SIZE) + 1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200611
612 memset(buf, 0, sizeof(buf));
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100613 buf[HB(0)] = HDMI_INFOFRAME_TYPE_AVI;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200614 buf[HB(1)] = 0x02;
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100615 buf[HB(2)] = HDMI_AVI_INFOFRAME_SIZE;
Russell King893c3e52013-08-27 01:27:42 +0100616 buf[PB(1)] = HDMI_SCAN_MODE_UNDERSCAN;
Jean-Francois Moinebdf63452014-01-25 18:14:40 +0100617 buf[PB(2)] = HDMI_ACTIVE_ASPECT_PICTURE;
Russell King893c3e52013-08-27 01:27:42 +0100618 buf[PB(3)] = HDMI_QUANTIZATION_RANGE_FULL << 2;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200619 buf[PB(4)] = drm_match_cea_mode(mode);
620
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100621 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, buf,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200622 sizeof(buf));
623}
624
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100625static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200626{
627 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100628 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
629 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
630 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200631 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100632 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200633 }
634}
635
636static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100637tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200638 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
639{
640 uint8_t buf[6], clksel_aip, clksel_fs, ca_i2s, cts_n, adiv;
641 uint32_t n;
642
643 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100644 reg_write(priv, REG_ENA_AP, p->audio_cfg);
645 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200646
647 /* Set audio input source */
648 switch (p->audio_format) {
649 case AFMT_SPDIF:
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100650 reg_write(priv, REG_MUX_AP, 0x40);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200651 clksel_aip = AIP_CLKSEL_AIP(0);
652 /* FS64SPDIF */
653 clksel_fs = AIP_CLKSEL_FS(2);
654 cts_n = CTS_N_M(3) | CTS_N_K(3);
655 ca_i2s = 0;
656 break;
657
658 case AFMT_I2S:
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100659 reg_write(priv, REG_MUX_AP, 0x64);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200660 clksel_aip = AIP_CLKSEL_AIP(1);
661 /* ACLK */
662 clksel_fs = AIP_CLKSEL_FS(0);
663 cts_n = CTS_N_M(3) | CTS_N_K(3);
664 ca_i2s = CA_I2S_CA_I2S(0);
665 break;
David Herrmann3b288022013-09-01 15:23:04 +0200666
667 default:
668 BUG();
669 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200670 }
671
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100672 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
673 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200674
675 /* Enable automatic CTS generation */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100676 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_ACR_MAN);
677 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200678
679 /*
680 * Audio input somehow depends on HDMI line rate which is
681 * related to pixclk. Testing showed that modes with pixclk
682 * >100MHz need a larger divider while <40MHz need the default.
683 * There is no detailed info in the datasheet, so we just
684 * assume 100MHz requires larger divider.
685 */
686 if (mode->clock > 100000)
687 adiv = AUDIO_DIV_SERCLK_16;
688 else
689 adiv = AUDIO_DIV_SERCLK_8;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100690 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200691
692 /*
693 * This is the approximate value of N, which happens to be
694 * the recommended values for non-coherent clocks.
695 */
696 n = 128 * p->audio_sample_rate / 1000;
697
698 /* Write the CTS and N values */
699 buf[0] = 0x44;
700 buf[1] = 0x42;
701 buf[2] = 0x01;
702 buf[3] = n;
703 buf[4] = n >> 8;
704 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100705 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200706
707 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100708 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200709
710 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100711 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
712 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200713
714 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100715 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200716 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100717 buf[2] = IEC958_AES3_CON_FS_NOTID;
718 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
719 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100720 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200721
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100722 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100723 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100724 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200725
726 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100727 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200728}
729
Rob Clarke7792ce2013-01-08 19:21:02 -0600730/* DRM encoder functions */
731
732static void
733tda998x_encoder_set_config(struct drm_encoder *encoder, void *params)
734{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200735 struct tda998x_priv *priv = to_tda998x_priv(encoder);
736 struct tda998x_encoder_params *p = params;
737
738 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
739 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
740 VIP_CNTRL_0_SWAP_B(p->swap_b) |
741 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
742 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
743 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
744 VIP_CNTRL_1_SWAP_D(p->swap_d) |
745 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
746 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
747 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
748 VIP_CNTRL_2_SWAP_F(p->swap_f) |
749 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
750
751 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600752}
753
754static void
755tda998x_encoder_dpms(struct drm_encoder *encoder, int mode)
756{
757 struct tda998x_priv *priv = to_tda998x_priv(encoder);
758
759 /* we only care about on or off: */
760 if (mode != DRM_MODE_DPMS_ON)
761 mode = DRM_MODE_DPMS_OFF;
762
763 if (mode == priv->dpms)
764 return;
765
766 switch (mode) {
767 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200768 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100769 reg_write(priv, REG_ENA_VP_0, 0xff);
770 reg_write(priv, REG_ENA_VP_1, 0xff);
771 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600772 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100773 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
774 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
775 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600776 break;
777 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100778 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100779 reg_write(priv, REG_ENA_VP_0, 0x00);
780 reg_write(priv, REG_ENA_VP_1, 0x00);
781 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600782 break;
783 }
784
785 priv->dpms = mode;
786}
787
788static void
789tda998x_encoder_save(struct drm_encoder *encoder)
790{
791 DBG("");
792}
793
794static void
795tda998x_encoder_restore(struct drm_encoder *encoder)
796{
797 DBG("");
798}
799
800static bool
801tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
802 const struct drm_display_mode *mode,
803 struct drm_display_mode *adjusted_mode)
804{
805 return true;
806}
807
808static int
809tda998x_encoder_mode_valid(struct drm_encoder *encoder,
810 struct drm_display_mode *mode)
811{
812 return MODE_OK;
813}
814
815static void
816tda998x_encoder_mode_set(struct drm_encoder *encoder,
817 struct drm_display_mode *mode,
818 struct drm_display_mode *adjusted_mode)
819{
820 struct tda998x_priv *priv = to_tda998x_priv(encoder);
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200821 uint16_t ref_pix, ref_line, n_pix, n_line;
822 uint16_t hs_pix_s, hs_pix_e;
823 uint16_t vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
824 uint16_t vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
825 uint16_t vwin1_line_s, vwin1_line_e;
826 uint16_t vwin2_line_s, vwin2_line_e;
827 uint16_t de_pix_s, de_pix_e;
Rob Clarke7792ce2013-01-08 19:21:02 -0600828 uint8_t reg, div, rep;
829
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200830 /*
831 * Internally TDA998x is using ITU-R BT.656 style sync but
832 * we get VESA style sync. TDA998x is using a reference pixel
833 * relative to ITU to sync to the input frame and for output
834 * sync generation. Currently, we are using reference detection
835 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
836 * which is position of rising VS with coincident rising HS.
837 *
838 * Now there is some issues to take care of:
839 * - HDMI data islands require sync-before-active
840 * - TDA998x register values must be > 0 to be enabled
841 * - REFLINE needs an additional offset of +1
842 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
843 *
844 * So we add +1 to all horizontal and vertical register values,
845 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600846 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200847 n_pix = mode->htotal;
848 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600849
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200850 hs_pix_e = mode->hsync_end - mode->hdisplay;
851 hs_pix_s = mode->hsync_start - mode->hdisplay;
852 de_pix_e = mode->htotal;
853 de_pix_s = mode->htotal - mode->hdisplay;
854 ref_pix = 3 + hs_pix_s;
855
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200856 /*
857 * Attached LCD controllers may generate broken sync. Allow
858 * those to adjust the position of the rising VS edge by adding
859 * HSKEW to ref_pix.
860 */
861 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
862 ref_pix += adjusted_mode->hskew;
863
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200864 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
865 ref_line = 1 + mode->vsync_start - mode->vdisplay;
866 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
867 vwin1_line_e = vwin1_line_s + mode->vdisplay;
868 vs1_pix_s = vs1_pix_e = hs_pix_s;
869 vs1_line_s = mode->vsync_start - mode->vdisplay;
870 vs1_line_e = vs1_line_s +
871 mode->vsync_end - mode->vsync_start;
872 vwin2_line_s = vwin2_line_e = 0;
873 vs2_pix_s = vs2_pix_e = 0;
874 vs2_line_s = vs2_line_e = 0;
875 } else {
876 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
877 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
878 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
879 vs1_pix_s = vs1_pix_e = hs_pix_s;
880 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
881 vs1_line_e = vs1_line_s +
882 (mode->vsync_end - mode->vsync_start)/2;
883 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
884 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
885 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
886 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
887 vs2_line_e = vs2_line_s +
888 (mode->vsync_end - mode->vsync_start)/2;
889 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600890
891 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100892 if (div != 0) {
893 div--;
894 if (div > 3)
895 div = 3;
896 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600897
Rob Clarke7792ce2013-01-08 19:21:02 -0600898 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100899 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600900
901 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100902 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100903 reg_clear(priv, REG_TX33, TX33_HDMI);
904 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600905
Rob Clarke7792ce2013-01-08 19:21:02 -0600906 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100907 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600908 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100909 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
910 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600911 VIP_CNTRL_4_BLC(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100912 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600913
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100914 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
915 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_DE);
916 reg_write(priv, REG_SERIALIZER, 0);
917 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600918
919 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
920 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100921 reg_write(priv, REG_RPT_CNTRL, 0);
922 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600923 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
924
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100925 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600926 PLL_SERIAL_2_SRL_PR(rep));
927
Rob Clarke7792ce2013-01-08 19:21:02 -0600928 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100929 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
930 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600931
932 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100933 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -0600934
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100935 reg_write(priv, REG_TBG_CNTRL_0, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600936
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200937 /*
938 * Sync on rising HSYNC/VSYNC
939 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100940 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200941
942 /*
943 * TDA19988 requires high-active sync at input stage,
944 * so invert low-active sync provided by master encoder here
945 */
946 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100947 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -0600948 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100949 reg |= VIP_CNTRL_3_V_TGL;
950 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600951
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100952 reg_write(priv, REG_VIDFORMAT, 0x00);
953 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
954 reg_write16(priv, REG_REFLINE_MSB, ref_line);
955 reg_write16(priv, REG_NPIX_MSB, n_pix);
956 reg_write16(priv, REG_NLINE_MSB, n_line);
957 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
958 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
959 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
960 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
961 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
962 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
963 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
964 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
965 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
966 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
967 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
968 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
969 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
970 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
971 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
972 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -0600973
974 if (priv->rev == TDA19988) {
975 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100976 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600977 }
978
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100979 /*
980 * Always generate sync polarity relative to input sync and
981 * revert input stage toggled sync at output stage
982 */
983 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
984 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
985 reg |= TBG_CNTRL_1_H_TGL;
986 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
987 reg |= TBG_CNTRL_1_V_TGL;
988 reg_write(priv, REG_TBG_CNTRL_1, reg);
989
Rob Clarke7792ce2013-01-08 19:21:02 -0600990 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100991 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200992
993 /* Only setup the info frames if the sink is HDMI */
994 if (priv->is_hdmi_sink) {
995 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100996 reg &= ~TBG_CNTRL_1_DWIN_DIS;
997 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100998 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
999 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001000
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001001 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001002
1003 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001004 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001005 &priv->params);
1006 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001007}
1008
1009static enum drm_connector_status
1010tda998x_encoder_detect(struct drm_encoder *encoder,
1011 struct drm_connector *connector)
1012{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001013 struct tda998x_priv *priv = to_tda998x_priv(encoder);
1014 uint8_t val = cec_read(priv, REG_CEC_RXSHPDLEV);
1015
Rob Clarke7792ce2013-01-08 19:21:02 -06001016 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1017 connector_status_disconnected;
1018}
1019
1020static int
1021read_edid_block(struct drm_encoder *encoder, uint8_t *buf, int blk)
1022{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001023 struct tda998x_priv *priv = to_tda998x_priv(encoder);
Rob Clarke7792ce2013-01-08 19:21:02 -06001024 uint8_t offset, segptr;
1025 int ret, i;
1026
Rob Clarke7792ce2013-01-08 19:21:02 -06001027 offset = (blk & 1) ? 128 : 0;
1028 segptr = blk / 2;
1029
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001030 reg_write(priv, REG_DDC_ADDR, 0xa0);
1031 reg_write(priv, REG_DDC_OFFS, offset);
1032 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1033 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001034
1035 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001036 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001037 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001038
1039 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001040 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001041
1042 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001043 if (priv->hdmi->irq) {
1044 i = wait_event_timeout(priv->wq_edid,
1045 !priv->wq_edid_wait,
1046 msecs_to_jiffies(100));
1047 if (i < 0) {
1048 dev_err(encoder->dev->dev, "read edid wait err %d\n", i);
1049 return i;
1050 }
1051 } else {
1052 for (i = 10; i > 0; i--) {
1053 msleep(10);
1054 ret = reg_read(priv, REG_INT_FLAGS_2);
1055 if (ret < 0)
1056 return ret;
1057 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1058 break;
1059 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001060 }
1061
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001062 if (i == 0) {
1063 dev_err(encoder->dev->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001064 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001065 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001066
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001067 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, EDID_LENGTH);
Rob Clarke7792ce2013-01-08 19:21:02 -06001068 if (ret != EDID_LENGTH) {
Jean-Francois Moine704d63f2014-01-25 18:14:46 +01001069 dev_err(encoder->dev->dev, "failed to read edid block %d: %d\n",
Rob Clarke7792ce2013-01-08 19:21:02 -06001070 blk, ret);
1071 return ret;
1072 }
1073
Rob Clarke7792ce2013-01-08 19:21:02 -06001074 return 0;
1075}
1076
1077static uint8_t *
1078do_get_edid(struct drm_encoder *encoder)
1079{
Russell King063b4722013-08-14 21:43:26 +02001080 struct tda998x_priv *priv = to_tda998x_priv(encoder);
Jean-Francois Moine704d63f2014-01-25 18:14:46 +01001081 int j, valid_extensions = 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001082 uint8_t *block, *new;
1083 bool print_bad_edid = drm_debug & DRM_UT_KMS;
1084
1085 if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
1086 return NULL;
1087
Russell King063b4722013-08-14 21:43:26 +02001088 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001089 reg_clear(priv, REG_TX4, TX4_PD_RAM);
Russell King063b4722013-08-14 21:43:26 +02001090
Rob Clarke7792ce2013-01-08 19:21:02 -06001091 /* base block fetch */
1092 if (read_edid_block(encoder, block, 0))
1093 goto fail;
1094
1095 if (!drm_edid_block_valid(block, 0, print_bad_edid))
1096 goto fail;
1097
1098 /* if there's no extensions, we're done */
1099 if (block[0x7e] == 0)
Russell King063b4722013-08-14 21:43:26 +02001100 goto done;
Rob Clarke7792ce2013-01-08 19:21:02 -06001101
1102 new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
1103 if (!new)
1104 goto fail;
1105 block = new;
1106
1107 for (j = 1; j <= block[0x7e]; j++) {
1108 uint8_t *ext_block = block + (valid_extensions + 1) * EDID_LENGTH;
1109 if (read_edid_block(encoder, ext_block, j))
1110 goto fail;
1111
1112 if (!drm_edid_block_valid(ext_block, j, print_bad_edid))
1113 goto fail;
1114
1115 valid_extensions++;
1116 }
1117
1118 if (valid_extensions != block[0x7e]) {
1119 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
1120 block[0x7e] = valid_extensions;
1121 new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
1122 if (!new)
1123 goto fail;
1124 block = new;
1125 }
1126
Russell King063b4722013-08-14 21:43:26 +02001127done:
1128 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001129 reg_set(priv, REG_TX4, TX4_PD_RAM);
Russell King063b4722013-08-14 21:43:26 +02001130
Rob Clarke7792ce2013-01-08 19:21:02 -06001131 return block;
1132
1133fail:
Russell King063b4722013-08-14 21:43:26 +02001134 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001135 reg_set(priv, REG_TX4, TX4_PD_RAM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001136 dev_warn(encoder->dev->dev, "failed to read EDID\n");
1137 kfree(block);
1138 return NULL;
1139}
1140
1141static int
1142tda998x_encoder_get_modes(struct drm_encoder *encoder,
1143 struct drm_connector *connector)
1144{
Russell Kingc4c11dd2013-08-14 21:43:30 +02001145 struct tda998x_priv *priv = to_tda998x_priv(encoder);
Rob Clarke7792ce2013-01-08 19:21:02 -06001146 struct edid *edid = (struct edid *)do_get_edid(encoder);
1147 int n = 0;
1148
1149 if (edid) {
1150 drm_mode_connector_update_edid_property(connector, edid);
1151 n = drm_add_edid_modes(connector, edid);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001152 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
Rob Clarke7792ce2013-01-08 19:21:02 -06001153 kfree(edid);
1154 }
1155
1156 return n;
1157}
1158
1159static int
1160tda998x_encoder_create_resources(struct drm_encoder *encoder,
1161 struct drm_connector *connector)
1162{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001163 struct tda998x_priv *priv = to_tda998x_priv(encoder);
1164
1165 if (priv->hdmi->irq)
1166 connector->polled = DRM_CONNECTOR_POLL_HPD;
1167 else
1168 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1169 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001170 return 0;
1171}
1172
1173static int
1174tda998x_encoder_set_property(struct drm_encoder *encoder,
1175 struct drm_connector *connector,
1176 struct drm_property *property,
1177 uint64_t val)
1178{
1179 DBG("");
1180 return 0;
1181}
1182
1183static void
1184tda998x_encoder_destroy(struct drm_encoder *encoder)
1185{
1186 struct tda998x_priv *priv = to_tda998x_priv(encoder);
1187 drm_i2c_encoder_destroy(encoder);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001188
1189 /* disable all IRQs and free the IRQ handler */
1190 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1191 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1192 if (priv->hdmi->irq)
1193 free_irq(priv->hdmi->irq, priv);
1194
Jean-Francois Moinefc275a72014-01-25 18:14:42 +01001195 if (priv->cec)
1196 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001197 kfree(priv);
1198}
1199
1200static struct drm_encoder_slave_funcs tda998x_encoder_funcs = {
1201 .set_config = tda998x_encoder_set_config,
1202 .destroy = tda998x_encoder_destroy,
1203 .dpms = tda998x_encoder_dpms,
1204 .save = tda998x_encoder_save,
1205 .restore = tda998x_encoder_restore,
1206 .mode_fixup = tda998x_encoder_mode_fixup,
1207 .mode_valid = tda998x_encoder_mode_valid,
1208 .mode_set = tda998x_encoder_mode_set,
1209 .detect = tda998x_encoder_detect,
1210 .get_modes = tda998x_encoder_get_modes,
1211 .create_resources = tda998x_encoder_create_resources,
1212 .set_property = tda998x_encoder_set_property,
1213};
1214
1215/* I2C driver functions */
1216
1217static int
1218tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1219{
1220 return 0;
1221}
1222
1223static int
1224tda998x_remove(struct i2c_client *client)
1225{
1226 return 0;
1227}
1228
1229static int
1230tda998x_encoder_init(struct i2c_client *client,
1231 struct drm_device *dev,
1232 struct drm_encoder_slave *encoder_slave)
1233{
Rob Clarke7792ce2013-01-08 19:21:02 -06001234 struct tda998x_priv *priv;
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001235 struct device_node *np = client->dev.of_node;
1236 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001237 int rev_lo, rev_hi, ret;
Rob Clarke7792ce2013-01-08 19:21:02 -06001238
1239 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1240 if (!priv)
1241 return -ENOMEM;
1242
Russell King5e74c222013-08-14 21:43:29 +02001243 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1244 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1245 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1246
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001247 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001248 priv->hdmi = client;
Rob Clarke7792ce2013-01-08 19:21:02 -06001249 priv->cec = i2c_new_dummy(client->adapter, 0x34);
Dave Jones71c68c42014-02-12 22:47:51 -05001250 if (!priv->cec) {
1251 kfree(priv);
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001252 return -ENODEV;
Dave Jones71c68c42014-02-12 22:47:51 -05001253 }
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001254
1255 priv->encoder = &encoder_slave->base;
Rob Clarke7792ce2013-01-08 19:21:02 -06001256 priv->dpms = DRM_MODE_DPMS_OFF;
1257
1258 encoder_slave->slave_priv = priv;
1259 encoder_slave->slave_funcs = &tda998x_encoder_funcs;
1260
1261 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001262 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001263 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1264
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001265 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001266
1267 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001268 rev_lo = reg_read(priv, REG_VERSION_LSB);
1269 rev_hi = reg_read(priv, REG_VERSION_MSB);
1270 if (rev_lo < 0 || rev_hi < 0) {
1271 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001272 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001273 }
1274
1275 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001276
1277 /* mask off feature bits: */
1278 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1279
1280 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001281 case TDA9989N2:
1282 dev_info(&client->dev, "found TDA9989 n2");
1283 break;
1284 case TDA19989:
1285 dev_info(&client->dev, "found TDA19989");
1286 break;
1287 case TDA19989N2:
1288 dev_info(&client->dev, "found TDA19989 n2");
1289 break;
1290 case TDA19988:
1291 dev_info(&client->dev, "found TDA19988");
1292 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001293 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001294 dev_err(&client->dev, "found unsupported device: %04x\n",
1295 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001296 goto fail;
1297 }
1298
1299 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001300 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001301
1302 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001303 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001304
1305 /* if necessary, disable multi-master: */
1306 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001307 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001308
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001309 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001310 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1311
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001312 /* initialize the optional IRQ */
1313 if (client->irq) {
1314 int irqf_trigger;
1315
1316 /* init read EDID waitqueue */
1317 init_waitqueue_head(&priv->wq_edid);
1318
1319 /* clear pending interrupts */
1320 reg_read(priv, REG_INT_FLAGS_0);
1321 reg_read(priv, REG_INT_FLAGS_1);
1322 reg_read(priv, REG_INT_FLAGS_2);
1323
1324 irqf_trigger =
1325 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1326 ret = request_threaded_irq(client->irq, NULL,
1327 tda998x_irq_thread,
1328 irqf_trigger | IRQF_ONESHOT,
1329 "tda998x", priv);
1330 if (ret) {
1331 dev_err(&client->dev,
1332 "failed to request IRQ#%u: %d\n",
1333 client->irq, ret);
1334 goto fail;
1335 }
1336
1337 /* enable HPD irq */
1338 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1339 }
1340
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001341 /* enable EDID read irq: */
1342 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1343
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001344 if (!np)
1345 return 0; /* non-DT */
1346
1347 /* get the optional video properties */
1348 ret = of_property_read_u32(np, "video-ports", &video);
1349 if (ret == 0) {
1350 priv->vip_cntrl_0 = video >> 16;
1351 priv->vip_cntrl_1 = video >> 8;
1352 priv->vip_cntrl_2 = video;
1353 }
1354
Rob Clarke7792ce2013-01-08 19:21:02 -06001355 return 0;
1356
1357fail:
1358 /* if encoder_init fails, the encoder slave is never registered,
1359 * so cleanup here:
1360 */
1361 if (priv->cec)
1362 i2c_unregister_device(priv->cec);
1363 kfree(priv);
1364 encoder_slave->slave_priv = NULL;
1365 encoder_slave->slave_funcs = NULL;
1366 return -ENXIO;
1367}
1368
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001369#ifdef CONFIG_OF
1370static const struct of_device_id tda998x_dt_ids[] = {
1371 { .compatible = "nxp,tda998x", },
1372 { }
1373};
1374MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1375#endif
1376
Rob Clarke7792ce2013-01-08 19:21:02 -06001377static struct i2c_device_id tda998x_ids[] = {
1378 { "tda998x", 0 },
1379 { }
1380};
1381MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1382
1383static struct drm_i2c_encoder_driver tda998x_driver = {
1384 .i2c_driver = {
1385 .probe = tda998x_probe,
1386 .remove = tda998x_remove,
1387 .driver = {
1388 .name = "tda998x",
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001389 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001390 },
1391 .id_table = tda998x_ids,
1392 },
1393 .encoder_init = tda998x_encoder_init,
1394};
1395
1396/* Module initialization */
1397
1398static int __init
1399tda998x_init(void)
1400{
1401 DBG("");
1402 return drm_i2c_encoder_register(THIS_MODULE, &tda998x_driver);
1403}
1404
1405static void __exit
1406tda998x_exit(void)
1407{
1408 DBG("");
1409 drm_i2c_encoder_unregister(&tda998x_driver);
1410}
1411
1412MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1413MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1414MODULE_LICENSE("GPL");
1415
1416module_init(tda998x_init);
1417module_exit(tda998x_exit);