1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Gateworks Corporation
5 #include <linux/delay.h>
6 #include <linux/hdmi.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_graph.h>
13 #include <linux/platform_device.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/types.h>
16 #include <linux/v4l2-dv-timings.h>
17 #include <linux/videodev2.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-dv-timings.h>
22 #include <media/v4l2-event.h>
23 #include <media/v4l2-fwnode.h>
24 #include <media/i2c/tda1997x.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
31 #include <dt-bindings/media/tda1997x.h>
33 #include "tda1997x_regs.h"
35 #define TDA1997X_MBUS_CODES 5
39 module_param(debug, int, 0644);
40 MODULE_PARM_DESC(debug, "debug level (0-2)");
43 static const char * const audtype_names[] = {
44 "PCM", /* PCM Samples */
45 "HBR", /* High Bit Rate Audio */
46 "OBA", /* One-Bit Audio */
47 "DST" /* Direct Stream Transfer */
50 /* Audio output port formats */
52 AUDFMT_TYPE_DISABLED = 0,
56 static const char * const audfmt_names[] = {
62 /* Video input formats */
63 static const char * const hdmi_colorspace_names[] = {
64 "RGB", "YUV422", "YUV444", "YUV420", "", "", "", "",
66 static const char * const hdmi_colorimetry_names[] = {
67 "", "ITU601", "ITU709", "Extended",
69 static const char * const v4l2_quantization_names[] = {
72 "Limited Range (16-235)",
75 /* Video output port formats */
76 static const char * const vidfmt_names[] = {
77 "RGB444/YUV444", /* RGB/YUV444 16bit data bus, 8bpp */
78 "YUV422 semi-planar", /* YUV422 16bit data base, 8bpp */
79 "YUV422 CCIR656", /* BT656 (YUV 8bpp 2 clock per pixel) */
84 * Colorspace conversion matrices
86 struct color_matrix_coefs {
118 /* NB: 4096 is 1.0 using fixed point numbers */
119 static const struct color_matrix_coefs conv_matrix[] = {
121 "YUV709 -> RGB full",
129 "YUV601 -> RGB full",
137 "RGB limited -> RGB full",
145 "RGB limited -> ITU601",
153 "RGB limited -> ITU709",
161 "RGB full -> ITU601",
169 "RGB full -> ITU709",
178 static const struct v4l2_dv_timings_cap tda1997x_dv_timings_cap = {
179 .type = V4L2_DV_BT_656_1120,
180 /* keep this initialization for compatibility with GCC < 4.4.6 */
183 V4L2_INIT_BT_TIMINGS(
184 640, 1920, /* min/max width */
185 350, 1200, /* min/max height */
186 13000000, 165000000, /* min/max pixelclock */
188 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
189 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
191 V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE |
192 V4L2_DV_BT_CAP_REDUCED_BLANKING |
193 V4L2_DV_BT_CAP_CUSTOM
197 /* regulator supplies */
198 static const char * const tda1997x_supply_name[] = {
199 "DOVDD", /* Digital I/O supply */
200 "DVDD", /* Digital Core supply */
201 "AVDD", /* Analog supply */
204 #define TDA1997X_NUM_SUPPLIES ARRAY_SIZE(tda1997x_supply_name)
211 enum tda1997x_hdmi_pads {
216 struct tda1997x_chip_info {
217 enum tda1997x_type type;
221 struct tda1997x_state {
222 const struct tda1997x_chip_info *info;
223 struct tda1997x_platform_data pdata;
224 struct i2c_client *client;
225 struct i2c_client *client_cec;
226 struct v4l2_subdev sd;
227 struct regulator_bulk_data supplies[TDA1997X_NUM_SUPPLIES];
228 struct media_pad pads[TDA1997X_NUM_PADS];
230 struct mutex page_lock;
233 /* detected info from chip */
242 char mptrw_in_progress;
243 char activity_status;
244 char input_detect[2];
247 struct hdmi_avi_infoframe avi_infoframe;
248 struct v4l2_hdmi_colorimetry colorimetry;
249 u32 rgb_quantization_range;
250 struct v4l2_dv_timings timings;
252 const struct color_matrix_coefs *conv;
253 u32 mbus_codes[TDA1997X_MBUS_CODES]; /* available modes */
254 u32 mbus_code; /* current mode */
258 struct v4l2_ctrl_handler hdl;
259 struct v4l2_ctrl *detect_tx_5v_ctrl;
260 struct v4l2_ctrl *rgb_quantization_range_ctrl;
264 int audio_samplerate;
266 int audio_samplesize;
268 struct mutex audio_lock;
269 struct snd_pcm_substream *audio_stream;
277 struct delayed_work delayed_work_enable_hpd;
280 static const struct v4l2_event tda1997x_ev_fmt = {
281 .type = V4L2_EVENT_SOURCE_CHANGE,
282 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
285 static const struct tda1997x_chip_info tda1997x_chip_info[] = {
296 static inline struct tda1997x_state *to_state(struct v4l2_subdev *sd)
298 return container_of(sd, struct tda1997x_state, sd);
301 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
303 return &container_of(ctrl->handler, struct tda1997x_state, hdl)->sd;
306 static int tda1997x_cec_read(struct v4l2_subdev *sd, u8 reg)
308 struct tda1997x_state *state = to_state(sd);
311 val = i2c_smbus_read_byte_data(state->client_cec, reg);
313 v4l_err(state->client, "read reg error: reg=%2x\n", reg);
320 static int tda1997x_cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
322 struct tda1997x_state *state = to_state(sd);
325 ret = i2c_smbus_write_byte_data(state->client_cec, reg, val);
327 v4l_err(state->client, "write reg error:reg=%2x,val=%2x\n",
335 /* -----------------------------------------------------------------------------
339 static int tda1997x_setpage(struct v4l2_subdev *sd, u8 page)
341 struct tda1997x_state *state = to_state(sd);
344 if (state->page != page) {
345 ret = i2c_smbus_write_byte_data(state->client,
346 REG_CURPAGE_00H, page);
348 v4l_err(state->client,
349 "write reg error:reg=%2x,val=%2x\n",
350 REG_CURPAGE_00H, page);
358 static inline int io_read(struct v4l2_subdev *sd, u16 reg)
360 struct tda1997x_state *state = to_state(sd);
363 mutex_lock(&state->page_lock);
364 if (tda1997x_setpage(sd, reg >> 8)) {
369 val = i2c_smbus_read_byte_data(state->client, reg&0xff);
371 v4l_err(state->client, "read reg error: reg=%2x\n", reg & 0xff);
377 mutex_unlock(&state->page_lock);
381 static inline long io_read16(struct v4l2_subdev *sd, u16 reg)
386 val = io_read(sd, reg);
390 val = io_read(sd, reg + 1);
398 static inline long io_read24(struct v4l2_subdev *sd, u16 reg)
403 val = io_read(sd, reg);
407 val = io_read(sd, reg + 1);
411 val = io_read(sd, reg + 2);
419 static unsigned int io_readn(struct v4l2_subdev *sd, u16 reg, u8 len, u8 *data)
425 for (i = 0; i < len; i++) {
426 val = io_read(sd, reg + i);
436 static int io_write(struct v4l2_subdev *sd, u16 reg, u8 val)
438 struct tda1997x_state *state = to_state(sd);
441 mutex_lock(&state->page_lock);
442 if (tda1997x_setpage(sd, reg >> 8)) {
447 ret = i2c_smbus_write_byte_data(state->client, reg & 0xff, val);
449 v4l_err(state->client, "write reg error:reg=%2x,val=%2x\n",
456 mutex_unlock(&state->page_lock);
460 static int io_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
464 ret = io_write(sd, reg, (val >> 8) & 0xff);
467 ret = io_write(sd, reg + 1, val & 0xff);
473 static int io_write24(struct v4l2_subdev *sd, u16 reg, u32 val)
477 ret = io_write(sd, reg, (val >> 16) & 0xff);
480 ret = io_write(sd, reg + 1, (val >> 8) & 0xff);
483 ret = io_write(sd, reg + 2, val & 0xff);
489 /* -----------------------------------------------------------------------------
494 HPD_LOW_BP, /* HPD low and pulse of at least 100ms */
495 HPD_LOW_OTHER, /* HPD low and pulse of at least 100ms */
496 HPD_HIGH_BP, /* HIGH */
498 HPD_PULSE, /* HPD low pulse */
501 /* manual HPD (Hot Plug Detect) control */
502 static int tda1997x_manual_hpd(struct v4l2_subdev *sd, enum hpd_mode mode)
504 u8 hpd_auto, hpd_pwr, hpd_man;
506 hpd_auto = io_read(sd, REG_HPD_AUTO_CTRL);
507 hpd_pwr = io_read(sd, REG_HPD_POWER);
508 hpd_man = io_read(sd, REG_HPD_MAN_CTRL);
510 /* mask out unused bits */
511 hpd_man &= (HPD_MAN_CTRL_HPD_PULSE |
517 /* HPD low and pulse of at least 100ms */
520 hpd_pwr &= ~HPD_POWER_BP_MASK;
521 /* disable HPD_A and HPD_B */
522 hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
523 io_write(sd, REG_HPD_POWER, hpd_pwr);
524 io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
529 hpd_pwr &= ~HPD_POWER_BP_MASK;
530 hpd_pwr |= 1 << HPD_POWER_BP_SHIFT;
531 io_write(sd, REG_HPD_POWER, hpd_pwr);
533 /* HPD low and pulse of at least 100ms */
535 /* disable HPD_A and HPD_B */
536 hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
538 hpd_auto &= ~HPD_AUTO_HP_OTHER;
539 io_write(sd, REG_HPD_AUTO_CTRL, hpd_auto);
540 io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
544 hpd_auto |= HPD_AUTO_HP_OTHER;
545 io_write(sd, REG_HPD_AUTO_CTRL, hpd_auto);
549 /* disable HPD_A and HPD_B */
550 hpd_man &= ~(HPD_MAN_CTRL_HPD_A | HPD_MAN_CTRL_HPD_B);
551 io_write(sd, REG_HPD_MAN_CTRL, hpd_man);
558 static void tda1997x_delayed_work_enable_hpd(struct work_struct *work)
560 struct delayed_work *dwork = to_delayed_work(work);
561 struct tda1997x_state *state = container_of(dwork,
562 struct tda1997x_state,
563 delayed_work_enable_hpd);
564 struct v4l2_subdev *sd = &state->sd;
566 v4l2_dbg(2, debug, sd, "%s\n", __func__);
569 tda1997x_manual_hpd(sd, HPD_HIGH_OTHER);
570 tda1997x_manual_hpd(sd, HPD_HIGH_BP);
572 state->edid.present = 1;
575 static void tda1997x_disable_edid(struct v4l2_subdev *sd)
577 struct tda1997x_state *state = to_state(sd);
579 v4l2_dbg(1, debug, sd, "%s\n", __func__);
580 cancel_delayed_work_sync(&state->delayed_work_enable_hpd);
583 tda1997x_manual_hpd(sd, HPD_LOW_BP);
586 static void tda1997x_enable_edid(struct v4l2_subdev *sd)
588 struct tda1997x_state *state = to_state(sd);
590 v4l2_dbg(1, debug, sd, "%s\n", __func__);
592 /* Enable hotplug after 100ms */
593 schedule_delayed_work(&state->delayed_work_enable_hpd, HZ / 10);
596 /* -----------------------------------------------------------------------------
601 * configure vid_fmt based on mbus_code
604 tda1997x_setup_format(struct tda1997x_state *state, u32 code)
606 v4l_dbg(1, debug, state->client, "%s code=0x%x\n", __func__, code);
608 case MEDIA_BUS_FMT_RGB121212_1X36:
609 case MEDIA_BUS_FMT_RGB888_1X24:
610 case MEDIA_BUS_FMT_YUV12_1X36:
611 case MEDIA_BUS_FMT_YUV8_1X24:
612 state->vid_fmt = OF_FMT_444;
614 case MEDIA_BUS_FMT_UYVY12_1X24:
615 case MEDIA_BUS_FMT_UYVY10_1X20:
616 case MEDIA_BUS_FMT_UYVY8_1X16:
617 state->vid_fmt = OF_FMT_422_SMPT;
619 case MEDIA_BUS_FMT_UYVY12_2X12:
620 case MEDIA_BUS_FMT_UYVY10_2X10:
621 case MEDIA_BUS_FMT_UYVY8_2X8:
622 state->vid_fmt = OF_FMT_422_CCIR;
625 v4l_err(state->client, "incompatible format (0x%x)\n", code);
628 v4l_dbg(1, debug, state->client, "%s code=0x%x fmt=%s\n", __func__,
629 code, vidfmt_names[state->vid_fmt]);
630 state->mbus_code = code;
636 * The color conversion matrix will convert between the colorimetry of the
637 * HDMI input to the desired output format RGB|YUV. RGB output is to be
638 * full-range and YUV is to be limited range.
640 * RGB full-range uses values from 0 to 255 which is recommended on a monitor
641 * and RGB Limited uses values from 16 to 236 (16=black, 235=white) which is
642 * typically recommended on a TV.
645 tda1997x_configure_csc(struct v4l2_subdev *sd)
647 struct tda1997x_state *state = to_state(sd);
648 struct hdmi_avi_infoframe *avi = &state->avi_infoframe;
649 struct v4l2_hdmi_colorimetry *c = &state->colorimetry;
650 /* Blanking code values depend on output colorspace (RGB or YUV) */
651 struct blanking_codes {
656 static const struct blanking_codes rgb_blanking = { 64, 64, 64 };
657 static const struct blanking_codes yuv_blanking = { 64, 512, 512 };
658 const struct blanking_codes *blanking_codes = NULL;
661 v4l_dbg(1, debug, state->client, "input:%s quant:%s output:%s\n",
662 hdmi_colorspace_names[avi->colorspace],
663 v4l2_quantization_names[c->quantization],
664 vidfmt_names[state->vid_fmt]);
666 switch (state->vid_fmt) {
669 blanking_codes = &rgb_blanking;
670 if (c->colorspace == V4L2_COLORSPACE_SRGB) {
671 if (c->quantization == V4L2_QUANTIZATION_LIM_RANGE)
672 state->conv = &conv_matrix[RGBLIMITED_RGBFULL];
674 if (c->colorspace == V4L2_COLORSPACE_REC709)
675 state->conv = &conv_matrix[ITU709_RGBFULL];
676 else if (c->colorspace == V4L2_COLORSPACE_SMPTE170M)
677 state->conv = &conv_matrix[ITU601_RGBFULL];
682 case OF_FMT_422_SMPT: /* semi-planar */
683 case OF_FMT_422_CCIR: /* CCIR656 */
684 blanking_codes = &yuv_blanking;
685 if ((c->colorspace == V4L2_COLORSPACE_SRGB) &&
686 (c->quantization == V4L2_QUANTIZATION_FULL_RANGE)) {
687 if (state->timings.bt.height <= 576)
688 state->conv = &conv_matrix[RGBFULL_ITU601];
690 state->conv = &conv_matrix[RGBFULL_ITU709];
691 } else if ((c->colorspace == V4L2_COLORSPACE_SRGB) &&
692 (c->quantization == V4L2_QUANTIZATION_LIM_RANGE)) {
693 if (state->timings.bt.height <= 576)
694 state->conv = &conv_matrix[RGBLIMITED_ITU601];
696 state->conv = &conv_matrix[RGBLIMITED_ITU709];
702 v4l_dbg(1, debug, state->client, "%s\n",
704 /* enable matrix conversion */
705 reg = io_read(sd, REG_VDP_CTRL);
706 reg &= ~VDP_CTRL_MATRIX_BP;
707 io_write(sd, REG_VDP_CTRL, reg);
709 io_write16(sd, REG_VDP_MATRIX + 0, state->conv->offint1);
710 io_write16(sd, REG_VDP_MATRIX + 2, state->conv->offint2);
711 io_write16(sd, REG_VDP_MATRIX + 4, state->conv->offint3);
713 io_write16(sd, REG_VDP_MATRIX + 6, state->conv->p11coef);
714 io_write16(sd, REG_VDP_MATRIX + 8, state->conv->p12coef);
715 io_write16(sd, REG_VDP_MATRIX + 10, state->conv->p13coef);
716 io_write16(sd, REG_VDP_MATRIX + 12, state->conv->p21coef);
717 io_write16(sd, REG_VDP_MATRIX + 14, state->conv->p22coef);
718 io_write16(sd, REG_VDP_MATRIX + 16, state->conv->p23coef);
719 io_write16(sd, REG_VDP_MATRIX + 18, state->conv->p31coef);
720 io_write16(sd, REG_VDP_MATRIX + 20, state->conv->p32coef);
721 io_write16(sd, REG_VDP_MATRIX + 22, state->conv->p33coef);
723 io_write16(sd, REG_VDP_MATRIX + 24, state->conv->offout1);
724 io_write16(sd, REG_VDP_MATRIX + 26, state->conv->offout2);
725 io_write16(sd, REG_VDP_MATRIX + 28, state->conv->offout3);
727 /* disable matrix conversion */
728 reg = io_read(sd, REG_VDP_CTRL);
729 reg |= VDP_CTRL_MATRIX_BP;
730 io_write(sd, REG_VDP_CTRL, reg);
733 /* SetBlankingCodes */
734 if (blanking_codes) {
735 io_write16(sd, REG_BLK_GY, blanking_codes->code_gy);
736 io_write16(sd, REG_BLK_BU, blanking_codes->code_bu);
737 io_write16(sd, REG_BLK_RV, blanking_codes->code_rv);
741 /* Configure frame detection window and VHREF timing generator */
743 tda1997x_configure_vhref(struct v4l2_subdev *sd)
745 struct tda1997x_state *state = to_state(sd);
746 const struct v4l2_bt_timings *bt = &state->timings.bt;
748 u16 href_start, href_end;
749 u16 vref_f1_start, vref_f2_start;
750 u8 vref_f1_width, vref_f2_width;
752 u16 fieldref_f1_start, fieldref_f2_start;
755 href_start = bt->hbackporch + bt->hsync + 1;
756 href_end = href_start + bt->width;
757 vref_f1_start = bt->height + bt->vbackporch + bt->vsync +
758 bt->il_vbackporch + bt->il_vsync +
760 vref_f1_width = bt->vbackporch + bt->vsync + bt->vfrontporch;
763 fieldref_f1_start = 0;
764 fieldref_f2_start = 0;
765 if (bt->interlaced) {
766 vref_f2_start = (bt->height / 2) +
767 (bt->il_vbackporch + bt->il_vsync - 1);
768 vref_f2_width = bt->il_vbackporch + bt->il_vsync +
770 fieldref_f2_start = vref_f2_start + bt->il_vfrontporch +
775 width = V4L2_DV_BT_FRAME_WIDTH(bt);
776 lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
779 * Configure Frame Detection Window:
780 * horiz area where the VHREF module consider a VSYNC a new frame
782 io_write16(sd, REG_FDW_S, 0x2ef); /* start position */
783 io_write16(sd, REG_FDW_E, 0x141); /* end position */
785 /* Set Pixel And Line Counters */
786 if (state->chip_revision == 0)
787 io_write16(sd, REG_PXCNT_PR, 4);
789 io_write16(sd, REG_PXCNT_PR, 1);
790 io_write16(sd, REG_PXCNT_NPIX, width & MASK_VHREF);
791 io_write16(sd, REG_LCNT_PR, 1);
792 io_write16(sd, REG_LCNT_NLIN, lines & MASK_VHREF);
795 * Configure the VHRef timing generator responsible for rebuilding all
796 * horiz and vert synch and ref signals from its input allowing auto
797 * detection algorithms and forcing predefined modes (480i & 576i)
799 reg = VHREF_STD_DET_OFF << VHREF_STD_DET_SHIFT;
800 io_write(sd, REG_VHREF_CTRL, reg);
803 * Configure the VHRef timing values. In case the VHREF generator has
804 * been configured in manual mode, this will allow to manually set all
805 * horiz and vert ref values (non-active pixel areas) of the generator
806 * and allows setting the frame reference params.
808 /* horizontal reference start/end */
809 io_write16(sd, REG_HREF_S, href_start & MASK_VHREF);
810 io_write16(sd, REG_HREF_E, href_end & MASK_VHREF);
811 /* vertical reference f1 start/end */
812 io_write16(sd, REG_VREF_F1_S, vref_f1_start & MASK_VHREF);
813 io_write(sd, REG_VREF_F1_WIDTH, vref_f1_width);
814 /* vertical reference f2 start/end */
815 io_write16(sd, REG_VREF_F2_S, vref_f2_start & MASK_VHREF);
816 io_write(sd, REG_VREF_F2_WIDTH, vref_f2_width);
818 /* F1/F2 FREF, field polarity */
819 reg = fieldref_f1_start & MASK_VHREF;
820 reg |= field_polarity << 8;
821 io_write16(sd, REG_FREF_F1_S, reg);
822 reg = fieldref_f2_start & MASK_VHREF;
823 io_write16(sd, REG_FREF_F2_S, reg);
826 /* Configure Video Output port signals */
828 tda1997x_configure_vidout(struct tda1997x_state *state)
830 struct v4l2_subdev *sd = &state->sd;
831 struct tda1997x_platform_data *pdata = &state->pdata;
835 /* Configure pixel clock generator: delay, polarity, rate */
836 reg = (state->vid_fmt == OF_FMT_422_CCIR) ?
837 PCLK_SEL_X2 : PCLK_SEL_X1;
838 reg |= pdata->vidout_delay_pclk << PCLK_DELAY_SHIFT;
839 reg |= pdata->vidout_inv_pclk << PCLK_INV_SHIFT;
840 io_write(sd, REG_PCLK, reg);
842 /* Configure pre-filter */
843 prefilter = 0; /* filters off */
844 /* YUV422 mode requires conversion */
845 if ((state->vid_fmt == OF_FMT_422_SMPT) ||
846 (state->vid_fmt == OF_FMT_422_CCIR)) {
847 /* 2/7 taps for Rv and Bu */
848 prefilter = FILTERS_CTRL_2_7TAP << FILTERS_CTRL_BU_SHIFT |
849 FILTERS_CTRL_2_7TAP << FILTERS_CTRL_RV_SHIFT;
851 io_write(sd, REG_FILTERS_CTRL, prefilter);
853 /* Configure video port */
854 reg = state->vid_fmt & OF_FMT_MASK;
855 if (state->vid_fmt == OF_FMT_422_CCIR)
856 reg |= (OF_BLK | OF_TRC);
858 io_write(sd, REG_OF, reg);
860 /* Configure formatter and conversions */
861 reg = io_read(sd, REG_VDP_CTRL);
862 /* pre-filter is needed unless (REG_FILTERS_CTRL == 0) */
864 reg |= VDP_CTRL_PREFILTER_BP;
866 reg &= ~VDP_CTRL_PREFILTER_BP;
867 /* formatter is needed for YUV422 and for trc/blc codes */
868 if (state->vid_fmt == OF_FMT_444)
869 reg |= VDP_CTRL_FORMATTER_BP;
870 /* formatter and compdel needed for timing/blanking codes */
872 reg &= ~(VDP_CTRL_FORMATTER_BP | VDP_CTRL_COMPDEL_BP);
873 /* activate compdel for small sync delays */
874 if ((pdata->vidout_delay_vs < 4) || (pdata->vidout_delay_hs < 4))
875 reg &= ~VDP_CTRL_COMPDEL_BP;
876 io_write(sd, REG_VDP_CTRL, reg);
878 /* Configure DE output signal: delay, polarity, and source */
879 reg = pdata->vidout_delay_de << DE_FREF_DELAY_SHIFT |
880 pdata->vidout_inv_de << DE_FREF_INV_SHIFT |
881 pdata->vidout_sel_de << DE_FREF_SEL_SHIFT;
882 io_write(sd, REG_DE_FREF, reg);
884 /* Configure HS/HREF output signal: delay, polarity, and source */
885 if (state->vid_fmt != OF_FMT_422_CCIR) {
886 reg = pdata->vidout_delay_hs << HS_HREF_DELAY_SHIFT |
887 pdata->vidout_inv_hs << HS_HREF_INV_SHIFT |
888 pdata->vidout_sel_hs << HS_HREF_SEL_SHIFT;
890 reg = HS_HREF_SEL_NONE << HS_HREF_SEL_SHIFT;
891 io_write(sd, REG_HS_HREF, reg);
893 /* Configure VS/VREF output signal: delay, polarity, and source */
894 if (state->vid_fmt != OF_FMT_422_CCIR) {
895 reg = pdata->vidout_delay_vs << VS_VREF_DELAY_SHIFT |
896 pdata->vidout_inv_vs << VS_VREF_INV_SHIFT |
897 pdata->vidout_sel_vs << VS_VREF_SEL_SHIFT;
899 reg = VS_VREF_SEL_NONE << VS_VREF_SEL_SHIFT;
900 io_write(sd, REG_VS_VREF, reg);
905 /* Configure Audio output port signals */
907 tda1997x_configure_audout(struct v4l2_subdev *sd, u8 channel_assignment)
909 struct tda1997x_state *state = to_state(sd);
910 struct tda1997x_platform_data *pdata = &state->pdata;
911 bool sp_used_by_fifo = true;
914 if (!pdata->audout_format)
917 /* channel assignment (CEA-861-D Table 20) */
918 io_write(sd, REG_AUDIO_PATH, channel_assignment);
920 /* Audio output configuration */
922 switch (pdata->audout_format) {
923 case AUDFMT_TYPE_I2S:
924 reg |= AUDCFG_BUS_I2S << AUDCFG_BUS_SHIFT;
926 case AUDFMT_TYPE_SPDIF:
927 reg |= AUDCFG_BUS_SPDIF << AUDCFG_BUS_SHIFT;
930 switch (state->audio_type) {
931 case AUDCFG_TYPE_PCM:
932 reg |= AUDCFG_TYPE_PCM << AUDCFG_TYPE_SHIFT;
934 case AUDCFG_TYPE_OBA:
935 reg |= AUDCFG_TYPE_OBA << AUDCFG_TYPE_SHIFT;
937 case AUDCFG_TYPE_DST:
938 reg |= AUDCFG_TYPE_DST << AUDCFG_TYPE_SHIFT;
939 sp_used_by_fifo = false;
941 case AUDCFG_TYPE_HBR:
942 reg |= AUDCFG_TYPE_HBR << AUDCFG_TYPE_SHIFT;
943 if (pdata->audout_layout == 1) {
944 /* demuxed via AP0:AP3 */
945 reg |= AUDCFG_HBR_DEMUX << AUDCFG_HBR_SHIFT;
946 if (pdata->audout_format == AUDFMT_TYPE_SPDIF)
947 sp_used_by_fifo = false;
949 /* straight via AP0 */
950 reg |= AUDCFG_HBR_STRAIGHT << AUDCFG_HBR_SHIFT;
954 if (pdata->audout_width == 32)
955 reg |= AUDCFG_I2SW_32 << AUDCFG_I2SW_SHIFT;
957 reg |= AUDCFG_I2SW_16 << AUDCFG_I2SW_SHIFT;
959 /* automatic hardware mute */
960 if (pdata->audio_auto_mute)
961 reg |= AUDCFG_AUTO_MUTE_EN;
963 if (pdata->audout_invert_clk)
964 reg |= AUDCFG_CLK_INVERT;
965 io_write(sd, REG_AUDCFG, reg);
968 reg = (pdata->audout_layout) ? AUDIO_LAYOUT_LAYOUT1 : 0;
969 if (!pdata->audout_layoutauto)
970 reg |= AUDIO_LAYOUT_MANUAL;
972 reg |= AUDIO_LAYOUT_SP_FLAG;
973 io_write(sd, REG_AUDIO_LAYOUT, reg);
975 /* FIFO Latency value */
976 io_write(sd, REG_FIFO_LATENCY_VAL, 0x80);
978 /* Audio output port config */
979 if (sp_used_by_fifo) {
980 reg = AUDIO_OUT_ENABLE_AP0;
981 if (channel_assignment >= 0x01)
982 reg |= AUDIO_OUT_ENABLE_AP1;
983 if (channel_assignment >= 0x04)
984 reg |= AUDIO_OUT_ENABLE_AP2;
985 if (channel_assignment >= 0x0c)
986 reg |= AUDIO_OUT_ENABLE_AP3;
987 /* specific cases where AP1 is not used */
988 if ((channel_assignment == 0x04)
989 || (channel_assignment == 0x08)
990 || (channel_assignment == 0x0c)
991 || (channel_assignment == 0x10)
992 || (channel_assignment == 0x14)
993 || (channel_assignment == 0x18)
994 || (channel_assignment == 0x1c))
995 reg &= ~AUDIO_OUT_ENABLE_AP1;
996 /* specific cases where AP2 is not used */
997 if ((channel_assignment >= 0x14)
998 && (channel_assignment <= 0x17))
999 reg &= ~AUDIO_OUT_ENABLE_AP2;
1001 reg = AUDIO_OUT_ENABLE_AP3 |
1002 AUDIO_OUT_ENABLE_AP2 |
1003 AUDIO_OUT_ENABLE_AP1 |
1004 AUDIO_OUT_ENABLE_AP0;
1006 if (pdata->audout_format == AUDFMT_TYPE_I2S)
1007 reg |= (AUDIO_OUT_ENABLE_ACLK | AUDIO_OUT_ENABLE_WS);
1008 io_write(sd, REG_AUDIO_OUT_ENABLE, reg);
1010 /* reset test mode to normal audio freq auto selection */
1011 io_write(sd, REG_TEST_MODE, 0x00);
1016 /* Soft Reset of specific hdmi info */
1018 tda1997x_hdmi_info_reset(struct v4l2_subdev *sd, u8 info_rst, bool reset_sus)
1022 /* reset infoframe engine packets */
1023 reg = io_read(sd, REG_HDMI_INFO_RST);
1024 io_write(sd, REG_HDMI_INFO_RST, info_rst);
1026 /* if infoframe engine has been reset clear INT_FLG_MODE */
1027 if (reg & RESET_IF) {
1028 reg = io_read(sd, REG_INT_FLG_CLR_MODE);
1029 io_write(sd, REG_INT_FLG_CLR_MODE, reg);
1032 /* Disable REFTIM to restart start-up-sequencer (SUS) */
1033 reg = io_read(sd, REG_RATE_CTRL);
1034 reg &= ~RATE_REFTIM_ENABLE;
1036 reg |= RATE_REFTIM_ENABLE;
1037 reg = io_write(sd, REG_RATE_CTRL, reg);
1043 tda1997x_power_mode(struct tda1997x_state *state, bool enable)
1045 struct v4l2_subdev *sd = &state->sd;
1049 /* Automatic control of TMDS */
1050 io_write(sd, REG_PON_OVR_EN, PON_DIS);
1051 /* Enable current bias unit */
1052 io_write(sd, REG_CFG1, PON_EN);
1053 /* Enable deep color PLL */
1054 io_write(sd, REG_DEEP_PLL7_BYP, PON_DIS);
1055 /* Output buffers active */
1056 reg = io_read(sd, REG_OF);
1057 reg &= ~OF_VP_ENABLE;
1058 io_write(sd, REG_OF, reg);
1060 /* Power down EDID mode sequence */
1061 /* Output buffers in HiZ */
1062 reg = io_read(sd, REG_OF);
1063 reg |= OF_VP_ENABLE;
1064 io_write(sd, REG_OF, reg);
1065 /* Disable deep color PLL */
1066 io_write(sd, REG_DEEP_PLL7_BYP, PON_EN);
1067 /* Disable current bias unit */
1068 io_write(sd, REG_CFG1, PON_DIS);
1069 /* Manual control of TMDS */
1070 io_write(sd, REG_PON_OVR_EN, PON_EN);
1075 tda1997x_detect_tx_5v(struct v4l2_subdev *sd)
1077 u8 reg = io_read(sd, REG_DETECT_5V);
1079 return ((reg & DETECT_5V_SEL) ? 1 : 0);
1083 tda1997x_detect_tx_hpd(struct v4l2_subdev *sd)
1085 u8 reg = io_read(sd, REG_DETECT_5V);
1087 return ((reg & DETECT_HPD) ? 1 : 0);
1091 tda1997x_detect_std(struct tda1997x_state *state,
1092 struct v4l2_dv_timings *timings)
1094 struct v4l2_subdev *sd = &state->sd;
1097 * Read the FMT registers
1098 * REG_V_PER: Period of a frame (or field) in MCLK (27MHz) cycles
1099 * REG_H_PER: Period of a line in MCLK (27MHz) cycles
1100 * REG_HS_WIDTH: Period of horiz sync pulse in MCLK (27MHz) cycles
1102 u32 vper, vsync_pos;
1103 u16 hper, hsync_pos, hsper, interlaced;
1104 u16 htot, hact, hfront, hsync, hback;
1105 u16 vtot, vact, vfront1, vfront2, vsync, vback1, vback2;
1107 if (!state->input_detect[0] && !state->input_detect[1])
1110 vper = io_read24(sd, REG_V_PER);
1111 hper = io_read16(sd, REG_H_PER);
1112 hsper = io_read16(sd, REG_HS_WIDTH);
1113 vsync_pos = vper & MASK_VPER_SYNC_POS;
1114 hsync_pos = hper & MASK_HPER_SYNC_POS;
1115 interlaced = hsper & MASK_HSWIDTH_INTERLACED;
1118 hsper &= MASK_HSWIDTH;
1119 v4l2_dbg(1, debug, sd, "Signal Timings: %u/%u/%u\n", vper, hper, hsper);
1121 htot = io_read16(sd, REG_FMT_H_TOT);
1122 hact = io_read16(sd, REG_FMT_H_ACT);
1123 hfront = io_read16(sd, REG_FMT_H_FRONT);
1124 hsync = io_read16(sd, REG_FMT_H_SYNC);
1125 hback = io_read16(sd, REG_FMT_H_BACK);
1127 vtot = io_read16(sd, REG_FMT_V_TOT);
1128 vact = io_read16(sd, REG_FMT_V_ACT);
1129 vfront1 = io_read(sd, REG_FMT_V_FRONT_F1);
1130 vfront2 = io_read(sd, REG_FMT_V_FRONT_F2);
1131 vsync = io_read(sd, REG_FMT_V_SYNC);
1132 vback1 = io_read(sd, REG_FMT_V_BACK_F1);
1133 vback2 = io_read(sd, REG_FMT_V_BACK_F2);
1135 v4l2_dbg(1, debug, sd, "Geometry: H %u %u %u %u %u Sync%c V %u %u %u %u %u %u %u Sync%c\n",
1136 htot, hact, hfront, hsync, hback, hsync_pos ? '+' : '-',
1137 vtot, vact, vfront1, vfront2, vsync, vback1, vback2, vsync_pos ? '+' : '-');
1142 timings->type = V4L2_DV_BT_656_1120;
1143 timings->bt.width = hact;
1144 timings->bt.hfrontporch = hfront;
1145 timings->bt.hsync = hsync;
1146 timings->bt.hbackporch = hback;
1147 timings->bt.height = vact;
1148 timings->bt.vfrontporch = vfront1;
1149 timings->bt.vsync = vsync;
1150 timings->bt.vbackporch = vback1;
1151 timings->bt.interlaced = interlaced ? V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1152 timings->bt.polarities = vsync_pos ? V4L2_DV_VSYNC_POS_POL : 0;
1153 timings->bt.polarities |= hsync_pos ? V4L2_DV_HSYNC_POS_POL : 0;
1155 timings->bt.pixelclock = (u64)htot * vtot * 27000000;
1157 timings->bt.il_vfrontporch = vfront2;
1158 timings->bt.il_vsync = timings->bt.vsync;
1159 timings->bt.il_vbackporch = vback2;
1160 do_div(timings->bt.pixelclock, vper * 2 /* full frame */);
1162 timings->bt.il_vfrontporch = 0;
1163 timings->bt.il_vsync = 0;
1164 timings->bt.il_vbackporch = 0;
1165 do_div(timings->bt.pixelclock, vper);
1167 v4l2_find_dv_timings_cap(timings, &tda1997x_dv_timings_cap,
1168 (u32)timings->bt.pixelclock / 500, NULL, NULL);
1169 v4l2_print_dv_timings(sd->name, "Detected format: ", timings, false);
1173 /* some sort of errata workaround for chip revision 0 (N1) */
1174 static void tda1997x_reset_n1(struct tda1997x_state *state)
1176 struct v4l2_subdev *sd = &state->sd;
1179 /* clear HDMI mode flag in BCAPS */
1180 io_write(sd, REG_CLK_CFG, CLK_CFG_SEL_ACLK_EN | CLK_CFG_SEL_ACLK);
1181 io_write(sd, REG_PON_OVR_EN, PON_EN);
1182 io_write(sd, REG_PON_CBIAS, PON_EN);
1183 io_write(sd, REG_PON_PLL, PON_EN);
1185 reg = io_read(sd, REG_MODE_REC_CFG1);
1188 io_write(sd, REG_MODE_REC_CFG1, reg);
1189 io_write(sd, REG_CLK_CFG, CLK_CFG_DIS);
1190 io_write(sd, REG_PON_OVR_EN, PON_DIS);
1191 reg = io_read(sd, REG_MODE_REC_CFG1);
1193 io_write(sd, REG_MODE_REC_CFG1, reg);
1197 * Activity detection must only be notified when stable_clk_x AND active_x
1198 * bits are set to 1. If only stable_clk_x bit is set to 1 but not
1199 * active_x, it means that the TMDS clock is not in the defined range
1200 * and activity detection must not be notified.
1203 tda1997x_read_activity_status_regs(struct v4l2_subdev *sd)
1207 /* Read CLK_A_STATUS register */
1208 reg = io_read(sd, REG_CLK_A_STATUS);
1209 /* ignore if not active */
1210 if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
1211 reg &= ~MASK_CLK_STABLE;
1212 status |= ((reg & MASK_CLK_STABLE) >> 2);
1214 /* Read CLK_B_STATUS register */
1215 reg = io_read(sd, REG_CLK_B_STATUS);
1216 /* ignore if not active */
1217 if ((reg & MASK_CLK_STABLE) && !(reg & MASK_CLK_ACTIVE))
1218 reg &= ~MASK_CLK_STABLE;
1219 status |= ((reg & MASK_CLK_STABLE) >> 1);
1221 /* Read the SUS_STATUS register */
1222 reg = io_read(sd, REG_SUS_STATUS);
1224 /* If state = 5 => TMDS is locked */
1225 if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED)
1226 status |= MASK_SUS_STATE;
1228 status &= ~MASK_SUS_STATE;
1234 set_rgb_quantization_range(struct tda1997x_state *state)
1236 struct v4l2_hdmi_colorimetry *c = &state->colorimetry;
1238 state->colorimetry = v4l2_hdmi_rx_colorimetry(&state->avi_infoframe,
1240 state->timings.bt.height);
1241 /* If ycbcr_enc is V4L2_YCBCR_ENC_DEFAULT, we receive RGB */
1242 if (c->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
1243 switch (state->rgb_quantization_range) {
1244 case V4L2_DV_RGB_RANGE_LIMITED:
1245 c->quantization = V4L2_QUANTIZATION_FULL_RANGE;
1247 case V4L2_DV_RGB_RANGE_FULL:
1248 c->quantization = V4L2_QUANTIZATION_LIM_RANGE;
1252 v4l_dbg(1, debug, state->client,
1253 "colorspace=%d/%d colorimetry=%d range=%s content=%d\n",
1254 state->avi_infoframe.colorspace, c->colorspace,
1255 state->avi_infoframe.colorimetry,
1256 v4l2_quantization_names[c->quantization],
1257 state->avi_infoframe.content_type);
1260 /* parse an infoframe and do some sanity checks on it */
1262 tda1997x_parse_infoframe(struct tda1997x_state *state, u16 addr)
1264 struct v4l2_subdev *sd = &state->sd;
1265 union hdmi_infoframe frame;
1266 u8 buffer[40] = { 0 };
1271 len = io_readn(sd, addr, sizeof(buffer), buffer);
1272 err = hdmi_infoframe_unpack(&frame, buffer, len);
1274 v4l_err(state->client,
1275 "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
1276 len, addr, buffer[0]);
1279 hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
1280 switch (frame.any.type) {
1281 /* Audio InfoFrame: see HDMI spec 8.2.2 */
1282 case HDMI_INFOFRAME_TYPE_AUDIO:
1284 switch (frame.audio.sample_frequency) {
1285 case HDMI_AUDIO_SAMPLE_FREQUENCY_32000:
1286 state->audio_samplerate = 32000;
1288 case HDMI_AUDIO_SAMPLE_FREQUENCY_44100:
1289 state->audio_samplerate = 44100;
1291 case HDMI_AUDIO_SAMPLE_FREQUENCY_48000:
1292 state->audio_samplerate = 48000;
1294 case HDMI_AUDIO_SAMPLE_FREQUENCY_88200:
1295 state->audio_samplerate = 88200;
1297 case HDMI_AUDIO_SAMPLE_FREQUENCY_96000:
1298 state->audio_samplerate = 96000;
1300 case HDMI_AUDIO_SAMPLE_FREQUENCY_176400:
1301 state->audio_samplerate = 176400;
1303 case HDMI_AUDIO_SAMPLE_FREQUENCY_192000:
1304 state->audio_samplerate = 192000;
1307 case HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM:
1312 switch (frame.audio.sample_size) {
1313 case HDMI_AUDIO_SAMPLE_SIZE_16:
1314 state->audio_samplesize = 16;
1316 case HDMI_AUDIO_SAMPLE_SIZE_20:
1317 state->audio_samplesize = 20;
1319 case HDMI_AUDIO_SAMPLE_SIZE_24:
1320 state->audio_samplesize = 24;
1322 case HDMI_AUDIO_SAMPLE_SIZE_STREAM:
1328 state->audio_channels = frame.audio.channels;
1329 if (frame.audio.channel_allocation &&
1330 frame.audio.channel_allocation != state->audio_ch_alloc) {
1331 /* use the channel assignment from the infoframe */
1332 state->audio_ch_alloc = frame.audio.channel_allocation;
1333 tda1997x_configure_audout(sd, state->audio_ch_alloc);
1334 /* reset the audio FIFO */
1335 tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
1339 /* Auxiliary Video information (AVI) InfoFrame: see HDMI spec 8.2.1 */
1340 case HDMI_INFOFRAME_TYPE_AVI:
1341 state->avi_infoframe = frame.avi;
1342 set_rgb_quantization_range(state);
1344 /* configure upsampler: 0=bypass 1=repeatchroma 2=interpolate */
1345 reg = io_read(sd, REG_PIX_REPEAT);
1346 reg &= ~PIX_REPEAT_MASK_UP_SEL;
1347 if (frame.avi.colorspace == HDMI_COLORSPACE_YUV422)
1348 reg |= (PIX_REPEAT_CHROMA << PIX_REPEAT_SHIFT);
1349 io_write(sd, REG_PIX_REPEAT, reg);
1351 /* ConfigurePixelRepeater: repeat n-times each pixel */
1352 reg = io_read(sd, REG_PIX_REPEAT);
1353 reg &= ~PIX_REPEAT_MASK_REP;
1354 reg |= frame.avi.pixel_repeat;
1355 io_write(sd, REG_PIX_REPEAT, reg);
1357 /* configure the receiver with the new colorspace */
1358 tda1997x_configure_csc(sd);
1366 static void tda1997x_irq_sus(struct tda1997x_state *state, u8 *flags)
1368 struct v4l2_subdev *sd = &state->sd;
1371 source = io_read(sd, REG_INT_FLG_CLR_SUS);
1372 io_write(sd, REG_INT_FLG_CLR_SUS, source);
1374 if (source & MASK_MPT) {
1375 /* reset MTP in use flag if set */
1376 if (state->mptrw_in_progress)
1377 state->mptrw_in_progress = 0;
1380 if (source & MASK_SUS_END) {
1381 /* reset audio FIFO */
1382 reg = io_read(sd, REG_HDMI_INFO_RST);
1383 reg |= MASK_SR_FIFO_FIFO_CTRL;
1384 io_write(sd, REG_HDMI_INFO_RST, reg);
1385 reg &= ~MASK_SR_FIFO_FIFO_CTRL;
1386 io_write(sd, REG_HDMI_INFO_RST, reg);
1388 /* reset HDMI flags */
1389 state->hdmi_status = 0;
1392 /* filter FMT interrupt based on SUS state */
1393 reg = io_read(sd, REG_SUS_STATUS);
1394 if (((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED)
1395 || (source & MASK_MPT)) {
1396 source &= ~MASK_FMT;
1399 if (source & (MASK_FMT | MASK_SUS_END)) {
1400 reg = io_read(sd, REG_SUS_STATUS);
1401 if ((reg & MASK_SUS_STATUS) != LAST_STATE_REACHED) {
1402 v4l_err(state->client, "BAD SUS STATUS\n");
1406 tda1997x_detect_std(state, NULL);
1407 /* notify user of change in resolution */
1408 v4l2_subdev_notify_event(&state->sd, &tda1997x_ev_fmt);
1412 static void tda1997x_irq_ddc(struct tda1997x_state *state, u8 *flags)
1414 struct v4l2_subdev *sd = &state->sd;
1417 source = io_read(sd, REG_INT_FLG_CLR_DDC);
1418 io_write(sd, REG_INT_FLG_CLR_DDC, source);
1419 if (source & MASK_EDID_MTP) {
1420 /* reset MTP in use flag if set */
1421 if (state->mptrw_in_progress)
1422 state->mptrw_in_progress = 0;
1425 /* Detection of +5V */
1426 if (source & MASK_DET_5V) {
1427 v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
1428 tda1997x_detect_tx_5v(sd));
1432 static void tda1997x_irq_rate(struct tda1997x_state *state, u8 *flags)
1434 struct v4l2_subdev *sd = &state->sd;
1439 source = io_read(sd, REG_INT_FLG_CLR_RATE);
1440 io_write(sd, REG_INT_FLG_CLR_RATE, source);
1442 /* read status regs */
1443 irq_status = tda1997x_read_activity_status_regs(sd);
1446 * read clock status reg until INT_FLG_CLR_RATE is still 0
1447 * after the read to make sure its the last one
1451 irq_status = tda1997x_read_activity_status_regs(sd);
1452 reg = io_read(sd, REG_INT_FLG_CLR_RATE);
1453 io_write(sd, REG_INT_FLG_CLR_RATE, reg);
1457 /* we only pay attention to stability change events */
1458 if (source & (MASK_RATE_A_ST | MASK_RATE_B_ST)) {
1459 int input = (source & MASK_RATE_A_ST)?0:1;
1463 if ((irq_status & mask) != (state->activity_status & mask)) {
1465 if ((irq_status & mask) == 0) {
1466 v4l_info(state->client,
1467 "HDMI-%c: Digital Activity Lost\n",
1470 /* bypass up/down sampler and pixel repeater */
1471 reg = io_read(sd, REG_PIX_REPEAT);
1472 reg &= ~PIX_REPEAT_MASK_UP_SEL;
1473 reg &= ~PIX_REPEAT_MASK_REP;
1474 io_write(sd, REG_PIX_REPEAT, reg);
1476 if (state->chip_revision == 0)
1477 tda1997x_reset_n1(state);
1479 state->input_detect[input] = 0;
1480 v4l2_subdev_notify_event(sd, &tda1997x_ev_fmt);
1483 /* activity detected */
1485 v4l_info(state->client,
1486 "HDMI-%c: Digital Activity Detected\n",
1488 state->input_detect[input] = 1;
1491 /* hold onto current state */
1492 state->activity_status = (irq_status & mask);
1497 static void tda1997x_irq_info(struct tda1997x_state *state, u8 *flags)
1499 struct v4l2_subdev *sd = &state->sd;
1502 source = io_read(sd, REG_INT_FLG_CLR_INFO);
1503 io_write(sd, REG_INT_FLG_CLR_INFO, source);
1505 /* Audio infoframe */
1506 if (source & MASK_AUD_IF) {
1507 tda1997x_parse_infoframe(state, AUD_IF);
1508 source &= ~MASK_AUD_IF;
1511 /* Source Product Descriptor infoframe change */
1512 if (source & MASK_SPD_IF) {
1513 tda1997x_parse_infoframe(state, SPD_IF);
1514 source &= ~MASK_SPD_IF;
1517 /* Auxiliary Video Information infoframe */
1518 if (source & MASK_AVI_IF) {
1519 tda1997x_parse_infoframe(state, AVI_IF);
1520 source &= ~MASK_AVI_IF;
1524 static void tda1997x_irq_audio(struct tda1997x_state *state, u8 *flags)
1526 struct v4l2_subdev *sd = &state->sd;
1529 source = io_read(sd, REG_INT_FLG_CLR_AUDIO);
1530 io_write(sd, REG_INT_FLG_CLR_AUDIO, source);
1532 /* reset audio FIFO on FIFO pointer error or audio mute */
1533 if (source & MASK_ERROR_FIFO_PT ||
1534 source & MASK_MUTE_FLG) {
1535 /* audio reset audio FIFO */
1536 reg = io_read(sd, REG_SUS_STATUS);
1537 if ((reg & MASK_SUS_STATUS) == LAST_STATE_REACHED) {
1538 reg = io_read(sd, REG_HDMI_INFO_RST);
1539 reg |= MASK_SR_FIFO_FIFO_CTRL;
1540 io_write(sd, REG_HDMI_INFO_RST, reg);
1541 reg &= ~MASK_SR_FIFO_FIFO_CTRL;
1542 io_write(sd, REG_HDMI_INFO_RST, reg);
1543 /* reset channel status IT if present */
1544 source &= ~(MASK_CH_STATE);
1547 if (source & MASK_AUDIO_FREQ_FLG) {
1548 static const int freq[] = {
1549 0, 32000, 44100, 48000, 88200, 96000, 176400, 192000
1552 reg = io_read(sd, REG_AUDIO_FREQ);
1553 state->audio_samplerate = freq[reg & 7];
1554 v4l_info(state->client, "Audio Frequency Change: %dHz\n",
1555 state->audio_samplerate);
1557 if (source & MASK_AUDIO_FLG) {
1558 reg = io_read(sd, REG_AUDIO_FLAGS);
1559 if (reg & BIT(AUDCFG_TYPE_DST))
1560 state->audio_type = AUDCFG_TYPE_DST;
1561 if (reg & BIT(AUDCFG_TYPE_OBA))
1562 state->audio_type = AUDCFG_TYPE_OBA;
1563 if (reg & BIT(AUDCFG_TYPE_HBR))
1564 state->audio_type = AUDCFG_TYPE_HBR;
1565 if (reg & BIT(AUDCFG_TYPE_PCM))
1566 state->audio_type = AUDCFG_TYPE_PCM;
1567 v4l_info(state->client, "Audio Type: %s\n",
1568 audtype_names[state->audio_type]);
1572 static void tda1997x_irq_hdcp(struct tda1997x_state *state, u8 *flags)
1574 struct v4l2_subdev *sd = &state->sd;
1577 source = io_read(sd, REG_INT_FLG_CLR_HDCP);
1578 io_write(sd, REG_INT_FLG_CLR_HDCP, source);
1580 /* reset MTP in use flag if set */
1581 if (source & MASK_HDCP_MTP)
1582 state->mptrw_in_progress = 0;
1583 if (source & MASK_STATE_C5) {
1584 /* REPEATER: mask AUDIO and IF irqs to avoid IF during auth */
1585 reg = io_read(sd, REG_INT_MASK_TOP);
1586 reg &= ~(INTERRUPT_AUDIO | INTERRUPT_INFO);
1587 io_write(sd, REG_INT_MASK_TOP, reg);
1588 *flags &= (INTERRUPT_AUDIO | INTERRUPT_INFO);
1592 static irqreturn_t tda1997x_isr_thread(int irq, void *d)
1594 struct tda1997x_state *state = d;
1595 struct v4l2_subdev *sd = &state->sd;
1598 mutex_lock(&state->lock);
1600 /* read interrupt flags */
1601 flags = io_read(sd, REG_INT_FLG_CLR_TOP);
1605 /* SUS interrupt source (Input activity events) */
1606 if (flags & INTERRUPT_SUS)
1607 tda1997x_irq_sus(state, &flags);
1608 /* DDC interrupt source (Display Data Channel) */
1609 else if (flags & INTERRUPT_DDC)
1610 tda1997x_irq_ddc(state, &flags);
1611 /* RATE interrupt source (Digital Input activity) */
1612 else if (flags & INTERRUPT_RATE)
1613 tda1997x_irq_rate(state, &flags);
1614 /* Infoframe change interrupt */
1615 else if (flags & INTERRUPT_INFO)
1616 tda1997x_irq_info(state, &flags);
1617 /* Audio interrupt source:
1618 * freq change, DST,OBA,HBR,ASP flags, mute, FIFO err
1620 else if (flags & INTERRUPT_AUDIO)
1621 tda1997x_irq_audio(state, &flags);
1622 /* HDCP interrupt source (content protection) */
1623 if (flags & INTERRUPT_HDCP)
1624 tda1997x_irq_hdcp(state, &flags);
1625 } while (flags != 0);
1626 mutex_unlock(&state->lock);
1631 /* -----------------------------------------------------------------------------
1632 * v4l2_subdev_video_ops
1636 tda1997x_g_input_status(struct v4l2_subdev *sd, u32 *status)
1638 struct tda1997x_state *state = to_state(sd);
1643 mutex_lock(&state->lock);
1644 vper = io_read24(sd, REG_V_PER) & MASK_VPER;
1645 hper = io_read16(sd, REG_H_PER) & MASK_HPER;
1646 hsper = io_read16(sd, REG_HS_WIDTH) & MASK_HSWIDTH;
1648 * The tda1997x supports A/B inputs but only a single output.
1649 * The irq handler monitors for timing changes on both inputs and
1650 * sets the input_detect array to 0|1 depending on signal presence.
1651 * I believe selection of A vs B is automatic.
1653 * The vper/hper/hsper registers provide the frame period, line period
1654 * and horiz sync period (units of MCLK clock cycles (27MHz)) and
1655 * testing shows these values to be random if no signal is present
1658 v4l2_dbg(1, debug, sd, "inputs:%d/%d timings:%d/%d/%d\n",
1659 state->input_detect[0], state->input_detect[1],
1661 if (!state->input_detect[0] && !state->input_detect[1])
1662 *status = V4L2_IN_ST_NO_SIGNAL;
1663 else if (!vper || !hper || !hsper)
1664 *status = V4L2_IN_ST_NO_SYNC;
1667 mutex_unlock(&state->lock);
1672 static int tda1997x_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1673 struct v4l2_dv_timings *timings)
1675 struct tda1997x_state *state = to_state(sd);
1677 v4l_dbg(1, debug, state->client, "%s\n", __func__);
1679 if (v4l2_match_dv_timings(&state->timings, timings, 0, false))
1680 return 0; /* no changes */
1682 if (!v4l2_valid_dv_timings(timings, &tda1997x_dv_timings_cap,
1686 mutex_lock(&state->lock);
1687 state->timings = *timings;
1688 /* setup frame detection window and VHREF timing generator */
1689 tda1997x_configure_vhref(sd);
1690 /* configure colorspace conversion */
1691 tda1997x_configure_csc(sd);
1692 mutex_unlock(&state->lock);
1697 static int tda1997x_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1698 struct v4l2_dv_timings *timings)
1700 struct tda1997x_state *state = to_state(sd);
1702 v4l_dbg(1, debug, state->client, "%s\n", __func__);
1703 mutex_lock(&state->lock);
1704 *timings = state->timings;
1705 mutex_unlock(&state->lock);
1710 static int tda1997x_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1711 struct v4l2_dv_timings *timings)
1713 struct tda1997x_state *state = to_state(sd);
1716 v4l_dbg(1, debug, state->client, "%s\n", __func__);
1717 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1718 mutex_lock(&state->lock);
1719 ret = tda1997x_detect_std(state, timings);
1720 mutex_unlock(&state->lock);
1725 static const struct v4l2_subdev_video_ops tda1997x_video_ops = {
1726 .g_input_status = tda1997x_g_input_status,
1730 /* -----------------------------------------------------------------------------
1731 * v4l2_subdev_pad_ops
1734 static int tda1997x_init_state(struct v4l2_subdev *sd,
1735 struct v4l2_subdev_state *sd_state)
1737 struct tda1997x_state *state = to_state(sd);
1738 struct v4l2_mbus_framefmt *mf;
1740 mf = v4l2_subdev_state_get_format(sd_state, 0);
1741 mf->code = state->mbus_codes[0];
1746 static int tda1997x_enum_mbus_code(struct v4l2_subdev *sd,
1747 struct v4l2_subdev_state *sd_state,
1748 struct v4l2_subdev_mbus_code_enum *code)
1750 struct tda1997x_state *state = to_state(sd);
1752 v4l_dbg(1, debug, state->client, "%s %d\n", __func__, code->index);
1753 if (code->index >= ARRAY_SIZE(state->mbus_codes))
1756 if (!state->mbus_codes[code->index])
1759 code->code = state->mbus_codes[code->index];
1764 static void tda1997x_fill_format(struct tda1997x_state *state,
1765 struct v4l2_mbus_framefmt *format)
1767 const struct v4l2_bt_timings *bt;
1769 memset(format, 0, sizeof(*format));
1770 bt = &state->timings.bt;
1771 format->width = bt->width;
1772 format->height = bt->height;
1773 format->colorspace = state->colorimetry.colorspace;
1774 format->field = (bt->interlaced) ?
1775 V4L2_FIELD_SEQ_TB : V4L2_FIELD_NONE;
1778 static int tda1997x_get_format(struct v4l2_subdev *sd,
1779 struct v4l2_subdev_state *sd_state,
1780 struct v4l2_subdev_format *format)
1782 struct tda1997x_state *state = to_state(sd);
1784 v4l_dbg(1, debug, state->client, "%s pad=%d which=%d\n",
1785 __func__, format->pad, format->which);
1787 tda1997x_fill_format(state, &format->format);
1789 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1790 struct v4l2_mbus_framefmt *fmt;
1792 fmt = v4l2_subdev_state_get_format(sd_state, format->pad);
1793 format->format.code = fmt->code;
1795 format->format.code = state->mbus_code;
1800 static int tda1997x_set_format(struct v4l2_subdev *sd,
1801 struct v4l2_subdev_state *sd_state,
1802 struct v4l2_subdev_format *format)
1804 struct tda1997x_state *state = to_state(sd);
1808 v4l_dbg(1, debug, state->client, "%s pad=%d which=%d fmt=0x%x\n",
1809 __func__, format->pad, format->which, format->format.code);
1811 for (i = 0; i < ARRAY_SIZE(state->mbus_codes); i++) {
1812 if (format->format.code == state->mbus_codes[i]) {
1813 code = state->mbus_codes[i];
1818 code = state->mbus_codes[0];
1820 tda1997x_fill_format(state, &format->format);
1821 format->format.code = code;
1823 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1824 struct v4l2_mbus_framefmt *fmt;
1826 fmt = v4l2_subdev_state_get_format(sd_state, format->pad);
1827 *fmt = format->format;
1829 int ret = tda1997x_setup_format(state, format->format.code);
1833 /* mbus_code has changed - re-configure csc/vidout */
1834 tda1997x_configure_csc(sd);
1835 tda1997x_configure_vidout(state);
1841 static int tda1997x_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1843 struct tda1997x_state *state = to_state(sd);
1845 v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
1846 memset(edid->reserved, 0, sizeof(edid->reserved));
1848 if (edid->start_block == 0 && edid->blocks == 0) {
1849 edid->blocks = state->edid.blocks;
1853 if (!state->edid.present)
1856 if (edid->start_block >= state->edid.blocks)
1859 if (edid->start_block + edid->blocks > state->edid.blocks)
1860 edid->blocks = state->edid.blocks - edid->start_block;
1862 memcpy(edid->edid, state->edid.edid + edid->start_block * 128,
1863 edid->blocks * 128);
1868 static int tda1997x_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
1870 struct tda1997x_state *state = to_state(sd);
1873 v4l_dbg(1, debug, state->client, "%s pad=%d\n", __func__, edid->pad);
1874 memset(edid->reserved, 0, sizeof(edid->reserved));
1876 if (edid->start_block != 0)
1879 if (edid->blocks == 0) {
1880 state->edid.blocks = 0;
1881 state->edid.present = 0;
1882 tda1997x_disable_edid(sd);
1886 if (edid->blocks > 2) {
1891 tda1997x_disable_edid(sd);
1893 /* write base EDID */
1894 for (i = 0; i < 128; i++)
1895 io_write(sd, REG_EDID_IN_BYTE0 + i, edid->edid[i]);
1897 /* write CEA Extension */
1898 for (i = 0; i < 128; i++)
1899 io_write(sd, REG_EDID_IN_BYTE128 + i, edid->edid[i+128]);
1902 memcpy(state->edid.edid, edid->edid, 256);
1903 state->edid.blocks = edid->blocks;
1905 tda1997x_enable_edid(sd);
1910 static int tda1997x_get_dv_timings_cap(struct v4l2_subdev *sd,
1911 struct v4l2_dv_timings_cap *cap)
1913 *cap = tda1997x_dv_timings_cap;
1917 static int tda1997x_enum_dv_timings(struct v4l2_subdev *sd,
1918 struct v4l2_enum_dv_timings *timings)
1920 return v4l2_enum_dv_timings_cap(timings, &tda1997x_dv_timings_cap,
1924 static const struct v4l2_subdev_pad_ops tda1997x_pad_ops = {
1925 .enum_mbus_code = tda1997x_enum_mbus_code,
1926 .get_fmt = tda1997x_get_format,
1927 .set_fmt = tda1997x_set_format,
1928 .get_edid = tda1997x_get_edid,
1929 .set_edid = tda1997x_set_edid,
1930 .s_dv_timings = tda1997x_s_dv_timings,
1931 .g_dv_timings = tda1997x_g_dv_timings,
1932 .query_dv_timings = tda1997x_query_dv_timings,
1933 .dv_timings_cap = tda1997x_get_dv_timings_cap,
1934 .enum_dv_timings = tda1997x_enum_dv_timings,
1937 /* -----------------------------------------------------------------------------
1938 * v4l2_subdev_core_ops
1941 static int tda1997x_log_infoframe(struct v4l2_subdev *sd, int addr)
1943 struct tda1997x_state *state = to_state(sd);
1944 union hdmi_infoframe frame;
1945 u8 buffer[40] = { 0 };
1949 len = io_readn(sd, addr, sizeof(buffer), buffer);
1950 v4l2_dbg(1, debug, sd, "infoframe: addr=%d len=%d\n", addr, len);
1951 err = hdmi_infoframe_unpack(&frame, buffer, len);
1953 v4l_err(state->client,
1954 "failed parsing %d byte infoframe: 0x%04x/0x%02x\n",
1955 len, addr, buffer[0]);
1958 hdmi_infoframe_log(KERN_INFO, &state->client->dev, &frame);
1963 static int tda1997x_log_status(struct v4l2_subdev *sd)
1965 struct tda1997x_state *state = to_state(sd);
1966 struct v4l2_dv_timings timings;
1967 struct hdmi_avi_infoframe *avi = &state->avi_infoframe;
1969 v4l2_info(sd, "-----Chip status-----\n");
1970 v4l2_info(sd, "Chip: %s N%d\n", state->info->name,
1971 state->chip_revision + 1);
1972 v4l2_info(sd, "EDID Enabled: %s\n", state->edid.present ? "yes" : "no");
1974 v4l2_info(sd, "-----Signal status-----\n");
1975 v4l2_info(sd, "Cable detected (+5V power): %s\n",
1976 tda1997x_detect_tx_5v(sd) ? "yes" : "no");
1977 v4l2_info(sd, "HPD detected: %s\n",
1978 tda1997x_detect_tx_hpd(sd) ? "yes" : "no");
1980 v4l2_info(sd, "-----Video Timings-----\n");
1981 switch (tda1997x_detect_std(state, &timings)) {
1983 v4l2_info(sd, "No video detected\n");
1986 v4l2_info(sd, "Invalid signal detected\n");
1989 v4l2_print_dv_timings(sd->name, "Configured format: ",
1990 &state->timings, true);
1992 v4l2_info(sd, "-----Color space-----\n");
1993 v4l2_info(sd, "Input color space: %s %s %s",
1994 hdmi_colorspace_names[avi->colorspace],
1995 (avi->colorspace == HDMI_COLORSPACE_RGB) ? "" :
1996 hdmi_colorimetry_names[avi->colorimetry],
1997 v4l2_quantization_names[state->colorimetry.quantization]);
1998 v4l2_info(sd, "Output color space: %s",
1999 vidfmt_names[state->vid_fmt]);
2000 v4l2_info(sd, "Color space conversion: %s", state->conv ?
2001 state->conv->name : "None");
2003 v4l2_info(sd, "-----Audio-----\n");
2004 if (state->audio_channels) {
2005 v4l2_info(sd, "audio: %dch %dHz\n", state->audio_channels,
2006 state->audio_samplerate);
2008 v4l2_info(sd, "audio: none\n");
2011 v4l2_info(sd, "-----Infoframes-----\n");
2012 tda1997x_log_infoframe(sd, AUD_IF);
2013 tda1997x_log_infoframe(sd, SPD_IF);
2014 tda1997x_log_infoframe(sd, AVI_IF);
2019 static int tda1997x_subscribe_event(struct v4l2_subdev *sd,
2021 struct v4l2_event_subscription *sub)
2023 switch (sub->type) {
2024 case V4L2_EVENT_SOURCE_CHANGE:
2025 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
2026 case V4L2_EVENT_CTRL:
2027 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
2033 static const struct v4l2_subdev_core_ops tda1997x_core_ops = {
2034 .log_status = tda1997x_log_status,
2035 .subscribe_event = tda1997x_subscribe_event,
2036 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2039 /* -----------------------------------------------------------------------------
2043 static const struct v4l2_subdev_ops tda1997x_subdev_ops = {
2044 .core = &tda1997x_core_ops,
2045 .video = &tda1997x_video_ops,
2046 .pad = &tda1997x_pad_ops,
2049 static const struct v4l2_subdev_internal_ops tda1997x_internal_ops = {
2050 .init_state = tda1997x_init_state,
2053 /* -----------------------------------------------------------------------------
2057 static int tda1997x_s_ctrl(struct v4l2_ctrl *ctrl)
2059 struct v4l2_subdev *sd = to_sd(ctrl);
2060 struct tda1997x_state *state = to_state(sd);
2063 /* allow overriding the default RGB quantization range */
2064 case V4L2_CID_DV_RX_RGB_RANGE:
2065 state->rgb_quantization_range = ctrl->val;
2066 set_rgb_quantization_range(state);
2067 tda1997x_configure_csc(sd);
2074 static int tda1997x_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2076 struct v4l2_subdev *sd = to_sd(ctrl);
2077 struct tda1997x_state *state = to_state(sd);
2079 if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
2080 ctrl->val = state->avi_infoframe.content_type;
2086 static const struct v4l2_ctrl_ops tda1997x_ctrl_ops = {
2087 .s_ctrl = tda1997x_s_ctrl,
2088 .g_volatile_ctrl = tda1997x_g_volatile_ctrl,
2091 static int tda1997x_core_init(struct v4l2_subdev *sd)
2093 struct tda1997x_state *state = to_state(sd);
2094 struct tda1997x_platform_data *pdata = &state->pdata;
2099 io_write(sd, REG_HPD_AUTO_CTRL, HPD_AUTO_HPD_UNSEL);
2100 if (state->chip_revision == 0) {
2101 io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_DIS_HDCP | MAN_RST_HDCP);
2102 io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
2105 /* reset infoframe at end of start-up-sequencer */
2106 io_write(sd, REG_SUS_SET_RGB2, 0x06);
2107 io_write(sd, REG_SUS_SET_RGB3, 0x06);
2109 /* Enable TMDS pull-ups */
2110 io_write(sd, REG_RT_MAN_CTRL, RT_MAN_CTRL_RT |
2111 RT_MAN_CTRL_RT_B | RT_MAN_CTRL_RT_A);
2113 /* enable sync measurement timing */
2114 tda1997x_cec_write(sd, REG_PWR_CONTROL & 0xff, 0x04);
2115 /* adjust CEC clock divider */
2116 tda1997x_cec_write(sd, REG_OSC_DIVIDER & 0xff, 0x03);
2117 tda1997x_cec_write(sd, REG_EN_OSC_PERIOD_LSB & 0xff, 0xa0);
2118 io_write(sd, REG_TIMER_D, 0x54);
2119 /* enable power switch */
2120 reg = tda1997x_cec_read(sd, REG_CONTROL & 0xff);
2122 tda1997x_cec_write(sd, REG_CONTROL & 0xff, reg);
2125 /* read the chip version */
2126 reg = io_read(sd, REG_VERSION);
2127 /* get the chip configuration */
2128 reg = io_read(sd, REG_CMTP_REG10);
2130 /* enable interrupts we care about */
2131 io_write(sd, REG_INT_MASK_TOP,
2132 INTERRUPT_HDCP | INTERRUPT_AUDIO | INTERRUPT_INFO |
2133 INTERRUPT_RATE | INTERRUPT_SUS);
2134 /* config_mtp,fmt,sus_end,sus_st */
2135 io_write(sd, REG_INT_MASK_SUS, MASK_MPT | MASK_FMT | MASK_SUS_END);
2136 /* rate stability change for inputs A/B */
2137 io_write(sd, REG_INT_MASK_RATE, MASK_RATE_B_ST | MASK_RATE_A_ST);
2139 io_write(sd, REG_INT_MASK_INFO,
2140 MASK_AUD_IF | MASK_SPD_IF | MASK_AVI_IF);
2141 /* audio_freq,audio_flg,mute_flg,fifo_err */
2142 io_write(sd, REG_INT_MASK_AUDIO,
2143 MASK_AUDIO_FREQ_FLG | MASK_AUDIO_FLG | MASK_MUTE_FLG |
2144 MASK_ERROR_FIFO_PT);
2145 /* HDCP C5 state reached */
2146 io_write(sd, REG_INT_MASK_HDCP, MASK_STATE_C5);
2147 /* 5V detect and HDP pulse end */
2148 io_write(sd, REG_INT_MASK_DDC, MASK_DET_5V);
2149 /* don't care about AFE/MODE */
2150 io_write(sd, REG_INT_MASK_AFE, 0);
2151 io_write(sd, REG_INT_MASK_MODE, 0);
2153 /* clear all interrupts */
2154 io_write(sd, REG_INT_FLG_CLR_TOP, 0xff);
2155 io_write(sd, REG_INT_FLG_CLR_SUS, 0xff);
2156 io_write(sd, REG_INT_FLG_CLR_DDC, 0xff);
2157 io_write(sd, REG_INT_FLG_CLR_RATE, 0xff);
2158 io_write(sd, REG_INT_FLG_CLR_MODE, 0xff);
2159 io_write(sd, REG_INT_FLG_CLR_INFO, 0xff);
2160 io_write(sd, REG_INT_FLG_CLR_AUDIO, 0xff);
2161 io_write(sd, REG_INT_FLG_CLR_HDCP, 0xff);
2162 io_write(sd, REG_INT_FLG_CLR_AFE, 0xff);
2164 /* init TMDS equalizer */
2165 if (state->chip_revision == 0)
2166 io_write(sd, REG_CGU_DBG_SEL, 1 << CGU_DBG_CLK_SEL_SHIFT);
2167 io_write24(sd, REG_CLK_MIN_RATE, CLK_MIN_RATE);
2168 io_write24(sd, REG_CLK_MAX_RATE, CLK_MAX_RATE);
2169 if (state->chip_revision == 0)
2170 io_write(sd, REG_WDL_CFG, WDL_CFG_VAL);
2172 io_write(sd, REG_DEEP_COLOR_CTRL, DC_FILTER_VAL);
2173 /* disable test pattern */
2174 io_write(sd, REG_SVC_MODE, 0x00);
2175 /* update HDMI INFO CTRL */
2176 io_write(sd, REG_INFO_CTRL, 0xff);
2177 /* write HDMI INFO EXCEED value */
2178 io_write(sd, REG_INFO_EXCEED, 3);
2180 if (state->chip_revision == 0)
2181 tda1997x_reset_n1(state);
2184 * No HDCP acknowledge when HDCP is disabled
2185 * and reset SUS to force format detection
2187 tda1997x_hdmi_info_reset(sd, NACK_HDCP, true);
2190 tda1997x_manual_hpd(sd, HPD_LOW_BP);
2192 /* Configure receiver capabilities */
2193 io_write(sd, REG_HDCP_BCAPS, HDCP_HDMI | HDCP_FAST_REAUTH);
2195 /* Configure HDMI: Auto HDCP mode, packet controlled mute */
2196 reg = HDMI_CTRL_MUTE_AUTO << HDMI_CTRL_MUTE_SHIFT;
2197 reg |= HDMI_CTRL_HDCP_AUTO << HDMI_CTRL_HDCP_SHIFT;
2198 io_write(sd, REG_HDMI_CTRL, reg);
2200 /* reset start-up-sequencer to force format detection */
2201 tda1997x_hdmi_info_reset(sd, 0, true);
2203 /* disable matrix conversion */
2204 reg = io_read(sd, REG_VDP_CTRL);
2205 reg |= VDP_CTRL_MATRIX_BP;
2206 io_write(sd, REG_VDP_CTRL, reg);
2208 /* set video output mode */
2209 tda1997x_configure_vidout(state);
2211 /* configure video output port */
2212 for (i = 0; i < 9; i++) {
2213 v4l_dbg(1, debug, state->client, "vidout_cfg[%d]=0x%02x\n", i,
2214 pdata->vidout_port_cfg[i]);
2215 io_write(sd, REG_VP35_32_CTRL + i, pdata->vidout_port_cfg[i]);
2218 /* configure audio output port */
2219 tda1997x_configure_audout(sd, 0);
2221 /* configure audio clock freq */
2222 switch (pdata->audout_mclk_fs) {
2224 reg = AUDIO_CLOCK_SEL_512FS;
2227 reg = AUDIO_CLOCK_SEL_256FS;
2230 reg = AUDIO_CLOCK_SEL_128FS;
2233 reg = AUDIO_CLOCK_SEL_64FS;
2236 reg = AUDIO_CLOCK_SEL_32FS;
2239 reg = AUDIO_CLOCK_SEL_16FS;
2242 io_write(sd, REG_AUDIO_CLOCK, reg);
2244 /* reset advanced infoframes (ISRC1/ISRC2/ACP) */
2245 tda1997x_hdmi_info_reset(sd, RESET_AI, false);
2246 /* reset infoframe */
2247 tda1997x_hdmi_info_reset(sd, RESET_IF, false);
2248 /* reset audio infoframes */
2249 tda1997x_hdmi_info_reset(sd, RESET_AUDIO, false);
2251 tda1997x_hdmi_info_reset(sd, RESET_GAMUT, false);
2253 /* get initial HDMI status */
2254 state->hdmi_status = io_read(sd, REG_HDMI_FLAGS);
2256 io_write(sd, REG_EDID_ENABLE, EDID_ENABLE_A_EN | EDID_ENABLE_B_EN);
2260 static int tda1997x_set_power(struct tda1997x_state *state, bool on)
2265 ret = regulator_bulk_enable(TDA1997X_NUM_SUPPLIES,
2269 ret = regulator_bulk_disable(TDA1997X_NUM_SUPPLIES,
2276 static const struct i2c_device_id tda1997x_i2c_id[] = {
2277 {"tda19971", (kernel_ulong_t)&tda1997x_chip_info[TDA19971]},
2278 {"tda19973", (kernel_ulong_t)&tda1997x_chip_info[TDA19973]},
2281 MODULE_DEVICE_TABLE(i2c, tda1997x_i2c_id);
2283 static const struct of_device_id tda1997x_of_id[] __maybe_unused = {
2284 { .compatible = "nxp,tda19971", .data = &tda1997x_chip_info[TDA19971] },
2285 { .compatible = "nxp,tda19973", .data = &tda1997x_chip_info[TDA19973] },
2288 MODULE_DEVICE_TABLE(of, tda1997x_of_id);
2290 static int tda1997x_parse_dt(struct tda1997x_state *state)
2292 struct tda1997x_platform_data *pdata = &state->pdata;
2293 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2294 struct device_node *ep;
2295 struct device_node *np;
2302 * setup default values:
2303 * - HREF: active high from start to end of row
2304 * - VS: Vertical Sync active high at beginning of frame
2305 * - DE: Active high when data valid
2308 pdata->vidout_sel_hs = HS_HREF_SEL_HREF_VHREF;
2309 pdata->vidout_sel_vs = VS_VREF_SEL_VREF_HDMI;
2310 pdata->vidout_sel_de = DE_FREF_SEL_DE_VHREF;
2312 np = state->client->dev.of_node;
2313 ep = of_graph_get_endpoint_by_regs(np, 0, -1);
2317 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), &bus_cfg);
2323 pdata->vidout_bus_type = bus_cfg.bus_type;
2325 /* polarity of HS/VS/DE */
2326 flags = bus_cfg.bus.parallel.flags;
2327 if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
2328 pdata->vidout_inv_hs = 1;
2329 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2330 pdata->vidout_inv_vs = 1;
2331 if (flags & V4L2_MBUS_DATA_ACTIVE_LOW)
2332 pdata->vidout_inv_de = 1;
2333 pdata->vidout_bus_width = bus_cfg.bus.parallel.bus_width;
2335 /* video output port config */
2336 ret = of_property_count_u32_elems(np, "nxp,vidout-portcfg");
2340 for (i = 0; i < ret / 2 && i < 9; i++) {
2341 of_property_read_u32_index(np, "nxp,vidout-portcfg",
2343 of_property_read_u32_index(np, "nxp,vidout-portcfg",
2346 pdata->vidout_port_cfg[reg] = val;
2349 v4l_err(state->client, "nxp,vidout-portcfg missing\n");
2353 /* default to channel layout dictated by packet header */
2354 pdata->audout_layoutauto = true;
2356 pdata->audout_format = AUDFMT_TYPE_DISABLED;
2357 if (!of_property_read_string(np, "nxp,audout-format", &str)) {
2358 if (strcmp(str, "i2s") == 0)
2359 pdata->audout_format = AUDFMT_TYPE_I2S;
2360 else if (strcmp(str, "spdif") == 0)
2361 pdata->audout_format = AUDFMT_TYPE_SPDIF;
2363 v4l_err(state->client, "nxp,audout-format invalid\n");
2366 if (!of_property_read_u32(np, "nxp,audout-layout", &v)) {
2372 v4l_err(state->client,
2373 "nxp,audout-layout invalid\n");
2376 pdata->audout_layout = v;
2378 if (!of_property_read_u32(np, "nxp,audout-width", &v)) {
2384 v4l_err(state->client,
2385 "nxp,audout-width invalid\n");
2388 pdata->audout_width = v;
2390 if (!of_property_read_u32(np, "nxp,audout-mclk-fs", &v)) {
2400 v4l_err(state->client,
2401 "nxp,audout-mclk-fs invalid\n");
2404 pdata->audout_mclk_fs = v;
2411 static int tda1997x_get_regulators(struct tda1997x_state *state)
2415 for (i = 0; i < TDA1997X_NUM_SUPPLIES; i++)
2416 state->supplies[i].supply = tda1997x_supply_name[i];
2418 return devm_regulator_bulk_get(&state->client->dev,
2419 TDA1997X_NUM_SUPPLIES,
2423 static int tda1997x_identify_module(struct tda1997x_state *state)
2425 struct v4l2_subdev *sd = &state->sd;
2426 enum tda1997x_type type;
2429 /* Read chip configuration*/
2430 reg = io_read(sd, REG_CMTP_REG10);
2431 state->tmdsb_clk = (reg >> 6) & 0x01; /* use tmds clock B_inv for B */
2432 state->tmdsb_soc = (reg >> 5) & 0x01; /* tmds of input B */
2433 state->port_30bit = (reg >> 2) & 0x03; /* 30bit vs 24bit */
2434 state->output_2p5 = (reg >> 1) & 0x01; /* output supply 2.5v */
2435 switch ((reg >> 4) & 0x03) {
2444 dev_err(&state->client->dev, "unsupported chip ID\n");
2447 if (state->info->type != type) {
2448 dev_err(&state->client->dev, "chip id mismatch\n");
2452 /* read chip revision */
2453 state->chip_revision = io_read(sd, REG_CMTP_REG11);
2458 static const struct media_entity_operations tda1997x_media_ops = {
2459 .link_validate = v4l2_subdev_link_validate,
2463 /* -----------------------------------------------------------------------------
2467 /* refine sample-rate based on HDMI source */
2468 static int tda1997x_pcm_startup(struct snd_pcm_substream *substream,
2469 struct snd_soc_dai *dai)
2471 struct v4l2_subdev *sd = snd_soc_dai_get_drvdata(dai);
2472 struct tda1997x_state *state = to_state(sd);
2473 struct snd_soc_component *component = dai->component;
2474 struct snd_pcm_runtime *rtd = substream->runtime;
2477 rate = state->audio_samplerate;
2478 err = snd_pcm_hw_constraint_minmax(rtd, SNDRV_PCM_HW_PARAM_RATE,
2481 dev_err(component->dev, "failed to constrain samplerate to %dHz\n",
2485 dev_info(component->dev, "set samplerate constraint to %dHz\n", rate);
2490 static const struct snd_soc_dai_ops tda1997x_dai_ops = {
2491 .startup = tda1997x_pcm_startup,
2494 static struct snd_soc_dai_driver tda1997x_audio_dai = {
2497 .stream_name = "Capture",
2500 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
2501 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
2502 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
2503 SNDRV_PCM_RATE_192000,
2505 .ops = &tda1997x_dai_ops,
2508 static int tda1997x_codec_probe(struct snd_soc_component *component)
2513 static void tda1997x_codec_remove(struct snd_soc_component *component)
2517 static const struct snd_soc_component_driver tda1997x_codec_driver = {
2518 .probe = tda1997x_codec_probe,
2519 .remove = tda1997x_codec_remove,
2521 .use_pmdown_time = 1,
2525 static int tda1997x_probe(struct i2c_client *client)
2527 const struct i2c_device_id *id = i2c_client_get_device_id(client);
2528 struct tda1997x_state *state;
2529 struct tda1997x_platform_data *pdata;
2530 struct v4l2_subdev *sd;
2531 struct v4l2_ctrl_handler *hdl;
2532 struct v4l2_ctrl *ctrl;
2533 static const struct v4l2_dv_timings cea1920x1080 =
2534 V4L2_DV_BT_CEA_1920X1080P60;
2538 /* Check if the adapter supports the needed features */
2539 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2542 state = kzalloc(sizeof(struct tda1997x_state), GFP_KERNEL);
2546 state->client = client;
2547 pdata = &state->pdata;
2548 if (IS_ENABLED(CONFIG_OF) && client->dev.of_node) {
2549 const struct of_device_id *oid;
2551 oid = of_match_node(tda1997x_of_id, client->dev.of_node);
2552 state->info = oid->data;
2554 ret = tda1997x_parse_dt(state);
2556 v4l_err(client, "DT parsing error\n");
2557 goto err_free_state;
2559 } else if (client->dev.platform_data) {
2560 struct tda1997x_platform_data *pdata =
2561 client->dev.platform_data;
2563 (const struct tda1997x_chip_info *)id->driver_data;
2564 state->pdata = *pdata;
2566 v4l_err(client, "No platform data\n");
2568 goto err_free_state;
2571 ret = tda1997x_get_regulators(state);
2573 goto err_free_state;
2575 ret = tda1997x_set_power(state, 1);
2577 goto err_free_state;
2579 mutex_init(&state->page_lock);
2580 mutex_init(&state->lock);
2583 INIT_DELAYED_WORK(&state->delayed_work_enable_hpd,
2584 tda1997x_delayed_work_enable_hpd);
2586 /* set video format based on chip and bus width */
2587 ret = tda1997x_identify_module(state);
2589 goto err_free_mutex;
2591 /* initialize subdev */
2593 v4l2_i2c_subdev_init(sd, client, &tda1997x_subdev_ops);
2594 sd->internal_ops = &tda1997x_internal_ops;
2595 snprintf(sd->name, sizeof(sd->name), "%s %d-%04x",
2596 id->name, i2c_adapter_id(client->adapter),
2598 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2599 sd->entity.function = MEDIA_ENT_F_DV_DECODER;
2600 sd->entity.ops = &tda1997x_media_ops;
2602 /* set allowed mbus modes based on chip, bus-type, and bus-width */
2604 mbus_codes = state->mbus_codes;
2605 switch (state->info->type) {
2607 switch (pdata->vidout_bus_type) {
2608 case V4L2_MBUS_PARALLEL:
2609 switch (pdata->vidout_bus_width) {
2611 mbus_codes[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
2612 mbus_codes[i++] = MEDIA_BUS_FMT_YUV12_1X36;
2615 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
2619 case V4L2_MBUS_BT656:
2620 switch (pdata->vidout_bus_width) {
2624 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
2625 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
2626 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
2635 switch (pdata->vidout_bus_type) {
2636 case V4L2_MBUS_PARALLEL:
2637 switch (pdata->vidout_bus_width) {
2639 mbus_codes[i++] = MEDIA_BUS_FMT_RGB888_1X24;
2640 mbus_codes[i++] = MEDIA_BUS_FMT_YUV8_1X24;
2641 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
2644 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
2647 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
2651 case V4L2_MBUS_BT656:
2652 switch (pdata->vidout_bus_width) {
2657 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY12_2X12;
2660 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY10_2X10;
2663 mbus_codes[i++] = MEDIA_BUS_FMT_UYVY8_2X8;
2672 if (WARN_ON(i > ARRAY_SIZE(state->mbus_codes))) {
2674 goto err_free_mutex;
2677 /* default format */
2678 tda1997x_setup_format(state, state->mbus_codes[0]);
2679 state->timings = cea1920x1080;
2682 * default to SRGB full range quantization
2683 * (in case we don't get an infoframe such as DVI signal
2685 state->colorimetry.colorspace = V4L2_COLORSPACE_SRGB;
2686 state->colorimetry.quantization = V4L2_QUANTIZATION_FULL_RANGE;
2688 /* disable/reset HDCP to get correct I2C access to Rx HDMI */
2689 io_write(sd, REG_MAN_SUS_HDMI_SEL, MAN_RST_HDCP | MAN_DIS_HDCP);
2692 * if N2 version, reset compdel_bp as it may generate some small pixel
2693 * shifts in case of embedded sync/or delay lower than 4
2695 if (state->chip_revision != 0) {
2696 io_write(sd, REG_MAN_SUS_HDMI_SEL, 0x00);
2697 io_write(sd, REG_VDP_CTRL, 0x1f);
2700 v4l_info(client, "NXP %s N%d detected\n", state->info->name,
2701 state->chip_revision + 1);
2702 v4l_info(client, "video: %dbit %s %d formats available\n",
2703 pdata->vidout_bus_width,
2704 (pdata->vidout_bus_type == V4L2_MBUS_PARALLEL) ?
2705 "parallel" : "BT656",
2707 if (pdata->audout_format) {
2708 v4l_info(client, "audio: %dch %s layout%d sysclk=%d*fs\n",
2709 pdata->audout_layout ? 2 : 8,
2710 audfmt_names[pdata->audout_format],
2711 pdata->audout_layout,
2712 pdata->audout_mclk_fs);
2715 ret = 0x34 + ((io_read(sd, REG_SLAVE_ADDR)>>4) & 0x03);
2716 state->client_cec = devm_i2c_new_dummy_device(&client->dev,
2717 client->adapter, ret);
2718 if (IS_ERR(state->client_cec)) {
2719 ret = PTR_ERR(state->client_cec);
2720 goto err_free_mutex;
2723 v4l_info(client, "CEC slave address 0x%02x\n", ret);
2725 ret = tda1997x_core_init(sd);
2727 goto err_free_mutex;
2729 /* control handlers */
2731 v4l2_ctrl_handler_init(hdl, 3);
2732 ctrl = v4l2_ctrl_new_std_menu(hdl, &tda1997x_ctrl_ops,
2733 V4L2_CID_DV_RX_IT_CONTENT_TYPE,
2734 V4L2_DV_IT_CONTENT_TYPE_NO_ITC, 0,
2735 V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
2737 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2738 /* custom controls */
2739 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
2740 V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
2741 state->rgb_quantization_range_ctrl = v4l2_ctrl_new_std_menu(hdl,
2743 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL, 0,
2744 V4L2_DV_RGB_RANGE_AUTO);
2745 state->sd.ctrl_handler = hdl;
2748 goto err_free_handler;
2750 v4l2_ctrl_handler_setup(hdl);
2752 /* initialize source pads */
2753 state->pads[TDA1997X_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE;
2754 ret = media_entity_pads_init(&sd->entity, TDA1997X_NUM_PADS,
2757 v4l_err(client, "failed entity_init: %d", ret);
2758 goto err_free_handler;
2761 ret = v4l2_async_register_subdev(sd);
2763 goto err_free_media;
2765 /* register audio DAI */
2766 if (pdata->audout_format) {
2769 if (pdata->audout_width == 32)
2770 formats = SNDRV_PCM_FMTBIT_S32_LE;
2772 formats = SNDRV_PCM_FMTBIT_S16_LE;
2773 tda1997x_audio_dai.capture.formats = formats;
2774 ret = devm_snd_soc_register_component(&state->client->dev,
2775 &tda1997x_codec_driver,
2776 &tda1997x_audio_dai, 1);
2778 dev_err(&client->dev, "register audio codec failed\n");
2779 goto err_free_media;
2781 v4l_info(state->client, "registered audio codec\n");
2785 ret = devm_request_threaded_irq(&client->dev, client->irq,
2786 NULL, tda1997x_isr_thread,
2787 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
2788 KBUILD_MODNAME, state);
2790 v4l_err(client, "irq%d reg failed: %d\n", client->irq, ret);
2791 goto err_free_media;
2797 media_entity_cleanup(&sd->entity);
2799 v4l2_ctrl_handler_free(&state->hdl);
2801 cancel_delayed_work(&state->delayed_work_enable_hpd);
2802 mutex_destroy(&state->page_lock);
2803 mutex_destroy(&state->lock);
2804 tda1997x_set_power(state, 0);
2807 dev_err(&client->dev, "%s failed: %d\n", __func__, ret);
2812 static void tda1997x_remove(struct i2c_client *client)
2814 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2815 struct tda1997x_state *state = to_state(sd);
2816 struct tda1997x_platform_data *pdata = &state->pdata;
2818 if (pdata->audout_format) {
2819 mutex_destroy(&state->audio_lock);
2822 disable_irq(state->client->irq);
2823 tda1997x_power_mode(state, 0);
2825 v4l2_async_unregister_subdev(sd);
2826 media_entity_cleanup(&sd->entity);
2827 v4l2_ctrl_handler_free(&state->hdl);
2828 regulator_bulk_disable(TDA1997X_NUM_SUPPLIES, state->supplies);
2829 cancel_delayed_work_sync(&state->delayed_work_enable_hpd);
2830 mutex_destroy(&state->page_lock);
2831 mutex_destroy(&state->lock);
2836 static struct i2c_driver tda1997x_i2c_driver = {
2839 .of_match_table = of_match_ptr(tda1997x_of_id),
2841 .probe = tda1997x_probe,
2842 .remove = tda1997x_remove,
2843 .id_table = tda1997x_i2c_id,
2846 module_i2c_driver(tda1997x_i2c_driver);
2849 MODULE_DESCRIPTION("TDA1997X HDMI Receiver driver");
2850 MODULE_LICENSE("GPL v2");