xref: /linux/drivers/media/i2c/ths8200.c (revision ed29f894970065402e988088a19698f252b80144)
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>
2214735190SHans Verkuil #include <linux/v4l2-dv-timings.h>
2314735190SHans Verkuil 
24*ed29f894SLad, Prabhakar #include <media/v4l2-async.h>
2514735190SHans Verkuil #include <media/v4l2-device.h>
2614735190SHans Verkuil 
2714735190SHans Verkuil #include "ths8200_regs.h"
2814735190SHans Verkuil 
2914735190SHans Verkuil static int debug;
3014735190SHans Verkuil module_param(debug, int, 0644);
3114735190SHans Verkuil MODULE_PARM_DESC(debug, "debug level (0-2)");
3214735190SHans Verkuil 
3314735190SHans Verkuil MODULE_DESCRIPTION("Texas Instruments THS8200 video encoder driver");
3414735190SHans Verkuil MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
3514735190SHans Verkuil MODULE_AUTHOR("Martin Bugge <martin.bugge@cisco.com>");
3614735190SHans Verkuil MODULE_LICENSE("GPL v2");
3714735190SHans Verkuil 
3814735190SHans Verkuil struct ths8200_state {
3914735190SHans Verkuil 	struct v4l2_subdev sd;
4014735190SHans Verkuil 	uint8_t chip_version;
4114735190SHans Verkuil 	/* Is the ths8200 powered on? */
4214735190SHans Verkuil 	bool power_on;
4314735190SHans Verkuil 	struct v4l2_dv_timings dv_timings;
4414735190SHans Verkuil };
4514735190SHans Verkuil 
4614735190SHans Verkuil static const struct v4l2_dv_timings ths8200_timings[] = {
4714735190SHans Verkuil 	V4L2_DV_BT_CEA_720X480P59_94,
4814735190SHans Verkuil 	V4L2_DV_BT_CEA_1280X720P24,
4914735190SHans Verkuil 	V4L2_DV_BT_CEA_1280X720P25,
5014735190SHans Verkuil 	V4L2_DV_BT_CEA_1280X720P30,
5114735190SHans Verkuil 	V4L2_DV_BT_CEA_1280X720P50,
5214735190SHans Verkuil 	V4L2_DV_BT_CEA_1280X720P60,
5314735190SHans Verkuil 	V4L2_DV_BT_CEA_1920X1080P24,
5414735190SHans Verkuil 	V4L2_DV_BT_CEA_1920X1080P25,
5514735190SHans Verkuil 	V4L2_DV_BT_CEA_1920X1080P30,
5614735190SHans Verkuil 	V4L2_DV_BT_CEA_1920X1080P50,
5714735190SHans Verkuil 	V4L2_DV_BT_CEA_1920X1080P60,
5814735190SHans Verkuil };
5914735190SHans Verkuil 
6014735190SHans Verkuil static inline struct ths8200_state *to_state(struct v4l2_subdev *sd)
6114735190SHans Verkuil {
6214735190SHans Verkuil 	return container_of(sd, struct ths8200_state, sd);
6314735190SHans Verkuil }
6414735190SHans Verkuil 
6514735190SHans Verkuil static inline unsigned hblanking(const struct v4l2_bt_timings *t)
6614735190SHans Verkuil {
6714735190SHans Verkuil 	return t->hfrontporch + t->hsync + t->hbackporch;
6814735190SHans Verkuil }
6914735190SHans Verkuil 
7014735190SHans Verkuil static inline unsigned htotal(const struct v4l2_bt_timings *t)
7114735190SHans Verkuil {
7214735190SHans Verkuil 	return t->width + t->hfrontporch + t->hsync + t->hbackporch;
7314735190SHans Verkuil }
7414735190SHans Verkuil 
7514735190SHans Verkuil static inline unsigned vblanking(const struct v4l2_bt_timings *t)
7614735190SHans Verkuil {
7714735190SHans Verkuil 	return t->vfrontporch + t->vsync + t->vbackporch;
7814735190SHans Verkuil }
7914735190SHans Verkuil 
8014735190SHans Verkuil static inline unsigned vtotal(const struct v4l2_bt_timings *t)
8114735190SHans Verkuil {
8214735190SHans Verkuil 	return t->height + t->vfrontporch + t->vsync + t->vbackporch;
8314735190SHans Verkuil }
8414735190SHans Verkuil 
8514735190SHans Verkuil static int ths8200_read(struct v4l2_subdev *sd, u8 reg)
8614735190SHans Verkuil {
8714735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
8814735190SHans Verkuil 
8914735190SHans Verkuil 	return i2c_smbus_read_byte_data(client, reg);
9014735190SHans Verkuil }
9114735190SHans Verkuil 
9214735190SHans Verkuil static int ths8200_write(struct v4l2_subdev *sd, u8 reg, u8 val)
9314735190SHans Verkuil {
9414735190SHans Verkuil 	struct i2c_client *client = v4l2_get_subdevdata(sd);
9514735190SHans Verkuil 	int ret;
9614735190SHans Verkuil 	int i;
9714735190SHans Verkuil 
9814735190SHans Verkuil 	for (i = 0; i < 3; i++) {
9914735190SHans Verkuil 		ret = i2c_smbus_write_byte_data(client, reg, val);
10014735190SHans Verkuil 		if (ret == 0)
10114735190SHans Verkuil 			return 0;
10214735190SHans Verkuil 	}
10314735190SHans Verkuil 	v4l2_err(sd, "I2C Write Problem\n");
10414735190SHans Verkuil 	return ret;
10514735190SHans Verkuil }
10614735190SHans Verkuil 
10714735190SHans Verkuil /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
10814735190SHans Verkuil  * and then the value-mask (to be OR-ed).
10914735190SHans Verkuil  */
11014735190SHans Verkuil static inline void
11114735190SHans Verkuil ths8200_write_and_or(struct v4l2_subdev *sd, u8 reg,
11214735190SHans Verkuil 		     uint8_t clr_mask, uint8_t val_mask)
11314735190SHans Verkuil {
11414735190SHans Verkuil 	ths8200_write(sd, reg, (ths8200_read(sd, reg) & clr_mask) | val_mask);
11514735190SHans Verkuil }
11614735190SHans Verkuil 
11714735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
11814735190SHans Verkuil 
11914735190SHans Verkuil static int ths8200_g_register(struct v4l2_subdev *sd,
12014735190SHans Verkuil 			      struct v4l2_dbg_register *reg)
12114735190SHans Verkuil {
12214735190SHans Verkuil 	reg->val = ths8200_read(sd, reg->reg & 0xff);
12314735190SHans Verkuil 	reg->size = 1;
12414735190SHans Verkuil 
12514735190SHans Verkuil 	return 0;
12614735190SHans Verkuil }
12714735190SHans Verkuil 
12814735190SHans Verkuil static int ths8200_s_register(struct v4l2_subdev *sd,
12914735190SHans Verkuil 			      const struct v4l2_dbg_register *reg)
13014735190SHans Verkuil {
13114735190SHans Verkuil 	ths8200_write(sd, reg->reg & 0xff, reg->val & 0xff);
13214735190SHans Verkuil 
13314735190SHans Verkuil 	return 0;
13414735190SHans Verkuil }
13514735190SHans Verkuil #endif
13614735190SHans Verkuil 
13714735190SHans Verkuil static void ths8200_print_timings(struct v4l2_subdev *sd,
13814735190SHans Verkuil 				  struct v4l2_dv_timings *timings,
13914735190SHans Verkuil 				  const char *txt, bool detailed)
14014735190SHans Verkuil {
14114735190SHans Verkuil 	struct v4l2_bt_timings *bt = &timings->bt;
14214735190SHans Verkuil 	u32 htot, vtot;
14314735190SHans Verkuil 
14414735190SHans Verkuil 	if (timings->type != V4L2_DV_BT_656_1120)
14514735190SHans Verkuil 		return;
14614735190SHans Verkuil 
14714735190SHans Verkuil 	htot = htotal(bt);
14814735190SHans Verkuil 	vtot = vtotal(bt);
14914735190SHans Verkuil 
15014735190SHans Verkuil 	v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
15114735190SHans Verkuil 		  txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
15214735190SHans Verkuil 		  (htot * vtot) > 0 ? ((u32)bt->pixelclock / (htot * vtot)) : 0,
15314735190SHans Verkuil 		  htot, vtot);
15414735190SHans Verkuil 
15514735190SHans Verkuil 	if (detailed) {
15614735190SHans Verkuil 		v4l2_info(sd, "    horizontal: fp = %d, %ssync = %d, bp = %d\n",
15714735190SHans Verkuil 			  bt->hfrontporch,
15814735190SHans Verkuil 			  (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
15914735190SHans Verkuil 			  bt->hsync, bt->hbackporch);
16014735190SHans Verkuil 		v4l2_info(sd, "    vertical: fp = %d, %ssync = %d, bp = %d\n",
16114735190SHans Verkuil 			  bt->vfrontporch,
16214735190SHans Verkuil 			  (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
16314735190SHans Verkuil 			  bt->vsync, bt->vbackporch);
16414735190SHans Verkuil 		v4l2_info(sd,
16514735190SHans Verkuil 			  "    pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
16614735190SHans Verkuil 			  bt->pixelclock, bt->flags, bt->standards);
16714735190SHans Verkuil 	}
16814735190SHans Verkuil }
16914735190SHans Verkuil 
17014735190SHans Verkuil static int ths8200_log_status(struct v4l2_subdev *sd)
17114735190SHans Verkuil {
17214735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
17314735190SHans Verkuil 	uint8_t reg_03 = ths8200_read(sd, THS8200_CHIP_CTL);
17414735190SHans Verkuil 
17514735190SHans Verkuil 	v4l2_info(sd, "----- Chip status -----\n");
17614735190SHans Verkuil 	v4l2_info(sd, "version: %u\n", state->chip_version);
17714735190SHans Verkuil 	v4l2_info(sd, "power: %s\n", (reg_03 & 0x0c) ? "off" : "on");
17814735190SHans Verkuil 	v4l2_info(sd, "reset: %s\n", (reg_03 & 0x01) ? "off" : "on");
17914735190SHans Verkuil 	v4l2_info(sd, "test pattern: %s\n",
18014735190SHans Verkuil 		  (reg_03 & 0x20) ? "enabled" : "disabled");
18114735190SHans Verkuil 	v4l2_info(sd, "format: %ux%u\n",
18214735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_MSB) * 256 +
18314735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_PIXEL_CNT_LSB),
18414735190SHans Verkuil 		  (ths8200_read(sd, THS8200_DTG2_LINE_CNT_MSB) & 0x07) * 256 +
18514735190SHans Verkuil 		  ths8200_read(sd, THS8200_DTG2_LINE_CNT_LSB));
18614735190SHans Verkuil 	ths8200_print_timings(sd, &state->dv_timings,
18714735190SHans Verkuil 			      "Configured format:", true);
18814735190SHans Verkuil 
18914735190SHans Verkuil 	return 0;
19014735190SHans Verkuil }
19114735190SHans Verkuil 
19214735190SHans Verkuil /* Power up/down ths8200 */
19314735190SHans Verkuil static int ths8200_s_power(struct v4l2_subdev *sd, int on)
19414735190SHans Verkuil {
19514735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
19614735190SHans Verkuil 
19714735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off");
19814735190SHans Verkuil 
19914735190SHans Verkuil 	state->power_on = on;
20014735190SHans Verkuil 
20114735190SHans Verkuil 	/* Power up/down - leave in reset state until input video is present */
20214735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xf2, (on ? 0x00 : 0x0c));
20314735190SHans Verkuil 
20414735190SHans Verkuil 	return 0;
20514735190SHans Verkuil }
20614735190SHans Verkuil 
20714735190SHans Verkuil static const struct v4l2_subdev_core_ops ths8200_core_ops = {
20814735190SHans Verkuil 	.log_status = ths8200_log_status,
20914735190SHans Verkuil 	.s_power = ths8200_s_power,
21014735190SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
21114735190SHans Verkuil 	.g_register = ths8200_g_register,
21214735190SHans Verkuil 	.s_register = ths8200_s_register,
21314735190SHans Verkuil #endif
21414735190SHans Verkuil };
21514735190SHans Verkuil 
21614735190SHans Verkuil /* -----------------------------------------------------------------------------
21714735190SHans Verkuil  * V4L2 subdev video operations
21814735190SHans Verkuil  */
21914735190SHans Verkuil 
22014735190SHans Verkuil static int ths8200_s_stream(struct v4l2_subdev *sd, int enable)
22114735190SHans Verkuil {
22214735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
22314735190SHans Verkuil 
22414735190SHans Verkuil 	if (enable && !state->power_on)
22514735190SHans Verkuil 		ths8200_s_power(sd, true);
22614735190SHans Verkuil 
22714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0xfe,
22814735190SHans Verkuil 			     (enable ? 0x01 : 0x00));
22914735190SHans Verkuil 
23014735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: %sable\n",
23114735190SHans Verkuil 		 __func__, (enable ? "en" : "dis"));
23214735190SHans Verkuil 
23314735190SHans Verkuil 	return 0;
23414735190SHans Verkuil }
23514735190SHans Verkuil 
23614735190SHans Verkuil static void ths8200_core_init(struct v4l2_subdev *sd)
23714735190SHans Verkuil {
23814735190SHans Verkuil 	/* setup clocks */
23914735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_CHIP_CTL, 0x3f, 0xc0);
24014735190SHans Verkuil 
24114735190SHans Verkuil 	/**** Data path control (DATA) ****/
24214735190SHans Verkuil 	/* Set FSADJ 700 mV,
24314735190SHans Verkuil 	 * bypass 422-444 interpolation,
24414735190SHans Verkuil 	 * input format 30 bit RGB444
24514735190SHans Verkuil 	 */
24614735190SHans Verkuil 	ths8200_write(sd, THS8200_DATA_CNTL, 0x70);
24714735190SHans Verkuil 
24814735190SHans Verkuil 	/* DTG Mode (Video blocked during blanking
24914735190SHans Verkuil 	 * VESA slave
25014735190SHans Verkuil 	 */
25114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_MODE, 0x87);
25214735190SHans Verkuil 
25314735190SHans Verkuil 	/**** Display Timing Generator Control, Part 1 (DTG1). ****/
25414735190SHans Verkuil 
25514735190SHans Verkuil 	/* Disable embedded syncs on the output by setting
25614735190SHans Verkuil 	 * the amplitude to zero for all channels.
25714735190SHans Verkuil 	 */
25814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_Y_SYNC_MSB, 0x2a);
25914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_CBCR_SYNC_MSB, 0x2a);
26014735190SHans Verkuil }
26114735190SHans Verkuil 
26214735190SHans Verkuil static void ths8200_setup(struct v4l2_subdev *sd, struct v4l2_bt_timings *bt)
26314735190SHans Verkuil {
26414735190SHans Verkuil 	uint8_t polarity = 0;
26514735190SHans Verkuil 	uint16_t line_start_active_video = (bt->vsync + bt->vbackporch);
26614735190SHans Verkuil 	uint16_t line_start_front_porch  = (vtotal(bt) - bt->vfrontporch);
26714735190SHans Verkuil 
26814735190SHans Verkuil 	/*** System ****/
26914735190SHans Verkuil 	/* Set chip in reset while it is configured */
27014735190SHans Verkuil 	ths8200_s_stream(sd, false);
27114735190SHans Verkuil 
27214735190SHans Verkuil 	/* configure video output timings */
27314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_A, bt->hsync);
27414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_B, bt->hfrontporch);
27514735190SHans Verkuil 
27614735190SHans Verkuil 	/* Zero for progressive scan formats.*/
27714735190SHans Verkuil 	if (!bt->interlaced)
27814735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_SPEC_C, 0x00);
27914735190SHans Verkuil 
28014735190SHans Verkuil 	/* Distance from leading edge of h sync to start of active video.
28114735190SHans Verkuil 	 * MSB in 0x2b
28214735190SHans Verkuil 	 */
28314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_D_LSB,
28414735190SHans Verkuil 		      (bt->hbackporch + bt->hsync) & 0xff);
28514735190SHans Verkuil 	/* Zero for SDTV-mode. MSB in 0x2b */
28614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_E_LSB, 0x00);
28714735190SHans Verkuil 	/*
28814735190SHans Verkuil 	 * MSB for dtg1_spec(d/e/h). See comment for
28914735190SHans Verkuil 	 * corresponding LSB registers.
29014735190SHans Verkuil 	 */
29114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_DEH_MSB,
29214735190SHans Verkuil 		      ((bt->hbackporch + bt->hsync) & 0x100) >> 1);
29314735190SHans Verkuil 
29414735190SHans Verkuil 	/* h front porch */
29514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_LSB, (bt->hfrontporch) & 0xff);
29614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_K_MSB,
29714735190SHans Verkuil 		      ((bt->hfrontporch) & 0x700) >> 8);
29814735190SHans Verkuil 
29914735190SHans Verkuil 	/* Half the line length. Used to calculate SDTV line types. */
30014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_LSB, (htotal(bt)/2) & 0xff);
30114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_SPEC_G_MSB,
30214735190SHans Verkuil 		      ((htotal(bt)/2) >> 8) & 0x0f);
30314735190SHans Verkuil 
30414735190SHans Verkuil 	/* Total pixels per line (ex. 720p: 1650) */
30514735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_MSB, htotal(bt) >> 8);
30614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_TOT_PIXELS_LSB, htotal(bt) & 0xff);
30714735190SHans Verkuil 
30814735190SHans Verkuil 	/* Frame height and field height */
30914735190SHans Verkuil 	/* Field height should be programmed higher than frame_size for
31014735190SHans Verkuil 	 * progressive scan formats
31114735190SHans Verkuil 	 */
31214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_FIELD_SZ_MSB,
31314735190SHans Verkuil 		      ((vtotal(bt) >> 4) & 0xf0) + 0x7);
31414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG1_FRAME_SZ_LSB, vtotal(bt) & 0xff);
31514735190SHans Verkuil 
31614735190SHans Verkuil 	/* Should be programmed higher than frame_size
31714735190SHans Verkuil 	 * for progressive formats
31814735190SHans Verkuil 	 */
31914735190SHans Verkuil 	if (!bt->interlaced)
32014735190SHans Verkuil 		ths8200_write(sd, THS8200_DTG1_FIELD_SZ_LSB, 0xff);
32114735190SHans Verkuil 
32214735190SHans Verkuil 	/**** Display Timing Generator Control, Part 2 (DTG2). ****/
32314735190SHans Verkuil 	/* Set breakpoint line numbers and types
32414735190SHans Verkuil 	 * THS8200 generates line types with different properties. A line type
32514735190SHans Verkuil 	 * that sets all the RGB-outputs to zero is used in the blanking areas,
32614735190SHans Verkuil 	 * while a line type that enable the RGB-outputs is used in active video
32714735190SHans Verkuil 	 * area. The line numbers for start of active video, start of front
32814735190SHans Verkuil 	 * porch and after the last line in the frame must be set with the
32914735190SHans Verkuil 	 * corresponding line types.
33014735190SHans Verkuil 	 *
33114735190SHans Verkuil 	 * Line types:
33214735190SHans Verkuil 	 * 0x9 - Full normal sync pulse: Blocks data when dtg1_pass is off.
33314735190SHans Verkuil 	 *       Used in blanking area.
33414735190SHans Verkuil 	 * 0x0 - Active video: Video data is always passed. Used in active
33514735190SHans Verkuil 	 *       video area.
33614735190SHans Verkuil 	 */
33714735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_BP1_2_MSB, 0x88,
33814735190SHans Verkuil 			     ((line_start_active_video >> 4) & 0x70) +
33914735190SHans Verkuil 			     ((line_start_front_porch >> 8) & 0x07));
34014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_4_MSB, ((vtotal(bt)) >> 4) & 0x70);
34114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP1_LSB, line_start_active_video & 0xff);
34214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP2_LSB, line_start_front_porch & 0xff);
34314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_BP3_LSB, (vtotal(bt)) & 0xff);
34414735190SHans Verkuil 
34514735190SHans Verkuil 	/* line types */
34614735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE1, 0x90);
34714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_LINETYPE2, 0x90);
34814735190SHans Verkuil 
34914735190SHans Verkuil 	/* h sync width transmitted */
35014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_LSB, bt->hsync & 0xff);
35114735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0x3f,
35214735190SHans Verkuil 			     (bt->hsync >> 2) & 0xc0);
35314735190SHans Verkuil 
35414735190SHans Verkuil 	/* The pixel value h sync is asserted on */
35514735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_HLENGTH_LSB_HDLY_MSB, 0xe0,
35614735190SHans Verkuil 			     (htotal(bt) >> 8) & 0x1f);
35714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HLENGTH_HDLY_LSB, htotal(bt));
35814735190SHans Verkuil 
35914735190SHans Verkuil 	/* v sync width transmitted */
36014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH1_LSB, (bt->vsync) & 0xff);
36114735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0x3f,
36214735190SHans Verkuil 			     ((bt->vsync) >> 2) & 0xc0);
36314735190SHans Verkuil 
36414735190SHans Verkuil 	/* The pixel value v sync is asserted on */
36514735190SHans Verkuil 	ths8200_write_and_or(sd, THS8200_DTG2_VLENGTH1_MSB_VDLY1_MSB, 0xf8,
36614735190SHans Verkuil 			     (vtotal(bt)>>8) & 0x7);
36714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VDLY1_LSB, vtotal(bt));
36814735190SHans Verkuil 
36914735190SHans Verkuil 	/* For progressive video vlength2 must be set to all 0 and vdly2 must
37014735190SHans Verkuil 	 * be set to all 1.
37114735190SHans Verkuil 	 */
37214735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_LSB, 0x00);
37314735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VLENGTH2_MSB_VDLY2_MSB, 0x07);
37414735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VDLY2_LSB, 0xff);
37514735190SHans Verkuil 
37614735190SHans Verkuil 	/* Internal delay factors to synchronize the sync pulses and the data */
37714735190SHans Verkuil 	/* Experimental values delays (hor 4, ver 1) */
37814735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_MSB, (htotal(bt)>>8) & 0x1f);
37914735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_HS_IN_DLY_LSB, (htotal(bt) - 4) & 0xff);
38014735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_MSB, 0);
38114735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_VS_IN_DLY_LSB, 1);
38214735190SHans Verkuil 
38314735190SHans Verkuil 	/* Polarity of received and transmitted sync signals */
38414735190SHans Verkuil 	if (bt->polarities & V4L2_DV_HSYNC_POS_POL) {
38514735190SHans Verkuil 		polarity |= 0x01; /* HS_IN */
38614735190SHans Verkuil 		polarity |= 0x08; /* HS_OUT */
38714735190SHans Verkuil 	}
38814735190SHans Verkuil 	if (bt->polarities & V4L2_DV_VSYNC_POS_POL) {
38914735190SHans Verkuil 		polarity |= 0x02; /* VS_IN */
39014735190SHans Verkuil 		polarity |= 0x10; /* VS_OUT */
39114735190SHans Verkuil 	}
39214735190SHans Verkuil 
39314735190SHans Verkuil 	/* RGB mode, no embedded timings */
39414735190SHans Verkuil 	/* Timing of video input bus is derived from HS, VS, and FID dedicated
39514735190SHans Verkuil 	 * inputs
39614735190SHans Verkuil 	 */
39714735190SHans Verkuil 	ths8200_write(sd, THS8200_DTG2_CNTL, 0x47 | polarity);
39814735190SHans Verkuil 
39914735190SHans Verkuil 	/* leave reset */
40014735190SHans Verkuil 	ths8200_s_stream(sd, true);
40114735190SHans Verkuil 
40214735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s: frame %dx%d, polarity %d\n"
40314735190SHans Verkuil 		 "horizontal: front porch %d, back porch %d, sync %d\n"
40414735190SHans Verkuil 		 "vertical: sync %d\n", __func__, htotal(bt), vtotal(bt),
40514735190SHans Verkuil 		 polarity, bt->hfrontporch, bt->hbackporch,
40614735190SHans Verkuil 		 bt->hsync, bt->vsync);
40714735190SHans Verkuil }
40814735190SHans Verkuil 
40914735190SHans Verkuil static int ths8200_s_dv_timings(struct v4l2_subdev *sd,
41014735190SHans Verkuil 				struct v4l2_dv_timings *timings)
41114735190SHans Verkuil {
41214735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
41314735190SHans Verkuil 	int i;
41414735190SHans Verkuil 
41514735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
41614735190SHans Verkuil 
41714735190SHans Verkuil 	if (timings->type != V4L2_DV_BT_656_1120)
41814735190SHans Verkuil 		return -EINVAL;
41914735190SHans Verkuil 
42014735190SHans Verkuil 	/* TODO Support interlaced formats */
42114735190SHans Verkuil 	if (timings->bt.interlaced) {
42214735190SHans Verkuil 		v4l2_dbg(1, debug, sd, "TODO Support interlaced formats\n");
42314735190SHans Verkuil 		return -EINVAL;
42414735190SHans Verkuil 	}
42514735190SHans Verkuil 
42614735190SHans Verkuil 	for (i = 0; i < ARRAY_SIZE(ths8200_timings); i++) {
42714735190SHans Verkuil 		if (v4l_match_dv_timings(&ths8200_timings[i], timings, 10))
42814735190SHans Verkuil 			break;
42914735190SHans Verkuil 	}
43014735190SHans Verkuil 
43114735190SHans Verkuil 	if (i == ARRAY_SIZE(ths8200_timings)) {
43214735190SHans Verkuil 		v4l2_dbg(1, debug, sd, "Unsupported format\n");
43314735190SHans Verkuil 		return -EINVAL;
43414735190SHans Verkuil 	}
43514735190SHans Verkuil 
43614735190SHans Verkuil 	timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS;
43714735190SHans Verkuil 
43814735190SHans Verkuil 	/* save timings */
43914735190SHans Verkuil 	state->dv_timings = *timings;
44014735190SHans Verkuil 
44114735190SHans Verkuil 	ths8200_setup(sd, &timings->bt);
44214735190SHans Verkuil 
44314735190SHans Verkuil 	return 0;
44414735190SHans Verkuil }
44514735190SHans Verkuil 
44614735190SHans Verkuil static int ths8200_g_dv_timings(struct v4l2_subdev *sd,
44714735190SHans Verkuil 				struct v4l2_dv_timings *timings)
44814735190SHans Verkuil {
44914735190SHans Verkuil 	struct ths8200_state *state = to_state(sd);
45014735190SHans Verkuil 
45114735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s:\n", __func__);
45214735190SHans Verkuil 
45314735190SHans Verkuil 	*timings = state->dv_timings;
45414735190SHans Verkuil 
45514735190SHans Verkuil 	return 0;
45614735190SHans Verkuil }
45714735190SHans Verkuil 
45814735190SHans Verkuil static int ths8200_enum_dv_timings(struct v4l2_subdev *sd,
45914735190SHans Verkuil 				   struct v4l2_enum_dv_timings *timings)
46014735190SHans Verkuil {
46114735190SHans Verkuil 	/* Check requested format index is within range */
46214735190SHans Verkuil 	if (timings->index >= ARRAY_SIZE(ths8200_timings))
46314735190SHans Verkuil 		return -EINVAL;
46414735190SHans Verkuil 
46514735190SHans Verkuil 	timings->timings = ths8200_timings[timings->index];
46614735190SHans Verkuil 
46714735190SHans Verkuil 	return 0;
46814735190SHans Verkuil }
46914735190SHans Verkuil 
47014735190SHans Verkuil static int ths8200_dv_timings_cap(struct v4l2_subdev *sd,
47114735190SHans Verkuil 				  struct v4l2_dv_timings_cap *cap)
47214735190SHans Verkuil {
47314735190SHans Verkuil 	cap->type = V4L2_DV_BT_656_1120;
47414735190SHans Verkuil 	cap->bt.max_width = 1920;
47514735190SHans Verkuil 	cap->bt.max_height = 1080;
47614735190SHans Verkuil 	cap->bt.min_pixelclock = 27000000;
47714735190SHans Verkuil 	cap->bt.max_pixelclock = 148500000;
47814735190SHans Verkuil 	cap->bt.standards = V4L2_DV_BT_STD_CEA861;
47914735190SHans Verkuil 	cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE;
48014735190SHans Verkuil 
48114735190SHans Verkuil 	return 0;
48214735190SHans Verkuil }
48314735190SHans Verkuil 
48414735190SHans Verkuil /* Specific video subsystem operation handlers */
48514735190SHans Verkuil static const struct v4l2_subdev_video_ops ths8200_video_ops = {
48614735190SHans Verkuil 	.s_stream = ths8200_s_stream,
48714735190SHans Verkuil 	.s_dv_timings = ths8200_s_dv_timings,
48814735190SHans Verkuil 	.g_dv_timings = ths8200_g_dv_timings,
48914735190SHans Verkuil 	.enum_dv_timings = ths8200_enum_dv_timings,
49014735190SHans Verkuil 	.dv_timings_cap = ths8200_dv_timings_cap,
49114735190SHans Verkuil };
49214735190SHans Verkuil 
49314735190SHans Verkuil /* V4L2 top level operation handlers */
49414735190SHans Verkuil static const struct v4l2_subdev_ops ths8200_ops = {
49514735190SHans Verkuil 	.core  = &ths8200_core_ops,
49614735190SHans Verkuil 	.video = &ths8200_video_ops,
49714735190SHans Verkuil };
49814735190SHans Verkuil 
49914735190SHans Verkuil static int ths8200_probe(struct i2c_client *client,
50014735190SHans Verkuil 			 const struct i2c_device_id *id)
50114735190SHans Verkuil {
50214735190SHans Verkuil 	struct ths8200_state *state;
50314735190SHans Verkuil 	struct v4l2_subdev *sd;
504*ed29f894SLad, Prabhakar 	int error;
50514735190SHans Verkuil 
50614735190SHans Verkuil 	/* Check if the adapter supports the needed features */
50714735190SHans Verkuil 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
50814735190SHans Verkuil 		return -EIO;
50914735190SHans Verkuil 
51014735190SHans Verkuil 	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
51114735190SHans Verkuil 	if (!state)
51214735190SHans Verkuil 		return -ENOMEM;
51314735190SHans Verkuil 
51414735190SHans Verkuil 	sd = &state->sd;
51514735190SHans Verkuil 	v4l2_i2c_subdev_init(sd, client, &ths8200_ops);
51614735190SHans Verkuil 
51714735190SHans Verkuil 	state->chip_version = ths8200_read(sd, THS8200_VERSION);
51814735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "chip version 0x%x\n", state->chip_version);
51914735190SHans Verkuil 
52014735190SHans Verkuil 	ths8200_core_init(sd);
52114735190SHans Verkuil 
522*ed29f894SLad, Prabhakar 	error = v4l2_async_register_subdev(&state->sd);
523*ed29f894SLad, Prabhakar 	if (error)
524*ed29f894SLad, Prabhakar 		return error;
525*ed29f894SLad, Prabhakar 
52614735190SHans Verkuil 	v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
52714735190SHans Verkuil 		  client->addr << 1, client->adapter->name);
52814735190SHans Verkuil 
52914735190SHans Verkuil 	return 0;
53014735190SHans Verkuil }
53114735190SHans Verkuil 
53214735190SHans Verkuil static int ths8200_remove(struct i2c_client *client)
53314735190SHans Verkuil {
53414735190SHans Verkuil 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
535*ed29f894SLad, Prabhakar 	struct ths8200_state *decoder = to_state(sd);
53614735190SHans Verkuil 
53714735190SHans Verkuil 	v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name,
53814735190SHans Verkuil 		 client->addr << 1, client->adapter->name);
53914735190SHans Verkuil 
54014735190SHans Verkuil 	ths8200_s_power(sd, false);
541*ed29f894SLad, Prabhakar 	v4l2_async_unregister_subdev(&decoder->sd);
54214735190SHans Verkuil 	v4l2_device_unregister_subdev(sd);
54314735190SHans Verkuil 
54414735190SHans Verkuil 	return 0;
54514735190SHans Verkuil }
54614735190SHans Verkuil 
54714735190SHans Verkuil static struct i2c_device_id ths8200_id[] = {
54814735190SHans Verkuil 	{ "ths8200", 0 },
54914735190SHans Verkuil 	{},
55014735190SHans Verkuil };
55114735190SHans Verkuil MODULE_DEVICE_TABLE(i2c, ths8200_id);
55214735190SHans Verkuil 
55314735190SHans Verkuil static struct i2c_driver ths8200_driver = {
55414735190SHans Verkuil 	.driver = {
55514735190SHans Verkuil 		.owner = THIS_MODULE,
55614735190SHans Verkuil 		.name = "ths8200",
55714735190SHans Verkuil 	},
55814735190SHans Verkuil 	.probe = ths8200_probe,
55914735190SHans Verkuil 	.remove = ths8200_remove,
56014735190SHans Verkuil 	.id_table = ths8200_id,
56114735190SHans Verkuil };
56214735190SHans Verkuil 
56314735190SHans Verkuil module_i2c_driver(ths8200_driver);
564