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