blob: 6795f094f7373b5dfe55ed56508808b109d65a49 [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>
26#include <drm/drm_encoder_slave.h>
27#include <drm/drm_edid.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;
Rob Clarke7792ce2013-01-08 19:21:02 -060036 uint16_t rev;
37 uint8_t current_page;
38 int dpms;
Russell Kingc4c11dd2013-08-14 21:43:30 +020039 bool is_hdmi_sink;
Russell King5e74c222013-08-14 21:43:29 +020040 u8 vip_cntrl_0;
41 u8 vip_cntrl_1;
42 u8 vip_cntrl_2;
Russell Kingc4c11dd2013-08-14 21:43:30 +020043 struct tda998x_encoder_params params;
Jean-Francois Moine12473b72014-01-25 18:14:38 +010044
45 wait_queue_head_t wq_edid;
46 volatile int wq_edid_wait;
47 struct drm_encoder *encoder;
Rob Clarke7792ce2013-01-08 19:21:02 -060048};
49
50#define to_tda998x_priv(x) ((struct tda998x_priv *)to_encoder_slave(x)->slave_priv)
51
52/* The TDA9988 series of devices use a paged register scheme.. to simplify
53 * things we encode the page # in upper bits of the register #. To read/
54 * write a given register, we need to make sure CURPAGE register is set
55 * appropriately. Which implies reads/writes are not atomic. Fun!
56 */
57
58#define REG(page, addr) (((page) << 8) | (addr))
59#define REG2ADDR(reg) ((reg) & 0xff)
60#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
61
62#define REG_CURPAGE 0xff /* write */
63
64
65/* Page 00h: General Control */
66#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
67#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
68# define MAIN_CNTRL0_SR (1 << 0)
69# define MAIN_CNTRL0_DECS (1 << 1)
70# define MAIN_CNTRL0_DEHS (1 << 2)
71# define MAIN_CNTRL0_CECS (1 << 3)
72# define MAIN_CNTRL0_CEHS (1 << 4)
73# define MAIN_CNTRL0_SCALER (1 << 7)
74#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
75#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
76# define SOFTRESET_AUDIO (1 << 0)
77# define SOFTRESET_I2C_MASTER (1 << 1)
78#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
79#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
80#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
81# define I2C_MASTER_DIS_MM (1 << 0)
82# define I2C_MASTER_DIS_FILT (1 << 1)
83# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +020084#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
85# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -060086#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
87#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
88#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
89# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +020090#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -060091#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
92#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
93#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
94#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
95#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
96# define VIP_CNTRL_0_MIRR_A (1 << 7)
97# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
98# define VIP_CNTRL_0_MIRR_B (1 << 3)
99# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
100#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
101# define VIP_CNTRL_1_MIRR_C (1 << 7)
102# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
103# define VIP_CNTRL_1_MIRR_D (1 << 3)
104# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
105#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
106# define VIP_CNTRL_2_MIRR_E (1 << 7)
107# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
108# define VIP_CNTRL_2_MIRR_F (1 << 3)
109# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
110#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
111# define VIP_CNTRL_3_X_TGL (1 << 0)
112# define VIP_CNTRL_3_H_TGL (1 << 1)
113# define VIP_CNTRL_3_V_TGL (1 << 2)
114# define VIP_CNTRL_3_EMB (1 << 3)
115# define VIP_CNTRL_3_SYNC_DE (1 << 4)
116# define VIP_CNTRL_3_SYNC_HS (1 << 5)
117# define VIP_CNTRL_3_DE_INT (1 << 6)
118# define VIP_CNTRL_3_EDGE (1 << 7)
119#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
120# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
121# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
122# define VIP_CNTRL_4_CCIR656 (1 << 4)
123# define VIP_CNTRL_4_656_ALT (1 << 5)
124# define VIP_CNTRL_4_TST_656 (1 << 6)
125# define VIP_CNTRL_4_TST_PAT (1 << 7)
126#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
127# define VIP_CNTRL_5_CKCASE (1 << 0)
128# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200129#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100130# define MUX_AP_SELECT_I2S 0x64
131# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb24812013-08-14 21:43:27 +0200132#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600133#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
134# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
135# define MAT_CONTRL_MAT_BP (1 << 2)
136#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
137#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
138#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
139#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
140#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
141#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
142#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
143#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
144#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
145#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
146#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
147#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
148#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
149#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
150#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
151#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
152#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200153#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
154#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600155#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
156#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200157#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
158#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600159#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
160#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
161#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
162#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
163#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
164#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
165#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
166#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
167#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
168#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200169#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
170#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
171#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
172#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600173#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
174#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
175#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
176#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
177#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200178# define TBG_CNTRL_0_TOP_TGL (1 << 0)
179# define TBG_CNTRL_0_TOP_SEL (1 << 1)
180# define TBG_CNTRL_0_DE_EXT (1 << 2)
181# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600182# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
183# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
184# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
185#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200186# define TBG_CNTRL_1_H_TGL (1 << 0)
187# define TBG_CNTRL_1_V_TGL (1 << 1)
188# define TBG_CNTRL_1_TGL_EN (1 << 2)
189# define TBG_CNTRL_1_X_EXT (1 << 3)
190# define TBG_CNTRL_1_H_EXT (1 << 4)
191# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600192# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
193#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
194#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
195# define HVF_CNTRL_0_SM (1 << 7)
196# define HVF_CNTRL_0_RWB (1 << 6)
197# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
198# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
199#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
200# define HVF_CNTRL_1_FOR (1 << 0)
201# define HVF_CNTRL_1_YUVBLK (1 << 1)
202# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
203# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
204# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
205#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200206#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
207# define I2S_FORMAT(x) (((x) & 3) << 0)
208#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100209# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
210# define AIP_CLKSEL_AIP_I2S (1 << 3)
211# define AIP_CLKSEL_FS_ACLK (0 << 0)
212# define AIP_CLKSEL_FS_MCLK (1 << 0)
213# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600214
215/* Page 02h: PLL settings */
216#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
217# define PLL_SERIAL_1_SRL_FDN (1 << 0)
218# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
219# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
220#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100221# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600222# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
223#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
224# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
225# define PLL_SERIAL_3_SRL_DE (1 << 2)
226# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
227#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
228#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
229#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
230#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
231#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
232#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
233#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
234#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
235#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200236# define AUDIO_DIV_SERCLK_1 0
237# define AUDIO_DIV_SERCLK_2 1
238# define AUDIO_DIV_SERCLK_4 2
239# define AUDIO_DIV_SERCLK_8 3
240# define AUDIO_DIV_SERCLK_16 4
241# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600242#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
243# define SEL_CLK_SEL_CLK1 (1 << 0)
244# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
245# define SEL_CLK_ENA_SC_CLK (1 << 3)
246#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
247
248
249/* Page 09h: EDID Control */
250#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
251/* next 127 successive registers are the EDID block */
252#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
253#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
254#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
255#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
256#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
257
258
259/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200260#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
261#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
262#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
263#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
264#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600265
266
267/* Page 11h: audio settings and content info packets */
268#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
269# define AIP_CNTRL_0_RST_FIFO (1 << 0)
270# define AIP_CNTRL_0_SWAP (1 << 1)
271# define AIP_CNTRL_0_LAYOUT (1 << 2)
272# define AIP_CNTRL_0_ACR_MAN (1 << 5)
273# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200274#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
275# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
276# define CA_I2S_HBR_CHSTAT (1 << 6)
277#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
278#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
279#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
280#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
281#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
282#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
283#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
284#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
285# define CTS_N_K(x) (((x) & 7) << 0)
286# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600287#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
288# define ENC_CNTRL_RST_ENC (1 << 0)
289# define ENC_CNTRL_RST_SEL (1 << 1)
290# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200291#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
292# define DIP_FLAGS_ACR (1 << 0)
293# define DIP_FLAGS_GC (1 << 1)
294#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
295# define DIP_IF_FLAGS_IF1 (1 << 1)
296# define DIP_IF_FLAGS_IF2 (1 << 2)
297# define DIP_IF_FLAGS_IF3 (1 << 3)
298# define DIP_IF_FLAGS_IF4 (1 << 4)
299# define DIP_IF_FLAGS_IF5 (1 << 5)
300#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600301
302
303/* Page 12h: HDCP and OTP */
304#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200305#define REG_TX4 REG(0x12, 0x9b) /* read/write */
306# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600307#define REG_TX33 REG(0x12, 0xb8) /* read/write */
308# define TX33_HDMI (1 << 1)
309
310
311/* Page 13h: Gamut related metadata packets */
312
313
314
315/* CEC registers: (not paged)
316 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100317#define REG_CEC_INTSTATUS 0xee /* read */
318# define CEC_INTSTATUS_CEC (1 << 0)
319# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600320#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
321# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
322# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
323# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
324# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100325#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
326#define REG_CEC_RXSHPDINT 0xfd /* read */
Rob Clarke7792ce2013-01-08 19:21:02 -0600327#define REG_CEC_RXSHPDLEV 0xfe /* read */
328# define CEC_RXSHPDLEV_RXSENS (1 << 0)
329# define CEC_RXSHPDLEV_HPD (1 << 1)
330
331#define REG_CEC_ENAMODS 0xff /* read/write */
332# define CEC_ENAMODS_DIS_FRO (1 << 6)
333# define CEC_ENAMODS_DIS_CCLK (1 << 5)
334# define CEC_ENAMODS_EN_RXSENS (1 << 2)
335# define CEC_ENAMODS_EN_HDMI (1 << 1)
336# define CEC_ENAMODS_EN_CEC (1 << 0)
337
338
339/* Device versions: */
340#define TDA9989N2 0x0101
341#define TDA19989 0x0201
342#define TDA19989N2 0x0202
343#define TDA19988 0x0301
344
345static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100346cec_write(struct tda998x_priv *priv, uint16_t addr, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600347{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100348 struct i2c_client *client = priv->cec;
Rob Clarke7792ce2013-01-08 19:21:02 -0600349 uint8_t buf[] = {addr, val};
350 int ret;
351
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100352 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600353 if (ret < 0)
354 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
355}
356
357static uint8_t
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100358cec_read(struct tda998x_priv *priv, uint8_t addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600359{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100360 struct i2c_client *client = priv->cec;
Rob Clarke7792ce2013-01-08 19:21:02 -0600361 uint8_t val;
362 int ret;
363
364 ret = i2c_master_send(client, &addr, sizeof(addr));
365 if (ret < 0)
366 goto fail;
367
368 ret = i2c_master_recv(client, &val, sizeof(val));
369 if (ret < 0)
370 goto fail;
371
372 return val;
373
374fail:
375 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
376 return 0;
377}
378
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100379static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100380set_page(struct tda998x_priv *priv, uint16_t reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600381{
Rob Clarke7792ce2013-01-08 19:21:02 -0600382 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100383 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600384 uint8_t buf[] = {
385 REG_CURPAGE, REG2PAGE(reg)
386 };
387 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100388 if (ret < 0) {
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100389 dev_err(&client->dev, "setpage %04x err %d\n",
390 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100391 return ret;
392 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600393
394 priv->current_page = REG2PAGE(reg);
395 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100396 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600397}
398
399static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100400reg_read_range(struct tda998x_priv *priv, uint16_t reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600401{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100402 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600403 uint8_t addr = REG2ADDR(reg);
404 int ret;
405
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100406 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100407 ret = set_page(priv, reg);
408 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100409 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600410
411 ret = i2c_master_send(client, &addr, sizeof(addr));
412 if (ret < 0)
413 goto fail;
414
415 ret = i2c_master_recv(client, buf, cnt);
416 if (ret < 0)
417 goto fail;
418
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100419 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600420
421fail:
422 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100423out:
424 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600425 return ret;
426}
427
Russell Kingc4c11dd2013-08-14 21:43:30 +0200428static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100429reg_write_range(struct tda998x_priv *priv, uint16_t reg, uint8_t *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200430{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100431 struct i2c_client *client = priv->hdmi;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200432 uint8_t buf[cnt+1];
433 int ret;
434
435 buf[0] = REG2ADDR(reg);
436 memcpy(&buf[1], p, cnt);
437
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100438 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100439 ret = set_page(priv, reg);
440 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100441 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200442
443 ret = i2c_master_send(client, buf, cnt + 1);
444 if (ret < 0)
445 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100446out:
447 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200448}
449
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100450static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100451reg_read(struct tda998x_priv *priv, uint16_t reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600452{
453 uint8_t val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100454 int ret;
455
456 ret = reg_read_range(priv, reg, &val, sizeof(val));
457 if (ret < 0)
458 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600459 return val;
460}
461
462static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100463reg_write(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600464{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100465 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600466 uint8_t buf[] = {REG2ADDR(reg), val};
467 int ret;
468
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100469 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100470 ret = set_page(priv, reg);
471 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100472 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600473
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100474 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600475 if (ret < 0)
476 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100477out:
478 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600479}
480
481static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100482reg_write16(struct tda998x_priv *priv, uint16_t reg, uint16_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600483{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100484 struct i2c_client *client = priv->hdmi;
Rob Clarke7792ce2013-01-08 19:21:02 -0600485 uint8_t buf[] = {REG2ADDR(reg), val >> 8, val};
486 int ret;
487
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100488 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100489 ret = set_page(priv, reg);
490 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100491 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600492
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100493 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600494 if (ret < 0)
495 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100496out:
497 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600498}
499
500static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100501reg_set(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600502{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100503 int old_val;
504
505 old_val = reg_read(priv, reg);
506 if (old_val >= 0)
507 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600508}
509
510static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100511reg_clear(struct tda998x_priv *priv, uint16_t reg, uint8_t val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600512{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100513 int old_val;
514
515 old_val = reg_read(priv, reg);
516 if (old_val >= 0)
517 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600518}
519
520static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100521tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600522{
523 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100524 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600525 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100526 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600527 msleep(50);
528
529 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100530 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
531 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600532
533 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100534 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
535 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
536 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
537 reg_write(priv, REG_SERIALIZER, 0x00);
538 reg_write(priv, REG_BUFFER_OUT, 0x00);
539 reg_write(priv, REG_PLL_SCG1, 0x00);
540 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
541 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
542 reg_write(priv, REG_PLL_SCGN1, 0xfa);
543 reg_write(priv, REG_PLL_SCGN2, 0x00);
544 reg_write(priv, REG_PLL_SCGR1, 0x5b);
545 reg_write(priv, REG_PLL_SCGR2, 0x00);
546 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200547
548 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100549 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600550}
551
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100552/*
553 * only 2 interrupts may occur: screen plug/unplug and EDID read
554 */
555static irqreturn_t tda998x_irq_thread(int irq, void *data)
556{
557 struct tda998x_priv *priv = data;
558 u8 sta, cec, lvl, flag0, flag1, flag2;
559
560 if (!priv)
561 return IRQ_HANDLED;
562 sta = cec_read(priv, REG_CEC_INTSTATUS);
563 cec = cec_read(priv, REG_CEC_RXSHPDINT);
564 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
565 flag0 = reg_read(priv, REG_INT_FLAGS_0);
566 flag1 = reg_read(priv, REG_INT_FLAGS_1);
567 flag2 = reg_read(priv, REG_INT_FLAGS_2);
568 DRM_DEBUG_DRIVER(
569 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
570 sta, cec, lvl, flag0, flag1, flag2);
571 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
572 priv->wq_edid_wait = 0;
573 wake_up(&priv->wq_edid);
574 } else if (cec != 0) { /* HPD change */
575 if (priv->encoder && priv->encoder->dev)
576 drm_helper_hpd_irq_event(priv->encoder->dev);
577 }
578 return IRQ_HANDLED;
579}
580
Russell Kingc4c11dd2013-08-14 21:43:30 +0200581static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes)
582{
Daniel Vetter8268bd42014-04-05 18:24:29 +0200583 int sum = 0;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200584
585 while (bytes--)
Daniel Vetter8268bd42014-04-05 18:24:29 +0200586 sum -= *buf++;
587 return sum;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200588}
589
590#define HB(x) (x)
591#define PB(x) (HB(2) + 1 + (x))
592
593static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100594tda998x_write_if(struct tda998x_priv *priv, uint8_t bit, uint16_t addr,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200595 uint8_t *buf, size_t size)
596{
597 buf[PB(0)] = tda998x_cksum(buf, size);
598
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100599 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
600 reg_write_range(priv, addr, buf, size);
601 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200602}
603
604static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100605tda998x_write_aif(struct tda998x_priv *priv, struct tda998x_encoder_params *p)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200606{
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100607 u8 buf[PB(HDMI_AUDIO_INFOFRAME_SIZE) + 1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200608
Jean-Francois Moine7288ca02014-01-25 18:14:44 +0100609 memset(buf, 0, sizeof(buf));
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100610 buf[HB(0)] = HDMI_INFOFRAME_TYPE_AUDIO;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200611 buf[HB(1)] = 0x01;
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100612 buf[HB(2)] = HDMI_AUDIO_INFOFRAME_SIZE;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200613 buf[PB(1)] = p->audio_frame[1] & 0x07; /* CC */
614 buf[PB(2)] = p->audio_frame[2] & 0x1c; /* SF */
615 buf[PB(4)] = p->audio_frame[4];
616 buf[PB(5)] = p->audio_frame[5] & 0xf8; /* DM_INH + LSV */
617
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100618 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, buf,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200619 sizeof(buf));
620}
621
622static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100623tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200624{
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100625 u8 buf[PB(HDMI_AVI_INFOFRAME_SIZE) + 1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200626
627 memset(buf, 0, sizeof(buf));
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100628 buf[HB(0)] = HDMI_INFOFRAME_TYPE_AVI;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200629 buf[HB(1)] = 0x02;
Jean-Francois Moine9e541462014-01-25 18:14:41 +0100630 buf[HB(2)] = HDMI_AVI_INFOFRAME_SIZE;
Russell King893c3e52013-08-27 01:27:42 +0100631 buf[PB(1)] = HDMI_SCAN_MODE_UNDERSCAN;
Jean-Francois Moinebdf63452014-01-25 18:14:40 +0100632 buf[PB(2)] = HDMI_ACTIVE_ASPECT_PICTURE;
Russell King893c3e52013-08-27 01:27:42 +0100633 buf[PB(3)] = HDMI_QUANTIZATION_RANGE_FULL << 2;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200634 buf[PB(4)] = drm_match_cea_mode(mode);
635
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100636 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, buf,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200637 sizeof(buf));
638}
639
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100640static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200641{
642 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100643 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
644 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
645 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200646 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100647 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200648 }
649}
650
651static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100652tda998x_configure_audio(struct tda998x_priv *priv,
Russell Kingc4c11dd2013-08-14 21:43:30 +0200653 struct drm_display_mode *mode, struct tda998x_encoder_params *p)
654{
Jean-Francois Moine85c988b2014-01-25 18:14:40 +0100655 uint8_t buf[6], clksel_aip, clksel_fs, cts_n, adiv;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200656 uint32_t n;
657
658 /* Enable audio ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100659 reg_write(priv, REG_ENA_AP, p->audio_cfg);
660 reg_write(priv, REG_ENA_ACLK, p->audio_clk_cfg);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200661
662 /* Set audio input source */
663 switch (p->audio_format) {
664 case AFMT_SPDIF:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100665 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
666 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
667 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200668 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200669 break;
670
671 case AFMT_I2S:
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100672 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
673 clksel_aip = AIP_CLKSEL_AIP_I2S;
674 clksel_fs = AIP_CLKSEL_FS_ACLK;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200675 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200676 break;
David Herrmann3b288022013-09-01 15:23:04 +0200677
678 default:
679 BUG();
680 return;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200681 }
682
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100683 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100684 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
685 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100686 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200687
688 /*
689 * Audio input somehow depends on HDMI line rate which is
690 * related to pixclk. Testing showed that modes with pixclk
691 * >100MHz need a larger divider while <40MHz need the default.
692 * There is no detailed info in the datasheet, so we just
693 * assume 100MHz requires larger divider.
694 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100695 adiv = AUDIO_DIV_SERCLK_8;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200696 if (mode->clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100697 adiv++; /* AUDIO_DIV_SERCLK_16 */
698
699 /* S/PDIF asks for a larger divider */
700 if (p->audio_format == AFMT_SPDIF)
701 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
702
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100703 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200704
705 /*
706 * This is the approximate value of N, which happens to be
707 * the recommended values for non-coherent clocks.
708 */
709 n = 128 * p->audio_sample_rate / 1000;
710
711 /* Write the CTS and N values */
712 buf[0] = 0x44;
713 buf[1] = 0x42;
714 buf[2] = 0x01;
715 buf[3] = n;
716 buf[4] = n >> 8;
717 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100718 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200719
720 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100721 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200722
723 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100724 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
725 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200726
727 /* Write the channel status */
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100728 buf[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200729 buf[1] = 0x00;
Jean-Francois Moinef0b33b22014-01-25 18:14:39 +0100730 buf[2] = IEC958_AES3_CON_FS_NOTID;
731 buf[3] = IEC958_AES4_CON_ORIGFS_NOTID |
732 IEC958_AES4_CON_MAX_WORDLEN_24;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100733 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200734
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100735 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100736 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100737 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200738
739 /* Write the audio information packet */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100740 tda998x_write_aif(priv, p);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200741}
742
Rob Clarke7792ce2013-01-08 19:21:02 -0600743/* DRM encoder functions */
744
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000745static void tda998x_encoder_set_config(struct tda998x_priv *priv,
746 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600747{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200748 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
749 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
750 VIP_CNTRL_0_SWAP_B(p->swap_b) |
751 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
752 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
753 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
754 VIP_CNTRL_1_SWAP_D(p->swap_d) |
755 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
756 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
757 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
758 VIP_CNTRL_2_SWAP_F(p->swap_f) |
759 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
760
761 priv->params = *p;
Rob Clarke7792ce2013-01-08 19:21:02 -0600762}
763
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000764static void tda998x_encoder_dpms(struct tda998x_priv *priv, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600765{
Rob Clarke7792ce2013-01-08 19:21:02 -0600766 /* we only care about on or off: */
767 if (mode != DRM_MODE_DPMS_ON)
768 mode = DRM_MODE_DPMS_OFF;
769
770 if (mode == priv->dpms)
771 return;
772
773 switch (mode) {
774 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200775 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100776 reg_write(priv, REG_ENA_VP_0, 0xff);
777 reg_write(priv, REG_ENA_VP_1, 0xff);
778 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600779 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100780 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
781 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
782 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600783 break;
784 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100785 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100786 reg_write(priv, REG_ENA_VP_0, 0x00);
787 reg_write(priv, REG_ENA_VP_1, 0x00);
788 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600789 break;
790 }
791
792 priv->dpms = mode;
793}
794
795static void
796tda998x_encoder_save(struct drm_encoder *encoder)
797{
798 DBG("");
799}
800
801static void
802tda998x_encoder_restore(struct drm_encoder *encoder)
803{
804 DBG("");
805}
806
807static bool
808tda998x_encoder_mode_fixup(struct drm_encoder *encoder,
809 const struct drm_display_mode *mode,
810 struct drm_display_mode *adjusted_mode)
811{
812 return true;
813}
814
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000815static int tda998x_encoder_mode_valid(struct tda998x_priv *priv,
816 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600817{
Russell King92fbdfc2014-02-07 19:52:33 +0000818 if (mode->clock > 150000)
819 return MODE_CLOCK_HIGH;
820 if (mode->htotal >= BIT(13))
821 return MODE_BAD_HVALUE;
822 if (mode->vtotal >= BIT(11))
823 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600824 return MODE_OK;
825}
826
827static void
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000828tda998x_encoder_mode_set(struct tda998x_priv *priv,
829 struct drm_display_mode *mode,
830 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600831{
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200832 uint16_t ref_pix, ref_line, n_pix, n_line;
833 uint16_t hs_pix_s, hs_pix_e;
834 uint16_t vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
835 uint16_t vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
836 uint16_t vwin1_line_s, vwin1_line_e;
837 uint16_t vwin2_line_s, vwin2_line_e;
838 uint16_t de_pix_s, de_pix_e;
Rob Clarke7792ce2013-01-08 19:21:02 -0600839 uint8_t reg, div, rep;
840
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200841 /*
842 * Internally TDA998x is using ITU-R BT.656 style sync but
843 * we get VESA style sync. TDA998x is using a reference pixel
844 * relative to ITU to sync to the input frame and for output
845 * sync generation. Currently, we are using reference detection
846 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
847 * which is position of rising VS with coincident rising HS.
848 *
849 * Now there is some issues to take care of:
850 * - HDMI data islands require sync-before-active
851 * - TDA998x register values must be > 0 to be enabled
852 * - REFLINE needs an additional offset of +1
853 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
854 *
855 * So we add +1 to all horizontal and vertical register values,
856 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600857 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200858 n_pix = mode->htotal;
859 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600860
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200861 hs_pix_e = mode->hsync_end - mode->hdisplay;
862 hs_pix_s = mode->hsync_start - mode->hdisplay;
863 de_pix_e = mode->htotal;
864 de_pix_s = mode->htotal - mode->hdisplay;
865 ref_pix = 3 + hs_pix_s;
866
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200867 /*
868 * Attached LCD controllers may generate broken sync. Allow
869 * those to adjust the position of the rising VS edge by adding
870 * HSKEW to ref_pix.
871 */
872 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
873 ref_pix += adjusted_mode->hskew;
874
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200875 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
876 ref_line = 1 + mode->vsync_start - mode->vdisplay;
877 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
878 vwin1_line_e = vwin1_line_s + mode->vdisplay;
879 vs1_pix_s = vs1_pix_e = hs_pix_s;
880 vs1_line_s = mode->vsync_start - mode->vdisplay;
881 vs1_line_e = vs1_line_s +
882 mode->vsync_end - mode->vsync_start;
883 vwin2_line_s = vwin2_line_e = 0;
884 vs2_pix_s = vs2_pix_e = 0;
885 vs2_line_s = vs2_line_e = 0;
886 } else {
887 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
888 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
889 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
890 vs1_pix_s = vs1_pix_e = hs_pix_s;
891 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
892 vs1_line_e = vs1_line_s +
893 (mode->vsync_end - mode->vsync_start)/2;
894 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
895 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
896 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
897 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
898 vs2_line_e = vs2_line_s +
899 (mode->vsync_end - mode->vsync_start)/2;
900 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600901
902 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100903 if (div != 0) {
904 div--;
905 if (div > 3)
906 div = 3;
907 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600908
Rob Clarke7792ce2013-01-08 19:21:02 -0600909 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100910 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600911
912 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100913 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100914 reg_clear(priv, REG_TX33, TX33_HDMI);
915 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600916
Rob Clarke7792ce2013-01-08 19:21:02 -0600917 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100918 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600919 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100920 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
921 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600922 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600923
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100924 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100925 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
926 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100927 reg_write(priv, REG_SERIALIZER, 0);
928 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600929
930 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
931 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100932 reg_write(priv, REG_RPT_CNTRL, 0);
933 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600934 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
935
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100936 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600937 PLL_SERIAL_2_SRL_PR(rep));
938
Rob Clarke7792ce2013-01-08 19:21:02 -0600939 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100940 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
941 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -0600942
943 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100944 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -0600945
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200946 /*
947 * Sync on rising HSYNC/VSYNC
948 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100949 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200950
951 /*
952 * TDA19988 requires high-active sync at input stage,
953 * so invert low-active sync provided by master encoder here
954 */
955 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100956 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -0600957 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100958 reg |= VIP_CNTRL_3_V_TGL;
959 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600960
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100961 reg_write(priv, REG_VIDFORMAT, 0x00);
962 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
963 reg_write16(priv, REG_REFLINE_MSB, ref_line);
964 reg_write16(priv, REG_NPIX_MSB, n_pix);
965 reg_write16(priv, REG_NLINE_MSB, n_line);
966 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
967 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
968 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
969 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
970 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
971 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
972 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
973 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
974 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
975 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
976 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
977 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
978 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
979 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
980 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
981 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -0600982
983 if (priv->rev == TDA19988) {
984 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100985 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600986 }
987
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100988 /*
989 * Always generate sync polarity relative to input sync and
990 * revert input stage toggled sync at output stage
991 */
992 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
993 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
994 reg |= TBG_CNTRL_1_H_TGL;
995 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
996 reg |= TBG_CNTRL_1_V_TGL;
997 reg_write(priv, REG_TBG_CNTRL_1, reg);
998
Rob Clarke7792ce2013-01-08 19:21:02 -0600999 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001000 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001001
1002 /* Only setup the info frames if the sink is HDMI */
1003 if (priv->is_hdmi_sink) {
1004 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001005 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1006 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001007 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1008 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001009
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001010 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001011
1012 if (priv->params.audio_cfg)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001013 tda998x_configure_audio(priv, adjusted_mode,
Russell Kingc4c11dd2013-08-14 21:43:30 +02001014 &priv->params);
1015 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001016}
1017
1018static enum drm_connector_status
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001019tda998x_encoder_detect(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001020{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001021 uint8_t val = cec_read(priv, REG_CEC_RXSHPDLEV);
1022
Rob Clarke7792ce2013-01-08 19:21:02 -06001023 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1024 connector_status_disconnected;
1025}
1026
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001027static int read_edid_block(struct tda998x_priv *priv, uint8_t *buf, int blk)
Rob Clarke7792ce2013-01-08 19:21:02 -06001028{
1029 uint8_t offset, segptr;
1030 int ret, i;
1031
Rob Clarke7792ce2013-01-08 19:21:02 -06001032 offset = (blk & 1) ? 128 : 0;
1033 segptr = blk / 2;
1034
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001035 reg_write(priv, REG_DDC_ADDR, 0xa0);
1036 reg_write(priv, REG_DDC_OFFS, offset);
1037 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1038 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001039
1040 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001041 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001042 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001043
1044 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001045 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001046
1047 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001048 if (priv->hdmi->irq) {
1049 i = wait_event_timeout(priv->wq_edid,
1050 !priv->wq_edid_wait,
1051 msecs_to_jiffies(100));
1052 if (i < 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001053 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001054 return i;
1055 }
1056 } else {
Russell King713456d2014-03-03 14:09:36 +00001057 for (i = 100; i > 0; i--) {
1058 msleep(1);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001059 ret = reg_read(priv, REG_INT_FLAGS_2);
1060 if (ret < 0)
1061 return ret;
1062 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1063 break;
1064 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001065 }
1066
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001067 if (i == 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001068 dev_err(&priv->hdmi->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001069 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001070 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001071
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001072 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, EDID_LENGTH);
Rob Clarke7792ce2013-01-08 19:21:02 -06001073 if (ret != EDID_LENGTH) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001074 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1075 blk, ret);
Rob Clarke7792ce2013-01-08 19:21:02 -06001076 return ret;
1077 }
1078
Rob Clarke7792ce2013-01-08 19:21:02 -06001079 return 0;
1080}
1081
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001082static uint8_t *do_get_edid(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001083{
Jean-Francois Moine704d63f2014-01-25 18:14:46 +01001084 int j, valid_extensions = 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001085 uint8_t *block, *new;
1086 bool print_bad_edid = drm_debug & DRM_UT_KMS;
1087
1088 if ((block = kmalloc(EDID_LENGTH, GFP_KERNEL)) == NULL)
1089 return NULL;
1090
Russell King063b4722013-08-14 21:43:26 +02001091 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001092 reg_clear(priv, REG_TX4, TX4_PD_RAM);
Russell King063b4722013-08-14 21:43:26 +02001093
Rob Clarke7792ce2013-01-08 19:21:02 -06001094 /* base block fetch */
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001095 if (read_edid_block(priv, block, 0))
Rob Clarke7792ce2013-01-08 19:21:02 -06001096 goto fail;
1097
1098 if (!drm_edid_block_valid(block, 0, print_bad_edid))
1099 goto fail;
1100
1101 /* if there's no extensions, we're done */
1102 if (block[0x7e] == 0)
Russell King063b4722013-08-14 21:43:26 +02001103 goto done;
Rob Clarke7792ce2013-01-08 19:21:02 -06001104
1105 new = krealloc(block, (block[0x7e] + 1) * EDID_LENGTH, GFP_KERNEL);
1106 if (!new)
1107 goto fail;
1108 block = new;
1109
1110 for (j = 1; j <= block[0x7e]; j++) {
1111 uint8_t *ext_block = block + (valid_extensions + 1) * EDID_LENGTH;
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001112 if (read_edid_block(priv, ext_block, j))
Rob Clarke7792ce2013-01-08 19:21:02 -06001113 goto fail;
1114
1115 if (!drm_edid_block_valid(ext_block, j, print_bad_edid))
1116 goto fail;
1117
1118 valid_extensions++;
1119 }
1120
1121 if (valid_extensions != block[0x7e]) {
1122 block[EDID_LENGTH-1] += block[0x7e] - valid_extensions;
1123 block[0x7e] = valid_extensions;
1124 new = krealloc(block, (valid_extensions + 1) * EDID_LENGTH, GFP_KERNEL);
1125 if (!new)
1126 goto fail;
1127 block = new;
1128 }
1129
Russell King063b4722013-08-14 21:43:26 +02001130done:
1131 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001132 reg_set(priv, REG_TX4, TX4_PD_RAM);
Russell King063b4722013-08-14 21:43:26 +02001133
Rob Clarke7792ce2013-01-08 19:21:02 -06001134 return block;
1135
1136fail:
Russell King063b4722013-08-14 21:43:26 +02001137 if (priv->rev == TDA19988)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001138 reg_set(priv, REG_TX4, TX4_PD_RAM);
Russell King5e7fe2f2014-02-07 19:13:23 +00001139 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001140 kfree(block);
1141 return NULL;
1142}
1143
1144static int
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001145tda998x_encoder_get_modes(struct tda998x_priv *priv,
1146 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001147{
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001148 struct edid *edid = (struct edid *)do_get_edid(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001149 int n = 0;
1150
1151 if (edid) {
1152 drm_mode_connector_update_edid_property(connector, edid);
1153 n = drm_add_edid_modes(connector, edid);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001154 priv->is_hdmi_sink = drm_detect_hdmi_monitor(edid);
Rob Clarke7792ce2013-01-08 19:21:02 -06001155 kfree(edid);
1156 }
1157
1158 return n;
1159}
1160
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001161static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1162 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001163{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001164 if (priv->hdmi->irq)
1165 connector->polled = DRM_CONNECTOR_POLL_HPD;
1166 else
1167 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1168 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001169}
1170
1171static int
1172tda998x_encoder_set_property(struct drm_encoder *encoder,
1173 struct drm_connector *connector,
1174 struct drm_property *property,
1175 uint64_t val)
1176{
1177 DBG("");
1178 return 0;
1179}
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);
1186 if (priv->hdmi->irq)
1187 free_irq(priv->hdmi->irq, priv);
1188
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001189 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001190}
1191
1192/* Slave encoder support */
1193
1194static void
1195tda998x_encoder_slave_set_config(struct drm_encoder *encoder, void *params)
1196{
1197 tda998x_encoder_set_config(to_tda998x_priv(encoder), params);
1198}
1199
1200static void tda998x_encoder_slave_destroy(struct drm_encoder *encoder)
1201{
1202 struct tda998x_priv *priv = to_tda998x_priv(encoder);
1203
1204 tda998x_destroy(priv);
Guido Martínez2e48cec2014-06-17 11:17:03 -03001205 drm_i2c_encoder_destroy(encoder);
Rob Clarke7792ce2013-01-08 19:21:02 -06001206 kfree(priv);
1207}
1208
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001209static void tda998x_encoder_slave_dpms(struct drm_encoder *encoder, int mode)
1210{
1211 tda998x_encoder_dpms(to_tda998x_priv(encoder), mode);
1212}
1213
1214static int tda998x_encoder_slave_mode_valid(struct drm_encoder *encoder,
1215 struct drm_display_mode *mode)
1216{
1217 return tda998x_encoder_mode_valid(to_tda998x_priv(encoder), mode);
1218}
1219
1220static void
1221tda998x_encoder_slave_mode_set(struct drm_encoder *encoder,
1222 struct drm_display_mode *mode,
1223 struct drm_display_mode *adjusted_mode)
1224{
1225 tda998x_encoder_mode_set(to_tda998x_priv(encoder), mode, adjusted_mode);
1226}
1227
1228static enum drm_connector_status
1229tda998x_encoder_slave_detect(struct drm_encoder *encoder,
1230 struct drm_connector *connector)
1231{
1232 return tda998x_encoder_detect(to_tda998x_priv(encoder));
1233}
1234
1235static int tda998x_encoder_slave_get_modes(struct drm_encoder *encoder,
1236 struct drm_connector *connector)
1237{
1238 return tda998x_encoder_get_modes(to_tda998x_priv(encoder), connector);
1239}
1240
1241static int
1242tda998x_encoder_slave_create_resources(struct drm_encoder *encoder,
1243 struct drm_connector *connector)
1244{
1245 tda998x_encoder_set_polling(to_tda998x_priv(encoder), connector);
1246 return 0;
1247}
1248
1249static struct drm_encoder_slave_funcs tda998x_encoder_slave_funcs = {
1250 .set_config = tda998x_encoder_slave_set_config,
1251 .destroy = tda998x_encoder_slave_destroy,
1252 .dpms = tda998x_encoder_slave_dpms,
Rob Clarke7792ce2013-01-08 19:21:02 -06001253 .save = tda998x_encoder_save,
1254 .restore = tda998x_encoder_restore,
1255 .mode_fixup = tda998x_encoder_mode_fixup,
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001256 .mode_valid = tda998x_encoder_slave_mode_valid,
1257 .mode_set = tda998x_encoder_slave_mode_set,
1258 .detect = tda998x_encoder_slave_detect,
1259 .get_modes = tda998x_encoder_slave_get_modes,
1260 .create_resources = tda998x_encoder_slave_create_resources,
Rob Clarke7792ce2013-01-08 19:21:02 -06001261 .set_property = tda998x_encoder_set_property,
1262};
1263
1264/* I2C driver functions */
1265
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001266static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001267{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001268 struct device_node *np = client->dev.of_node;
1269 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001270 int rev_lo, rev_hi, ret;
Rob Clarke7792ce2013-01-08 19:21:02 -06001271
Russell King5e74c222013-08-14 21:43:29 +02001272 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1273 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1274 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1275
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001276 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001277 priv->hdmi = client;
Rob Clarke7792ce2013-01-08 19:21:02 -06001278 priv->cec = i2c_new_dummy(client->adapter, 0x34);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001279 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001280 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001281
Rob Clarke7792ce2013-01-08 19:21:02 -06001282 priv->dpms = DRM_MODE_DPMS_OFF;
1283
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001284 mutex_init(&priv->mutex); /* protect the page access */
1285
Rob Clarke7792ce2013-01-08 19:21:02 -06001286 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001287 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001288 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1289
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001290 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001291
1292 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001293 rev_lo = reg_read(priv, REG_VERSION_LSB);
1294 rev_hi = reg_read(priv, REG_VERSION_MSB);
1295 if (rev_lo < 0 || rev_hi < 0) {
1296 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001297 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001298 }
1299
1300 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001301
1302 /* mask off feature bits: */
1303 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1304
1305 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001306 case TDA9989N2:
1307 dev_info(&client->dev, "found TDA9989 n2");
1308 break;
1309 case TDA19989:
1310 dev_info(&client->dev, "found TDA19989");
1311 break;
1312 case TDA19989N2:
1313 dev_info(&client->dev, "found TDA19989 n2");
1314 break;
1315 case TDA19988:
1316 dev_info(&client->dev, "found TDA19988");
1317 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001318 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001319 dev_err(&client->dev, "found unsupported device: %04x\n",
1320 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001321 goto fail;
1322 }
1323
1324 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001325 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001326
1327 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001328 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001329
1330 /* if necessary, disable multi-master: */
1331 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001332 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001333
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001334 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001335 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1336
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001337 /* initialize the optional IRQ */
1338 if (client->irq) {
1339 int irqf_trigger;
1340
1341 /* init read EDID waitqueue */
1342 init_waitqueue_head(&priv->wq_edid);
1343
1344 /* clear pending interrupts */
1345 reg_read(priv, REG_INT_FLAGS_0);
1346 reg_read(priv, REG_INT_FLAGS_1);
1347 reg_read(priv, REG_INT_FLAGS_2);
1348
1349 irqf_trigger =
1350 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1351 ret = request_threaded_irq(client->irq, NULL,
1352 tda998x_irq_thread,
1353 irqf_trigger | IRQF_ONESHOT,
1354 "tda998x", priv);
1355 if (ret) {
1356 dev_err(&client->dev,
1357 "failed to request IRQ#%u: %d\n",
1358 client->irq, ret);
1359 goto fail;
1360 }
1361
1362 /* enable HPD irq */
1363 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1364 }
1365
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001366 /* enable EDID read irq: */
1367 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1368
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001369 if (!np)
1370 return 0; /* non-DT */
1371
1372 /* get the optional video properties */
1373 ret = of_property_read_u32(np, "video-ports", &video);
1374 if (ret == 0) {
1375 priv->vip_cntrl_0 = video >> 16;
1376 priv->vip_cntrl_1 = video >> 8;
1377 priv->vip_cntrl_2 = video;
1378 }
1379
Rob Clarke7792ce2013-01-08 19:21:02 -06001380 return 0;
1381
1382fail:
1383 /* if encoder_init fails, the encoder slave is never registered,
1384 * so cleanup here:
1385 */
1386 if (priv->cec)
1387 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001388 return -ENXIO;
1389}
1390
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001391static int tda998x_encoder_init(struct i2c_client *client,
1392 struct drm_device *dev,
1393 struct drm_encoder_slave *encoder_slave)
1394{
1395 struct tda998x_priv *priv;
1396 int ret;
1397
1398 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1399 if (!priv)
1400 return -ENOMEM;
1401
1402 priv->encoder = &encoder_slave->base;
1403
1404 ret = tda998x_create(client, priv);
1405 if (ret) {
1406 kfree(priv);
1407 return ret;
1408 }
1409
1410 encoder_slave->slave_priv = priv;
1411 encoder_slave->slave_funcs = &tda998x_encoder_slave_funcs;
1412
1413 return 0;
1414}
1415
Russell Kingc707c362014-02-07 19:49:44 +00001416struct tda998x_priv2 {
1417 struct tda998x_priv base;
1418 struct drm_encoder encoder;
1419 struct drm_connector connector;
1420};
1421
1422#define conn_to_tda998x_priv2(x) \
1423 container_of(x, struct tda998x_priv2, connector);
1424
1425#define enc_to_tda998x_priv2(x) \
1426 container_of(x, struct tda998x_priv2, encoder);
1427
1428static void tda998x_encoder2_dpms(struct drm_encoder *encoder, int mode)
1429{
1430 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1431
1432 tda998x_encoder_dpms(&priv->base, mode);
1433}
1434
1435static void tda998x_encoder_prepare(struct drm_encoder *encoder)
1436{
1437 tda998x_encoder2_dpms(encoder, DRM_MODE_DPMS_OFF);
1438}
1439
1440static void tda998x_encoder_commit(struct drm_encoder *encoder)
1441{
1442 tda998x_encoder2_dpms(encoder, DRM_MODE_DPMS_ON);
1443}
1444
1445static void tda998x_encoder2_mode_set(struct drm_encoder *encoder,
1446 struct drm_display_mode *mode,
1447 struct drm_display_mode *adjusted_mode)
1448{
1449 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1450
1451 tda998x_encoder_mode_set(&priv->base, mode, adjusted_mode);
1452}
1453
1454static const struct drm_encoder_helper_funcs tda998x_encoder_helper_funcs = {
1455 .dpms = tda998x_encoder2_dpms,
1456 .save = tda998x_encoder_save,
1457 .restore = tda998x_encoder_restore,
1458 .mode_fixup = tda998x_encoder_mode_fixup,
1459 .prepare = tda998x_encoder_prepare,
1460 .commit = tda998x_encoder_commit,
1461 .mode_set = tda998x_encoder2_mode_set,
1462};
1463
1464static void tda998x_encoder_destroy(struct drm_encoder *encoder)
1465{
1466 struct tda998x_priv2 *priv = enc_to_tda998x_priv2(encoder);
1467
1468 tda998x_destroy(&priv->base);
1469 drm_encoder_cleanup(encoder);
1470}
1471
1472static const struct drm_encoder_funcs tda998x_encoder_funcs = {
1473 .destroy = tda998x_encoder_destroy,
1474};
1475
1476static int tda998x_connector_get_modes(struct drm_connector *connector)
1477{
1478 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1479
1480 return tda998x_encoder_get_modes(&priv->base, connector);
1481}
1482
1483static int tda998x_connector_mode_valid(struct drm_connector *connector,
1484 struct drm_display_mode *mode)
1485{
1486 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1487
1488 return tda998x_encoder_mode_valid(&priv->base, mode);
1489}
1490
1491static struct drm_encoder *
1492tda998x_connector_best_encoder(struct drm_connector *connector)
1493{
1494 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1495
1496 return &priv->encoder;
1497}
1498
1499static
1500const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
1501 .get_modes = tda998x_connector_get_modes,
1502 .mode_valid = tda998x_connector_mode_valid,
1503 .best_encoder = tda998x_connector_best_encoder,
1504};
1505
1506static enum drm_connector_status
1507tda998x_connector_detect(struct drm_connector *connector, bool force)
1508{
1509 struct tda998x_priv2 *priv = conn_to_tda998x_priv2(connector);
1510
1511 return tda998x_encoder_detect(&priv->base);
1512}
1513
1514static void tda998x_connector_destroy(struct drm_connector *connector)
1515{
Dave Airlie74cd62e2014-08-05 10:34:33 +10001516 drm_connector_unregister(connector);
Russell Kingc707c362014-02-07 19:49:44 +00001517 drm_connector_cleanup(connector);
1518}
1519
1520static const struct drm_connector_funcs tda998x_connector_funcs = {
1521 .dpms = drm_helper_connector_dpms,
1522 .fill_modes = drm_helper_probe_single_connector_modes,
1523 .detect = tda998x_connector_detect,
1524 .destroy = tda998x_connector_destroy,
1525};
1526
1527static int tda998x_bind(struct device *dev, struct device *master, void *data)
1528{
1529 struct tda998x_encoder_params *params = dev->platform_data;
1530 struct i2c_client *client = to_i2c_client(dev);
1531 struct drm_device *drm = data;
1532 struct tda998x_priv2 *priv;
1533 int ret;
1534
1535 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1536 if (!priv)
1537 return -ENOMEM;
1538
1539 dev_set_drvdata(dev, priv);
1540
1541 priv->base.encoder = &priv->encoder;
1542 priv->connector.interlace_allowed = 1;
1543 priv->encoder.possible_crtcs = 1 << 0;
1544
1545 ret = tda998x_create(client, &priv->base);
1546 if (ret)
1547 return ret;
1548
1549 if (!dev->of_node && params)
1550 tda998x_encoder_set_config(&priv->base, params);
1551
1552 tda998x_encoder_set_polling(&priv->base, &priv->connector);
1553
1554 drm_encoder_helper_add(&priv->encoder, &tda998x_encoder_helper_funcs);
1555 ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
1556 DRM_MODE_ENCODER_TMDS);
1557 if (ret)
1558 goto err_encoder;
1559
1560 drm_connector_helper_add(&priv->connector,
1561 &tda998x_connector_helper_funcs);
1562 ret = drm_connector_init(drm, &priv->connector,
1563 &tda998x_connector_funcs,
1564 DRM_MODE_CONNECTOR_HDMIA);
1565 if (ret)
1566 goto err_connector;
1567
Dave Airlie74cd62e2014-08-05 10:34:33 +10001568 ret = drm_connector_register(&priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001569 if (ret)
1570 goto err_sysfs;
1571
1572 priv->connector.encoder = &priv->encoder;
1573 drm_mode_connector_attach_encoder(&priv->connector, &priv->encoder);
1574
1575 return 0;
1576
1577err_sysfs:
1578 drm_connector_cleanup(&priv->connector);
1579err_connector:
1580 drm_encoder_cleanup(&priv->encoder);
1581err_encoder:
1582 tda998x_destroy(&priv->base);
1583 return ret;
1584}
1585
1586static void tda998x_unbind(struct device *dev, struct device *master,
1587 void *data)
1588{
1589 struct tda998x_priv2 *priv = dev_get_drvdata(dev);
1590
1591 drm_connector_cleanup(&priv->connector);
1592 drm_encoder_cleanup(&priv->encoder);
1593 tda998x_destroy(&priv->base);
1594}
1595
1596static const struct component_ops tda998x_ops = {
1597 .bind = tda998x_bind,
1598 .unbind = tda998x_unbind,
1599};
1600
1601static int
1602tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1603{
1604 return component_add(&client->dev, &tda998x_ops);
1605}
1606
1607static int tda998x_remove(struct i2c_client *client)
1608{
1609 component_del(&client->dev, &tda998x_ops);
1610 return 0;
1611}
1612
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001613#ifdef CONFIG_OF
1614static const struct of_device_id tda998x_dt_ids[] = {
1615 { .compatible = "nxp,tda998x", },
1616 { }
1617};
1618MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1619#endif
1620
Rob Clarke7792ce2013-01-08 19:21:02 -06001621static struct i2c_device_id tda998x_ids[] = {
1622 { "tda998x", 0 },
1623 { }
1624};
1625MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1626
1627static struct drm_i2c_encoder_driver tda998x_driver = {
1628 .i2c_driver = {
1629 .probe = tda998x_probe,
1630 .remove = tda998x_remove,
1631 .driver = {
1632 .name = "tda998x",
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001633 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001634 },
1635 .id_table = tda998x_ids,
1636 },
1637 .encoder_init = tda998x_encoder_init,
1638};
1639
1640/* Module initialization */
1641
1642static int __init
1643tda998x_init(void)
1644{
1645 DBG("");
1646 return drm_i2c_encoder_register(THIS_MODULE, &tda998x_driver);
1647}
1648
1649static void __exit
1650tda998x_exit(void)
1651{
1652 DBG("");
1653 drm_i2c_encoder_unregister(&tda998x_driver);
1654}
1655
1656MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1657MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1658MODULE_LICENSE("GPL");
1659
1660module_init(tda998x_init);
1661module_exit(tda998x_exit);