xref: /linux/drivers/media/i2c/ths8200.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 },
522912289aSHans Verkuil 	V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1080, 25000000, 148500000,
5383291f78SGianluca Gennari 		V4L2_DV_BT_STD_CEA861, V4L2_DV_BT_CAP_PROGRESSIVE)
5414735190SHans Verkuil };
5514735190SHans Verkuil 
to_state(struct v4l2_subdev * sd)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 
htotal(const struct v4l2_bt_timings * t)6114735190SHans Verkuil static inline unsigned htotal(const struct v4l2_bt_timings *t)
6214735190SHans Verkuil {
63eacf8f9aSHans Verkuil 	return V4L2_DV_BT_FRAME_WIDTH(t);
6414735190SHans Verkuil }
6514735190SHans Verkuil 
vtotal(const struct v4l2_bt_timings * t)6614735190SHans Verkuil static inline unsigned vtotal(const struct v4l2_bt_timings *t)
6714735190SHans Verkuil {
68eacf8f9aSHans Verkuil 	return V4L2_DV_BT_FRAME_HEIGHT(t);
6914735190SHans Verkuil }
7014735190SHans Verkuil 
ths8200_read(struct v4l2_subdev * sd,u8 reg)7114735190SHans Verkuil static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
7214735190SHans Verkuil {
7314735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
7414735190SHans Verkuil 
7514735190SHans Verkuil 	return i2c_smbus_read_byte_data(client, reg);
7614735190SHans Verkuil }
7714735190SHans Verkuil 
ths8200_write(struct v4l2_subdev * sd,u8 reg,u8 val)7814735190SHans Verkuil static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
7914735190SHans Verkuil {
8014735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
8114735190SHans Verkuil 	int ret;
8214735190SHans Verkuil 	int i;
8314735190SHans Verkuil 
8414735190SHans Verkuil 	for (i = 0; i < 3; i++) {
8514735190SHans Verkuil 		ret = i2c_smbus_write_byte_data(client, reg, val);
8614735190SHans Verkuil 		if (ret == 0)
8714735190SHans Verkuil 			return 0;
8814735190SHans Verkuil 	}
8914735190SHans Verkuil 	v4l2_err(sd, "I2C Write Problem\n");
9014735190SHans Verkuil 	return ret;
9114735190SHans Verkuil }
9214735190SHans Verkuil 
9314735190SHans Verkuil /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
9414735190SHans Verkuil  * and then the value-mask (to be OR-ed).
9514735190SHans Verkuil  */
9614735190SHans Verkuil static inline void
ths8200_write_and_or(struct v4l2_subdev * sd,u8 reg,uint8_t clr_mask,uint8_t val_mask)9714735190SHans Verkuil ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
9814735190SHans Verkuil 		     uint8_t clr_mask, uint8_t val_mask)
9914735190SHans Verkuil {
10014735190SHans Verkuil 	ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
10114735190SHans Verkuil }
10214735190SHans Verkuil 
10314735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
10414735190SHans Verkuil 
ths8200_g_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)10514735190SHans Verkuil static int ths8200_g_register(struct v4l2_subdev *sd,
10614735190SHans Verkuil 			      struct v4l2_dbg_register *reg)
10714735190SHans Verkuil {
10814735190SHans Verkuil 	reg->val = ths8200_read(sd, reg->reg & 0xff);
10914735190SHans Verkuil 	reg->size = 1;
11014735190SHans Verkuil 
11114735190SHans Verkuil 	return 0;
11214735190SHans Verkuil }
11314735190SHans Verkuil 
ths8200_s_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)11414735190SHans Verkuil static int ths8200_s_register(struct v4l2_subdev *sd,
11514735190SHans Verkuil 			      const struct v4l2_dbg_register *reg)
11614735190SHans Verkuil {
11714735190SHans Verkuil 	ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
11814735190SHans Verkuil 
11914735190SHans Verkuil 	return 0;
12014735190SHans Verkuil }
12114735190SHans Verkuil #endif
12214735190SHans Verkuil 
ths8200_log_status(struct v4l2_subdev * sd)12314735190SHans Verkuil static int ths8200_log_status(struct v4l2_subdev *sd)
12414735190SHans Verkuil {
12514735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
12614735190SHans Verkuil 	uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
12714735190SHans Verkuil 
12814735190SHans Verkuil 	v4l2_info(sd, "----- Chip status -----\n");
12914735190SHans Verkuil 	v4l2_info(sd, "version: %u\n", state->chip_version);
13014735190SHans Verkuil 	v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
13114735190SHans Verkuil 	v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
13214735190SHans Verkuil 	v4l2_info(sd, "test pattern: %s\n",
13314735190SHans Verkuil 		  (reg_03 & 0x20) ? "enabled" : "disabled");
13414735190SHans Verkuil 	v4l2_info(sd, "format: %ux%u\n",
13514735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
13614735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
13714735190SHans Verkuil 		  (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
13814735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
13911d034c8SHans Verkuil 	v4l2_print_dv_timings(sd->name, "Configured format:",
14011d034c8SHans Verkuil 			      &state->dv_timings, true);
14114735190SHans Verkuil 	return 0;
14214735190SHans Verkuil }
14314735190SHans Verkuil 
14414735190SHans Verkuil /* Power up/down ths8200 */
ths8200_s_power(struct v4l2_subdev * sd,int on)14514735190SHans Verkuil static int ths8200_s_power(struct v4l2_subdev *sd, int on)
14614735190SHans Verkuil {
14714735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
14814735190SHans Verkuil 
14914735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
15014735190SHans Verkuil 
15114735190SHans Verkuil 	state->power_on = on;
15214735190SHans Verkuil 
15314735190SHans Verkuil 	/* Power up/down - leave in reset state until input video is present */
15414735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
15514735190SHans Verkuil 
15614735190SHans Verkuil 	return 0;
15714735190SHans Verkuil }
15814735190SHans Verkuil 
15914735190SHans Verkuil static const struct v4l2_subdev_core_ops ths8200_core_ops = {
16014735190SHans Verkuil 	.log_status = ths8200_log_status,
16114735190SHans Verkuil 	.s_power = ths8200_s_power,
16214735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
16314735190SHans Verkuil 	.g_register = ths8200_g_register,
16414735190SHans Verkuil 	.s_register = ths8200_s_register,
16514735190SHans Verkuil #endif
16614735190SHans Verkuil };
16714735190SHans Verkuil 
16814735190SHans Verkuil /* -----------------------------------------------------------------------------
16914735190SHans Verkuil  * V4L2 subdev video operations
17014735190SHans Verkuil  */
17114735190SHans Verkuil 
ths8200_s_stream(struct v4l2_subdev * sd,int enable)17214735190SHans Verkuil static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
17314735190SHans Verkuil {
17414735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
17514735190SHans Verkuil 
17614735190SHans Verkuil 	if (enable && !state->power_on)
17714735190SHans Verkuil 		ths8200_s_power(sd, true);
17814735190SHans Verkuil 
17914735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
18014735190SHans Verkuil 			     (enable ? 0x01 : 0x00));
18114735190SHans Verkuil 
18214735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: %sable\n",
18314735190SHans Verkuil 		 __func__, (enable ? "en" : "dis"));
18414735190SHans Verkuil 
18514735190SHans Verkuil 	return 0;
18614735190SHans Verkuil }
18714735190SHans Verkuil 
ths8200_core_init(struct v4l2_subdev * sd)18814735190SHans Verkuil static void ths8200_core_init(struct v4l2_subdev *sd)
18914735190SHans Verkuil {
19014735190SHans Verkuil 	/* setup clocks */
19114735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
19214735190SHans Verkuil 
19314735190SHans Verkuil 	/**** Data path control (DATA) ****/
19414735190SHans Verkuil 	/* Set FSADJ 700 mV,
19514735190SHans Verkuil 	 * bypass 422-444 interpolation,
19614735190SHans Verkuil 	 * input format 30 bit RGB444
19714735190SHans Verkuil 	 */
19814735190SHans Verkuil 	ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
19914735190SHans Verkuil 
20014735190SHans Verkuil 	/* DTG Mode (Video blocked during blanking
20114735190SHans Verkuil 	 * VESA slave
20214735190SHans Verkuil 	 */
20314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
20414735190SHans Verkuil 
20514735190SHans Verkuil 	/**** Display Timing Generator Control, Part 1 (DTG1). ****/
20614735190SHans Verkuil 
20714735190SHans Verkuil 	/* Disable embedded syncs on the output by setting
20814735190SHans Verkuil 	 * the amplitude to zero for all channels.
20914735190SHans Verkuil 	 */
2108a027fafSMartin Bugge 	ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x00);
2118a027fafSMartin Bugge 	ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x00);
21214735190SHans Verkuil }
21314735190SHans Verkuil 
ths8200_setup(struct v4l2_subdev * sd,struct v4l2_bt_timings * bt)21414735190SHans Verkuil static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
21514735190SHans Verkuil {
21614735190SHans Verkuil 	uint8_t polarity = 0;
21714735190SHans Verkuil 	uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
21814735190SHans Verkuil 	uint16_t line_start_front_porch  = (vtotal(bt) - bt->vfrontporch);
21914735190SHans Verkuil 
22014735190SHans Verkuil 	/*** System ****/
22114735190SHans Verkuil 	/* Set chip in reset while it is configured */
22214735190SHans Verkuil 	ths8200_s_stream(sd, false);
22314735190SHans Verkuil 
22414735190SHans Verkuil 	/* configure video output timings */
22514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
22614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
22714735190SHans Verkuil 
22814735190SHans Verkuil 	/* Zero for progressive scan formats.*/
22914735190SHans Verkuil 	if (!bt->interlaced)
23014735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
23114735190SHans Verkuil 
23214735190SHans Verkuil 	/* Distance from leading edge of h sync to start of active video.
23314735190SHans Verkuil 	 * MSB in 0x2b
23414735190SHans Verkuil 	 */
23514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
23614735190SHans Verkuil 		      (bt->hbackporch + bt->hsync) & 0xff);
23714735190SHans Verkuil 	/* Zero for SDTV-mode. MSB in 0x2b */
23814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
23914735190SHans Verkuil 	/*
24014735190SHans Verkuil 	 * MSB for dtg1_spec(d/e/h). See comment for
24114735190SHans Verkuil 	 * corresponding LSB registers.
24214735190SHans Verkuil 	 */
24314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
24414735190SHans Verkuil 		      ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
24514735190SHans Verkuil 
24614735190SHans Verkuil 	/* h front porch */
24714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
24814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
24914735190SHans Verkuil 		      ((bt->hfrontporch) & 0x700) >> 8);
25014735190SHans Verkuil 
25114735190SHans Verkuil 	/* Half the line length. Used to calculate SDTV line types. */
25214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
25314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
25414735190SHans Verkuil 		      ((htotal(bt)/2) >> 8) & 0x0f);
25514735190SHans Verkuil 
25614735190SHans Verkuil 	/* Total pixels per line (ex. 720p: 1650) */
25714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
25814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
25914735190SHans Verkuil 
26014735190SHans Verkuil 	/* Frame height and field height */
26114735190SHans Verkuil 	/* Field height should be programmed higher than frame_size for
26214735190SHans Verkuil 	 * progressive scan formats
26314735190SHans Verkuil 	 */
26414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
26514735190SHans Verkuil 		      ((vtotal(bt) >> 4) & 0xf0) + 0x7);
26614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
26714735190SHans Verkuil 
26814735190SHans Verkuil 	/* Should be programmed higher than frame_size
26914735190SHans Verkuil 	 * for progressive formats
27014735190SHans Verkuil 	 */
27114735190SHans Verkuil 	if (!bt->interlaced)
27214735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
27314735190SHans Verkuil 
27414735190SHans Verkuil 	/**** Display Timing Generator Control, Part 2 (DTG2). ****/
27514735190SHans Verkuil 	/* Set breakpoint line numbers and types
27614735190SHans Verkuil 	 * THS8200 generates line types with different properties. A line type
27714735190SHans Verkuil 	 * that sets all the RGB-outputs to zero is used in the blanking areas,
27814735190SHans Verkuil 	 * while a line type that enable the RGB-outputs is used in active video
27914735190SHans Verkuil 	 * area. The line numbers for start of active video, start of front
28014735190SHans Verkuil 	 * porch and after the last line in the frame must be set with the
28114735190SHans Verkuil 	 * corresponding line types.
28214735190SHans Verkuil 	 *
28314735190SHans Verkuil 	 * Line types:
28414735190SHans Verkuil 	 * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
28514735190SHans Verkuil 	 *       Used in blanking area.
28614735190SHans Verkuil 	 * 0x0 - Active video: Video data is always passed. Used in active
28714735190SHans Verkuil 	 *       video area.
28814735190SHans Verkuil 	 */
28914735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
29014735190SHans Verkuil 			     ((line_start_active_video >> 4) & 0x70) +
29114735190SHans Verkuil 			     ((line_start_front_porch >> 8) & 0x07));
29214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
29314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
29414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
29514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
29614735190SHans Verkuil 
29714735190SHans Verkuil 	/* line types */
29814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
29914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
30014735190SHans Verkuil 
30114735190SHans Verkuil 	/* h sync width transmitted */
30214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
30314735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
30414735190SHans Verkuil 			     (bt->hsync >> 2) & 0xc0);
30514735190SHans Verkuil 
30614735190SHans Verkuil 	/* The pixel value h sync is asserted on */
30714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
30814735190SHans Verkuil 			     (htotal(bt) >> 8) & 0x1f);
30914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
31014735190SHans Verkuil 
3112dfb1c93SMartin Bugge 	/* v sync width transmitted (must add 1 to get correct output) */
3122dfb1c93SMartin Bugge 	ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync + 1) & 0xff);
31314735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
3142dfb1c93SMartin Bugge 			     ((bt->vsync + 1) >> 2) & 0xc0);
31514735190SHans Verkuil 
3162dfb1c93SMartin Bugge 	/* The pixel value v sync is asserted on (must add 1 to get correct output) */
31714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
3182dfb1c93SMartin Bugge 			     ((vtotal(bt) + 1) >> 8) & 0x7);
3192dfb1c93SMartin Bugge 	ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt) + 1);
32014735190SHans Verkuil 
32114735190SHans Verkuil 	/* For progressive video vlength2 must be set to all 0 and vdly2 must
32214735190SHans Verkuil 	 * be set to all 1.
32314735190SHans Verkuil 	 */
32414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
32514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
32614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
32714735190SHans Verkuil 
32814735190SHans Verkuil 	/* Internal delay factors to synchronize the sync pulses and the data */
3292dfb1c93SMartin Bugge 	/* Experimental values delays (hor 0, ver 0) */
3302dfb1c93SMartin Bugge 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, 0);
3312dfb1c93SMartin Bugge 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, 0);
33214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
3332dfb1c93SMartin Bugge 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 0);
33414735190SHans Verkuil 
33514735190SHans Verkuil 	/* Polarity of received and transmitted sync signals */
33614735190SHans Verkuil 	if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
33714735190SHans Verkuil 		polarity |= 0x01; /* HS_IN */
33814735190SHans Verkuil 		polarity |= 0x08; /* HS_OUT */
33914735190SHans Verkuil 	}
34014735190SHans Verkuil 	if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
34114735190SHans Verkuil 		polarity |= 0x02; /* VS_IN */
34214735190SHans Verkuil 		polarity |= 0x10; /* VS_OUT */
34314735190SHans Verkuil 	}
34414735190SHans Verkuil 
34514735190SHans Verkuil 	/* RGB mode, no embedded timings */
34614735190SHans Verkuil 	/* Timing of video input bus is derived from HS, VS, and FID dedicated
34714735190SHans Verkuil 	 * inputs
34814735190SHans Verkuil 	 */
34900b9f51eSMartin Bugge 	ths8200_write(sd, THS8200_DTG2_CNTL, 0x44 | polarity);
35014735190SHans Verkuil 
35114735190SHans Verkuil 	/* leave reset */
35214735190SHans Verkuil 	ths8200_s_stream(sd, true);
35314735190SHans Verkuil 
35414735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
35514735190SHans Verkuil 		 "horizontal: front porch %d, back porch %d, sync %d\n"
35614735190SHans Verkuil 		 "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
35714735190SHans Verkuil 		 polarity, bt->hfrontporch, bt->hbackporch,
35814735190SHans Verkuil 		 bt->hsync, bt->vsync);
35914735190SHans Verkuil }
36014735190SHans Verkuil 
ths8200_s_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)361695cbc75SPaweł Anikiel static int ths8200_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
36214735190SHans Verkuil 				struct v4l2_dv_timings *timings)
36314735190SHans Verkuil {
36414735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
36514735190SHans Verkuil 
36614735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
36714735190SHans Verkuil 
368695cbc75SPaweł Anikiel 	if (pad != 0)
369695cbc75SPaweł Anikiel 		return -EINVAL;
370695cbc75SPaweł Anikiel 
371b8f0fff4SHans Verkuil 	if (!v4l2_valid_dv_timings(timings, &ths8200_timings_cap,
372b8f0fff4SHans Verkuil 				NULL, NULL))
37314735190SHans Verkuil 		return -EINVAL;
37414735190SHans Verkuil 
375b8f0fff4SHans Verkuil 	if (!v4l2_find_dv_timings_cap(timings, &ths8200_timings_cap, 10,
376b8f0fff4SHans Verkuil 				NULL, NULL)) {
37714735190SHans Verkuil 		v4l2_dbg(1, debug, sd, "Unsupported format\n");
37814735190SHans Verkuil 		return -EINVAL;
37914735190SHans Verkuil 	}
38014735190SHans Verkuil 
38114735190SHans Verkuil 	timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
38214735190SHans Verkuil 
38314735190SHans Verkuil 	/* save timings */
38414735190SHans Verkuil 	state->dv_timings = *timings;
38514735190SHans Verkuil 
38614735190SHans Verkuil 	ths8200_setup(sd, &timings->bt);
38714735190SHans Verkuil 
38814735190SHans Verkuil 	return 0;
38914735190SHans Verkuil }
39014735190SHans Verkuil 
ths8200_g_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)391695cbc75SPaweł Anikiel static int ths8200_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
39214735190SHans Verkuil 				struct v4l2_dv_timings *timings)
39314735190SHans Verkuil {
39414735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
39514735190SHans Verkuil 
39614735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
39714735190SHans Verkuil 
398695cbc75SPaweł Anikiel 	if (pad != 0)
399695cbc75SPaweł Anikiel 		return -EINVAL;
400695cbc75SPaweł Anikiel 
40114735190SHans Verkuil 	*timings = state->dv_timings;
40214735190SHans Verkuil 
40314735190SHans Verkuil 	return 0;
40414735190SHans Verkuil }
40514735190SHans Verkuil 
ths8200_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)40614735190SHans Verkuil static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
40714735190SHans Verkuil 				   struct v4l2_enum_dv_timings *timings)
40814735190SHans Verkuil {
409553bca82SLaurent Pinchart 	if (timings->pad != 0)
410553bca82SLaurent Pinchart 		return -EINVAL;
411553bca82SLaurent Pinchart 
412b8f0fff4SHans Verkuil 	return v4l2_enum_dv_timings_cap(timings, &ths8200_timings_cap,
413b8f0fff4SHans Verkuil 			NULL, NULL);
41414735190SHans Verkuil }
41514735190SHans Verkuil 
ths8200_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)41614735190SHans Verkuil static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
41714735190SHans Verkuil 				  struct v4l2_dv_timings_cap *cap)
41814735190SHans Verkuil {
419553bca82SLaurent Pinchart 	if (cap->pad != 0)
420553bca82SLaurent Pinchart 		return -EINVAL;
421553bca82SLaurent Pinchart 
42204164904SHans Verkuil 	*cap = ths8200_timings_cap;
42314735190SHans Verkuil 	return 0;
42414735190SHans Verkuil }
42514735190SHans Verkuil 
42614735190SHans Verkuil /* Specific video subsystem operation handlers */
42714735190SHans Verkuil static const struct v4l2_subdev_video_ops ths8200_video_ops = {
42814735190SHans Verkuil 	.s_stream = ths8200_s_stream,
42914735190SHans Verkuil };
43014735190SHans Verkuil 
431553bca82SLaurent Pinchart static const struct v4l2_subdev_pad_ops ths8200_pad_ops = {
432695cbc75SPaweł Anikiel 	.s_dv_timings = ths8200_s_dv_timings,
433695cbc75SPaweł Anikiel 	.g_dv_timings = ths8200_g_dv_timings,
434553bca82SLaurent Pinchart 	.enum_dv_timings = ths8200_enum_dv_timings,
435553bca82SLaurent Pinchart 	.dv_timings_cap = ths8200_dv_timings_cap,
436553bca82SLaurent Pinchart };
437553bca82SLaurent Pinchart 
43814735190SHans Verkuil /* V4L2 top level operation handlers */
43914735190SHans Verkuil static const struct v4l2_subdev_ops ths8200_ops = {
44014735190SHans Verkuil 	.core  = &ths8200_core_ops,
44114735190SHans Verkuil 	.video = &ths8200_video_ops,
442553bca82SLaurent Pinchart 	.pad = &ths8200_pad_ops,
44314735190SHans Verkuil };
44414735190SHans Verkuil 
ths8200_probe(struct i2c_client * client)445e6714993SKieran Bingham static int ths8200_probe(struct i2c_client *client)
44614735190SHans Verkuil {
44714735190SHans Verkuil 	struct ths8200_state *state;
44814735190SHans Verkuil 	struct v4l2_subdev *sd;
449ed29f894SLad, Prabhakar 	int error;
45014735190SHans Verkuil 
45114735190SHans Verkuil 	/* Check if the adapter supports the needed features */
45214735190SHans Verkuil 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
45314735190SHans Verkuil 		return -EIO;
45414735190SHans Verkuil 
45514735190SHans Verkuil 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
45614735190SHans Verkuil 	if (!state)
45714735190SHans Verkuil 		return -ENOMEM;
45814735190SHans Verkuil 
45914735190SHans Verkuil 	sd = &state->sd;
46014735190SHans Verkuil 	v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
46114735190SHans Verkuil 
46214735190SHans Verkuil 	state->chip_version = ths8200_read(sd, THS8200_VERSION);
46314735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
46414735190SHans Verkuil 
46514735190SHans Verkuil 	ths8200_core_init(sd);
46614735190SHans Verkuil 
467ed29f894SLad, Prabhakar 	error = v4l2_async_register_subdev(&state->sd);
468ed29f894SLad, Prabhakar 	if (error)
469ed29f894SLad, Prabhakar 		return error;
470ed29f894SLad, Prabhakar 
47114735190SHans Verkuil 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
47214735190SHans Verkuil 		  client->addr << 1, client->adapter->name);
47314735190SHans Verkuil 
47414735190SHans Verkuil 	return 0;
47514735190SHans Verkuil }
47614735190SHans Verkuil 
ths8200_remove(struct i2c_client * client)477ed5c2f5fSUwe Kleine-König static void ths8200_remove(struct i2c_client *client)
47814735190SHans Verkuil {
47914735190SHans Verkuil 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
480ed29f894SLad, Prabhakar 	struct ths8200_state *decoder = to_state(sd);
48114735190SHans Verkuil 
48214735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
48314735190SHans Verkuil 		 client->addr << 1, client->adapter->name);
48414735190SHans Verkuil 
48514735190SHans Verkuil 	ths8200_s_power(sd, false);
486ed29f894SLad, Prabhakar 	v4l2_async_unregister_subdev(&decoder->sd);
48714735190SHans Verkuil }
48814735190SHans Verkuil 
489e0ee62c4SArvind Yadav static const struct i2c_device_id ths8200_id[] = {
490*cc4cbd4bSUwe Kleine-König 	{ "ths8200" },
491*cc4cbd4bSUwe Kleine-König 	{}
49214735190SHans Verkuil };
49314735190SHans Verkuil MODULE_DEVICE_TABLE(i2c, ths8200_id);
49414735190SHans Verkuil 
4950fb0f8f5SLad, Prabhakar #if IS_ENABLED(CONFIG_OF)
4960fb0f8f5SLad, Prabhakar static const struct of_device_id ths8200_of_match[] = {
4970fb0f8f5SLad, Prabhakar 	{ .compatible = "ti,ths8200", },
4980fb0f8f5SLad, Prabhakar 	{ /* sentinel */ },
4990fb0f8f5SLad, Prabhakar };
5000fb0f8f5SLad, Prabhakar MODULE_DEVICE_TABLE(of, ths8200_of_match);
5010fb0f8f5SLad, Prabhakar #endif
5020fb0f8f5SLad, Prabhakar 
50314735190SHans Verkuil static struct i2c_driver ths8200_driver = {
50414735190SHans Verkuil 	.driver = {
50514735190SHans Verkuil 		.name = "ths8200",
5060fb0f8f5SLad, Prabhakar 		.of_match_table = of_match_ptr(ths8200_of_match),
50714735190SHans Verkuil 	},
508aaeb31c0SUwe Kleine-König 	.probe = ths8200_probe,
50914735190SHans Verkuil 	.remove = ths8200_remove,
51014735190SHans Verkuil 	.id_table = ths8200_id,
51114735190SHans Verkuil };
51214735190SHans Verkuil 
51314735190SHans Verkuil module_i2c_driver(ths8200_driver);
514