blob: a2a463cec2448b5780afc3fc8ee3fb217eeafd37 [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;
Russell Kingeed64b52015-08-14 11:18:28 +010054 struct drm_connector connector;
Rob Clarke7792ce2013-01-08 19:21:02 -060055};
56
Rob Clarke7792ce2013-01-08 19:21:02 -060057/* The TDA9988 series of devices use a paged register scheme.. to simplify
58 * things we encode the page # in upper bits of the register #. To read/
59 * write a given register, we need to make sure CURPAGE register is set
60 * appropriately. Which implies reads/writes are not atomic. Fun!
61 */
62
63#define REG(page, addr) (((page) << 8) | (addr))
64#define REG2ADDR(reg) ((reg) & 0xff)
65#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
66
67#define REG_CURPAGE 0xff /* write */
68
69
70/* Page 00h: General Control */
71#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
72#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
73# define MAIN_CNTRL0_SR (1 << 0)
74# define MAIN_CNTRL0_DECS (1 << 1)
75# define MAIN_CNTRL0_DEHS (1 << 2)
76# define MAIN_CNTRL0_CECS (1 << 3)
77# define MAIN_CNTRL0_CEHS (1 << 4)
78# define MAIN_CNTRL0_SCALER (1 << 7)
79#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
80#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
81# define SOFTRESET_AUDIO (1 << 0)
82# define SOFTRESET_I2C_MASTER (1 << 1)
83#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
84#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
85#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
86# define I2C_MASTER_DIS_MM (1 << 0)
87# define I2C_MASTER_DIS_FILT (1 << 1)
88# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020089#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
90# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060091#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
92#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
93#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
94# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +020095#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -060096#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
97#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
98#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
99#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
100#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
101# define VIP_CNTRL_0_MIRR_A (1 << 7)
102# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
103# define VIP_CNTRL_0_MIRR_B (1 << 3)
104# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
105#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
106# define VIP_CNTRL_1_MIRR_C (1 << 7)
107# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
108# define VIP_CNTRL_1_MIRR_D (1 << 3)
109# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
110#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
111# define VIP_CNTRL_2_MIRR_E (1 << 7)
112# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
113# define VIP_CNTRL_2_MIRR_F (1 << 3)
114# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
115#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
116# define VIP_CNTRL_3_X_TGL (1 << 0)
117# define VIP_CNTRL_3_H_TGL (1 << 1)
118# define VIP_CNTRL_3_V_TGL (1 << 2)
119# define VIP_CNTRL_3_EMB (1 << 3)
120# define VIP_CNTRL_3_SYNC_DE (1 << 4)
121# define VIP_CNTRL_3_SYNC_HS (1 << 5)
122# define VIP_CNTRL_3_DE_INT (1 << 6)
123# define VIP_CNTRL_3_EDGE (1 << 7)
124#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
125# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
126# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
127# define VIP_CNTRL_4_CCIR656 (1 << 4)
128# define VIP_CNTRL_4_656_ALT (1 << 5)
129# define VIP_CNTRL_4_TST_656 (1 << 6)
130# define VIP_CNTRL_4_TST_PAT (1 << 7)
131#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
132# define VIP_CNTRL_5_CKCASE (1 << 0)
133# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200134#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100135# define MUX_AP_SELECT_I2S 0x64
136# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb24812013-08-14 21:43:27 +0200137#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600138#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
139# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
140# define MAT_CONTRL_MAT_BP (1 << 2)
141#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
142#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
143#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
144#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
145#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
146#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
147#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
148#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
149#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
150#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
151#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
152#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
153#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
154#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
155#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
156#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
157#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200158#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
159#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600160#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
161#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200162#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
163#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600164#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
165#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
166#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
167#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
168#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
169#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
170#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
171#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
172#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
173#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200174#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
175#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
176#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
177#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600178#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
179#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
180#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
181#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
182#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200183# define TBG_CNTRL_0_TOP_TGL (1 << 0)
184# define TBG_CNTRL_0_TOP_SEL (1 << 1)
185# define TBG_CNTRL_0_DE_EXT (1 << 2)
186# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600187# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
188# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
189# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
190#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200191# define TBG_CNTRL_1_H_TGL (1 << 0)
192# define TBG_CNTRL_1_V_TGL (1 << 1)
193# define TBG_CNTRL_1_TGL_EN (1 << 2)
194# define TBG_CNTRL_1_X_EXT (1 << 3)
195# define TBG_CNTRL_1_H_EXT (1 << 4)
196# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600197# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
198#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
199#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
200# define HVF_CNTRL_0_SM (1 << 7)
201# define HVF_CNTRL_0_RWB (1 << 6)
202# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
203# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
204#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
205# define HVF_CNTRL_1_FOR (1 << 0)
206# define HVF_CNTRL_1_YUVBLK (1 << 1)
207# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
208# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
209# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
210#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200211#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
212# define I2S_FORMAT(x) (((x) & 3) << 0)
213#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100214# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
215# define AIP_CLKSEL_AIP_I2S (1 << 3)
216# define AIP_CLKSEL_FS_ACLK (0 << 0)
217# define AIP_CLKSEL_FS_MCLK (1 << 0)
218# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600219
220/* Page 02h: PLL settings */
221#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
222# define PLL_SERIAL_1_SRL_FDN (1 << 0)
223# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
224# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
225#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100226# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600227# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
228#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
229# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
230# define PLL_SERIAL_3_SRL_DE (1 << 2)
231# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
232#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
233#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
234#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
235#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
236#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
237#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
238#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
239#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
240#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200241# define AUDIO_DIV_SERCLK_1 0
242# define AUDIO_DIV_SERCLK_2 1
243# define AUDIO_DIV_SERCLK_4 2
244# define AUDIO_DIV_SERCLK_8 3
245# define AUDIO_DIV_SERCLK_16 4
246# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600247#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
248# define SEL_CLK_SEL_CLK1 (1 << 0)
249# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
250# define SEL_CLK_ENA_SC_CLK (1 << 3)
251#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
252
253
254/* Page 09h: EDID Control */
255#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
256/* next 127 successive registers are the EDID block */
257#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
258#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
259#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
260#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
261#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
262
263
264/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200265#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
266#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
267#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
268#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
269#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600270
271
272/* Page 11h: audio settings and content info packets */
273#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
274# define AIP_CNTRL_0_RST_FIFO (1 << 0)
275# define AIP_CNTRL_0_SWAP (1 << 1)
276# define AIP_CNTRL_0_LAYOUT (1 << 2)
277# define AIP_CNTRL_0_ACR_MAN (1 << 5)
278# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200279#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
280# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
281# define CA_I2S_HBR_CHSTAT (1 << 6)
282#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
283#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
284#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
285#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
286#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
287#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
288#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
289#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
290# define CTS_N_K(x) (((x) & 7) << 0)
291# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600292#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
293# define ENC_CNTRL_RST_ENC (1 << 0)
294# define ENC_CNTRL_RST_SEL (1 << 1)
295# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200296#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
297# define DIP_FLAGS_ACR (1 << 0)
298# define DIP_FLAGS_GC (1 << 1)
299#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
300# define DIP_IF_FLAGS_IF1 (1 << 1)
301# define DIP_IF_FLAGS_IF2 (1 << 2)
302# define DIP_IF_FLAGS_IF3 (1 << 3)
303# define DIP_IF_FLAGS_IF4 (1 << 4)
304# define DIP_IF_FLAGS_IF5 (1 << 5)
305#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600306
307
308/* Page 12h: HDCP and OTP */
309#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200310#define REG_TX4 REG(0x12, 0x9b) /* read/write */
311# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600312#define REG_TX33 REG(0x12, 0xb8) /* read/write */
313# define TX33_HDMI (1 << 1)
314
315
316/* Page 13h: Gamut related metadata packets */
317
318
319
320/* CEC registers: (not paged)
321 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100322#define REG_CEC_INTSTATUS 0xee /* read */
323# define CEC_INTSTATUS_CEC (1 << 0)
324# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600325#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
326# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
327# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
328# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
329# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100330#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
331#define REG_CEC_RXSHPDINT 0xfd /* read */
Russell Kingec5d3e82015-06-06 21:41:10 +0100332# define CEC_RXSHPDINT_RXSENS BIT(0)
333# define CEC_RXSHPDINT_HPD BIT(1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600334#define REG_CEC_RXSHPDLEV 0xfe /* read */
335# define CEC_RXSHPDLEV_RXSENS (1 << 0)
336# define CEC_RXSHPDLEV_HPD (1 << 1)
337
338#define REG_CEC_ENAMODS 0xff /* read/write */
339# define CEC_ENAMODS_DIS_FRO (1 << 6)
340# define CEC_ENAMODS_DIS_CCLK (1 << 5)
341# define CEC_ENAMODS_EN_RXSENS (1 << 2)
342# define CEC_ENAMODS_EN_HDMI (1 << 1)
343# define CEC_ENAMODS_EN_CEC (1 << 0)
344
345
346/* Device versions: */
347#define TDA9989N2 0x0101
348#define TDA19989 0x0201
349#define TDA19989N2 0x0202
350#define TDA19988 0x0301
351
352static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100353cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600354{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100355 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100356 u8 buf[] = {addr, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600357 int ret;
358
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100359 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600360 if (ret < 0)
361 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
362}
363
Russell Kinge66e03a2015-06-06 21:41:10 +0100364static u8
365cec_read(struct tda998x_priv *priv, u8 addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600366{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100367 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100368 u8 val;
Rob Clarke7792ce2013-01-08 19:21:02 -0600369 int ret;
370
371 ret = i2c_master_send(client, &addr, sizeof(addr));
372 if (ret < 0)
373 goto fail;
374
375 ret = i2c_master_recv(client, &val, sizeof(val));
376 if (ret < 0)
377 goto fail;
378
379 return val;
380
381fail:
382 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
383 return 0;
384}
385
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100386static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100387set_page(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600388{
Rob Clarke7792ce2013-01-08 19:21:02 -0600389 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100390 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100391 u8 buf[] = {
Rob Clarke7792ce2013-01-08 19:21:02 -0600392 REG_CURPAGE, REG2PAGE(reg)
393 };
394 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100395 if (ret < 0) {
Julia Lawall288ffc72014-12-07 20:20:59 +0100396 dev_err(&client->dev, "%s %04x err %d\n", __func__,
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100397 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100398 return ret;
399 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600400
401 priv->current_page = REG2PAGE(reg);
402 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100403 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600404}
405
406static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100407reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600408{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100409 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100410 u8 addr = REG2ADDR(reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600411 int ret;
412
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100413 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100414 ret = set_page(priv, reg);
415 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100416 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600417
418 ret = i2c_master_send(client, &addr, sizeof(addr));
419 if (ret < 0)
420 goto fail;
421
422 ret = i2c_master_recv(client, buf, cnt);
423 if (ret < 0)
424 goto fail;
425
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100426 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600427
428fail:
429 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100430out:
431 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600432 return ret;
433}
434
Russell Kingc4c11dd2013-08-14 21:43:30 +0200435static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100436reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200437{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100438 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100439 u8 buf[cnt+1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200440 int ret;
441
442 buf[0] = REG2ADDR(reg);
443 memcpy(&buf[1], p, cnt);
444
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100445 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100446 ret = set_page(priv, reg);
447 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100448 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200449
450 ret = i2c_master_send(client, buf, cnt + 1);
451 if (ret < 0)
452 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100453out:
454 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200455}
456
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100457static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100458reg_read(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600459{
Russell Kinge66e03a2015-06-06 21:41:10 +0100460 u8 val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100461 int ret;
462
463 ret = reg_read_range(priv, reg, &val, sizeof(val));
464 if (ret < 0)
465 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600466 return val;
467}
468
469static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100470reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600471{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100472 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100473 u8 buf[] = {REG2ADDR(reg), val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600474 int ret;
475
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100476 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100477 ret = set_page(priv, reg);
478 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100479 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600480
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100481 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600482 if (ret < 0)
483 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100484out:
485 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600486}
487
488static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100489reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600490{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100491 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100492 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600493 int ret;
494
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100495 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100496 ret = set_page(priv, reg);
497 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100498 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600499
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100500 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600501 if (ret < 0)
502 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100503out:
504 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600505}
506
507static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100508reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600509{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100510 int old_val;
511
512 old_val = reg_read(priv, reg);
513 if (old_val >= 0)
514 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600515}
516
517static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100518reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600519{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100520 int old_val;
521
522 old_val = reg_read(priv, reg);
523 if (old_val >= 0)
524 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600525}
526
527static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100528tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600529{
530 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100531 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600532 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100533 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600534 msleep(50);
535
536 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100537 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
538 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600539
540 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100541 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
542 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
543 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
544 reg_write(priv, REG_SERIALIZER, 0x00);
545 reg_write(priv, REG_BUFFER_OUT, 0x00);
546 reg_write(priv, REG_PLL_SCG1, 0x00);
547 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
548 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
549 reg_write(priv, REG_PLL_SCGN1, 0xfa);
550 reg_write(priv, REG_PLL_SCGN2, 0x00);
551 reg_write(priv, REG_PLL_SCGR1, 0x5b);
552 reg_write(priv, REG_PLL_SCGR2, 0x00);
553 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200554
555 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100556 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600557}
558
Russell King0fc6f442015-06-06 21:41:09 +0100559/*
560 * The TDA998x has a problem when trying to read the EDID close to a
561 * HPD assertion: it needs a delay of 100ms to avoid timing out while
562 * trying to read EDID data.
563 *
564 * However, tda998x_encoder_get_modes() may be called at any moment
565 * after tda998x_encoder_detect() indicates that we are connected, so
566 * we need to delay probing modes in tda998x_encoder_get_modes() after
567 * we have seen a HPD inactive->active transition. This code implements
568 * that delay.
569 */
570static void tda998x_edid_delay_done(unsigned long data)
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100571{
Russell King0fc6f442015-06-06 21:41:09 +0100572 struct tda998x_priv *priv = (struct tda998x_priv *)data;
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100573
Russell King0fc6f442015-06-06 21:41:09 +0100574 priv->edid_delay_active = false;
575 wake_up(&priv->edid_delay_waitq);
576 schedule_work(&priv->detect_work);
577}
578
579static void tda998x_edid_delay_start(struct tda998x_priv *priv)
580{
581 priv->edid_delay_active = true;
582 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
583}
584
585static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
586{
587 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
588}
589
590/*
591 * We need to run the KMS hotplug event helper outside of our threaded
592 * interrupt routine as this can call back into our get_modes method,
593 * which will want to make use of interrupts.
594 */
595static void tda998x_detect_work(struct work_struct *work)
596{
597 struct tda998x_priv *priv =
598 container_of(work, struct tda998x_priv, detect_work);
Russell King78e401f2015-08-14 11:17:12 +0100599 struct drm_device *dev = priv->encoder.dev;
Russell King0fc6f442015-06-06 21:41:09 +0100600
601 if (dev)
602 drm_kms_helper_hotplug_event(dev);
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100603}
604
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100605/*
606 * only 2 interrupts may occur: screen plug/unplug and EDID read
607 */
608static irqreturn_t tda998x_irq_thread(int irq, void *data)
609{
610 struct tda998x_priv *priv = data;
611 u8 sta, cec, lvl, flag0, flag1, flag2;
Russell Kingf84a97d2015-06-06 21:41:09 +0100612 bool handled = false;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100613
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100614 sta = cec_read(priv, REG_CEC_INTSTATUS);
615 cec = cec_read(priv, REG_CEC_RXSHPDINT);
616 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
617 flag0 = reg_read(priv, REG_INT_FLAGS_0);
618 flag1 = reg_read(priv, REG_INT_FLAGS_1);
619 flag2 = reg_read(priv, REG_INT_FLAGS_2);
620 DRM_DEBUG_DRIVER(
621 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
622 sta, cec, lvl, flag0, flag1, flag2);
Russell Kingec5d3e82015-06-06 21:41:10 +0100623
624 if (cec & CEC_RXSHPDINT_HPD) {
Russell King0fc6f442015-06-06 21:41:09 +0100625 if (lvl & CEC_RXSHPDLEV_HPD)
626 tda998x_edid_delay_start(priv);
627 else
628 schedule_work(&priv->detect_work);
629
Russell Kingf84a97d2015-06-06 21:41:09 +0100630 handled = true;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100631 }
Russell Kingec5d3e82015-06-06 21:41:10 +0100632
633 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
634 priv->wq_edid_wait = 0;
635 wake_up(&priv->wq_edid);
636 handled = true;
637 }
638
Russell Kingf84a97d2015-06-06 21:41:09 +0100639 return IRQ_RETVAL(handled);
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100640}
641
Russell Kingc4c11dd2013-08-14 21:43:30 +0200642static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100643tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
Russell King96795df2015-08-06 10:52:05 +0100644 union hdmi_infoframe *frame)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200645{
Russell King96795df2015-08-06 10:52:05 +0100646 u8 buf[32];
647 ssize_t len;
648
649 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
650 if (len < 0) {
651 dev_err(&priv->hdmi->dev,
652 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
653 frame->any.type, len);
654 return;
655 }
656
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100657 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
Russell King96795df2015-08-06 10:52:05 +0100658 reg_write_range(priv, addr, buf, len);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100659 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200660}
661
662static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100663tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200664{
Russell King96795df2015-08-06 10:52:05 +0100665 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200666
Russell King96795df2015-08-06 10:52:05 +0100667 hdmi_audio_infoframe_init(&frame.audio);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200668
Russell King96795df2015-08-06 10:52:05 +0100669 frame.audio.channels = p->audio_frame[1] & 0x07;
670 frame.audio.channel_allocation = p->audio_frame[4];
671 frame.audio.level_shift_value = (p->audio_frame[5] & 0x78) >> 3;
672 frame.audio.downmix_inhibit = (p->audio_frame[5] & 0x80) >> 7;
Jean-Francois Moine4a6ca1a2015-07-17 13:07:35 +0200673
Russell King96795df2015-08-06 10:52:05 +0100674 /*
675 * L-PCM and IEC61937 compressed audio shall always set sample
676 * frequency to "refer to stream". For others, see the HDMI
677 * specification.
678 */
679 frame.audio.sample_frequency = (p->audio_frame[2] & 0x1c) >> 2;
680
681 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200682}
683
684static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100685tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200686{
Russell King96795df2015-08-06 10:52:05 +0100687 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200688
Russell King96795df2015-08-06 10:52:05 +0100689 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
690 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200691
Russell King96795df2015-08-06 10:52:05 +0100692 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200693}
694
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100695static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200696{
697 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100698 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
699 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
700 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200701 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100702 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200703 }
704}
705
706static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100707tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200708 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
709{
Russell Kinge66e03a2015-06-06 21:41:10 +0100710 u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
711 u32 n;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200712
713 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100714 reg_write(priv, REG_ENA_AP, p->audio_cfg);
715 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200716
717 /* Set audio input source */
718 switch (p->audio_format) {
719 case AFMT_SPDIF:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100720 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
721 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
722 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200723 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200724 break;
725
726 case AFMT_I2S:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100727 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
728 clksel_aip = AIP_CLKSEL_AIP_I2S;
729 clksel_fs = AIP_CLKSEL_FS_ACLK;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200730 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200731 break;
David Herrmann3b288022013-09-01 15:23:04 +0200732
733 default:
734 BUG();
735 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200736 }
737
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100738 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100739 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
740 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100741 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200742
743 /*
744 * Audio input somehow depends on HDMI line rate which is
745 * related to pixclk. Testing showed that modes with pixclk
746 * >100MHz need a larger divider while <40MHz need the default.
747 * There is no detailed info in the datasheet, so we just
748 * assume 100MHz requires larger divider.
749 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100750 adiv = AUDIO_DIV_SERCLK_8;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200751 if (mode->clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100752 adiv++; /* AUDIO_DIV_SERCLK_16 */
753
754 /* S/PDIF asks for a larger divider */
755 if (p->audio_format == AFMT_SPDIF)
756 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
757
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100758 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200759
760 /*
761 * This is the approximate value of N, which happens to be
762 * the recommended values for non-coherent clocks.
763 */
764 n = 128 * p->audio_sample_rate / 1000;
765
766 /* Write the CTS and N values */
767 buf[0] = 0x44;
768 buf[1] = 0x42;
769 buf[2] = 0x01;
770 buf[3] = n;
771 buf[4] = n >> 8;
772 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100773 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200774
775 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100776 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200777
778 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100779 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
780 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200781
782 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100783 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200784 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100785 buf[2] = IEC958_AES3_CON_FS_NOTID;
786 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
787 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100788 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200789
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100790 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100791 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100792 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200793
794 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100795 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200796}
797
Rob Clarke7792ce2013-01-08 19:21:02 -0600798/* DRM encoder functions */
799
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000800static void tda998x_encoder_set_config(struct tda998x_priv *priv,
801 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600802{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200803 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
804 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
805 VIP_CNTRL_0_SWAP_B(p->swap_b) |
806 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
807 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
808 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
809 VIP_CNTRL_1_SWAP_D(p->swap_d) |
810 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
811 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
812 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
813 VIP_CNTRL_2_SWAP_F(p->swap_f) |
814 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
815
816 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600817}
818
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000819static void tda998x_encoder_dpms(struct tda998x_priv *priv, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600820{
Rob Clarke7792ce2013-01-08 19:21:02 -0600821 /* we only care about on or off: */
822 if (mode != DRM_MODE_DPMS_ON)
823 mode = DRM_MODE_DPMS_OFF;
824
825 if (mode == priv->dpms)
826 return;
827
828 switch (mode) {
829 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200830 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100831 reg_write(priv, REG_ENA_VP_0, 0xff);
832 reg_write(priv, REG_ENA_VP_1, 0xff);
833 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600834 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100835 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
836 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
837 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600838 break;
839 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100840 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100841 reg_write(priv, REG_ENA_VP_0, 0x00);
842 reg_write(priv, REG_ENA_VP_1, 0x00);
843 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600844 break;
845 }
846
847 priv->dpms = mode;
848}
849
850static void
851tda998x_encoder_save(struct drm_encoder *encoder)
852{
853 DBG("");
854}
855
856static void
857tda998x_encoder_restore(struct drm_encoder *encoder)
858{
859 DBG("");
860}
861
862static bool
863tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
864 const struct drm_display_mode *mode,
865 struct drm_display_mode *adjusted_mode)
866{
867 return true;
868}
869
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000870static int tda998x_encoder_mode_valid(struct tda998x_priv *priv,
871 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600872{
Russell King92fbdfc2014-02-07 19:52:33 +0000873 if (mode->clock > 150000)
874 return MODE_CLOCK_HIGH;
875 if (mode->htotal >= BIT(13))
876 return MODE_BAD_HVALUE;
877 if (mode->vtotal >= BIT(11))
878 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600879 return MODE_OK;
880}
881
882static void
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000883tda998x_encoder_mode_set(struct tda998x_priv *priv,
884 struct drm_display_mode *mode,
885 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600886{
Russell Kinge66e03a2015-06-06 21:41:10 +0100887 u16 ref_pix, ref_line, n_pix, n_line;
888 u16 hs_pix_s, hs_pix_e;
889 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
890 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
891 u16 vwin1_line_s, vwin1_line_e;
892 u16 vwin2_line_s, vwin2_line_e;
893 u16 de_pix_s, de_pix_e;
894 u8 reg, div, rep;
Rob Clarke7792ce2013-01-08 19:21:02 -0600895
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200896 /*
897 * Internally TDA998x is using ITU-R BT.656 style sync but
898 * we get VESA style sync. TDA998x is using a reference pixel
899 * relative to ITU to sync to the input frame and for output
900 * sync generation. Currently, we are using reference detection
901 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
902 * which is position of rising VS with coincident rising HS.
903 *
904 * Now there is some issues to take care of:
905 * - HDMI data islands require sync-before-active
906 * - TDA998x register values must be > 0 to be enabled
907 * - REFLINE needs an additional offset of +1
908 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
909 *
910 * So we add +1 to all horizontal and vertical register values,
911 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600912 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200913 n_pix = mode->htotal;
914 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600915
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200916 hs_pix_e = mode->hsync_end - mode->hdisplay;
917 hs_pix_s = mode->hsync_start - mode->hdisplay;
918 de_pix_e = mode->htotal;
919 de_pix_s = mode->htotal - mode->hdisplay;
920 ref_pix = 3 + hs_pix_s;
921
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200922 /*
923 * Attached LCD controllers may generate broken sync. Allow
924 * those to adjust the position of the rising VS edge by adding
925 * HSKEW to ref_pix.
926 */
927 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
928 ref_pix += adjusted_mode->hskew;
929
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200930 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
931 ref_line = 1 + mode->vsync_start - mode->vdisplay;
932 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
933 vwin1_line_e = vwin1_line_s + mode->vdisplay;
934 vs1_pix_s = vs1_pix_e = hs_pix_s;
935 vs1_line_s = mode->vsync_start - mode->vdisplay;
936 vs1_line_e = vs1_line_s +
937 mode->vsync_end - mode->vsync_start;
938 vwin2_line_s = vwin2_line_e = 0;
939 vs2_pix_s = vs2_pix_e = 0;
940 vs2_line_s = vs2_line_e = 0;
941 } else {
942 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
943 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
944 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
945 vs1_pix_s = vs1_pix_e = hs_pix_s;
946 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
947 vs1_line_e = vs1_line_s +
948 (mode->vsync_end - mode->vsync_start)/2;
949 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
950 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
951 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
952 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
953 vs2_line_e = vs2_line_s +
954 (mode->vsync_end - mode->vsync_start)/2;
955 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600956
957 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100958 if (div != 0) {
959 div--;
960 if (div > 3)
961 div = 3;
962 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600963
Rob Clarke7792ce2013-01-08 19:21:02 -0600964 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100965 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600966
967 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100968 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100969 reg_clear(priv, REG_TX33, TX33_HDMI);
970 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600971
Rob Clarke7792ce2013-01-08 19:21:02 -0600972 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100973 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600974 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100975 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
976 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600977 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600978
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100979 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100980 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
981 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100982 reg_write(priv, REG_SERIALIZER, 0);
983 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600984
985 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
986 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100987 reg_write(priv, REG_RPT_CNTRL, 0);
988 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600989 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
990
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100991 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600992 PLL_SERIAL_2_SRL_PR(rep));
993
Rob Clarke7792ce2013-01-08 19:21:02 -0600994 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100995 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
996 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600997
998 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100999 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -06001000
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001001 /*
1002 * Sync on rising HSYNC/VSYNC
1003 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001004 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001005
1006 /*
1007 * TDA19988 requires high-active sync at input stage,
1008 * so invert low-active sync provided by master encoder here
1009 */
1010 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001011 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -06001012 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001013 reg |= VIP_CNTRL_3_V_TGL;
1014 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -06001015
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001016 reg_write(priv, REG_VIDFORMAT, 0x00);
1017 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1018 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1019 reg_write16(priv, REG_NPIX_MSB, n_pix);
1020 reg_write16(priv, REG_NLINE_MSB, n_line);
1021 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1022 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1023 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1024 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1025 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1026 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1027 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1028 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1029 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1030 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1031 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1032 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1033 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1034 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1035 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1036 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -06001037
1038 if (priv->rev == TDA19988) {
1039 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001040 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001041 }
1042
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001043 /*
1044 * Always generate sync polarity relative to input sync and
1045 * revert input stage toggled sync at output stage
1046 */
1047 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1048 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1049 reg |= TBG_CNTRL_1_H_TGL;
1050 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1051 reg |= TBG_CNTRL_1_V_TGL;
1052 reg_write(priv, REG_TBG_CNTRL_1, reg);
1053
Rob Clarke7792ce2013-01-08 19:21:02 -06001054 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001055 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001056
1057 /* Only setup the info frames if the sink is HDMI */
1058 if (priv->is_hdmi_sink) {
1059 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001060 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1061 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001062 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1063 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001064
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001065 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001066
1067 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001068 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001069 &priv->params);
1070 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001071}
1072
1073static enum drm_connector_status
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001074tda998x_encoder_detect(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001075{
Russell Kinge66e03a2015-06-06 21:41:10 +01001076 u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001077
Rob Clarke7792ce2013-01-08 19:21:02 -06001078 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1079 connector_status_disconnected;
1080}
1081
Laurent Pinchart07259f82015-01-16 18:37:43 +02001082static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
Rob Clarke7792ce2013-01-08 19:21:02 -06001083{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001084 struct tda998x_priv *priv = data;
Russell Kinge66e03a2015-06-06 21:41:10 +01001085 u8 offset, segptr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001086 int ret, i;
1087
Rob Clarke7792ce2013-01-08 19:21:02 -06001088 offset = (blk & 1) ? 128 : 0;
1089 segptr = blk / 2;
1090
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001091 reg_write(priv, REG_DDC_ADDR, 0xa0);
1092 reg_write(priv, REG_DDC_OFFS, offset);
1093 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1094 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001095
1096 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001097 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001098 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001099
1100 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001101 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001102
1103 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001104 if (priv->hdmi->irq) {
1105 i = wait_event_timeout(priv->wq_edid,
1106 !priv->wq_edid_wait,
1107 msecs_to_jiffies(100));
1108 if (i < 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001109 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001110 return i;
1111 }
1112 } else {
Russell King713456d2014-03-03 14:09:36 +00001113 for (i = 100; i > 0; i--) {
1114 msleep(1);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001115 ret = reg_read(priv, REG_INT_FLAGS_2);
1116 if (ret < 0)
1117 return ret;
1118 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1119 break;
1120 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001121 }
1122
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001123 if (i == 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001124 dev_err(&priv->hdmi->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001125 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001126 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001127
Laurent Pinchart07259f82015-01-16 18:37:43 +02001128 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1129 if (ret != length) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001130 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1131 blk, ret);
Rob Clarke7792ce2013-01-08 19:21:02 -06001132 return ret;
1133 }
1134
Rob Clarke7792ce2013-01-08 19:21:02 -06001135 return 0;
1136}
1137
Rob Clarke7792ce2013-01-08 19:21:02 -06001138static int
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001139tda998x_encoder_get_modes(struct tda998x_priv *priv,
1140 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001141{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001142 struct edid *edid;
1143 int n;
Rob Clarke7792ce2013-01-08 19:21:02 -06001144
Russell King0fc6f442015-06-06 21:41:09 +01001145 /*
1146 * If we get killed while waiting for the HPD timeout, return
1147 * no modes found: we are not in a restartable path, so we
1148 * can't handle signals gracefully.
1149 */
1150 if (tda998x_edid_delay_wait(priv))
1151 return 0;
1152
Laurent Pinchart07259f82015-01-16 18:37:43 +02001153 if (priv->rev == TDA19988)
1154 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1155
1156 edid = drm_do_get_edid(connector, read_edid_block, priv);
1157
1158 if (priv->rev == TDA19988)
1159 reg_set(priv, REG_TX4, TX4_PD_RAM);
1160
1161 if (!edid) {
1162 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1163 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001164 }
1165
Laurent Pinchart07259f82015-01-16 18:37:43 +02001166 drm_mode_connector_update_edid_property(connector, edid);
1167 n = drm_add_edid_modes(connector, edid);
1168 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
1169 kfree(edid);
1170
Rob Clarke7792ce2013-01-08 19:21:02 -06001171 return n;
1172}
1173
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001174static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1175 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001176{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001177 if (priv->hdmi->irq)
1178 connector->polled = DRM_CONNECTOR_POLL_HPD;
1179 else
1180 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1181 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001182}
1183
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001184static void tda998x_destroy(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001185{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001186 /* disable all IRQs and free the IRQ handler */
1187 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1188 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
Russell King0fc6f442015-06-06 21:41:09 +01001189
1190 if (priv->hdmi->irq)
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001191 free_irq(priv->hdmi->irq, priv);
Russell King0fc6f442015-06-06 21:41:09 +01001192
1193 del_timer_sync(&priv->edid_delay_timer);
1194 cancel_work_sync(&priv->detect_work);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001195
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001196 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001197}
1198
Rob Clarke7792ce2013-01-08 19:21:02 -06001199/* I2C driver functions */
1200
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001201static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001202{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001203 struct device_node *np = client->dev.of_node;
1204 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001205 int rev_lo, rev_hi, ret;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001206 unsigned short cec_addr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001207
Russell King5e74c222013-08-14 21:43:29 +02001208 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1209 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1210 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1211
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001212 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001213 priv->hdmi = client;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001214 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1215 cec_addr = 0x34 + (client->addr & 0x03);
1216 priv->cec = i2c_new_dummy(client->adapter, cec_addr);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001217 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001218 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001219
Rob Clarke7792ce2013-01-08 19:21:02 -06001220 priv->dpms = DRM_MODE_DPMS_OFF;
1221
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001222 mutex_init(&priv->mutex); /* protect the page access */
Russell King0fc6f442015-06-06 21:41:09 +01001223 init_waitqueue_head(&priv->edid_delay_waitq);
1224 setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
1225 (unsigned long)priv);
1226 INIT_WORK(&priv->detect_work, tda998x_detect_work);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001227
Rob Clarke7792ce2013-01-08 19:21:02 -06001228 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001229 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001230 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1231
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001232 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001233
1234 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001235 rev_lo = reg_read(priv, REG_VERSION_LSB);
1236 rev_hi = reg_read(priv, REG_VERSION_MSB);
1237 if (rev_lo < 0 || rev_hi < 0) {
1238 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001239 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001240 }
1241
1242 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001243
1244 /* mask off feature bits: */
1245 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1246
1247 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001248 case TDA9989N2:
1249 dev_info(&client->dev, "found TDA9989 n2");
1250 break;
1251 case TDA19989:
1252 dev_info(&client->dev, "found TDA19989");
1253 break;
1254 case TDA19989N2:
1255 dev_info(&client->dev, "found TDA19989 n2");
1256 break;
1257 case TDA19988:
1258 dev_info(&client->dev, "found TDA19988");
1259 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001260 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001261 dev_err(&client->dev, "found unsupported device: %04x\n",
1262 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001263 goto fail;
1264 }
1265
1266 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001267 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001268
1269 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001270 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001271
1272 /* if necessary, disable multi-master: */
1273 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001274 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001275
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001276 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001277 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1278
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001279 /* initialize the optional IRQ */
1280 if (client->irq) {
1281 int irqf_trigger;
1282
Jean-Francois Moine6833d262014-11-29 08:57:15 +01001283 /* init read EDID waitqueue and HDP work */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001284 init_waitqueue_head(&priv->wq_edid);
1285
1286 /* clear pending interrupts */
1287 reg_read(priv, REG_INT_FLAGS_0);
1288 reg_read(priv, REG_INT_FLAGS_1);
1289 reg_read(priv, REG_INT_FLAGS_2);
1290
1291 irqf_trigger =
1292 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1293 ret = request_threaded_irq(client->irq, NULL,
1294 tda998x_irq_thread,
1295 irqf_trigger | IRQF_ONESHOT,
1296 "tda998x", priv);
1297 if (ret) {
1298 dev_err(&client->dev,
1299 "failed to request IRQ#%u: %d\n",
1300 client->irq, ret);
1301 goto fail;
1302 }
1303
1304 /* enable HPD irq */
1305 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1306 }
1307
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001308 /* enable EDID read irq: */
1309 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1310
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001311 if (!np)
1312 return 0; /* non-DT */
1313
1314 /* get the optional video properties */
1315 ret = of_property_read_u32(np, "video-ports", &video);
1316 if (ret == 0) {
1317 priv->vip_cntrl_0 = video >> 16;
1318 priv->vip_cntrl_1 = video >> 8;
1319 priv->vip_cntrl_2 = video;
1320 }
1321
Rob Clarke7792ce2013-01-08 19:21:02 -06001322 return 0;
1323
1324fail:
1325 /* if encoder_init fails, the encoder slave is never registered,
1326 * so cleanup here:
1327 */
1328 if (priv->cec)
1329 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001330 return -ENXIO;
1331}
1332
Russell Kingc707c362014-02-07 19:49:44 +00001333struct tda998x_priv2 {
1334 struct tda998x_priv base;
Russell Kingc707c362014-02-07 19:49:44 +00001335};
1336
1337#define conn_to_tda998x_priv2(x) \
Russell Kingeed64b52015-08-14 11:18:28 +01001338 container_of(x, struct tda998x_priv2, base.connector);
Russell Kingc707c362014-02-07 19:49:44 +00001339
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 Kingeed64b52015-08-14 11:18:28 +01001466 priv->base.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
Russell Kingeed64b52015-08-14 11:18:28 +01001476 tda998x_encoder_set_polling(&priv->base, &priv->base.connector);
Russell Kingc707c362014-02-07 19:49:44 +00001477
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
Russell Kingeed64b52015-08-14 11:18:28 +01001484 drm_connector_helper_add(&priv->base.connector,
Russell Kingc707c362014-02-07 19:49:44 +00001485 &tda998x_connector_helper_funcs);
Russell Kingeed64b52015-08-14 11:18:28 +01001486 ret = drm_connector_init(drm, &priv->base.connector,
Russell Kingc707c362014-02-07 19:49:44 +00001487 &tda998x_connector_funcs,
1488 DRM_MODE_CONNECTOR_HDMIA);
1489 if (ret)
1490 goto err_connector;
1491
Russell Kingeed64b52015-08-14 11:18:28 +01001492 ret = drm_connector_register(&priv->base.connector);
Russell Kingc707c362014-02-07 19:49:44 +00001493 if (ret)
1494 goto err_sysfs;
1495
Russell Kingeed64b52015-08-14 11:18:28 +01001496 priv->base.connector.encoder = &priv->base.encoder;
1497 drm_mode_connector_attach_encoder(&priv->base.connector, &priv->base.encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001498
1499 return 0;
1500
1501err_sysfs:
Russell Kingeed64b52015-08-14 11:18:28 +01001502 drm_connector_cleanup(&priv->base.connector);
Russell Kingc707c362014-02-07 19:49:44 +00001503err_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
Russell Kingeed64b52015-08-14 11:18:28 +01001515 drm_connector_cleanup(&priv->base.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");