blob: f23c1a8d0e33e515b44fc95a88685ed13ad3372c [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>
Jyri Sarha7e567622016-08-09 22:00:05 +030023#include <sound/hdmi-codec.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060024
25#include <drm/drmP.h>
Liviu Dudau (ARM)9736e9882015-11-23 16:52:42 +010026#include <drm/drm_atomic_helper.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060027#include <drm/drm_crtc_helper.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060028#include <drm/drm_edid.h>
Russell King5dbcf312014-06-15 11:11:10 +010029#include <drm/drm_of.h>
Russell Kingc4c11dd2013-08-14 21:43:30 +020030#include <drm/i2c/tda998x.h>
Rob Clarke7792ce2013-01-08 19:21:02 -060031
32#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
33
Jyri Sarha7e567622016-08-09 22:00:05 +030034struct tda998x_audio_port {
35 u8 format; /* AFMT_xxx */
36 u8 config; /* AP value */
37};
38
Rob Clarke7792ce2013-01-08 19:21:02 -060039struct tda998x_priv {
40 struct i2c_client *cec;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +010041 struct i2c_client *hdmi;
Jean-Francois Moineed9a8422014-11-29 08:30:51 +010042 struct mutex mutex;
Russell Kinge66e03a2015-06-06 21:41:10 +010043 u16 rev;
44 u8 current_page;
Rob Clarke7792ce2013-01-08 19:21:02 -060045 int dpms;
Russell King896a4132016-10-23 11:32:42 +010046 bool supports_infoframes;
Russell King8f3f21f2016-11-02 21:15:04 +000047 bool sink_has_audio;
Russell King5e74c222013-08-14 21:43:29 +020048 u8 vip_cntrl_0;
49 u8 vip_cntrl_1;
50 u8 vip_cntrl_2;
Russell King319e6582016-10-23 11:32:43 +010051 unsigned long tmds_clock;
Jyri Sarha95db3b22016-08-09 22:00:04 +030052 struct tda998x_audio_params audio_params;
Jean-Francois Moine12473b72014-01-25 18:14:38 +010053
Jyri Sarha7e567622016-08-09 22:00:05 +030054 struct platform_device *audio_pdev;
55 struct mutex audio_mutex;
56
Jean-Francois Moine12473b72014-01-25 18:14:38 +010057 wait_queue_head_t wq_edid;
58 volatile int wq_edid_wait;
Russell King0fc6f442015-06-06 21:41:09 +010059
60 struct work_struct detect_work;
61 struct timer_list edid_delay_timer;
62 wait_queue_head_t edid_delay_waitq;
63 bool edid_delay_active;
Russell King78e401f2015-08-14 11:17:12 +010064
65 struct drm_encoder encoder;
Russell Kingeed64b52015-08-14 11:18:28 +010066 struct drm_connector connector;
Jyri Sarha7e567622016-08-09 22:00:05 +030067
68 struct tda998x_audio_port audio_port[2];
Rob Clarke7792ce2013-01-08 19:21:02 -060069};
70
Russell King9525c4d2015-08-14 11:28:53 +010071#define conn_to_tda998x_priv(x) \
72 container_of(x, struct tda998x_priv, connector)
73
74#define enc_to_tda998x_priv(x) \
75 container_of(x, struct tda998x_priv, encoder)
76
Rob Clarke7792ce2013-01-08 19:21:02 -060077/* The TDA9988 series of devices use a paged register scheme.. to simplify
78 * things we encode the page # in upper bits of the register #. To read/
79 * write a given register, we need to make sure CURPAGE register is set
80 * appropriately. Which implies reads/writes are not atomic. Fun!
81 */
82
83#define REG(page, addr) (((page) << 8) | (addr))
84#define REG2ADDR(reg) ((reg) & 0xff)
85#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
86
87#define REG_CURPAGE 0xff /* write */
88
89
90/* Page 00h: General Control */
91#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
92#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
93# define MAIN_CNTRL0_SR (1 << 0)
94# define MAIN_CNTRL0_DECS (1 << 1)
95# define MAIN_CNTRL0_DEHS (1 << 2)
96# define MAIN_CNTRL0_CECS (1 << 3)
97# define MAIN_CNTRL0_CEHS (1 << 4)
98# define MAIN_CNTRL0_SCALER (1 << 7)
99#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
100#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
101# define SOFTRESET_AUDIO (1 << 0)
102# define SOFTRESET_I2C_MASTER (1 << 1)
103#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
104#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
105#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
106# define I2C_MASTER_DIS_MM (1 << 0)
107# define I2C_MASTER_DIS_FILT (1 << 1)
108# define I2C_MASTER_APP_STRT_LAT (1 << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200109#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
110# define FEAT_POWERDOWN_SPDIF (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600111#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
112#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
113#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
114# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200115#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600116#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
117#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
118#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
119#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
120#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
121# define VIP_CNTRL_0_MIRR_A (1 << 7)
122# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
123# define VIP_CNTRL_0_MIRR_B (1 << 3)
124# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
125#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
126# define VIP_CNTRL_1_MIRR_C (1 << 7)
127# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
128# define VIP_CNTRL_1_MIRR_D (1 << 3)
129# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
130#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
131# define VIP_CNTRL_2_MIRR_E (1 << 7)
132# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
133# define VIP_CNTRL_2_MIRR_F (1 << 3)
134# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
135#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
136# define VIP_CNTRL_3_X_TGL (1 << 0)
137# define VIP_CNTRL_3_H_TGL (1 << 1)
138# define VIP_CNTRL_3_V_TGL (1 << 2)
139# define VIP_CNTRL_3_EMB (1 << 3)
140# define VIP_CNTRL_3_SYNC_DE (1 << 4)
141# define VIP_CNTRL_3_SYNC_HS (1 << 5)
142# define VIP_CNTRL_3_DE_INT (1 << 6)
143# define VIP_CNTRL_3_EDGE (1 << 7)
144#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
145# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
146# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
147# define VIP_CNTRL_4_CCIR656 (1 << 4)
148# define VIP_CNTRL_4_656_ALT (1 << 5)
149# define VIP_CNTRL_4_TST_656 (1 << 6)
150# define VIP_CNTRL_4_TST_PAT (1 << 7)
151#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
152# define VIP_CNTRL_5_CKCASE (1 << 0)
153# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200154#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100155# define MUX_AP_SELECT_I2S 0x64
156# define MUX_AP_SELECT_SPDIF 0x40
Russell Kingbcb24812013-08-14 21:43:27 +0200157#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600158#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
159# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
160# define MAT_CONTRL_MAT_BP (1 << 2)
161#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
162#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
163#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
164#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
165#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
166#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
167#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
168#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
169#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
170#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
171#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
172#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
173#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
174#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
175#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
176#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
177#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200178#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
179#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600180#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
181#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200182#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
183#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600184#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
185#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
186#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
187#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
188#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
189#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
190#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
191#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
192#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
193#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200194#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
195#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
196#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
197#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600198#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
199#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
200#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
201#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
202#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200203# define TBG_CNTRL_0_TOP_TGL (1 << 0)
204# define TBG_CNTRL_0_TOP_SEL (1 << 1)
205# define TBG_CNTRL_0_DE_EXT (1 << 2)
206# define TBG_CNTRL_0_TOP_EXT (1 << 3)
Rob Clarke7792ce2013-01-08 19:21:02 -0600207# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
208# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
209# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
210#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200211# define TBG_CNTRL_1_H_TGL (1 << 0)
212# define TBG_CNTRL_1_V_TGL (1 << 1)
213# define TBG_CNTRL_1_TGL_EN (1 << 2)
214# define TBG_CNTRL_1_X_EXT (1 << 3)
215# define TBG_CNTRL_1_H_EXT (1 << 4)
216# define TBG_CNTRL_1_V_EXT (1 << 5)
Rob Clarke7792ce2013-01-08 19:21:02 -0600217# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
218#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
219#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
220# define HVF_CNTRL_0_SM (1 << 7)
221# define HVF_CNTRL_0_RWB (1 << 6)
222# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
223# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
224#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
225# define HVF_CNTRL_1_FOR (1 << 0)
226# define HVF_CNTRL_1_YUVBLK (1 << 1)
227# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
228# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
229# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
230#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200231#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
232# define I2S_FORMAT(x) (((x) & 3) << 0)
233#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100234# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
235# define AIP_CLKSEL_AIP_I2S (1 << 3)
236# define AIP_CLKSEL_FS_ACLK (0 << 0)
237# define AIP_CLKSEL_FS_MCLK (1 << 0)
238# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600239
240/* Page 02h: PLL settings */
241#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
242# define PLL_SERIAL_1_SRL_FDN (1 << 0)
243# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
244# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
245#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100246# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
Rob Clarke7792ce2013-01-08 19:21:02 -0600247# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
248#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
249# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
250# define PLL_SERIAL_3_SRL_DE (1 << 2)
251# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
252#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
253#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
254#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
255#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
256#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
257#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
258#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
259#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
260#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200261# define AUDIO_DIV_SERCLK_1 0
262# define AUDIO_DIV_SERCLK_2 1
263# define AUDIO_DIV_SERCLK_4 2
264# define AUDIO_DIV_SERCLK_8 3
265# define AUDIO_DIV_SERCLK_16 4
266# define AUDIO_DIV_SERCLK_32 5
Rob Clarke7792ce2013-01-08 19:21:02 -0600267#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
268# define SEL_CLK_SEL_CLK1 (1 << 0)
269# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
270# define SEL_CLK_ENA_SC_CLK (1 << 3)
271#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
272
273
274/* Page 09h: EDID Control */
275#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
276/* next 127 successive registers are the EDID block */
277#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
278#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
279#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
280#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
281#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
282
283
284/* Page 10h: information frames and packets */
Russell Kingc4c11dd2013-08-14 21:43:30 +0200285#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
286#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
287#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
288#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
289#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600290
291
292/* Page 11h: audio settings and content info packets */
293#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
294# define AIP_CNTRL_0_RST_FIFO (1 << 0)
295# define AIP_CNTRL_0_SWAP (1 << 1)
296# define AIP_CNTRL_0_LAYOUT (1 << 2)
297# define AIP_CNTRL_0_ACR_MAN (1 << 5)
298# define AIP_CNTRL_0_RST_CTS (1 << 6)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200299#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
300# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
301# define CA_I2S_HBR_CHSTAT (1 << 6)
302#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
303#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
304#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
305#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
306#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
307#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
308#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
309#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
310# define CTS_N_K(x) (((x) & 7) << 0)
311# define CTS_N_M(x) (((x) & 3) << 4)
Rob Clarke7792ce2013-01-08 19:21:02 -0600312#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
313# define ENC_CNTRL_RST_ENC (1 << 0)
314# define ENC_CNTRL_RST_SEL (1 << 1)
315# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200316#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
317# define DIP_FLAGS_ACR (1 << 0)
318# define DIP_FLAGS_GC (1 << 1)
319#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
320# define DIP_IF_FLAGS_IF1 (1 << 1)
321# define DIP_IF_FLAGS_IF2 (1 << 2)
322# define DIP_IF_FLAGS_IF3 (1 << 3)
323# define DIP_IF_FLAGS_IF4 (1 << 4)
324# define DIP_IF_FLAGS_IF5 (1 << 5)
325#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
Rob Clarke7792ce2013-01-08 19:21:02 -0600326
327
328/* Page 12h: HDCP and OTP */
329#define REG_TX3 REG(0x12, 0x9a) /* read/write */
Russell King063b4722013-08-14 21:43:26 +0200330#define REG_TX4 REG(0x12, 0x9b) /* read/write */
331# define TX4_PD_RAM (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600332#define REG_TX33 REG(0x12, 0xb8) /* read/write */
333# define TX33_HDMI (1 << 1)
334
335
336/* Page 13h: Gamut related metadata packets */
337
338
339
340/* CEC registers: (not paged)
341 */
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100342#define REG_CEC_INTSTATUS 0xee /* read */
343# define CEC_INTSTATUS_CEC (1 << 0)
344# define CEC_INTSTATUS_HDMI (1 << 1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600345#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
346# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
347# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
348# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
349# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100350#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
351#define REG_CEC_RXSHPDINT 0xfd /* read */
Russell Kingec5d3e82015-06-06 21:41:10 +0100352# define CEC_RXSHPDINT_RXSENS BIT(0)
353# define CEC_RXSHPDINT_HPD BIT(1)
Rob Clarke7792ce2013-01-08 19:21:02 -0600354#define REG_CEC_RXSHPDLEV 0xfe /* read */
355# define CEC_RXSHPDLEV_RXSENS (1 << 0)
356# define CEC_RXSHPDLEV_HPD (1 << 1)
357
358#define REG_CEC_ENAMODS 0xff /* read/write */
359# define CEC_ENAMODS_DIS_FRO (1 << 6)
360# define CEC_ENAMODS_DIS_CCLK (1 << 5)
361# define CEC_ENAMODS_EN_RXSENS (1 << 2)
362# define CEC_ENAMODS_EN_HDMI (1 << 1)
363# define CEC_ENAMODS_EN_CEC (1 << 0)
364
365
366/* Device versions: */
367#define TDA9989N2 0x0101
368#define TDA19989 0x0201
369#define TDA19989N2 0x0202
370#define TDA19988 0x0301
371
372static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100373cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600374{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100375 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100376 u8 buf[] = {addr, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600377 int ret;
378
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100379 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600380 if (ret < 0)
381 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
382}
383
Russell Kinge66e03a2015-06-06 21:41:10 +0100384static u8
385cec_read(struct tda998x_priv *priv, u8 addr)
Rob Clarke7792ce2013-01-08 19:21:02 -0600386{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100387 struct i2c_client *client = priv->cec;
Russell Kinge66e03a2015-06-06 21:41:10 +0100388 u8 val;
Rob Clarke7792ce2013-01-08 19:21:02 -0600389 int ret;
390
391 ret = i2c_master_send(client, &addr, sizeof(addr));
392 if (ret < 0)
393 goto fail;
394
395 ret = i2c_master_recv(client, &val, sizeof(val));
396 if (ret < 0)
397 goto fail;
398
399 return val;
400
401fail:
402 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
403 return 0;
404}
405
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100406static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100407set_page(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600408{
Rob Clarke7792ce2013-01-08 19:21:02 -0600409 if (REG2PAGE(reg) != priv->current_page) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100410 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100411 u8 buf[] = {
Rob Clarke7792ce2013-01-08 19:21:02 -0600412 REG_CURPAGE, REG2PAGE(reg)
413 };
414 int ret = i2c_master_send(client, buf, sizeof(buf));
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100415 if (ret < 0) {
Julia Lawall288ffc72014-12-07 20:20:59 +0100416 dev_err(&client->dev, "%s %04x err %d\n", __func__,
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100417 reg, ret);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100418 return ret;
419 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600420
421 priv->current_page = REG2PAGE(reg);
422 }
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100423 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -0600424}
425
426static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100427reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
Rob Clarke7792ce2013-01-08 19:21:02 -0600428{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100429 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100430 u8 addr = REG2ADDR(reg);
Rob Clarke7792ce2013-01-08 19:21:02 -0600431 int ret;
432
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100433 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100434 ret = set_page(priv, reg);
435 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100436 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600437
438 ret = i2c_master_send(client, &addr, sizeof(addr));
439 if (ret < 0)
440 goto fail;
441
442 ret = i2c_master_recv(client, buf, cnt);
443 if (ret < 0)
444 goto fail;
445
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100446 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600447
448fail:
449 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100450out:
451 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600452 return ret;
453}
454
Russell Kingc4c11dd2013-08-14 21:43:30 +0200455static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100456reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200457{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100458 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100459 u8 buf[cnt+1];
Russell Kingc4c11dd2013-08-14 21:43:30 +0200460 int ret;
461
462 buf[0] = REG2ADDR(reg);
463 memcpy(&buf[1], p, cnt);
464
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100465 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100466 ret = set_page(priv, reg);
467 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100468 goto out;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200469
470 ret = i2c_master_send(client, buf, cnt + 1);
471 if (ret < 0)
472 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100473out:
474 mutex_unlock(&priv->mutex);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200475}
476
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100477static int
Russell Kinge66e03a2015-06-06 21:41:10 +0100478reg_read(struct tda998x_priv *priv, u16 reg)
Rob Clarke7792ce2013-01-08 19:21:02 -0600479{
Russell Kinge66e03a2015-06-06 21:41:10 +0100480 u8 val = 0;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100481 int ret;
482
483 ret = reg_read_range(priv, reg, &val, sizeof(val));
484 if (ret < 0)
485 return ret;
Rob Clarke7792ce2013-01-08 19:21:02 -0600486 return val;
487}
488
489static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100490reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600491{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100492 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100493 u8 buf[] = {REG2ADDR(reg), val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600494 int ret;
495
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100496 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100497 ret = set_page(priv, reg);
498 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100499 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600500
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100501 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600502 if (ret < 0)
503 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100504out:
505 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600506}
507
508static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100509reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600510{
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100511 struct i2c_client *client = priv->hdmi;
Russell Kinge66e03a2015-06-06 21:41:10 +0100512 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
Rob Clarke7792ce2013-01-08 19:21:02 -0600513 int ret;
514
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100515 mutex_lock(&priv->mutex);
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100516 ret = set_page(priv, reg);
517 if (ret < 0)
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100518 goto out;
Rob Clarke7792ce2013-01-08 19:21:02 -0600519
Jean-Francois Moine704d63f2014-01-25 18:14:46 +0100520 ret = i2c_master_send(client, buf, sizeof(buf));
Rob Clarke7792ce2013-01-08 19:21:02 -0600521 if (ret < 0)
522 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +0100523out:
524 mutex_unlock(&priv->mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -0600525}
526
527static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100528reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600529{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100530 int old_val;
531
532 old_val = reg_read(priv, reg);
533 if (old_val >= 0)
534 reg_write(priv, reg, old_val | val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600535}
536
537static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100538reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
Rob Clarke7792ce2013-01-08 19:21:02 -0600539{
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +0100540 int old_val;
541
542 old_val = reg_read(priv, reg);
543 if (old_val >= 0)
544 reg_write(priv, reg, old_val & ~val);
Rob Clarke7792ce2013-01-08 19:21:02 -0600545}
546
547static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100548tda998x_reset(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -0600549{
550 /* reset audio and i2c master: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100551 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
Rob Clarke7792ce2013-01-08 19:21:02 -0600552 msleep(50);
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100553 reg_write(priv, REG_SOFTRESET, 0);
Rob Clarke7792ce2013-01-08 19:21:02 -0600554 msleep(50);
555
556 /* reset transmitter: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100557 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
558 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
Rob Clarke7792ce2013-01-08 19:21:02 -0600559
560 /* PLL registers common configuration */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100561 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
562 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
563 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
564 reg_write(priv, REG_SERIALIZER, 0x00);
565 reg_write(priv, REG_BUFFER_OUT, 0x00);
566 reg_write(priv, REG_PLL_SCG1, 0x00);
567 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
568 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
569 reg_write(priv, REG_PLL_SCGN1, 0xfa);
570 reg_write(priv, REG_PLL_SCGN2, 0x00);
571 reg_write(priv, REG_PLL_SCGR1, 0x5b);
572 reg_write(priv, REG_PLL_SCGR2, 0x00);
573 reg_write(priv, REG_PLL_SCG2, 0x10);
Russell Kingbcb24812013-08-14 21:43:27 +0200574
575 /* Write the default value MUX register */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100576 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
Rob Clarke7792ce2013-01-08 19:21:02 -0600577}
578
Russell King0fc6f442015-06-06 21:41:09 +0100579/*
580 * The TDA998x has a problem when trying to read the EDID close to a
581 * HPD assertion: it needs a delay of 100ms to avoid timing out while
582 * trying to read EDID data.
583 *
584 * However, tda998x_encoder_get_modes() may be called at any moment
Russell King9525c4d2015-08-14 11:28:53 +0100585 * after tda998x_connector_detect() indicates that we are connected, so
Russell King0fc6f442015-06-06 21:41:09 +0100586 * we need to delay probing modes in tda998x_encoder_get_modes() after
587 * we have seen a HPD inactive->active transition. This code implements
588 * that delay.
589 */
590static void tda998x_edid_delay_done(unsigned long data)
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100591{
Russell King0fc6f442015-06-06 21:41:09 +0100592 struct tda998x_priv *priv = (struct tda998x_priv *)data;
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100593
Russell King0fc6f442015-06-06 21:41:09 +0100594 priv->edid_delay_active = false;
595 wake_up(&priv->edid_delay_waitq);
596 schedule_work(&priv->detect_work);
597}
598
599static void tda998x_edid_delay_start(struct tda998x_priv *priv)
600{
601 priv->edid_delay_active = true;
602 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
603}
604
605static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
606{
607 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
608}
609
610/*
611 * We need to run the KMS hotplug event helper outside of our threaded
612 * interrupt routine as this can call back into our get_modes method,
613 * which will want to make use of interrupts.
614 */
615static void tda998x_detect_work(struct work_struct *work)
616{
617 struct tda998x_priv *priv =
618 container_of(work, struct tda998x_priv, detect_work);
Russell King78e401f2015-08-14 11:17:12 +0100619 struct drm_device *dev = priv->encoder.dev;
Russell King0fc6f442015-06-06 21:41:09 +0100620
621 if (dev)
622 drm_kms_helper_hotplug_event(dev);
Jean-Francois Moine6833d262014-11-29 08:57:15 +0100623}
624
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100625/*
626 * only 2 interrupts may occur: screen plug/unplug and EDID read
627 */
628static irqreturn_t tda998x_irq_thread(int irq, void *data)
629{
630 struct tda998x_priv *priv = data;
631 u8 sta, cec, lvl, flag0, flag1, flag2;
Russell Kingf84a97d2015-06-06 21:41:09 +0100632 bool handled = false;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100633
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100634 sta = cec_read(priv, REG_CEC_INTSTATUS);
635 cec = cec_read(priv, REG_CEC_RXSHPDINT);
636 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
637 flag0 = reg_read(priv, REG_INT_FLAGS_0);
638 flag1 = reg_read(priv, REG_INT_FLAGS_1);
639 flag2 = reg_read(priv, REG_INT_FLAGS_2);
640 DRM_DEBUG_DRIVER(
641 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
642 sta, cec, lvl, flag0, flag1, flag2);
Russell Kingec5d3e82015-06-06 21:41:10 +0100643
644 if (cec & CEC_RXSHPDINT_HPD) {
Russell King0fc6f442015-06-06 21:41:09 +0100645 if (lvl & CEC_RXSHPDLEV_HPD)
646 tda998x_edid_delay_start(priv);
647 else
648 schedule_work(&priv->detect_work);
649
Russell Kingf84a97d2015-06-06 21:41:09 +0100650 handled = true;
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100651 }
Russell Kingec5d3e82015-06-06 21:41:10 +0100652
653 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
654 priv->wq_edid_wait = 0;
655 wake_up(&priv->wq_edid);
656 handled = true;
657 }
658
Russell Kingf84a97d2015-06-06 21:41:09 +0100659 return IRQ_RETVAL(handled);
Jean-Francois Moine12473b72014-01-25 18:14:38 +0100660}
661
Russell Kingc4c11dd2013-08-14 21:43:30 +0200662static void
Russell Kinge66e03a2015-06-06 21:41:10 +0100663tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
Russell King96795df2015-08-06 10:52:05 +0100664 union hdmi_infoframe *frame)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200665{
Russell King96795df2015-08-06 10:52:05 +0100666 u8 buf[32];
667 ssize_t len;
668
669 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
670 if (len < 0) {
671 dev_err(&priv->hdmi->dev,
672 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
673 frame->any.type, len);
674 return;
675 }
676
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100677 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
Russell King96795df2015-08-06 10:52:05 +0100678 reg_write_range(priv, addr, buf, len);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100679 reg_set(priv, REG_DIP_IF_FLAGS, bit);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200680}
681
Jyri Sarha95db3b22016-08-09 22:00:04 +0300682static int tda998x_write_aif(struct tda998x_priv *priv,
683 struct hdmi_audio_infoframe *cea)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200684{
Russell King96795df2015-08-06 10:52:05 +0100685 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200686
Jyri Sarha95db3b22016-08-09 22:00:04 +0300687 frame.audio = *cea;
Russell King96795df2015-08-06 10:52:05 +0100688
689 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
Jyri Sarha95db3b22016-08-09 22:00:04 +0300690
691 return 0;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200692}
693
694static void
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100695tda998x_write_avi(struct tda998x_priv *priv, struct drm_display_mode *mode)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200696{
Russell King96795df2015-08-06 10:52:05 +0100697 union hdmi_infoframe frame;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200698
Russell King96795df2015-08-06 10:52:05 +0100699 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode);
700 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200701
Russell King96795df2015-08-06 10:52:05 +0100702 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200703}
704
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100705static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200706{
707 if (on) {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100708 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
709 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
710 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200711 } else {
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100712 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200713 }
714}
715
Jyri Sarha95db3b22016-08-09 22:00:04 +0300716static int
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100717tda998x_configure_audio(struct tda998x_priv *priv,
Russell King319e6582016-10-23 11:32:43 +0100718 struct tda998x_audio_params *params)
Russell Kingc4c11dd2013-08-14 21:43:30 +0200719{
Russell Kinge66e03a2015-06-06 21:41:10 +0100720 u8 buf[6], clksel_aip, clksel_fs, cts_n, adiv;
721 u32 n;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200722
723 /* Enable audio ports */
Jyri Sarha95db3b22016-08-09 22:00:04 +0300724 reg_write(priv, REG_ENA_AP, params->config);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200725
726 /* Set audio input source */
Jyri Sarha95db3b22016-08-09 22:00:04 +0300727 switch (params->format) {
Russell Kingc4c11dd2013-08-14 21:43:30 +0200728 case AFMT_SPDIF:
Jyri Sarha95db3b22016-08-09 22:00:04 +0300729 reg_write(priv, REG_ENA_ACLK, 0);
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100730 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_SPDIF);
731 clksel_aip = AIP_CLKSEL_AIP_SPDIF;
732 clksel_fs = AIP_CLKSEL_FS_FS64SPDIF;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200733 cts_n = CTS_N_M(3) | CTS_N_K(3);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200734 break;
735
736 case AFMT_I2S:
Jyri Sarha95db3b22016-08-09 22:00:04 +0300737 reg_write(priv, REG_ENA_ACLK, 1);
Jean-Francois Moine10df1a92014-01-25 18:14:40 +0100738 reg_write(priv, REG_MUX_AP, MUX_AP_SELECT_I2S);
739 clksel_aip = AIP_CLKSEL_AIP_I2S;
740 clksel_fs = AIP_CLKSEL_FS_ACLK;
Jyri Sarha95db3b22016-08-09 22:00:04 +0300741 switch (params->sample_width) {
742 case 16:
743 cts_n = CTS_N_M(3) | CTS_N_K(1);
744 break;
745 case 18:
746 case 20:
747 case 24:
748 cts_n = CTS_N_M(3) | CTS_N_K(2);
749 break;
750 default:
751 case 32:
752 cts_n = CTS_N_M(3) | CTS_N_K(3);
753 break;
754 }
Russell Kingc4c11dd2013-08-14 21:43:30 +0200755 break;
David Herrmann3b288022013-09-01 15:23:04 +0200756
757 default:
Jyri Sarha7e567622016-08-09 22:00:05 +0300758 dev_err(&priv->hdmi->dev, "Unsupported I2S format\n");
Jyri Sarha95db3b22016-08-09 22:00:04 +0300759 return -EINVAL;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200760 }
761
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100762 reg_write(priv, REG_AIP_CLKSEL, clksel_aip);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100763 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
764 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100765 reg_write(priv, REG_CTS_N, cts_n);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200766
767 /*
768 * Audio input somehow depends on HDMI line rate which is
769 * related to pixclk. Testing showed that modes with pixclk
770 * >100MHz need a larger divider while <40MHz need the default.
771 * There is no detailed info in the datasheet, so we just
772 * assume 100MHz requires larger divider.
773 */
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100774 adiv = AUDIO_DIV_SERCLK_8;
Russell King319e6582016-10-23 11:32:43 +0100775 if (priv->tmds_clock > 100000)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100776 adiv++; /* AUDIO_DIV_SERCLK_16 */
777
778 /* S/PDIF asks for a larger divider */
Jyri Sarha95db3b22016-08-09 22:00:04 +0300779 if (params->format == AFMT_SPDIF)
Jean-Francois Moine2470fec2014-01-25 18:14:36 +0100780 adiv++; /* AUDIO_DIV_SERCLK_16 or _32 */
781
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100782 reg_write(priv, REG_AUDIO_DIV, adiv);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200783
784 /*
785 * This is the approximate value of N, which happens to be
786 * the recommended values for non-coherent clocks.
787 */
Jyri Sarha95db3b22016-08-09 22:00:04 +0300788 n = 128 * params->sample_rate / 1000;
Russell Kingc4c11dd2013-08-14 21:43:30 +0200789
790 /* Write the CTS and N values */
791 buf[0] = 0x44;
792 buf[1] = 0x42;
793 buf[2] = 0x01;
794 buf[3] = n;
795 buf[4] = n >> 8;
796 buf[5] = n >> 16;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100797 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200798
799 /* Set CTS clock reference */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100800 reg_write(priv, REG_AIP_CLKSEL, clksel_aip | clksel_fs);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200801
802 /* Reset CTS generator */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100803 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
804 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200805
Jyri Sarha95db3b22016-08-09 22:00:04 +0300806 /* Write the channel status
807 * The REG_CH_STAT_B-registers skip IEC958 AES2 byte, because
808 * there is a separate register for each I2S wire.
809 */
810 buf[0] = params->status[0];
811 buf[1] = params->status[1];
812 buf[2] = params->status[3];
813 buf[3] = params->status[4];
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100814 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200815
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100816 tda998x_audio_mute(priv, true);
Jean-Francois Moine73d5e252014-01-25 18:14:44 +0100817 msleep(20);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100818 tda998x_audio_mute(priv, false);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200819
Jyri Sarha95db3b22016-08-09 22:00:04 +0300820 return tda998x_write_aif(priv, &params->cea);
Russell Kingc4c11dd2013-08-14 21:43:30 +0200821}
822
Rob Clarke7792ce2013-01-08 19:21:02 -0600823/* DRM encoder functions */
824
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000825static void tda998x_encoder_set_config(struct tda998x_priv *priv,
826 const struct tda998x_encoder_params *p)
Rob Clarke7792ce2013-01-08 19:21:02 -0600827{
Russell Kingc4c11dd2013-08-14 21:43:30 +0200828 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
829 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
830 VIP_CNTRL_0_SWAP_B(p->swap_b) |
831 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
832 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
833 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
834 VIP_CNTRL_1_SWAP_D(p->swap_d) |
835 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
836 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
837 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
838 VIP_CNTRL_2_SWAP_F(p->swap_f) |
839 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
840
Jyri Sarha95db3b22016-08-09 22:00:04 +0300841 priv->audio_params = p->audio_params;
Rob Clarke7792ce2013-01-08 19:21:02 -0600842}
843
Russell King9525c4d2015-08-14 11:28:53 +0100844static void tda998x_encoder_dpms(struct drm_encoder *encoder, int mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600845{
Russell King9525c4d2015-08-14 11:28:53 +0100846 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
847
Rob Clarke7792ce2013-01-08 19:21:02 -0600848 /* we only care about on or off: */
849 if (mode != DRM_MODE_DPMS_ON)
850 mode = DRM_MODE_DPMS_OFF;
851
852 if (mode == priv->dpms)
853 return;
854
855 switch (mode) {
856 case DRM_MODE_DPMS_ON:
Russell Kingc4c11dd2013-08-14 21:43:30 +0200857 /* enable video ports, audio will be enabled later */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100858 reg_write(priv, REG_ENA_VP_0, 0xff);
859 reg_write(priv, REG_ENA_VP_1, 0xff);
860 reg_write(priv, REG_ENA_VP_2, 0xff);
Rob Clarke7792ce2013-01-08 19:21:02 -0600861 /* set muxing after enabling ports: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100862 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
863 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
864 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
Rob Clarke7792ce2013-01-08 19:21:02 -0600865 break;
866 case DRM_MODE_DPMS_OFF:
Russell Kingdb6aaf42013-09-24 10:37:13 +0100867 /* disable video ports */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100868 reg_write(priv, REG_ENA_VP_0, 0x00);
869 reg_write(priv, REG_ENA_VP_1, 0x00);
870 reg_write(priv, REG_ENA_VP_2, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -0600871 break;
872 }
873
874 priv->dpms = mode;
875}
876
Russell King9525c4d2015-08-14 11:28:53 +0100877static int tda998x_connector_mode_valid(struct drm_connector *connector,
878 struct drm_display_mode *mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600879{
Liviu Dudau (ARM)e4618c42015-11-23 16:52:41 +0100880 /* TDA19988 dotclock can go up to 165MHz */
881 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
882
883 if (mode->clock > ((priv->rev == TDA19988) ? 165000 : 150000))
Russell King92fbdfc2014-02-07 19:52:33 +0000884 return MODE_CLOCK_HIGH;
885 if (mode->htotal >= BIT(13))
886 return MODE_BAD_HVALUE;
887 if (mode->vtotal >= BIT(11))
888 return MODE_BAD_VVALUE;
Rob Clarke7792ce2013-01-08 19:21:02 -0600889 return MODE_OK;
890}
891
892static void
Russell King9525c4d2015-08-14 11:28:53 +0100893tda998x_encoder_mode_set(struct drm_encoder *encoder,
Russell Kinga8f4d4d62014-02-07 19:17:21 +0000894 struct drm_display_mode *mode,
895 struct drm_display_mode *adjusted_mode)
Rob Clarke7792ce2013-01-08 19:21:02 -0600896{
Russell King9525c4d2015-08-14 11:28:53 +0100897 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
Russell Kinge66e03a2015-06-06 21:41:10 +0100898 u16 ref_pix, ref_line, n_pix, n_line;
899 u16 hs_pix_s, hs_pix_e;
900 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
901 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
902 u16 vwin1_line_s, vwin1_line_e;
903 u16 vwin2_line_s, vwin2_line_e;
904 u16 de_pix_s, de_pix_e;
905 u8 reg, div, rep;
Rob Clarke7792ce2013-01-08 19:21:02 -0600906
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200907 /*
908 * Internally TDA998x is using ITU-R BT.656 style sync but
909 * we get VESA style sync. TDA998x is using a reference pixel
910 * relative to ITU to sync to the input frame and for output
911 * sync generation. Currently, we are using reference detection
912 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
913 * which is position of rising VS with coincident rising HS.
914 *
915 * Now there is some issues to take care of:
916 * - HDMI data islands require sync-before-active
917 * - TDA998x register values must be > 0 to be enabled
918 * - REFLINE needs an additional offset of +1
919 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
920 *
921 * So we add +1 to all horizontal and vertical register values,
922 * plus an additional +3 for REFPIX as we are using RGB input only.
Rob Clarke7792ce2013-01-08 19:21:02 -0600923 */
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200924 n_pix = mode->htotal;
925 n_line = mode->vtotal;
Rob Clarke7792ce2013-01-08 19:21:02 -0600926
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200927 hs_pix_e = mode->hsync_end - mode->hdisplay;
928 hs_pix_s = mode->hsync_start - mode->hdisplay;
929 de_pix_e = mode->htotal;
930 de_pix_s = mode->htotal - mode->hdisplay;
931 ref_pix = 3 + hs_pix_s;
932
Sebastian Hesselbarth179f1aa2013-08-14 21:43:32 +0200933 /*
934 * Attached LCD controllers may generate broken sync. Allow
935 * those to adjust the position of the rising VS edge by adding
936 * HSKEW to ref_pix.
937 */
938 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
939 ref_pix += adjusted_mode->hskew;
940
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +0200941 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
942 ref_line = 1 + mode->vsync_start - mode->vdisplay;
943 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
944 vwin1_line_e = vwin1_line_s + mode->vdisplay;
945 vs1_pix_s = vs1_pix_e = hs_pix_s;
946 vs1_line_s = mode->vsync_start - mode->vdisplay;
947 vs1_line_e = vs1_line_s +
948 mode->vsync_end - mode->vsync_start;
949 vwin2_line_s = vwin2_line_e = 0;
950 vs2_pix_s = vs2_pix_e = 0;
951 vs2_line_s = vs2_line_e = 0;
952 } else {
953 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
954 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
955 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
956 vs1_pix_s = vs1_pix_e = hs_pix_s;
957 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
958 vs1_line_e = vs1_line_s +
959 (mode->vsync_end - mode->vsync_start)/2;
960 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
961 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
962 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
963 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
964 vs2_line_e = vs2_line_s +
965 (mode->vsync_end - mode->vsync_start)/2;
966 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600967
968 div = 148500 / mode->clock;
Jean-Francois Moine3ae471f2014-01-25 18:14:36 +0100969 if (div != 0) {
970 div--;
971 if (div > 3)
972 div = 3;
973 }
Rob Clarke7792ce2013-01-08 19:21:02 -0600974
Russell King2cae8e02016-11-02 21:38:34 +0000975 mutex_lock(&priv->audio_mutex);
976
Rob Clarke7792ce2013-01-08 19:21:02 -0600977 /* mute the audio FIFO: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100978 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
Rob Clarke7792ce2013-01-08 19:21:02 -0600979
980 /* set HDMI HDCP mode off: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +0100981 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100982 reg_clear(priv, REG_TX33, TX33_HDMI);
983 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600984
Rob Clarke7792ce2013-01-08 19:21:02 -0600985 /* no pre-filter or interpolator: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100986 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600987 HVF_CNTRL_0_INTPOL(0));
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100988 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
989 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -0600990 VIP_CNTRL_4_BLC(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600991
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100992 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
Jean-Francois Moinea8b517e2014-01-25 18:14:39 +0100993 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
994 PLL_SERIAL_3_SRL_DE);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +0100995 reg_write(priv, REG_SERIALIZER, 0);
996 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
Rob Clarke7792ce2013-01-08 19:21:02 -0600997
998 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
999 rep = 0;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001000 reg_write(priv, REG_RPT_CNTRL, 0);
1001 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_VRF_CLK(0) |
Rob Clarke7792ce2013-01-08 19:21:02 -06001002 SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
1003
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001004 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
Rob Clarke7792ce2013-01-08 19:21:02 -06001005 PLL_SERIAL_2_SRL_PR(rep));
1006
Rob Clarke7792ce2013-01-08 19:21:02 -06001007 /* set color matrix bypass flag: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001008 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
1009 MAT_CONTRL_MAT_SC(1));
Rob Clarke7792ce2013-01-08 19:21:02 -06001010
1011 /* set BIAS tmds value: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001012 reg_write(priv, REG_ANA_GENERAL, 0x09);
Rob Clarke7792ce2013-01-08 19:21:02 -06001013
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001014 /*
1015 * Sync on rising HSYNC/VSYNC
1016 */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001017 reg = VIP_CNTRL_3_SYNC_HS;
Sebastian Hesselbarth088d61d2013-08-14 21:43:31 +02001018
1019 /*
1020 * TDA19988 requires high-active sync at input stage,
1021 * so invert low-active sync provided by master encoder here
1022 */
1023 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001024 reg |= VIP_CNTRL_3_H_TGL;
Rob Clarke7792ce2013-01-08 19:21:02 -06001025 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001026 reg |= VIP_CNTRL_3_V_TGL;
1027 reg_write(priv, REG_VIP_CNTRL_3, reg);
Rob Clarke7792ce2013-01-08 19:21:02 -06001028
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001029 reg_write(priv, REG_VIDFORMAT, 0x00);
1030 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1031 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1032 reg_write16(priv, REG_NPIX_MSB, n_pix);
1033 reg_write16(priv, REG_NLINE_MSB, n_line);
1034 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1035 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1036 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1037 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1038 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1039 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1040 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1041 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1042 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1043 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1044 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1045 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1046 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1047 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1048 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1049 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
Rob Clarke7792ce2013-01-08 19:21:02 -06001050
1051 if (priv->rev == TDA19988) {
1052 /* let incoming pixels fill the active space (if any) */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001053 reg_write(priv, REG_ENABLE_SPACE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001054 }
1055
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001056 /*
1057 * Always generate sync polarity relative to input sync and
1058 * revert input stage toggled sync at output stage
1059 */
1060 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1061 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1062 reg |= TBG_CNTRL_1_H_TGL;
1063 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1064 reg |= TBG_CNTRL_1_V_TGL;
1065 reg_write(priv, REG_TBG_CNTRL_1, reg);
1066
Rob Clarke7792ce2013-01-08 19:21:02 -06001067 /* must be last register set: */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001068 reg_write(priv, REG_TBG_CNTRL_0, 0);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001069
Russell King319e6582016-10-23 11:32:43 +01001070 priv->tmds_clock = adjusted_mode->clock;
1071
Russell King896a4132016-10-23 11:32:42 +01001072 /* CEA-861B section 6 says that:
1073 * CEA version 1 (CEA-861) has no support for infoframes.
1074 * CEA version 2 (CEA-861A) supports version 1 AVI infoframes,
1075 * and optional basic audio.
1076 * CEA version 3 (CEA-861B) supports version 1 and 2 AVI infoframes,
1077 * and optional digital audio, with audio infoframes.
1078 *
1079 * Since we only support generation of version 2 AVI infoframes,
1080 * ignore CEA version 2 and below (iow, behave as if we're a
1081 * CEA-861 source.)
1082 */
1083 priv->supports_infoframes = priv->connector.display_info.cea_rev >= 3;
1084
1085 if (priv->supports_infoframes) {
Russell Kingc4c11dd2013-08-14 21:43:30 +02001086 /* We need to turn HDMI HDCP stuff on to get audio through */
Jean-Francois Moine81b53a12014-01-25 18:14:42 +01001087 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1088 reg_write(priv, REG_TBG_CNTRL_1, reg);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001089 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1090 reg_set(priv, REG_TX33, TX33_HDMI);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001091
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001092 tda998x_write_avi(priv, adjusted_mode);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001093
Russell King8f3f21f2016-11-02 21:15:04 +00001094 if (priv->audio_params.format != AFMT_UNUSED &&
1095 priv->sink_has_audio)
Russell King319e6582016-10-23 11:32:43 +01001096 tda998x_configure_audio(priv, &priv->audio_params);
Russell Kingc4c11dd2013-08-14 21:43:30 +02001097 }
Russell King319e6582016-10-23 11:32:43 +01001098
1099 mutex_unlock(&priv->audio_mutex);
Rob Clarke7792ce2013-01-08 19:21:02 -06001100}
1101
Russell King8f3f21f2016-11-02 21:15:04 +00001102static int tda998x_connector_fill_modes(struct drm_connector *connector,
1103 uint32_t maxX, uint32_t maxY)
1104{
1105 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
1106 int ret;
1107
1108 ret = drm_helper_probe_single_connector_modes(connector, maxX, maxY);
1109
1110 if (connector->edid_blob_ptr) {
1111 struct edid *edid = (void *)connector->edid_blob_ptr->data;
1112
1113 priv->sink_has_audio = drm_detect_monitor_audio(edid);
1114 } else {
1115 priv->sink_has_audio = false;
1116 }
1117
1118 return ret;
1119}
1120
Rob Clarke7792ce2013-01-08 19:21:02 -06001121static enum drm_connector_status
Russell King9525c4d2015-08-14 11:28:53 +01001122tda998x_connector_detect(struct drm_connector *connector, bool force)
Rob Clarke7792ce2013-01-08 19:21:02 -06001123{
Russell King9525c4d2015-08-14 11:28:53 +01001124 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
Russell Kinge66e03a2015-06-06 21:41:10 +01001125 u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001126
Rob Clarke7792ce2013-01-08 19:21:02 -06001127 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1128 connector_status_disconnected;
1129}
1130
Laurent Pinchart07259f82015-01-16 18:37:43 +02001131static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
Rob Clarke7792ce2013-01-08 19:21:02 -06001132{
Laurent Pinchart07259f82015-01-16 18:37:43 +02001133 struct tda998x_priv *priv = data;
Russell Kinge66e03a2015-06-06 21:41:10 +01001134 u8 offset, segptr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001135 int ret, i;
1136
Rob Clarke7792ce2013-01-08 19:21:02 -06001137 offset = (blk & 1) ? 128 : 0;
1138 segptr = blk / 2;
1139
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001140 reg_write(priv, REG_DDC_ADDR, 0xa0);
1141 reg_write(priv, REG_DDC_OFFS, offset);
1142 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1143 reg_write(priv, REG_DDC_SEGM, segptr);
Rob Clarke7792ce2013-01-08 19:21:02 -06001144
1145 /* enable reading EDID: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001146 priv->wq_edid_wait = 1;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001147 reg_write(priv, REG_EDID_CTRL, 0x1);
Rob Clarke7792ce2013-01-08 19:21:02 -06001148
1149 /* flag must be cleared by sw: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001150 reg_write(priv, REG_EDID_CTRL, 0x0);
Rob Clarke7792ce2013-01-08 19:21:02 -06001151
1152 /* wait for block read to complete: */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001153 if (priv->hdmi->irq) {
1154 i = wait_event_timeout(priv->wq_edid,
1155 !priv->wq_edid_wait,
1156 msecs_to_jiffies(100));
1157 if (i < 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001158 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001159 return i;
1160 }
1161 } else {
Russell King713456d2014-03-03 14:09:36 +00001162 for (i = 100; i > 0; i--) {
1163 msleep(1);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001164 ret = reg_read(priv, REG_INT_FLAGS_2);
1165 if (ret < 0)
1166 return ret;
1167 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1168 break;
1169 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001170 }
1171
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001172 if (i == 0) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001173 dev_err(&priv->hdmi->dev, "read edid timeout\n");
Rob Clarke7792ce2013-01-08 19:21:02 -06001174 return -ETIMEDOUT;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001175 }
Rob Clarke7792ce2013-01-08 19:21:02 -06001176
Laurent Pinchart07259f82015-01-16 18:37:43 +02001177 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1178 if (ret != length) {
Russell King5e7fe2f2014-02-07 19:13:23 +00001179 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1180 blk, ret);
Rob Clarke7792ce2013-01-08 19:21:02 -06001181 return ret;
1182 }
1183
Rob Clarke7792ce2013-01-08 19:21:02 -06001184 return 0;
1185}
1186
Russell King9525c4d2015-08-14 11:28:53 +01001187static int tda998x_connector_get_modes(struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001188{
Russell King9525c4d2015-08-14 11:28:53 +01001189 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
Laurent Pinchart07259f82015-01-16 18:37:43 +02001190 struct edid *edid;
1191 int n;
Rob Clarke7792ce2013-01-08 19:21:02 -06001192
Russell King0fc6f442015-06-06 21:41:09 +01001193 /*
1194 * If we get killed while waiting for the HPD timeout, return
1195 * no modes found: we are not in a restartable path, so we
1196 * can't handle signals gracefully.
1197 */
1198 if (tda998x_edid_delay_wait(priv))
1199 return 0;
1200
Laurent Pinchart07259f82015-01-16 18:37:43 +02001201 if (priv->rev == TDA19988)
1202 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1203
1204 edid = drm_do_get_edid(connector, read_edid_block, priv);
1205
1206 if (priv->rev == TDA19988)
1207 reg_set(priv, REG_TX4, TX4_PD_RAM);
1208
1209 if (!edid) {
1210 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1211 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001212 }
1213
Laurent Pinchart07259f82015-01-16 18:37:43 +02001214 drm_mode_connector_update_edid_property(connector, edid);
1215 n = drm_add_edid_modes(connector, edid);
Jyri Sarha7e567622016-08-09 22:00:05 +03001216 drm_edid_to_eld(connector, edid);
1217
Laurent Pinchart07259f82015-01-16 18:37:43 +02001218 kfree(edid);
1219
Rob Clarke7792ce2013-01-08 19:21:02 -06001220 return n;
1221}
1222
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001223static void tda998x_encoder_set_polling(struct tda998x_priv *priv,
1224 struct drm_connector *connector)
Rob Clarke7792ce2013-01-08 19:21:02 -06001225{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001226 if (priv->hdmi->irq)
1227 connector->polled = DRM_CONNECTOR_POLL_HPD;
1228 else
1229 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1230 DRM_CONNECTOR_POLL_DISCONNECT;
Rob Clarke7792ce2013-01-08 19:21:02 -06001231}
1232
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001233static void tda998x_destroy(struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001234{
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001235 /* disable all IRQs and free the IRQ handler */
1236 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1237 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
Russell King0fc6f442015-06-06 21:41:09 +01001238
Jyri Sarha7e567622016-08-09 22:00:05 +03001239 if (priv->audio_pdev)
1240 platform_device_unregister(priv->audio_pdev);
1241
Russell King0fc6f442015-06-06 21:41:09 +01001242 if (priv->hdmi->irq)
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001243 free_irq(priv->hdmi->irq, priv);
Russell King0fc6f442015-06-06 21:41:09 +01001244
1245 del_timer_sync(&priv->edid_delay_timer);
1246 cancel_work_sync(&priv->detect_work);
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001247
Jean-Francois Moine89fc8682014-07-07 17:59:51 +02001248 i2c_unregister_device(priv->cec);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001249}
1250
Jyri Sarha7e567622016-08-09 22:00:05 +03001251static int tda998x_audio_hw_params(struct device *dev, void *data,
1252 struct hdmi_codec_daifmt *daifmt,
1253 struct hdmi_codec_params *params)
1254{
1255 struct tda998x_priv *priv = dev_get_drvdata(dev);
1256 int i, ret;
1257 struct tda998x_audio_params audio = {
1258 .sample_width = params->sample_width,
1259 .sample_rate = params->sample_rate,
1260 .cea = params->cea,
1261 };
1262
Jyri Sarha7e567622016-08-09 22:00:05 +03001263 memcpy(audio.status, params->iec.status,
1264 min(sizeof(audio.status), sizeof(params->iec.status)));
1265
1266 switch (daifmt->fmt) {
1267 case HDMI_I2S:
1268 if (daifmt->bit_clk_inv || daifmt->frame_clk_inv ||
1269 daifmt->bit_clk_master || daifmt->frame_clk_master) {
1270 dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
1271 daifmt->bit_clk_inv, daifmt->frame_clk_inv,
1272 daifmt->bit_clk_master,
1273 daifmt->frame_clk_master);
1274 return -EINVAL;
1275 }
1276 for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
1277 if (priv->audio_port[i].format == AFMT_I2S)
1278 audio.config = priv->audio_port[i].config;
1279 audio.format = AFMT_I2S;
1280 break;
1281 case HDMI_SPDIF:
1282 for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++)
1283 if (priv->audio_port[i].format == AFMT_SPDIF)
1284 audio.config = priv->audio_port[i].config;
1285 audio.format = AFMT_SPDIF;
1286 break;
1287 default:
1288 dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
1289 return -EINVAL;
1290 }
1291
1292 if (audio.config == 0) {
1293 dev_err(dev, "%s: No audio configutation found\n", __func__);
1294 return -EINVAL;
1295 }
1296
1297 mutex_lock(&priv->audio_mutex);
Russell King8f3f21f2016-11-02 21:15:04 +00001298 if (priv->supports_infoframes && priv->sink_has_audio)
Russell King896a4132016-10-23 11:32:42 +01001299 ret = tda998x_configure_audio(priv, &audio);
1300 else
1301 ret = 0;
Jyri Sarha7e567622016-08-09 22:00:05 +03001302
1303 if (ret == 0)
1304 priv->audio_params = audio;
1305 mutex_unlock(&priv->audio_mutex);
1306
1307 return ret;
1308}
1309
1310static void tda998x_audio_shutdown(struct device *dev, void *data)
1311{
1312 struct tda998x_priv *priv = dev_get_drvdata(dev);
1313
1314 mutex_lock(&priv->audio_mutex);
1315
1316 reg_write(priv, REG_ENA_AP, 0);
1317
1318 priv->audio_params.format = AFMT_UNUSED;
1319
1320 mutex_unlock(&priv->audio_mutex);
1321}
1322
1323int tda998x_audio_digital_mute(struct device *dev, void *data, bool enable)
1324{
1325 struct tda998x_priv *priv = dev_get_drvdata(dev);
1326
1327 mutex_lock(&priv->audio_mutex);
1328
1329 tda998x_audio_mute(priv, enable);
1330
1331 mutex_unlock(&priv->audio_mutex);
1332 return 0;
1333}
1334
1335static int tda998x_audio_get_eld(struct device *dev, void *data,
1336 uint8_t *buf, size_t len)
1337{
1338 struct tda998x_priv *priv = dev_get_drvdata(dev);
1339 struct drm_mode_config *config = &priv->encoder.dev->mode_config;
1340 struct drm_connector *connector;
1341 int ret = -ENODEV;
1342
1343 mutex_lock(&config->mutex);
1344 list_for_each_entry(connector, &config->connector_list, head) {
1345 if (&priv->encoder == connector->encoder) {
1346 memcpy(buf, connector->eld,
1347 min(sizeof(connector->eld), len));
1348 ret = 0;
1349 }
1350 }
1351 mutex_unlock(&config->mutex);
1352
1353 return ret;
1354}
1355
1356static const struct hdmi_codec_ops audio_codec_ops = {
1357 .hw_params = tda998x_audio_hw_params,
1358 .audio_shutdown = tda998x_audio_shutdown,
1359 .digital_mute = tda998x_audio_digital_mute,
1360 .get_eld = tda998x_audio_get_eld,
1361};
1362
1363static int tda998x_audio_codec_init(struct tda998x_priv *priv,
1364 struct device *dev)
1365{
1366 struct hdmi_codec_pdata codec_data = {
1367 .ops = &audio_codec_ops,
1368 .max_i2s_channels = 2,
1369 };
1370 int i;
1371
1372 for (i = 0; i < ARRAY_SIZE(priv->audio_port); i++) {
1373 if (priv->audio_port[i].format == AFMT_I2S &&
1374 priv->audio_port[i].config != 0)
1375 codec_data.i2s = 1;
1376 if (priv->audio_port[i].format == AFMT_SPDIF &&
1377 priv->audio_port[i].config != 0)
1378 codec_data.spdif = 1;
1379 }
1380
1381 priv->audio_pdev = platform_device_register_data(
1382 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
1383 &codec_data, sizeof(codec_data));
1384
1385 return PTR_ERR_OR_ZERO(priv->audio_pdev);
1386}
1387
Rob Clarke7792ce2013-01-08 19:21:02 -06001388/* I2C driver functions */
1389
Jyri Sarha7e567622016-08-09 22:00:05 +03001390static int tda998x_get_audio_ports(struct tda998x_priv *priv,
1391 struct device_node *np)
1392{
1393 const u32 *port_data;
1394 u32 size;
1395 int i;
1396
1397 port_data = of_get_property(np, "audio-ports", &size);
1398 if (!port_data)
1399 return 0;
1400
1401 size /= sizeof(u32);
1402 if (size > 2 * ARRAY_SIZE(priv->audio_port) || size % 2 != 0) {
1403 dev_err(&priv->hdmi->dev,
1404 "Bad number of elements in audio-ports dt-property\n");
1405 return -EINVAL;
1406 }
1407
1408 size /= 2;
1409
1410 for (i = 0; i < size; i++) {
1411 u8 afmt = be32_to_cpup(&port_data[2*i]);
1412 u8 ena_ap = be32_to_cpup(&port_data[2*i+1]);
1413
1414 if (afmt != AFMT_SPDIF && afmt != AFMT_I2S) {
1415 dev_err(&priv->hdmi->dev,
1416 "Bad audio format %u\n", afmt);
1417 return -EINVAL;
1418 }
1419
1420 priv->audio_port[i].format = afmt;
1421 priv->audio_port[i].config = ena_ap;
1422 }
1423
1424 if (priv->audio_port[0].format == priv->audio_port[1].format) {
1425 dev_err(&priv->hdmi->dev,
1426 "There can only be on I2S port and one SPDIF port\n");
1427 return -EINVAL;
1428 }
1429 return 0;
1430}
1431
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001432static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
Rob Clarke7792ce2013-01-08 19:21:02 -06001433{
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001434 struct device_node *np = client->dev.of_node;
1435 u32 video;
Russell Kingfb7544d2014-02-02 16:18:24 +00001436 int rev_lo, rev_hi, ret;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001437 unsigned short cec_addr;
Rob Clarke7792ce2013-01-08 19:21:02 -06001438
Russell Kingba300c12016-11-17 23:55:00 +00001439 mutex_init(&priv->audio_mutex); /* Protect access from audio thread */
1440
Russell King5e74c222013-08-14 21:43:29 +02001441 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1442 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1443 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1444
Jean-Francois Moine2eb4c7b2014-01-25 18:14:45 +01001445 priv->current_page = 0xff;
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001446 priv->hdmi = client;
Andrew Jacksoncfe38752014-11-07 08:31:25 +00001447 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1448 cec_addr = 0x34 + (client->addr & 0x03);
1449 priv->cec = i2c_new_dummy(client->adapter, cec_addr);
Russell Kinga8f4d4d62014-02-07 19:17:21 +00001450 if (!priv->cec)
Jean-Francois Moine6ae668c2014-01-25 18:14:43 +01001451 return -ENODEV;
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001452
Rob Clarke7792ce2013-01-08 19:21:02 -06001453 priv->dpms = DRM_MODE_DPMS_OFF;
1454
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001455 mutex_init(&priv->mutex); /* protect the page access */
Russell King0fc6f442015-06-06 21:41:09 +01001456 init_waitqueue_head(&priv->edid_delay_waitq);
1457 setup_timer(&priv->edid_delay_timer, tda998x_edid_delay_done,
1458 (unsigned long)priv);
1459 INIT_WORK(&priv->detect_work, tda998x_detect_work);
Jean-Francois Moineed9a8422014-11-29 08:30:51 +01001460
Rob Clarke7792ce2013-01-08 19:21:02 -06001461 /* wake up the device: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001462 cec_write(priv, REG_CEC_ENAMODS,
Rob Clarke7792ce2013-01-08 19:21:02 -06001463 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1464
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001465 tda998x_reset(priv);
Rob Clarke7792ce2013-01-08 19:21:02 -06001466
1467 /* read version: */
Russell Kingfb7544d2014-02-02 16:18:24 +00001468 rev_lo = reg_read(priv, REG_VERSION_LSB);
1469 rev_hi = reg_read(priv, REG_VERSION_MSB);
1470 if (rev_lo < 0 || rev_hi < 0) {
1471 ret = rev_lo < 0 ? rev_lo : rev_hi;
Jean-Francois Moine7d2eadc2014-01-25 18:14:45 +01001472 goto fail;
Russell Kingfb7544d2014-02-02 16:18:24 +00001473 }
1474
1475 priv->rev = rev_lo | rev_hi << 8;
Rob Clarke7792ce2013-01-08 19:21:02 -06001476
1477 /* mask off feature bits: */
1478 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1479
1480 switch (priv->rev) {
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001481 case TDA9989N2:
1482 dev_info(&client->dev, "found TDA9989 n2");
1483 break;
1484 case TDA19989:
1485 dev_info(&client->dev, "found TDA19989");
1486 break;
1487 case TDA19989N2:
1488 dev_info(&client->dev, "found TDA19989 n2");
1489 break;
1490 case TDA19988:
1491 dev_info(&client->dev, "found TDA19988");
1492 break;
Rob Clarke7792ce2013-01-08 19:21:02 -06001493 default:
Jean-Francois Moineb728fab2014-01-25 18:14:46 +01001494 dev_err(&client->dev, "found unsupported device: %04x\n",
1495 priv->rev);
Rob Clarke7792ce2013-01-08 19:21:02 -06001496 goto fail;
1497 }
1498
1499 /* after reset, enable DDC: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001500 reg_write(priv, REG_DDC_DISABLE, 0x00);
Rob Clarke7792ce2013-01-08 19:21:02 -06001501
1502 /* set clock on DDC channel: */
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001503 reg_write(priv, REG_TX3, 39);
Rob Clarke7792ce2013-01-08 19:21:02 -06001504
1505 /* if necessary, disable multi-master: */
1506 if (priv->rev == TDA19989)
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001507 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
Rob Clarke7792ce2013-01-08 19:21:02 -06001508
Jean-Francois Moine2f7f7302014-01-25 18:14:47 +01001509 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
Rob Clarke7792ce2013-01-08 19:21:02 -06001510 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1511
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001512 /* initialize the optional IRQ */
1513 if (client->irq) {
1514 int irqf_trigger;
1515
Jean-Francois Moine6833d262014-11-29 08:57:15 +01001516 /* init read EDID waitqueue and HDP work */
Jean-Francois Moine12473b72014-01-25 18:14:38 +01001517 init_waitqueue_head(&priv->wq_edid);
1518
1519 /* clear pending interrupts */
1520 reg_read(priv, REG_INT_FLAGS_0);
1521 reg_read(priv, REG_INT_FLAGS_1);
1522 reg_read(priv, REG_INT_FLAGS_2);
1523
1524 irqf_trigger =
1525 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1526 ret = request_threaded_irq(client->irq, NULL,
1527 tda998x_irq_thread,
1528 irqf_trigger | IRQF_ONESHOT,
1529 "tda998x", priv);
1530 if (ret) {
1531 dev_err(&client->dev,
1532 "failed to request IRQ#%u: %d\n",
1533 client->irq, ret);
1534 goto fail;
1535 }
1536
1537 /* enable HPD irq */
1538 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1539 }
1540
Jean-Francois Moinee4782622014-01-25 18:14:38 +01001541 /* enable EDID read irq: */
1542 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1543
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001544 if (!np)
1545 return 0; /* non-DT */
1546
Jyri Sarha7e567622016-08-09 22:00:05 +03001547 /* get the device tree parameters */
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001548 ret = of_property_read_u32(np, "video-ports", &video);
1549 if (ret == 0) {
1550 priv->vip_cntrl_0 = video >> 16;
1551 priv->vip_cntrl_1 = video >> 8;
1552 priv->vip_cntrl_2 = video;
1553 }
1554
Jyri Sarha7e567622016-08-09 22:00:05 +03001555 ret = tda998x_get_audio_ports(priv, np);
1556 if (ret)
1557 goto fail;
1558
1559 if (priv->audio_port[0].format != AFMT_UNUSED)
1560 tda998x_audio_codec_init(priv, &client->dev);
1561
1562 return 0;
Rob Clarke7792ce2013-01-08 19:21:02 -06001563fail:
1564 /* if encoder_init fails, the encoder slave is never registered,
1565 * so cleanup here:
1566 */
1567 if (priv->cec)
1568 i2c_unregister_device(priv->cec);
Rob Clarke7792ce2013-01-08 19:21:02 -06001569 return -ENXIO;
1570}
1571
Russell Kingc707c362014-02-07 19:49:44 +00001572static void tda998x_encoder_prepare(struct drm_encoder *encoder)
1573{
Russell King9525c4d2015-08-14 11:28:53 +01001574 tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
Russell Kingc707c362014-02-07 19:49:44 +00001575}
1576
1577static void tda998x_encoder_commit(struct drm_encoder *encoder)
1578{
Russell King9525c4d2015-08-14 11:28:53 +01001579 tda998x_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
Russell Kingc707c362014-02-07 19:49:44 +00001580}
1581
1582static const struct drm_encoder_helper_funcs tda998x_encoder_helper_funcs = {
Russell King9525c4d2015-08-14 11:28:53 +01001583 .dpms = tda998x_encoder_dpms,
Russell Kingc707c362014-02-07 19:49:44 +00001584 .prepare = tda998x_encoder_prepare,
1585 .commit = tda998x_encoder_commit,
Russell King9525c4d2015-08-14 11:28:53 +01001586 .mode_set = tda998x_encoder_mode_set,
Russell Kingc707c362014-02-07 19:49:44 +00001587};
1588
1589static void tda998x_encoder_destroy(struct drm_encoder *encoder)
1590{
Russell Kinga3584f62015-08-14 11:22:50 +01001591 struct tda998x_priv *priv = enc_to_tda998x_priv(encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001592
Russell Kinga3584f62015-08-14 11:22:50 +01001593 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001594 drm_encoder_cleanup(encoder);
1595}
1596
1597static const struct drm_encoder_funcs tda998x_encoder_funcs = {
1598 .destroy = tda998x_encoder_destroy,
1599};
1600
Russell Kingc707c362014-02-07 19:49:44 +00001601static struct drm_encoder *
1602tda998x_connector_best_encoder(struct drm_connector *connector)
1603{
Russell Kinga3584f62015-08-14 11:22:50 +01001604 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
Russell Kingc707c362014-02-07 19:49:44 +00001605
Russell Kinga3584f62015-08-14 11:22:50 +01001606 return &priv->encoder;
Russell Kingc707c362014-02-07 19:49:44 +00001607}
1608
1609static
1610const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
1611 .get_modes = tda998x_connector_get_modes,
1612 .mode_valid = tda998x_connector_mode_valid,
1613 .best_encoder = tda998x_connector_best_encoder,
1614};
1615
Russell Kingc707c362014-02-07 19:49:44 +00001616static void tda998x_connector_destroy(struct drm_connector *connector)
1617{
Russell Kingc707c362014-02-07 19:49:44 +00001618 drm_connector_cleanup(connector);
1619}
1620
Jyri Sarhadad82ea2016-01-16 22:17:54 +02001621static int tda998x_connector_dpms(struct drm_connector *connector, int mode)
1622{
1623 if (drm_core_check_feature(connector->dev, DRIVER_ATOMIC))
1624 return drm_atomic_helper_connector_dpms(connector, mode);
1625 else
1626 return drm_helper_connector_dpms(connector, mode);
1627}
1628
Russell Kingc707c362014-02-07 19:49:44 +00001629static const struct drm_connector_funcs tda998x_connector_funcs = {
Jyri Sarhadad82ea2016-01-16 22:17:54 +02001630 .dpms = tda998x_connector_dpms,
Liviu Dudau (ARM)9736e9882015-11-23 16:52:42 +01001631 .reset = drm_atomic_helper_connector_reset,
Russell King8f3f21f2016-11-02 21:15:04 +00001632 .fill_modes = tda998x_connector_fill_modes,
Russell Kingc707c362014-02-07 19:49:44 +00001633 .detect = tda998x_connector_detect,
1634 .destroy = tda998x_connector_destroy,
Liviu Dudau (ARM)9736e9882015-11-23 16:52:42 +01001635 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1636 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Russell Kingc707c362014-02-07 19:49:44 +00001637};
1638
1639static int tda998x_bind(struct device *dev, struct device *master, void *data)
1640{
1641 struct tda998x_encoder_params *params = dev->platform_data;
1642 struct i2c_client *client = to_i2c_client(dev);
1643 struct drm_device *drm = data;
Russell Kinga3584f62015-08-14 11:22:50 +01001644 struct tda998x_priv *priv;
Russell Kinge66e03a2015-06-06 21:41:10 +01001645 u32 crtcs = 0;
Russell Kingc707c362014-02-07 19:49:44 +00001646 int ret;
1647
1648 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1649 if (!priv)
1650 return -ENOMEM;
1651
1652 dev_set_drvdata(dev, priv);
1653
Russell King5dbcf312014-06-15 11:11:10 +01001654 if (dev->of_node)
1655 crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
1656
1657 /* If no CRTCs were found, fall back to our old behaviour */
1658 if (crtcs == 0) {
1659 dev_warn(dev, "Falling back to first CRTC\n");
1660 crtcs = 1 << 0;
1661 }
1662
Russell Kinga3584f62015-08-14 11:22:50 +01001663 priv->connector.interlace_allowed = 1;
1664 priv->encoder.possible_crtcs = crtcs;
Russell Kingc707c362014-02-07 19:49:44 +00001665
Russell Kinga3584f62015-08-14 11:22:50 +01001666 ret = tda998x_create(client, priv);
Russell Kingc707c362014-02-07 19:49:44 +00001667 if (ret)
1668 return ret;
1669
1670 if (!dev->of_node && params)
Russell Kinga3584f62015-08-14 11:22:50 +01001671 tda998x_encoder_set_config(priv, params);
Russell Kingc707c362014-02-07 19:49:44 +00001672
Russell Kinga3584f62015-08-14 11:22:50 +01001673 tda998x_encoder_set_polling(priv, &priv->connector);
Russell Kingc707c362014-02-07 19:49:44 +00001674
Russell Kinga3584f62015-08-14 11:22:50 +01001675 drm_encoder_helper_add(&priv->encoder, &tda998x_encoder_helper_funcs);
1676 ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001677 DRM_MODE_ENCODER_TMDS, NULL);
Russell Kingc707c362014-02-07 19:49:44 +00001678 if (ret)
1679 goto err_encoder;
1680
Russell Kinga3584f62015-08-14 11:22:50 +01001681 drm_connector_helper_add(&priv->connector,
Russell Kingc707c362014-02-07 19:49:44 +00001682 &tda998x_connector_helper_funcs);
Russell Kinga3584f62015-08-14 11:22:50 +01001683 ret = drm_connector_init(drm, &priv->connector,
Russell Kingc707c362014-02-07 19:49:44 +00001684 &tda998x_connector_funcs,
1685 DRM_MODE_CONNECTOR_HDMIA);
1686 if (ret)
1687 goto err_connector;
1688
Russell Kinga3584f62015-08-14 11:22:50 +01001689 drm_mode_connector_attach_encoder(&priv->connector, &priv->encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001690
1691 return 0;
1692
Russell Kingc707c362014-02-07 19:49:44 +00001693err_connector:
Russell Kinga3584f62015-08-14 11:22:50 +01001694 drm_encoder_cleanup(&priv->encoder);
Russell Kingc707c362014-02-07 19:49:44 +00001695err_encoder:
Russell Kinga3584f62015-08-14 11:22:50 +01001696 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001697 return ret;
1698}
1699
1700static void tda998x_unbind(struct device *dev, struct device *master,
1701 void *data)
1702{
Russell Kinga3584f62015-08-14 11:22:50 +01001703 struct tda998x_priv *priv = dev_get_drvdata(dev);
Russell Kingc707c362014-02-07 19:49:44 +00001704
Russell Kinga3584f62015-08-14 11:22:50 +01001705 drm_connector_cleanup(&priv->connector);
1706 drm_encoder_cleanup(&priv->encoder);
1707 tda998x_destroy(priv);
Russell Kingc707c362014-02-07 19:49:44 +00001708}
1709
1710static const struct component_ops tda998x_ops = {
1711 .bind = tda998x_bind,
1712 .unbind = tda998x_unbind,
1713};
1714
1715static int
1716tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
1717{
1718 return component_add(&client->dev, &tda998x_ops);
1719}
1720
1721static int tda998x_remove(struct i2c_client *client)
1722{
1723 component_del(&client->dev, &tda998x_ops);
1724 return 0;
1725}
1726
Jean-Francois Moine0d44ea12014-01-25 18:14:41 +01001727#ifdef CONFIG_OF
1728static const struct of_device_id tda998x_dt_ids[] = {
1729 { .compatible = "nxp,tda998x", },
1730 { }
1731};
1732MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
1733#endif
1734
Rob Clarke7792ce2013-01-08 19:21:02 -06001735static struct i2c_device_id tda998x_ids[] = {
1736 { "tda998x", 0 },
1737 { }
1738};
1739MODULE_DEVICE_TABLE(i2c, tda998x_ids);
1740
Russell King3d58e312015-08-14 11:13:50 +01001741static struct i2c_driver tda998x_driver = {
1742 .probe = tda998x_probe,
1743 .remove = tda998x_remove,
1744 .driver = {
1745 .name = "tda998x",
1746 .of_match_table = of_match_ptr(tda998x_dt_ids),
Rob Clarke7792ce2013-01-08 19:21:02 -06001747 },
Russell King3d58e312015-08-14 11:13:50 +01001748 .id_table = tda998x_ids,
Rob Clarke7792ce2013-01-08 19:21:02 -06001749};
1750
Russell King3d58e312015-08-14 11:13:50 +01001751module_i2c_driver(tda998x_driver);
Rob Clarke7792ce2013-01-08 19:21:02 -06001752
1753MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
1754MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
1755MODULE_LICENSE("GPL");