blob: a46248f0c9c30d9a97787f4e94bb739d330f46b6 [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
Russell King9525c4d2015-08-14 11:28:53 +010057#define conn_to_tda998x_priv(x) \
58 container_of(x, struct tda998x_priv, connector)
59
60#define enc_to_tda998x_priv(x) \
61 container_of(x, struct tda998x_priv, encoder)
62
Rob Clarke7792ce2013-01-08 19:21:02 -060063/* The TDA9988 series of devices use a paged register scheme.. to simplify
64 * things we encode the page # in upper bits of the register #. To read/
65 * write a given register, we need to make sure CURPAGE register is set
66 * appropriately. Which implies reads/writes are not atomic. Fun!
67 */
68
69#define REG(page, addr) (((page) << 8) | (addr))
70#define REG2ADDR(reg) ((reg) & 0xff)
71#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
72
73#define REG_CURPAGE 0xff /* write */
74
75
76/* Page 00h: General Control */
77#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
78#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
79# define MAIN_CNTRL0_SR (1 << 0)
80# define MAIN_CNTRL0_DECS (1 << 1)
81# define MAIN_CNTRL0_DEHS (1 << 2)
82# define MAIN_CNTRL0_CECS (1 << 3)
83# define MAIN_CNTRL0_CEHS (1 << 4)
84# define MAIN_CNTRL0_SCALER (1 << 7)
85#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
86#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
87# define SOFTRESET_AUDIO (1 << 0)
88# define SOFTRESET_I2C_MASTER (1 << 1)
89#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
90#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
91#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
92# define I2C_MASTER_DIS_MM (1 << 0)
93# define I2C_MASTER_DIS_FILT (1 << 1)
94# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020095#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
96# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060097#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
98#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
99#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
100# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200101#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600102#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
103#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
104#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
105#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
106#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
107# define VIP_CNTRL_0_MIRR_A (1 << 7)
108# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
109# define VIP_CNTRL_0_MIRR_B (1 << 3)
110# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
111#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
112# define VIP_CNTRL_1_MIRR_C (1 << 7)
113# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
114# define VIP_CNTRL_1_MIRR_D (1 << 3)
115# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
116#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
117# define VIP_CNTRL_2_MIRR_E (1 << 7)
118# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
119# define VIP_CNTRL_2_MIRR_F (1 << 3)
120# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
121#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
122# define VIP_CNTRL_3_X_TGL (1 << 0)
123# define VIP_CNTRL_3_H_TGL (1 << 1)
124# define VIP_CNTRL_3_V_TGL (1 << 2)
125# define VIP_CNTRL_3_EMB (1 << 3)
126# define VIP_CNTRL_3_SYNC_DE (1 << 4)
127# define VIP_CNTRL_3_SYNC_HS (1 << 5)
128# define VIP_CNTRL_3_DE_INT (1 << 6)
129# define VIP_CNTRL_3_EDGE (1 << 7)
130#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
131# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
132# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
133# define VIP_CNTRL_4_CCIR656 (1 << 4)
134# define VIP_CNTRL_4_656_ALT (1 << 5)
135# define VIP_CNTRL_4_TST_656 (1 << 6)
136# define VIP_CNTRL_4_TST_PAT (1 << 7)
137#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
138# define VIP_CNTRL_5_CKCASE (1 << 0)
139# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200140#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100141# define MUX_AP_SELECT_I2S 0x64
142# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb24812013-08-14 21:43:27 +0200143#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600144#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
145# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
146# define MAT_CONTRL_MAT_BP (1 << 2)
147#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
148#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
149#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
150#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
151#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
152#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
153#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
154#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
155#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
156#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
157#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
158#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
159#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
160#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
161#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
162#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
163#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200164#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
165#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600166#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
167#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200168#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
169#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600170#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
171#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
172#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
173#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
174#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
175#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
176#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
177#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
178#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
179#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200180#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
181#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
182#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
183#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600184#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
185#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
186#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
187#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
188#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200189# define TBG_CNTRL_0_TOP_TGL (1 << 0)
190# define TBG_CNTRL_0_TOP_SEL (1 << 1)
191# define TBG_CNTRL_0_DE_EXT (1 << 2)
192# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600193# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
194# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
195# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
196#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200197# define TBG_CNTRL_1_H_TGL (1 << 0)
198# define TBG_CNTRL_1_V_TGL (1 << 1)
199# define TBG_CNTRL_1_TGL_EN (1 << 2)
200# define TBG_CNTRL_1_X_EXT (1 << 3)
201# define TBG_CNTRL_1_H_EXT (1 << 4)
202# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600203# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
204#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
205#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
206# define HVF_CNTRL_0_SM (1 << 7)
207# define HVF_CNTRL_0_RWB (1 << 6)
208# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
209# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
210#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
211# define HVF_CNTRL_1_FOR (1 << 0)
212# define HVF_CNTRL_1_YUVBLK (1 << 1)
213# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
214# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
215# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
216#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200217#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
218# define I2S_FORMAT(x) (((x) & 3) << 0)
219#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100220# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
221# define AIP_CLKSEL_AIP_I2S (1 << 3)
222# define AIP_CLKSEL_FS_ACLK (0 << 0)
223# define AIP_CLKSEL_FS_MCLK (1 << 0)
224# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600225
226/* Page 02h: PLL settings */
227#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
228# define PLL_SERIAL_1_SRL_FDN (1 << 0)
229# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
230# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
231#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100232# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600233# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
234#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
235# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
236# define PLL_SERIAL_3_SRL_DE (1 << 2)
237# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
238#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
239#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
240#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
241#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
242#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
243#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
244#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
245#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
246#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200247# define AUDIO_DIV_SERCLK_1 0
248# define AUDIO_DIV_SERCLK_2 1
249# define AUDIO_DIV_SERCLK_4 2
250# define AUDIO_DIV_SERCLK_8 3
251# define AUDIO_DIV_SERCLK_16 4
252# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600253#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
254# define SEL_CLK_SEL_CLK1 (1 << 0)
255# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
256# define SEL_CLK_ENA_SC_CLK (1 << 3)
257#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
258
259
260/* Page 09h: EDID Control */
261#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
262/* next 127 successive registers are the EDID block */
263#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
264#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
265#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
266#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
267#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
268
269
270/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200271#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
272#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
273#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
274#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
275#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600276
277
278/* Page 11h: audio settings and content info packets */
279#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
280# define AIP_CNTRL_0_RST_FIFO (1 << 0)
281# define AIP_CNTRL_0_SWAP (1 << 1)
282# define AIP_CNTRL_0_LAYOUT (1 << 2)
283# define AIP_CNTRL_0_ACR_MAN (1 << 5)
284# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200285#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
286# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
287# define CA_I2S_HBR_CHSTAT (1 << 6)
288#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
289#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
290#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
291#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
292#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
293#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
294#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
295#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
296# define CTS_N_K(x) (((x) & 7) << 0)
297# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600298#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
299# define ENC_CNTRL_RST_ENC (1 << 0)
300# define ENC_CNTRL_RST_SEL (1 << 1)
301# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200302#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
303# define DIP_FLAGS_ACR (1 << 0)
304# define DIP_FLAGS_GC (1 << 1)
305#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
306# define DIP_IF_FLAGS_IF1 (1 << 1)
307# define DIP_IF_FLAGS_IF2 (1 << 2)
308# define DIP_IF_FLAGS_IF3 (1 << 3)
309# define DIP_IF_FLAGS_IF4 (1 << 4)
310# define DIP_IF_FLAGS_IF5 (1 << 5)
311#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600312
313
314/* Page 12h: HDCP and OTP */
315#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200316#define REG_TX4 REG(0x12, 0x9b) /* read/write */
317# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600318#define REG_TX33 REG(0x12, 0xb8) /* read/write */
319# define TX33_HDMI (1 << 1)
320
321
322/* Page 13h: Gamut related metadata packets */
323
324
325
326/* CEC registers: (not paged)
327 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100328#define REG_CEC_INTSTATUS 0xee /* read */
329# define CEC_INTSTATUS_CEC (1 << 0)
330# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600331#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
332# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
333# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
334# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
335# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100336#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
337#define REG_CEC_RXSHPDINT 0xfd /* read */
Russell Kingec5d3e82015-06-06 21:41:10 +0100338# define CEC_RXSHPDINT_RXSENS BIT(0)
339# define CEC_RXSHPDINT_HPD BIT(1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600340#define REG_CEC_RXSHPDLEV 0xfe /* read */
341# define CEC_RXSHPDLEV_RXSENS (1 << 0)
342# define CEC_RXSHPDLEV_HPD (1 << 1)
343
344#define REG_CEC_ENAMODS 0xff /* read/write */
345# define CEC_ENAMODS_DIS_FRO (1 << 6)
346# define CEC_ENAMODS_DIS_CCLK (1 << 5)
347# define CEC_ENAMODS_EN_RXSENS (1 << 2)
348# define CEC_ENAMODS_EN_HDMI (1 << 1)
349# define CEC_ENAMODS_EN_CEC (1 << 0)
350
351
352/* Device versions: */
353#define TDA9989N2 0x0101
354#define TDA19989 0x0201
355#define TDA19989N2 0x0202
356#define TDA19988 0x0301
357
358static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100359cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600360{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100361 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100362 u8 buf[] = {addr, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600363 int ret;
364
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100365 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600366 if (ret < 0)
367 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
368}
369
Russell Kinge66e03a2015-06-06 21:41:10 +0100370static u8
371cec_read(struct tda998x_priv *priv, u8 addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600372{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100373 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100374 u8 val;
Rob Clarke7792ce2013-01-08 19:21:02 -0600375 int ret;
376
377 ret = i2c_master_send(client, &addr, sizeof(addr));
378 if (ret < 0)
379 goto fail;
380
381 ret = i2c_master_recv(client, &val, sizeof(val));
382 if (ret < 0)
383 goto fail;
384
385 return val;
386
387fail:
388 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
389 return 0;
390}
391
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100392static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100393set_page(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600394{
Rob Clarke7792ce2013-01-08 19:21:02 -0600395 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100396 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100397 u8 buf[] = {
Rob Clarke7792ce2013-01-08 19:21:02 -0600398 REG_CURPAGE, REG2PAGE(reg)
399 };
400 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100401 if (ret < 0) {
Julia Lawall288ffc72014-12-07 20:20:59 +0100402 dev_err(&client->dev, "%s %04x err %d\n", __func__,
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100403 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100404 return ret;
405 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600406
407 priv->current_page = REG2PAGE(reg);
408 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100409 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600410}
411
412static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100413reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600414{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100415 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100416 u8 addr = REG2ADDR(reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600417 int ret;
418
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100419 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100420 ret = set_page(priv, reg);
421 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100422 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600423
424 ret = i2c_master_send(client, &addr, sizeof(addr));
425 if (ret < 0)
426 goto fail;
427
428 ret = i2c_master_recv(client, buf, cnt);
429 if (ret < 0)
430 goto fail;
431
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100432 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600433
434fail:
435 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100436out:
437 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600438 return ret;
439}
440
Russell Kingc4c11dd2013-08-14 21:43:30 +0200441static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100442reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200443{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100444 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100445 u8 buf[cnt+1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200446 int ret;
447
448 buf[0] = REG2ADDR(reg);
449 memcpy(&buf[1], p, cnt);
450
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100451 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100452 ret = set_page(priv, reg);
453 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100454 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200455
456 ret = i2c_master_send(client, buf, cnt + 1);
457 if (ret < 0)
458 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100459out:
460 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200461}
462
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100463static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100464reg_read(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600465{
Russell Kinge66e03a2015-06-06 21:41:10 +0100466 u8 val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100467 int ret;
468
469 ret = reg_read_range(priv, reg, &val, sizeof(val));
470 if (ret < 0)
471 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600472 return val;
473}
474
475static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100476reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600477{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100478 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100479 u8 buf[] = {REG2ADDR(reg), val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600480 int ret;
481
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100482 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100483 ret = set_page(priv, reg);
484 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100485 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600486
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100487 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600488 if (ret < 0)
489 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100490out:
491 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600492}
493
494static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100495reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600496{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100497 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100498 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600499 int ret;
500
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100501 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100502 ret = set_page(priv, reg);
503 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100504 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600505
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100506 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600507 if (ret < 0)
508 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100509out:
510 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600511}
512
513static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100514reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600515{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100516 int old_val;
517
518 old_val = reg_read(priv, reg);
519 if (old_val >= 0)
520 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600521}
522
523static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100524reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600525{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100526 int old_val;
527
528 old_val = reg_read(priv, reg);
529 if (old_val >= 0)
530 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600531}
532
533static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100534tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600535{
536 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100537 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600538 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100539 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600540 msleep(50);
541
542 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100543 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
544 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600545
546 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100547 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
548 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
549 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
550 reg_write(priv, REG_SERIALIZER, 0x00);
551 reg_write(priv, REG_BUFFER_OUT, 0x00);
552 reg_write(priv, REG_PLL_SCG1, 0x00);
553 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
554 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
555 reg_write(priv, REG_PLL_SCGN1, 0xfa);
556 reg_write(priv, REG_PLL_SCGN2, 0x00);
557 reg_write(priv, REG_PLL_SCGR1, 0x5b);
558 reg_write(priv, REG_PLL_SCGR2, 0x00);
559 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200560
561 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100562 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600563}
564
Russell King0fc6f442015-06-06 21:41:09 +0100565/*
566 * The TDA998x has a problem when trying to read the EDID close to a
567 * HPD assertion: it needs a delay of 100ms to avoid timing out while
568 * trying to read EDID data.
569 *
570 * However, tda998x_encoder_get_modes() may be called at any moment
Russell King9525c4d2015-08-14 11:28:53 +0100571 * after tda998x_connector_detect() indicates that we are connected, so
Russell King0fc6f442015-06-06 21:41:09 +0100572 * we need to delay probing modes in tda998x_encoder_get_modes() after
573 * we have seen a HPD inactive->active transition. This code implements
574 * that delay.
575 */
576static void tda998x_edid_delay_done(unsigned long data)
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100577{
Russell King0fc6f442015-06-06 21:41:09 +0100578 struct tda998x_priv *priv = (struct tda998x_priv *)data;
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100579
Russell King0fc6f442015-06-06 21:41:09 +0100580 priv->edid_delay_active = false;
581 wake_up(&priv->edid_delay_waitq);
582 schedule_work(&priv->detect_work);
583}
584
585static void tda998x_edid_delay_start(struct tda998x_priv *priv)
586{
587 priv->edid_delay_active = true;
588 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
589}
590
591static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
592{
593 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
594}
595
596/*
597 * We need to run the KMS hotplug event helper outside of our threaded
598 * interrupt routine as this can call back into our get_modes method,
599 * which will want to make use of interrupts.
600 */
601static void tda998x_detect_work(struct work_struct *work)
602{
603 struct tda998x_priv *priv =
604 container_of(work, struct tda998x_priv, detect_work);
Russell King78e401f2015-08-14 11:17:12 +0100605 struct drm_device *dev = priv->encoder.dev;
Russell King0fc6f442015-06-06 21:41:09 +0100606
607 if (dev)
608 drm_kms_helper_hotplug_event(dev);
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100609}
610
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100611/*
612 * only 2 interrupts may occur: screen plug/unplug and EDID read
613 */
614static irqreturn_t tda998x_irq_thread(int irq, void *data)
615{
616 struct tda998x_priv *priv = data;
617 u8 sta, cec, lvl, flag0, flag1, flag2;
Russell Kingf84a97d2015-06-06 21:41:09 +0100618 bool handled = false;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100619
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100620 sta = cec_read(priv, REG_CEC_INTSTATUS);
621 cec = cec_read(priv, REG_CEC_RXSHPDINT);
622 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
623 flag0 = reg_read(priv, REG_INT_FLAGS_0);
624 flag1 = reg_read(priv, REG_INT_FLAGS_1);
625 flag2 = reg_read(priv, REG_INT_FLAGS_2);
626 DRM_DEBUG_DRIVER(
627 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
628 sta, cec, lvl, flag0, flag1, flag2);
Russell Kingec5d3e82015-06-06 21:41:10 +0100629
630 if (cec & CEC_RXSHPDINT_HPD) {
Russell King0fc6f442015-06-06 21:41:09 +0100631 if (lvl & CEC_RXSHPDLEV_HPD)
632 tda998x_edid_delay_start(priv);
633 else
634 schedule_work(&priv->detect_work);
635
Russell Kingf84a97d2015-06-06 21:41:09 +0100636 handled = true;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100637 }
Russell Kingec5d3e82015-06-06 21:41:10 +0100638
639 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
640 priv->wq_edid_wait = 0;
641 wake_up(&priv->wq_edid);
642 handled = true;
643 }
644
Russell Kingf84a97d2015-06-06 21:41:09 +0100645 return IRQ_RETVAL(handled);
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100646}
647
Russell Kingc4c11dd2013-08-14 21:43:30 +0200648static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100649tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
Russell King96795df2015-08-06 10:52:05 +0100650 union hdmi_infoframe *frame)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200651{
Russell King96795df2015-08-06 10:52:05 +0100652 u8 buf[32];
653 ssize_t len;
654
655 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
656 if (len < 0) {
657 dev_err(&priv->hdmi->dev,
658 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
659 frame->any.type, len);
660 return;
661 }
662
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100663 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
Russell King96795df2015-08-06 10:52:05 +0100664 reg_write_range(priv, addr, buf, len);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100665 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200666}
667
668static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100669tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200670{
Russell King96795df2015-08-06 10:52:05 +0100671 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200672
Russell King96795df2015-08-06 10:52:05 +0100673 hdmi_audio_infoframe_init(&frame.audio);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200674
Russell King96795df2015-08-06 10:52:05 +0100675 frame.audio.channels = p->audio_frame[1] & 0x07;
676 frame.audio.channel_allocation = p->audio_frame[4];
677 frame.audio.level_shift_value = (p->audio_frame[5] & 0x78) >> 3;
678 frame.audio.downmix_inhibit = (p->audio_frame[5] & 0x80) >> 7;
Jean-Francois Moine4a6ca1a2015-07-17 13:07:35 +0200679
Russell King96795df2015-08-06 10:52:05 +0100680 /*
681 * L-PCM and IEC61937 compressed audio shall always set sample
682 * frequency to "refer to stream". For others, see the HDMI
683 * specification.
684 */
685 frame.audio.sample_frequency = (p->audio_frame[2] & 0x1c) >> 2;
686
687 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200688}
689
690static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100691tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200692{
Russell King96795df2015-08-06 10:52:05 +0100693 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200694
Russell King96795df2015-08-06 10:52:05 +0100695 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
696 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200697
Russell King96795df2015-08-06 10:52:05 +0100698 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200699}
700
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100701static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200702{
703 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100704 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
705 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
706 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200707 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100708 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200709 }
710}
711
712static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100713tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200714 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
715{
Russell Kinge66e03a2015-06-06 21:41:10 +0100716 u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
717 u32 n;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200718
719 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100720 reg_write(priv, REG_ENA_AP, p->audio_cfg);
721 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200722
723 /* Set audio input source */
724 switch (p->audio_format) {
725 case AFMT_SPDIF:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100726 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
727 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
728 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200729 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200730 break;
731
732 case AFMT_I2S:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100733 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
734 clksel_aip = AIP_CLKSEL_AIP_I2S;
735 clksel_fs = AIP_CLKSEL_FS_ACLK;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200736 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200737 break;
David Herrmann3b288022013-09-01 15:23:04 +0200738
739 default:
740 BUG();
741 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200742 }
743
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100744 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100745 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
746 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100747 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200748
749 /*
750 * Audio input somehow depends on HDMI line rate which is
751 * related to pixclk. Testing showed that modes with pixclk
752 * >100MHz need a larger divider while <40MHz need the default.
753 * There is no detailed info in the datasheet, so we just
754 * assume 100MHz requires larger divider.
755 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100756 adiv = AUDIO_DIV_SERCLK_8;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200757 if (mode->clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100758 adiv++; /* AUDIO_DIV_SERCLK_16 */
759
760 /* S/PDIF asks for a larger divider */
761 if (p->audio_format == AFMT_SPDIF)
762 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
763
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100764 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200765
766 /*
767 * This is the approximate value of N, which happens to be
768 * the recommended values for non-coherent clocks.
769 */
770 n = 128 * p->audio_sample_rate / 1000;
771
772 /* Write the CTS and N values */
773 buf[0] = 0x44;
774 buf[1] = 0x42;
775 buf[2] = 0x01;
776 buf[3] = n;
777 buf[4] = n >> 8;
778 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100779 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200780
781 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100782 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200783
784 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100785 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
786 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200787
788 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100789 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200790 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100791 buf[2] = IEC958_AES3_CON_FS_NOTID;
792 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
793 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100794 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200795
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100796 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100797 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100798 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200799
800 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100801 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200802}
803
Rob Clarke7792ce2013-01-08 19:21:02 -0600804/* DRM encoder functions */
805
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000806static void tda998x_encoder_set_config(struct tda998x_priv *priv,
807 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600808{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200809 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
810 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
811 VIP_CNTRL_0_SWAP_B(p->swap_b) |
812 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
813 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
814 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
815 VIP_CNTRL_1_SWAP_D(p->swap_d) |
816 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
817 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
818 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
819 VIP_CNTRL_2_SWAP_F(p->swap_f) |
820 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
821
822 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600823}
824
Russell King9525c4d2015-08-14 11:28:53 +0100825static void tda998x_encoder_dpms(struct drm_encoder *encoder, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600826{
Russell King9525c4d2015-08-14 11:28:53 +0100827 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
828
Rob Clarke7792ce2013-01-08 19:21:02 -0600829 /* we only care about on or off: */
830 if (mode != DRM_MODE_DPMS_ON)
831 mode = DRM_MODE_DPMS_OFF;
832
833 if (mode == priv->dpms)
834 return;
835
836 switch (mode) {
837 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200838 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100839 reg_write(priv, REG_ENA_VP_0, 0xff);
840 reg_write(priv, REG_ENA_VP_1, 0xff);
841 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600842 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100843 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
844 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
845 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600846 break;
847 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100848 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100849 reg_write(priv, REG_ENA_VP_0, 0x00);
850 reg_write(priv, REG_ENA_VP_1, 0x00);
851 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600852 break;
853 }
854
855 priv->dpms = mode;
856}
857
Rob Clarke7792ce2013-01-08 19:21:02 -0600858static bool
859tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
860 const struct drm_display_mode *mode,
861 struct drm_display_mode *adjusted_mode)
862{
863 return true;
864}
865
Russell King9525c4d2015-08-14 11:28:53 +0100866static int tda998x_connector_mode_valid(struct drm_connector *connector,
867 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600868{
Russell King92fbdfc2014-02-07 19:52:33 +0000869 if (mode->clock > 150000)
870 return MODE_CLOCK_HIGH;
871 if (mode->htotal >= BIT(13))
872 return MODE_BAD_HVALUE;
873 if (mode->vtotal >= BIT(11))
874 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600875 return MODE_OK;
876}
877
878static void
Russell King9525c4d2015-08-14 11:28:53 +0100879tda998x_encoder_mode_set(struct drm_encoder *encoder,
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000880 struct drm_display_mode *mode,
881 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600882{
Russell King9525c4d2015-08-14 11:28:53 +0100883 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
Russell Kinge66e03a2015-06-06 21:41:10 +0100884 u16 ref_pix, ref_line, n_pix, n_line;
885 u16 hs_pix_s, hs_pix_e;
886 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
887 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
888 u16 vwin1_line_s, vwin1_line_e;
889 u16 vwin2_line_s, vwin2_line_e;
890 u16 de_pix_s, de_pix_e;
891 u8 reg, div, rep;
Rob Clarke7792ce2013-01-08 19:21:02 -0600892
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200893 /*
894 * Internally TDA998x is using ITU-R BT.656 style sync but
895 * we get VESA style sync. TDA998x is using a reference pixel
896 * relative to ITU to sync to the input frame and for output
897 * sync generation. Currently, we are using reference detection
898 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
899 * which is position of rising VS with coincident rising HS.
900 *
901 * Now there is some issues to take care of:
902 * - HDMI data islands require sync-before-active
903 * - TDA998x register values must be > 0 to be enabled
904 * - REFLINE needs an additional offset of +1
905 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
906 *
907 * So we add +1 to all horizontal and vertical register values,
908 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600909 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200910 n_pix = mode->htotal;
911 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600912
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200913 hs_pix_e = mode->hsync_end - mode->hdisplay;
914 hs_pix_s = mode->hsync_start - mode->hdisplay;
915 de_pix_e = mode->htotal;
916 de_pix_s = mode->htotal - mode->hdisplay;
917 ref_pix = 3 + hs_pix_s;
918
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200919 /*
920 * Attached LCD controllers may generate broken sync. Allow
921 * those to adjust the position of the rising VS edge by adding
922 * HSKEW to ref_pix.
923 */
924 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
925 ref_pix += adjusted_mode->hskew;
926
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200927 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
928 ref_line = 1 + mode->vsync_start - mode->vdisplay;
929 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
930 vwin1_line_e = vwin1_line_s + mode->vdisplay;
931 vs1_pix_s = vs1_pix_e = hs_pix_s;
932 vs1_line_s = mode->vsync_start - mode->vdisplay;
933 vs1_line_e = vs1_line_s +
934 mode->vsync_end - mode->vsync_start;
935 vwin2_line_s = vwin2_line_e = 0;
936 vs2_pix_s = vs2_pix_e = 0;
937 vs2_line_s = vs2_line_e = 0;
938 } else {
939 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
940 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
941 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
942 vs1_pix_s = vs1_pix_e = hs_pix_s;
943 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
944 vs1_line_e = vs1_line_s +
945 (mode->vsync_end - mode->vsync_start)/2;
946 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
947 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
948 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
949 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
950 vs2_line_e = vs2_line_s +
951 (mode->vsync_end - mode->vsync_start)/2;
952 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600953
954 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100955 if (div != 0) {
956 div--;
957 if (div > 3)
958 div = 3;
959 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600960
Rob Clarke7792ce2013-01-08 19:21:02 -0600961 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100962 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600963
964 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100965 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100966 reg_clear(priv, REG_TX33, TX33_HDMI);
967 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600968
Rob Clarke7792ce2013-01-08 19:21:02 -0600969 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100970 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600971 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100972 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
973 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600974 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600975
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100976 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100977 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
978 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100979 reg_write(priv, REG_SERIALIZER, 0);
980 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600981
982 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
983 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100984 reg_write(priv, REG_RPT_CNTRL, 0);
985 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600986 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
987
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100988 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600989 PLL_SERIAL_2_SRL_PR(rep));
990
Rob Clarke7792ce2013-01-08 19:21:02 -0600991 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100992 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
993 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600994
995 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100996 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -0600997
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200998 /*
999 * Sync on rising HSYNC/VSYNC
1000 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001001 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001002
1003 /*
1004 * TDA19988 requires high-active sync at input stage,
1005 * so invert low-active sync provided by master encoder here
1006 */
1007 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001008 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -06001009 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001010 reg |= VIP_CNTRL_3_V_TGL;
1011 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -06001012
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001013 reg_write(priv, REG_VIDFORMAT, 0x00);
1014 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1015 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1016 reg_write16(priv, REG_NPIX_MSB, n_pix);
1017 reg_write16(priv, REG_NLINE_MSB, n_line);
1018 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1019 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1020 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1021 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1022 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1023 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1024 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1025 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1026 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1027 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1028 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1029 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1030 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1031 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1032 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1033 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -06001034
1035 if (priv->rev == TDA19988) {
1036 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001037 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001038 }
1039
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001040 /*
1041 * Always generate sync polarity relative to input sync and
1042 * revert input stage toggled sync at output stage
1043 */
1044 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1045 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1046 reg |= TBG_CNTRL_1_H_TGL;
1047 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1048 reg |= TBG_CNTRL_1_V_TGL;
1049 reg_write(priv, REG_TBG_CNTRL_1, reg);
1050
Rob Clarke7792ce2013-01-08 19:21:02 -06001051 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001052 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001053
1054 /* Only setup the info frames if the sink is HDMI */
1055 if (priv->is_hdmi_sink) {
1056 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001057 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1058 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001059 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1060 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001061
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001062 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001063
1064 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001065 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001066 &priv->params);
1067 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001068}
1069
1070static enum drm_connector_status
Russell King9525c4d2015-08-14 11:28:53 +01001071tda998x_connector_detect(struct drm_connector *connector, bool force)
Rob Clarke7792ce2013-01-08 19:21:02 -06001072{
Russell King9525c4d2015-08-14 11:28:53 +01001073 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
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
Russell King9525c4d2015-08-14 11:28:53 +01001136static int tda998x_connector_get_modes(struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001137{
Russell King9525c4d2015-08-14 11:28:53 +01001138 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
Laurent Pinchart07259f82015-01-16 18:37:43 +02001139 struct edid *edid;
1140 int n;
Rob Clarke7792ce2013-01-08 19:21:02 -06001141
Russell King0fc6f442015-06-06 21:41:09 +01001142 /*
1143 * If we get killed while waiting for the HPD timeout, return
1144 * no modes found: we are not in a restartable path, so we
1145 * can't handle signals gracefully.
1146 */
1147 if (tda998x_edid_delay_wait(priv))
1148 return 0;
1149
Laurent Pinchart07259f82015-01-16 18:37:43 +02001150 if (priv->rev == TDA19988)
1151 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1152
1153 edid = drm_do_get_edid(connector, read_edid_block, priv);
1154
1155 if (priv->rev == TDA19988)
1156 reg_set(priv, REG_TX4, TX4_PD_RAM);
1157
1158 if (!edid) {
1159 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1160 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001161 }
1162
Laurent Pinchart07259f82015-01-16 18:37:43 +02001163 drm_mode_connector_update_edid_property(connector, edid);
1164 n = drm_add_edid_modes(connector, edid);
1165 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
1166 kfree(edid);
1167
Rob Clarke7792ce2013-01-08 19:21:02 -06001168 return n;
1169}
1170
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001171static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1172 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001173{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001174 if (priv->hdmi->irq)
1175 connector->polled = DRM_CONNECTOR_POLL_HPD;
1176 else
1177 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1178 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001179}
1180
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001181static void tda998x_destroy(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001182{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001183 /* disable all IRQs and free the IRQ handler */
1184 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1185 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
Russell King0fc6f442015-06-06 21:41:09 +01001186
1187 if (priv->hdmi->irq)
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001188 free_irq(priv->hdmi->irq, priv);
Russell King0fc6f442015-06-06 21:41:09 +01001189
1190 del_timer_sync(&priv->edid_delay_timer);
1191 cancel_work_sync(&priv->detect_work);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001192
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001193 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001194}
1195
Rob Clarke7792ce2013-01-08 19:21:02 -06001196/* I2C driver functions */
1197
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001198static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001199{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001200 struct device_node *np = client->dev.of_node;
1201 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001202 int rev_lo, rev_hi, ret;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001203 unsigned short cec_addr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001204
Russell King5e74c222013-08-14 21:43:29 +02001205 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1206 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1207 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1208
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001209 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001210 priv->hdmi = client;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001211 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1212 cec_addr = 0x34 + (client->addr & 0x03);
1213 priv->cec = i2c_new_dummy(client->adapter, cec_addr);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001214 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001215 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001216
Rob Clarke7792ce2013-01-08 19:21:02 -06001217 priv->dpms = DRM_MODE_DPMS_OFF;
1218
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001219 mutex_init(&priv->mutex); /* protect the page access */
Russell King0fc6f442015-06-06 21:41:09 +01001220 init_waitqueue_head(&priv->edid_delay_waitq);
1221 setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
1222 (unsigned long)priv);
1223 INIT_WORK(&priv->detect_work, tda998x_detect_work);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001224
Rob Clarke7792ce2013-01-08 19:21:02 -06001225 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001226 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001227 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1228
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001229 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001230
1231 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001232 rev_lo = reg_read(priv, REG_VERSION_LSB);
1233 rev_hi = reg_read(priv, REG_VERSION_MSB);
1234 if (rev_lo < 0 || rev_hi < 0) {
1235 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001236 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001237 }
1238
1239 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001240
1241 /* mask off feature bits: */
1242 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1243
1244 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001245 case TDA9989N2:
1246 dev_info(&client->dev, "found TDA9989 n2");
1247 break;
1248 case TDA19989:
1249 dev_info(&client->dev, "found TDA19989");
1250 break;
1251 case TDA19989N2:
1252 dev_info(&client->dev, "found TDA19989 n2");
1253 break;
1254 case TDA19988:
1255 dev_info(&client->dev, "found TDA19988");
1256 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001257 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001258 dev_err(&client->dev, "found unsupported device: %04x\n",
1259 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001260 goto fail;
1261 }
1262
1263 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001264 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001265
1266 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001267 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001268
1269 /* if necessary, disable multi-master: */
1270 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001271 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001272
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001273 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001274 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1275
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001276 /* initialize the optional IRQ */
1277 if (client->irq) {
1278 int irqf_trigger;
1279
Jean-Francois Moine6833d262014-11-29 08:57:15 +01001280 /* init read EDID waitqueue and HDP work */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001281 init_waitqueue_head(&priv->wq_edid);
1282
1283 /* clear pending interrupts */
1284 reg_read(priv, REG_INT_FLAGS_0);
1285 reg_read(priv, REG_INT_FLAGS_1);
1286 reg_read(priv, REG_INT_FLAGS_2);
1287
1288 irqf_trigger =
1289 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1290 ret = request_threaded_irq(client->irq, NULL,
1291 tda998x_irq_thread,
1292 irqf_trigger | IRQF_ONESHOT,
1293 "tda998x", priv);
1294 if (ret) {
1295 dev_err(&client->dev,
1296 "failed to request IRQ#%u: %d\n",
1297 client->irq, ret);
1298 goto fail;
1299 }
1300
1301 /* enable HPD irq */
1302 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1303 }
1304
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001305 /* enable EDID read irq: */
1306 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1307
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001308 if (!np)
1309 return 0; /* non-DT */
1310
1311 /* get the optional video properties */
1312 ret = of_property_read_u32(np, "video-ports", &video);
1313 if (ret == 0) {
1314 priv->vip_cntrl_0 = video >> 16;
1315 priv->vip_cntrl_1 = video >> 8;
1316 priv->vip_cntrl_2 = video;
1317 }
1318
Rob Clarke7792ce2013-01-08 19:21:02 -06001319 return 0;
1320
1321fail:
1322 /* if encoder_init fails, the encoder slave is never registered,
1323 * so cleanup here:
1324 */
1325 if (priv->cec)
1326 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001327 return -ENXIO;
1328}
1329
Russell Kingc707c362014-02-07 19:49:44 +00001330static void tda998x_encoder_prepare(struct drm_encoder *encoder)
1331{
Russell King9525c4d2015-08-14 11:28:53 +01001332 tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
Russell Kingc707c362014-02-07 19:49:44 +00001333}
1334
1335static void tda998x_encoder_commit(struct drm_encoder *encoder)
1336{
Russell King9525c4d2015-08-14 11:28:53 +01001337 tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
Russell Kingc707c362014-02-07 19:49:44 +00001338}
1339
1340static const struct drm_encoder_helper_funcs tda998x_encoder_helper_funcs = {
Russell King9525c4d2015-08-14 11:28:53 +01001341 .dpms = tda998x_encoder_dpms,
Russell Kingc707c362014-02-07 19:49:44 +00001342 .mode_fixup = tda998x_encoder_mode_fixup,
1343 .prepare = tda998x_encoder_prepare,
1344 .commit = tda998x_encoder_commit,
Russell King9525c4d2015-08-14 11:28:53 +01001345 .mode_set = tda998x_encoder_mode_set,
Russell Kingc707c362014-02-07 19:49:44 +00001346};
1347
1348static void tda998x_encoder_destroy(struct drm_encoder *encoder)
1349{
Russell Kinga3584f62015-08-14 11:22:50 +01001350 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001351
Russell Kinga3584f62015-08-14 11:22:50 +01001352 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001353 drm_encoder_cleanup(encoder);
1354}
1355
1356static const struct drm_encoder_funcs tda998x_encoder_funcs = {
1357 .destroy = tda998x_encoder_destroy,
1358};
1359
Russell Kingc707c362014-02-07 19:49:44 +00001360static struct drm_encoder *
1361tda998x_connector_best_encoder(struct drm_connector *connector)
1362{
Russell Kinga3584f62015-08-14 11:22:50 +01001363 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
Russell Kingc707c362014-02-07 19:49:44 +00001364
Russell Kinga3584f62015-08-14 11:22:50 +01001365 return &priv->encoder;
Russell Kingc707c362014-02-07 19:49:44 +00001366}
1367
1368static
1369const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
1370 .get_modes = tda998x_connector_get_modes,
1371 .mode_valid = tda998x_connector_mode_valid,
1372 .best_encoder = tda998x_connector_best_encoder,
1373};
1374
Russell Kingc707c362014-02-07 19:49:44 +00001375static void tda998x_connector_destroy(struct drm_connector *connector)
1376{
Dave Airlie74cd62e2014-08-05 10:34:33 +10001377 drm_connector_unregister(connector);
Russell Kingc707c362014-02-07 19:49:44 +00001378 drm_connector_cleanup(connector);
1379}
1380
1381static const struct drm_connector_funcs tda998x_connector_funcs = {
1382 .dpms = drm_helper_connector_dpms,
1383 .fill_modes = drm_helper_probe_single_connector_modes,
1384 .detect = tda998x_connector_detect,
1385 .destroy = tda998x_connector_destroy,
1386};
1387
1388static int tda998x_bind(struct device *dev, struct device *master, void *data)
1389{
1390 struct tda998x_encoder_params *params = dev->platform_data;
1391 struct i2c_client *client = to_i2c_client(dev);
1392 struct drm_device *drm = data;
Russell Kinga3584f62015-08-14 11:22:50 +01001393 struct tda998x_priv *priv;
Russell Kinge66e03a2015-06-06 21:41:10 +01001394 u32 crtcs = 0;
Russell Kingc707c362014-02-07 19:49:44 +00001395 int ret;
1396
1397 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1398 if (!priv)
1399 return -ENOMEM;
1400
1401 dev_set_drvdata(dev, priv);
1402
Russell King5dbcf312014-06-15 11:11:10 +01001403 if (dev->of_node)
1404 crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
1405
1406 /* If no CRTCs were found, fall back to our old behaviour */
1407 if (crtcs == 0) {
1408 dev_warn(dev, "Falling back to first CRTC\n");
1409 crtcs = 1 << 0;
1410 }
1411
Russell Kinga3584f62015-08-14 11:22:50 +01001412 priv->connector.interlace_allowed = 1;
1413 priv->encoder.possible_crtcs = crtcs;
Russell Kingc707c362014-02-07 19:49:44 +00001414
Russell Kinga3584f62015-08-14 11:22:50 +01001415 ret = tda998x_create(client, priv);
Russell Kingc707c362014-02-07 19:49:44 +00001416 if (ret)
1417 return ret;
1418
1419 if (!dev->of_node && params)
Russell Kinga3584f62015-08-14 11:22:50 +01001420 tda998x_encoder_set_config(priv, params);
Russell Kingc707c362014-02-07 19:49:44 +00001421
Russell Kinga3584f62015-08-14 11:22:50 +01001422 tda998x_encoder_set_polling(priv, &priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001423
Russell Kinga3584f62015-08-14 11:22:50 +01001424 drm_encoder_helper_add(&priv->encoder, &tda998x_encoder_helper_funcs);
1425 ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001426 DRM_MODE_ENCODER_TMDS, NULL);
Russell Kingc707c362014-02-07 19:49:44 +00001427 if (ret)
1428 goto err_encoder;
1429
Russell Kinga3584f62015-08-14 11:22:50 +01001430 drm_connector_helper_add(&priv->connector,
Russell Kingc707c362014-02-07 19:49:44 +00001431 &tda998x_connector_helper_funcs);
Russell Kinga3584f62015-08-14 11:22:50 +01001432 ret = drm_connector_init(drm, &priv->connector,
Russell Kingc707c362014-02-07 19:49:44 +00001433 &tda998x_connector_funcs,
1434 DRM_MODE_CONNECTOR_HDMIA);
1435 if (ret)
1436 goto err_connector;
1437
Russell Kinga3584f62015-08-14 11:22:50 +01001438 ret = drm_connector_register(&priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001439 if (ret)
1440 goto err_sysfs;
1441
Russell Kinga3584f62015-08-14 11:22:50 +01001442 priv->connector.encoder = &priv->encoder;
1443 drm_mode_connector_attach_encoder(&priv->connector, &priv->encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001444
1445 return 0;
1446
1447err_sysfs:
Russell Kinga3584f62015-08-14 11:22:50 +01001448 drm_connector_cleanup(&priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001449err_connector:
Russell Kinga3584f62015-08-14 11:22:50 +01001450 drm_encoder_cleanup(&priv->encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001451err_encoder:
Russell Kinga3584f62015-08-14 11:22:50 +01001452 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001453 return ret;
1454}
1455
1456static void tda998x_unbind(struct device *dev, struct device *master,
1457 void *data)
1458{
Russell Kinga3584f62015-08-14 11:22:50 +01001459 struct tda998x_priv *priv = dev_get_drvdata(dev);
Russell Kingc707c362014-02-07 19:49:44 +00001460
Russell Kinga3584f62015-08-14 11:22:50 +01001461 drm_connector_cleanup(&priv->connector);
1462 drm_encoder_cleanup(&priv->encoder);
1463 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001464}
1465
1466static const struct component_ops tda998x_ops = {
1467 .bind = tda998x_bind,
1468 .unbind = tda998x_unbind,
1469};
1470
1471static int
1472tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1473{
1474 return component_add(&client->dev, &tda998x_ops);
1475}
1476
1477static int tda998x_remove(struct i2c_client *client)
1478{
1479 component_del(&client->dev, &tda998x_ops);
1480 return 0;
1481}
1482
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001483#ifdef CONFIG_OF
1484static const struct of_device_id tda998x_dt_ids[] = {
1485 { .compatible = "nxp,tda998x", },
1486 { }
1487};
1488MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1489#endif
1490
Rob Clarke7792ce2013-01-08 19:21:02 -06001491static struct i2c_device_id tda998x_ids[] = {
1492 { "tda998x", 0 },
1493 { }
1494};
1495MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1496
Russell King3d58e312015-08-14 11:13:50 +01001497static struct i2c_driver tda998x_driver = {
1498 .probe = tda998x_probe,
1499 .remove = tda998x_remove,
1500 .driver = {
1501 .name = "tda998x",
1502 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001503 },
Russell King3d58e312015-08-14 11:13:50 +01001504 .id_table = tda998x_ids,
Rob Clarke7792ce2013-01-08 19:21:02 -06001505};
1506
Russell King3d58e312015-08-14 11:13:50 +01001507module_i2c_driver(tda998x_driver);
Rob Clarke7792ce2013-01-08 19:21:02 -06001508
1509MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1510MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1511MODULE_LICENSE("GPL");