V4L/DVB (4005): Add support for the cx25836/7 video decoder.

Signed-off-by: Scott Alfter <salfter@ssai.us>
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/video/cx25840/cx25840-audio.c b/drivers/media/video/cx25840/cx25840-audio.c
index 9a4b813..f035f2b 100644
--- a/drivers/media/video/cx25840/cx25840-audio.c
+++ b/drivers/media/video/cx25840/cx25840-audio.c
@@ -31,7 +31,8 @@
 		return -EINVAL;
 
 	/* assert soft reset */
-	cx25840_and_or(client, 0x810, ~0x1, 0x01);
+	if (!state->is_cx25836)
+		cx25840_and_or(client, 0x810, ~0x1, 0x01);
 
 	/* common for all inputs and rates */
 	/* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
@@ -46,6 +47,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0xee39bb01);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src3/4/6_ctl = 0x0801f77f */
 			cx25840_write4(client, 0x900, 0x7ff70108);
 			cx25840_write4(client, 0x904, 0x7ff70108);
@@ -59,6 +63,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0xd66bec00);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src3/4/6_ctl = 0x08016d59 */
 			cx25840_write4(client, 0x900, 0x596d0108);
 			cx25840_write4(client, 0x904, 0x596d0108);
@@ -72,6 +79,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0xe5d69800);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src3/4/6_ctl = 0x08014faa */
 			cx25840_write4(client, 0x900, 0xaa4f0108);
 			cx25840_write4(client, 0x904, 0xaa4f0108);
@@ -87,6 +97,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0x69082a01);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src1_ctl = 0x08010000 */
 			cx25840_write4(client, 0x8f8, 0x00000108);
 
@@ -106,6 +119,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0xd66bec00);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src1_ctl = 0x08010000 */
 			cx25840_write4(client, 0x8f8, 0xcd600108);
 
@@ -122,6 +138,9 @@
 			/* AUX_PLL_FRAC */
 			cx25840_write4(client, 0x110, 0xe5d69800);
 
+			if (state->is_cx25836)
+				break;
+
 			/* src1_ctl = 0x08010000 */
 			cx25840_write4(client, 0x8f8, 0x00800108);
 
@@ -134,7 +153,8 @@
 	}
 
 	/* deassert soft reset */
-	cx25840_and_or(client, 0x810, ~0x1, 0x00);
+	if (!state->is_cx25836)
+		cx25840_and_or(client, 0x810, ~0x1, 0x00);
 
 	state->audclk_freq = freq;
 
diff --git a/drivers/media/video/cx25840/cx25840-core.c b/drivers/media/video/cx25840/cx25840-core.c
index a961bb2..e4655e3 100644
--- a/drivers/media/video/cx25840/cx25840-core.c
+++ b/drivers/media/video/cx25840/cx25840-core.c
@@ -105,7 +105,7 @@
 	    (buffer[2] << 8) | buffer[3];
 }
 
-int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
+int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
 		   u8 or_value)
 {
 	return cx25840_write(client, addr,
@@ -117,7 +117,8 @@
 
 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
 						enum cx25840_audio_input aud_input);
-static void log_status(struct i2c_client *client);
+static void log_audio_status(struct i2c_client *client);
+static void log_video_status(struct i2c_client *client);
 
 /* ----------------------------------------------------------------------- */
 
@@ -147,6 +148,33 @@
 	cx25840_write(client, 0x15d, 0xe1);
 }
 
+static void cx25836_initialize(struct i2c_client *client)
+{
+	/* reset configuration is described on page 3-77 of the CX25836 datasheet */
+	/* 2. */
+	cx25840_and_or(client, 0x000, ~0x01, 0x01);
+	cx25840_and_or(client, 0x000, ~0x01, 0x00);
+	/* 3a. */
+	cx25840_and_or(client, 0x15a, ~0x70, 0x00);
+	/* 3b. */
+	cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
+	/* 3c. */
+	cx25840_and_or(client, 0x159, ~0x02, 0x02);
+	/* 3d. */
+	/* There should be a 10-us delay here, but since the
+	   i2c bus already has a 10-us delay we don't need to do
+	   anything */
+	/* 3e. */
+	cx25840_and_or(client, 0x159, ~0x02, 0x00);
+	/* 3f. */
+	cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
+	/* 3g. */
+	cx25840_and_or(client, 0x159, ~0x01, 0x00);
+	cx25840_and_or(client, 0x159, ~0x01, 0x01);
+	/* 3h. */
+	cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
+}
+
 static void cx25840_initialize(struct i2c_client *client, int loadfw)
 {
 	struct cx25840_state *state = i2c_get_clientdata(client);
@@ -319,8 +347,10 @@
 
 	state->vid_input = vid_input;
 	state->aud_input = aud_input;
-	cx25840_audio_set_path(client);
-	input_change(client);
+	if (!state->is_cx25836) {
+		cx25840_audio_set_path(client);
+		input_change(client);
+	}
 	return 0;
 }
 
@@ -370,6 +400,7 @@
 
 v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
 {
+	struct cx25840_state *state = i2c_get_clientdata(client);
 	/* check VID_FMT_SEL first */
 	u8 fmt = cx25840_read(client, 0x400) & 0xf;
 
@@ -383,7 +414,7 @@
 	{
 		/* if the audio std is A2-M, then this is the South Korean
 		   NTSC standard */
-		if (cx25840_read(client, 0x805) == 2)
+		if (!state->is_cx25836 && cx25840_read(client, 0x805) == 2)
 			return V4L2_STD_NTSC_M_KR;
 		return V4L2_STD_NTSC_M;
 	}
@@ -456,6 +487,8 @@
 	case V4L2_CID_AUDIO_TREBLE:
 	case V4L2_CID_AUDIO_BALANCE:
 	case V4L2_CID_AUDIO_MUTE:
+		if (state->is_cx25836)
+			return -EINVAL;
 		return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
 
 	default:
@@ -490,6 +523,8 @@
 	case V4L2_CID_AUDIO_TREBLE:
 	case V4L2_CID_AUDIO_BALANCE:
 	case V4L2_CID_AUDIO_MUTE:
+		if (state->is_cx25836)
+			return -EINVAL;
 		return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
 	default:
 		return -EINVAL;
@@ -579,7 +614,7 @@
 
 /* ----------------------------------------------------------------------- */
 
-static struct v4l2_queryctrl cx25840_qctrl[] = {
+static struct v4l2_queryctrl cx25836_qctrl[] = {
 	{
 		.id            = V4L2_CID_BRIGHTNESS,
 		.type          = V4L2_CTRL_TYPE_INTEGER,
@@ -616,7 +651,11 @@
 		.step          = 1,
 		.default_value = 0,
 		.flags 	       = 0,
-	}, {
+	},
+};
+
+static struct v4l2_queryctrl cx25840_qctrl[] = {
+	{
 		.id            = V4L2_CID_AUDIO_VOLUME,
 		.type          = V4L2_CTRL_TYPE_INTEGER,
 		.name          = "Volume",
@@ -706,8 +745,8 @@
 
 	case VIDIOC_STREAMON:
 		v4l_dbg(1, cx25840_debug, client, "enable output\n");
-		cx25840_write(client, 0x115, 0x8c);
-		cx25840_write(client, 0x116, 0x07);
+		cx25840_write(client, 0x115, state->is_cx25836 ? 0x0c : 0x8c);
+		cx25840_write(client, 0x116, state->is_cx25836 ? 0x04 : 0x07);
 		break;
 
 	case VIDIOC_STREAMOFF:
@@ -717,7 +756,9 @@
 		break;
 
 	case VIDIOC_LOG_STATUS:
-		log_status(client);
+		log_video_status(client);
+		if (!state->is_cx25836)
+			log_audio_status(client);
 		break;
 
 	case VIDIOC_G_CTRL:
@@ -731,6 +772,14 @@
 		struct v4l2_queryctrl *qc = arg;
 		int i;
 
+		for (i = 0; i < ARRAY_SIZE(cx25836_qctrl); i++)
+			if (qc->id && qc->id == cx25836_qctrl[i].id) {
+				memcpy(qc, &cx25836_qctrl[i], sizeof(*qc));
+				return 0;
+			}
+		if (state->is_cx25836)
+			return -EINVAL;
+
 		for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
 			if (qc->id && qc->id == cx25840_qctrl[i].id) {
 				memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
@@ -760,31 +809,41 @@
 		return set_input(client, route->input, state->aud_input);
 
 	case VIDIOC_INT_G_AUDIO_ROUTING:
+		if (state->is_cx25836)
+			return -EINVAL;
 		route->input = state->aud_input;
 		route->output = 0;
 		break;
 
 	case VIDIOC_INT_S_AUDIO_ROUTING:
+		if (state->is_cx25836)
+			return -EINVAL;
 		return set_input(client, state->vid_input, route->input);
 
 	case VIDIOC_S_FREQUENCY:
-		input_change(client);
+		if (!state->is_cx25836) {
+			input_change(client);
+		}
 		break;
 
 	case VIDIOC_G_TUNER:
 	{
-		u8 mode = cx25840_read(client, 0x804);
-		u8 vpres = cx25840_read(client, 0x80a) & 0x10;
+		u8 vpres = cx25840_read(client, 0x40e) & 0x20;
+		u8 mode;
 		int val = 0;
 
 		if (state->radio)
 			break;
 
+		vt->signal = vpres ? 0xffff : 0x0;
+		if (state->is_cx25836)
+			break;
+
 		vt->capability |=
 		    V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
 		    V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
 
-		vt->signal = vpres ? 0xffff : 0x0;
+		mode = cx25840_read(client, 0x804);
 
 		/* get rxsubchans and audmode */
 		if ((mode & 0xf) == 1)
@@ -804,7 +863,7 @@
 	}
 
 	case VIDIOC_S_TUNER:
-		if (state->radio)
+		if (state->radio || state->is_cx25836)
 			break;
 
 		switch (vt->audmode) {
@@ -846,12 +905,14 @@
 		return set_v4lfmt(client, (struct v4l2_format *)arg);
 
 	case VIDIOC_INT_RESET:
-		cx25840_initialize(client, 0);
+		if (state->is_cx25836)
+			cx25836_initialize(client);
+		else
+			cx25840_initialize(client, 0);
 		break;
 
 	case VIDIOC_INT_G_CHIP_IDENT:
-		*(enum v4l2_chip_ident *)arg =
-			V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
+		*(enum v4l2_chip_ident *)arg = state->id;
 		break;
 
 	default:
@@ -870,6 +931,7 @@
 {
 	struct i2c_client *client;
 	struct cx25840_state *state;
+	enum v4l2_chip_ident id;
 	u16 device_id;
 
 	/* Check if the adapter supports the needed features
@@ -878,10 +940,11 @@
 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
 		return 0;
 
-	client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
-	if (client == 0)
+	state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
+	if (state == 0)
 		return -ENOMEM;
 
+	client = &state->c;
 	client->addr = address;
 	client->adapter = adapter;
 	client->driver = &i2c_driver_cx25840;
@@ -893,10 +956,18 @@
 	device_id |= cx25840_read(client, 0x100);
 
 	/* The high byte of the device ID should be
-	 * 0x84 if chip is present */
-	if ((device_id & 0xff00) != 0x8400) {
+	 * 0x83 for the cx2583x and 0x84 for the cx2584x */
+	if ((device_id & 0xff00) == 0x8300) {
+		id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
+		state->is_cx25836 = 1;
+	}
+	else if ((device_id & 0xff00) == 0x8400) {
+		id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
+		state->is_cx25836 = 0;
+	}
+	else {
 		v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
-		kfree(client);
+		kfree(state);
 		return 0;
 	}
 
@@ -905,21 +976,18 @@
 		    (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
 		    address << 1, adapter->name);
 
-	state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
-	if (state == NULL) {
-		kfree(client);
-		return -ENOMEM;
-	}
-
 	i2c_set_clientdata(client, state);
-	memset(state, 0, sizeof(struct cx25840_state));
 	state->vid_input = CX25840_COMPOSITE7;
 	state->aud_input = CX25840_AUDIO8;
 	state->audclk_freq = 48000;
 	state->pvr150_workaround = 0;
 	state->audmode = V4L2_TUNER_MODE_LANG1;
+	state->id = id;
 
-	cx25840_initialize(client, 1);
+	if (state->is_cx25836)
+		cx25836_initialize(client);
+	else
+		cx25840_initialize(client, 1);
 
 	i2c_attach_client(client);
 
@@ -944,7 +1012,6 @@
 	}
 
 	kfree(state);
-	kfree(client);
 
 	return 0;
 }
@@ -977,7 +1044,7 @@
 
 /* ----------------------------------------------------------------------- */
 
-static void log_status(struct i2c_client *client)
+static void log_video_status(struct i2c_client *client)
 {
 	static const char *const fmt_strs[] = {
 		"0x0",
@@ -989,9 +1056,36 @@
 	};
 
 	struct cx25840_state *state = i2c_get_clientdata(client);
-	u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
 	u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
 	u8 gen_stat1 = cx25840_read(client, 0x40d);
+	u8 gen_stat2 = cx25840_read(client, 0x40e);
+	int vid_input = state->vid_input;
+
+	v4l_info(client, "Video signal:              %spresent\n",
+		    (gen_stat2 & 0x20) ? "" : "not ");
+	v4l_info(client, "Detected format:           %s\n",
+		    fmt_strs[gen_stat1 & 0xf]);
+
+	v4l_info(client, "Specified standard:        %s\n",
+		    vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
+
+	if (vid_input >= CX25840_COMPOSITE1 &&
+	    vid_input <= CX25840_COMPOSITE8) {
+		v4l_info(client, "Specified video input:     Composite %d\n",
+			vid_input - CX25840_COMPOSITE1 + 1);
+	} else {
+		v4l_info(client, "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
+			(vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
+	}
+
+	v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
+}
+
+/* ----------------------------------------------------------------------- */
+
+static void log_audio_status(struct i2c_client *client)
+{
+	struct cx25840_state *state = i2c_get_clientdata(client);
 	u8 download_ctl = cx25840_read(client, 0x803);
 	u8 mod_det_stat0 = cx25840_read(client, 0x804);
 	u8 mod_det_stat1 = cx25840_read(client, 0x805);
@@ -999,15 +1093,9 @@
 	u8 pref_mode = cx25840_read(client, 0x809);
 	u8 afc0 = cx25840_read(client, 0x80b);
 	u8 mute_ctl = cx25840_read(client, 0x8d3);
-	int vid_input = state->vid_input;
 	int aud_input = state->aud_input;
 	char *p;
 
-	v4l_info(client, "Video signal:              %spresent\n",
-		    (microctrl_vidfmt & 0x10) ? "" : "not ");
-	v4l_info(client, "Detected format:           %s\n",
-		    fmt_strs[gen_stat1 & 0xf]);
-
 	switch (mod_det_stat0) {
 	case 0x00: p = "mono"; break;
 	case 0x01: p = "stereo"; break;
@@ -1107,25 +1195,12 @@
 		v4l_info(client, "Configured audio system:   %s\n", p);
 	}
 
-	v4l_info(client, "Specified standard:        %s\n",
-		    vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
-
-	if (vid_input >= CX25840_COMPOSITE1 &&
-	    vid_input <= CX25840_COMPOSITE8) {
-		v4l_info(client, "Specified video input:     Composite %d\n",
-			vid_input - CX25840_COMPOSITE1 + 1);
-	} else {
-		v4l_info(client, "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
-			(vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
-	}
 	if (aud_input) {
 		v4l_info(client, "Specified audio input:     Tuner (In%d)\n", aud_input);
 	} else {
 		v4l_info(client, "Specified audio input:     External\n");
 	}
 
-	v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
-
 	switch (pref_mode & 0xf) {
 	case 0: p = "mono/language A"; break;
 	case 1: p = "language B"; break;
diff --git a/drivers/media/video/cx25840/cx25840-core.h b/drivers/media/video/cx25840/cx25840-core.h
index 1736929..69d7bd2 100644
--- a/drivers/media/video/cx25840/cx25840-core.h
+++ b/drivers/media/video/cx25840/cx25840-core.h
@@ -33,12 +33,15 @@
 #define CX25840_CID_ENABLE_PVR150_WORKAROUND (V4L2_CID_PRIVATE_BASE+0)
 
 struct cx25840_state {
+	struct i2c_client c;
 	int pvr150_workaround;
 	int radio;
 	enum cx25840_video_input vid_input;
 	enum cx25840_audio_input aud_input;
 	u32 audclk_freq;
 	int audmode;
+	enum v4l2_chip_ident id;
+	int is_cx25836;
 };
 
 /* ----------------------------------------------------------------------- */
@@ -47,7 +50,7 @@
 int cx25840_write4(struct i2c_client *client, u16 addr, u32 value);
 u8 cx25840_read(struct i2c_client *client, u16 addr);
 u32 cx25840_read4(struct i2c_client *client, u16 addr);
-int cx25840_and_or(struct i2c_client *client, u16 addr, u8 mask, u8 value);
+int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned mask, u8 value);
 v4l2_std_id cx25840_get_v4lstd(struct i2c_client *client);
 
 /* ----------------------------------------------------------------------- */