2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
20 #include <linux/module.h>
23 #include <drm/drm_crtc_helper.h>
24 #include <drm/drm_encoder_slave.h>
25 #include <drm/drm_edid.h>
28 #define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
31 struct i2c_client
*cec
;
37 #define to_tda998x_priv(x) ((struct tda998x_priv *)to_encoder_slave(x)->slave_priv)
39 /* The TDA9988 series of devices use a paged register scheme.. to simplify
40 * things we encode the page # in upper bits of the register #. To read/
41 * write a given register, we need to make sure CURPAGE register is set
42 * appropriately. Which implies reads/writes are not atomic. Fun!
45 #define REG(page, addr) (((page) << 8) | (addr))
46 #define REG2ADDR(reg) ((reg) & 0xff)
47 #define REG2PAGE(reg) (((reg) >> 8) & 0xff)
49 #define REG_CURPAGE 0xff /* write */
52 /* Page 00h: General Control */
53 #define REG_VERSION_LSB REG(0x00, 0x00) /* read */
54 #define REG_MAIN_CNTRL0 REG(0x00, 0x01) /* read/write */
55 # define MAIN_CNTRL0_SR (1 << 0)
56 # define MAIN_CNTRL0_DECS (1 << 1)
57 # define MAIN_CNTRL0_DEHS (1 << 2)
58 # define MAIN_CNTRL0_CECS (1 << 3)
59 # define MAIN_CNTRL0_CEHS (1 << 4)
60 # define MAIN_CNTRL0_SCALER (1 << 7)
61 #define REG_VERSION_MSB REG(0x00, 0x02) /* read */
62 #define REG_SOFTRESET REG(0x00, 0x0a) /* write */
63 # define SOFTRESET_AUDIO (1 << 0)
64 # define SOFTRESET_I2C_MASTER (1 << 1)
65 #define REG_DDC_DISABLE REG(0x00, 0x0b) /* read/write */
66 #define REG_CCLK_ON REG(0x00, 0x0c) /* read/write */
67 #define REG_I2C_MASTER REG(0x00, 0x0d) /* read/write */
68 # define I2C_MASTER_DIS_MM (1 << 0)
69 # define I2C_MASTER_DIS_FILT (1 << 1)
70 # define I2C_MASTER_APP_STRT_LAT (1 << 2)
71 #define REG_INT_FLAGS_0 REG(0x00, 0x0f) /* read/write */
72 #define REG_INT_FLAGS_1 REG(0x00, 0x10) /* read/write */
73 #define REG_INT_FLAGS_2 REG(0x00, 0x11) /* read/write */
74 # define INT_FLAGS_2_EDID_BLK_RD (1 << 1)
75 #define REG_ENA_VP_0 REG(0x00, 0x18) /* read/write */
76 #define REG_ENA_VP_1 REG(0x00, 0x19) /* read/write */
77 #define REG_ENA_VP_2 REG(0x00, 0x1a) /* read/write */
78 #define REG_ENA_AP REG(0x00, 0x1e) /* read/write */
79 #define REG_VIP_CNTRL_0 REG(0x00, 0x20) /* write */
80 # define VIP_CNTRL_0_MIRR_A (1 << 7)
81 # define VIP_CNTRL_0_SWAP_A(x) (((x) & 7) << 4)
82 # define VIP_CNTRL_0_MIRR_B (1 << 3)
83 # define VIP_CNTRL_0_SWAP_B(x) (((x) & 7) << 0)
84 #define REG_VIP_CNTRL_1 REG(0x00, 0x21) /* write */
85 # define VIP_CNTRL_1_MIRR_C (1 << 7)
86 # define VIP_CNTRL_1_SWAP_C(x) (((x) & 7) << 4)
87 # define VIP_CNTRL_1_MIRR_D (1 << 3)
88 # define VIP_CNTRL_1_SWAP_D(x) (((x) & 7) << 0)
89 #define REG_VIP_CNTRL_2 REG(0x00, 0x22) /* write */
90 # define VIP_CNTRL_2_MIRR_E (1 << 7)
91 # define VIP_CNTRL_2_SWAP_E(x) (((x) & 7) << 4)
92 # define VIP_CNTRL_2_MIRR_F (1 << 3)
93 # define VIP_CNTRL_2_SWAP_F(x) (((x) & 7) << 0)
94 #define REG_VIP_CNTRL_3 REG(0x00, 0x23) /* write */
95 # define VIP_CNTRL_3_X_TGL (1 << 0)
96 # define VIP_CNTRL_3_H_TGL (1 << 1)
97 # define VIP_CNTRL_3_V_TGL (1 << 2)
98 # define VIP_CNTRL_3_EMB (1 << 3)
99 # define VIP_CNTRL_3_SYNC_DE (1 << 4)
100 # define VIP_CNTRL_3_SYNC_HS (1 << 5)
101 # define VIP_CNTRL_3_DE_INT (1 << 6)
102 # define VIP_CNTRL_3_EDGE (1 << 7)
103 #define REG_VIP_CNTRL_4 REG(0x00, 0x24) /* write */
104 # define VIP_CNTRL_4_BLC(x) (((x) & 3) << 0)
105 # define VIP_CNTRL_4_BLANKIT(x) (((x) & 3) << 2)
106 # define VIP_CNTRL_4_CCIR656 (1 << 4)
107 # define VIP_CNTRL_4_656_ALT (1 << 5)
108 # define VIP_CNTRL_4_TST_656 (1 << 6)
109 # define VIP_CNTRL_4_TST_PAT (1 << 7)
110 #define REG_VIP_CNTRL_5 REG(0x00, 0x25) /* write */
111 # define VIP_CNTRL_5_CKCASE (1 << 0)
112 # define VIP_CNTRL_5_SP_CNT(x) (((x) & 3) << 1)
113 #define REG_MAT_CONTRL REG(0x00, 0x80) /* write */
114 # define MAT_CONTRL_MAT_SC(x) (((x) & 3) << 0)
115 # define MAT_CONTRL_MAT_BP (1 << 2)
116 #define REG_VIDFORMAT REG(0x00, 0xa0) /* write */
117 #define REG_REFPIX_MSB REG(0x00, 0xa1) /* write */
118 #define REG_REFPIX_LSB REG(0x00, 0xa2) /* write */
119 #define REG_REFLINE_MSB REG(0x00, 0xa3) /* write */
120 #define REG_REFLINE_LSB REG(0x00, 0xa4) /* write */
121 #define REG_NPIX_MSB REG(0x00, 0xa5) /* write */
122 #define REG_NPIX_LSB REG(0x00, 0xa6) /* write */
123 #define REG_NLINE_MSB REG(0x00, 0xa7) /* write */
124 #define REG_NLINE_LSB REG(0x00, 0xa8) /* write */
125 #define REG_VS_LINE_STRT_1_MSB REG(0x00, 0xa9) /* write */
126 #define REG_VS_LINE_STRT_1_LSB REG(0x00, 0xaa) /* write */
127 #define REG_VS_PIX_STRT_1_MSB REG(0x00, 0xab) /* write */
128 #define REG_VS_PIX_STRT_1_LSB REG(0x00, 0xac) /* write */
129 #define REG_VS_LINE_END_1_MSB REG(0x00, 0xad) /* write */
130 #define REG_VS_LINE_END_1_LSB REG(0x00, 0xae) /* write */
131 #define REG_VS_PIX_END_1_MSB REG(0x00, 0xaf) /* write */
132 #define REG_VS_PIX_END_1_LSB REG(0x00, 0xb0) /* write */
133 #define REG_VS_PIX_STRT_2_MSB REG(0x00, 0xb3) /* write */
134 #define REG_VS_PIX_STRT_2_LSB REG(0x00, 0xb4) /* write */
135 #define REG_VS_PIX_END_2_MSB REG(0x00, 0xb7) /* write */
136 #define REG_VS_PIX_END_2_LSB REG(0x00, 0xb8) /* write */
137 #define REG_HS_PIX_START_MSB REG(0x00, 0xb9) /* write */
138 #define REG_HS_PIX_START_LSB REG(0x00, 0xba) /* write */
139 #define REG_HS_PIX_STOP_MSB REG(0x00, 0xbb) /* write */
140 #define REG_HS_PIX_STOP_LSB REG(0x00, 0xbc) /* write */
141 #define REG_VWIN_START_1_MSB REG(0x00, 0xbd) /* write */
142 #define REG_VWIN_START_1_LSB REG(0x00, 0xbe) /* write */
143 #define REG_VWIN_END_1_MSB REG(0x00, 0xbf) /* write */
144 #define REG_VWIN_END_1_LSB REG(0x00, 0xc0) /* write */
145 #define REG_DE_START_MSB REG(0x00, 0xc5) /* write */
146 #define REG_DE_START_LSB REG(0x00, 0xc6) /* write */
147 #define REG_DE_STOP_MSB REG(0x00, 0xc7) /* write */
148 #define REG_DE_STOP_LSB REG(0x00, 0xc8) /* write */
149 #define REG_TBG_CNTRL_0 REG(0x00, 0xca) /* write */
150 # define TBG_CNTRL_0_FRAME_DIS (1 << 5)
151 # define TBG_CNTRL_0_SYNC_MTHD (1 << 6)
152 # define TBG_CNTRL_0_SYNC_ONCE (1 << 7)
153 #define REG_TBG_CNTRL_1 REG(0x00, 0xcb) /* write */
154 # define TBG_CNTRL_1_VH_TGL_0 (1 << 0)
155 # define TBG_CNTRL_1_VH_TGL_1 (1 << 1)
156 # define TBG_CNTRL_1_VH_TGL_2 (1 << 2)
157 # define TBG_CNTRL_1_VHX_EXT_DE (1 << 3)
158 # define TBG_CNTRL_1_VHX_EXT_HS (1 << 4)
159 # define TBG_CNTRL_1_VHX_EXT_VS (1 << 5)
160 # define TBG_CNTRL_1_DWIN_DIS (1 << 6)
161 #define REG_ENABLE_SPACE REG(0x00, 0xd6) /* write */
162 #define REG_HVF_CNTRL_0 REG(0x00, 0xe4) /* write */
163 # define HVF_CNTRL_0_SM (1 << 7)
164 # define HVF_CNTRL_0_RWB (1 << 6)
165 # define HVF_CNTRL_0_PREFIL(x) (((x) & 3) << 2)
166 # define HVF_CNTRL_0_INTPOL(x) (((x) & 3) << 0)
167 #define REG_HVF_CNTRL_1 REG(0x00, 0xe5) /* write */
168 # define HVF_CNTRL_1_FOR (1 << 0)
169 # define HVF_CNTRL_1_YUVBLK (1 << 1)
170 # define HVF_CNTRL_1_VQR(x) (((x) & 3) << 2)
171 # define HVF_CNTRL_1_PAD(x) (((x) & 3) << 4)
172 # define HVF_CNTRL_1_SEMI_PLANAR (1 << 6)
173 #define REG_RPT_CNTRL REG(0x00, 0xf0) /* write */
176 /* Page 02h: PLL settings */
177 #define REG_PLL_SERIAL_1 REG(0x02, 0x00) /* read/write */
178 # define PLL_SERIAL_1_SRL_FDN (1 << 0)
179 # define PLL_SERIAL_1_SRL_IZ(x) (((x) & 3) << 1)
180 # define PLL_SERIAL_1_SRL_MAN_IZ (1 << 6)
181 #define REG_PLL_SERIAL_2 REG(0x02, 0x01) /* read/write */
182 # define PLL_SERIAL_2_SRL_NOSC(x) (((x) & 3) << 0)
183 # define PLL_SERIAL_2_SRL_PR(x) (((x) & 0xf) << 4)
184 #define REG_PLL_SERIAL_3 REG(0x02, 0x02) /* read/write */
185 # define PLL_SERIAL_3_SRL_CCIR (1 << 0)
186 # define PLL_SERIAL_3_SRL_DE (1 << 2)
187 # define PLL_SERIAL_3_SRL_PXIN_SEL (1 << 4)
188 #define REG_SERIALIZER REG(0x02, 0x03) /* read/write */
189 #define REG_BUFFER_OUT REG(0x02, 0x04) /* read/write */
190 #define REG_PLL_SCG1 REG(0x02, 0x05) /* read/write */
191 #define REG_PLL_SCG2 REG(0x02, 0x06) /* read/write */
192 #define REG_PLL_SCGN1 REG(0x02, 0x07) /* read/write */
193 #define REG_PLL_SCGN2 REG(0x02, 0x08) /* read/write */
194 #define REG_PLL_SCGR1 REG(0x02, 0x09) /* read/write */
195 #define REG_PLL_SCGR2 REG(0x02, 0x0a) /* read/write */
196 #define REG_AUDIO_DIV REG(0x02, 0x0e) /* read/write */
197 #define REG_SEL_CLK REG(0x02, 0x11) /* read/write */
198 # define SEL_CLK_SEL_CLK1 (1 << 0)
199 # define SEL_CLK_SEL_VRF_CLK(x) (((x) & 3) << 1)
200 # define SEL_CLK_ENA_SC_CLK (1 << 3)
201 #define REG_ANA_GENERAL REG(0x02, 0x12) /* read/write */
204 /* Page 09h: EDID Control */
205 #define REG_EDID_DATA_0 REG(0x09, 0x00) /* read */
206 /* next 127 successive registers are the EDID block */
207 #define REG_EDID_CTRL REG(0x09, 0xfa) /* read/write */
208 #define REG_DDC_ADDR REG(0x09, 0xfb) /* read/write */
209 #define REG_DDC_OFFS REG(0x09, 0xfc) /* read/write */
210 #define REG_DDC_SEGM_ADDR REG(0x09, 0xfd) /* read/write */
211 #define REG_DDC_SEGM REG(0x09, 0xfe) /* read/write */
214 /* Page 10h: information frames and packets */
217 /* Page 11h: audio settings and content info packets */
218 #define REG_AIP_CNTRL_0 REG(0x11, 0x00) /* read/write */
219 # define AIP_CNTRL_0_RST_FIFO (1 << 0)
220 # define AIP_CNTRL_0_SWAP (1 << 1)
221 # define AIP_CNTRL_0_LAYOUT (1 << 2)
222 # define AIP_CNTRL_0_ACR_MAN (1 << 5)
223 # define AIP_CNTRL_0_RST_CTS (1 << 6)
224 #define REG_ENC_CNTRL REG(0x11, 0x0d) /* read/write */
225 # define ENC_CNTRL_RST_ENC (1 << 0)
226 # define ENC_CNTRL_RST_SEL (1 << 1)
227 # define ENC_CNTRL_CTL_CODE(x) (((x) & 3) << 2)
230 /* Page 12h: HDCP and OTP */
231 #define REG_TX3 REG(0x12, 0x9a) /* read/write */
232 #define REG_TX33 REG(0x12, 0xb8) /* read/write */
233 # define TX33_HDMI (1 << 1)
236 /* Page 13h: Gamut related metadata packets */
240 /* CEC registers: (not paged)
242 #define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
243 # define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
244 # define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
245 # define CEC_FRO_IM_CLK_CTRL_IMCLK_SEL (1 << 1)
246 # define CEC_FRO_IM_CLK_CTRL_FRO_DIV (1 << 0)
247 #define REG_CEC_RXSHPDLEV 0xfe /* read */
248 # define CEC_RXSHPDLEV_RXSENS (1 << 0)
249 # define CEC_RXSHPDLEV_HPD (1 << 1)
251 #define REG_CEC_ENAMODS 0xff /* read/write */
252 # define CEC_ENAMODS_DIS_FRO (1 << 6)
253 # define CEC_ENAMODS_DIS_CCLK (1 << 5)
254 # define CEC_ENAMODS_EN_RXSENS (1 << 2)
255 # define CEC_ENAMODS_EN_HDMI (1 << 1)
256 # define CEC_ENAMODS_EN_CEC (1 << 0)
259 /* Device versions: */
260 #define TDA9989N2 0x0101
261 #define TDA19989 0x0201
262 #define TDA19989N2 0x0202
263 #define TDA19988 0x0301
266 cec_write(struct drm_encoder
*encoder
, uint16_t addr
, uint8_t val
)
268 struct i2c_client
*client
= to_tda998x_priv(encoder
)->cec
;
269 uint8_t buf
[] = {addr
, val
};
272 ret
= i2c_master_send(client
, buf
, ARRAY_SIZE(buf
));
274 dev_err(&client
->dev
, "Error %d writing to cec:0x%x\n", ret
, addr
);
278 cec_read(struct drm_encoder
*encoder
, uint8_t addr
)
280 struct i2c_client
*client
= to_tda998x_priv(encoder
)->cec
;
284 ret
= i2c_master_send(client
, &addr
, sizeof(addr
));
288 ret
= i2c_master_recv(client
, &val
, sizeof(val
));
295 dev_err(&client
->dev
, "Error %d reading from cec:0x%x\n", ret
, addr
);
300 set_page(struct drm_encoder
*encoder
, uint16_t reg
)
302 struct tda998x_priv
*priv
= to_tda998x_priv(encoder
);
304 if (REG2PAGE(reg
) != priv
->current_page
) {
305 struct i2c_client
*client
= drm_i2c_encoder_get_client(encoder
);
307 REG_CURPAGE
, REG2PAGE(reg
)
309 int ret
= i2c_master_send(client
, buf
, sizeof(buf
));
311 dev_err(&client
->dev
, "Error %d writing to REG_CURPAGE\n", ret
);
313 priv
->current_page
= REG2PAGE(reg
);
318 reg_read_range(struct drm_encoder
*encoder
, uint16_t reg
, char *buf
, int cnt
)
320 struct i2c_client
*client
= drm_i2c_encoder_get_client(encoder
);
321 uint8_t addr
= REG2ADDR(reg
);
324 set_page(encoder
, reg
);
326 ret
= i2c_master_send(client
, &addr
, sizeof(addr
));
330 ret
= i2c_master_recv(client
, buf
, cnt
);
337 dev_err(&client
->dev
, "Error %d reading from 0x%x\n", ret
, reg
);
342 reg_read(struct drm_encoder
*encoder
, uint16_t reg
)
345 reg_read_range(encoder
, reg
, &val
, sizeof(val
));
350 reg_write(struct drm_encoder
*encoder
, uint16_t reg
, uint8_t val
)
352 struct i2c_client
*client
= drm_i2c_encoder_get_client(encoder
);
353 uint8_t buf
[] = {REG2ADDR(reg
), val
};
356 set_page(encoder
, reg
);
358 ret
= i2c_master_send(client
, buf
, ARRAY_SIZE(buf
));
360 dev_err(&client
->dev
, "Error %d writing to 0x%x\n", ret
, reg
);
364 reg_write16(struct drm_encoder
*encoder
, uint16_t reg
, uint16_t val
)
366 struct i2c_client
*client
= drm_i2c_encoder_get_client(encoder
);
367 uint8_t buf
[] = {REG2ADDR(reg
), val
>> 8, val
};
370 set_page(encoder
, reg
);
372 ret
= i2c_master_send(client
, buf
, ARRAY_SIZE(buf
));
374 dev_err(&client
->dev
, "Error %d writing to 0x%x\n", ret
, reg
);
378 reg_set(struct drm_encoder
*encoder
, uint16_t reg
, uint8_t val
)
380 reg_write(encoder
, reg
, reg_read(encoder
, reg
) | val
);
384 reg_clear(struct drm_encoder
*encoder
, uint16_t reg
, uint8_t val
)
386 reg_write(encoder
, reg
, reg_read(encoder
, reg
) & ~val
);
390 tda998x_reset(struct drm_encoder
*encoder
)
392 /* reset audio and i2c master: */
393 reg_set(encoder
, REG_SOFTRESET
, SOFTRESET_AUDIO
| SOFTRESET_I2C_MASTER
);
395 reg_clear(encoder
, REG_SOFTRESET
, SOFTRESET_AUDIO
| SOFTRESET_I2C_MASTER
);
398 /* reset transmitter: */
399 reg_set(encoder
, REG_MAIN_CNTRL0
, MAIN_CNTRL0_SR
);
400 reg_clear(encoder
, REG_MAIN_CNTRL0
, MAIN_CNTRL0_SR
);
402 /* PLL registers common configuration */
403 reg_write(encoder
, REG_PLL_SERIAL_1
, 0x00);
404 reg_write(encoder
, REG_PLL_SERIAL_2
, PLL_SERIAL_2_SRL_NOSC(1));
405 reg_write(encoder
, REG_PLL_SERIAL_3
, 0x00);
406 reg_write(encoder
, REG_SERIALIZER
, 0x00);
407 reg_write(encoder
, REG_BUFFER_OUT
, 0x00);
408 reg_write(encoder
, REG_PLL_SCG1
, 0x00);
409 reg_write(encoder
, REG_AUDIO_DIV
, 0x03);
410 reg_write(encoder
, REG_SEL_CLK
, SEL_CLK_SEL_CLK1
| SEL_CLK_ENA_SC_CLK
);
411 reg_write(encoder
, REG_PLL_SCGN1
, 0xfa);
412 reg_write(encoder
, REG_PLL_SCGN2
, 0x00);
413 reg_write(encoder
, REG_PLL_SCGR1
, 0x5b);
414 reg_write(encoder
, REG_PLL_SCGR2
, 0x00);
415 reg_write(encoder
, REG_PLL_SCG2
, 0x10);
418 /* DRM encoder functions */
421 tda998x_encoder_set_config(struct drm_encoder
*encoder
, void *params
)
426 tda998x_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
428 struct tda998x_priv
*priv
= to_tda998x_priv(encoder
);
430 /* we only care about on or off: */
431 if (mode
!= DRM_MODE_DPMS_ON
)
432 mode
= DRM_MODE_DPMS_OFF
;
434 if (mode
== priv
->dpms
)
438 case DRM_MODE_DPMS_ON
:
439 /* enable audio and video ports */
440 reg_write(encoder
, REG_ENA_AP
, 0xff);
441 reg_write(encoder
, REG_ENA_VP_0
, 0xff);
442 reg_write(encoder
, REG_ENA_VP_1
, 0xff);
443 reg_write(encoder
, REG_ENA_VP_2
, 0xff);
444 /* set muxing after enabling ports: */
445 reg_write(encoder
, REG_VIP_CNTRL_0
,
446 VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3));
447 reg_write(encoder
, REG_VIP_CNTRL_1
,
448 VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1));
449 reg_write(encoder
, REG_VIP_CNTRL_2
,
450 VIP_CNTRL_2_SWAP_E(4) | VIP_CNTRL_2_SWAP_F(5));
452 case DRM_MODE_DPMS_OFF
:
453 /* disable audio and video ports */
454 reg_write(encoder
, REG_ENA_AP
, 0x00);
455 reg_write(encoder
, REG_ENA_VP_0
, 0x00);
456 reg_write(encoder
, REG_ENA_VP_1
, 0x00);
457 reg_write(encoder
, REG_ENA_VP_2
, 0x00);
465 tda998x_encoder_save(struct drm_encoder
*encoder
)
471 tda998x_encoder_restore(struct drm_encoder
*encoder
)
477 tda998x_encoder_mode_fixup(struct drm_encoder
*encoder
,
478 const struct drm_display_mode
*mode
,
479 struct drm_display_mode
*adjusted_mode
)
485 tda998x_encoder_mode_valid(struct drm_encoder
*encoder
,
486 struct drm_display_mode
*mode
)
492 tda998x_encoder_mode_set(struct drm_encoder
*encoder
,
493 struct drm_display_mode
*mode
,
494 struct drm_display_mode
*adjusted_mode
)
496 struct tda998x_priv
*priv
= to_tda998x_priv(encoder
);
497 uint16_t hs_start
, hs_end
, line_start
, line_end
;
498 uint16_t vwin_start
, vwin_end
, de_start
, de_end
;
499 uint16_t ref_pix
, ref_line
, pix_start2
;
500 uint8_t reg
, div
, rep
;
502 hs_start
= mode
->hsync_start
- mode
->hdisplay
;
503 hs_end
= mode
->hsync_end
- mode
->hdisplay
;
505 line_end
= 1 + mode
->vsync_end
- mode
->vsync_start
;
506 vwin_start
= mode
->vtotal
- mode
->vsync_start
;
507 vwin_end
= vwin_start
+ mode
->vdisplay
;
508 de_start
= mode
->htotal
- mode
->hdisplay
;
509 de_end
= mode
->htotal
;
512 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
513 pix_start2
= (mode
->htotal
/ 2) + hs_start
;
515 /* TODO how is this value calculated? It is 2 for all common
516 * formats in the tables in out of tree nxp driver (assuming
517 * I've properly deciphered their byzantine table system)
521 /* this might changes for other color formats from the CRTC: */
522 ref_pix
= 3 + hs_start
;
524 div
= 148500 / mode
->clock
;
526 DBG("clock=%d, div=%u", mode
->clock
, div
);
527 DBG("hs_start=%u, hs_end=%u, line_start=%u, line_end=%u",
528 hs_start
, hs_end
, line_start
, line_end
);
529 DBG("vwin_start=%u, vwin_end=%u, de_start=%u, de_end=%u",
530 vwin_start
, vwin_end
, de_start
, de_end
);
531 DBG("ref_line=%u, ref_pix=%u, pix_start2=%u",
532 ref_line
, ref_pix
, pix_start2
);
534 /* mute the audio FIFO: */
535 reg_set(encoder
, REG_AIP_CNTRL_0
, AIP_CNTRL_0_RST_FIFO
);
537 /* set HDMI HDCP mode off: */
538 reg_set(encoder
, REG_TBG_CNTRL_1
, TBG_CNTRL_1_DWIN_DIS
);
539 reg_clear(encoder
, REG_TX33
, TX33_HDMI
);
541 reg_write(encoder
, REG_ENC_CNTRL
, ENC_CNTRL_CTL_CODE(0));
542 /* no pre-filter or interpolator: */
543 reg_write(encoder
, REG_HVF_CNTRL_0
, HVF_CNTRL_0_PREFIL(0) |
544 HVF_CNTRL_0_INTPOL(0));
545 reg_write(encoder
, REG_VIP_CNTRL_5
, VIP_CNTRL_5_SP_CNT(0));
546 reg_write(encoder
, REG_VIP_CNTRL_4
, VIP_CNTRL_4_BLANKIT(0) |
548 reg_clear(encoder
, REG_PLL_SERIAL_3
, PLL_SERIAL_3_SRL_CCIR
);
550 reg_clear(encoder
, REG_PLL_SERIAL_1
, PLL_SERIAL_1_SRL_MAN_IZ
);
551 reg_clear(encoder
, REG_PLL_SERIAL_3
, PLL_SERIAL_3_SRL_DE
);
552 reg_write(encoder
, REG_SERIALIZER
, 0);
553 reg_write(encoder
, REG_HVF_CNTRL_1
, HVF_CNTRL_1_VQR(0));
555 /* TODO enable pixel repeat for pixel rates less than 25Msamp/s */
557 reg_write(encoder
, REG_RPT_CNTRL
, 0);
558 reg_write(encoder
, REG_SEL_CLK
, SEL_CLK_SEL_VRF_CLK(0) |
559 SEL_CLK_SEL_CLK1
| SEL_CLK_ENA_SC_CLK
);
561 reg_write(encoder
, REG_PLL_SERIAL_2
, PLL_SERIAL_2_SRL_NOSC(div
) |
562 PLL_SERIAL_2_SRL_PR(rep
));
564 reg_write16(encoder
, REG_VS_PIX_STRT_2_MSB
, pix_start2
);
565 reg_write16(encoder
, REG_VS_PIX_END_2_MSB
, pix_start2
);
567 /* set color matrix bypass flag: */
568 reg_set(encoder
, REG_MAT_CONTRL
, MAT_CONTRL_MAT_BP
);
570 /* set BIAS tmds value: */
571 reg_write(encoder
, REG_ANA_GENERAL
, 0x09);
573 reg_clear(encoder
, REG_TBG_CNTRL_0
, TBG_CNTRL_0_SYNC_MTHD
);
575 reg_write(encoder
, REG_VIP_CNTRL_3
, 0);
576 reg_set(encoder
, REG_VIP_CNTRL_3
, VIP_CNTRL_3_SYNC_HS
);
577 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
578 reg_set(encoder
, REG_VIP_CNTRL_3
, VIP_CNTRL_3_V_TGL
);
580 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
581 reg_set(encoder
, REG_VIP_CNTRL_3
, VIP_CNTRL_3_H_TGL
);
583 reg_write(encoder
, REG_VIDFORMAT
, 0x00);
584 reg_write16(encoder
, REG_NPIX_MSB
, mode
->hdisplay
- 1);
585 reg_write16(encoder
, REG_NLINE_MSB
, mode
->vdisplay
- 1);
586 reg_write16(encoder
, REG_VS_LINE_STRT_1_MSB
, line_start
);
587 reg_write16(encoder
, REG_VS_LINE_END_1_MSB
, line_end
);
588 reg_write16(encoder
, REG_VS_PIX_STRT_1_MSB
, hs_start
);
589 reg_write16(encoder
, REG_VS_PIX_END_1_MSB
, hs_start
);
590 reg_write16(encoder
, REG_HS_PIX_START_MSB
, hs_start
);
591 reg_write16(encoder
, REG_HS_PIX_STOP_MSB
, hs_end
);
592 reg_write16(encoder
, REG_VWIN_START_1_MSB
, vwin_start
);
593 reg_write16(encoder
, REG_VWIN_END_1_MSB
, vwin_end
);
594 reg_write16(encoder
, REG_DE_START_MSB
, de_start
);
595 reg_write16(encoder
, REG_DE_STOP_MSB
, de_end
);
597 if (priv
->rev
== TDA19988
) {
598 /* let incoming pixels fill the active space (if any) */
599 reg_write(encoder
, REG_ENABLE_SPACE
, 0x01);
602 reg_write16(encoder
, REG_REFPIX_MSB
, ref_pix
);
603 reg_write16(encoder
, REG_REFLINE_MSB
, ref_line
);
605 reg
= TBG_CNTRL_1_VHX_EXT_DE
|
606 TBG_CNTRL_1_VHX_EXT_HS
|
607 TBG_CNTRL_1_VHX_EXT_VS
|
608 TBG_CNTRL_1_DWIN_DIS
| /* HDCP off */
609 TBG_CNTRL_1_VH_TGL_2
;
610 if (mode
->flags
& (DRM_MODE_FLAG_NVSYNC
| DRM_MODE_FLAG_NHSYNC
))
611 reg
|= TBG_CNTRL_1_VH_TGL_0
;
612 reg_set(encoder
, REG_TBG_CNTRL_1
, reg
);
614 /* must be last register set: */
615 reg_clear(encoder
, REG_TBG_CNTRL_0
, TBG_CNTRL_0_SYNC_ONCE
);
618 static enum drm_connector_status
619 tda998x_encoder_detect(struct drm_encoder
*encoder
,
620 struct drm_connector
*connector
)
622 uint8_t val
= cec_read(encoder
, REG_CEC_RXSHPDLEV
);
623 return (val
& CEC_RXSHPDLEV_HPD
) ? connector_status_connected
:
624 connector_status_disconnected
;
628 read_edid_block(struct drm_encoder
*encoder
, uint8_t *buf
, int blk
)
630 uint8_t offset
, segptr
;
633 /* enable EDID read irq: */
634 reg_set(encoder
, REG_INT_FLAGS_2
, INT_FLAGS_2_EDID_BLK_RD
);
636 offset
= (blk
& 1) ? 128 : 0;
639 reg_write(encoder
, REG_DDC_ADDR
, 0xa0);
640 reg_write(encoder
, REG_DDC_OFFS
, offset
);
641 reg_write(encoder
, REG_DDC_SEGM_ADDR
, 0x60);
642 reg_write(encoder
, REG_DDC_SEGM
, segptr
);
644 /* enable reading EDID: */
645 reg_write(encoder
, REG_EDID_CTRL
, 0x1);
647 /* flag must be cleared by sw: */
648 reg_write(encoder
, REG_EDID_CTRL
, 0x0);
650 /* wait for block read to complete: */
651 for (i
= 100; i
> 0; i
--) {
652 uint8_t val
= reg_read(encoder
, REG_INT_FLAGS_2
);
653 if (val
& INT_FLAGS_2_EDID_BLK_RD
)
661 ret
= reg_read_range(encoder
, REG_EDID_DATA_0
, buf
, EDID_LENGTH
);
662 if (ret
!= EDID_LENGTH
) {
663 dev_err(encoder
->dev
->dev
, "failed to read edid block %d: %d",
668 reg_clear(encoder
, REG_INT_FLAGS_2
, INT_FLAGS_2_EDID_BLK_RD
);
674 do_get_edid(struct drm_encoder
*encoder
)
676 int j
= 0, valid_extensions
= 0;
677 uint8_t *block
, *new;
678 bool print_bad_edid
= drm_debug
& DRM_UT_KMS
;
680 if ((block
= kmalloc(EDID_LENGTH
, GFP_KERNEL
)) == NULL
)
683 /* base block fetch */
684 if (read_edid_block(encoder
, block
, 0))
687 if (!drm_edid_block_valid(block
, 0, print_bad_edid
))
690 /* if there's no extensions, we're done */
691 if (block
[0x7e] == 0)
694 new = krealloc(block
, (block
[0x7e] + 1) * EDID_LENGTH
, GFP_KERNEL
);
699 for (j
= 1; j
<= block
[0x7e]; j
++) {
700 uint8_t *ext_block
= block
+ (valid_extensions
+ 1) * EDID_LENGTH
;
701 if (read_edid_block(encoder
, ext_block
, j
))
704 if (!drm_edid_block_valid(ext_block
, j
, print_bad_edid
))
710 if (valid_extensions
!= block
[0x7e]) {
711 block
[EDID_LENGTH
-1] += block
[0x7e] - valid_extensions
;
712 block
[0x7e] = valid_extensions
;
713 new = krealloc(block
, (valid_extensions
+ 1) * EDID_LENGTH
, GFP_KERNEL
);
722 dev_warn(encoder
->dev
->dev
, "failed to read EDID\n");
728 tda998x_encoder_get_modes(struct drm_encoder
*encoder
,
729 struct drm_connector
*connector
)
731 struct edid
*edid
= (struct edid
*)do_get_edid(encoder
);
735 drm_mode_connector_update_edid_property(connector
, edid
);
736 n
= drm_add_edid_modes(connector
, edid
);
744 tda998x_encoder_create_resources(struct drm_encoder
*encoder
,
745 struct drm_connector
*connector
)
752 tda998x_encoder_set_property(struct drm_encoder
*encoder
,
753 struct drm_connector
*connector
,
754 struct drm_property
*property
,
762 tda998x_encoder_destroy(struct drm_encoder
*encoder
)
764 struct tda998x_priv
*priv
= to_tda998x_priv(encoder
);
765 drm_i2c_encoder_destroy(encoder
);
769 static struct drm_encoder_slave_funcs tda998x_encoder_funcs
= {
770 .set_config
= tda998x_encoder_set_config
,
771 .destroy
= tda998x_encoder_destroy
,
772 .dpms
= tda998x_encoder_dpms
,
773 .save
= tda998x_encoder_save
,
774 .restore
= tda998x_encoder_restore
,
775 .mode_fixup
= tda998x_encoder_mode_fixup
,
776 .mode_valid
= tda998x_encoder_mode_valid
,
777 .mode_set
= tda998x_encoder_mode_set
,
778 .detect
= tda998x_encoder_detect
,
779 .get_modes
= tda998x_encoder_get_modes
,
780 .create_resources
= tda998x_encoder_create_resources
,
781 .set_property
= tda998x_encoder_set_property
,
784 /* I2C driver functions */
787 tda998x_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
793 tda998x_remove(struct i2c_client
*client
)
799 tda998x_encoder_init(struct i2c_client
*client
,
800 struct drm_device
*dev
,
801 struct drm_encoder_slave
*encoder_slave
)
803 struct drm_encoder
*encoder
= &encoder_slave
->base
;
804 struct tda998x_priv
*priv
;
806 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
810 priv
->current_page
= 0;
811 priv
->cec
= i2c_new_dummy(client
->adapter
, 0x34);
812 priv
->dpms
= DRM_MODE_DPMS_OFF
;
814 encoder_slave
->slave_priv
= priv
;
815 encoder_slave
->slave_funcs
= &tda998x_encoder_funcs
;
817 /* wake up the device: */
818 cec_write(encoder
, REG_CEC_ENAMODS
,
819 CEC_ENAMODS_EN_RXSENS
| CEC_ENAMODS_EN_HDMI
);
821 tda998x_reset(encoder
);
824 priv
->rev
= reg_read(encoder
, REG_VERSION_LSB
) |
825 reg_read(encoder
, REG_VERSION_MSB
) << 8;
827 /* mask off feature bits: */
828 priv
->rev
&= ~0x30; /* not-hdcp and not-scalar bit */
831 case TDA9989N2
: dev_info(dev
->dev
, "found TDA9989 n2"); break;
832 case TDA19989
: dev_info(dev
->dev
, "found TDA19989"); break;
833 case TDA19989N2
: dev_info(dev
->dev
, "found TDA19989 n2"); break;
834 case TDA19988
: dev_info(dev
->dev
, "found TDA19988"); break;
836 DBG("found unsupported device: %04x", priv
->rev
);
840 /* after reset, enable DDC: */
841 reg_write(encoder
, REG_DDC_DISABLE
, 0x00);
843 /* set clock on DDC channel: */
844 reg_write(encoder
, REG_TX3
, 39);
846 /* if necessary, disable multi-master: */
847 if (priv
->rev
== TDA19989
)
848 reg_set(encoder
, REG_I2C_MASTER
, I2C_MASTER_DIS_MM
);
850 cec_write(encoder
, REG_CEC_FRO_IM_CLK_CTRL
,
851 CEC_FRO_IM_CLK_CTRL_GHOST_DIS
| CEC_FRO_IM_CLK_CTRL_IMCLK_SEL
);
856 /* if encoder_init fails, the encoder slave is never registered,
860 i2c_unregister_device(priv
->cec
);
862 encoder_slave
->slave_priv
= NULL
;
863 encoder_slave
->slave_funcs
= NULL
;
867 static struct i2c_device_id tda998x_ids
[] = {
871 MODULE_DEVICE_TABLE(i2c
, tda998x_ids
);
873 static struct drm_i2c_encoder_driver tda998x_driver
= {
875 .probe
= tda998x_probe
,
876 .remove
= tda998x_remove
,
880 .id_table
= tda998x_ids
,
882 .encoder_init
= tda998x_encoder_init
,
885 /* Module initialization */
891 return drm_i2c_encoder_register(THIS_MODULE
, &tda998x_driver
);
898 drm_i2c_encoder_unregister(&tda998x_driver
);
901 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
902 MODULE_DESCRIPTION("NXP Semiconductors TDA998X HDMI Encoder");
903 MODULE_LICENSE("GPL");
905 module_init(tda998x_init
);
906 module_exit(tda998x_exit
);