1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BayLibre, SAS
5 * Copyright (C) 2018-2019, Artem Mygaiev
6 * Copyright (C) 2017, Fresco Logic, Incorporated.
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/interrupt.h>
13 #include <linux/i2c.h>
14 #include <linux/bitfield.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/of_graph.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/regulator/consumer.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_bridge.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_edid.h>
26 #include <drm/drm_modes.h>
27 #include <drm/drm_print.h>
28 #include <drm/drm_probe_helper.h>
30 #include <sound/hdmi-codec.h>
32 #define IT66121_VENDOR_ID0_REG 0x00
33 #define IT66121_VENDOR_ID1_REG 0x01
34 #define IT66121_DEVICE_ID0_REG 0x02
35 #define IT66121_DEVICE_ID1_REG 0x03
37 #define IT66121_VENDOR_ID0 0x54
38 #define IT66121_VENDOR_ID1 0x49
39 #define IT66121_DEVICE_ID0 0x12
40 #define IT66121_DEVICE_ID1 0x06
41 #define IT66121_REVISION_MASK GENMASK(7, 4)
42 #define IT66121_DEVICE_ID1_MASK GENMASK(3, 0)
44 #define IT66121_MASTER_SEL_REG 0x10
45 #define IT66121_MASTER_SEL_HOST BIT(0)
47 #define IT66121_AFE_DRV_REG 0x61
48 #define IT66121_AFE_DRV_RST BIT(4)
49 #define IT66121_AFE_DRV_PWD BIT(5)
51 #define IT66121_INPUT_MODE_REG 0x70
52 #define IT66121_INPUT_MODE_RGB (0 << 6)
53 #define IT66121_INPUT_MODE_YUV422 BIT(6)
54 #define IT66121_INPUT_MODE_YUV444 (2 << 6)
55 #define IT66121_INPUT_MODE_CCIR656 BIT(4)
56 #define IT66121_INPUT_MODE_SYNCEMB BIT(3)
57 #define IT66121_INPUT_MODE_DDR BIT(2)
59 #define IT66121_INPUT_CSC_REG 0x72
60 #define IT66121_INPUT_CSC_ENDITHER BIT(7)
61 #define IT66121_INPUT_CSC_ENUDFILTER BIT(6)
62 #define IT66121_INPUT_CSC_DNFREE_GO BIT(5)
63 #define IT66121_INPUT_CSC_RGB_TO_YUV 0x02
64 #define IT66121_INPUT_CSC_YUV_TO_RGB 0x03
65 #define IT66121_INPUT_CSC_NO_CONV 0x00
67 #define IT66121_AFE_XP_REG 0x62
68 #define IT66121_AFE_XP_GAINBIT BIT(7)
69 #define IT66121_AFE_XP_PWDPLL BIT(6)
70 #define IT66121_AFE_XP_ENI BIT(5)
71 #define IT66121_AFE_XP_ENO BIT(4)
72 #define IT66121_AFE_XP_RESETB BIT(3)
73 #define IT66121_AFE_XP_PWDI BIT(2)
75 #define IT66121_AFE_IP_REG 0x64
76 #define IT66121_AFE_IP_GAINBIT BIT(7)
77 #define IT66121_AFE_IP_PWDPLL BIT(6)
78 #define IT66121_AFE_IP_CKSEL_05 (0 << 4)
79 #define IT66121_AFE_IP_CKSEL_1 BIT(4)
80 #define IT66121_AFE_IP_CKSEL_2 (2 << 4)
81 #define IT66121_AFE_IP_CKSEL_2OR4 (3 << 4)
82 #define IT66121_AFE_IP_ER0 BIT(3)
83 #define IT66121_AFE_IP_RESETB BIT(2)
84 #define IT66121_AFE_IP_ENC BIT(1)
85 #define IT66121_AFE_IP_EC1 BIT(0)
87 #define IT66121_AFE_XP_EC1_REG 0x68
88 #define IT66121_AFE_XP_EC1_LOWCLK BIT(4)
90 #define IT66121_SW_RST_REG 0x04
91 #define IT66121_SW_RST_REF BIT(5)
92 #define IT66121_SW_RST_AREF BIT(4)
93 #define IT66121_SW_RST_VID BIT(3)
94 #define IT66121_SW_RST_AUD BIT(2)
95 #define IT66121_SW_RST_HDCP BIT(0)
97 #define IT66121_DDC_COMMAND_REG 0x15
98 #define IT66121_DDC_COMMAND_BURST_READ 0x0
99 #define IT66121_DDC_COMMAND_EDID_READ 0x3
100 #define IT66121_DDC_COMMAND_FIFO_CLR 0x9
101 #define IT66121_DDC_COMMAND_SCL_PULSE 0xA
102 #define IT66121_DDC_COMMAND_ABORT 0xF
104 #define IT66121_HDCP_REG 0x20
105 #define IT66121_HDCP_CPDESIRED BIT(0)
106 #define IT66121_HDCP_EN1P1FEAT BIT(1)
108 #define IT66121_INT_STATUS1_REG 0x06
109 #define IT66121_INT_STATUS1_AUD_OVF BIT(7)
110 #define IT66121_INT_STATUS1_DDC_NOACK BIT(5)
111 #define IT66121_INT_STATUS1_DDC_FIFOERR BIT(4)
112 #define IT66121_INT_STATUS1_DDC_BUSHANG BIT(2)
113 #define IT66121_INT_STATUS1_RX_SENS_STATUS BIT(1)
114 #define IT66121_INT_STATUS1_HPD_STATUS BIT(0)
116 #define IT66121_DDC_HEADER_REG 0x11
117 #define IT66121_DDC_HEADER_HDCP 0x74
118 #define IT66121_DDC_HEADER_EDID 0xA0
120 #define IT66121_DDC_OFFSET_REG 0x12
121 #define IT66121_DDC_BYTE_REG 0x13
122 #define IT66121_DDC_SEGMENT_REG 0x14
123 #define IT66121_DDC_RD_FIFO_REG 0x17
125 #define IT66121_CLK_BANK_REG 0x0F
126 #define IT66121_CLK_BANK_PWROFF_RCLK BIT(6)
127 #define IT66121_CLK_BANK_PWROFF_ACLK BIT(5)
128 #define IT66121_CLK_BANK_PWROFF_TXCLK BIT(4)
129 #define IT66121_CLK_BANK_PWROFF_CRCLK BIT(3)
130 #define IT66121_CLK_BANK_0 0
131 #define IT66121_CLK_BANK_1 1
133 #define IT66121_INT_REG 0x05
134 #define IT66121_INT_ACTIVE_HIGH BIT(7)
135 #define IT66121_INT_OPEN_DRAIN BIT(6)
136 #define IT66121_INT_TX_CLK_OFF BIT(0)
138 #define IT66121_INT_MASK1_REG 0x09
139 #define IT66121_INT_MASK1_AUD_OVF BIT(7)
140 #define IT66121_INT_MASK1_DDC_NOACK BIT(5)
141 #define IT66121_INT_MASK1_DDC_FIFOERR BIT(4)
142 #define IT66121_INT_MASK1_DDC_BUSHANG BIT(2)
143 #define IT66121_INT_MASK1_RX_SENS BIT(1)
144 #define IT66121_INT_MASK1_HPD BIT(0)
146 #define IT66121_INT_CLR1_REG 0x0C
147 #define IT66121_INT_CLR1_PKTACP BIT(7)
148 #define IT66121_INT_CLR1_PKTNULL BIT(6)
149 #define IT66121_INT_CLR1_PKTGEN BIT(5)
150 #define IT66121_INT_CLR1_KSVLISTCHK BIT(4)
151 #define IT66121_INT_CLR1_AUTHDONE BIT(3)
152 #define IT66121_INT_CLR1_AUTHFAIL BIT(2)
153 #define IT66121_INT_CLR1_RX_SENS BIT(1)
154 #define IT66121_INT_CLR1_HPD BIT(0)
156 #define IT66121_AV_MUTE_REG 0xC1
157 #define IT66121_AV_MUTE_ON BIT(0)
158 #define IT66121_AV_MUTE_BLUESCR BIT(1)
160 #define IT66121_PKT_CTS_CTRL_REG 0xC5
161 #define IT66121_PKT_CTS_CTRL_SEL BIT(1)
163 #define IT66121_PKT_GEN_CTRL_REG 0xC6
164 #define IT66121_PKT_GEN_CTRL_ON BIT(0)
165 #define IT66121_PKT_GEN_CTRL_RPT BIT(1)
167 #define IT66121_AVIINFO_DB1_REG 0x158
168 #define IT66121_AVIINFO_DB2_REG 0x159
169 #define IT66121_AVIINFO_DB3_REG 0x15A
170 #define IT66121_AVIINFO_DB4_REG 0x15B
171 #define IT66121_AVIINFO_DB5_REG 0x15C
172 #define IT66121_AVIINFO_CSUM_REG 0x15D
173 #define IT66121_AVIINFO_DB6_REG 0x15E
174 #define IT66121_AVIINFO_DB7_REG 0x15F
175 #define IT66121_AVIINFO_DB8_REG 0x160
176 #define IT66121_AVIINFO_DB9_REG 0x161
177 #define IT66121_AVIINFO_DB10_REG 0x162
178 #define IT66121_AVIINFO_DB11_REG 0x163
179 #define IT66121_AVIINFO_DB12_REG 0x164
180 #define IT66121_AVIINFO_DB13_REG 0x165
182 #define IT66121_AVI_INFO_PKT_REG 0xCD
183 #define IT66121_AVI_INFO_PKT_ON BIT(0)
184 #define IT66121_AVI_INFO_PKT_RPT BIT(1)
186 #define IT66121_HDMI_MODE_REG 0xC0
187 #define IT66121_HDMI_MODE_HDMI BIT(0)
189 #define IT66121_SYS_STATUS_REG 0x0E
190 #define IT66121_SYS_STATUS_ACTIVE_IRQ BIT(7)
191 #define IT66121_SYS_STATUS_HPDETECT BIT(6)
192 #define IT66121_SYS_STATUS_SENDECTECT BIT(5)
193 #define IT66121_SYS_STATUS_VID_STABLE BIT(4)
194 #define IT66121_SYS_STATUS_AUD_CTS_CLR BIT(1)
195 #define IT66121_SYS_STATUS_CLEAR_IRQ BIT(0)
197 #define IT66121_DDC_STATUS_REG 0x16
198 #define IT66121_DDC_STATUS_TX_DONE BIT(7)
199 #define IT66121_DDC_STATUS_ACTIVE BIT(6)
200 #define IT66121_DDC_STATUS_NOACK BIT(5)
201 #define IT66121_DDC_STATUS_WAIT_BUS BIT(4)
202 #define IT66121_DDC_STATUS_ARBI_LOSE BIT(3)
203 #define IT66121_DDC_STATUS_FIFO_FULL BIT(2)
204 #define IT66121_DDC_STATUS_FIFO_EMPTY BIT(1)
205 #define IT66121_DDC_STATUS_FIFO_VALID BIT(0)
207 #define IT66121_EDID_SLEEP_US 20000
208 #define IT66121_EDID_TIMEOUT_US 200000
209 #define IT66121_EDID_FIFO_SIZE 32
211 #define IT66121_CLK_CTRL0_REG 0x58
212 #define IT66121_CLK_CTRL0_AUTO_OVER_SAMPLING BIT(4)
213 #define IT66121_CLK_CTRL0_EXT_MCLK_MASK GENMASK(3, 2)
214 #define IT66121_CLK_CTRL0_EXT_MCLK_128FS (0 << 2)
215 #define IT66121_CLK_CTRL0_EXT_MCLK_256FS BIT(2)
216 #define IT66121_CLK_CTRL0_EXT_MCLK_512FS (2 << 2)
217 #define IT66121_CLK_CTRL0_EXT_MCLK_1024FS (3 << 2)
218 #define IT66121_CLK_CTRL0_AUTO_IPCLK BIT(0)
219 #define IT66121_CLK_STATUS1_REG 0x5E
220 #define IT66121_CLK_STATUS2_REG 0x5F
222 #define IT66121_AUD_CTRL0_REG 0xE0
223 #define IT66121_AUD_SWL (3 << 6)
224 #define IT66121_AUD_16BIT (0 << 6)
225 #define IT66121_AUD_18BIT BIT(6)
226 #define IT66121_AUD_20BIT (2 << 6)
227 #define IT66121_AUD_24BIT (3 << 6)
228 #define IT66121_AUD_SPDIFTC BIT(5)
229 #define IT66121_AUD_SPDIF BIT(4)
230 #define IT66121_AUD_I2S (0 << 4)
231 #define IT66121_AUD_EN_I2S3 BIT(3)
232 #define IT66121_AUD_EN_I2S2 BIT(2)
233 #define IT66121_AUD_EN_I2S1 BIT(1)
234 #define IT66121_AUD_EN_I2S0 BIT(0)
235 #define IT66121_AUD_CTRL0_AUD_SEL BIT(4)
237 #define IT66121_AUD_CTRL1_REG 0xE1
238 #define IT66121_AUD_FIFOMAP_REG 0xE2
239 #define IT66121_AUD_CTRL3_REG 0xE3
240 #define IT66121_AUD_SRCVALID_FLAT_REG 0xE4
241 #define IT66121_AUD_FLAT_SRC0 BIT(4)
242 #define IT66121_AUD_FLAT_SRC1 BIT(5)
243 #define IT66121_AUD_FLAT_SRC2 BIT(6)
244 #define IT66121_AUD_FLAT_SRC3 BIT(7)
245 #define IT66121_AUD_HDAUDIO_REG 0xE5
247 #define IT66121_AUD_PKT_CTS0_REG 0x130
248 #define IT66121_AUD_PKT_CTS1_REG 0x131
249 #define IT66121_AUD_PKT_CTS2_REG 0x132
250 #define IT66121_AUD_PKT_N0_REG 0x133
251 #define IT66121_AUD_PKT_N1_REG 0x134
252 #define IT66121_AUD_PKT_N2_REG 0x135
254 #define IT66121_AUD_CHST_MODE_REG 0x191
255 #define IT66121_AUD_CHST_CAT_REG 0x192
256 #define IT66121_AUD_CHST_SRCNUM_REG 0x193
257 #define IT66121_AUD_CHST_CHTNUM_REG 0x194
258 #define IT66121_AUD_CHST_CA_FS_REG 0x198
259 #define IT66121_AUD_CHST_OFS_WL_REG 0x199
261 #define IT66121_AUD_PKT_CTS_CNT0_REG 0x1A0
262 #define IT66121_AUD_PKT_CTS_CNT1_REG 0x1A1
263 #define IT66121_AUD_PKT_CTS_CNT2_REG 0x1A2
265 #define IT66121_AUD_FS_22P05K 0x4
266 #define IT66121_AUD_FS_44P1K 0x0
267 #define IT66121_AUD_FS_88P2K 0x8
268 #define IT66121_AUD_FS_176P4K 0xC
269 #define IT66121_AUD_FS_24K 0x6
270 #define IT66121_AUD_FS_48K 0x2
271 #define IT66121_AUD_FS_96K 0xA
272 #define IT66121_AUD_FS_192K 0xE
273 #define IT66121_AUD_FS_768K 0x9
274 #define IT66121_AUD_FS_32K 0x3
275 #define IT66121_AUD_FS_OTHER 0x1
277 #define IT66121_AUD_SWL_21BIT 0xD
278 #define IT66121_AUD_SWL_24BIT 0xB
279 #define IT66121_AUD_SWL_23BIT 0x9
280 #define IT66121_AUD_SWL_22BIT 0x5
281 #define IT66121_AUD_SWL_20BIT 0x3
282 #define IT66121_AUD_SWL_17BIT 0xC
283 #define IT66121_AUD_SWL_19BIT 0x8
284 #define IT66121_AUD_SWL_18BIT 0x4
285 #define IT66121_AUD_SWL_16BIT 0x2
286 #define IT66121_AUD_SWL_NOT_INDICATED 0x0
288 #define IT66121_VENDOR_ID0 0x54
289 #define IT66121_VENDOR_ID1 0x49
290 #define IT66121_DEVICE_ID0 0x12
291 #define IT66121_DEVICE_ID1 0x06
292 #define IT66121_DEVICE_MASK 0x0F
293 #define IT66121_AFE_CLK_HIGH 80000 /* Khz */
296 struct regmap *regmap;
297 struct drm_bridge bridge;
298 struct drm_bridge *next_bridge;
299 struct drm_connector *connector;
301 struct gpio_desc *gpio_reset;
302 struct i2c_client *client;
303 struct regulator_bulk_data supplies[3];
305 struct mutex lock; /* Protects fields below and device registers */
306 struct hdmi_avi_infoframe hdmi_avi_infoframe;
308 struct platform_device *pdev;
316 static const struct regmap_range_cfg it66121_regmap_banks[] = {
321 .selector_reg = IT66121_CLK_BANK_REG,
322 .selector_mask = 0x1,
324 .window_start = 0x00,
329 static const struct regmap_config it66121_regmap_config = {
332 .max_register = 0x1FF,
333 .ranges = it66121_regmap_banks,
334 .num_ranges = ARRAY_SIZE(it66121_regmap_banks),
337 static void it66121_hw_reset(struct it66121_ctx *ctx)
339 gpiod_set_value(ctx->gpio_reset, 1);
341 gpiod_set_value(ctx->gpio_reset, 0);
344 static inline int ite66121_power_on(struct it66121_ctx *ctx)
346 return regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
349 static inline int ite66121_power_off(struct it66121_ctx *ctx)
351 return regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
354 static inline int it66121_preamble_ddc(struct it66121_ctx *ctx)
356 return regmap_write(ctx->regmap, IT66121_MASTER_SEL_REG, IT66121_MASTER_SEL_HOST);
359 static inline int it66121_fire_afe(struct it66121_ctx *ctx)
361 return regmap_write(ctx->regmap, IT66121_AFE_DRV_REG, 0);
364 /* TOFIX: Handle YCbCr Input & Output */
365 static int it66121_configure_input(struct it66121_ctx *ctx)
368 u8 mode = IT66121_INPUT_MODE_RGB;
370 if (ctx->bus_width == 12)
371 mode |= IT66121_INPUT_MODE_DDR;
373 ret = regmap_write(ctx->regmap, IT66121_INPUT_MODE_REG, mode);
377 return regmap_write(ctx->regmap, IT66121_INPUT_CSC_REG, IT66121_INPUT_CSC_NO_CONV);
381 * it66121_configure_afe() - Configure the analog front end
382 * @ctx: it66121_ctx object
383 * @mode: mode to configure
386 * zero if success, a negative error code otherwise.
388 static int it66121_configure_afe(struct it66121_ctx *ctx,
389 const struct drm_display_mode *mode)
393 ret = regmap_write(ctx->regmap, IT66121_AFE_DRV_REG,
394 IT66121_AFE_DRV_RST);
398 if (mode->clock > IT66121_AFE_CLK_HIGH) {
399 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_REG,
400 IT66121_AFE_XP_GAINBIT |
402 IT66121_AFE_XP_GAINBIT);
406 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_IP_REG,
407 IT66121_AFE_IP_GAINBIT |
410 IT66121_AFE_IP_GAINBIT);
414 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_EC1_REG,
415 IT66121_AFE_XP_EC1_LOWCLK, 0x80);
419 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_REG,
420 IT66121_AFE_XP_GAINBIT |
426 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_IP_REG,
427 IT66121_AFE_IP_GAINBIT |
429 IT66121_AFE_IP_EC1, IT66121_AFE_IP_ER0 |
434 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_EC1_REG,
435 IT66121_AFE_XP_EC1_LOWCLK,
436 IT66121_AFE_XP_EC1_LOWCLK);
441 /* Clear reset flags */
442 ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
443 IT66121_SW_RST_REF | IT66121_SW_RST_VID, 0);
447 return it66121_fire_afe(ctx);
450 static inline int it66121_wait_ddc_ready(struct it66121_ctx *ctx)
453 u32 busy = IT66121_DDC_STATUS_NOACK | IT66121_DDC_STATUS_WAIT_BUS |
454 IT66121_DDC_STATUS_ARBI_LOSE;
456 ret = regmap_read_poll_timeout(ctx->regmap, IT66121_DDC_STATUS_REG, val, true,
457 IT66121_EDID_SLEEP_US, IT66121_EDID_TIMEOUT_US);
467 static int it66121_clear_ddc_fifo(struct it66121_ctx *ctx)
471 ret = it66121_preamble_ddc(ctx);
475 return regmap_write(ctx->regmap, IT66121_DDC_COMMAND_REG,
476 IT66121_DDC_COMMAND_FIFO_CLR);
479 static int it66121_abort_ddc_ops(struct it66121_ctx *ctx)
482 unsigned int swreset, cpdesire;
484 ret = regmap_read(ctx->regmap, IT66121_SW_RST_REG, &swreset);
488 ret = regmap_read(ctx->regmap, IT66121_HDCP_REG, &cpdesire);
492 ret = regmap_write(ctx->regmap, IT66121_HDCP_REG,
493 cpdesire & (~IT66121_HDCP_CPDESIRED & 0xFF));
497 ret = regmap_write(ctx->regmap, IT66121_SW_RST_REG,
498 (swreset | IT66121_SW_RST_HDCP));
502 ret = it66121_preamble_ddc(ctx);
506 ret = regmap_write(ctx->regmap, IT66121_DDC_COMMAND_REG,
507 IT66121_DDC_COMMAND_ABORT);
511 return it66121_wait_ddc_ready(ctx);
514 static int it66121_get_edid_block(void *context, u8 *buf,
515 unsigned int block, size_t len)
517 struct it66121_ctx *ctx = context;
523 offset = (block % 2) * len;
526 ret = regmap_read(ctx->regmap, IT66121_INT_STATUS1_REG, &val);
530 if (val & IT66121_INT_STATUS1_DDC_BUSHANG) {
531 ret = it66121_abort_ddc_ops(ctx);
536 ret = it66121_clear_ddc_fifo(ctx);
541 cnt = (remain > IT66121_EDID_FIFO_SIZE) ?
542 IT66121_EDID_FIFO_SIZE : remain;
543 ret = it66121_preamble_ddc(ctx);
547 ret = regmap_write(ctx->regmap, IT66121_DDC_COMMAND_REG,
548 IT66121_DDC_COMMAND_FIFO_CLR);
552 ret = it66121_wait_ddc_ready(ctx);
556 ret = regmap_read(ctx->regmap, IT66121_INT_STATUS1_REG, &val);
560 if (val & IT66121_INT_STATUS1_DDC_BUSHANG) {
561 ret = it66121_abort_ddc_ops(ctx);
566 ret = it66121_preamble_ddc(ctx);
570 ret = regmap_write(ctx->regmap, IT66121_DDC_HEADER_REG,
571 IT66121_DDC_HEADER_EDID);
575 ret = regmap_write(ctx->regmap, IT66121_DDC_OFFSET_REG, offset);
579 ret = regmap_write(ctx->regmap, IT66121_DDC_BYTE_REG, cnt);
583 ret = regmap_write(ctx->regmap, IT66121_DDC_SEGMENT_REG, block);
587 ret = regmap_write(ctx->regmap, IT66121_DDC_COMMAND_REG,
588 IT66121_DDC_COMMAND_EDID_READ);
595 /* Per programming manual, sleep here before emptying the FIFO */
598 ret = it66121_wait_ddc_ready(ctx);
603 ret = regmap_read(ctx->regmap, IT66121_DDC_RD_FIFO_REG, &val);
614 static bool it66121_is_hpd_detect(struct it66121_ctx *ctx)
618 if (regmap_read(ctx->regmap, IT66121_SYS_STATUS_REG, &val))
621 return val & IT66121_SYS_STATUS_HPDETECT;
624 static int it66121_bridge_attach(struct drm_bridge *bridge,
625 enum drm_bridge_attach_flags flags)
627 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
630 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR))
633 ret = drm_bridge_attach(bridge->encoder, ctx->next_bridge, bridge, flags);
637 ret = regmap_write_bits(ctx->regmap, IT66121_CLK_BANK_REG,
638 IT66121_CLK_BANK_PWROFF_RCLK, 0);
642 ret = regmap_write_bits(ctx->regmap, IT66121_INT_REG,
643 IT66121_INT_TX_CLK_OFF, 0);
647 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_DRV_REG,
648 IT66121_AFE_DRV_PWD, 0);
652 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_REG,
653 IT66121_AFE_XP_PWDI | IT66121_AFE_XP_PWDPLL, 0);
657 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_IP_REG,
658 IT66121_AFE_IP_PWDPLL, 0);
662 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_DRV_REG,
663 IT66121_AFE_DRV_RST, 0);
667 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_XP_REG,
668 IT66121_AFE_XP_RESETB, IT66121_AFE_XP_RESETB);
672 ret = regmap_write_bits(ctx->regmap, IT66121_AFE_IP_REG,
673 IT66121_AFE_IP_RESETB, IT66121_AFE_IP_RESETB);
677 ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
683 /* Per programming manual, sleep here for bridge to settle */
686 /* Start interrupts */
687 return regmap_write_bits(ctx->regmap, IT66121_INT_MASK1_REG,
688 IT66121_INT_MASK1_DDC_NOACK |
689 IT66121_INT_MASK1_DDC_FIFOERR |
690 IT66121_INT_MASK1_DDC_BUSHANG, 0);
693 static int it66121_set_mute(struct it66121_ctx *ctx, bool mute)
696 unsigned int val = 0;
699 val = IT66121_AV_MUTE_ON;
701 ret = regmap_write_bits(ctx->regmap, IT66121_AV_MUTE_REG, IT66121_AV_MUTE_ON, val);
705 return regmap_write(ctx->regmap, IT66121_PKT_GEN_CTRL_REG,
706 IT66121_PKT_GEN_CTRL_ON | IT66121_PKT_GEN_CTRL_RPT);
709 #define MAX_OUTPUT_SEL_FORMATS 1
711 static u32 *it66121_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
712 struct drm_bridge_state *bridge_state,
713 struct drm_crtc_state *crtc_state,
714 struct drm_connector_state *conn_state,
715 unsigned int *num_output_fmts)
719 output_fmts = kcalloc(MAX_OUTPUT_SEL_FORMATS, sizeof(*output_fmts),
724 /* TOFIX handle more than MEDIA_BUS_FMT_RGB888_1X24 as output format */
725 output_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
726 *num_output_fmts = 1;
731 #define MAX_INPUT_SEL_FORMATS 1
733 static u32 *it66121_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
734 struct drm_bridge_state *bridge_state,
735 struct drm_crtc_state *crtc_state,
736 struct drm_connector_state *conn_state,
738 unsigned int *num_input_fmts)
740 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
745 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
750 if (ctx->bus_width == 12)
751 /* IT66121FN Datasheet specifies Little-Endian ordering */
752 input_fmts[0] = MEDIA_BUS_FMT_RGB888_2X12_LE;
754 /* TOFIX support more input bus formats in 24bit width */
755 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
761 static void it66121_bridge_enable(struct drm_bridge *bridge,
762 struct drm_bridge_state *bridge_state)
764 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
765 struct drm_atomic_state *state = bridge_state->base.state;
767 ctx->connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
769 it66121_set_mute(ctx, false);
772 static void it66121_bridge_disable(struct drm_bridge *bridge,
773 struct drm_bridge_state *bridge_state)
775 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
777 it66121_set_mute(ctx, true);
779 ctx->connector = NULL;
783 void it66121_bridge_mode_set(struct drm_bridge *bridge,
784 const struct drm_display_mode *mode,
785 const struct drm_display_mode *adjusted_mode)
788 u8 buf[HDMI_INFOFRAME_SIZE(AVI)];
789 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
790 const u16 aviinfo_reg[HDMI_AVI_INFOFRAME_SIZE] = {
791 IT66121_AVIINFO_DB1_REG,
792 IT66121_AVIINFO_DB2_REG,
793 IT66121_AVIINFO_DB3_REG,
794 IT66121_AVIINFO_DB4_REG,
795 IT66121_AVIINFO_DB5_REG,
796 IT66121_AVIINFO_DB6_REG,
797 IT66121_AVIINFO_DB7_REG,
798 IT66121_AVIINFO_DB8_REG,
799 IT66121_AVIINFO_DB9_REG,
800 IT66121_AVIINFO_DB10_REG,
801 IT66121_AVIINFO_DB11_REG,
802 IT66121_AVIINFO_DB12_REG,
803 IT66121_AVIINFO_DB13_REG
806 mutex_lock(&ctx->lock);
808 hdmi_avi_infoframe_init(&ctx->hdmi_avi_infoframe);
810 ret = drm_hdmi_avi_infoframe_from_display_mode(&ctx->hdmi_avi_infoframe, ctx->connector,
813 DRM_ERROR("Failed to setup AVI infoframe: %d\n", ret);
817 ret = hdmi_avi_infoframe_pack(&ctx->hdmi_avi_infoframe, buf, sizeof(buf));
819 DRM_ERROR("Failed to pack infoframe: %d\n", ret);
823 /* Write new AVI infoframe packet */
824 for (i = 0; i < HDMI_AVI_INFOFRAME_SIZE; i++) {
825 if (regmap_write(ctx->regmap, aviinfo_reg[i], buf[i + HDMI_INFOFRAME_HEADER_SIZE]))
828 if (regmap_write(ctx->regmap, IT66121_AVIINFO_CSUM_REG, buf[3]))
831 /* Enable AVI infoframe */
832 if (regmap_write(ctx->regmap, IT66121_AVI_INFO_PKT_REG,
833 IT66121_AVI_INFO_PKT_ON | IT66121_AVI_INFO_PKT_RPT))
836 /* Set TX mode to HDMI */
837 if (regmap_write(ctx->regmap, IT66121_HDMI_MODE_REG, IT66121_HDMI_MODE_HDMI))
840 if (regmap_write_bits(ctx->regmap, IT66121_CLK_BANK_REG,
841 IT66121_CLK_BANK_PWROFF_TXCLK, IT66121_CLK_BANK_PWROFF_TXCLK))
844 if (it66121_configure_input(ctx))
847 if (it66121_configure_afe(ctx, adjusted_mode))
850 regmap_write_bits(ctx->regmap, IT66121_CLK_BANK_REG, IT66121_CLK_BANK_PWROFF_TXCLK, 0);
853 mutex_unlock(&ctx->lock);
856 static enum drm_mode_status it66121_bridge_mode_valid(struct drm_bridge *bridge,
857 const struct drm_display_info *info,
858 const struct drm_display_mode *mode)
860 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
861 unsigned long max_clock;
863 max_clock = (ctx->bus_width == 12) ? 74250 : 148500;
865 if (mode->clock > max_clock)
866 return MODE_CLOCK_HIGH;
868 if (mode->clock < 25000)
869 return MODE_CLOCK_LOW;
874 static enum drm_connector_status it66121_bridge_detect(struct drm_bridge *bridge)
876 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
878 return it66121_is_hpd_detect(ctx) ? connector_status_connected
879 : connector_status_disconnected;
882 static void it66121_bridge_hpd_enable(struct drm_bridge *bridge)
884 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
887 ret = regmap_write_bits(ctx->regmap, IT66121_INT_MASK1_REG, IT66121_INT_MASK1_HPD, 0);
889 dev_err(ctx->dev, "failed to enable HPD IRQ\n");
892 static void it66121_bridge_hpd_disable(struct drm_bridge *bridge)
894 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
897 ret = regmap_write_bits(ctx->regmap, IT66121_INT_MASK1_REG,
898 IT66121_INT_MASK1_HPD, IT66121_INT_MASK1_HPD);
900 dev_err(ctx->dev, "failed to disable HPD IRQ\n");
903 static struct edid *it66121_bridge_get_edid(struct drm_bridge *bridge,
904 struct drm_connector *connector)
906 struct it66121_ctx *ctx = container_of(bridge, struct it66121_ctx, bridge);
909 mutex_lock(&ctx->lock);
910 edid = drm_do_get_edid(connector, it66121_get_edid_block, ctx);
911 mutex_unlock(&ctx->lock);
916 static const struct drm_bridge_funcs it66121_bridge_funcs = {
917 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
918 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
919 .atomic_reset = drm_atomic_helper_bridge_reset,
920 .attach = it66121_bridge_attach,
921 .atomic_get_output_bus_fmts = it66121_bridge_atomic_get_output_bus_fmts,
922 .atomic_get_input_bus_fmts = it66121_bridge_atomic_get_input_bus_fmts,
923 .atomic_enable = it66121_bridge_enable,
924 .atomic_disable = it66121_bridge_disable,
925 .mode_set = it66121_bridge_mode_set,
926 .mode_valid = it66121_bridge_mode_valid,
927 .detect = it66121_bridge_detect,
928 .get_edid = it66121_bridge_get_edid,
929 .hpd_enable = it66121_bridge_hpd_enable,
930 .hpd_disable = it66121_bridge_hpd_disable,
933 static irqreturn_t it66121_irq_threaded_handler(int irq, void *dev_id)
937 struct it66121_ctx *ctx = dev_id;
938 struct device *dev = ctx->dev;
939 enum drm_connector_status status;
942 mutex_lock(&ctx->lock);
944 ret = regmap_read(ctx->regmap, IT66121_SYS_STATUS_REG, &val);
948 if (!(val & IT66121_SYS_STATUS_ACTIVE_IRQ))
951 ret = regmap_read(ctx->regmap, IT66121_INT_STATUS1_REG, &val);
953 dev_err(dev, "Cannot read STATUS1_REG %d\n", ret);
955 if (val & IT66121_INT_STATUS1_DDC_FIFOERR)
956 it66121_clear_ddc_fifo(ctx);
957 if (val & (IT66121_INT_STATUS1_DDC_BUSHANG |
958 IT66121_INT_STATUS1_DDC_NOACK))
959 it66121_abort_ddc_ops(ctx);
960 if (val & IT66121_INT_STATUS1_HPD_STATUS) {
961 regmap_write_bits(ctx->regmap, IT66121_INT_CLR1_REG,
962 IT66121_INT_CLR1_HPD, IT66121_INT_CLR1_HPD);
964 status = it66121_is_hpd_detect(ctx) ? connector_status_connected
965 : connector_status_disconnected;
971 regmap_write_bits(ctx->regmap, IT66121_SYS_STATUS_REG,
972 IT66121_SYS_STATUS_CLEAR_IRQ,
973 IT66121_SYS_STATUS_CLEAR_IRQ);
976 mutex_unlock(&ctx->lock);
979 drm_bridge_hpd_notify(&ctx->bridge, status);
984 static int it661221_set_chstat(struct it66121_ctx *ctx, u8 iec60958_chstat[])
988 ret = regmap_write(ctx->regmap, IT66121_AUD_CHST_MODE_REG, iec60958_chstat[0] & 0x7C);
992 ret = regmap_write(ctx->regmap, IT66121_AUD_CHST_CAT_REG, iec60958_chstat[1]);
996 ret = regmap_write(ctx->regmap, IT66121_AUD_CHST_SRCNUM_REG, iec60958_chstat[2] & 0x0F);
1000 ret = regmap_write(ctx->regmap, IT66121_AUD_CHST_CHTNUM_REG,
1001 (iec60958_chstat[2] >> 4) & 0x0F);
1005 ret = regmap_write(ctx->regmap, IT66121_AUD_CHST_CA_FS_REG, iec60958_chstat[3]);
1009 return regmap_write(ctx->regmap, IT66121_AUD_CHST_OFS_WL_REG, iec60958_chstat[4]);
1012 static int it661221_set_lpcm_audio(struct it66121_ctx *ctx, u8 audio_src_num, u8 audio_swl)
1015 unsigned int audio_enable = 0;
1016 unsigned int audio_format = 0;
1018 switch (audio_swl) {
1020 audio_enable |= IT66121_AUD_16BIT;
1023 audio_enable |= IT66121_AUD_18BIT;
1026 audio_enable |= IT66121_AUD_20BIT;
1030 audio_enable |= IT66121_AUD_24BIT;
1034 audio_format |= 0x40;
1035 switch (audio_src_num) {
1037 audio_enable |= IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
1038 IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0;
1041 audio_enable |= IT66121_AUD_EN_I2S2 | IT66121_AUD_EN_I2S1 |
1042 IT66121_AUD_EN_I2S0;
1045 audio_enable |= IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0;
1049 audio_format &= ~0x40;
1050 audio_enable |= IT66121_AUD_EN_I2S0;
1054 audio_format |= 0x01;
1055 ctx->audio.ch_enable = audio_enable;
1057 ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, audio_enable & 0xF0);
1061 ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL1_REG, audio_format);
1065 ret = regmap_write(ctx->regmap, IT66121_AUD_FIFOMAP_REG, 0xE4);
1069 ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL3_REG, 0x00);
1073 ret = regmap_write(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG, 0x00);
1077 return regmap_write(ctx->regmap, IT66121_AUD_HDAUDIO_REG, 0x00);
1080 static int it661221_set_ncts(struct it66121_ctx *ctx, u8 fs)
1086 case IT66121_AUD_FS_32K:
1089 case IT66121_AUD_FS_44P1K:
1092 case IT66121_AUD_FS_48K:
1095 case IT66121_AUD_FS_88P2K:
1098 case IT66121_AUD_FS_96K:
1101 case IT66121_AUD_FS_176P4K:
1104 case IT66121_AUD_FS_192K:
1107 case IT66121_AUD_FS_768K:
1115 ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N0_REG, (u8)((n) & 0xFF));
1119 ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N1_REG, (u8)((n >> 8) & 0xFF));
1123 ret = regmap_write(ctx->regmap, IT66121_AUD_PKT_N2_REG, (u8)((n >> 16) & 0xF));
1127 if (ctx->audio.auto_cts) {
1129 u8 cts_stable_cnt = 0;
1130 unsigned int sum_cts = 0;
1131 unsigned int cts = 0;
1132 unsigned int last_cts = 0;
1136 while (loop_cnt--) {
1138 regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT2_REG, &val);
1140 regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT1_REG, &val);
1142 regmap_read(ctx->regmap, IT66121_AUD_PKT_CTS_CNT0_REG, &val);
1148 diff = last_cts - cts;
1150 diff = cts - last_cts;
1161 if (cts_stable_cnt >= 32) {
1162 last_cts = (sum_cts >> 5);
1168 regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS0_REG, (u8)((last_cts) & 0xFF));
1169 regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS1_REG, (u8)((last_cts >> 8) & 0xFF));
1170 regmap_write(ctx->regmap, IT66121_AUD_PKT_CTS2_REG, (u8)((last_cts >> 16) & 0x0F));
1173 ret = regmap_write(ctx->regmap, 0xF8, 0xC3);
1177 ret = regmap_write(ctx->regmap, 0xF8, 0xA5);
1181 if (ctx->audio.auto_cts) {
1182 ret = regmap_write_bits(ctx->regmap, IT66121_PKT_CTS_CTRL_REG,
1183 IT66121_PKT_CTS_CTRL_SEL,
1186 ret = regmap_write_bits(ctx->regmap, IT66121_PKT_CTS_CTRL_REG,
1187 IT66121_PKT_CTS_CTRL_SEL,
1194 return regmap_write(ctx->regmap, 0xF8, 0xFF);
1197 static int it661221_audio_output_enable(struct it66121_ctx *ctx, bool enable)
1202 ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
1203 IT66121_SW_RST_AUD | IT66121_SW_RST_AREF,
1208 ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
1209 IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
1210 IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0,
1211 ctx->audio.ch_enable);
1213 ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
1214 IT66121_AUD_EN_I2S3 | IT66121_AUD_EN_I2S2 |
1215 IT66121_AUD_EN_I2S1 | IT66121_AUD_EN_I2S0,
1216 ctx->audio.ch_enable & 0xF0);
1220 ret = regmap_write_bits(ctx->regmap, IT66121_SW_RST_REG,
1221 IT66121_SW_RST_AUD | IT66121_SW_RST_AREF,
1222 IT66121_SW_RST_AUD | IT66121_SW_RST_AREF);
1228 static int it661221_audio_ch_enable(struct it66121_ctx *ctx, bool enable)
1233 ret = regmap_write(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG, 0);
1237 ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, ctx->audio.ch_enable);
1239 ret = regmap_write(ctx->regmap, IT66121_AUD_CTRL0_REG, ctx->audio.ch_enable & 0xF0);
1245 static int it66121_audio_hw_params(struct device *dev, void *data,
1246 struct hdmi_codec_daifmt *daifmt,
1247 struct hdmi_codec_params *params)
1252 struct it66121_ctx *ctx = dev_get_drvdata(dev);
1253 static u8 iec60958_chstat[5];
1254 unsigned int channels = params->channels;
1255 unsigned int sample_rate = params->sample_rate;
1256 unsigned int sample_width = params->sample_width;
1258 mutex_lock(&ctx->lock);
1259 dev_dbg(dev, "%s: %u, %u, %u, %u\n", __func__,
1260 daifmt->fmt, sample_rate, sample_width, channels);
1262 switch (daifmt->fmt) {
1264 dev_dbg(dev, "Using HDMI I2S\n");
1267 dev_err(dev, "Invalid or unsupported DAI format %d\n", daifmt->fmt);
1272 // Set audio clock recovery (N/CTS)
1273 ret = regmap_write(ctx->regmap, IT66121_CLK_CTRL0_REG,
1274 IT66121_CLK_CTRL0_AUTO_OVER_SAMPLING |
1275 IT66121_CLK_CTRL0_EXT_MCLK_256FS |
1276 IT66121_CLK_CTRL0_AUTO_IPCLK);
1280 ret = regmap_write_bits(ctx->regmap, IT66121_AUD_CTRL0_REG,
1281 IT66121_AUD_CTRL0_AUD_SEL, 0); // remove spdif selection
1285 switch (sample_rate) {
1287 fs = IT66121_AUD_FS_44P1K;
1290 fs = IT66121_AUD_FS_88P2K;
1293 fs = IT66121_AUD_FS_176P4K;
1296 fs = IT66121_AUD_FS_32K;
1299 fs = IT66121_AUD_FS_48K;
1302 fs = IT66121_AUD_FS_96K;
1305 fs = IT66121_AUD_FS_192K;
1308 fs = IT66121_AUD_FS_768K;
1311 fs = IT66121_AUD_FS_48K;
1316 ret = it661221_set_ncts(ctx, fs);
1318 dev_err(dev, "Failed to set N/CTS: %d\n", ret);
1322 // Set audio format register (except audio channel enable)
1323 ret = it661221_set_lpcm_audio(ctx, (channels + 1) / 2, sample_width);
1325 dev_err(dev, "Failed to set LPCM audio: %d\n", ret);
1329 // Set audio channel status
1330 iec60958_chstat[0] = 0;
1331 if ((channels + 1) / 2 == 1)
1332 iec60958_chstat[0] |= 0x1;
1333 iec60958_chstat[0] &= ~(1 << 1);
1334 iec60958_chstat[1] = 0;
1335 iec60958_chstat[2] = (channels + 1) / 2;
1336 iec60958_chstat[2] |= (channels << 4) & 0xF0;
1337 iec60958_chstat[3] = fs;
1339 switch (sample_width) {
1341 swl = IT66121_AUD_SWL_21BIT;
1344 swl = IT66121_AUD_SWL_24BIT;
1347 swl = IT66121_AUD_SWL_23BIT;
1350 swl = IT66121_AUD_SWL_22BIT;
1353 swl = IT66121_AUD_SWL_20BIT;
1356 swl = IT66121_AUD_SWL_17BIT;
1359 swl = IT66121_AUD_SWL_19BIT;
1362 swl = IT66121_AUD_SWL_18BIT;
1365 swl = IT66121_AUD_SWL_16BIT;
1368 swl = IT66121_AUD_SWL_NOT_INDICATED;
1372 iec60958_chstat[4] = (((~fs) << 4) & 0xF0) | swl;
1373 ret = it661221_set_chstat(ctx, iec60958_chstat);
1375 dev_err(dev, "Failed to set channel status: %d\n", ret);
1379 // Enable audio channel enable while input clock stable (if SPDIF).
1380 ret = it661221_audio_ch_enable(ctx, true);
1382 dev_err(dev, "Failed to enable audio channel: %d\n", ret);
1386 ret = regmap_write_bits(ctx->regmap, IT66121_INT_MASK1_REG,
1387 IT66121_INT_MASK1_AUD_OVF,
1392 dev_dbg(dev, "HDMI audio enabled.\n");
1394 mutex_unlock(&ctx->lock);
1399 static int it66121_audio_startup(struct device *dev, void *data)
1402 struct it66121_ctx *ctx = dev_get_drvdata(dev);
1404 dev_dbg(dev, "%s\n", __func__);
1406 mutex_lock(&ctx->lock);
1407 ret = it661221_audio_output_enable(ctx, true);
1409 dev_err(dev, "Failed to enable audio output: %d\n", ret);
1411 mutex_unlock(&ctx->lock);
1416 static void it66121_audio_shutdown(struct device *dev, void *data)
1419 struct it66121_ctx *ctx = dev_get_drvdata(dev);
1421 dev_dbg(dev, "%s\n", __func__);
1423 mutex_lock(&ctx->lock);
1424 ret = it661221_audio_output_enable(ctx, false);
1426 dev_err(dev, "Failed to disable audio output: %d\n", ret);
1428 mutex_unlock(&ctx->lock);
1431 static int it66121_audio_mute(struct device *dev, void *data,
1432 bool enable, int direction)
1435 struct it66121_ctx *ctx = dev_get_drvdata(dev);
1437 dev_dbg(dev, "%s: enable=%s, direction=%d\n",
1438 __func__, enable ? "true" : "false", direction);
1440 mutex_lock(&ctx->lock);
1443 ret = regmap_write_bits(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG,
1444 IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
1445 IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3,
1446 IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
1447 IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3);
1449 ret = regmap_write_bits(ctx->regmap, IT66121_AUD_SRCVALID_FLAT_REG,
1450 IT66121_AUD_FLAT_SRC0 | IT66121_AUD_FLAT_SRC1 |
1451 IT66121_AUD_FLAT_SRC2 | IT66121_AUD_FLAT_SRC3,
1455 mutex_unlock(&ctx->lock);
1460 static int it66121_audio_get_eld(struct device *dev, void *data,
1461 u8 *buf, size_t len)
1463 struct it66121_ctx *ctx = dev_get_drvdata(dev);
1465 mutex_lock(&ctx->lock);
1467 memcpy(buf, ctx->connector->eld,
1468 min(sizeof(ctx->connector->eld), len));
1470 mutex_unlock(&ctx->lock);
1475 static const struct hdmi_codec_ops it66121_audio_codec_ops = {
1476 .hw_params = it66121_audio_hw_params,
1477 .audio_startup = it66121_audio_startup,
1478 .audio_shutdown = it66121_audio_shutdown,
1479 .mute_stream = it66121_audio_mute,
1480 .get_eld = it66121_audio_get_eld,
1481 .no_capture_mute = 1,
1484 static int it66121_audio_codec_init(struct it66121_ctx *ctx, struct device *dev)
1486 struct hdmi_codec_pdata codec_data = {
1487 .ops = &it66121_audio_codec_ops,
1488 .i2s = 1, /* Only i2s support for now */
1490 .max_i2s_channels = 8,
1493 dev_dbg(dev, "%s\n", __func__);
1495 if (!of_property_read_bool(dev->of_node, "#sound-dai-cells")) {
1496 dev_info(dev, "No \"#sound-dai-cells\", no audio\n");
1500 ctx->audio.pdev = platform_device_register_data(dev,
1501 HDMI_CODEC_DRV_NAME,
1502 PLATFORM_DEVID_AUTO,
1504 sizeof(codec_data));
1506 if (IS_ERR(ctx->audio.pdev)) {
1507 dev_err(dev, "Failed to initialize HDMI audio codec: %d\n",
1508 PTR_ERR_OR_ZERO(ctx->audio.pdev));
1511 return PTR_ERR_OR_ZERO(ctx->audio.pdev);
1514 static int it66121_probe(struct i2c_client *client,
1515 const struct i2c_device_id *id)
1517 u32 revision_id, vendor_ids[2] = { 0 }, device_ids[2] = { 0 };
1518 struct device_node *ep;
1520 struct it66121_ctx *ctx;
1521 struct device *dev = &client->dev;
1523 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1524 dev_err(dev, "I2C check functionality failed.\n");
1528 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
1532 ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, 0);
1537 ctx->client = client;
1539 of_property_read_u32(ep, "bus-width", &ctx->bus_width);
1542 if (ctx->bus_width != 12 && ctx->bus_width != 24)
1545 ep = of_graph_get_remote_node(dev->of_node, 1, -1);
1547 dev_err(ctx->dev, "The endpoint is unconnected\n");
1551 if (!of_device_is_available(ep)) {
1553 dev_err(ctx->dev, "The remote device is disabled\n");
1557 ctx->next_bridge = of_drm_find_bridge(ep);
1559 if (!ctx->next_bridge) {
1560 dev_dbg(ctx->dev, "Next bridge not found, deferring probe\n");
1561 return -EPROBE_DEFER;
1564 i2c_set_clientdata(client, ctx);
1565 mutex_init(&ctx->lock);
1567 ctx->supplies[0].supply = "vcn33";
1568 ctx->supplies[1].supply = "vcn18";
1569 ctx->supplies[2].supply = "vrf12";
1570 ret = devm_regulator_bulk_get(ctx->dev, 3, ctx->supplies);
1572 dev_err(ctx->dev, "regulator_bulk failed\n");
1576 ret = ite66121_power_on(ctx);
1580 it66121_hw_reset(ctx);
1582 ctx->regmap = devm_regmap_init_i2c(client, &it66121_regmap_config);
1583 if (IS_ERR(ctx->regmap)) {
1584 ite66121_power_off(ctx);
1585 return PTR_ERR(ctx->regmap);
1588 regmap_read(ctx->regmap, IT66121_VENDOR_ID0_REG, &vendor_ids[0]);
1589 regmap_read(ctx->regmap, IT66121_VENDOR_ID1_REG, &vendor_ids[1]);
1590 regmap_read(ctx->regmap, IT66121_DEVICE_ID0_REG, &device_ids[0]);
1591 regmap_read(ctx->regmap, IT66121_DEVICE_ID1_REG, &device_ids[1]);
1593 /* Revision is shared with DEVICE_ID1 */
1594 revision_id = FIELD_GET(IT66121_REVISION_MASK, device_ids[1]);
1595 device_ids[1] &= IT66121_DEVICE_ID1_MASK;
1597 if (vendor_ids[0] != IT66121_VENDOR_ID0 || vendor_ids[1] != IT66121_VENDOR_ID1 ||
1598 device_ids[0] != IT66121_DEVICE_ID0 || device_ids[1] != IT66121_DEVICE_ID1) {
1599 ite66121_power_off(ctx);
1603 ctx->bridge.funcs = &it66121_bridge_funcs;
1604 ctx->bridge.of_node = dev->of_node;
1605 ctx->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1606 ctx->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_HPD;
1608 ret = devm_request_threaded_irq(dev, client->irq, NULL, it66121_irq_threaded_handler,
1609 IRQF_ONESHOT, dev_name(dev), ctx);
1611 dev_err(dev, "Failed to request irq %d:%d\n", client->irq, ret);
1612 ite66121_power_off(ctx);
1616 it66121_audio_codec_init(ctx, dev);
1618 drm_bridge_add(&ctx->bridge);
1620 dev_info(ctx->dev, "IT66121 revision %d probed\n", revision_id);
1625 static int it66121_remove(struct i2c_client *client)
1627 struct it66121_ctx *ctx = i2c_get_clientdata(client);
1629 ite66121_power_off(ctx);
1630 drm_bridge_remove(&ctx->bridge);
1631 mutex_destroy(&ctx->lock);
1636 static const struct of_device_id it66121_dt_match[] = {
1637 { .compatible = "ite,it66121" },
1640 MODULE_DEVICE_TABLE(of, it66121_dt_match);
1642 static const struct i2c_device_id it66121_id[] = {
1646 MODULE_DEVICE_TABLE(i2c, it66121_id);
1648 static struct i2c_driver it66121_driver = {
1651 .of_match_table = it66121_dt_match,
1653 .probe = it66121_probe,
1654 .remove = it66121_remove,
1655 .id_table = it66121_id,
1658 module_i2c_driver(it66121_driver);
1660 MODULE_AUTHOR("Phong LE");
1661 MODULE_DESCRIPTION("IT66121 HDMI transmitter driver");
1662 MODULE_LICENSE("GPL v2");