drm/i2c: tda998x: improve correctness of quantisation range
[linux-2.6-block.git] / drivers / gpu / drm / i2c / tda998x_drv.c
CommitLineData
e7792ce2
RC
1/*
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
c707c361 18#include <linux/component.h>
7e8675f0 19#include <linux/gpio/consumer.h>
893c3e53 20#include <linux/hdmi.h>
e7792ce2 21#include <linux/module.h>
7e8675f0 22#include <linux/platform_data/tda9950.h>
12473b7d 23#include <linux/irq.h>
f0b33b28 24#include <sound/asoundef.h>
7e567624 25#include <sound/hdmi-codec.h>
e7792ce2
RC
26
27#include <drm/drmP.h>
9736e988 28#include <drm/drm_atomic_helper.h>
e7792ce2 29#include <drm/drm_edid.h>
5dbcf319 30#include <drm/drm_of.h>
fcd70cd3 31#include <drm/drm_probe_helper.h>
c4c11dd1 32#include <drm/i2c/tda998x.h>
e7792ce2 33
7e8675f0
RK
34#include <media/cec-notifier.h>
35
e7792ce2
RC
36#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
37
71689160
RK
38enum {
39 AUDIO_ROUTE_I2S,
40 AUDIO_ROUTE_SPDIF,
41 AUDIO_ROUTE_NUM
7e567624
JS
42};
43
26f7bf12
RK
44struct tda998x_audio_route {
45 u8 ena_aclk;
46 u8 mux_ap;
47 u8 aip_clksel;
48};
49
2e9943aa 50struct tda998x_audio_settings {
26f7bf12 51 const struct tda998x_audio_route *route;
125a4f93
RK
52 struct hdmi_audio_infoframe cea;
53 unsigned int sample_rate;
54 u8 status[5];
82642ab7 55 u8 ena_ap;
935b9ca3 56 u8 i2s_format;
a03a915b 57 u8 cts_n;
2e9943aa
RK
58};
59
e7792ce2
RC
60struct tda998x_priv {
61 struct i2c_client *cec;
2f7f730a 62 struct i2c_client *hdmi;
ed9a8426 63 struct mutex mutex;
e66e03ab 64 u16 rev;
14e5b588 65 u8 cec_addr;
e66e03ab 66 u8 current_page;
3cb43378 67 bool is_on;
896a4130 68 bool supports_infoframes;
8f3f21f6 69 bool sink_has_audio;
fcc22c5f 70 enum hdmi_quantization_range rgb_quant_range;
5e74c22c
RK
71 u8 vip_cntrl_0;
72 u8 vip_cntrl_1;
73 u8 vip_cntrl_2;
319e658c 74 unsigned long tmds_clock;
2e9943aa 75 struct tda998x_audio_settings audio;
12473b7d 76
7e567624
JS
77 struct platform_device *audio_pdev;
78 struct mutex audio_mutex;
79
7e8675f0 80 struct mutex edid_mutex;
12473b7d
JFM
81 wait_queue_head_t wq_edid;
82 volatile int wq_edid_wait;
0fc6f44d
RK
83
84 struct work_struct detect_work;
85 struct timer_list edid_delay_timer;
86 wait_queue_head_t edid_delay_waitq;
87 bool edid_delay_active;
78e401f9
RK
88
89 struct drm_encoder encoder;
30bd8b86 90 struct drm_bridge bridge;
eed64b59 91 struct drm_connector connector;
7e567624 92
71689160 93 u8 audio_port_enable[AUDIO_ROUTE_NUM];
7e8675f0
RK
94 struct tda9950_glue cec_glue;
95 struct gpio_desc *calib;
96 struct cec_notifier *cec_notify;
e7792ce2
RC
97};
98
9525c4dd
RK
99#define conn_to_tda998x_priv(x) \
100 container_of(x, struct tda998x_priv, connector)
9525c4dd
RK
101#define enc_to_tda998x_priv(x) \
102 container_of(x, struct tda998x_priv, encoder)
30bd8b86
RK
103#define bridge_to_tda998x_priv(x) \
104 container_of(x, struct tda998x_priv, bridge)
9525c4dd 105
e7792ce2
RC
106/* The TDA9988 series of devices use a paged register scheme.. to simplify
107 * things we encode the page # in upper bits of the register #. To read/
108 * write a given register, we need to make sure CURPAGE register is set
109 * appropriately. Which implies reads/writes are not atomic. Fun!
110 */
111
112#define REG(page, addr) (((page) << 8) | (addr))
113#define REG2ADDR(reg) ((reg) & 0xff)
114#define REG2PAGE(reg) (((reg) >> 8) & 0xff)
115
116#define REG_CURPAGE 0xff /* write */
117
118
119/* Page 00h: General Control */
120#define REG_VERSION_LSB REG(0x00, 0x00) /* read */
121#define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
122# define MAIN_CNTRL0_SR (1 << 0)
123# define MAIN_CNTRL0_DECS (1 << 1)
124# define MAIN_CNTRL0_DEHS (1 << 2)
125# define MAIN_CNTRL0_CECS (1 << 3)
126# define MAIN_CNTRL0_CEHS (1 << 4)
127# define MAIN_CNTRL0_SCALER (1 << 7)
128#define REG_VERSION_MSB REG(0x00, 0x02) /* read */
129#define REG_SOFTRESET REG(0x00, 0x0a) /* write */
130# define SOFTRESET_AUDIO (1 << 0)
131# define SOFTRESET_I2C_MASTER (1 << 1)
132#define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
133#define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
134#define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
135# define I2C_MASTER_DIS_MM (1 << 0)
136# define I2C_MASTER_DIS_FILT (1 << 1)
137# define I2C_MASTER_APP_STRT_LAT (1 << 2)
c4c11dd1 138#define REG_FEAT_POWERDOWN REG(0x00, 0x0e) /* read/write */
9476ed2e
RK
139# define FEAT_POWERDOWN_PREFILT BIT(0)
140# define FEAT_POWERDOWN_CSC BIT(1)
c4c11dd1 141# define FEAT_POWERDOWN_SPDIF (1 << 3)
e7792ce2
RC
142#define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
143#define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
144#define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
145# define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
c4c11dd1 146#define REG_ENA_ACLK REG(0x00, 0x16) /* read/write */
e7792ce2
RC
147#define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
148#define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
149#define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
150#define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
151#define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
152# define VIP_CNTRL_0_MIRR_A (1 << 7)
153# define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
154# define VIP_CNTRL_0_MIRR_B (1 << 3)
155# define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
156#define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
157# define VIP_CNTRL_1_MIRR_C (1 << 7)
158# define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
159# define VIP_CNTRL_1_MIRR_D (1 << 3)
160# define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
161#define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
162# define VIP_CNTRL_2_MIRR_E (1 << 7)
163# define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
164# define VIP_CNTRL_2_MIRR_F (1 << 3)
165# define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
166#define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
167# define VIP_CNTRL_3_X_TGL (1 << 0)
168# define VIP_CNTRL_3_H_TGL (1 << 1)
169# define VIP_CNTRL_3_V_TGL (1 << 2)
170# define VIP_CNTRL_3_EMB (1 << 3)
171# define VIP_CNTRL_3_SYNC_DE (1 << 4)
172# define VIP_CNTRL_3_SYNC_HS (1 << 5)
173# define VIP_CNTRL_3_DE_INT (1 << 6)
174# define VIP_CNTRL_3_EDGE (1 << 7)
175#define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
176# define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
177# define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
178# define VIP_CNTRL_4_CCIR656 (1 << 4)
179# define VIP_CNTRL_4_656_ALT (1 << 5)
180# define VIP_CNTRL_4_TST_656 (1 << 6)
181# define VIP_CNTRL_4_TST_PAT (1 << 7)
182#define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
183# define VIP_CNTRL_5_CKCASE (1 << 0)
184# define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
c4c11dd1 185#define REG_MUX_AP REG(0x00, 0x26) /* read/write */
10df1a95
JFM
186# define MUX_AP_SELECT_I2S 0x64
187# define MUX_AP_SELECT_SPDIF 0x40
bcb2481d 188#define REG_MUX_VP_VIP_OUT REG(0x00, 0x27) /* read/write */
e7792ce2
RC
189#define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
190# define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
191# define MAT_CONTRL_MAT_BP (1 << 2)
192#define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
193#define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
194#define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
195#define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
196#define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
197#define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
198#define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
199#define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
200#define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
201#define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
202#define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
203#define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
204#define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
205#define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
206#define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
207#define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
208#define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
088d61d1
SH
209#define REG_VS_LINE_STRT_2_MSB REG(0x00, 0xb1) /* write */
210#define REG_VS_LINE_STRT_2_LSB REG(0x00, 0xb2) /* write */
e7792ce2
RC
211#define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
212#define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
088d61d1
SH
213#define REG_VS_LINE_END_2_MSB REG(0x00, 0xb5) /* write */
214#define REG_VS_LINE_END_2_LSB REG(0x00, 0xb6) /* write */
e7792ce2
RC
215#define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
216#define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
217#define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
218#define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
219#define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
220#define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
221#define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
222#define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
223#define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
224#define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
088d61d1
SH
225#define REG_VWIN_START_2_MSB REG(0x00, 0xc1) /* write */
226#define REG_VWIN_START_2_LSB REG(0x00, 0xc2) /* write */
227#define REG_VWIN_END_2_MSB REG(0x00, 0xc3) /* write */
228#define REG_VWIN_END_2_LSB REG(0x00, 0xc4) /* write */
e7792ce2
RC
229#define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
230#define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
231#define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
232#define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
233#define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
088d61d1
SH
234# define TBG_CNTRL_0_TOP_TGL (1 << 0)
235# define TBG_CNTRL_0_TOP_SEL (1 << 1)
236# define TBG_CNTRL_0_DE_EXT (1 << 2)
237# define TBG_CNTRL_0_TOP_EXT (1 << 3)
e7792ce2
RC
238# define TBG_CNTRL_0_FRAME_DIS (1 << 5)
239# define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
240# define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
241#define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
088d61d1
SH
242# define TBG_CNTRL_1_H_TGL (1 << 0)
243# define TBG_CNTRL_1_V_TGL (1 << 1)
244# define TBG_CNTRL_1_TGL_EN (1 << 2)
245# define TBG_CNTRL_1_X_EXT (1 << 3)
246# define TBG_CNTRL_1_H_EXT (1 << 4)
247# define TBG_CNTRL_1_V_EXT (1 << 5)
e7792ce2
RC
248# define TBG_CNTRL_1_DWIN_DIS (1 << 6)
249#define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
250#define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
251# define HVF_CNTRL_0_SM (1 << 7)
252# define HVF_CNTRL_0_RWB (1 << 6)
253# define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
254# define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
255#define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
256# define HVF_CNTRL_1_FOR (1 << 0)
257# define HVF_CNTRL_1_YUVBLK (1 << 1)
258# define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
259# define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
260# define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
261#define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
2807ba75 262# define RPT_CNTRL_REPEAT(x) ((x) & 15)
c4c11dd1 263#define REG_I2S_FORMAT REG(0x00, 0xfc) /* read/write */
935b9ca3
RK
264# define I2S_FORMAT_PHILIPS (0 << 0)
265# define I2S_FORMAT_LEFT_J (2 << 0)
266# define I2S_FORMAT_RIGHT_J (3 << 0)
c4c11dd1 267#define REG_AIP_CLKSEL REG(0x00, 0xfd) /* write */
10df1a95
JFM
268# define AIP_CLKSEL_AIP_SPDIF (0 << 3)
269# define AIP_CLKSEL_AIP_I2S (1 << 3)
270# define AIP_CLKSEL_FS_ACLK (0 << 0)
271# define AIP_CLKSEL_FS_MCLK (1 << 0)
272# define AIP_CLKSEL_FS_FS64SPDIF (2 << 0)
e7792ce2
RC
273
274/* Page 02h: PLL settings */
275#define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
276# define PLL_SERIAL_1_SRL_FDN (1 << 0)
277# define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
278# define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
279#define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
3ae471f7 280# define PLL_SERIAL_2_SRL_NOSC(x) ((x) << 0)
e7792ce2
RC
281# define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
282#define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
283# define PLL_SERIAL_3_SRL_CCIR (1 << 0)
284# define PLL_SERIAL_3_SRL_DE (1 << 2)
285# define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
286#define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
287#define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
288#define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
289#define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
290#define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
291#define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
292#define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
293#define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
294#define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
c4c11dd1
RK
295# define AUDIO_DIV_SERCLK_1 0
296# define AUDIO_DIV_SERCLK_2 1
297# define AUDIO_DIV_SERCLK_4 2
298# define AUDIO_DIV_SERCLK_8 3
299# define AUDIO_DIV_SERCLK_16 4
300# define AUDIO_DIV_SERCLK_32 5
e7792ce2
RC
301#define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
302# define SEL_CLK_SEL_CLK1 (1 << 0)
303# define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
304# define SEL_CLK_ENA_SC_CLK (1 << 3)
305#define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
306
307
308/* Page 09h: EDID Control */
309#define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
310/* next 127 successive registers are the EDID block */
311#define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
312#define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
313#define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
314#define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
315#define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
316
317
318/* Page 10h: information frames and packets */
c4c11dd1
RK
319#define REG_IF1_HB0 REG(0x10, 0x20) /* read/write */
320#define REG_IF2_HB0 REG(0x10, 0x40) /* read/write */
321#define REG_IF3_HB0 REG(0x10, 0x60) /* read/write */
322#define REG_IF4_HB0 REG(0x10, 0x80) /* read/write */
323#define REG_IF5_HB0 REG(0x10, 0xa0) /* read/write */
e7792ce2
RC
324
325
326/* Page 11h: audio settings and content info packets */
327#define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
328# define AIP_CNTRL_0_RST_FIFO (1 << 0)
329# define AIP_CNTRL_0_SWAP (1 << 1)
330# define AIP_CNTRL_0_LAYOUT (1 << 2)
331# define AIP_CNTRL_0_ACR_MAN (1 << 5)
332# define AIP_CNTRL_0_RST_CTS (1 << 6)
c4c11dd1
RK
333#define REG_CA_I2S REG(0x11, 0x01) /* read/write */
334# define CA_I2S_CA_I2S(x) (((x) & 31) << 0)
335# define CA_I2S_HBR_CHSTAT (1 << 6)
336#define REG_LATENCY_RD REG(0x11, 0x04) /* read/write */
337#define REG_ACR_CTS_0 REG(0x11, 0x05) /* read/write */
338#define REG_ACR_CTS_1 REG(0x11, 0x06) /* read/write */
339#define REG_ACR_CTS_2 REG(0x11, 0x07) /* read/write */
340#define REG_ACR_N_0 REG(0x11, 0x08) /* read/write */
341#define REG_ACR_N_1 REG(0x11, 0x09) /* read/write */
342#define REG_ACR_N_2 REG(0x11, 0x0a) /* read/write */
343#define REG_CTS_N REG(0x11, 0x0c) /* read/write */
344# define CTS_N_K(x) (((x) & 7) << 0)
345# define CTS_N_M(x) (((x) & 3) << 4)
e7792ce2
RC
346#define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
347# define ENC_CNTRL_RST_ENC (1 << 0)
348# define ENC_CNTRL_RST_SEL (1 << 1)
349# define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
c4c11dd1
RK
350#define REG_DIP_FLAGS REG(0x11, 0x0e) /* read/write */
351# define DIP_FLAGS_ACR (1 << 0)
352# define DIP_FLAGS_GC (1 << 1)
353#define REG_DIP_IF_FLAGS REG(0x11, 0x0f) /* read/write */
354# define DIP_IF_FLAGS_IF1 (1 << 1)
355# define DIP_IF_FLAGS_IF2 (1 << 2)
356# define DIP_IF_FLAGS_IF3 (1 << 3)
357# define DIP_IF_FLAGS_IF4 (1 << 4)
358# define DIP_IF_FLAGS_IF5 (1 << 5)
359#define REG_CH_STAT_B(x) REG(0x11, 0x14 + (x)) /* read/write */
e7792ce2
RC
360
361
362/* Page 12h: HDCP and OTP */
363#define REG_TX3 REG(0x12, 0x9a) /* read/write */
063b472f
RK
364#define REG_TX4 REG(0x12, 0x9b) /* read/write */
365# define TX4_PD_RAM (1 << 1)
e7792ce2
RC
366#define REG_TX33 REG(0x12, 0xb8) /* read/write */
367# define TX33_HDMI (1 << 1)
368
369
370/* Page 13h: Gamut related metadata packets */
371
372
373
374/* CEC registers: (not paged)
375 */
12473b7d
JFM
376#define REG_CEC_INTSTATUS 0xee /* read */
377# define CEC_INTSTATUS_CEC (1 << 0)
378# define CEC_INTSTATUS_HDMI (1 << 1)
7e8675f0
RK
379#define REG_CEC_CAL_XOSC_CTRL1 0xf2
380# define CEC_CAL_XOSC_CTRL1_ENA_CAL BIT(0)
381#define REG_CEC_DES_FREQ2 0xf5
382# define CEC_DES_FREQ2_DIS_AUTOCAL BIT(7)
383#define REG_CEC_CLK 0xf6
384# define CEC_CLK_FRO 0x11
e7792ce2
RC
385#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
386# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
387# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
388# define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
389# define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
12473b7d
JFM
390#define REG_CEC_RXSHPDINTENA 0xfc /* read/write */
391#define REG_CEC_RXSHPDINT 0xfd /* read */
ec5d3e83
RK
392# define CEC_RXSHPDINT_RXSENS BIT(0)
393# define CEC_RXSHPDINT_HPD BIT(1)
e7792ce2
RC
394#define REG_CEC_RXSHPDLEV 0xfe /* read */
395# define CEC_RXSHPDLEV_RXSENS (1 << 0)
396# define CEC_RXSHPDLEV_HPD (1 << 1)
397
398#define REG_CEC_ENAMODS 0xff /* read/write */
7e8675f0 399# define CEC_ENAMODS_EN_CEC_CLK (1 << 7)
e7792ce2
RC
400# define CEC_ENAMODS_DIS_FRO (1 << 6)
401# define CEC_ENAMODS_DIS_CCLK (1 << 5)
402# define CEC_ENAMODS_EN_RXSENS (1 << 2)
403# define CEC_ENAMODS_EN_HDMI (1 << 1)
404# define CEC_ENAMODS_EN_CEC (1 << 0)
405
406
407/* Device versions: */
408#define TDA9989N2 0x0101
409#define TDA19989 0x0201
410#define TDA19989N2 0x0202
411#define TDA19988 0x0301
412
413static void
e66e03ab 414cec_write(struct tda998x_priv *priv, u16 addr, u8 val)
e7792ce2 415{
e66e03ab 416 u8 buf[] = {addr, val};
14e5b588
RK
417 struct i2c_msg msg = {
418 .addr = priv->cec_addr,
419 .len = 2,
420 .buf = buf,
421 };
e7792ce2
RC
422 int ret;
423
14e5b588 424 ret = i2c_transfer(priv->hdmi->adapter, &msg, 1);
e7792ce2 425 if (ret < 0)
14e5b588
RK
426 dev_err(&priv->hdmi->dev, "Error %d writing to cec:0x%x\n",
427 ret, addr);
e7792ce2
RC
428}
429
e66e03ab
RK
430static u8
431cec_read(struct tda998x_priv *priv, u8 addr)
e7792ce2 432{
e66e03ab 433 u8 val;
14e5b588
RK
434 struct i2c_msg msg[2] = {
435 {
436 .addr = priv->cec_addr,
437 .len = 1,
438 .buf = &addr,
439 }, {
440 .addr = priv->cec_addr,
441 .flags = I2C_M_RD,
442 .len = 1,
443 .buf = &val,
444 },
445 };
e7792ce2
RC
446 int ret;
447
14e5b588
RK
448 ret = i2c_transfer(priv->hdmi->adapter, msg, ARRAY_SIZE(msg));
449 if (ret < 0) {
450 dev_err(&priv->hdmi->dev, "Error %d reading from cec:0x%x\n",
451 ret, addr);
452 val = 0;
453 }
e7792ce2
RC
454
455 return val;
e7792ce2
RC
456}
457
7e8675f0
RK
458static void cec_enamods(struct tda998x_priv *priv, u8 mods, bool enable)
459{
460 int val = cec_read(priv, REG_CEC_ENAMODS);
461
462 if (val < 0)
463 return;
464
465 if (enable)
466 val |= mods;
467 else
468 val &= ~mods;
469
470 cec_write(priv, REG_CEC_ENAMODS, val);
471}
472
473static void tda998x_cec_set_calibration(struct tda998x_priv *priv, bool enable)
474{
475 if (enable) {
476 u8 val;
477
478 cec_write(priv, 0xf3, 0xc0);
479 cec_write(priv, 0xf4, 0xd4);
480
481 /* Enable automatic calibration mode */
482 val = cec_read(priv, REG_CEC_DES_FREQ2);
483 val &= ~CEC_DES_FREQ2_DIS_AUTOCAL;
484 cec_write(priv, REG_CEC_DES_FREQ2, val);
485
486 /* Enable free running oscillator */
487 cec_write(priv, REG_CEC_CLK, CEC_CLK_FRO);
488 cec_enamods(priv, CEC_ENAMODS_DIS_FRO, false);
489
490 cec_write(priv, REG_CEC_CAL_XOSC_CTRL1,
491 CEC_CAL_XOSC_CTRL1_ENA_CAL);
492 } else {
493 cec_write(priv, REG_CEC_CAL_XOSC_CTRL1, 0);
494 }
495}
496
497/*
498 * Calibration for the internal oscillator: we need to set calibration mode,
499 * and then pulse the IRQ line low for a 10ms ± 1% period.
500 */
501static void tda998x_cec_calibration(struct tda998x_priv *priv)
502{
503 struct gpio_desc *calib = priv->calib;
504
505 mutex_lock(&priv->edid_mutex);
506 if (priv->hdmi->irq > 0)
507 disable_irq(priv->hdmi->irq);
508 gpiod_direction_output(calib, 1);
509 tda998x_cec_set_calibration(priv, true);
510
511 local_irq_disable();
512 gpiod_set_value(calib, 0);
513 mdelay(10);
514 gpiod_set_value(calib, 1);
515 local_irq_enable();
516
517 tda998x_cec_set_calibration(priv, false);
518 gpiod_direction_input(calib);
519 if (priv->hdmi->irq > 0)
520 enable_irq(priv->hdmi->irq);
521 mutex_unlock(&priv->edid_mutex);
522}
523
524static int tda998x_cec_hook_init(void *data)
525{
526 struct tda998x_priv *priv = data;
527 struct gpio_desc *calib;
528
529 calib = gpiod_get(&priv->hdmi->dev, "nxp,calib", GPIOD_ASIS);
530 if (IS_ERR(calib)) {
531 dev_warn(&priv->hdmi->dev, "failed to get calibration gpio: %ld\n",
532 PTR_ERR(calib));
533 return PTR_ERR(calib);
534 }
535
536 priv->calib = calib;
537
538 return 0;
539}
540
541static void tda998x_cec_hook_exit(void *data)
542{
543 struct tda998x_priv *priv = data;
544
545 gpiod_put(priv->calib);
546 priv->calib = NULL;
547}
548
549static int tda998x_cec_hook_open(void *data)
550{
551 struct tda998x_priv *priv = data;
552
553 cec_enamods(priv, CEC_ENAMODS_EN_CEC_CLK | CEC_ENAMODS_EN_CEC, true);
554 tda998x_cec_calibration(priv);
555
556 return 0;
557}
558
559static void tda998x_cec_hook_release(void *data)
560{
561 struct tda998x_priv *priv = data;
562
563 cec_enamods(priv, CEC_ENAMODS_EN_CEC_CLK | CEC_ENAMODS_EN_CEC, false);
564}
565
7d2eadc9 566static int
e66e03ab 567set_page(struct tda998x_priv *priv, u16 reg)
e7792ce2 568{
e7792ce2 569 if (REG2PAGE(reg) != priv->current_page) {
2f7f730a 570 struct i2c_client *client = priv->hdmi;
e66e03ab 571 u8 buf[] = {
e7792ce2
RC
572 REG_CURPAGE, REG2PAGE(reg)
573 };
574 int ret = i2c_master_send(client, buf, sizeof(buf));
7d2eadc9 575 if (ret < 0) {
288ffc73 576 dev_err(&client->dev, "%s %04x err %d\n", __func__,
704d63f5 577 reg, ret);
7d2eadc9
JFM
578 return ret;
579 }
e7792ce2
RC
580
581 priv->current_page = REG2PAGE(reg);
582 }
7d2eadc9 583 return 0;
e7792ce2
RC
584}
585
586static int
e66e03ab 587reg_read_range(struct tda998x_priv *priv, u16 reg, char *buf, int cnt)
e7792ce2 588{
2f7f730a 589 struct i2c_client *client = priv->hdmi;
e66e03ab 590 u8 addr = REG2ADDR(reg);
e7792ce2
RC
591 int ret;
592
ed9a8426 593 mutex_lock(&priv->mutex);
7d2eadc9
JFM
594 ret = set_page(priv, reg);
595 if (ret < 0)
ed9a8426 596 goto out;
e7792ce2
RC
597
598 ret = i2c_master_send(client, &addr, sizeof(addr));
599 if (ret < 0)
600 goto fail;
601
602 ret = i2c_master_recv(client, buf, cnt);
603 if (ret < 0)
604 goto fail;
605
ed9a8426 606 goto out;
e7792ce2
RC
607
608fail:
609 dev_err(&client->dev, "Error %d reading from 0x%x\n", ret, reg);
ed9a8426
JFM
610out:
611 mutex_unlock(&priv->mutex);
e7792ce2
RC
612 return ret;
613}
614
ca510ead
LA
615#define MAX_WRITE_RANGE_BUF 32
616
c4c11dd1 617static void
e66e03ab 618reg_write_range(struct tda998x_priv *priv, u16 reg, u8 *p, int cnt)
c4c11dd1 619{
2f7f730a 620 struct i2c_client *client = priv->hdmi;
ca510ead
LA
621 /* This is the maximum size of the buffer passed in */
622 u8 buf[MAX_WRITE_RANGE_BUF + 1];
c4c11dd1
RK
623 int ret;
624
ca510ead
LA
625 if (cnt > MAX_WRITE_RANGE_BUF) {
626 dev_err(&client->dev, "Fixed write buffer too small (%d)\n",
627 MAX_WRITE_RANGE_BUF);
628 return;
629 }
630
c4c11dd1
RK
631 buf[0] = REG2ADDR(reg);
632 memcpy(&buf[1], p, cnt);
633
ed9a8426 634 mutex_lock(&priv->mutex);
7d2eadc9
JFM
635 ret = set_page(priv, reg);
636 if (ret < 0)
ed9a8426 637 goto out;
c4c11dd1
RK
638
639 ret = i2c_master_send(client, buf, cnt + 1);
640 if (ret < 0)
641 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
ed9a8426
JFM
642out:
643 mutex_unlock(&priv->mutex);
c4c11dd1
RK
644}
645
7d2eadc9 646static int
e66e03ab 647reg_read(struct tda998x_priv *priv, u16 reg)
e7792ce2 648{
e66e03ab 649 u8 val = 0;
7d2eadc9
JFM
650 int ret;
651
652 ret = reg_read_range(priv, reg, &val, sizeof(val));
653 if (ret < 0)
654 return ret;
e7792ce2
RC
655 return val;
656}
657
658static void
e66e03ab 659reg_write(struct tda998x_priv *priv, u16 reg, u8 val)
e7792ce2 660{
2f7f730a 661 struct i2c_client *client = priv->hdmi;
e66e03ab 662 u8 buf[] = {REG2ADDR(reg), val};
e7792ce2
RC
663 int ret;
664
ed9a8426 665 mutex_lock(&priv->mutex);
7d2eadc9
JFM
666 ret = set_page(priv, reg);
667 if (ret < 0)
ed9a8426 668 goto out;
e7792ce2 669
704d63f5 670 ret = i2c_master_send(client, buf, sizeof(buf));
e7792ce2
RC
671 if (ret < 0)
672 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
ed9a8426
JFM
673out:
674 mutex_unlock(&priv->mutex);
e7792ce2
RC
675}
676
677static void
e66e03ab 678reg_write16(struct tda998x_priv *priv, u16 reg, u16 val)
e7792ce2 679{
2f7f730a 680 struct i2c_client *client = priv->hdmi;
e66e03ab 681 u8 buf[] = {REG2ADDR(reg), val >> 8, val};
e7792ce2
RC
682 int ret;
683
ed9a8426 684 mutex_lock(&priv->mutex);
7d2eadc9
JFM
685 ret = set_page(priv, reg);
686 if (ret < 0)
ed9a8426 687 goto out;
e7792ce2 688
704d63f5 689 ret = i2c_master_send(client, buf, sizeof(buf));
e7792ce2
RC
690 if (ret < 0)
691 dev_err(&client->dev, "Error %d writing to 0x%x\n", ret, reg);
ed9a8426
JFM
692out:
693 mutex_unlock(&priv->mutex);
e7792ce2
RC
694}
695
696static void
e66e03ab 697reg_set(struct tda998x_priv *priv, u16 reg, u8 val)
e7792ce2 698{
7d2eadc9
JFM
699 int old_val;
700
701 old_val = reg_read(priv, reg);
702 if (old_val >= 0)
703 reg_write(priv, reg, old_val | val);
e7792ce2
RC
704}
705
706static void
e66e03ab 707reg_clear(struct tda998x_priv *priv, u16 reg, u8 val)
e7792ce2 708{
7d2eadc9
JFM
709 int old_val;
710
711 old_val = reg_read(priv, reg);
712 if (old_val >= 0)
713 reg_write(priv, reg, old_val & ~val);
e7792ce2
RC
714}
715
716static void
2f7f730a 717tda998x_reset(struct tda998x_priv *priv)
e7792ce2
RC
718{
719 /* reset audio and i2c master: */
81b53a16 720 reg_write(priv, REG_SOFTRESET, SOFTRESET_AUDIO | SOFTRESET_I2C_MASTER);
e7792ce2 721 msleep(50);
81b53a16 722 reg_write(priv, REG_SOFTRESET, 0);
e7792ce2
RC
723 msleep(50);
724
725 /* reset transmitter: */
2f7f730a
JFM
726 reg_set(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
727 reg_clear(priv, REG_MAIN_CNTRL0, MAIN_CNTRL0_SR);
e7792ce2
RC
728
729 /* PLL registers common configuration */
2f7f730a
JFM
730 reg_write(priv, REG_PLL_SERIAL_1, 0x00);
731 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(1));
732 reg_write(priv, REG_PLL_SERIAL_3, 0x00);
733 reg_write(priv, REG_SERIALIZER, 0x00);
734 reg_write(priv, REG_BUFFER_OUT, 0x00);
735 reg_write(priv, REG_PLL_SCG1, 0x00);
736 reg_write(priv, REG_AUDIO_DIV, AUDIO_DIV_SERCLK_8);
737 reg_write(priv, REG_SEL_CLK, SEL_CLK_SEL_CLK1 | SEL_CLK_ENA_SC_CLK);
738 reg_write(priv, REG_PLL_SCGN1, 0xfa);
739 reg_write(priv, REG_PLL_SCGN2, 0x00);
740 reg_write(priv, REG_PLL_SCGR1, 0x5b);
741 reg_write(priv, REG_PLL_SCGR2, 0x00);
742 reg_write(priv, REG_PLL_SCG2, 0x10);
bcb2481d
RK
743
744 /* Write the default value MUX register */
2f7f730a 745 reg_write(priv, REG_MUX_VP_VIP_OUT, 0x24);
e7792ce2
RC
746}
747
0fc6f44d
RK
748/*
749 * The TDA998x has a problem when trying to read the EDID close to a
750 * HPD assertion: it needs a delay of 100ms to avoid timing out while
751 * trying to read EDID data.
752 *
95a9b686 753 * However, tda998x_connector_get_modes() may be called at any moment
9525c4dd 754 * after tda998x_connector_detect() indicates that we are connected, so
95a9b686 755 * we need to delay probing modes in tda998x_connector_get_modes() after
0fc6f44d
RK
756 * we have seen a HPD inactive->active transition. This code implements
757 * that delay.
758 */
e99e88a9 759static void tda998x_edid_delay_done(struct timer_list *t)
0fc6f44d 760{
e99e88a9 761 struct tda998x_priv *priv = from_timer(priv, t, edid_delay_timer);
0fc6f44d
RK
762
763 priv->edid_delay_active = false;
764 wake_up(&priv->edid_delay_waitq);
765 schedule_work(&priv->detect_work);
766}
767
768static void tda998x_edid_delay_start(struct tda998x_priv *priv)
769{
770 priv->edid_delay_active = true;
771 mod_timer(&priv->edid_delay_timer, jiffies + HZ/10);
772}
773
774static int tda998x_edid_delay_wait(struct tda998x_priv *priv)
775{
776 return wait_event_killable(priv->edid_delay_waitq, !priv->edid_delay_active);
777}
778
779/*
780 * We need to run the KMS hotplug event helper outside of our threaded
781 * interrupt routine as this can call back into our get_modes method,
782 * which will want to make use of interrupts.
783 */
784static void tda998x_detect_work(struct work_struct *work)
6833d26e 785{
6833d26e 786 struct tda998x_priv *priv =
0fc6f44d 787 container_of(work, struct tda998x_priv, detect_work);
b1eb4f84 788 struct drm_device *dev = priv->connector.dev;
6833d26e 789
0fc6f44d
RK
790 if (dev)
791 drm_kms_helper_hotplug_event(dev);
6833d26e
JFM
792}
793
12473b7d
JFM
794/*
795 * only 2 interrupts may occur: screen plug/unplug and EDID read
796 */
797static irqreturn_t tda998x_irq_thread(int irq, void *data)
798{
799 struct tda998x_priv *priv = data;
800 u8 sta, cec, lvl, flag0, flag1, flag2;
f84a97d4 801 bool handled = false;
12473b7d 802
12473b7d 803 sta = cec_read(priv, REG_CEC_INTSTATUS);
ae81553c
RK
804 if (sta & CEC_INTSTATUS_HDMI) {
805 cec = cec_read(priv, REG_CEC_RXSHPDINT);
806 lvl = cec_read(priv, REG_CEC_RXSHPDLEV);
807 flag0 = reg_read(priv, REG_INT_FLAGS_0);
808 flag1 = reg_read(priv, REG_INT_FLAGS_1);
809 flag2 = reg_read(priv, REG_INT_FLAGS_2);
810 DRM_DEBUG_DRIVER(
811 "tda irq sta %02x cec %02x lvl %02x f0 %02x f1 %02x f2 %02x\n",
812 sta, cec, lvl, flag0, flag1, flag2);
813
814 if (cec & CEC_RXSHPDINT_HPD) {
7e8675f0 815 if (lvl & CEC_RXSHPDLEV_HPD) {
ae81553c 816 tda998x_edid_delay_start(priv);
7e8675f0 817 } else {
ae81553c 818 schedule_work(&priv->detect_work);
7e8675f0
RK
819 cec_notifier_set_phys_addr(priv->cec_notify,
820 CEC_PHYS_ADDR_INVALID);
821 }
ae81553c
RK
822
823 handled = true;
824 }
ec5d3e83 825
ae81553c
RK
826 if ((flag2 & INT_FLAGS_2_EDID_BLK_RD) && priv->wq_edid_wait) {
827 priv->wq_edid_wait = 0;
828 wake_up(&priv->wq_edid);
829 handled = true;
830 }
ec5d3e83
RK
831 }
832
f84a97d4 833 return IRQ_RETVAL(handled);
12473b7d
JFM
834}
835
c4c11dd1 836static void
e66e03ab 837tda998x_write_if(struct tda998x_priv *priv, u8 bit, u16 addr,
96795df1 838 union hdmi_infoframe *frame)
c4c11dd1 839{
ca510ead 840 u8 buf[MAX_WRITE_RANGE_BUF];
96795df1
RK
841 ssize_t len;
842
843 len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
844 if (len < 0) {
845 dev_err(&priv->hdmi->dev,
846 "hdmi_infoframe_pack() type=0x%02x failed: %zd\n",
847 frame->any.type, len);
848 return;
849 }
850
2f7f730a 851 reg_clear(priv, REG_DIP_IF_FLAGS, bit);
96795df1 852 reg_write_range(priv, addr, buf, len);
2f7f730a 853 reg_set(priv, REG_DIP_IF_FLAGS, bit);
c4c11dd1
RK
854}
855
900b2b72
RK
856static void tda998x_write_aif(struct tda998x_priv *priv,
857 const struct hdmi_audio_infoframe *cea)
c4c11dd1 858{
96795df1
RK
859 union hdmi_infoframe frame;
860
95db3b25 861 frame.audio = *cea;
4a6ca1a2 862
96795df1 863 tda998x_write_if(priv, DIP_IF_FLAGS_IF4, REG_IF4_HB0, &frame);
c4c11dd1
RK
864}
865
866static void
63f8f3ba 867tda998x_write_avi(struct tda998x_priv *priv, const struct drm_display_mode *mode)
c4c11dd1 868{
96795df1 869 union hdmi_infoframe frame;
8c7a075d 870
13d0add3
VS
871 drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
872 &priv->connector, mode);
96795df1 873 frame.avi.quantization_range = HDMI_QUANTIZATION_RANGE_FULL;
fcc22c5f
RK
874 drm_hdmi_avi_infoframe_quant_range(&frame.avi, &priv->connector, mode,
875 priv->rgb_quant_range);
8c7a075d 876
96795df1 877 tda998x_write_if(priv, DIP_IF_FLAGS_IF2, REG_IF2_HB0, &frame);
c4c11dd1
RK
878}
879
ad975f93
RK
880/* Audio support */
881
26f7bf12
RK
882static const struct tda998x_audio_route tda998x_audio_route[AUDIO_ROUTE_NUM] = {
883 [AUDIO_ROUTE_I2S] = {
884 .ena_aclk = 1,
885 .mux_ap = MUX_AP_SELECT_I2S,
886 .aip_clksel = AIP_CLKSEL_AIP_I2S | AIP_CLKSEL_FS_ACLK,
887 },
888 [AUDIO_ROUTE_SPDIF] = {
889 .ena_aclk = 0,
890 .mux_ap = MUX_AP_SELECT_SPDIF,
891 .aip_clksel = AIP_CLKSEL_AIP_SPDIF | AIP_CLKSEL_FS_FS64SPDIF,
892 },
893};
894
895/* Configure the TDA998x audio data and clock routing. */
896static int tda998x_derive_routing(struct tda998x_priv *priv,
897 struct tda998x_audio_settings *s,
898 unsigned int route)
899{
900 s->route = &tda998x_audio_route[route];
901 s->ena_ap = priv->audio_port_enable[route];
902 if (s->ena_ap == 0) {
903 dev_err(&priv->hdmi->dev, "no audio configuration found\n");
904 return -EINVAL;
905 }
906
907 return 0;
908}
909
7dad3740
RK
910/*
911 * The audio clock divisor register controls a divider producing Audio_Clk_Out
912 * from SERclk by dividing it by 2^n where 0 <= n <= 5. We don't know what
913 * Audio_Clk_Out or SERclk are. We guess SERclk is the same as TMDS clock.
914 *
915 * It seems that Audio_Clk_Out must be the smallest value that is greater
916 * than 128*fs, otherwise audio does not function. There is some suggestion
917 * that 126*fs is a better value.
918 */
919static u8 tda998x_get_adiv(struct tda998x_priv *priv, unsigned int fs)
920{
921 unsigned long min_audio_clk = fs * 128;
922 unsigned long ser_clk = priv->tmds_clock * 1000;
923 u8 adiv;
924
925 for (adiv = AUDIO_DIV_SERCLK_32; adiv != AUDIO_DIV_SERCLK_1; adiv--)
926 if (ser_clk > min_audio_clk << adiv)
927 break;
928
929 dev_dbg(&priv->hdmi->dev,
930 "ser_clk=%luHz fs=%uHz min_aclk=%luHz adiv=%d\n",
931 ser_clk, fs, min_audio_clk, adiv);
932
933 return adiv;
934}
935
a03a915b
RK
936/*
937 * In auto-CTS mode, the TDA998x uses a "measured time stamp" counter to
938 * generate the CTS value. It appears that the "measured time stamp" is
939 * the number of TDMS clock cycles within a number of audio input clock
940 * cycles defined by the k and N parameters defined below, in a similar
941 * way to that which is set out in the CTS generation in the HDMI spec.
942 *
943 * tmdsclk ----> mts -> /m ---> CTS
944 * ^
945 * sclk -> /k -> /N
946 *
947 * CTS = mts / m, where m is 2^M.
948 * /k is a divider based on the K value below, K+1 for K < 4, or 8 for K >= 4
949 * /N is a divider based on the HDMI specified N value.
950 *
951 * This produces the following equation:
952 * CTS = tmds_clock * k * N / (sclk * m)
953 *
954 * When combined with the sink-side equation, and realising that sclk is
955 * bclk_ratio * fs, we end up with:
956 * k = m * bclk_ratio / 128.
957 *
958 * Note: S/PDIF always uses a bclk_ratio of 64.
959 */
960static int tda998x_derive_cts_n(struct tda998x_priv *priv,
961 struct tda998x_audio_settings *settings,
962 unsigned int ratio)
963{
964 switch (ratio) {
965 case 16:
966 settings->cts_n = CTS_N_M(3) | CTS_N_K(0);
967 break;
968 case 32:
969 settings->cts_n = CTS_N_M(3) | CTS_N_K(1);
970 break;
971 case 48:
972 settings->cts_n = CTS_N_M(3) | CTS_N_K(2);
973 break;
974 case 64:
975 settings->cts_n = CTS_N_M(3) | CTS_N_K(3);
976 break;
977 case 128:
978 settings->cts_n = CTS_N_M(0) | CTS_N_K(0);
979 break;
980 default:
981 dev_err(&priv->hdmi->dev, "unsupported bclk ratio %ufs\n",
982 ratio);
983 return -EINVAL;
984 }
985 return 0;
986}
987
2f7f730a 988static void tda998x_audio_mute(struct tda998x_priv *priv, bool on)
c4c11dd1
RK
989{
990 if (on) {
2f7f730a
JFM
991 reg_set(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
992 reg_clear(priv, REG_SOFTRESET, SOFTRESET_AUDIO);
993 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
c4c11dd1 994 } else {
2f7f730a 995 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
c4c11dd1
RK
996 }
997}
998
900b2b72 999static void tda998x_configure_audio(struct tda998x_priv *priv)
c4c11dd1 1000{
900b2b72 1001 const struct tda998x_audio_settings *settings = &priv->audio;
26f7bf12 1002 u8 buf[6], adiv;
e66e03ab 1003 u32 n;
c4c11dd1 1004
82642ab7
RK
1005 /* If audio is not configured, there is nothing to do. */
1006 if (settings->ena_ap == 0)
900b2b72 1007 return;
82642ab7 1008
125a4f93 1009 adiv = tda998x_get_adiv(priv, settings->sample_rate);
7dad3740 1010
c4c11dd1 1011 /* Enable audio ports */
82642ab7 1012 reg_write(priv, REG_ENA_AP, settings->ena_ap);
26f7bf12
RK
1013 reg_write(priv, REG_ENA_ACLK, settings->route->ena_aclk);
1014 reg_write(priv, REG_MUX_AP, settings->route->mux_ap);
935b9ca3 1015 reg_write(priv, REG_I2S_FORMAT, settings->i2s_format);
26f7bf12 1016 reg_write(priv, REG_AIP_CLKSEL, settings->route->aip_clksel);
a8b517e5
JFM
1017 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_LAYOUT |
1018 AIP_CNTRL_0_ACR_MAN); /* auto CTS */
a03a915b 1019 reg_write(priv, REG_CTS_N, settings->cts_n);
2f7f730a 1020 reg_write(priv, REG_AUDIO_DIV, adiv);
c4c11dd1
RK
1021
1022 /*
1023 * This is the approximate value of N, which happens to be
1024 * the recommended values for non-coherent clocks.
1025 */
125a4f93 1026 n = 128 * settings->sample_rate / 1000;
c4c11dd1
RK
1027
1028 /* Write the CTS and N values */
1029 buf[0] = 0x44;
1030 buf[1] = 0x42;
1031 buf[2] = 0x01;
1032 buf[3] = n;
1033 buf[4] = n >> 8;
1034 buf[5] = n >> 16;
2f7f730a 1035 reg_write_range(priv, REG_ACR_CTS_0, buf, 6);
c4c11dd1 1036
c4c11dd1 1037 /* Reset CTS generator */
2f7f730a
JFM
1038 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
1039 reg_clear(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_CTS);
c4c11dd1 1040
95db3b25
JS
1041 /* Write the channel status
1042 * The REG_CH_STAT_B-registers skip IEC958 AES2 byte, because
1043 * there is a separate register for each I2S wire.
1044 */
125a4f93
RK
1045 buf[0] = settings->status[0];
1046 buf[1] = settings->status[1];
1047 buf[2] = settings->status[3];
1048 buf[3] = settings->status[4];
2f7f730a 1049 reg_write_range(priv, REG_CH_STAT_B(0), buf, 4);
c4c11dd1 1050
2f7f730a 1051 tda998x_audio_mute(priv, true);
73d5e253 1052 msleep(20);
2f7f730a 1053 tda998x_audio_mute(priv, false);
c4c11dd1 1054
125a4f93 1055 tda998x_write_aif(priv, &settings->cea);
c4c11dd1
RK
1056}
1057
ad975f93
RK
1058static int tda998x_audio_hw_params(struct device *dev, void *data,
1059 struct hdmi_codec_daifmt *daifmt,
1060 struct hdmi_codec_params *params)
1061{
1062 struct tda998x_priv *priv = dev_get_drvdata(dev);
a03a915b 1063 unsigned int bclk_ratio;
935b9ca3 1064 bool spdif = daifmt->fmt == HDMI_SPDIF;
71689160 1065 int ret;
2e9943aa 1066 struct tda998x_audio_settings audio = {
125a4f93
RK
1067 .sample_rate = params->sample_rate,
1068 .cea = params->cea,
ad975f93
RK
1069 };
1070
125a4f93
RK
1071 memcpy(audio.status, params->iec.status,
1072 min(sizeof(audio.status), sizeof(params->iec.status)));
ad975f93
RK
1073
1074 switch (daifmt->fmt) {
1075 case HDMI_I2S:
935b9ca3
RK
1076 audio.i2s_format = I2S_FORMAT_PHILIPS;
1077 break;
1078 case HDMI_LEFT_J:
1079 audio.i2s_format = I2S_FORMAT_LEFT_J;
1080 break;
1081 case HDMI_RIGHT_J:
1082 audio.i2s_format = I2S_FORMAT_RIGHT_J;
ad975f93
RK
1083 break;
1084 case HDMI_SPDIF:
935b9ca3 1085 audio.i2s_format = 0;
ad975f93
RK
1086 break;
1087 default:
1088 dev_err(dev, "%s: Invalid format %d\n", __func__, daifmt->fmt);
1089 return -EINVAL;
1090 }
1091
935b9ca3
RK
1092 if (!spdif &&
1093 (daifmt->bit_clk_inv || daifmt->frame_clk_inv ||
1094 daifmt->bit_clk_master || daifmt->frame_clk_master)) {
1095 dev_err(dev, "%s: Bad flags %d %d %d %d\n", __func__,
1096 daifmt->bit_clk_inv, daifmt->frame_clk_inv,
1097 daifmt->bit_clk_master,
1098 daifmt->frame_clk_master);
1099 return -EINVAL;
1100 }
1101
26f7bf12
RK
1102 ret = tda998x_derive_routing(priv, &audio, AUDIO_ROUTE_I2S + spdif);
1103 if (ret < 0)
1104 return ret;
1105
a03a915b
RK
1106 bclk_ratio = spdif ? 64 : params->sample_width * 2;
1107 ret = tda998x_derive_cts_n(priv, &audio, bclk_ratio);
1108 if (ret < 0)
1109 return ret;
1110
ad975f93 1111 mutex_lock(&priv->audio_mutex);
900b2b72 1112 priv->audio = audio;
ad975f93 1113 if (priv->supports_infoframes && priv->sink_has_audio)
900b2b72 1114 tda998x_configure_audio(priv);
ad975f93
RK
1115 mutex_unlock(&priv->audio_mutex);
1116
900b2b72 1117 return 0;
ad975f93
RK
1118}
1119
1120static void tda998x_audio_shutdown(struct device *dev, void *data)
1121{
1122 struct tda998x_priv *priv = dev_get_drvdata(dev);
1123
1124 mutex_lock(&priv->audio_mutex);
1125
1126 reg_write(priv, REG_ENA_AP, 0);
82642ab7 1127 priv->audio.ena_ap = 0;
ad975f93
RK
1128
1129 mutex_unlock(&priv->audio_mutex);
1130}
1131
1132int tda998x_audio_digital_mute(struct device *dev, void *data, bool enable)
1133{
1134 struct tda998x_priv *priv = dev_get_drvdata(dev);
1135
1136 mutex_lock(&priv->audio_mutex);
1137
1138 tda998x_audio_mute(priv, enable);
1139
1140 mutex_unlock(&priv->audio_mutex);
1141 return 0;
1142}
1143
1144static int tda998x_audio_get_eld(struct device *dev, void *data,
1145 uint8_t *buf, size_t len)
1146{
1147 struct tda998x_priv *priv = dev_get_drvdata(dev);
ad975f93 1148
02efac0f
RK
1149 mutex_lock(&priv->audio_mutex);
1150 memcpy(buf, priv->connector.eld,
1151 min(sizeof(priv->connector.eld), len));
1152 mutex_unlock(&priv->audio_mutex);
1153
1154 return 0;
ad975f93
RK
1155}
1156
1157static const struct hdmi_codec_ops audio_codec_ops = {
1158 .hw_params = tda998x_audio_hw_params,
1159 .audio_shutdown = tda998x_audio_shutdown,
1160 .digital_mute = tda998x_audio_digital_mute,
1161 .get_eld = tda998x_audio_get_eld,
1162};
1163
1164static int tda998x_audio_codec_init(struct tda998x_priv *priv,
1165 struct device *dev)
1166{
1167 struct hdmi_codec_pdata codec_data = {
1168 .ops = &audio_codec_ops,
1169 .max_i2s_channels = 2,
1170 };
ad975f93 1171
71689160
RK
1172 if (priv->audio_port_enable[AUDIO_ROUTE_I2S])
1173 codec_data.i2s = 1;
1174 if (priv->audio_port_enable[AUDIO_ROUTE_SPDIF])
1175 codec_data.spdif = 1;
ad975f93
RK
1176
1177 priv->audio_pdev = platform_device_register_data(
1178 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
1179 &codec_data, sizeof(codec_data));
1180
1181 return PTR_ERR_OR_ZERO(priv->audio_pdev);
1182}
1183
25576733
RK
1184/* DRM connector functions */
1185
25576733
RK
1186static enum drm_connector_status
1187tda998x_connector_detect(struct drm_connector *connector, bool force)
1188{
1189 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
1190 u8 val = cec_read(priv, REG_CEC_RXSHPDLEV);
1191
1192 return (val & CEC_RXSHPDLEV_HPD) ? connector_status_connected :
1193 connector_status_disconnected;
1194}
1195
1196static void tda998x_connector_destroy(struct drm_connector *connector)
1197{
1198 drm_connector_cleanup(connector);
1199}
1200
1201static const struct drm_connector_funcs tda998x_connector_funcs = {
25576733 1202 .reset = drm_atomic_helper_connector_reset,
a3d335f5 1203 .fill_modes = drm_helper_probe_single_connector_modes,
25576733
RK
1204 .detect = tda998x_connector_detect,
1205 .destroy = tda998x_connector_destroy,
1206 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1207 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1208};
1209
1210static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
1211{
1212 struct tda998x_priv *priv = data;
1213 u8 offset, segptr;
1214 int ret, i;
1215
1216 offset = (blk & 1) ? 128 : 0;
1217 segptr = blk / 2;
1218
7e8675f0
RK
1219 mutex_lock(&priv->edid_mutex);
1220
25576733
RK
1221 reg_write(priv, REG_DDC_ADDR, 0xa0);
1222 reg_write(priv, REG_DDC_OFFS, offset);
1223 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
1224 reg_write(priv, REG_DDC_SEGM, segptr);
1225
1226 /* enable reading EDID: */
1227 priv->wq_edid_wait = 1;
1228 reg_write(priv, REG_EDID_CTRL, 0x1);
1229
1230 /* flag must be cleared by sw: */
1231 reg_write(priv, REG_EDID_CTRL, 0x0);
1232
1233 /* wait for block read to complete: */
1234 if (priv->hdmi->irq) {
1235 i = wait_event_timeout(priv->wq_edid,
1236 !priv->wq_edid_wait,
1237 msecs_to_jiffies(100));
1238 if (i < 0) {
1239 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
7e8675f0
RK
1240 ret = i;
1241 goto failed;
25576733
RK
1242 }
1243 } else {
1244 for (i = 100; i > 0; i--) {
1245 msleep(1);
1246 ret = reg_read(priv, REG_INT_FLAGS_2);
1247 if (ret < 0)
7e8675f0 1248 goto failed;
25576733
RK
1249 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1250 break;
1251 }
1252 }
1253
1254 if (i == 0) {
1255 dev_err(&priv->hdmi->dev, "read edid timeout\n");
7e8675f0
RK
1256 ret = -ETIMEDOUT;
1257 goto failed;
25576733
RK
1258 }
1259
1260 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1261 if (ret != length) {
1262 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1263 blk, ret);
7e8675f0 1264 goto failed;
25576733
RK
1265 }
1266
7e8675f0
RK
1267 ret = 0;
1268
1269 failed:
1270 mutex_unlock(&priv->edid_mutex);
1271 return ret;
25576733
RK
1272}
1273
1274static int tda998x_connector_get_modes(struct drm_connector *connector)
1275{
1276 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
1277 struct edid *edid;
1278 int n;
1279
1280 /*
1281 * If we get killed while waiting for the HPD timeout, return
1282 * no modes found: we are not in a restartable path, so we
1283 * can't handle signals gracefully.
1284 */
1285 if (tda998x_edid_delay_wait(priv))
1286 return 0;
1287
1288 if (priv->rev == TDA19988)
1289 reg_clear(priv, REG_TX4, TX4_PD_RAM);
1290
1291 edid = drm_do_get_edid(connector, read_edid_block, priv);
1292
1293 if (priv->rev == TDA19988)
1294 reg_set(priv, REG_TX4, TX4_PD_RAM);
1295
1296 if (!edid) {
1297 dev_warn(&priv->hdmi->dev, "failed to read EDID\n");
1298 return 0;
1299 }
1300
c555f023 1301 drm_connector_update_edid_property(connector, edid);
a3d335f5
RK
1302 cec_notifier_set_phys_addr_from_edid(priv->cec_notify, edid);
1303
1304 mutex_lock(&priv->audio_mutex);
25576733 1305 n = drm_add_edid_modes(connector, edid);
a3d335f5
RK
1306 priv->sink_has_audio = drm_detect_monitor_audio(edid);
1307 mutex_unlock(&priv->audio_mutex);
25576733
RK
1308
1309 kfree(edid);
1310
1311 return n;
1312}
1313
25576733
RK
1314static struct drm_encoder *
1315tda998x_connector_best_encoder(struct drm_connector *connector)
1316{
1317 struct tda998x_priv *priv = conn_to_tda998x_priv(connector);
1318
30bd8b86 1319 return priv->bridge.encoder;
25576733
RK
1320}
1321
1322static
1323const struct drm_connector_helper_funcs tda998x_connector_helper_funcs = {
1324 .get_modes = tda998x_connector_get_modes,
25576733
RK
1325 .best_encoder = tda998x_connector_best_encoder,
1326};
1327
a2f75662
RK
1328static int tda998x_connector_init(struct tda998x_priv *priv,
1329 struct drm_device *drm)
1330{
1331 struct drm_connector *connector = &priv->connector;
1332 int ret;
1333
1334 connector->interlace_allowed = 1;
1335
1336 if (priv->hdmi->irq)
1337 connector->polled = DRM_CONNECTOR_POLL_HPD;
1338 else
1339 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
1340 DRM_CONNECTOR_POLL_DISCONNECT;
1341
1342 drm_connector_helper_add(connector, &tda998x_connector_helper_funcs);
1343 ret = drm_connector_init(drm, connector, &tda998x_connector_funcs,
1344 DRM_MODE_CONNECTOR_HDMIA);
1345 if (ret)
1346 return ret;
1347
a7ccc5a4
DA
1348 drm_connector_attach_encoder(&priv->connector,
1349 priv->bridge.encoder);
a2f75662
RK
1350
1351 return 0;
1352}
1353
30bd8b86 1354/* DRM bridge functions */
e7792ce2 1355
30bd8b86 1356static int tda998x_bridge_attach(struct drm_bridge *bridge)
e7792ce2 1357{
30bd8b86 1358 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
9525c4dd 1359
30bd8b86
RK
1360 return tda998x_connector_init(priv, bridge->dev);
1361}
e7792ce2 1362
30bd8b86
RK
1363static void tda998x_bridge_detach(struct drm_bridge *bridge)
1364{
1365 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
e7792ce2 1366
30bd8b86
RK
1367 drm_connector_cleanup(&priv->connector);
1368}
e7792ce2 1369
b073a70e
RK
1370static enum drm_mode_status tda998x_bridge_mode_valid(struct drm_bridge *bridge,
1371 const struct drm_display_mode *mode)
1372{
1373 /* TDA19988 dotclock can go up to 165MHz */
1374 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
1375
1376 if (mode->clock > ((priv->rev == TDA19988) ? 165000 : 150000))
1377 return MODE_CLOCK_HIGH;
1378 if (mode->htotal >= BIT(13))
1379 return MODE_BAD_HVALUE;
1380 if (mode->vtotal >= BIT(11))
1381 return MODE_BAD_VVALUE;
1382 return MODE_OK;
1383}
1384
30bd8b86 1385static void tda998x_bridge_enable(struct drm_bridge *bridge)
e7792ce2 1386{
30bd8b86
RK
1387 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
1388
2c6e7583 1389 if (!priv->is_on) {
c4c11dd1 1390 /* enable video ports, audio will be enabled later */
2f7f730a
JFM
1391 reg_write(priv, REG_ENA_VP_0, 0xff);
1392 reg_write(priv, REG_ENA_VP_1, 0xff);
1393 reg_write(priv, REG_ENA_VP_2, 0xff);
e7792ce2 1394 /* set muxing after enabling ports: */
2f7f730a
JFM
1395 reg_write(priv, REG_VIP_CNTRL_0, priv->vip_cntrl_0);
1396 reg_write(priv, REG_VIP_CNTRL_1, priv->vip_cntrl_1);
1397 reg_write(priv, REG_VIP_CNTRL_2, priv->vip_cntrl_2);
3cb43378
RK
1398
1399 priv->is_on = true;
2c6e7583
PR
1400 }
1401}
1402
30bd8b86 1403static void tda998x_bridge_disable(struct drm_bridge *bridge)
2c6e7583 1404{
30bd8b86
RK
1405 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
1406
2c6e7583 1407 if (priv->is_on) {
db6aaf4d 1408 /* disable video ports */
2f7f730a
JFM
1409 reg_write(priv, REG_ENA_VP_0, 0x00);
1410 reg_write(priv, REG_ENA_VP_1, 0x00);
1411 reg_write(priv, REG_ENA_VP_2, 0x00);
e7792ce2 1412
3cb43378
RK
1413 priv->is_on = false;
1414 }
e7792ce2
RC
1415}
1416
30bd8b86 1417static void tda998x_bridge_mode_set(struct drm_bridge *bridge,
63f8f3ba
LP
1418 const struct drm_display_mode *mode,
1419 const struct drm_display_mode *adjusted_mode)
e7792ce2 1420{
30bd8b86 1421 struct tda998x_priv *priv = bridge_to_tda998x_priv(bridge);
926a299c 1422 unsigned long tmds_clock;
e66e03ab
RK
1423 u16 ref_pix, ref_line, n_pix, n_line;
1424 u16 hs_pix_s, hs_pix_e;
1425 u16 vs1_pix_s, vs1_pix_e, vs1_line_s, vs1_line_e;
1426 u16 vs2_pix_s, vs2_pix_e, vs2_line_s, vs2_line_e;
1427 u16 vwin1_line_s, vwin1_line_e;
1428 u16 vwin2_line_s, vwin2_line_e;
1429 u16 de_pix_s, de_pix_e;
2807ba75 1430 u8 reg, div, rep, sel_clk;
e7792ce2 1431
fcc22c5f
RK
1432 /*
1433 * Since we are "computer" like, our source invariably produces
1434 * full-range RGB. If the monitor supports full-range, then use
1435 * it, otherwise reduce to limited-range.
1436 */
1437 priv->rgb_quant_range =
1438 priv->connector.display_info.rgb_quant_range_selectable ?
1439 HDMI_QUANTIZATION_RANGE_FULL :
1440 drm_default_rgb_quant_range(adjusted_mode);
1441
088d61d1
SH
1442 /*
1443 * Internally TDA998x is using ITU-R BT.656 style sync but
1444 * we get VESA style sync. TDA998x is using a reference pixel
1445 * relative to ITU to sync to the input frame and for output
1446 * sync generation. Currently, we are using reference detection
1447 * from HS/VS, i.e. REFPIX/REFLINE denote frame start sync point
1448 * which is position of rising VS with coincident rising HS.
1449 *
1450 * Now there is some issues to take care of:
1451 * - HDMI data islands require sync-before-active
1452 * - TDA998x register values must be > 0 to be enabled
1453 * - REFLINE needs an additional offset of +1
1454 * - REFPIX needs an addtional offset of +1 for UYUV and +3 for RGB
1455 *
1456 * So we add +1 to all horizontal and vertical register values,
1457 * plus an additional +3 for REFPIX as we are using RGB input only.
e7792ce2 1458 */
088d61d1
SH
1459 n_pix = mode->htotal;
1460 n_line = mode->vtotal;
1461
1462 hs_pix_e = mode->hsync_end - mode->hdisplay;
1463 hs_pix_s = mode->hsync_start - mode->hdisplay;
1464 de_pix_e = mode->htotal;
1465 de_pix_s = mode->htotal - mode->hdisplay;
1466 ref_pix = 3 + hs_pix_s;
1467
179f1aa4
SH
1468 /*
1469 * Attached LCD controllers may generate broken sync. Allow
1470 * those to adjust the position of the rising VS edge by adding
1471 * HSKEW to ref_pix.
1472 */
1473 if (adjusted_mode->flags & DRM_MODE_FLAG_HSKEW)
1474 ref_pix += adjusted_mode->hskew;
1475
088d61d1
SH
1476 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) == 0) {
1477 ref_line = 1 + mode->vsync_start - mode->vdisplay;
1478 vwin1_line_s = mode->vtotal - mode->vdisplay - 1;
1479 vwin1_line_e = vwin1_line_s + mode->vdisplay;
1480 vs1_pix_s = vs1_pix_e = hs_pix_s;
1481 vs1_line_s = mode->vsync_start - mode->vdisplay;
1482 vs1_line_e = vs1_line_s +
1483 mode->vsync_end - mode->vsync_start;
1484 vwin2_line_s = vwin2_line_e = 0;
1485 vs2_pix_s = vs2_pix_e = 0;
1486 vs2_line_s = vs2_line_e = 0;
1487 } else {
1488 ref_line = 1 + (mode->vsync_start - mode->vdisplay)/2;
1489 vwin1_line_s = (mode->vtotal - mode->vdisplay)/2;
1490 vwin1_line_e = vwin1_line_s + mode->vdisplay/2;
1491 vs1_pix_s = vs1_pix_e = hs_pix_s;
1492 vs1_line_s = (mode->vsync_start - mode->vdisplay)/2;
1493 vs1_line_e = vs1_line_s +
1494 (mode->vsync_end - mode->vsync_start)/2;
1495 vwin2_line_s = vwin1_line_s + mode->vtotal/2;
1496 vwin2_line_e = vwin2_line_s + mode->vdisplay/2;
1497 vs2_pix_s = vs2_pix_e = hs_pix_s + mode->htotal/2;
1498 vs2_line_s = vs1_line_s + mode->vtotal/2 ;
1499 vs2_line_e = vs2_line_s +
1500 (mode->vsync_end - mode->vsync_start)/2;
1501 }
e7792ce2 1502
2807ba75
RK
1503 /*
1504 * Select pixel repeat depending on the double-clock flag
1505 * (which means we have to repeat each pixel once.)
1506 */
1507 rep = mode->flags & DRM_MODE_FLAG_DBLCLK ? 1 : 0;
1508 sel_clk = SEL_CLK_ENA_SC_CLK | SEL_CLK_SEL_CLK1 |
1509 SEL_CLK_SEL_VRF_CLK(rep ? 2 : 0);
1510
1511 /* the TMDS clock is scaled up by the pixel repeat */
1512 tmds_clock = mode->clock * (1 + rep);
926a299c
RK
1513
1514 /*
1515 * The divisor is power-of-2. The TDA9983B datasheet gives
1516 * this as ranges of Msample/s, which is 10x the TMDS clock:
1517 * 0 - 800 to 1500 Msample/s
1518 * 1 - 400 to 800 Msample/s
1519 * 2 - 200 to 400 Msample/s
1520 * 3 - as 2 above
1521 */
1522 for (div = 0; div < 3; div++)
1523 if (80000 >> div <= tmds_clock)
1524 break;
e7792ce2 1525
2cae8e02
RK
1526 mutex_lock(&priv->audio_mutex);
1527
2807ba75
RK
1528 priv->tmds_clock = tmds_clock;
1529
e7792ce2 1530 /* mute the audio FIFO: */
2f7f730a 1531 reg_set(priv, REG_AIP_CNTRL_0, AIP_CNTRL_0_RST_FIFO);
e7792ce2
RC
1532
1533 /* set HDMI HDCP mode off: */
81b53a16 1534 reg_write(priv, REG_TBG_CNTRL_1, TBG_CNTRL_1_DWIN_DIS);
2f7f730a
JFM
1535 reg_clear(priv, REG_TX33, TX33_HDMI);
1536 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(0));
e7792ce2 1537
e7792ce2 1538 /* no pre-filter or interpolator: */
2f7f730a 1539 reg_write(priv, REG_HVF_CNTRL_0, HVF_CNTRL_0_PREFIL(0) |
e7792ce2 1540 HVF_CNTRL_0_INTPOL(0));
9476ed2e 1541 reg_set(priv, REG_FEAT_POWERDOWN, FEAT_POWERDOWN_PREFILT);
2f7f730a
JFM
1542 reg_write(priv, REG_VIP_CNTRL_5, VIP_CNTRL_5_SP_CNT(0));
1543 reg_write(priv, REG_VIP_CNTRL_4, VIP_CNTRL_4_BLANKIT(0) |
e7792ce2 1544 VIP_CNTRL_4_BLC(0));
e7792ce2 1545
2f7f730a 1546 reg_clear(priv, REG_PLL_SERIAL_1, PLL_SERIAL_1_SRL_MAN_IZ);
a8b517e5
JFM
1547 reg_clear(priv, REG_PLL_SERIAL_3, PLL_SERIAL_3_SRL_CCIR |
1548 PLL_SERIAL_3_SRL_DE);
2f7f730a
JFM
1549 reg_write(priv, REG_SERIALIZER, 0);
1550 reg_write(priv, REG_HVF_CNTRL_1, HVF_CNTRL_1_VQR(0));
e7792ce2 1551
2807ba75
RK
1552 reg_write(priv, REG_RPT_CNTRL, RPT_CNTRL_REPEAT(rep));
1553 reg_write(priv, REG_SEL_CLK, sel_clk);
2f7f730a 1554 reg_write(priv, REG_PLL_SERIAL_2, PLL_SERIAL_2_SRL_NOSC(div) |
e7792ce2
RC
1555 PLL_SERIAL_2_SRL_PR(rep));
1556
fcc22c5f
RK
1557 /* set color matrix according to output rgb quant range */
1558 if (priv->rgb_quant_range == HDMI_QUANTIZATION_RANGE_LIMITED) {
1559 static u8 tda998x_full_to_limited_range[] = {
1560 MAT_CONTRL_MAT_SC(2),
1561 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1562 0x03, 0x6f, 0x00, 0x00, 0x00, 0x00,
1563 0x00, 0x00, 0x03, 0x6f, 0x00, 0x00,
1564 0x00, 0x00, 0x00, 0x00, 0x03, 0x6f,
1565 0x00, 0x40, 0x00, 0x40, 0x00, 0x40
1566 };
1567 reg_clear(priv, REG_FEAT_POWERDOWN, FEAT_POWERDOWN_CSC);
1568 reg_write_range(priv, REG_MAT_CONTRL,
1569 tda998x_full_to_limited_range,
1570 sizeof(tda998x_full_to_limited_range));
1571 } else {
1572 reg_write(priv, REG_MAT_CONTRL, MAT_CONTRL_MAT_BP |
1573 MAT_CONTRL_MAT_SC(1));
1574 reg_set(priv, REG_FEAT_POWERDOWN, FEAT_POWERDOWN_CSC);
1575 }
e7792ce2
RC
1576
1577 /* set BIAS tmds value: */
2f7f730a 1578 reg_write(priv, REG_ANA_GENERAL, 0x09);
e7792ce2 1579
088d61d1
SH
1580 /*
1581 * Sync on rising HSYNC/VSYNC
1582 */
81b53a16 1583 reg = VIP_CNTRL_3_SYNC_HS;
088d61d1
SH
1584
1585 /*
1586 * TDA19988 requires high-active sync at input stage,
1587 * so invert low-active sync provided by master encoder here
1588 */
1589 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
81b53a16 1590 reg |= VIP_CNTRL_3_H_TGL;
e7792ce2 1591 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
81b53a16
JFM
1592 reg |= VIP_CNTRL_3_V_TGL;
1593 reg_write(priv, REG_VIP_CNTRL_3, reg);
2f7f730a
JFM
1594
1595 reg_write(priv, REG_VIDFORMAT, 0x00);
1596 reg_write16(priv, REG_REFPIX_MSB, ref_pix);
1597 reg_write16(priv, REG_REFLINE_MSB, ref_line);
1598 reg_write16(priv, REG_NPIX_MSB, n_pix);
1599 reg_write16(priv, REG_NLINE_MSB, n_line);
1600 reg_write16(priv, REG_VS_LINE_STRT_1_MSB, vs1_line_s);
1601 reg_write16(priv, REG_VS_PIX_STRT_1_MSB, vs1_pix_s);
1602 reg_write16(priv, REG_VS_LINE_END_1_MSB, vs1_line_e);
1603 reg_write16(priv, REG_VS_PIX_END_1_MSB, vs1_pix_e);
1604 reg_write16(priv, REG_VS_LINE_STRT_2_MSB, vs2_line_s);
1605 reg_write16(priv, REG_VS_PIX_STRT_2_MSB, vs2_pix_s);
1606 reg_write16(priv, REG_VS_LINE_END_2_MSB, vs2_line_e);
1607 reg_write16(priv, REG_VS_PIX_END_2_MSB, vs2_pix_e);
1608 reg_write16(priv, REG_HS_PIX_START_MSB, hs_pix_s);
1609 reg_write16(priv, REG_HS_PIX_STOP_MSB, hs_pix_e);
1610 reg_write16(priv, REG_VWIN_START_1_MSB, vwin1_line_s);
1611 reg_write16(priv, REG_VWIN_END_1_MSB, vwin1_line_e);
1612 reg_write16(priv, REG_VWIN_START_2_MSB, vwin2_line_s);
1613 reg_write16(priv, REG_VWIN_END_2_MSB, vwin2_line_e);
1614 reg_write16(priv, REG_DE_START_MSB, de_pix_s);
1615 reg_write16(priv, REG_DE_STOP_MSB, de_pix_e);
e7792ce2
RC
1616
1617 if (priv->rev == TDA19988) {
1618 /* let incoming pixels fill the active space (if any) */
2f7f730a 1619 reg_write(priv, REG_ENABLE_SPACE, 0x00);
e7792ce2
RC
1620 }
1621
81b53a16
JFM
1622 /*
1623 * Always generate sync polarity relative to input sync and
1624 * revert input stage toggled sync at output stage
1625 */
1626 reg = TBG_CNTRL_1_DWIN_DIS | TBG_CNTRL_1_TGL_EN;
1627 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
1628 reg |= TBG_CNTRL_1_H_TGL;
1629 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
1630 reg |= TBG_CNTRL_1_V_TGL;
1631 reg_write(priv, REG_TBG_CNTRL_1, reg);
1632
e7792ce2 1633 /* must be last register set: */
81b53a16 1634 reg_write(priv, REG_TBG_CNTRL_0, 0);
c4c11dd1 1635
896a4130
RK
1636 /* CEA-861B section 6 says that:
1637 * CEA version 1 (CEA-861) has no support for infoframes.
1638 * CEA version 2 (CEA-861A) supports version 1 AVI infoframes,
1639 * and optional basic audio.
1640 * CEA version 3 (CEA-861B) supports version 1 and 2 AVI infoframes,
1641 * and optional digital audio, with audio infoframes.
1642 *
1643 * Since we only support generation of version 2 AVI infoframes,
1644 * ignore CEA version 2 and below (iow, behave as if we're a
1645 * CEA-861 source.)
1646 */
1647 priv->supports_infoframes = priv->connector.display_info.cea_rev >= 3;
1648
1649 if (priv->supports_infoframes) {
c4c11dd1 1650 /* We need to turn HDMI HDCP stuff on to get audio through */
81b53a16
JFM
1651 reg &= ~TBG_CNTRL_1_DWIN_DIS;
1652 reg_write(priv, REG_TBG_CNTRL_1, reg);
2f7f730a
JFM
1653 reg_write(priv, REG_ENC_CNTRL, ENC_CNTRL_CTL_CODE(1));
1654 reg_set(priv, REG_TX33, TX33_HDMI);
c4c11dd1 1655
2f7f730a 1656 tda998x_write_avi(priv, adjusted_mode);
c4c11dd1 1657
82642ab7 1658 if (priv->sink_has_audio)
900b2b72 1659 tda998x_configure_audio(priv);
c4c11dd1 1660 }
319e658c
RK
1661
1662 mutex_unlock(&priv->audio_mutex);
e7792ce2
RC
1663}
1664
30bd8b86
RK
1665static const struct drm_bridge_funcs tda998x_bridge_funcs = {
1666 .attach = tda998x_bridge_attach,
1667 .detach = tda998x_bridge_detach,
b073a70e 1668 .mode_valid = tda998x_bridge_mode_valid,
30bd8b86
RK
1669 .disable = tda998x_bridge_disable,
1670 .mode_set = tda998x_bridge_mode_set,
1671 .enable = tda998x_bridge_enable,
1672};
a8f4d4d6 1673
e7792ce2
RC
1674/* I2C driver functions */
1675
7e567624
JS
1676static int tda998x_get_audio_ports(struct tda998x_priv *priv,
1677 struct device_node *np)
1678{
1679 const u32 *port_data;
1680 u32 size;
1681 int i;
1682
1683 port_data = of_get_property(np, "audio-ports", &size);
1684 if (!port_data)
1685 return 0;
1686
1687 size /= sizeof(u32);
71689160 1688 if (size > 2 * ARRAY_SIZE(priv->audio_port_enable) || size % 2 != 0) {
7e567624
JS
1689 dev_err(&priv->hdmi->dev,
1690 "Bad number of elements in audio-ports dt-property\n");
1691 return -EINVAL;
1692 }
1693
1694 size /= 2;
1695
1696 for (i = 0; i < size; i++) {
71689160 1697 unsigned int route;
7e567624
JS
1698 u8 afmt = be32_to_cpup(&port_data[2*i]);
1699 u8 ena_ap = be32_to_cpup(&port_data[2*i+1]);
1700
71689160
RK
1701 switch (afmt) {
1702 case AFMT_I2S:
1703 route = AUDIO_ROUTE_I2S;
1704 break;
1705 case AFMT_SPDIF:
1706 route = AUDIO_ROUTE_SPDIF;
1707 break;
1708 default:
7e567624
JS
1709 dev_err(&priv->hdmi->dev,
1710 "Bad audio format %u\n", afmt);
1711 return -EINVAL;
1712 }
1713
125a4f93
RK
1714 if (!ena_ap) {
1715 dev_err(&priv->hdmi->dev, "invalid zero port config\n");
1716 continue;
1717 }
1718
71689160
RK
1719 if (priv->audio_port_enable[route]) {
1720 dev_err(&priv->hdmi->dev,
125a4f93
RK
1721 "%s format already configured\n",
1722 route == AUDIO_ROUTE_SPDIF ? "SPDIF" : "I2S");
71689160
RK
1723 return -EINVAL;
1724 }
7e567624 1725
71689160 1726 priv->audio_port_enable[route] = ena_ap;
7e567624
JS
1727 }
1728 return 0;
1729}
1730
a03a915b
RK
1731static int tda998x_set_config(struct tda998x_priv *priv,
1732 const struct tda998x_encoder_params *p)
e7792ce2 1733{
6c1187aa
RK
1734 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(p->swap_a) |
1735 (p->mirr_a ? VIP_CNTRL_0_MIRR_A : 0) |
1736 VIP_CNTRL_0_SWAP_B(p->swap_b) |
1737 (p->mirr_b ? VIP_CNTRL_0_MIRR_B : 0);
1738 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(p->swap_c) |
1739 (p->mirr_c ? VIP_CNTRL_1_MIRR_C : 0) |
1740 VIP_CNTRL_1_SWAP_D(p->swap_d) |
1741 (p->mirr_d ? VIP_CNTRL_1_MIRR_D : 0);
1742 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(p->swap_e) |
1743 (p->mirr_e ? VIP_CNTRL_2_MIRR_E : 0) |
1744 VIP_CNTRL_2_SWAP_F(p->swap_f) |
1745 (p->mirr_f ? VIP_CNTRL_2_MIRR_F : 0);
1746
935b9ca3 1747 if (p->audio_params.format != AFMT_UNUSED) {
26f7bf12 1748 unsigned int ratio, route;
a03a915b
RK
1749 bool spdif = p->audio_params.format == AFMT_SPDIF;
1750
26f7bf12
RK
1751 route = AUDIO_ROUTE_I2S + spdif;
1752
1753 priv->audio.route = &tda998x_audio_route[route];
125a4f93
RK
1754 priv->audio.cea = p->audio_params.cea;
1755 priv->audio.sample_rate = p->audio_params.sample_rate;
1756 memcpy(priv->audio.status, p->audio_params.status,
1757 min(sizeof(priv->audio.status),
1758 sizeof(p->audio_params.status)));
82642ab7 1759 priv->audio.ena_ap = p->audio_params.config;
935b9ca3 1760 priv->audio.i2s_format = I2S_FORMAT_PHILIPS;
a03a915b
RK
1761
1762 ratio = spdif ? 64 : p->audio_params.sample_width * 2;
1763 return tda998x_derive_cts_n(priv, &priv->audio, ratio);
935b9ca3 1764 }
a03a915b
RK
1765
1766 return 0;
6c1187aa
RK
1767}
1768
76767fda
RK
1769static void tda998x_destroy(struct device *dev)
1770{
1771 struct tda998x_priv *priv = dev_get_drvdata(dev);
1772
1773 drm_bridge_remove(&priv->bridge);
1774
1775 /* disable all IRQs and free the IRQ handler */
1776 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1777 reg_clear(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1778
1779 if (priv->audio_pdev)
1780 platform_device_unregister(priv->audio_pdev);
1781
1782 if (priv->hdmi->irq)
1783 free_irq(priv->hdmi->irq, priv);
1784
1785 del_timer_sync(&priv->edid_delay_timer);
1786 cancel_work_sync(&priv->detect_work);
1787
1788 i2c_unregister_device(priv->cec);
1789
1790 if (priv->cec_notify)
1791 cec_notifier_put(priv->cec_notify);
1792}
1793
2143adb0 1794static int tda998x_create(struct device *dev)
e7792ce2 1795{
2143adb0 1796 struct i2c_client *client = to_i2c_client(dev);
0d44ea19 1797 struct device_node *np = client->dev.of_node;
7e8675f0 1798 struct i2c_board_info cec_info;
2143adb0 1799 struct tda998x_priv *priv;
0d44ea19 1800 u32 video;
fb7544d7 1801 int rev_lo, rev_hi, ret;
e7792ce2 1802
2143adb0
RK
1803 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1804 if (!priv)
1805 return -ENOMEM;
1806
1807 dev_set_drvdata(dev, priv);
1808
d93ae190
RK
1809 mutex_init(&priv->mutex); /* protect the page access */
1810 mutex_init(&priv->audio_mutex); /* protect access from audio thread */
7e8675f0 1811 mutex_init(&priv->edid_mutex);
30bd8b86 1812 INIT_LIST_HEAD(&priv->bridge.list);
d93ae190
RK
1813 init_waitqueue_head(&priv->edid_delay_waitq);
1814 timer_setup(&priv->edid_delay_timer, tda998x_edid_delay_done, 0);
1815 INIT_WORK(&priv->detect_work, tda998x_detect_work);
ba300c17 1816
5e74c22c
RK
1817 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1818 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
1819 priv->vip_cntrl_2 = VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5);
1820
14e5b588
RK
1821 /* CEC I2C address bound to TDA998x I2C addr by configuration pins */
1822 priv->cec_addr = 0x34 + (client->addr & 0x03);
2eb4c7b1 1823 priv->current_page = 0xff;
2f7f730a 1824 priv->hdmi = client;
ed9a8426 1825
e7792ce2 1826 /* wake up the device: */
2f7f730a 1827 cec_write(priv, REG_CEC_ENAMODS,
e7792ce2
RC
1828 CEC_ENAMODS_EN_RXSENS | CEC_ENAMODS_EN_HDMI);
1829
2f7f730a 1830 tda998x_reset(priv);
e7792ce2
RC
1831
1832 /* read version: */
fb7544d7 1833 rev_lo = reg_read(priv, REG_VERSION_LSB);
6a765c3f 1834 if (rev_lo < 0) {
76767fda 1835 dev_err(dev, "failed to read version: %d\n", rev_lo);
6a765c3f
RK
1836 return rev_lo;
1837 }
1838
fb7544d7 1839 rev_hi = reg_read(priv, REG_VERSION_MSB);
6a765c3f 1840 if (rev_hi < 0) {
76767fda 1841 dev_err(dev, "failed to read version: %d\n", rev_hi);
6a765c3f 1842 return rev_hi;
fb7544d7
RK
1843 }
1844
1845 priv->rev = rev_lo | rev_hi << 8;
e7792ce2
RC
1846
1847 /* mask off feature bits: */
1848 priv->rev &= ~0x30; /* not-hdcp and not-scalar bit */
1849
1850 switch (priv->rev) {
b728fab7 1851 case TDA9989N2:
76767fda 1852 dev_info(dev, "found TDA9989 n2");
b728fab7
JFM
1853 break;
1854 case TDA19989:
76767fda 1855 dev_info(dev, "found TDA19989");
b728fab7
JFM
1856 break;
1857 case TDA19989N2:
76767fda 1858 dev_info(dev, "found TDA19989 n2");
b728fab7
JFM
1859 break;
1860 case TDA19988:
76767fda 1861 dev_info(dev, "found TDA19988");
b728fab7 1862 break;
e7792ce2 1863 default:
76767fda 1864 dev_err(dev, "found unsupported device: %04x\n", priv->rev);
6a765c3f 1865 return -ENXIO;
e7792ce2
RC
1866 }
1867
1868 /* after reset, enable DDC: */
2f7f730a 1869 reg_write(priv, REG_DDC_DISABLE, 0x00);
e7792ce2
RC
1870
1871 /* set clock on DDC channel: */
2f7f730a 1872 reg_write(priv, REG_TX3, 39);
e7792ce2
RC
1873
1874 /* if necessary, disable multi-master: */
1875 if (priv->rev == TDA19989)
2f7f730a 1876 reg_set(priv, REG_I2C_MASTER, I2C_MASTER_DIS_MM);
e7792ce2 1877
2f7f730a 1878 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
e7792ce2
RC
1879 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1880
ba8975f1
RK
1881 /* ensure interrupts are disabled */
1882 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1883
1884 /* clear pending interrupts */
1885 cec_read(priv, REG_CEC_RXSHPDINT);
1886 reg_read(priv, REG_INT_FLAGS_0);
1887 reg_read(priv, REG_INT_FLAGS_1);
1888 reg_read(priv, REG_INT_FLAGS_2);
1889
12473b7d
JFM
1890 /* initialize the optional IRQ */
1891 if (client->irq) {
ae81553c 1892 unsigned long irq_flags;
12473b7d 1893
6833d26e 1894 /* init read EDID waitqueue and HDP work */
12473b7d
JFM
1895 init_waitqueue_head(&priv->wq_edid);
1896
ae81553c 1897 irq_flags =
12473b7d 1898 irqd_get_trigger_type(irq_get_irq_data(client->irq));
7e8675f0
RK
1899
1900 priv->cec_glue.irq_flags = irq_flags;
1901
ae81553c 1902 irq_flags |= IRQF_SHARED | IRQF_ONESHOT;
12473b7d 1903 ret = request_threaded_irq(client->irq, NULL,
ae81553c 1904 tda998x_irq_thread, irq_flags,
12473b7d
JFM
1905 "tda998x", priv);
1906 if (ret) {
76767fda 1907 dev_err(dev, "failed to request IRQ#%u: %d\n",
12473b7d 1908 client->irq, ret);
6a765c3f 1909 goto err_irq;
12473b7d
JFM
1910 }
1911
1912 /* enable HPD irq */
1913 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1914 }
1915
76767fda 1916 priv->cec_notify = cec_notifier_get(dev);
7e8675f0
RK
1917 if (!priv->cec_notify) {
1918 ret = -ENOMEM;
1919 goto fail;
1920 }
1921
76767fda 1922 priv->cec_glue.parent = dev;
7e8675f0
RK
1923 priv->cec_glue.data = priv;
1924 priv->cec_glue.init = tda998x_cec_hook_init;
1925 priv->cec_glue.exit = tda998x_cec_hook_exit;
1926 priv->cec_glue.open = tda998x_cec_hook_open;
1927 priv->cec_glue.release = tda998x_cec_hook_release;
1928
1929 /*
1930 * Some TDA998x are actually two I2C devices merged onto one piece
1931 * of silicon: TDA9989 and TDA19989 combine the HDMI transmitter
1932 * with a slightly modified TDA9950 CEC device. The CEC device
1933 * is at the TDA9950 address, with the address pins strapped across
1934 * to the TDA998x address pins. Hence, it always has the same
1935 * offset.
1936 */
1937 memset(&cec_info, 0, sizeof(cec_info));
1938 strlcpy(cec_info.type, "tda9950", sizeof(cec_info.type));
1939 cec_info.addr = priv->cec_addr;
1940 cec_info.platform_data = &priv->cec_glue;
1941 cec_info.irq = client->irq;
1942
1943 priv->cec = i2c_new_device(client->adapter, &cec_info);
101e996b
RK
1944 if (!priv->cec) {
1945 ret = -ENODEV;
1946 goto fail;
1947 }
1948
e4782627
JFM
1949 /* enable EDID read irq: */
1950 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1951
6c1187aa
RK
1952 if (np) {
1953 /* get the device tree parameters */
1954 ret = of_property_read_u32(np, "video-ports", &video);
1955 if (ret == 0) {
1956 priv->vip_cntrl_0 = video >> 16;
1957 priv->vip_cntrl_1 = video >> 8;
1958 priv->vip_cntrl_2 = video;
1959 }
0d44ea19 1960
6c1187aa
RK
1961 ret = tda998x_get_audio_ports(priv, np);
1962 if (ret)
1963 goto fail;
0d44ea19 1964
71689160
RK
1965 if (priv->audio_port_enable[AUDIO_ROUTE_I2S] ||
1966 priv->audio_port_enable[AUDIO_ROUTE_SPDIF])
6c1187aa 1967 tda998x_audio_codec_init(priv, &client->dev);
76767fda 1968 } else if (dev->platform_data) {
a03a915b
RK
1969 ret = tda998x_set_config(priv, dev->platform_data);
1970 if (ret)
1971 goto fail;
0d44ea19
JFM
1972 }
1973
30bd8b86
RK
1974 priv->bridge.funcs = &tda998x_bridge_funcs;
1975#ifdef CONFIG_OF
1976 priv->bridge.of_node = dev->of_node;
1977#endif
7e567624 1978
30bd8b86 1979 drm_bridge_add(&priv->bridge);
7e567624
JS
1980
1981 return 0;
6a765c3f 1982
e7792ce2 1983fail:
2143adb0 1984 tda998x_destroy(dev);
6a765c3f 1985err_irq:
6a765c3f 1986 return ret;
e7792ce2
RC
1987}
1988
30bd8b86 1989/* DRM encoder functions */
c707c361
RK
1990
1991static void tda998x_encoder_destroy(struct drm_encoder *encoder)
1992{
c707c361
RK
1993 drm_encoder_cleanup(encoder);
1994}
1995
1996static const struct drm_encoder_funcs tda998x_encoder_funcs = {
1997 .destroy = tda998x_encoder_destroy,
1998};
1999
30bd8b86 2000static int tda998x_encoder_init(struct device *dev, struct drm_device *drm)
c707c361 2001{
30bd8b86 2002 struct tda998x_priv *priv = dev_get_drvdata(dev);
e66e03ab 2003 u32 crtcs = 0;
c707c361
RK
2004 int ret;
2005
5dbcf319
RK
2006 if (dev->of_node)
2007 crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
2008
2009 /* If no CRTCs were found, fall back to our old behaviour */
2010 if (crtcs == 0) {
2011 dev_warn(dev, "Falling back to first CRTC\n");
2012 crtcs = 1 << 0;
2013 }
2014
a3584f60 2015 priv->encoder.possible_crtcs = crtcs;
c707c361 2016
a3584f60 2017 ret = drm_encoder_init(drm, &priv->encoder, &tda998x_encoder_funcs,
13a3d91f 2018 DRM_MODE_ENCODER_TMDS, NULL);
c707c361
RK
2019 if (ret)
2020 goto err_encoder;
2021
30bd8b86 2022 ret = drm_bridge_attach(&priv->encoder, &priv->bridge, NULL);
c707c361 2023 if (ret)
30bd8b86 2024 goto err_bridge;
c707c361 2025
c707c361
RK
2026 return 0;
2027
30bd8b86 2028err_bridge:
a3584f60 2029 drm_encoder_cleanup(&priv->encoder);
c707c361 2030err_encoder:
c707c361
RK
2031 return ret;
2032}
2033
30bd8b86
RK
2034static int tda998x_bind(struct device *dev, struct device *master, void *data)
2035{
30bd8b86 2036 struct drm_device *drm = data;
30bd8b86 2037
5a03f534 2038 return tda998x_encoder_init(dev, drm);
30bd8b86
RK
2039}
2040
c707c361
RK
2041static void tda998x_unbind(struct device *dev, struct device *master,
2042 void *data)
2043{
a3584f60 2044 struct tda998x_priv *priv = dev_get_drvdata(dev);
c707c361 2045
a3584f60 2046 drm_encoder_cleanup(&priv->encoder);
c707c361
RK
2047}
2048
2049static const struct component_ops tda998x_ops = {
2050 .bind = tda998x_bind,
2051 .unbind = tda998x_unbind,
2052};
2053
2054static int
2055tda998x_probe(struct i2c_client *client, const struct i2c_device_id *id)
2056{
5a03f534
RK
2057 int ret;
2058
14e5b588
RK
2059 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
2060 dev_warn(&client->dev, "adapter does not support I2C\n");
2061 return -EIO;
2062 }
5a03f534
RK
2063
2064 ret = tda998x_create(&client->dev);
2065 if (ret)
2066 return ret;
2067
2068 ret = component_add(&client->dev, &tda998x_ops);
2069 if (ret)
2070 tda998x_destroy(&client->dev);
2071 return ret;
c707c361
RK
2072}
2073
2074static int tda998x_remove(struct i2c_client *client)
2075{
2076 component_del(&client->dev, &tda998x_ops);
5a03f534 2077 tda998x_destroy(&client->dev);
c707c361
RK
2078 return 0;
2079}
2080
0d44ea19
JFM
2081#ifdef CONFIG_OF
2082static const struct of_device_id tda998x_dt_ids[] = {
2083 { .compatible = "nxp,tda998x", },
2084 { }
2085};
2086MODULE_DEVICE_TABLE(of, tda998x_dt_ids);
2087#endif
2088
b7f08c89 2089static const struct i2c_device_id tda998x_ids[] = {
e7792ce2
RC
2090 { "tda998x", 0 },
2091 { }
2092};
2093MODULE_DEVICE_TABLE(i2c, tda998x_ids);
2094
3d58e318
RK
2095static struct i2c_driver tda998x_driver = {
2096 .probe = tda998x_probe,
2097 .remove = tda998x_remove,
2098 .driver = {
2099 .name = "tda998x",
2100 .of_match_table = of_match_ptr(tda998x_dt_ids),
e7792ce2 2101 },
3d58e318 2102 .id_table = tda998x_ids,
e7792ce2
RC
2103};
2104
3d58e318 2105module_i2c_driver(tda998x_driver);
e7792ce2
RC
2106
2107MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
2108MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
2109MODULE_LICENSE("GPL");