blob: e30a2a8c2a3cdc80b600dfd8d41c41a544d22c94 [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
Russell Kingc707c362014-02-07 19:49:44 +000018#include <linux/component.h>
Russell King893c3e52013-08-27 01:27:42 +010019#include <linux/hdmi.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060020#include <linux/module.h>
Jean-Francois Moine12473b72014-01-25 18:14:38 +010021#include <linux/irq.h>
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +010022#include <sound/asoundef.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060023
24#include <drm/drmP.h>
25#include <drm/drm_crtc_helper.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060026#include <drm/drm_edid.h>
Russell King5dbcf312014-06-15 11:11:10 +010027#include <drm/drm_of.h>
Russell Kingc4c11dd2013-08-14 21:43:30 +020028#include <drm/i2c/tda998x.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060029
30#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
31
32struct tda998x_priv {
33 struct i2c_client *cec;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +010034 struct i2c_client *hdmi;
Jean-Francois Moineed9a8422014-11-29 08:30:51 +010035 struct mutex mutex;
Russell Kinge66e03a2015-06-06 21:41:10 +010036 u16 rev;
37 u8 current_page;
Rob Clarke7792ce2013-01-08 19:21:02 -060038 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;
Russell King0fc6f442015-06-06 21:41:09 +010047
48 struct work_struct detect_work;
49 struct timer_list edid_delay_timer;
50 wait_queue_head_t edid_delay_waitq;
51 bool edid_delay_active;
Russell King78e401f2015-08-14 11:17:12 +010052
53 struct drm_encoder encoder;
Rob Clarke7792ce2013-01-08 19:21:02 -060054};
55
Rob Clarke7792ce2013-01-08 19:21:02 -060056/* The TDA9988 series of devices use a paged register scheme.. to simplify
57 * things we encode the page # in upper bits of the register #. To read/
58 * write a given register, we need to make sure CURPAGE register is set
59 * appropriately. Which implies reads/writes are not atomic. Fun!
60 */
61
62#define REG(page, addr) (((page) << 8) | (addr))
63#define REG2ADDR(reg) ((reg) & 0xff)
64#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
65
66#define REG_CURPAGE 0xff /* write */
67
68
69/* Page 00h: General Control */
70#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
71#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
72# define MAIN_CNTRL0_SR (1 << 0)
73# define MAIN_CNTRL0_DECS (1 << 1)
74# define MAIN_CNTRL0_DEHS (1 << 2)
75# define MAIN_CNTRL0_CECS (1 << 3)
76# define MAIN_CNTRL0_CEHS (1 << 4)
77# define MAIN_CNTRL0_SCALER (1 << 7)
78#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
79#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
80# define SOFTRESET_AUDIO (1 << 0)
81# define SOFTRESET_I2C_MASTER (1 << 1)
82#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
83#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
84#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
85# define I2C_MASTER_DIS_MM (1 << 0)
86# define I2C_MASTER_DIS_FILT (1 << 1)
87# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020088#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
89# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060090#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
91#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
92#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
93# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +020094#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -060095#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
96#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
97#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
98#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
99#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
100# define VIP_CNTRL_0_MIRR_A (1 << 7)
101# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
102# define VIP_CNTRL_0_MIRR_B (1 << 3)
103# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
104#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
105# define VIP_CNTRL_1_MIRR_C (1 << 7)
106# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
107# define VIP_CNTRL_1_MIRR_D (1 << 3)
108# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
109#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
110# define VIP_CNTRL_2_MIRR_E (1 << 7)
111# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
112# define VIP_CNTRL_2_MIRR_F (1 << 3)
113# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
114#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
115# define VIP_CNTRL_3_X_TGL (1 << 0)
116# define VIP_CNTRL_3_H_TGL (1 << 1)
117# define VIP_CNTRL_3_V_TGL (1 << 2)
118# define VIP_CNTRL_3_EMB (1 << 3)
119# define VIP_CNTRL_3_SYNC_DE (1 << 4)
120# define VIP_CNTRL_3_SYNC_HS (1 << 5)
121# define VIP_CNTRL_3_DE_INT (1 << 6)
122# define VIP_CNTRL_3_EDGE (1 << 7)
123#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
124# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
125# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
126# define VIP_CNTRL_4_CCIR656 (1 << 4)
127# define VIP_CNTRL_4_656_ALT (1 << 5)
128# define VIP_CNTRL_4_TST_656 (1 << 6)
129# define VIP_CNTRL_4_TST_PAT (1 << 7)
130#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
131# define VIP_CNTRL_5_CKCASE (1 << 0)
132# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200133#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100134# define MUX_AP_SELECT_I2S 0x64
135# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb24812013-08-14 21:43:27 +0200136#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600137#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
138# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
139# define MAT_CONTRL_MAT_BP (1 << 2)
140#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
141#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
142#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
143#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
144#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
145#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
146#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
147#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
148#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
149#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
150#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
151#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
152#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
153#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
154#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
155#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
156#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200157#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
158#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600159#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
160#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200161#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
162#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600163#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
164#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
165#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
166#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
167#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
168#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
169#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
170#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
171#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
172#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200173#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
174#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
175#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
176#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600177#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
178#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
179#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
180#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
181#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200182# define TBG_CNTRL_0_TOP_TGL (1 << 0)
183# define TBG_CNTRL_0_TOP_SEL (1 << 1)
184# define TBG_CNTRL_0_DE_EXT (1 << 2)
185# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600186# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
187# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
188# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
189#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200190# define TBG_CNTRL_1_H_TGL (1 << 0)
191# define TBG_CNTRL_1_V_TGL (1 << 1)
192# define TBG_CNTRL_1_TGL_EN (1 << 2)
193# define TBG_CNTRL_1_X_EXT (1 << 3)
194# define TBG_CNTRL_1_H_EXT (1 << 4)
195# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600196# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
197#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
198#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
199# define HVF_CNTRL_0_SM (1 << 7)
200# define HVF_CNTRL_0_RWB (1 << 6)
201# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
202# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
203#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
204# define HVF_CNTRL_1_FOR (1 << 0)
205# define HVF_CNTRL_1_YUVBLK (1 << 1)
206# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
207# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
208# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
209#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200210#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
211# define I2S_FORMAT(x) (((x) & 3) << 0)
212#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100213# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
214# define AIP_CLKSEL_AIP_I2S (1 << 3)
215# define AIP_CLKSEL_FS_ACLK (0 << 0)
216# define AIP_CLKSEL_FS_MCLK (1 << 0)
217# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600218
219/* Page 02h: PLL settings */
220#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
221# define PLL_SERIAL_1_SRL_FDN (1 << 0)
222# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
223# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
224#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100225# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600226# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
227#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
228# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
229# define PLL_SERIAL_3_SRL_DE (1 << 2)
230# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
231#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
232#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
233#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
234#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
235#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
236#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
237#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
238#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
239#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200240# define AUDIO_DIV_SERCLK_1 0
241# define AUDIO_DIV_SERCLK_2 1
242# define AUDIO_DIV_SERCLK_4 2
243# define AUDIO_DIV_SERCLK_8 3
244# define AUDIO_DIV_SERCLK_16 4
245# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600246#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
247# define SEL_CLK_SEL_CLK1 (1 << 0)
248# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
249# define SEL_CLK_ENA_SC_CLK (1 << 3)
250#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
251
252
253/* Page 09h: EDID Control */
254#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
255/* next 127 successive registers are the EDID block */
256#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
257#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
258#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
259#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
260#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
261
262
263/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200264#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
265#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
266#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
267#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
268#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600269
270
271/* Page 11h: audio settings and content info packets */
272#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
273# define AIP_CNTRL_0_RST_FIFO (1 << 0)
274# define AIP_CNTRL_0_SWAP (1 << 1)
275# define AIP_CNTRL_0_LAYOUT (1 << 2)
276# define AIP_CNTRL_0_ACR_MAN (1 << 5)
277# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200278#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
279# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
280# define CA_I2S_HBR_CHSTAT (1 << 6)
281#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
282#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
283#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
284#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
285#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
286#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
287#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
288#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
289# define CTS_N_K(x) (((x) & 7) << 0)
290# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600291#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
292# define ENC_CNTRL_RST_ENC (1 << 0)
293# define ENC_CNTRL_RST_SEL (1 << 1)
294# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200295#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
296# define DIP_FLAGS_ACR (1 << 0)
297# define DIP_FLAGS_GC (1 << 1)
298#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
299# define DIP_IF_FLAGS_IF1 (1 << 1)
300# define DIP_IF_FLAGS_IF2 (1 << 2)
301# define DIP_IF_FLAGS_IF3 (1 << 3)
302# define DIP_IF_FLAGS_IF4 (1 << 4)
303# define DIP_IF_FLAGS_IF5 (1 << 5)
304#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600305
306
307/* Page 12h: HDCP and OTP */
308#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200309#define REG_TX4 REG(0x12, 0x9b) /* read/write */
310# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600311#define REG_TX33 REG(0x12, 0xb8) /* read/write */
312# define TX33_HDMI (1 << 1)
313
314
315/* Page 13h: Gamut related metadata packets */
316
317
318
319/* CEC registers: (not paged)
320 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100321#define REG_CEC_INTSTATUS 0xee /* read */
322# define CEC_INTSTATUS_CEC (1 << 0)
323# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600324#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
325# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
326# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
327# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
328# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100329#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
330#define REG_CEC_RXSHPDINT 0xfd /* read */
Russell Kingec5d3e82015-06-06 21:41:10 +0100331# define CEC_RXSHPDINT_RXSENS BIT(0)
332# define CEC_RXSHPDINT_HPD BIT(1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600333#define REG_CEC_RXSHPDLEV 0xfe /* read */
334# define CEC_RXSHPDLEV_RXSENS (1 << 0)
335# define CEC_RXSHPDLEV_HPD (1 << 1)
336
337#define REG_CEC_ENAMODS 0xff /* read/write */
338# define CEC_ENAMODS_DIS_FRO (1 << 6)
339# define CEC_ENAMODS_DIS_CCLK (1 << 5)
340# define CEC_ENAMODS_EN_RXSENS (1 << 2)
341# define CEC_ENAMODS_EN_HDMI (1 << 1)
342# define CEC_ENAMODS_EN_CEC (1 << 0)
343
344
345/* Device versions: */
346#define TDA9989N2 0x0101
347#define TDA19989 0x0201
348#define TDA19989N2 0x0202
349#define TDA19988 0x0301
350
351static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100352cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600353{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100354 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100355 u8 buf[] = {addr, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600356 int ret;
357
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100358 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600359 if (ret < 0)
360 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
361}
362
Russell Kinge66e03a2015-06-06 21:41:10 +0100363static u8
364cec_read(struct tda998x_priv *priv, u8 addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600365{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100366 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100367 u8 val;
Rob Clarke7792ce2013-01-08 19:21:02 -0600368 int ret;
369
370 ret = i2c_master_send(client, &addr, sizeof(addr));
371 if (ret < 0)
372 goto fail;
373
374 ret = i2c_master_recv(client, &val, sizeof(val));
375 if (ret < 0)
376 goto fail;
377
378 return val;
379
380fail:
381 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
382 return 0;
383}
384
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100385static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100386set_page(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600387{
Rob Clarke7792ce2013-01-08 19:21:02 -0600388 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100389 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100390 u8 buf[] = {
Rob Clarke7792ce2013-01-08 19:21:02 -0600391 REG_CURPAGE, REG2PAGE(reg)
392 };
393 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100394 if (ret < 0) {
Julia Lawall288ffc72014-12-07 20:20:59 +0100395 dev_err(&client->dev, "%s %04x err %d\n", __func__,
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100396 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100397 return ret;
398 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600399
400 priv->current_page = REG2PAGE(reg);
401 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100402 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600403}
404
405static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100406reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600407{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100408 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100409 u8 addr = REG2ADDR(reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600410 int ret;
411
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100412 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100413 ret = set_page(priv, reg);
414 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100415 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600416
417 ret = i2c_master_send(client, &addr, sizeof(addr));
418 if (ret < 0)
419 goto fail;
420
421 ret = i2c_master_recv(client, buf, cnt);
422 if (ret < 0)
423 goto fail;
424
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100425 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600426
427fail:
428 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100429out:
430 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600431 return ret;
432}
433
Russell Kingc4c11dd2013-08-14 21:43:30 +0200434static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100435reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200436{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100437 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100438 u8 buf[cnt+1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200439 int ret;
440
441 buf[0] = REG2ADDR(reg);
442 memcpy(&buf[1], p, cnt);
443
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100444 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100445 ret = set_page(priv, reg);
446 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100447 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200448
449 ret = i2c_master_send(client, buf, cnt + 1);
450 if (ret < 0)
451 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100452out:
453 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200454}
455
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100456static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100457reg_read(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600458{
Russell Kinge66e03a2015-06-06 21:41:10 +0100459 u8 val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100460 int ret;
461
462 ret = reg_read_range(priv, reg, &val, sizeof(val));
463 if (ret < 0)
464 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600465 return val;
466}
467
468static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100469reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600470{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100471 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100472 u8 buf[] = {REG2ADDR(reg), val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600473 int ret;
474
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100475 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100476 ret = set_page(priv, reg);
477 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100478 goto out;
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);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100483out:
484 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600485}
486
487static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100488reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600489{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100490 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100491 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600492 int ret;
493
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100494 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100495 ret = set_page(priv, reg);
496 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100497 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600498
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100499 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600500 if (ret < 0)
501 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100502out:
503 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600504}
505
506static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100507reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600508{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100509 int old_val;
510
511 old_val = reg_read(priv, reg);
512 if (old_val >= 0)
513 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600514}
515
516static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100517reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600518{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100519 int old_val;
520
521 old_val = reg_read(priv, reg);
522 if (old_val >= 0)
523 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600524}
525
526static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100527tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600528{
529 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100530 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600531 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100532 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600533 msleep(50);
534
535 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100536 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
537 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600538
539 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100540 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
541 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
542 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
543 reg_write(priv, REG_SERIALIZER, 0x00);
544 reg_write(priv, REG_BUFFER_OUT, 0x00);
545 reg_write(priv, REG_PLL_SCG1, 0x00);
546 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
547 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
548 reg_write(priv, REG_PLL_SCGN1, 0xfa);
549 reg_write(priv, REG_PLL_SCGN2, 0x00);
550 reg_write(priv, REG_PLL_SCGR1, 0x5b);
551 reg_write(priv, REG_PLL_SCGR2, 0x00);
552 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200553
554 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100555 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600556}
557
Russell King0fc6f442015-06-06 21:41:09 +0100558/*
559 * The TDA998x has a problem when trying to read the EDID close to a
560 * HPD assertion: it needs a delay of 100ms to avoid timing out while
561 * trying to read EDID data.
562 *
563 * However, tda998x_encoder_get_modes() may be called at any moment
564 * after tda998x_encoder_detect() indicates that we are connected, so
565 * we need to delay probing modes in tda998x_encoder_get_modes() after
566 * we have seen a HPD inactive->active transition. This code implements
567 * that delay.
568 */
569static void tda998x_edid_delay_done(unsigned long data)
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100570{
Russell King0fc6f442015-06-06 21:41:09 +0100571 struct tda998x_priv *priv = (struct tda998x_priv *)data;
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100572
Russell King0fc6f442015-06-06 21:41:09 +0100573 priv->edid_delay_active = false;
574 wake_up(&priv->edid_delay_waitq);
575 schedule_work(&priv->detect_work);
576}
577
578static void tda998x_edid_delay_start(struct tda998x_priv *priv)
579{
580 priv->edid_delay_active = true;
581 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
582}
583
584static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
585{
586 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
587}
588
589/*
590 * We need to run the KMS hotplug event helper outside of our threaded
591 * interrupt routine as this can call back into our get_modes method,
592 * which will want to make use of interrupts.
593 */
594static void tda998x_detect_work(struct work_struct *work)
595{
596 struct tda998x_priv *priv =
597 container_of(work, struct tda998x_priv, detect_work);
Russell King78e401f2015-08-14 11:17:12 +0100598 struct drm_device *dev = priv->encoder.dev;
Russell King0fc6f442015-06-06 21:41:09 +0100599
600 if (dev)
601 drm_kms_helper_hotplug_event(dev);
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100602}
603
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100604/*
605 * only 2 interrupts may occur: screen plug/unplug and EDID read
606 */
607static irqreturn_t tda998x_irq_thread(int irq, void *data)
608{
609 struct tda998x_priv *priv = data;
610 u8 sta, cec, lvl, flag0, flag1, flag2;
Russell Kingf84a97d2015-06-06 21:41:09 +0100611 bool handled = false;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100612
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100613 sta = cec_read(priv, REG_CEC_INTSTATUS);
614 cec = cec_read(priv, REG_CEC_RXSHPDINT);
615 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
616 flag0 = reg_read(priv, REG_INT_FLAGS_0);
617 flag1 = reg_read(priv, REG_INT_FLAGS_1);
618 flag2 = reg_read(priv, REG_INT_FLAGS_2);
619 DRM_DEBUG_DRIVER(
620 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
621 sta, cec, lvl, flag0, flag1, flag2);
Russell Kingec5d3e82015-06-06 21:41:10 +0100622
623 if (cec & CEC_RXSHPDINT_HPD) {
Russell King0fc6f442015-06-06 21:41:09 +0100624 if (lvl & CEC_RXSHPDLEV_HPD)
625 tda998x_edid_delay_start(priv);
626 else
627 schedule_work(&priv->detect_work);
628
Russell Kingf84a97d2015-06-06 21:41:09 +0100629 handled = true;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100630 }
Russell Kingec5d3e82015-06-06 21:41:10 +0100631
632 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
633 priv->wq_edid_wait = 0;
634 wake_up(&priv->wq_edid);
635 handled = true;
636 }
637
Russell Kingf84a97d2015-06-06 21:41:09 +0100638 return IRQ_RETVAL(handled);
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100639}
640
Russell Kingc4c11dd2013-08-14 21:43:30 +0200641static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100642tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
Russell King96795df2015-08-06 10:52:05 +0100643 union hdmi_infoframe *frame)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200644{
Russell King96795df2015-08-06 10:52:05 +0100645 u8 buf[32];
646 ssize_t len;
647
648 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
649 if (len < 0) {
650 dev_err(&priv->hdmi->dev,
651 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
652 frame->any.type, len);
653 return;
654 }
655
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100656 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
Russell King96795df2015-08-06 10:52:05 +0100657 reg_write_range(priv, addr, buf, len);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100658 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200659}
660
661static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100662tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200663{
Russell King96795df2015-08-06 10:52:05 +0100664 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200665
Russell King96795df2015-08-06 10:52:05 +0100666 hdmi_audio_infoframe_init(&frame.audio);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200667
Russell King96795df2015-08-06 10:52:05 +0100668 frame.audio.channels = p->audio_frame[1] & 0x07;
669 frame.audio.channel_allocation = p->audio_frame[4];
670 frame.audio.level_shift_value = (p->audio_frame[5] & 0x78) >> 3;
671 frame.audio.downmix_inhibit = (p->audio_frame[5] & 0x80) >> 7;
Jean-Francois Moine4a6ca1a2015-07-17 13:07:35 +0200672
Russell King96795df2015-08-06 10:52:05 +0100673 /*
674 * L-PCM and IEC61937 compressed audio shall always set sample
675 * frequency to "refer to stream". For others, see the HDMI
676 * specification.
677 */
678 frame.audio.sample_frequency = (p->audio_frame[2] & 0x1c) >> 2;
679
680 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200681}
682
683static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100684tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200685{
Russell King96795df2015-08-06 10:52:05 +0100686 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200687
Russell King96795df2015-08-06 10:52:05 +0100688 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
689 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200690
Russell King96795df2015-08-06 10:52:05 +0100691 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200692}
693
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100694static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200695{
696 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100697 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
698 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
699 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200700 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100701 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200702 }
703}
704
705static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100706tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200707 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
708{
Russell Kinge66e03a2015-06-06 21:41:10 +0100709 u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
710 u32 n;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200711
712 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100713 reg_write(priv, REG_ENA_AP, p->audio_cfg);
714 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200715
716 /* Set audio input source */
717 switch (p->audio_format) {
718 case AFMT_SPDIF:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100719 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
720 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
721 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200722 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200723 break;
724
725 case AFMT_I2S:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100726 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
727 clksel_aip = AIP_CLKSEL_AIP_I2S;
728 clksel_fs = AIP_CLKSEL_FS_ACLK;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200729 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200730 break;
David Herrmann3b288022013-09-01 15:23:04 +0200731
732 default:
733 BUG();
734 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200735 }
736
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100737 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100738 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
739 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100740 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200741
742 /*
743 * Audio input somehow depends on HDMI line rate which is
744 * related to pixclk. Testing showed that modes with pixclk
745 * >100MHz need a larger divider while <40MHz need the default.
746 * There is no detailed info in the datasheet, so we just
747 * assume 100MHz requires larger divider.
748 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100749 adiv = AUDIO_DIV_SERCLK_8;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200750 if (mode->clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100751 adiv++; /* AUDIO_DIV_SERCLK_16 */
752
753 /* S/PDIF asks for a larger divider */
754 if (p->audio_format == AFMT_SPDIF)
755 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
756
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100757 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200758
759 /*
760 * This is the approximate value of N, which happens to be
761 * the recommended values for non-coherent clocks.
762 */
763 n = 128 * p->audio_sample_rate / 1000;
764
765 /* Write the CTS and N values */
766 buf[0] = 0x44;
767 buf[1] = 0x42;
768 buf[2] = 0x01;
769 buf[3] = n;
770 buf[4] = n >> 8;
771 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100772 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200773
774 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100775 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200776
777 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100778 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
779 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200780
781 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100782 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200783 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100784 buf[2] = IEC958_AES3_CON_FS_NOTID;
785 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
786 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100787 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200788
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100789 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100790 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100791 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200792
793 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100794 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200795}
796
Rob Clarke7792ce2013-01-08 19:21:02 -0600797/* DRM encoder functions */
798
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000799static void tda998x_encoder_set_config(struct tda998x_priv *priv,
800 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600801{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200802 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
803 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
804 VIP_CNTRL_0_SWAP_B(p->swap_b) |
805 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
806 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
807 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
808 VIP_CNTRL_1_SWAP_D(p->swap_d) |
809 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
810 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
811 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
812 VIP_CNTRL_2_SWAP_F(p->swap_f) |
813 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
814
815 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600816}
817
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000818static void tda998x_encoder_dpms(struct tda998x_priv *priv, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600819{
Rob Clarke7792ce2013-01-08 19:21:02 -0600820 /* we only care about on or off: */
821 if (mode != DRM_MODE_DPMS_ON)
822 mode = DRM_MODE_DPMS_OFF;
823
824 if (mode == priv->dpms)
825 return;
826
827 switch (mode) {
828 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200829 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100830 reg_write(priv, REG_ENA_VP_0, 0xff);
831 reg_write(priv, REG_ENA_VP_1, 0xff);
832 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600833 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100834 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
835 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
836 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600837 break;
838 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100839 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100840 reg_write(priv, REG_ENA_VP_0, 0x00);
841 reg_write(priv, REG_ENA_VP_1, 0x00);
842 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600843 break;
844 }
845
846 priv->dpms = mode;
847}
848
849static void
850tda998x_encoder_save(struct drm_encoder *encoder)
851{
852 DBG("");
853}
854
855static void
856tda998x_encoder_restore(struct drm_encoder *encoder)
857{
858 DBG("");
859}
860
861static bool
862tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
863 const struct drm_display_mode *mode,
864 struct drm_display_mode *adjusted_mode)
865{
866 return true;
867}
868
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000869static int tda998x_encoder_mode_valid(struct tda998x_priv *priv,
870 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600871{
Russell King92fbdfc2014-02-07 19:52:33 +0000872 if (mode->clock > 150000)
873 return MODE_CLOCK_HIGH;
874 if (mode->htotal >= BIT(13))
875 return MODE_BAD_HVALUE;
876 if (mode->vtotal >= BIT(11))
877 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600878 return MODE_OK;
879}
880
881static void
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000882tda998x_encoder_mode_set(struct tda998x_priv *priv,
883 struct drm_display_mode *mode,
884 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600885{
Russell Kinge66e03a2015-06-06 21:41:10 +0100886 u16 ref_pix, ref_line, n_pix, n_line;
887 u16 hs_pix_s, hs_pix_e;
888 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
889 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
890 u16 vwin1_line_s, vwin1_line_e;
891 u16 vwin2_line_s, vwin2_line_e;
892 u16 de_pix_s, de_pix_e;
893 u8 reg, div, rep;
Rob Clarke7792ce2013-01-08 19:21:02 -0600894
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200895 /*
896 * Internally TDA998x is using ITU-R BT.656 style sync but
897 * we get VESA style sync. TDA998x is using a reference pixel
898 * relative to ITU to sync to the input frame and for output
899 * sync generation. Currently, we are using reference detection
900 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
901 * which is position of rising VS with coincident rising HS.
902 *
903 * Now there is some issues to take care of:
904 * - HDMI data islands require sync-before-active
905 * - TDA998x register values must be > 0 to be enabled
906 * - REFLINE needs an additional offset of +1
907 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
908 *
909 * So we add +1 to all horizontal and vertical register values,
910 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600911 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200912 n_pix = mode->htotal;
913 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600914
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200915 hs_pix_e = mode->hsync_end - mode->hdisplay;
916 hs_pix_s = mode->hsync_start - mode->hdisplay;
917 de_pix_e = mode->htotal;
918 de_pix_s = mode->htotal - mode->hdisplay;
919 ref_pix = 3 + hs_pix_s;
920
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200921 /*
922 * Attached LCD controllers may generate broken sync. Allow
923 * those to adjust the position of the rising VS edge by adding
924 * HSKEW to ref_pix.
925 */
926 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
927 ref_pix += adjusted_mode->hskew;
928
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200929 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
930 ref_line = 1 + mode->vsync_start - mode->vdisplay;
931 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
932 vwin1_line_e = vwin1_line_s + mode->vdisplay;
933 vs1_pix_s = vs1_pix_e = hs_pix_s;
934 vs1_line_s = mode->vsync_start - mode->vdisplay;
935 vs1_line_e = vs1_line_s +
936 mode->vsync_end - mode->vsync_start;
937 vwin2_line_s = vwin2_line_e = 0;
938 vs2_pix_s = vs2_pix_e = 0;
939 vs2_line_s = vs2_line_e = 0;
940 } else {
941 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
942 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
943 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
944 vs1_pix_s = vs1_pix_e = hs_pix_s;
945 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
946 vs1_line_e = vs1_line_s +
947 (mode->vsync_end - mode->vsync_start)/2;
948 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
949 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
950 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
951 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
952 vs2_line_e = vs2_line_s +
953 (mode->vsync_end - mode->vsync_start)/2;
954 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600955
956 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100957 if (div != 0) {
958 div--;
959 if (div > 3)
960 div = 3;
961 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600962
Rob Clarke7792ce2013-01-08 19:21:02 -0600963 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100964 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600965
966 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100967 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100968 reg_clear(priv, REG_TX33, TX33_HDMI);
969 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600970
Rob Clarke7792ce2013-01-08 19:21:02 -0600971 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100972 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600973 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100974 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
975 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600976 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600977
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100978 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100979 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
980 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100981 reg_write(priv, REG_SERIALIZER, 0);
982 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600983
984 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
985 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100986 reg_write(priv, REG_RPT_CNTRL, 0);
987 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600988 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
989
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100990 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600991 PLL_SERIAL_2_SRL_PR(rep));
992
Rob Clarke7792ce2013-01-08 19:21:02 -0600993 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100994 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
995 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600996
997 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100998 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -0600999
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001000 /*
1001 * Sync on rising HSYNC/VSYNC
1002 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001003 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001004
1005 /*
1006 * TDA19988 requires high-active sync at input stage,
1007 * so invert low-active sync provided by master encoder here
1008 */
1009 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001010 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -06001011 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001012 reg |= VIP_CNTRL_3_V_TGL;
1013 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -06001014
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001015 reg_write(priv, REG_VIDFORMAT, 0x00);
1016 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1017 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1018 reg_write16(priv, REG_NPIX_MSB, n_pix);
1019 reg_write16(priv, REG_NLINE_MSB, n_line);
1020 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1021 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1022 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1023 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1024 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1025 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1026 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1027 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1028 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1029 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1030 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1031 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1032 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1033 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1034 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1035 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -06001036
1037 if (priv->rev == TDA19988) {
1038 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001039 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001040 }
1041
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001042 /*
1043 * Always generate sync polarity relative to input sync and
1044 * revert input stage toggled sync at output stage
1045 */
1046 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1047 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1048 reg |= TBG_CNTRL_1_H_TGL;
1049 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1050 reg |= TBG_CNTRL_1_V_TGL;
1051 reg_write(priv, REG_TBG_CNTRL_1, reg);
1052
Rob Clarke7792ce2013-01-08 19:21:02 -06001053 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001054 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001055
1056 /* Only setup the info frames if the sink is HDMI */
1057 if (priv->is_hdmi_sink) {
1058 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001059 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1060 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001061 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1062 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001063
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001064 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001065
1066 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001067 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001068 &priv->params);
1069 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001070}
1071
1072static enum drm_connector_status
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001073tda998x_encoder_detect(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001074{
Russell Kinge66e03a2015-06-06 21:41:10 +01001075 u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001076
Rob Clarke7792ce2013-01-08 19:21:02 -06001077 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1078 connector_status_disconnected;
1079}
1080
Laurent Pinchart07259f82015-01-16 18:37:43 +02001081static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
Rob Clarke7792ce2013-01-08 19:21:02 -06001082{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001083 struct tda998x_priv *priv = data;
Russell Kinge66e03a2015-06-06 21:41:10 +01001084 u8 offset, segptr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001085 int ret, i;
1086
Rob Clarke7792ce2013-01-08 19:21:02 -06001087 offset = (blk & 1) ? 128 : 0;
1088 segptr = blk / 2;
1089
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001090 reg_write(priv, REG_DDC_ADDR, 0xa0);
1091 reg_write(priv, REG_DDC_OFFS, offset);
1092 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1093 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001094
1095 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001096 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001097 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001098
1099 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001100 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001101
1102 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001103 if (priv->hdmi->irq) {
1104 i = wait_event_timeout(priv->wq_edid,
1105 !priv->wq_edid_wait,
1106 msecs_to_jiffies(100));
1107 if (i < 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001108 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001109 return i;
1110 }
1111 } else {
Russell King713456d2014-03-03 14:09:36 +00001112 for (i = 100; i > 0; i--) {
1113 msleep(1);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001114 ret = reg_read(priv, REG_INT_FLAGS_2);
1115 if (ret < 0)
1116 return ret;
1117 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1118 break;
1119 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001120 }
1121
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001122 if (i == 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001123 dev_err(&priv->hdmi->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001124 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001125 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001126
Laurent Pinchart07259f82015-01-16 18:37:43 +02001127 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1128 if (ret != length) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001129 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1130 blk, ret);
Rob Clarke7792ce2013-01-08 19:21:02 -06001131 return ret;
1132 }
1133
Rob Clarke7792ce2013-01-08 19:21:02 -06001134 return 0;
1135}
1136
Rob Clarke7792ce2013-01-08 19:21:02 -06001137static int
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001138tda998x_encoder_get_modes(struct tda998x_priv *priv,
1139 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001140{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001141 struct edid *edid;
1142 int n;
Rob Clarke7792ce2013-01-08 19:21:02 -06001143
Russell King0fc6f442015-06-06 21:41:09 +01001144 /*
1145 * If we get killed while waiting for the HPD timeout, return
1146 * no modes found: we are not in a restartable path, so we
1147 * can't handle signals gracefully.
1148 */
1149 if (tda998x_edid_delay_wait(priv))
1150 return 0;
1151
Laurent Pinchart07259f82015-01-16 18:37:43 +02001152 if (priv->rev == TDA19988)
1153 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1154
1155 edid = drm_do_get_edid(connector, read_edid_block, priv);
1156
1157 if (priv->rev == TDA19988)
1158 reg_set(priv, REG_TX4, TX4_PD_RAM);
1159
1160 if (!edid) {
1161 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1162 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001163 }
1164
Laurent Pinchart07259f82015-01-16 18:37:43 +02001165 drm_mode_connector_update_edid_property(connector, edid);
1166 n = drm_add_edid_modes(connector, edid);
1167 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
1168 kfree(edid);
1169
Rob Clarke7792ce2013-01-08 19:21:02 -06001170 return n;
1171}
1172
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001173static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1174 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001175{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001176 if (priv->hdmi->irq)
1177 connector->polled = DRM_CONNECTOR_POLL_HPD;
1178 else
1179 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1180 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001181}
1182
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001183static void tda998x_destroy(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001184{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001185 /* disable all IRQs and free the IRQ handler */
1186 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1187 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
Russell King0fc6f442015-06-06 21:41:09 +01001188
1189 if (priv->hdmi->irq)
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001190 free_irq(priv->hdmi->irq, priv);
Russell King0fc6f442015-06-06 21:41:09 +01001191
1192 del_timer_sync(&priv->edid_delay_timer);
1193 cancel_work_sync(&priv->detect_work);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001194
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001195 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001196}
1197
Rob Clarke7792ce2013-01-08 19:21:02 -06001198/* I2C driver functions */
1199
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001200static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001201{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001202 struct device_node *np = client->dev.of_node;
1203 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001204 int rev_lo, rev_hi, ret;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001205 unsigned short cec_addr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001206
Russell King5e74c222013-08-14 21:43:29 +02001207 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1208 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1209 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1210
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001211 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001212 priv->hdmi = client;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001213 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1214 cec_addr = 0x34 + (client->addr & 0x03);
1215 priv->cec = i2c_new_dummy(client->adapter, cec_addr);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001216 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001217 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001218
Rob Clarke7792ce2013-01-08 19:21:02 -06001219 priv->dpms = DRM_MODE_DPMS_OFF;
1220
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001221 mutex_init(&priv->mutex); /* protect the page access */
Russell King0fc6f442015-06-06 21:41:09 +01001222 init_waitqueue_head(&priv->edid_delay_waitq);
1223 setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
1224 (unsigned long)priv);
1225 INIT_WORK(&priv->detect_work, tda998x_detect_work);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001226
Rob Clarke7792ce2013-01-08 19:21:02 -06001227 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001228 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001229 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1230
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001231 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001232
1233 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001234 rev_lo = reg_read(priv, REG_VERSION_LSB);
1235 rev_hi = reg_read(priv, REG_VERSION_MSB);
1236 if (rev_lo < 0 || rev_hi < 0) {
1237 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001238 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001239 }
1240
1241 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001242
1243 /* mask off feature bits: */
1244 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1245
1246 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001247 case TDA9989N2:
1248 dev_info(&client->dev, "found TDA9989 n2");
1249 break;
1250 case TDA19989:
1251 dev_info(&client->dev, "found TDA19989");
1252 break;
1253 case TDA19989N2:
1254 dev_info(&client->dev, "found TDA19989 n2");
1255 break;
1256 case TDA19988:
1257 dev_info(&client->dev, "found TDA19988");
1258 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001259 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001260 dev_err(&client->dev, "found unsupported device: %04x\n",
1261 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001262 goto fail;
1263 }
1264
1265 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001266 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001267
1268 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001269 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001270
1271 /* if necessary, disable multi-master: */
1272 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001273 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001274
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001275 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001276 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1277
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001278 /* initialize the optional IRQ */
1279 if (client->irq) {
1280 int irqf_trigger;
1281
Jean-Francois Moine6833d262014-11-29 08:57:15 +01001282 /* init read EDID waitqueue and HDP work */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001283 init_waitqueue_head(&priv->wq_edid);
1284
1285 /* clear pending interrupts */
1286 reg_read(priv, REG_INT_FLAGS_0);
1287 reg_read(priv, REG_INT_FLAGS_1);
1288 reg_read(priv, REG_INT_FLAGS_2);
1289
1290 irqf_trigger =
1291 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1292 ret = request_threaded_irq(client->irq, NULL,
1293 tda998x_irq_thread,
1294 irqf_trigger | IRQF_ONESHOT,
1295 "tda998x", priv);
1296 if (ret) {
1297 dev_err(&client->dev,
1298 "failed to request IRQ#%u: %d\n",
1299 client->irq, ret);
1300 goto fail;
1301 }
1302
1303 /* enable HPD irq */
1304 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1305 }
1306
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001307 /* enable EDID read irq: */
1308 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1309
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001310 if (!np)
1311 return 0; /* non-DT */
1312
1313 /* get the optional video properties */
1314 ret = of_property_read_u32(np, "video-ports", &video);
1315 if (ret == 0) {
1316 priv->vip_cntrl_0 = video >> 16;
1317 priv->vip_cntrl_1 = video >> 8;
1318 priv->vip_cntrl_2 = video;
1319 }
1320
Rob Clarke7792ce2013-01-08 19:21:02 -06001321 return 0;
1322
1323fail:
1324 /* if encoder_init fails, the encoder slave is never registered,
1325 * so cleanup here:
1326 */
1327 if (priv->cec)
1328 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001329 return -ENXIO;
1330}
1331
Russell Kingc707c362014-02-07 19:49:44 +00001332struct tda998x_priv2 {
1333 struct tda998x_priv base;
Russell Kingc707c362014-02-07 19:49:44 +00001334 struct drm_connector connector;
1335};
1336
1337#define conn_to_tda998x_priv2(x) \
1338 container_of(x, struct tda998x_priv2, connector);
1339
1340#define enc_to_tda998x_priv2(x) \
Russell King78e401f2015-08-14 11:17:12 +01001341 container_of(x, struct tda998x_priv2, base.encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001342
1343static void tda998x_encoder2_dpms(struct drm_encoder *encoder, int mode)
1344{
1345 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1346
1347 tda998x_encoder_dpms(&priv->base, mode);
1348}
1349
1350static void tda998x_encoder_prepare(struct drm_encoder *encoder)
1351{
1352 tda998x_encoder2_dpms(encoder, DRM_MODE_DPMS_OFF);
1353}
1354
1355static void tda998x_encoder_commit(struct drm_encoder *encoder)
1356{
1357 tda998x_encoder2_dpms(encoder, DRM_MODE_DPMS_ON);
1358}
1359
1360static void tda998x_encoder2_mode_set(struct drm_encoder *encoder,
1361 struct drm_display_mode *mode,
1362 struct drm_display_mode *adjusted_mode)
1363{
1364 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1365
1366 tda998x_encoder_mode_set(&priv->base, mode, adjusted_mode);
1367}
1368
1369static const struct drm_encoder_helper_funcs tda998x_encoder_helper_funcs = {
1370 .dpms = tda998x_encoder2_dpms,
1371 .save = tda998x_encoder_save,
1372 .restore = tda998x_encoder_restore,
1373 .mode_fixup = tda998x_encoder_mode_fixup,
1374 .prepare = tda998x_encoder_prepare,
1375 .commit = tda998x_encoder_commit,
1376 .mode_set = tda998x_encoder2_mode_set,
1377};
1378
1379static void tda998x_encoder_destroy(struct drm_encoder *encoder)
1380{
1381 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1382
1383 tda998x_destroy(&priv->base);
1384 drm_encoder_cleanup(encoder);
1385}
1386
1387static const struct drm_encoder_funcs tda998x_encoder_funcs = {
1388 .destroy = tda998x_encoder_destroy,
1389};
1390
1391static int tda998x_connector_get_modes(struct drm_connector *connector)
1392{
1393 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1394
1395 return tda998x_encoder_get_modes(&priv->base, connector);
1396}
1397
1398static int tda998x_connector_mode_valid(struct drm_connector *connector,
1399 struct drm_display_mode *mode)
1400{
1401 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1402
1403 return tda998x_encoder_mode_valid(&priv->base, mode);
1404}
1405
1406static struct drm_encoder *
1407tda998x_connector_best_encoder(struct drm_connector *connector)
1408{
1409 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1410
Russell King78e401f2015-08-14 11:17:12 +01001411 return &priv->base.encoder;
Russell Kingc707c362014-02-07 19:49:44 +00001412}
1413
1414static
1415const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
1416 .get_modes = tda998x_connector_get_modes,
1417 .mode_valid = tda998x_connector_mode_valid,
1418 .best_encoder = tda998x_connector_best_encoder,
1419};
1420
1421static enum drm_connector_status
1422tda998x_connector_detect(struct drm_connector *connector, bool force)
1423{
1424 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1425
1426 return tda998x_encoder_detect(&priv->base);
1427}
1428
1429static void tda998x_connector_destroy(struct drm_connector *connector)
1430{
Dave Airlie74cd62e2014-08-05 10:34:33 +10001431 drm_connector_unregister(connector);
Russell Kingc707c362014-02-07 19:49:44 +00001432 drm_connector_cleanup(connector);
1433}
1434
1435static const struct drm_connector_funcs tda998x_connector_funcs = {
1436 .dpms = drm_helper_connector_dpms,
1437 .fill_modes = drm_helper_probe_single_connector_modes,
1438 .detect = tda998x_connector_detect,
1439 .destroy = tda998x_connector_destroy,
1440};
1441
1442static int tda998x_bind(struct device *dev, struct device *master, void *data)
1443{
1444 struct tda998x_encoder_params *params = dev->platform_data;
1445 struct i2c_client *client = to_i2c_client(dev);
1446 struct drm_device *drm = data;
1447 struct tda998x_priv2 *priv;
Russell Kinge66e03a2015-06-06 21:41:10 +01001448 u32 crtcs = 0;
Russell Kingc707c362014-02-07 19:49:44 +00001449 int ret;
1450
1451 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1452 if (!priv)
1453 return -ENOMEM;
1454
1455 dev_set_drvdata(dev, priv);
1456
Russell King5dbcf312014-06-15 11:11:10 +01001457 if (dev->of_node)
1458 crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
1459
1460 /* If no CRTCs were found, fall back to our old behaviour */
1461 if (crtcs == 0) {
1462 dev_warn(dev, "Falling back to first CRTC\n");
1463 crtcs = 1 << 0;
1464 }
1465
Russell Kingc707c362014-02-07 19:49:44 +00001466 priv->connector.interlace_allowed = 1;
Russell King78e401f2015-08-14 11:17:12 +01001467 priv->base.encoder.possible_crtcs = crtcs;
Russell Kingc707c362014-02-07 19:49:44 +00001468
1469 ret = tda998x_create(client, &priv->base);
1470 if (ret)
1471 return ret;
1472
1473 if (!dev->of_node && params)
1474 tda998x_encoder_set_config(&priv->base, params);
1475
1476 tda998x_encoder_set_polling(&priv->base, &priv->connector);
1477
Russell King78e401f2015-08-14 11:17:12 +01001478 drm_encoder_helper_add(&priv->base.encoder, &tda998x_encoder_helper_funcs);
1479 ret = drm_encoder_init(drm, &priv->base.encoder, &tda998x_encoder_funcs,
Russell Kingc707c362014-02-07 19:49:44 +00001480 DRM_MODE_ENCODER_TMDS);
1481 if (ret)
1482 goto err_encoder;
1483
1484 drm_connector_helper_add(&priv->connector,
1485 &tda998x_connector_helper_funcs);
1486 ret = drm_connector_init(drm, &priv->connector,
1487 &tda998x_connector_funcs,
1488 DRM_MODE_CONNECTOR_HDMIA);
1489 if (ret)
1490 goto err_connector;
1491
Dave Airlie74cd62e2014-08-05 10:34:33 +10001492 ret = drm_connector_register(&priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001493 if (ret)
1494 goto err_sysfs;
1495
Russell King78e401f2015-08-14 11:17:12 +01001496 priv->connector.encoder = &priv->base.encoder;
1497 drm_mode_connector_attach_encoder(&priv->connector, &priv->base.encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001498
1499 return 0;
1500
1501err_sysfs:
1502 drm_connector_cleanup(&priv->connector);
1503err_connector:
Russell King78e401f2015-08-14 11:17:12 +01001504 drm_encoder_cleanup(&priv->base.encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001505err_encoder:
1506 tda998x_destroy(&priv->base);
1507 return ret;
1508}
1509
1510static void tda998x_unbind(struct device *dev, struct device *master,
1511 void *data)
1512{
1513 struct tda998x_priv2 *priv = dev_get_drvdata(dev);
1514
1515 drm_connector_cleanup(&priv->connector);
Russell King78e401f2015-08-14 11:17:12 +01001516 drm_encoder_cleanup(&priv->base.encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001517 tda998x_destroy(&priv->base);
1518}
1519
1520static const struct component_ops tda998x_ops = {
1521 .bind = tda998x_bind,
1522 .unbind = tda998x_unbind,
1523};
1524
1525static int
1526tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1527{
1528 return component_add(&client->dev, &tda998x_ops);
1529}
1530
1531static int tda998x_remove(struct i2c_client *client)
1532{
1533 component_del(&client->dev, &tda998x_ops);
1534 return 0;
1535}
1536
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001537#ifdef CONFIG_OF
1538static const struct of_device_id tda998x_dt_ids[] = {
1539 { .compatible = "nxp,tda998x", },
1540 { }
1541};
1542MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1543#endif
1544
Rob Clarke7792ce2013-01-08 19:21:02 -06001545static struct i2c_device_id tda998x_ids[] = {
1546 { "tda998x", 0 },
1547 { }
1548};
1549MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1550
Russell King3d58e312015-08-14 11:13:50 +01001551static struct i2c_driver tda998x_driver = {
1552 .probe = tda998x_probe,
1553 .remove = tda998x_remove,
1554 .driver = {
1555 .name = "tda998x",
1556 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001557 },
Russell King3d58e312015-08-14 11:13:50 +01001558 .id_table = tda998x_ids,
Rob Clarke7792ce2013-01-08 19:21:02 -06001559};
1560
Russell King3d58e312015-08-14 11:13:50 +01001561module_i2c_driver(tda998x_driver);
Rob Clarke7792ce2013-01-08 19:21:02 -06001562
1563MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1564MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1565MODULE_LICENSE("GPL");