11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    hexium_gemini.c - v4l2 driver for Hexium Gemini frame grabber cards
3a8733ca5141c256322ab5ea9fd3074942a209bbaMauro Carvalho Chehab
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    Visit http://www.mihu.de/linux/saa7146/ and follow the link
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    to "hexium" for further details about this card.
6a8733ca5141c256322ab5ea9fd3074942a209bbaMauro Carvalho Chehab
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    Copyright (C) 2003 Michael Hunold <michael@mihu.de>
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    This program is free software; you can redistribute it and/or modify
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    it under the terms of the GNU General Public License as published by
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    the Free Software Foundation; either version 2 of the License, or
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    (at your option) any later version.
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    This program is distributed in the hope that it will be useful,
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    but WITHOUT ANY WARRANTY; without even the implied warranty of
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    GNU General Public License for more details.
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    You should have received a copy of the GNU General Public License
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    along with this program; if not, write to the Free Software
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2444d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2544d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DEBUG_VARIABLE debug
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <media/saa7146_vv.h>
297a707b89202f905bd9f9fbde326933c59a81214cPaul Gortmaker#include <linux/module.h>
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
31ff699e6bd02eb1c6d02c7c2b576c2ee6caab201cDouglas Schilling Landgrafstatic int debug;
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(debug, int, 0);
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(debug, "debug verbosity");
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* global variables */
36ff699e6bd02eb1c6d02c7c2b576c2ee6caab201cDouglas Schilling Landgrafstatic int hexium_num;
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_GEMINI			4
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_GEMINI_DUAL		5
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_INPUTS	9
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {
43657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 0, "CVBS 1",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
44657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 1, "CVBS 2",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
45657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 2, "CVBS 3",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
46657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 3, "CVBS 4",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
47657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 4, "CVBS 5",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
48657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 5, "CVBS 6",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
49657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 6, "Y/C 1",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
50657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 7, "Y/C 2",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
51657f2271050beabe725ba101c1d840f51a41f33dHans Verkuil	{ 8, "Y/C 3",	V4L2_INPUT_TYPE_CAMERA,	2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_AUDIOS	0
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct hexium_data
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 adr;
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 byte;
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_CONTROLS	1
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct v4l2_queryctrl hexium_controls[] = {
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ V4L2_CID_PRIVATE_BASE, V4L2_CTRL_TYPE_BOOLEAN, "B/W", 0, 1, 1, 0, 0 },
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_GEMINI_V_1_0		1
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define HEXIUM_GEMINI_DUAL_V_1_0	2
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct hexium
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int type;
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct video_device	*video_dev;
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_adapter	i2c_adapter;
76a8733ca5141c256322ab5ea9fd3074942a209bbaMauro Carvalho Chehab
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int 		cur_input;	/* current input */
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	v4l2_std_id 	cur_std;	/* current standard */
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		cur_bw;		/* current black/white status */
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Samsung KS0127B decoder default registers */
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic u8 hexium_ks0127b[0x100]={
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*00*/ 0x00,0x52,0x30,0x40,0x01,0x0C,0x2A,0x10,
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*08*/ 0x00,0x00,0x00,0x60,0x00,0x00,0x0F,0x06,
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*10*/ 0x00,0x00,0xE4,0xC0,0x00,0x00,0x00,0x00,
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*18*/ 0x14,0x9B,0xFE,0xFF,0xFC,0xFF,0x03,0x22,
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*20*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*28*/ 0x00,0x00,0x00,0x00,0x00,0x2C,0x9B,0x00,
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*30*/ 0x00,0x00,0x10,0x80,0x80,0x10,0x80,0x80,
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*38*/ 0x01,0x04,0x00,0x00,0x00,0x29,0xC0,0x00,
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*40*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*48*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*50*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*58*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*60*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*68*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*70*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*78*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*80*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*88*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*90*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*98*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*A0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*A8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*B0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*B8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*C0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*C8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*D0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*D8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*E0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*E8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*F0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*F8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_pal[] = {
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_pal_bw[] = {
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x01, 0x52 },	{ 0x12, 0x64 },	{ 0x2D, 0x2C },	{ 0x2E, 0x9B },	{ -1 , 0xFF }
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_ntsc[] = {
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_ntsc_bw[] = {
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_secam[] = {
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct hexium_data hexium_input_select[] = {
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x60 },
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x64 },
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x61 },
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x65 },
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x62 },
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x66 },
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x68 },
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x69 },
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0x02, 0x6A },
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* fixme: h_offset = 0 for Hexium Gemini *Dual*, which
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   are currently *not* supported*/
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_standard hexium_standards[] = {
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.name	= "PAL", 	.id	= V4L2_STD_PAL,
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_offset	= 28,	.v_field 	= 288,
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.h_offset	= 1,	.h_pixels 	= 680,
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_max_out	= 576,	.h_max_out	= 768,
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}, {
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.name	= "NTSC", 	.id	= V4L2_STD_NTSC,
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_offset	= 28,	.v_field 	= 240,
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.h_offset	= 1,	.h_pixels 	= 640,
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_max_out	= 480,	.h_max_out	= 640,
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}, {
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.name	= "SECAM", 	.id	= V4L2_STD_SECAM,
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_offset	= 28,	.v_field 	= 288,
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.h_offset	= 1,	.h_pixels 	= 720,
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.v_max_out	= 576,	.h_max_out	= 768,
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
169a8733ca5141c256322ab5ea9fd3074942a209bbaMauro Carvalho Chehab};
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* bring hardware to a sane state. this has to be done, just in case someone
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   wants to capture from this device before it has been properly initialized.
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   the capture engine would badly fail, because no valid signal arrives on the
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds   saa7146, thus leading to timeouts and stuff. */
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int hexium_init_done(struct saa7146_dev *dev)
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct hexium *hexium = (struct hexium *) dev->ext_priv;
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	union i2c_smbus_data data;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i = 0;
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18144d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("hexium_init_done called\n");
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* initialize the helper ics to useful values */
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < sizeof(hexium_ks0127b); i++) {
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data.byte = hexium_ks0127b[i];
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
18744d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches			pr_err("hexium_init_done() failed for address 0x%02x\n",
18844d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches			       i);
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int hexium_set_input(struct hexium *hexium, int input)
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	union i2c_smbus_data data;
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
19944d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("\n");
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data.byte = hexium_input_select[input].byte;
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, hexium_input_select[input].adr, I2C_SMBUS_BYTE_DATA, &data)) {
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -1;
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int hexium_set_standard(struct hexium *hexium, struct hexium_data *vdec)
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	union i2c_smbus_data data;
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i = 0;
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21444d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("\n");
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while (vdec[i].adr != -1) {
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data.byte = vdec[i].byte;
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, vdec[i].adr, I2C_SMBUS_BYTE_DATA, &data)) {
21944d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches			pr_err("hexium_init_done: hexium_set_standard() failed for address 0x%02x\n",
22044d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches			       i);
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -1;
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i++;
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
228b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
229b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
23044d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
231b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
232223ffe5f8270ba9d069f1cbff9acec095a6f58b1Roel Kluin	if (i->index >= HEXIUM_INPUTS)
233b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return -EINVAL;
234b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
235b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
236b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
23744d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
238b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return 0;
239b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
240b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
241b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
242b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
243b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
244b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct hexium *hexium = (struct hexium *) dev->ext_priv;
245b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
246b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	*input = hexium->cur_input;
247b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
24844d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("VIDIOC_G_INPUT: %d\n", *input);
249b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return 0;
250b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
251b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
252b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_s_input(struct file *file, void *fh, unsigned int input)
253b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
254b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
255b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct hexium *hexium = (struct hexium *) dev->ext_priv;
256b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
25744d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_EE("VIDIOC_S_INPUT %d\n", input);
258b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
259f14a2972e40dbfbe7077ec7ab21cc2729f7e7d6dRoel Kluin	if (input >= HEXIUM_INPUTS)
260b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return -EINVAL;
261b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
262b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	hexium->cur_input = input;
263b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	hexium_set_input(hexium, input);
264b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return 0;
265b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
266b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
267b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil/* the saa7146 provides some controls (brightness, contrast, saturation)
268b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil   which gets registered *after* this function. because of this we have
26925985edcedea6396277003854657b5f3cb31a628Lucas De Marchi   to return with a value != 0 even if the function succeeded.. */
270b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qc)
271b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
272b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
273b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	int i;
274b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
275b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {
276b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		if (hexium_controls[i].id == qc->id) {
277b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil			*qc = hexium_controls[i];
27844d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches			DEB_D("VIDIOC_QUERYCTRL %d\n", qc->id);
279b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil			return 0;
280b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		}
281b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
282b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return dev->ext_vv_data->core_ops->vidioc_queryctrl(file, fh, qc);
283b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
284b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
285b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *vc)
286b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
287b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
288b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct hexium *hexium = (struct hexium *) dev->ext_priv;
289b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	int i;
290b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
291b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {
292b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		if (hexium_controls[i].id == vc->id)
293b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil			break;
294b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
295b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
296b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (i < 0)
297b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return dev->ext_vv_data->core_ops->vidioc_g_ctrl(file, fh, vc);
298b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
299b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (vc->id == V4L2_CID_PRIVATE_BASE) {
300b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		vc->value = hexium->cur_bw;
30144d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches		DEB_D("VIDIOC_G_CTRL BW:%d\n", vc->value);
302b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
303b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
304b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return -EINVAL;
305b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
306b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
307b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuilstatic int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *vc)
308b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil{
309b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
310b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	struct hexium *hexium = (struct hexium *) dev->ext_priv;
311b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	int i = 0;
312b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
313b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {
314b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		if (hexium_controls[i].id == vc->id)
315b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil			break;
316b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
317b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
318b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (i < 0)
319b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return dev->ext_vv_data->core_ops->vidioc_s_ctrl(file, fh, vc);
320b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
321b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (vc->id == V4L2_CID_PRIVATE_BASE)
322b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium->cur_bw = vc->value;
323b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
32444d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_D("VIDIOC_S_CTRL BW:%d\n", hexium->cur_bw);
325b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
326b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (0 == hexium->cur_bw && V4L2_STD_PAL == hexium->cur_std) {
327b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium_set_standard(hexium, hexium_pal);
328b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
329b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
330b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (0 == hexium->cur_bw && V4L2_STD_NTSC == hexium->cur_std) {
331b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium_set_standard(hexium, hexium_ntsc);
332b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
333b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
334b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (0 == hexium->cur_bw && V4L2_STD_SECAM == hexium->cur_std) {
335b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium_set_standard(hexium, hexium_secam);
336b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
337b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
338b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (1 == hexium->cur_bw && V4L2_STD_PAL == hexium->cur_std) {
339b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium_set_standard(hexium, hexium_pal_bw);
340b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
341b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
342b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (1 == hexium->cur_bw && V4L2_STD_NTSC == hexium->cur_std) {
343b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		hexium_set_standard(hexium, hexium_ntsc_bw);
344b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return 0;
345b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	}
346b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	if (1 == hexium->cur_bw && V4L2_STD_SECAM == hexium->cur_std)
347b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		/* fixme: is there no bw secam mode? */
348b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil		return -EINVAL;
349b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
350b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	return -EINVAL;
351b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil}
352b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
353b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_ext_vv vv_data;
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* this function only gets called when the probing was successful */
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
359f00fd919ef71b3d6d52dbfa7ce827679d92af713Julia Lawall	struct hexium *hexium;
360cd7d9beb09d89d62bc3c6336e4cb9a2ee3da6163Hans Verkuil	int ret;
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
36244d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_EE("\n");
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3647408187d223f63d46a13b6a35b8f96b032c2f623Panagiotis Issaris	hexium = kzalloc(sizeof(struct hexium), GFP_KERNEL);
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (NULL == hexium) {
36644d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches		pr_err("not enough kernel memory in hexium_attach()\n");
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dev->ext_priv = hexium;
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* enable i2c-port pins */
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium->i2c_adapter = (struct i2c_adapter) {
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.name = "hexium gemini",
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	};
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
37944d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches		DEB_S("cannot register i2c-device. skipping.\n");
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kfree(hexium);
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EFAULT;
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*  set HWControl GPIO number 2 */
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_write(dev, DD1_INIT, 0x07000700);
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_write(dev, DD1_STREAM_B, 0x00000000);
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* the rest */
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium->cur_input = 0;
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium_init_done(dev);
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium_set_standard(hexium, hexium_pal);
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium->cur_std = V4L2_STD_PAL;
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium_set_input(hexium, 0);
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium->cur_input = 0;
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_vv_init(dev, &vv_data);
402b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_queryctrl = vidioc_queryctrl;
403b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_g_ctrl = vidioc_g_ctrl;
404b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_s_ctrl = vidioc_s_ctrl;
405b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_enum_input = vidioc_enum_input;
406b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_g_input = vidioc_g_input;
407b960074fec573fb1b226d9e2686ce51be807cdf1Hans Verkuil	vv_data.ops.vidioc_s_input = vidioc_s_input;
408cd7d9beb09d89d62bc3c6336e4cb9a2ee3da6163Hans Verkuil	ret = saa7146_register_device(&hexium->video_dev, dev, "hexium gemini", VFL_TYPE_GRABBER);
409cd7d9beb09d89d62bc3c6336e4cb9a2ee3da6163Hans Verkuil	if (ret < 0) {
41044d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches		pr_err("cannot register capture v4l2 device. skipping.\n");
411cd7d9beb09d89d62bc3c6336e4cb9a2ee3da6163Hans Verkuil		return ret;
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
41444d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	pr_info("found 'hexium gemini' frame grabber-%d\n", hexium_num);
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium_num++;
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int hexium_detach(struct saa7146_dev *dev)
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct hexium *hexium = (struct hexium *) dev->ext_priv;
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
42444d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches	DEB_EE("dev:%p\n", dev);
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_unregister_device(&hexium->video_dev, dev);
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_vv_release(dev);
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	hexium_num--;
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_del_adapter(&hexium->i2c_adapter);
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(hexium);
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct hexium *hexium = (struct hexium *) dev->ext_priv;
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (V4L2_STD_PAL == std->id) {
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium_set_standard(hexium, hexium_pal);
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium->cur_std = V4L2_STD_PAL;
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (V4L2_STD_NTSC == std->id) {
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium_set_standard(hexium, hexium_ntsc);
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium->cur_std = V4L2_STD_NTSC;
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (V4L2_STD_SECAM == std->id) {
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium_set_standard(hexium, hexium_secam);
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		hexium->cur_std = V4L2_STD_SECAM;
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -1;
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_extension hexium_extension;
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_pci_extension_data hexium_gemini_4bnc = {
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ext_priv = "Hexium Gemini (4 BNC)",
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ext = &hexium_extension,
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_pci_extension_data hexium_gemini_dual_4bnc = {
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ext_priv = "Hexium Gemini Dual (4 BNC)",
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ext = &hexium_extension,
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct pci_device_id pci_tbl[] = {
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .vendor = PCI_VENDOR_ID_PHILIPS,
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .subvendor = 0x17c8,
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .subdevice = 0x2401,
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .driver_data = (unsigned long) &hexium_gemini_4bnc,
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 },
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .vendor = PCI_VENDOR_ID_PHILIPS,
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .subvendor = 0x17c8,
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .subdevice = 0x2402,
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .driver_data = (unsigned long) &hexium_gemini_dual_4bnc,
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 },
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 .vendor = 0,
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 }
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DEVICE_TABLE(pci, pci_tbl);
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_ext_vv vv_data = {
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.inputs = HEXIUM_INPUTS,
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.capabilities = 0,
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.stds = &hexium_standards[0],
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.num_stds = sizeof(hexium_standards) / sizeof(struct saa7146_standard),
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.std_callback = &std_callback,
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct saa7146_extension hexium_extension = {
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name = "hexium gemini",
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flags = SAA7146_USE_I2C_IRQ,
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.pci_tbl = &pci_tbl[0],
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.module = THIS_MODULE,
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attach = hexium_attach,
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.detach = hexium_detach,
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.irq_mask = 0,
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.irq_func = NULL,
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init hexium_init_module(void)
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (0 != saa7146_register_extension(&hexium_extension)) {
51644d0b80e5ff741d502a6ccc8685a18bda1ac9da4Joe Perches		DEB_S("failed to register extension\n");
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENODEV;
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit hexium_cleanup_module(void)
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	saa7146_unregister_extension(&hexium_extension);
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(hexium_init_module);
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(hexium_cleanup_module);
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("video4linux-2 driver for Hexium Gemini frame grabber cards");
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
534