xref: /linux/drivers/media/i2c/ths8200.c (revision 00b9f51e1fda8a10ad6f52656722ee618e6a431d)
114735190SHans Verkuil /*
214735190SHans Verkuil  * ths8200 - Texas Instruments THS8200 video encoder driver
314735190SHans Verkuil  *
414735190SHans Verkuil  * Copyright 2013 Cisco Systems, Inc. and/or its affiliates.
514735190SHans Verkuil  *
614735190SHans Verkuil  * This program is free software; you may redistribute it and/or modify
714735190SHans Verkuil  * it under the terms of the GNU General Public License as published by
814735190SHans Verkuil  * the Free Software Foundation; version 2 of the License.
914735190SHans Verkuil  *
1014735190SHans Verkuil  * This program is free software; you can redistribute it and/or
1114735190SHans Verkuil  * modify it under the terms of the GNU General Public License as
1214735190SHans Verkuil  * published by the Free Software Foundation version 2.
1314735190SHans Verkuil  *
1414735190SHans Verkuil  * This program is distributed .as is. WITHOUT ANY WARRANTY of any
1514735190SHans Verkuil  * kind, whether express or implied; without even the implied warranty
1614735190SHans Verkuil  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1714735190SHans Verkuil  * GNU General Public License for more details.
1814735190SHans Verkuil  */
1914735190SHans Verkuil 
2014735190SHans Verkuil #include <linux/i2c.h>
2114735190SHans Verkuil #include <linux/module.h>
228f893815SSachin Kamat #include <linux/of.h>
2314735190SHans Verkuil #include <linux/v4l2-dv-timings.h>
2414735190SHans Verkuil 
2525764158SHans Verkuil #include <media/v4l2-dv-timings.h>
26ed29f894SLad, Prabhakar #include <media/v4l2-async.h>
2714735190SHans Verkuil #include <media/v4l2-device.h>
2814735190SHans Verkuil 
2914735190SHans Verkuil #include "ths8200_regs.h"
3014735190SHans Verkuil 
3114735190SHans Verkuil static int debug;
3214735190SHans Verkuil module_param(debug, int, 0644);
3314735190SHans Verkuil MODULE_PARM_DESC(debug, "debug level (0-2)");
3414735190SHans Verkuil 
3514735190SHans Verkuil MODULE_DESCRIPTION("Texas Instruments THS8200 video encoder driver");
3614735190SHans Verkuil MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
3714735190SHans Verkuil MODULE_AUTHOR("Martin Bugge <martin.bugge@cisco.com>");
3814735190SHans Verkuil MODULE_LICENSE("GPL v2");
3914735190SHans Verkuil 
4014735190SHans Verkuil struct ths8200_state {
4114735190SHans Verkuil 	struct v4l2_subdev sd;
4214735190SHans Verkuil 	uint8_t chip_version;
4314735190SHans Verkuil 	/* Is the ths8200 powered on? */
4414735190SHans Verkuil 	bool power_on;
4514735190SHans Verkuil 	struct v4l2_dv_timings dv_timings;
4614735190SHans Verkuil };
4714735190SHans Verkuil 
4804164904SHans Verkuil static const struct v4l2_dv_timings_cap ths8200_timings_cap = {
4904164904SHans Verkuil 	.type = V4L2_DV_BT_656_1120,
5083291f78SGianluca Gennari 	/* keep this initialization for compatibility with GCC < 4.4.6 */
5183291f78SGianluca Gennari 	.reserved = { 0 },
5283291f78SGianluca Gennari 	V4L2_INIT_BT_TIMINGS(0, 1920, 0, 1080, 25000000, 148500000,
5383291f78SGianluca Gennari 		V4L2_DV_BT_STD_CEA861, V4L2_DV_BT_CAP_PROGRESSIVE)
5414735190SHans Verkuil };
5514735190SHans Verkuil 
5614735190SHans Verkuil static inline struct ths8200_state *to_state(struct v4l2_subdev *sd)
5714735190SHans Verkuil {
5814735190SHans Verkuil 	return container_of(sd, struct ths8200_state, sd);
5914735190SHans Verkuil }
6014735190SHans Verkuil 
6114735190SHans Verkuil static inline unsigned hblanking(const struct v4l2_bt_timings *t)
6214735190SHans Verkuil {
63eacf8f9aSHans Verkuil 	return V4L2_DV_BT_BLANKING_WIDTH(t);
6414735190SHans Verkuil }
6514735190SHans Verkuil 
6614735190SHans Verkuil static inline unsigned htotal(const struct v4l2_bt_timings *t)
6714735190SHans Verkuil {
68eacf8f9aSHans Verkuil 	return V4L2_DV_BT_FRAME_WIDTH(t);
6914735190SHans Verkuil }
7014735190SHans Verkuil 
7114735190SHans Verkuil static inline unsigned vblanking(const struct v4l2_bt_timings *t)
7214735190SHans Verkuil {
73eacf8f9aSHans Verkuil 	return V4L2_DV_BT_BLANKING_HEIGHT(t);
7414735190SHans Verkuil }
7514735190SHans Verkuil 
7614735190SHans Verkuil static inline unsigned vtotal(const struct v4l2_bt_timings *t)
7714735190SHans Verkuil {
78eacf8f9aSHans Verkuil 	return V4L2_DV_BT_FRAME_HEIGHT(t);
7914735190SHans Verkuil }
8014735190SHans Verkuil 
8114735190SHans Verkuil static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
8214735190SHans Verkuil {
8314735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
8414735190SHans Verkuil 
8514735190SHans Verkuil 	return i2c_smbus_read_byte_data(client, reg);
8614735190SHans Verkuil }
8714735190SHans Verkuil 
8814735190SHans Verkuil static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
8914735190SHans Verkuil {
9014735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
9114735190SHans Verkuil 	int ret;
9214735190SHans Verkuil 	int i;
9314735190SHans Verkuil 
9414735190SHans Verkuil 	for (i = 0; i < 3; i++) {
9514735190SHans Verkuil 		ret = i2c_smbus_write_byte_data(client, reg, val);
9614735190SHans Verkuil 		if (ret == 0)
9714735190SHans Verkuil 			return 0;
9814735190SHans Verkuil 	}
9914735190SHans Verkuil 	v4l2_err(sd, "I2C Write Problem\n");
10014735190SHans Verkuil 	return ret;
10114735190SHans Verkuil }
10214735190SHans Verkuil 
10314735190SHans Verkuil /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
10414735190SHans Verkuil  * and then the value-mask (to be OR-ed).
10514735190SHans Verkuil  */
10614735190SHans Verkuil static inline void
10714735190SHans Verkuil ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
10814735190SHans Verkuil 		     uint8_t clr_mask, uint8_t val_mask)
10914735190SHans Verkuil {
11014735190SHans Verkuil 	ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
11114735190SHans Verkuil }
11214735190SHans Verkuil 
11314735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
11414735190SHans Verkuil 
11514735190SHans Verkuil static int ths8200_g_register(struct v4l2_subdev *sd,
11614735190SHans Verkuil 			      struct v4l2_dbg_register *reg)
11714735190SHans Verkuil {
11814735190SHans Verkuil 	reg->val = ths8200_read(sd, reg->reg & 0xff);
11914735190SHans Verkuil 	reg->size = 1;
12014735190SHans Verkuil 
12114735190SHans Verkuil 	return 0;
12214735190SHans Verkuil }
12314735190SHans Verkuil 
12414735190SHans Verkuil static int ths8200_s_register(struct v4l2_subdev *sd,
12514735190SHans Verkuil 			      const struct v4l2_dbg_register *reg)
12614735190SHans Verkuil {
12714735190SHans Verkuil 	ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
12814735190SHans Verkuil 
12914735190SHans Verkuil 	return 0;
13014735190SHans Verkuil }
13114735190SHans Verkuil #endif
13214735190SHans Verkuil 
13314735190SHans Verkuil static int ths8200_log_status(struct v4l2_subdev *sd)
13414735190SHans Verkuil {
13514735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
13614735190SHans Verkuil 	uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
13714735190SHans Verkuil 
13814735190SHans Verkuil 	v4l2_info(sd, "----- Chip status -----\n");
13914735190SHans Verkuil 	v4l2_info(sd, "version: %u\n", state->chip_version);
14014735190SHans Verkuil 	v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
14114735190SHans Verkuil 	v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
14214735190SHans Verkuil 	v4l2_info(sd, "test pattern: %s\n",
14314735190SHans Verkuil 		  (reg_03 & 0x20) ? "enabled" : "disabled");
14414735190SHans Verkuil 	v4l2_info(sd, "format: %ux%u\n",
14514735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
14614735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
14714735190SHans Verkuil 		  (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
14814735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
14911d034c8SHans Verkuil 	v4l2_print_dv_timings(sd->name, "Configured format:",
15011d034c8SHans Verkuil 			      &state->dv_timings, true);
15114735190SHans Verkuil 	return 0;
15214735190SHans Verkuil }
15314735190SHans Verkuil 
15414735190SHans Verkuil /* Power up/down ths8200 */
15514735190SHans Verkuil static int ths8200_s_power(struct v4l2_subdev *sd, int on)
15614735190SHans Verkuil {
15714735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
15814735190SHans Verkuil 
15914735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
16014735190SHans Verkuil 
16114735190SHans Verkuil 	state->power_on = on;
16214735190SHans Verkuil 
16314735190SHans Verkuil 	/* Power up/down - leave in reset state until input video is present */
16414735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
16514735190SHans Verkuil 
16614735190SHans Verkuil 	return 0;
16714735190SHans Verkuil }
16814735190SHans Verkuil 
16914735190SHans Verkuil static const struct v4l2_subdev_core_ops ths8200_core_ops = {
17014735190SHans Verkuil 	.log_status = ths8200_log_status,
17114735190SHans Verkuil 	.s_power = ths8200_s_power,
17214735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
17314735190SHans Verkuil 	.g_register = ths8200_g_register,
17414735190SHans Verkuil 	.s_register = ths8200_s_register,
17514735190SHans Verkuil #endif
17614735190SHans Verkuil };
17714735190SHans Verkuil 
17814735190SHans Verkuil /* -----------------------------------------------------------------------------
17914735190SHans Verkuil  * V4L2 subdev video operations
18014735190SHans Verkuil  */
18114735190SHans Verkuil 
18214735190SHans Verkuil static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
18314735190SHans Verkuil {
18414735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
18514735190SHans Verkuil 
18614735190SHans Verkuil 	if (enable && !state->power_on)
18714735190SHans Verkuil 		ths8200_s_power(sd, true);
18814735190SHans Verkuil 
18914735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
19014735190SHans Verkuil 			     (enable ? 0x01 : 0x00));
19114735190SHans Verkuil 
19214735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: %sable\n",
19314735190SHans Verkuil 		 __func__, (enable ? "en" : "dis"));
19414735190SHans Verkuil 
19514735190SHans Verkuil 	return 0;
19614735190SHans Verkuil }
19714735190SHans Verkuil 
19814735190SHans Verkuil static void ths8200_core_init(struct v4l2_subdev *sd)
19914735190SHans Verkuil {
20014735190SHans Verkuil 	/* setup clocks */
20114735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
20214735190SHans Verkuil 
20314735190SHans Verkuil 	/**** Data path control (DATA) ****/
20414735190SHans Verkuil 	/* Set FSADJ 700 mV,
20514735190SHans Verkuil 	 * bypass 422-444 interpolation,
20614735190SHans Verkuil 	 * input format 30 bit RGB444
20714735190SHans Verkuil 	 */
20814735190SHans Verkuil 	ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
20914735190SHans Verkuil 
21014735190SHans Verkuil 	/* DTG Mode (Video blocked during blanking
21114735190SHans Verkuil 	 * VESA slave
21214735190SHans Verkuil 	 */
21314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
21414735190SHans Verkuil 
21514735190SHans Verkuil 	/**** Display Timing Generator Control, Part 1 (DTG1). ****/
21614735190SHans Verkuil 
21714735190SHans Verkuil 	/* Disable embedded syncs on the output by setting
21814735190SHans Verkuil 	 * the amplitude to zero for all channels.
21914735190SHans Verkuil 	 */
2208a027fafSMartin Bugge 	ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x00);
2218a027fafSMartin Bugge 	ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x00);
22214735190SHans Verkuil }
22314735190SHans Verkuil 
22414735190SHans Verkuil static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
22514735190SHans Verkuil {
22614735190SHans Verkuil 	uint8_t polarity = 0;
22714735190SHans Verkuil 	uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
22814735190SHans Verkuil 	uint16_t line_start_front_porch  = (vtotal(bt) - bt->vfrontporch);
22914735190SHans Verkuil 
23014735190SHans Verkuil 	/*** System ****/
23114735190SHans Verkuil 	/* Set chip in reset while it is configured */
23214735190SHans Verkuil 	ths8200_s_stream(sd, false);
23314735190SHans Verkuil 
23414735190SHans Verkuil 	/* configure video output timings */
23514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
23614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
23714735190SHans Verkuil 
23814735190SHans Verkuil 	/* Zero for progressive scan formats.*/
23914735190SHans Verkuil 	if (!bt->interlaced)
24014735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
24114735190SHans Verkuil 
24214735190SHans Verkuil 	/* Distance from leading edge of h sync to start of active video.
24314735190SHans Verkuil 	 * MSB in 0x2b
24414735190SHans Verkuil 	 */
24514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
24614735190SHans Verkuil 		      (bt->hbackporch + bt->hsync) & 0xff);
24714735190SHans Verkuil 	/* Zero for SDTV-mode. MSB in 0x2b */
24814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
24914735190SHans Verkuil 	/*
25014735190SHans Verkuil 	 * MSB for dtg1_spec(d/e/h). See comment for
25114735190SHans Verkuil 	 * corresponding LSB registers.
25214735190SHans Verkuil 	 */
25314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
25414735190SHans Verkuil 		      ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
25514735190SHans Verkuil 
25614735190SHans Verkuil 	/* h front porch */
25714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
25814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
25914735190SHans Verkuil 		      ((bt->hfrontporch) & 0x700) >> 8);
26014735190SHans Verkuil 
26114735190SHans Verkuil 	/* Half the line length. Used to calculate SDTV line types. */
26214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
26314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
26414735190SHans Verkuil 		      ((htotal(bt)/2) >> 8) & 0x0f);
26514735190SHans Verkuil 
26614735190SHans Verkuil 	/* Total pixels per line (ex. 720p: 1650) */
26714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
26814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
26914735190SHans Verkuil 
27014735190SHans Verkuil 	/* Frame height and field height */
27114735190SHans Verkuil 	/* Field height should be programmed higher than frame_size for
27214735190SHans Verkuil 	 * progressive scan formats
27314735190SHans Verkuil 	 */
27414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
27514735190SHans Verkuil 		      ((vtotal(bt) >> 4) & 0xf0) + 0x7);
27614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
27714735190SHans Verkuil 
27814735190SHans Verkuil 	/* Should be programmed higher than frame_size
27914735190SHans Verkuil 	 * for progressive formats
28014735190SHans Verkuil 	 */
28114735190SHans Verkuil 	if (!bt->interlaced)
28214735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
28314735190SHans Verkuil 
28414735190SHans Verkuil 	/**** Display Timing Generator Control, Part 2 (DTG2). ****/
28514735190SHans Verkuil 	/* Set breakpoint line numbers and types
28614735190SHans Verkuil 	 * THS8200 generates line types with different properties. A line type
28714735190SHans Verkuil 	 * that sets all the RGB-outputs to zero is used in the blanking areas,
28814735190SHans Verkuil 	 * while a line type that enable the RGB-outputs is used in active video
28914735190SHans Verkuil 	 * area. The line numbers for start of active video, start of front
29014735190SHans Verkuil 	 * porch and after the last line in the frame must be set with the
29114735190SHans Verkuil 	 * corresponding line types.
29214735190SHans Verkuil 	 *
29314735190SHans Verkuil 	 * Line types:
29414735190SHans Verkuil 	 * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
29514735190SHans Verkuil 	 *       Used in blanking area.
29614735190SHans Verkuil 	 * 0x0 - Active video: Video data is always passed. Used in active
29714735190SHans Verkuil 	 *       video area.
29814735190SHans Verkuil 	 */
29914735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
30014735190SHans Verkuil 			     ((line_start_active_video >> 4) & 0x70) +
30114735190SHans Verkuil 			     ((line_start_front_porch >> 8) & 0x07));
30214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
30314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
30414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
30514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
30614735190SHans Verkuil 
30714735190SHans Verkuil 	/* line types */
30814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
30914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
31014735190SHans Verkuil 
31114735190SHans Verkuil 	/* h sync width transmitted */
31214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
31314735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
31414735190SHans Verkuil 			     (bt->hsync >> 2) & 0xc0);
31514735190SHans Verkuil 
31614735190SHans Verkuil 	/* The pixel value h sync is asserted on */
31714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
31814735190SHans Verkuil 			     (htotal(bt) >> 8) & 0x1f);
31914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
32014735190SHans Verkuil 
32114735190SHans Verkuil 	/* v sync width transmitted */
32214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync) & 0xff);
32314735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
32414735190SHans Verkuil 			     ((bt->vsync) >> 2) & 0xc0);
32514735190SHans Verkuil 
32614735190SHans Verkuil 	/* The pixel value v sync is asserted on */
32714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
32814735190SHans Verkuil 			     (vtotal(bt)>>8) & 0x7);
32914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt));
33014735190SHans Verkuil 
33114735190SHans Verkuil 	/* For progressive video vlength2 must be set to all 0 and vdly2 must
33214735190SHans Verkuil 	 * be set to all 1.
33314735190SHans Verkuil 	 */
33414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
33514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
33614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
33714735190SHans Verkuil 
33814735190SHans Verkuil 	/* Internal delay factors to synchronize the sync pulses and the data */
33914735190SHans Verkuil 	/* Experimental values delays (hor 4, ver 1) */
34014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, (htotal(bt)>>8) & 0x1f);
34114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, (htotal(bt) - 4) & 0xff);
34214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
34314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 1);
34414735190SHans Verkuil 
34514735190SHans Verkuil 	/* Polarity of received and transmitted sync signals */
34614735190SHans Verkuil 	if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
34714735190SHans Verkuil 		polarity |= 0x01; /* HS_IN */
34814735190SHans Verkuil 		polarity |= 0x08; /* HS_OUT */
34914735190SHans Verkuil 	}
35014735190SHans Verkuil 	if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
35114735190SHans Verkuil 		polarity |= 0x02; /* VS_IN */
35214735190SHans Verkuil 		polarity |= 0x10; /* VS_OUT */
35314735190SHans Verkuil 	}
35414735190SHans Verkuil 
35514735190SHans Verkuil 	/* RGB mode, no embedded timings */
35614735190SHans Verkuil 	/* Timing of video input bus is derived from HS, VS, and FID dedicated
35714735190SHans Verkuil 	 * inputs
35814735190SHans Verkuil 	 */
359*00b9f51eSMartin Bugge 	ths8200_write(sd, THS8200_DTG2_CNTL, 0x44 | polarity);
36014735190SHans Verkuil 
36114735190SHans Verkuil 	/* leave reset */
36214735190SHans Verkuil 	ths8200_s_stream(sd, true);
36314735190SHans Verkuil 
36414735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
36514735190SHans Verkuil 		 "horizontal: front porch %d, back porch %d, sync %d\n"
36614735190SHans Verkuil 		 "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
36714735190SHans Verkuil 		 polarity, bt->hfrontporch, bt->hbackporch,
36814735190SHans Verkuil 		 bt->hsync, bt->vsync);
36914735190SHans Verkuil }
37014735190SHans Verkuil 
37114735190SHans Verkuil static int ths8200_s_dv_timings(struct v4l2_subdev *sd,
37214735190SHans Verkuil 				struct v4l2_dv_timings *timings)
37314735190SHans Verkuil {
37414735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
37514735190SHans Verkuil 
37614735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
37714735190SHans Verkuil 
378b8f0fff4SHans Verkuil 	if (!v4l2_valid_dv_timings(timings, &ths8200_timings_cap,
379b8f0fff4SHans Verkuil 				NULL, NULL))
38014735190SHans Verkuil 		return -EINVAL;
38114735190SHans Verkuil 
382b8f0fff4SHans Verkuil 	if (!v4l2_find_dv_timings_cap(timings, &ths8200_timings_cap, 10,
383b8f0fff4SHans Verkuil 				NULL, NULL)) {
38414735190SHans Verkuil 		v4l2_dbg(1, debug, sd, "Unsupported format\n");
38514735190SHans Verkuil 		return -EINVAL;
38614735190SHans Verkuil 	}
38714735190SHans Verkuil 
38814735190SHans Verkuil 	timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
38914735190SHans Verkuil 
39014735190SHans Verkuil 	/* save timings */
39114735190SHans Verkuil 	state->dv_timings = *timings;
39214735190SHans Verkuil 
39314735190SHans Verkuil 	ths8200_setup(sd, &timings->bt);
39414735190SHans Verkuil 
39514735190SHans Verkuil 	return 0;
39614735190SHans Verkuil }
39714735190SHans Verkuil 
39814735190SHans Verkuil static int ths8200_g_dv_timings(struct v4l2_subdev *sd,
39914735190SHans Verkuil 				struct v4l2_dv_timings *timings)
40014735190SHans Verkuil {
40114735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
40214735190SHans Verkuil 
40314735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
40414735190SHans Verkuil 
40514735190SHans Verkuil 	*timings = state->dv_timings;
40614735190SHans Verkuil 
40714735190SHans Verkuil 	return 0;
40814735190SHans Verkuil }
40914735190SHans Verkuil 
41014735190SHans Verkuil static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
41114735190SHans Verkuil 				   struct v4l2_enum_dv_timings *timings)
41214735190SHans Verkuil {
413b8f0fff4SHans Verkuil 	return v4l2_enum_dv_timings_cap(timings, &ths8200_timings_cap,
414b8f0fff4SHans Verkuil 			NULL, NULL);
41514735190SHans Verkuil }
41614735190SHans Verkuil 
41714735190SHans Verkuil static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
41814735190SHans Verkuil 				  struct v4l2_dv_timings_cap *cap)
41914735190SHans Verkuil {
42004164904SHans Verkuil 	*cap = ths8200_timings_cap;
42114735190SHans Verkuil 	return 0;
42214735190SHans Verkuil }
42314735190SHans Verkuil 
42414735190SHans Verkuil /* Specific video subsystem operation handlers */
42514735190SHans Verkuil static const struct v4l2_subdev_video_ops ths8200_video_ops = {
42614735190SHans Verkuil 	.s_stream = ths8200_s_stream,
42714735190SHans Verkuil 	.s_dv_timings = ths8200_s_dv_timings,
42814735190SHans Verkuil 	.g_dv_timings = ths8200_g_dv_timings,
42914735190SHans Verkuil 	.enum_dv_timings = ths8200_enum_dv_timings,
43014735190SHans Verkuil 	.dv_timings_cap = ths8200_dv_timings_cap,
43114735190SHans Verkuil };
43214735190SHans Verkuil 
43314735190SHans Verkuil /* V4L2 top level operation handlers */
43414735190SHans Verkuil static const struct v4l2_subdev_ops ths8200_ops = {
43514735190SHans Verkuil 	.core  = &ths8200_core_ops,
43614735190SHans Verkuil 	.video = &ths8200_video_ops,
43714735190SHans Verkuil };
43814735190SHans Verkuil 
43914735190SHans Verkuil static int ths8200_probe(struct i2c_client *client,
44014735190SHans Verkuil 			 const struct i2c_device_id *id)
44114735190SHans Verkuil {
44214735190SHans Verkuil 	struct ths8200_state *state;
44314735190SHans Verkuil 	struct v4l2_subdev *sd;
444ed29f894SLad, Prabhakar 	int error;
44514735190SHans Verkuil 
44614735190SHans Verkuil 	/* Check if the adapter supports the needed features */
44714735190SHans Verkuil 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
44814735190SHans Verkuil 		return -EIO;
44914735190SHans Verkuil 
45014735190SHans Verkuil 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
45114735190SHans Verkuil 	if (!state)
45214735190SHans Verkuil 		return -ENOMEM;
45314735190SHans Verkuil 
45414735190SHans Verkuil 	sd = &state->sd;
45514735190SHans Verkuil 	v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
45614735190SHans Verkuil 
45714735190SHans Verkuil 	state->chip_version = ths8200_read(sd, THS8200_VERSION);
45814735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
45914735190SHans Verkuil 
46014735190SHans Verkuil 	ths8200_core_init(sd);
46114735190SHans Verkuil 
462ed29f894SLad, Prabhakar 	error = v4l2_async_register_subdev(&state->sd);
463ed29f894SLad, Prabhakar 	if (error)
464ed29f894SLad, Prabhakar 		return error;
465ed29f894SLad, Prabhakar 
46614735190SHans Verkuil 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
46714735190SHans Verkuil 		  client->addr << 1, client->adapter->name);
46814735190SHans Verkuil 
46914735190SHans Verkuil 	return 0;
47014735190SHans Verkuil }
47114735190SHans Verkuil 
47214735190SHans Verkuil static int ths8200_remove(struct i2c_client *client)
47314735190SHans Verkuil {
47414735190SHans Verkuil 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
475ed29f894SLad, Prabhakar 	struct ths8200_state *decoder = to_state(sd);
47614735190SHans Verkuil 
47714735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
47814735190SHans Verkuil 		 client->addr << 1, client->adapter->name);
47914735190SHans Verkuil 
48014735190SHans Verkuil 	ths8200_s_power(sd, false);
481ed29f894SLad, Prabhakar 	v4l2_async_unregister_subdev(&decoder->sd);
48214735190SHans Verkuil 	v4l2_device_unregister_subdev(sd);
48314735190SHans Verkuil 
48414735190SHans Verkuil 	return 0;
48514735190SHans Verkuil }
48614735190SHans Verkuil 
48714735190SHans Verkuil static struct i2c_device_id ths8200_id[] = {
48814735190SHans Verkuil 	{ "ths8200", 0 },
48914735190SHans Verkuil 	{},
49014735190SHans Verkuil };
49114735190SHans Verkuil MODULE_DEVICE_TABLE(i2c, ths8200_id);
49214735190SHans Verkuil 
4930fb0f8f5SLad, Prabhakar #if IS_ENABLED(CONFIG_OF)
4940fb0f8f5SLad, Prabhakar static const struct of_device_id ths8200_of_match[] = {
4950fb0f8f5SLad, Prabhakar 	{ .compatible = "ti,ths8200", },
4960fb0f8f5SLad, Prabhakar 	{ /* sentinel */ },
4970fb0f8f5SLad, Prabhakar };
4980fb0f8f5SLad, Prabhakar MODULE_DEVICE_TABLE(of, ths8200_of_match);
4990fb0f8f5SLad, Prabhakar #endif
5000fb0f8f5SLad, Prabhakar 
50114735190SHans Verkuil static struct i2c_driver ths8200_driver = {
50214735190SHans Verkuil 	.driver = {
50314735190SHans Verkuil 		.owner = THIS_MODULE,
50414735190SHans Verkuil 		.name = "ths8200",
5050fb0f8f5SLad, Prabhakar 		.of_match_table = of_match_ptr(ths8200_of_match),
50614735190SHans Verkuil 	},
50714735190SHans Verkuil 	.probe = ths8200_probe,
50814735190SHans Verkuil 	.remove = ths8200_remove,
50914735190SHans Verkuil 	.id_table = ths8200_id,
51014735190SHans Verkuil };
51114735190SHans Verkuil 
51214735190SHans Verkuil module_i2c_driver(ths8200_driver);
513