blob: 883025d35f87dda492bb108218e61b3655009245 [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;
47 struct drm_encoder *encoder;
Russell King0fc6f442015-06-06 21:41:09 +010048
49 struct work_struct detect_work;
50 struct timer_list edid_delay_timer;
51 wait_queue_head_t edid_delay_waitq;
52 bool edid_delay_active;
Rob Clarke7792ce2013-01-08 19:21:02 -060053};
54
Rob Clarke7792ce2013-01-08 19:21:02 -060055/* The TDA9988 series of devices use a paged register scheme.. to simplify
56 * things we encode the page # in upper bits of the register #. To read/
57 * write a given register, we need to make sure CURPAGE register is set
58 * appropriately. Which implies reads/writes are not atomic. Fun!
59 */
60
61#define REG(page, addr) (((page) << 8) | (addr))
62#define REG2ADDR(reg) ((reg) & 0xff)
63#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
64
65#define REG_CURPAGE 0xff /* write */
66
67
68/* Page 00h: General Control */
69#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
70#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
71# define MAIN_CNTRL0_SR (1 << 0)
72# define MAIN_CNTRL0_DECS (1 << 1)
73# define MAIN_CNTRL0_DEHS (1 << 2)
74# define MAIN_CNTRL0_CECS (1 << 3)
75# define MAIN_CNTRL0_CEHS (1 << 4)
76# define MAIN_CNTRL0_SCALER (1 << 7)
77#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
78#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
79# define SOFTRESET_AUDIO (1 << 0)
80# define SOFTRESET_I2C_MASTER (1 << 1)
81#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
82#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
83#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
84# define I2C_MASTER_DIS_MM (1 << 0)
85# define I2C_MASTER_DIS_FILT (1 << 1)
86# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020087#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
88# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060089#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
90#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
91#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
92# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +020093#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -060094#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
95#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
96#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
97#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
98#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
99# define VIP_CNTRL_0_MIRR_A (1 << 7)
100# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
101# define VIP_CNTRL_0_MIRR_B (1 << 3)
102# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
103#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
104# define VIP_CNTRL_1_MIRR_C (1 << 7)
105# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
106# define VIP_CNTRL_1_MIRR_D (1 << 3)
107# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
108#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
109# define VIP_CNTRL_2_MIRR_E (1 << 7)
110# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
111# define VIP_CNTRL_2_MIRR_F (1 << 3)
112# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
113#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
114# define VIP_CNTRL_3_X_TGL (1 << 0)
115# define VIP_CNTRL_3_H_TGL (1 << 1)
116# define VIP_CNTRL_3_V_TGL (1 << 2)
117# define VIP_CNTRL_3_EMB (1 << 3)
118# define VIP_CNTRL_3_SYNC_DE (1 << 4)
119# define VIP_CNTRL_3_SYNC_HS (1 << 5)
120# define VIP_CNTRL_3_DE_INT (1 << 6)
121# define VIP_CNTRL_3_EDGE (1 << 7)
122#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
123# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
124# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
125# define VIP_CNTRL_4_CCIR656 (1 << 4)
126# define VIP_CNTRL_4_656_ALT (1 << 5)
127# define VIP_CNTRL_4_TST_656 (1 << 6)
128# define VIP_CNTRL_4_TST_PAT (1 << 7)
129#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
130# define VIP_CNTRL_5_CKCASE (1 << 0)
131# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200132#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100133# define MUX_AP_SELECT_I2S 0x64
134# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb2481d2013-08-14 21:43:27 +0200135#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600136#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
137# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
138# define MAT_CONTRL_MAT_BP (1 << 2)
139#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
140#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
141#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
142#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
143#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
144#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
145#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
146#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
147#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
148#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
149#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
150#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
151#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
152#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
153#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
154#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
155#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200156#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
157#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600158#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
159#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200160#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
161#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600162#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
163#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
164#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
165#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
166#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
167#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
168#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
169#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
170#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
171#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200172#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
173#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
174#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
175#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600176#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
177#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
178#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
179#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
180#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200181# define TBG_CNTRL_0_TOP_TGL (1 << 0)
182# define TBG_CNTRL_0_TOP_SEL (1 << 1)
183# define TBG_CNTRL_0_DE_EXT (1 << 2)
184# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600185# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
186# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
187# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
188#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200189# define TBG_CNTRL_1_H_TGL (1 << 0)
190# define TBG_CNTRL_1_V_TGL (1 << 1)
191# define TBG_CNTRL_1_TGL_EN (1 << 2)
192# define TBG_CNTRL_1_X_EXT (1 << 3)
193# define TBG_CNTRL_1_H_EXT (1 << 4)
194# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600195# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
196#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
197#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
198# define HVF_CNTRL_0_SM (1 << 7)
199# define HVF_CNTRL_0_RWB (1 << 6)
200# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
201# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
202#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
203# define HVF_CNTRL_1_FOR (1 << 0)
204# define HVF_CNTRL_1_YUVBLK (1 << 1)
205# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
206# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
207# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
208#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200209#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
210# define I2S_FORMAT(x) (((x) & 3) << 0)
211#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100212# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
213# define AIP_CLKSEL_AIP_I2S (1 << 3)
214# define AIP_CLKSEL_FS_ACLK (0 << 0)
215# define AIP_CLKSEL_FS_MCLK (1 << 0)
216# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600217
218/* Page 02h: PLL settings */
219#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
220# define PLL_SERIAL_1_SRL_FDN (1 << 0)
221# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
222# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
223#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100224# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600225# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
226#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
227# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
228# define PLL_SERIAL_3_SRL_DE (1 << 2)
229# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
230#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
231#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
232#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
233#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
234#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
235#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
236#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
237#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
238#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200239# define AUDIO_DIV_SERCLK_1 0
240# define AUDIO_DIV_SERCLK_2 1
241# define AUDIO_DIV_SERCLK_4 2
242# define AUDIO_DIV_SERCLK_8 3
243# define AUDIO_DIV_SERCLK_16 4
244# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600245#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
246# define SEL_CLK_SEL_CLK1 (1 << 0)
247# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
248# define SEL_CLK_ENA_SC_CLK (1 << 3)
249#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
250
251
252/* Page 09h: EDID Control */
253#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
254/* next 127 successive registers are the EDID block */
255#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
256#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
257#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
258#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
259#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
260
261
262/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200263#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
264#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
265#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
266#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
267#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600268
269
270/* Page 11h: audio settings and content info packets */
271#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
272# define AIP_CNTRL_0_RST_FIFO (1 << 0)
273# define AIP_CNTRL_0_SWAP (1 << 1)
274# define AIP_CNTRL_0_LAYOUT (1 << 2)
275# define AIP_CNTRL_0_ACR_MAN (1 << 5)
276# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200277#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
278# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
279# define CA_I2S_HBR_CHSTAT (1 << 6)
280#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
281#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
282#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
283#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
284#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
285#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
286#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
287#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
288# define CTS_N_K(x) (((x) & 7) << 0)
289# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600290#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
291# define ENC_CNTRL_RST_ENC (1 << 0)
292# define ENC_CNTRL_RST_SEL (1 << 1)
293# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200294#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
295# define DIP_FLAGS_ACR (1 << 0)
296# define DIP_FLAGS_GC (1 << 1)
297#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
298# define DIP_IF_FLAGS_IF1 (1 << 1)
299# define DIP_IF_FLAGS_IF2 (1 << 2)
300# define DIP_IF_FLAGS_IF3 (1 << 3)
301# define DIP_IF_FLAGS_IF4 (1 << 4)
302# define DIP_IF_FLAGS_IF5 (1 << 5)
303#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600304
305
306/* Page 12h: HDCP and OTP */
307#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200308#define REG_TX4 REG(0x12, 0x9b) /* read/write */
309# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600310#define REG_TX33 REG(0x12, 0xb8) /* read/write */
311# define TX33_HDMI (1 << 1)
312
313
314/* Page 13h: Gamut related metadata packets */
315
316
317
318/* CEC registers: (not paged)
319 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100320#define REG_CEC_INTSTATUS 0xee /* read */
321# define CEC_INTSTATUS_CEC (1 << 0)
322# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600323#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
324# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
325# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
326# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
327# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100328#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
329#define REG_CEC_RXSHPDINT 0xfd /* read */
Russell Kingec5d3e82015-06-06 21:41:10 +0100330# define CEC_RXSHPDINT_RXSENS BIT(0)
331# define CEC_RXSHPDINT_HPD BIT(1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600332#define REG_CEC_RXSHPDLEV 0xfe /* read */
333# define CEC_RXSHPDLEV_RXSENS (1 << 0)
334# define CEC_RXSHPDLEV_HPD (1 << 1)
335
336#define REG_CEC_ENAMODS 0xff /* read/write */
337# define CEC_ENAMODS_DIS_FRO (1 << 6)
338# define CEC_ENAMODS_DIS_CCLK (1 << 5)
339# define CEC_ENAMODS_EN_RXSENS (1 << 2)
340# define CEC_ENAMODS_EN_HDMI (1 << 1)
341# define CEC_ENAMODS_EN_CEC (1 << 0)
342
343
344/* Device versions: */
345#define TDA9989N2 0x0101
346#define TDA19989 0x0201
347#define TDA19989N2 0x0202
348#define TDA19988 0x0301
349
350static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100351cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600352{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100353 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100354 u8 buf[] = {addr, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600355 int ret;
356
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100357 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600358 if (ret < 0)
359 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
360}
361
Russell Kinge66e03a2015-06-06 21:41:10 +0100362static u8
363cec_read(struct tda998x_priv *priv, u8 addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600364{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100365 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100366 u8 val;
Rob Clarke7792ce2013-01-08 19:21:02 -0600367 int ret;
368
369 ret = i2c_master_send(client, &addr, sizeof(addr));
370 if (ret < 0)
371 goto fail;
372
373 ret = i2c_master_recv(client, &val, sizeof(val));
374 if (ret < 0)
375 goto fail;
376
377 return val;
378
379fail:
380 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
381 return 0;
382}
383
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100384static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100385set_page(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600386{
Rob Clarke7792ce2013-01-08 19:21:02 -0600387 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100388 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100389 u8 buf[] = {
Rob Clarke7792ce2013-01-08 19:21:02 -0600390 REG_CURPAGE, REG2PAGE(reg)
391 };
392 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100393 if (ret < 0) {
Julia Lawall288ffc72014-12-07 20:20:59 +0100394 dev_err(&client->dev, "%s %04x err %d\n", __func__,
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100395 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100396 return ret;
397 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600398
399 priv->current_page = REG2PAGE(reg);
400 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100401 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600402}
403
404static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100405reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600406{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100407 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100408 u8 addr = REG2ADDR(reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600409 int ret;
410
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100411 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100412 ret = set_page(priv, reg);
413 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100414 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600415
416 ret = i2c_master_send(client, &addr, sizeof(addr));
417 if (ret < 0)
418 goto fail;
419
420 ret = i2c_master_recv(client, buf, cnt);
421 if (ret < 0)
422 goto fail;
423
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100424 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600425
426fail:
427 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100428out:
429 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600430 return ret;
431}
432
Russell Kingc4c11dd2013-08-14 21:43:30 +0200433static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100434reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200435{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100436 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100437 u8 buf[cnt+1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200438 int ret;
439
440 buf[0] = REG2ADDR(reg);
441 memcpy(&buf[1], p, cnt);
442
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100443 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100444 ret = set_page(priv, reg);
445 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100446 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200447
448 ret = i2c_master_send(client, buf, cnt + 1);
449 if (ret < 0)
450 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100451out:
452 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200453}
454
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100455static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100456reg_read(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600457{
Russell Kinge66e03a2015-06-06 21:41:10 +0100458 u8 val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100459 int ret;
460
461 ret = reg_read_range(priv, reg, &val, sizeof(val));
462 if (ret < 0)
463 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600464 return val;
465}
466
467static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100468reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600469{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100470 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100471 u8 buf[] = {REG2ADDR(reg), val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600472 int ret;
473
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100474 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100475 ret = set_page(priv, reg);
476 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100477 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600478
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100479 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600480 if (ret < 0)
481 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100482out:
483 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600484}
485
486static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100487reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600488{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100489 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100490 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600491 int ret;
492
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100493 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100494 ret = set_page(priv, reg);
495 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100496 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600497
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100498 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600499 if (ret < 0)
500 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100501out:
502 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600503}
504
505static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100506reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600507{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100508 int old_val;
509
510 old_val = reg_read(priv, reg);
511 if (old_val >= 0)
512 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600513}
514
515static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100516reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600517{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100518 int old_val;
519
520 old_val = reg_read(priv, reg);
521 if (old_val >= 0)
522 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600523}
524
525static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100526tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600527{
528 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100529 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600530 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100531 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600532 msleep(50);
533
534 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100535 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
536 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600537
538 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100539 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
540 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
541 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
542 reg_write(priv, REG_SERIALIZER, 0x00);
543 reg_write(priv, REG_BUFFER_OUT, 0x00);
544 reg_write(priv, REG_PLL_SCG1, 0x00);
545 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
546 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
547 reg_write(priv, REG_PLL_SCGN1, 0xfa);
548 reg_write(priv, REG_PLL_SCGN2, 0x00);
549 reg_write(priv, REG_PLL_SCGR1, 0x5b);
550 reg_write(priv, REG_PLL_SCGR2, 0x00);
551 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb2481d2013-08-14 21:43:27 +0200552
553 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100554 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600555}
556
Russell King0fc6f442015-06-06 21:41:09 +0100557/*
558 * The TDA998x has a problem when trying to read the EDID close to a
559 * HPD assertion: it needs a delay of 100ms to avoid timing out while
560 * trying to read EDID data.
561 *
562 * However, tda998x_encoder_get_modes() may be called at any moment
563 * after tda998x_encoder_detect() indicates that we are connected, so
564 * we need to delay probing modes in tda998x_encoder_get_modes() after
565 * we have seen a HPD inactive->active transition. This code implements
566 * that delay.
567 */
568static void tda998x_edid_delay_done(unsigned long data)
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100569{
Russell King0fc6f442015-06-06 21:41:09 +0100570 struct tda998x_priv *priv = (struct tda998x_priv *)data;
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100571
Russell King0fc6f442015-06-06 21:41:09 +0100572 priv->edid_delay_active = false;
573 wake_up(&priv->edid_delay_waitq);
574 schedule_work(&priv->detect_work);
575}
576
577static void tda998x_edid_delay_start(struct tda998x_priv *priv)
578{
579 priv->edid_delay_active = true;
580 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
581}
582
583static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
584{
585 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
586}
587
588/*
589 * We need to run the KMS hotplug event helper outside of our threaded
590 * interrupt routine as this can call back into our get_modes method,
591 * which will want to make use of interrupts.
592 */
593static void tda998x_detect_work(struct work_struct *work)
594{
595 struct tda998x_priv *priv =
596 container_of(work, struct tda998x_priv, detect_work);
597 struct drm_device *dev = priv->encoder->dev;
598
599 if (dev)
600 drm_kms_helper_hotplug_event(dev);
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100601}
602
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100603/*
604 * only 2 interrupts may occur: screen plug/unplug and EDID read
605 */
606static irqreturn_t tda998x_irq_thread(int irq, void *data)
607{
608 struct tda998x_priv *priv = data;
609 u8 sta, cec, lvl, flag0, flag1, flag2;
Russell Kingf84a97d2015-06-06 21:41:09 +0100610 bool handled = false;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100611
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100612 sta = cec_read(priv, REG_CEC_INTSTATUS);
613 cec = cec_read(priv, REG_CEC_RXSHPDINT);
614 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
615 flag0 = reg_read(priv, REG_INT_FLAGS_0);
616 flag1 = reg_read(priv, REG_INT_FLAGS_1);
617 flag2 = reg_read(priv, REG_INT_FLAGS_2);
618 DRM_DEBUG_DRIVER(
619 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
620 sta, cec, lvl, flag0, flag1, flag2);
Russell Kingec5d3e82015-06-06 21:41:10 +0100621
622 if (cec & CEC_RXSHPDINT_HPD) {
Russell King0fc6f442015-06-06 21:41:09 +0100623 if (lvl & CEC_RXSHPDLEV_HPD)
624 tda998x_edid_delay_start(priv);
625 else
626 schedule_work(&priv->detect_work);
627
Russell Kingf84a97d2015-06-06 21:41:09 +0100628 handled = true;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100629 }
Russell Kingec5d3e82015-06-06 21:41:10 +0100630
631 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
632 priv->wq_edid_wait = 0;
633 wake_up(&priv->wq_edid);
634 handled = true;
635 }
636
Russell Kingf84a97d2015-06-06 21:41:09 +0100637 return IRQ_RETVAL(handled);
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100638}
639
Russell Kingc4c11dd2013-08-14 21:43:30 +0200640static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100641tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
Russell King96795df2015-08-06 10:52:05 +0100642 union hdmi_infoframe *frame)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200643{
Russell King96795df2015-08-06 10:52:05 +0100644 u8 buf[32];
645 ssize_t len;
646
647 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
648 if (len < 0) {
649 dev_err(&priv->hdmi->dev,
650 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
651 frame->any.type, len);
652 return;
653 }
654
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100655 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
Russell King96795df2015-08-06 10:52:05 +0100656 reg_write_range(priv, addr, buf, len);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100657 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200658}
659
660static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100661tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200662{
Russell King96795df2015-08-06 10:52:05 +0100663 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200664
Russell King96795df2015-08-06 10:52:05 +0100665 hdmi_audio_infoframe_init(&frame.audio);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200666
Russell King96795df2015-08-06 10:52:05 +0100667 frame.audio.channels = p->audio_frame[1] & 0x07;
668 frame.audio.channel_allocation = p->audio_frame[4];
669 frame.audio.level_shift_value = (p->audio_frame[5] & 0x78) >> 3;
670 frame.audio.downmix_inhibit = (p->audio_frame[5] & 0x80) >> 7;
Jean-Francois Moine4a6ca1a2015-07-17 13:07:35 +0200671
Russell King96795df2015-08-06 10:52:05 +0100672 /*
673 * L-PCM and IEC61937 compressed audio shall always set sample
674 * frequency to "refer to stream". For others, see the HDMI
675 * specification.
676 */
677 frame.audio.sample_frequency = (p->audio_frame[2] & 0x1c) >> 2;
678
679 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200680}
681
682static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100683tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200684{
Russell King96795df2015-08-06 10:52:05 +0100685 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200686
Russell King96795df2015-08-06 10:52:05 +0100687 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
688 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200689
Russell King96795df2015-08-06 10:52:05 +0100690 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200691}
692
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100693static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200694{
695 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100696 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
697 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
698 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200699 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100700 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200701 }
702}
703
704static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100705tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200706 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
707{
Russell Kinge66e03a2015-06-06 21:41:10 +0100708 u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
709 u32 n;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200710
711 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100712 reg_write(priv, REG_ENA_AP, p->audio_cfg);
713 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200714
715 /* Set audio input source */
716 switch (p->audio_format) {
717 case AFMT_SPDIF:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100718 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
719 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
720 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200721 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200722 break;
723
724 case AFMT_I2S:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100725 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
726 clksel_aip = AIP_CLKSEL_AIP_I2S;
727 clksel_fs = AIP_CLKSEL_FS_ACLK;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200728 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200729 break;
David Herrmann3b288022013-09-01 15:23:04 +0200730
731 default:
732 BUG();
733 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200734 }
735
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100736 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100737 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
738 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100739 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200740
741 /*
742 * Audio input somehow depends on HDMI line rate which is
743 * related to pixclk. Testing showed that modes with pixclk
744 * >100MHz need a larger divider while <40MHz need the default.
745 * There is no detailed info in the datasheet, so we just
746 * assume 100MHz requires larger divider.
747 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100748 adiv = AUDIO_DIV_SERCLK_8;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200749 if (mode->clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100750 adiv++; /* AUDIO_DIV_SERCLK_16 */
751
752 /* S/PDIF asks for a larger divider */
753 if (p->audio_format == AFMT_SPDIF)
754 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
755
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100756 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200757
758 /*
759 * This is the approximate value of N, which happens to be
760 * the recommended values for non-coherent clocks.
761 */
762 n = 128 * p->audio_sample_rate / 1000;
763
764 /* Write the CTS and N values */
765 buf[0] = 0x44;
766 buf[1] = 0x42;
767 buf[2] = 0x01;
768 buf[3] = n;
769 buf[4] = n >> 8;
770 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100771 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200772
773 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100774 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200775
776 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100777 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
778 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200779
780 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100781 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200782 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100783 buf[2] = IEC958_AES3_CON_FS_NOTID;
784 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
785 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100786 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200787
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100788 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100789 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100790 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200791
792 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100793 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200794}
795
Rob Clarke7792ce2013-01-08 19:21:02 -0600796/* DRM encoder functions */
797
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000798static void tda998x_encoder_set_config(struct tda998x_priv *priv,
799 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600800{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200801 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
802 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
803 VIP_CNTRL_0_SWAP_B(p->swap_b) |
804 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
805 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
806 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
807 VIP_CNTRL_1_SWAP_D(p->swap_d) |
808 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
809 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
810 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
811 VIP_CNTRL_2_SWAP_F(p->swap_f) |
812 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
813
814 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600815}
816
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000817static void tda998x_encoder_dpms(struct tda998x_priv *priv, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600818{
Rob Clarke7792ce2013-01-08 19:21:02 -0600819 /* we only care about on or off: */
820 if (mode != DRM_MODE_DPMS_ON)
821 mode = DRM_MODE_DPMS_OFF;
822
823 if (mode == priv->dpms)
824 return;
825
826 switch (mode) {
827 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200828 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100829 reg_write(priv, REG_ENA_VP_0, 0xff);
830 reg_write(priv, REG_ENA_VP_1, 0xff);
831 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600832 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100833 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
834 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
835 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600836 break;
837 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100838 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100839 reg_write(priv, REG_ENA_VP_0, 0x00);
840 reg_write(priv, REG_ENA_VP_1, 0x00);
841 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600842 break;
843 }
844
845 priv->dpms = mode;
846}
847
848static void
849tda998x_encoder_save(struct drm_encoder *encoder)
850{
851 DBG("");
852}
853
854static void
855tda998x_encoder_restore(struct drm_encoder *encoder)
856{
857 DBG("");
858}
859
860static bool
861tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
862 const struct drm_display_mode *mode,
863 struct drm_display_mode *adjusted_mode)
864{
865 return true;
866}
867
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000868static int tda998x_encoder_mode_valid(struct tda998x_priv *priv,
869 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600870{
Russell King92fbdfc2014-02-07 19:52:33 +0000871 if (mode->clock > 150000)
872 return MODE_CLOCK_HIGH;
873 if (mode->htotal >= BIT(13))
874 return MODE_BAD_HVALUE;
875 if (mode->vtotal >= BIT(11))
876 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600877 return MODE_OK;
878}
879
880static void
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000881tda998x_encoder_mode_set(struct tda998x_priv *priv,
882 struct drm_display_mode *mode,
883 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600884{
Russell Kinge66e03a2015-06-06 21:41:10 +0100885 u16 ref_pix, ref_line, n_pix, n_line;
886 u16 hs_pix_s, hs_pix_e;
887 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
888 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
889 u16 vwin1_line_s, vwin1_line_e;
890 u16 vwin2_line_s, vwin2_line_e;
891 u16 de_pix_s, de_pix_e;
892 u8 reg, div, rep;
Rob Clarke7792ce2013-01-08 19:21:02 -0600893
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200894 /*
895 * Internally TDA998x is using ITU-R BT.656 style sync but
896 * we get VESA style sync. TDA998x is using a reference pixel
897 * relative to ITU to sync to the input frame and for output
898 * sync generation. Currently, we are using reference detection
899 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
900 * which is position of rising VS with coincident rising HS.
901 *
902 * Now there is some issues to take care of:
903 * - HDMI data islands require sync-before-active
904 * - TDA998x register values must be > 0 to be enabled
905 * - REFLINE needs an additional offset of +1
906 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
907 *
908 * So we add +1 to all horizontal and vertical register values,
909 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600910 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200911 n_pix = mode->htotal;
912 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600913
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200914 hs_pix_e = mode->hsync_end - mode->hdisplay;
915 hs_pix_s = mode->hsync_start - mode->hdisplay;
916 de_pix_e = mode->htotal;
917 de_pix_s = mode->htotal - mode->hdisplay;
918 ref_pix = 3 + hs_pix_s;
919
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200920 /*
921 * Attached LCD controllers may generate broken sync. Allow
922 * those to adjust the position of the rising VS edge by adding
923 * HSKEW to ref_pix.
924 */
925 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
926 ref_pix += adjusted_mode->hskew;
927
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200928 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
929 ref_line = 1 + mode->vsync_start - mode->vdisplay;
930 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
931 vwin1_line_e = vwin1_line_s + mode->vdisplay;
932 vs1_pix_s = vs1_pix_e = hs_pix_s;
933 vs1_line_s = mode->vsync_start - mode->vdisplay;
934 vs1_line_e = vs1_line_s +
935 mode->vsync_end - mode->vsync_start;
936 vwin2_line_s = vwin2_line_e = 0;
937 vs2_pix_s = vs2_pix_e = 0;
938 vs2_line_s = vs2_line_e = 0;
939 } else {
940 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
941 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
942 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
943 vs1_pix_s = vs1_pix_e = hs_pix_s;
944 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
945 vs1_line_e = vs1_line_s +
946 (mode->vsync_end - mode->vsync_start)/2;
947 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
948 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
949 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
950 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
951 vs2_line_e = vs2_line_s +
952 (mode->vsync_end - mode->vsync_start)/2;
953 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600954
955 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100956 if (div != 0) {
957 div--;
958 if (div > 3)
959 div = 3;
960 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600961
Rob Clarke7792ce2013-01-08 19:21:02 -0600962 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100963 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600964
965 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100966 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100967 reg_clear(priv, REG_TX33, TX33_HDMI);
968 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600969
Rob Clarke7792ce2013-01-08 19:21:02 -0600970 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100971 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600972 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100973 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
974 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600975 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600976
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100977 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100978 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
979 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100980 reg_write(priv, REG_SERIALIZER, 0);
981 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600982
983 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
984 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100985 reg_write(priv, REG_RPT_CNTRL, 0);
986 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600987 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
988
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100989 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600990 PLL_SERIAL_2_SRL_PR(rep));
991
Rob Clarke7792ce2013-01-08 19:21:02 -0600992 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100993 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
994 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600995
996 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100997 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -0600998
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200999 /*
1000 * Sync on rising HSYNC/VSYNC
1001 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001002 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001003
1004 /*
1005 * TDA19988 requires high-active sync at input stage,
1006 * so invert low-active sync provided by master encoder here
1007 */
1008 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001009 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -06001010 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001011 reg |= VIP_CNTRL_3_V_TGL;
1012 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -06001013
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001014 reg_write(priv, REG_VIDFORMAT, 0x00);
1015 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1016 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1017 reg_write16(priv, REG_NPIX_MSB, n_pix);
1018 reg_write16(priv, REG_NLINE_MSB, n_line);
1019 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1020 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1021 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1022 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1023 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1024 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1025 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1026 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1027 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1028 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1029 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1030 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1031 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1032 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1033 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1034 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -06001035
1036 if (priv->rev == TDA19988) {
1037 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001038 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001039 }
1040
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001041 /*
1042 * Always generate sync polarity relative to input sync and
1043 * revert input stage toggled sync at output stage
1044 */
1045 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1046 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1047 reg |= TBG_CNTRL_1_H_TGL;
1048 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1049 reg |= TBG_CNTRL_1_V_TGL;
1050 reg_write(priv, REG_TBG_CNTRL_1, reg);
1051
Rob Clarke7792ce2013-01-08 19:21:02 -06001052 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001053 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001054
1055 /* Only setup the info frames if the sink is HDMI */
1056 if (priv->is_hdmi_sink) {
1057 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001058 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1059 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001060 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1061 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001062
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001063 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001064
1065 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001066 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001067 &priv->params);
1068 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001069}
1070
1071static enum drm_connector_status
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001072tda998x_encoder_detect(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001073{
Russell Kinge66e03a2015-06-06 21:41:10 +01001074 u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001075
Rob Clarke7792ce2013-01-08 19:21:02 -06001076 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1077 connector_status_disconnected;
1078}
1079
Laurent Pinchart07259f82015-01-16 18:37:43 +02001080static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
Rob Clarke7792ce2013-01-08 19:21:02 -06001081{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001082 struct tda998x_priv *priv = data;
Russell Kinge66e03a2015-06-06 21:41:10 +01001083 u8 offset, segptr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001084 int ret, i;
1085
Rob Clarke7792ce2013-01-08 19:21:02 -06001086 offset = (blk & 1) ? 128 : 0;
1087 segptr = blk / 2;
1088
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001089 reg_write(priv, REG_DDC_ADDR, 0xa0);
1090 reg_write(priv, REG_DDC_OFFS, offset);
1091 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1092 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001093
1094 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001095 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001096 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001097
1098 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001099 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001100
1101 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001102 if (priv->hdmi->irq) {
1103 i = wait_event_timeout(priv->wq_edid,
1104 !priv->wq_edid_wait,
1105 msecs_to_jiffies(100));
1106 if (i < 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001107 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001108 return i;
1109 }
1110 } else {
Russell King713456d2014-03-03 14:09:36 +00001111 for (i = 100; i > 0; i--) {
1112 msleep(1);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001113 ret = reg_read(priv, REG_INT_FLAGS_2);
1114 if (ret < 0)
1115 return ret;
1116 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1117 break;
1118 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001119 }
1120
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001121 if (i == 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001122 dev_err(&priv->hdmi->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001123 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001124 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001125
Laurent Pinchart07259f82015-01-16 18:37:43 +02001126 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1127 if (ret != length) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001128 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1129 blk, ret);
Rob Clarke7792ce2013-01-08 19:21:02 -06001130 return ret;
1131 }
1132
Rob Clarke7792ce2013-01-08 19:21:02 -06001133 return 0;
1134}
1135
Rob Clarke7792ce2013-01-08 19:21:02 -06001136static int
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001137tda998x_encoder_get_modes(struct tda998x_priv *priv,
1138 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001139{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001140 struct edid *edid;
1141 int n;
Rob Clarke7792ce2013-01-08 19:21:02 -06001142
Russell King0fc6f442015-06-06 21:41:09 +01001143 /*
1144 * If we get killed while waiting for the HPD timeout, return
1145 * no modes found: we are not in a restartable path, so we
1146 * can't handle signals gracefully.
1147 */
1148 if (tda998x_edid_delay_wait(priv))
1149 return 0;
1150
Laurent Pinchart07259f82015-01-16 18:37:43 +02001151 if (priv->rev == TDA19988)
1152 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1153
1154 edid = drm_do_get_edid(connector, read_edid_block, priv);
1155
1156 if (priv->rev == TDA19988)
1157 reg_set(priv, REG_TX4, TX4_PD_RAM);
1158
1159 if (!edid) {
1160 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1161 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001162 }
1163
Laurent Pinchart07259f82015-01-16 18:37:43 +02001164 drm_mode_connector_update_edid_property(connector, edid);
1165 n = drm_add_edid_modes(connector, edid);
1166 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
1167 kfree(edid);
1168
Rob Clarke7792ce2013-01-08 19:21:02 -06001169 return n;
1170}
1171
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001172static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1173 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001174{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001175 if (priv->hdmi->irq)
1176 connector->polled = DRM_CONNECTOR_POLL_HPD;
1177 else
1178 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1179 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001180}
1181
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001182static void tda998x_destroy(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001183{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001184 /* disable all IRQs and free the IRQ handler */
1185 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1186 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
Russell King0fc6f442015-06-06 21:41:09 +01001187
1188 if (priv->hdmi->irq)
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001189 free_irq(priv->hdmi->irq, priv);
Russell King0fc6f442015-06-06 21:41:09 +01001190
1191 del_timer_sync(&priv->edid_delay_timer);
1192 cancel_work_sync(&priv->detect_work);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001193
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001194 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001195}
1196
Rob Clarke7792ce2013-01-08 19:21:02 -06001197/* I2C driver functions */
1198
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001199static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001200{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001201 struct device_node *np = client->dev.of_node;
1202 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001203 int rev_lo, rev_hi, ret;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001204 unsigned short cec_addr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001205
Russell King5e74c222013-08-14 21:43:29 +02001206 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1207 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1208 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1209
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001210 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001211 priv->hdmi = client;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001212 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1213 cec_addr = 0x34 + (client->addr & 0x03);
1214 priv->cec = i2c_new_dummy(client->adapter, cec_addr);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001215 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001216 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001217
Rob Clarke7792ce2013-01-08 19:21:02 -06001218 priv->dpms = DRM_MODE_DPMS_OFF;
1219
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001220 mutex_init(&priv->mutex); /* protect the page access */
Russell King0fc6f442015-06-06 21:41:09 +01001221 init_waitqueue_head(&priv->edid_delay_waitq);
1222 setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
1223 (unsigned long)priv);
1224 INIT_WORK(&priv->detect_work, tda998x_detect_work);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001225
Rob Clarke7792ce2013-01-08 19:21:02 -06001226 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001227 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001228 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1229
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001230 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001231
1232 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001233 rev_lo = reg_read(priv, REG_VERSION_LSB);
1234 rev_hi = reg_read(priv, REG_VERSION_MSB);
1235 if (rev_lo < 0 || rev_hi < 0) {
1236 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001237 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001238 }
1239
1240 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001241
1242 /* mask off feature bits: */
1243 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1244
1245 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001246 case TDA9989N2:
1247 dev_info(&client->dev, "found TDA9989 n2");
1248 break;
1249 case TDA19989:
1250 dev_info(&client->dev, "found TDA19989");
1251 break;
1252 case TDA19989N2:
1253 dev_info(&client->dev, "found TDA19989 n2");
1254 break;
1255 case TDA19988:
1256 dev_info(&client->dev, "found TDA19988");
1257 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001258 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001259 dev_err(&client->dev, "found unsupported device: %04x\n",
1260 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001261 goto fail;
1262 }
1263
1264 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001265 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001266
1267 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001268 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001269
1270 /* if necessary, disable multi-master: */
1271 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001272 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001273
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001274 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001275 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1276
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001277 /* initialize the optional IRQ */
1278 if (client->irq) {
1279 int irqf_trigger;
1280
Jean-Francois Moine6833d262014-11-29 08:57:15 +01001281 /* init read EDID waitqueue and HDP work */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001282 init_waitqueue_head(&priv->wq_edid);
1283
1284 /* clear pending interrupts */
1285 reg_read(priv, REG_INT_FLAGS_0);
1286 reg_read(priv, REG_INT_FLAGS_1);
1287 reg_read(priv, REG_INT_FLAGS_2);
1288
1289 irqf_trigger =
1290 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1291 ret = request_threaded_irq(client->irq, NULL,
1292 tda998x_irq_thread,
1293 irqf_trigger | IRQF_ONESHOT,
1294 "tda998x", priv);
1295 if (ret) {
1296 dev_err(&client->dev,
1297 "failed to request IRQ#%u: %d\n",
1298 client->irq, ret);
1299 goto fail;
1300 }
1301
1302 /* enable HPD irq */
1303 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1304 }
1305
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001306 /* enable EDID read irq: */
1307 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1308
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001309 if (!np)
1310 return 0; /* non-DT */
1311
1312 /* get the optional video properties */
1313 ret = of_property_read_u32(np, "video-ports", &video);
1314 if (ret == 0) {
1315 priv->vip_cntrl_0 = video >> 16;
1316 priv->vip_cntrl_1 = video >> 8;
1317 priv->vip_cntrl_2 = video;
1318 }
1319
Rob Clarke7792ce2013-01-08 19:21:02 -06001320 return 0;
1321
1322fail:
1323 /* if encoder_init fails, the encoder slave is never registered,
1324 * so cleanup here:
1325 */
1326 if (priv->cec)
1327 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001328 return -ENXIO;
1329}
1330
Russell Kingc707c362014-02-07 19:49:44 +00001331struct tda998x_priv2 {
1332 struct tda998x_priv base;
1333 struct drm_encoder encoder;
1334 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) \
1341 container_of(x, struct tda998x_priv2, encoder);
1342
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
1411 return &priv->encoder;
1412}
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->base.encoder = &priv->encoder;
1467 priv->connector.interlace_allowed = 1;
Russell King5dbcf312014-06-15 11:11:10 +01001468 priv->encoder.possible_crtcs = crtcs;
Russell Kingc707c362014-02-07 19:49:44 +00001469
1470 ret = tda998x_create(client, &priv->base);
1471 if (ret)
1472 return ret;
1473
1474 if (!dev->of_node && params)
1475 tda998x_encoder_set_config(&priv->base, params);
1476
1477 tda998x_encoder_set_polling(&priv->base, &priv->connector);
1478
1479 drm_encoder_helper_add(&priv->encoder, &tda998x_encoder_helper_funcs);
1480 ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
1481 DRM_MODE_ENCODER_TMDS);
1482 if (ret)
1483 goto err_encoder;
1484
1485 drm_connector_helper_add(&priv->connector,
1486 &tda998x_connector_helper_funcs);
1487 ret = drm_connector_init(drm, &priv->connector,
1488 &tda998x_connector_funcs,
1489 DRM_MODE_CONNECTOR_HDMIA);
1490 if (ret)
1491 goto err_connector;
1492
Dave Airlie74cd62e2014-08-05 10:34:33 +10001493 ret = drm_connector_register(&priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001494 if (ret)
1495 goto err_sysfs;
1496
1497 priv->connector.encoder = &priv->encoder;
1498 drm_mode_connector_attach_encoder(&priv->connector, &priv->encoder);
1499
1500 return 0;
1501
1502err_sysfs:
1503 drm_connector_cleanup(&priv->connector);
1504err_connector:
1505 drm_encoder_cleanup(&priv->encoder);
1506err_encoder:
1507 tda998x_destroy(&priv->base);
1508 return ret;
1509}
1510
1511static void tda998x_unbind(struct device *dev, struct device *master,
1512 void *data)
1513{
1514 struct tda998x_priv2 *priv = dev_get_drvdata(dev);
1515
1516 drm_connector_cleanup(&priv->connector);
1517 drm_encoder_cleanup(&priv->encoder);
1518 tda998x_destroy(&priv->base);
1519}
1520
1521static const struct component_ops tda998x_ops = {
1522 .bind = tda998x_bind,
1523 .unbind = tda998x_unbind,
1524};
1525
1526static int
1527tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1528{
1529 return component_add(&client->dev, &tda998x_ops);
1530}
1531
1532static int tda998x_remove(struct i2c_client *client)
1533{
1534 component_del(&client->dev, &tda998x_ops);
1535 return 0;
1536}
1537
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001538#ifdef CONFIG_OF
1539static const struct of_device_id tda998x_dt_ids[] = {
1540 { .compatible = "nxp,tda998x", },
1541 { }
1542};
1543MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1544#endif
1545
Rob Clarke7792ce2013-01-08 19:21:02 -06001546static struct i2c_device_id tda998x_ids[] = {
1547 { "tda998x", 0 },
1548 { }
1549};
1550MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1551
Russell King3d58e312015-08-14 11:13:50 +01001552static struct i2c_driver tda998x_driver = {
1553 .probe = tda998x_probe,
1554 .remove = tda998x_remove,
1555 .driver = {
1556 .name = "tda998x",
1557 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001558 },
Russell King3d58e312015-08-14 11:13:50 +01001559 .id_table = tda998x_ids,
Rob Clarke7792ce2013-01-08 19:21:02 -06001560};
1561
Russell King3d58e312015-08-14 11:13:50 +01001562module_i2c_driver(tda998x_driver);
Rob Clarke7792ce2013-01-08 19:21:02 -06001563
1564MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1565MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1566MODULE_LICENSE("GPL");