18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci    hexium_orion.c - v4l2 driver for the Hexium Orion frame grabber cards
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci    Visit http://www.mihu.de/linux/saa7146/ and follow the link
68c2ecf20Sopenharmony_ci    to "hexium" for further details about this card.
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci    Copyright (C) 2003 Michael Hunold <michael@mihu.de>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci*/
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define DEBUG_VARIABLE debug
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <media/drv-intf/saa7146_vv.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic int debug;
218c2ecf20Sopenharmony_cimodule_param(debug, int, 0);
228c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "debug verbosity");
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* global variables */
258c2ecf20Sopenharmony_cistatic int hexium_num;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define HEXIUM_HV_PCI6_ORION		1
288c2ecf20Sopenharmony_ci#define HEXIUM_ORION_1SVHS_3BNC		2
298c2ecf20Sopenharmony_ci#define HEXIUM_ORION_4BNC		3
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define HEXIUM_INPUTS	9
328c2ecf20Sopenharmony_cistatic struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {
338c2ecf20Sopenharmony_ci	{ 0, "CVBS 1",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
348c2ecf20Sopenharmony_ci	{ 1, "CVBS 2",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
358c2ecf20Sopenharmony_ci	{ 2, "CVBS 3",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
368c2ecf20Sopenharmony_ci	{ 3, "CVBS 4",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
378c2ecf20Sopenharmony_ci	{ 4, "CVBS 5",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
388c2ecf20Sopenharmony_ci	{ 5, "CVBS 6",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
398c2ecf20Sopenharmony_ci	{ 6, "Y/C 1",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
408c2ecf20Sopenharmony_ci	{ 7, "Y/C 2",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
418c2ecf20Sopenharmony_ci	{ 8, "Y/C 3",	V4L2_INPUT_TYPE_CAMERA,	0, 0, V4L2_STD_ALL, 0, V4L2_IN_CAP_STD },
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define HEXIUM_AUDIOS	0
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct hexium_data
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	s8 adr;
498c2ecf20Sopenharmony_ci	u8 byte;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct hexium
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	int type;
558c2ecf20Sopenharmony_ci	struct video_device	video_dev;
568c2ecf20Sopenharmony_ci	struct i2c_adapter	i2c_adapter;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	int cur_input;	/* current input */
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* Philips SAA7110 decoder default registers */
628c2ecf20Sopenharmony_cistatic u8 hexium_saa7110[53]={
638c2ecf20Sopenharmony_ci/*00*/ 0x4C,0x3C,0x0D,0xEF,0xBD,0xF0,0x00,0x00,
648c2ecf20Sopenharmony_ci/*08*/ 0xF8,0xF8,0x60,0x60,0x40,0x86,0x18,0x90,
658c2ecf20Sopenharmony_ci/*10*/ 0x00,0x2C,0x40,0x46,0x42,0x1A,0xFF,0xDA,
668c2ecf20Sopenharmony_ci/*18*/ 0xF0,0x8B,0x00,0x00,0x00,0x00,0x00,0x00,
678c2ecf20Sopenharmony_ci/*20*/ 0xD9,0x17,0x40,0x41,0x80,0x41,0x80,0x4F,
688c2ecf20Sopenharmony_ci/*28*/ 0xFE,0x01,0x0F,0x0F,0x03,0x01,0x81,0x03,
698c2ecf20Sopenharmony_ci/*30*/ 0x44,0x75,0x01,0x8C,0x03
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic struct {
738c2ecf20Sopenharmony_ci	struct hexium_data data[8];
748c2ecf20Sopenharmony_ci} hexium_input_select[] = {
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	{ /* cvbs 1 */
778c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
788c2ecf20Sopenharmony_ci		{ 0x20, 0xD9 },
798c2ecf20Sopenharmony_ci		{ 0x21, 0x17 }, // 0x16,
808c2ecf20Sopenharmony_ci		{ 0x22, 0x40 },
818c2ecf20Sopenharmony_ci		{ 0x2C, 0x03 },
828c2ecf20Sopenharmony_ci		{ 0x30, 0x44 },
838c2ecf20Sopenharmony_ci		{ 0x31, 0x75 }, // ??
848c2ecf20Sopenharmony_ci		{ 0x21, 0x16 }, // 0x03,
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci}, {
878c2ecf20Sopenharmony_ci	{ /* cvbs 2 */
888c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
898c2ecf20Sopenharmony_ci		{ 0x20, 0x78 },
908c2ecf20Sopenharmony_ci		{ 0x21, 0x07 }, // 0x03,
918c2ecf20Sopenharmony_ci		{ 0x22, 0xD2 },
928c2ecf20Sopenharmony_ci		{ 0x2C, 0x83 },
938c2ecf20Sopenharmony_ci		{ 0x30, 0x60 },
948c2ecf20Sopenharmony_ci		{ 0x31, 0xB5 }, // ?
958c2ecf20Sopenharmony_ci		{ 0x21, 0x03 },
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci}, {
988c2ecf20Sopenharmony_ci	{ /* cvbs 3 */
998c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
1008c2ecf20Sopenharmony_ci		{ 0x20, 0xBA },
1018c2ecf20Sopenharmony_ci		{ 0x21, 0x07 }, // 0x05,
1028c2ecf20Sopenharmony_ci		{ 0x22, 0x91 },
1038c2ecf20Sopenharmony_ci		{ 0x2C, 0x03 },
1048c2ecf20Sopenharmony_ci		{ 0x30, 0x60 },
1058c2ecf20Sopenharmony_ci		{ 0x31, 0xB5 }, // ??
1068c2ecf20Sopenharmony_ci		{ 0x21, 0x05 }, // 0x03,
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci}, {
1098c2ecf20Sopenharmony_ci	{ /* cvbs 4 */
1108c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
1118c2ecf20Sopenharmony_ci		{ 0x20, 0xD8 },
1128c2ecf20Sopenharmony_ci		{ 0x21, 0x17 }, // 0x16,
1138c2ecf20Sopenharmony_ci		{ 0x22, 0x40 },
1148c2ecf20Sopenharmony_ci		{ 0x2C, 0x03 },
1158c2ecf20Sopenharmony_ci		{ 0x30, 0x44 },
1168c2ecf20Sopenharmony_ci		{ 0x31, 0x75 }, // ??
1178c2ecf20Sopenharmony_ci		{ 0x21, 0x16 }, // 0x03,
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci}, {
1208c2ecf20Sopenharmony_ci	{ /* cvbs 5 */
1218c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
1228c2ecf20Sopenharmony_ci		{ 0x20, 0xB8 },
1238c2ecf20Sopenharmony_ci		{ 0x21, 0x07 }, // 0x05,
1248c2ecf20Sopenharmony_ci		{ 0x22, 0x91 },
1258c2ecf20Sopenharmony_ci		{ 0x2C, 0x03 },
1268c2ecf20Sopenharmony_ci		{ 0x30, 0x60 },
1278c2ecf20Sopenharmony_ci		{ 0x31, 0xB5 }, // ??
1288c2ecf20Sopenharmony_ci		{ 0x21, 0x05 }, // 0x03,
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci}, {
1318c2ecf20Sopenharmony_ci	{ /* cvbs 6 */
1328c2ecf20Sopenharmony_ci		{ 0x06, 0x00 },
1338c2ecf20Sopenharmony_ci		{ 0x20, 0x7C },
1348c2ecf20Sopenharmony_ci		{ 0x21, 0x07 }, // 0x03
1358c2ecf20Sopenharmony_ci		{ 0x22, 0xD2 },
1368c2ecf20Sopenharmony_ci		{ 0x2C, 0x83 },
1378c2ecf20Sopenharmony_ci		{ 0x30, 0x60 },
1388c2ecf20Sopenharmony_ci		{ 0x31, 0xB5 }, // ??
1398c2ecf20Sopenharmony_ci		{ 0x21, 0x03 },
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci}, {
1428c2ecf20Sopenharmony_ci	{ /* y/c 1 */
1438c2ecf20Sopenharmony_ci		{ 0x06, 0x80 },
1448c2ecf20Sopenharmony_ci		{ 0x20, 0x59 },
1458c2ecf20Sopenharmony_ci		{ 0x21, 0x17 },
1468c2ecf20Sopenharmony_ci		{ 0x22, 0x42 },
1478c2ecf20Sopenharmony_ci		{ 0x2C, 0xA3 },
1488c2ecf20Sopenharmony_ci		{ 0x30, 0x44 },
1498c2ecf20Sopenharmony_ci		{ 0x31, 0x75 },
1508c2ecf20Sopenharmony_ci		{ 0x21, 0x12 },
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci}, {
1538c2ecf20Sopenharmony_ci	{ /* y/c 2 */
1548c2ecf20Sopenharmony_ci		{ 0x06, 0x80 },
1558c2ecf20Sopenharmony_ci		{ 0x20, 0x9A },
1568c2ecf20Sopenharmony_ci		{ 0x21, 0x17 },
1578c2ecf20Sopenharmony_ci		{ 0x22, 0xB1 },
1588c2ecf20Sopenharmony_ci		{ 0x2C, 0x13 },
1598c2ecf20Sopenharmony_ci		{ 0x30, 0x60 },
1608c2ecf20Sopenharmony_ci		{ 0x31, 0xB5 },
1618c2ecf20Sopenharmony_ci		{ 0x21, 0x14 },
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci}, {
1648c2ecf20Sopenharmony_ci	{ /* y/c 3 */
1658c2ecf20Sopenharmony_ci		{ 0x06, 0x80 },
1668c2ecf20Sopenharmony_ci		{ 0x20, 0x3C },
1678c2ecf20Sopenharmony_ci		{ 0x21, 0x27 },
1688c2ecf20Sopenharmony_ci		{ 0x22, 0xC1 },
1698c2ecf20Sopenharmony_ci		{ 0x2C, 0x23 },
1708c2ecf20Sopenharmony_ci		{ 0x30, 0x44 },
1718c2ecf20Sopenharmony_ci		{ 0x31, 0x75 },
1728c2ecf20Sopenharmony_ci		{ 0x21, 0x21 },
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic struct saa7146_standard hexium_standards[] = {
1788c2ecf20Sopenharmony_ci	{
1798c2ecf20Sopenharmony_ci		.name	= "PAL",	.id	= V4L2_STD_PAL,
1808c2ecf20Sopenharmony_ci		.v_offset	= 16,	.v_field	= 288,
1818c2ecf20Sopenharmony_ci		.h_offset	= 1,	.h_pixels	= 680,
1828c2ecf20Sopenharmony_ci		.v_max_out	= 576,	.h_max_out	= 768,
1838c2ecf20Sopenharmony_ci	}, {
1848c2ecf20Sopenharmony_ci		.name	= "NTSC",	.id	= V4L2_STD_NTSC,
1858c2ecf20Sopenharmony_ci		.v_offset	= 16,	.v_field	= 240,
1868c2ecf20Sopenharmony_ci		.h_offset	= 1,	.h_pixels	= 640,
1878c2ecf20Sopenharmony_ci		.v_max_out	= 480,	.h_max_out	= 640,
1888c2ecf20Sopenharmony_ci	}, {
1898c2ecf20Sopenharmony_ci		.name	= "SECAM",	.id	= V4L2_STD_SECAM,
1908c2ecf20Sopenharmony_ci		.v_offset	= 16,	.v_field	= 288,
1918c2ecf20Sopenharmony_ci		.h_offset	= 1,	.h_pixels	= 720,
1928c2ecf20Sopenharmony_ci		.v_max_out	= 576,	.h_max_out	= 768,
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/* this is only called for old HV-PCI6/Orion cards
1978c2ecf20Sopenharmony_ci   without eeprom */
1988c2ecf20Sopenharmony_cistatic int hexium_probe(struct saa7146_dev *dev)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct hexium *hexium = NULL;
2018c2ecf20Sopenharmony_ci	union i2c_smbus_data data;
2028c2ecf20Sopenharmony_ci	int err = 0;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	DEB_EE("\n");
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* there are no hexium orion cards with revision 0 saa7146s */
2078c2ecf20Sopenharmony_ci	if (0 == dev->revision) {
2088c2ecf20Sopenharmony_ci		return -EFAULT;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	hexium = kzalloc(sizeof(*hexium), GFP_KERNEL);
2128c2ecf20Sopenharmony_ci	if (!hexium)
2138c2ecf20Sopenharmony_ci		return -ENOMEM;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* enable i2c-port pins */
2168c2ecf20Sopenharmony_ci	saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	saa7146_write(dev, DD1_INIT, 0x01000100);
2198c2ecf20Sopenharmony_ci	saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2208c2ecf20Sopenharmony_ci	saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	strscpy(hexium->i2c_adapter.name, "hexium orion",
2238c2ecf20Sopenharmony_ci		sizeof(hexium->i2c_adapter.name));
2248c2ecf20Sopenharmony_ci	saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);
2258c2ecf20Sopenharmony_ci	if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {
2268c2ecf20Sopenharmony_ci		DEB_S("cannot register i2c-device. skipping.\n");
2278c2ecf20Sopenharmony_ci		kfree(hexium);
2288c2ecf20Sopenharmony_ci		return -EFAULT;
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* set SAA7110 control GPIO 0 */
2328c2ecf20Sopenharmony_ci	saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTHI);
2338c2ecf20Sopenharmony_ci	/*  set HWControl GPIO number 2 */
2348c2ecf20Sopenharmony_ci	saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	mdelay(10);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/* detect newer Hexium Orion cards by subsystem ids */
2398c2ecf20Sopenharmony_ci	if (0x17c8 == dev->pci->subsystem_vendor && 0x0101 == dev->pci->subsystem_device) {
2408c2ecf20Sopenharmony_ci		pr_info("device is a Hexium Orion w/ 1 SVHS + 3 BNC inputs\n");
2418c2ecf20Sopenharmony_ci		/* we store the pointer in our private data field */
2428c2ecf20Sopenharmony_ci		dev->ext_priv = hexium;
2438c2ecf20Sopenharmony_ci		hexium->type = HEXIUM_ORION_1SVHS_3BNC;
2448c2ecf20Sopenharmony_ci		return 0;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (0x17c8 == dev->pci->subsystem_vendor && 0x2101 == dev->pci->subsystem_device) {
2488c2ecf20Sopenharmony_ci		pr_info("device is a Hexium Orion w/ 4 BNC inputs\n");
2498c2ecf20Sopenharmony_ci		/* we store the pointer in our private data field */
2508c2ecf20Sopenharmony_ci		dev->ext_priv = hexium;
2518c2ecf20Sopenharmony_ci		hexium->type = HEXIUM_ORION_4BNC;
2528c2ecf20Sopenharmony_ci		return 0;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* check if this is an old hexium Orion card by looking at
2568c2ecf20Sopenharmony_ci	   a saa7110 at address 0x4e */
2578c2ecf20Sopenharmony_ci	err = i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_READ,
2588c2ecf20Sopenharmony_ci			     0x00, I2C_SMBUS_BYTE_DATA, &data);
2598c2ecf20Sopenharmony_ci	if (err == 0) {
2608c2ecf20Sopenharmony_ci		pr_info("device is a Hexium HV-PCI6/Orion (old)\n");
2618c2ecf20Sopenharmony_ci		/* we store the pointer in our private data field */
2628c2ecf20Sopenharmony_ci		dev->ext_priv = hexium;
2638c2ecf20Sopenharmony_ci		hexium->type = HEXIUM_HV_PCI6_ORION;
2648c2ecf20Sopenharmony_ci		return 0;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	i2c_del_adapter(&hexium->i2c_adapter);
2688c2ecf20Sopenharmony_ci	kfree(hexium);
2698c2ecf20Sopenharmony_ci	return -EFAULT;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/* bring hardware to a sane state. this has to be done, just in case someone
2738c2ecf20Sopenharmony_ci   wants to capture from this device before it has been properly initialized.
2748c2ecf20Sopenharmony_ci   the capture engine would badly fail, because no valid signal arrives on the
2758c2ecf20Sopenharmony_ci   saa7146, thus leading to timeouts and stuff. */
2768c2ecf20Sopenharmony_cistatic int hexium_init_done(struct saa7146_dev *dev)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct hexium *hexium = (struct hexium *) dev->ext_priv;
2798c2ecf20Sopenharmony_ci	union i2c_smbus_data data;
2808c2ecf20Sopenharmony_ci	int i = 0;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	DEB_D("hexium_init_done called\n");
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* initialize the helper ics to useful values */
2858c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(hexium_saa7110); i++) {
2868c2ecf20Sopenharmony_ci		data.byte = hexium_saa7110[i];
2878c2ecf20Sopenharmony_ci		if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {
2888c2ecf20Sopenharmony_ci			pr_err("failed for address 0x%02x\n", i);
2898c2ecf20Sopenharmony_ci		}
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic int hexium_set_input(struct hexium *hexium, int input)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	union i2c_smbus_data data;
2988c2ecf20Sopenharmony_ci	int i = 0;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	DEB_D("\n");
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
3038c2ecf20Sopenharmony_ci		int adr = hexium_input_select[input].data[i].adr;
3048c2ecf20Sopenharmony_ci		data.byte = hexium_input_select[input].data[i].byte;
3058c2ecf20Sopenharmony_ci		if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x4e, 0, I2C_SMBUS_WRITE, adr, I2C_SMBUS_BYTE_DATA, &data)) {
3068c2ecf20Sopenharmony_ci			return -1;
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci		pr_debug("%d: 0x%02x => 0x%02x\n", input, adr, data.byte);
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	return 0;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	DEB_EE("VIDIOC_ENUMINPUT %d\n", i->index);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if (i->index >= HEXIUM_INPUTS)
3198c2ecf20Sopenharmony_ci		return -EINVAL;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	DEB_D("v4l2_ioctl: VIDIOC_ENUMINPUT %d\n", i->index);
3248c2ecf20Sopenharmony_ci	return 0;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int vidioc_g_input(struct file *file, void *fh, unsigned int *input)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
3308c2ecf20Sopenharmony_ci	struct hexium *hexium = (struct hexium *) dev->ext_priv;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	*input = hexium->cur_input;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	DEB_D("VIDIOC_G_INPUT: %d\n", *input);
3358c2ecf20Sopenharmony_ci	return 0;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic int vidioc_s_input(struct file *file, void *fh, unsigned int input)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;
3418c2ecf20Sopenharmony_ci	struct hexium *hexium = (struct hexium *) dev->ext_priv;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (input >= HEXIUM_INPUTS)
3448c2ecf20Sopenharmony_ci		return -EINVAL;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	hexium->cur_input = input;
3478c2ecf20Sopenharmony_ci	hexium_set_input(hexium, input);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return 0;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic struct saa7146_ext_vv vv_data;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci/* this function only gets called when the probing was successful */
3558c2ecf20Sopenharmony_cistatic int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct hexium *hexium = (struct hexium *) dev->ext_priv;
3588c2ecf20Sopenharmony_ci	int ret;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	DEB_EE("\n");
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ret = saa7146_vv_init(dev, &vv_data);
3638c2ecf20Sopenharmony_ci	if (ret) {
3648c2ecf20Sopenharmony_ci		pr_err("Error in saa7146_vv_init()\n");
3658c2ecf20Sopenharmony_ci		return ret;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	vv_data.vid_ops.vidioc_enum_input = vidioc_enum_input;
3698c2ecf20Sopenharmony_ci	vv_data.vid_ops.vidioc_g_input = vidioc_g_input;
3708c2ecf20Sopenharmony_ci	vv_data.vid_ops.vidioc_s_input = vidioc_s_input;
3718c2ecf20Sopenharmony_ci	if (0 != saa7146_register_device(&hexium->video_dev, dev, "hexium orion", VFL_TYPE_VIDEO)) {
3728c2ecf20Sopenharmony_ci		pr_err("cannot register capture v4l2 device. skipping.\n");
3738c2ecf20Sopenharmony_ci		return -1;
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	pr_err("found 'hexium orion' frame grabber-%d\n", hexium_num);
3778c2ecf20Sopenharmony_ci	hexium_num++;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* the rest */
3808c2ecf20Sopenharmony_ci	hexium->cur_input = 0;
3818c2ecf20Sopenharmony_ci	hexium_init_done(dev);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return 0;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int hexium_detach(struct saa7146_dev *dev)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	struct hexium *hexium = (struct hexium *) dev->ext_priv;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	DEB_EE("dev:%p\n", dev);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	saa7146_unregister_device(&hexium->video_dev, dev);
3938c2ecf20Sopenharmony_ci	saa7146_vv_release(dev);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	hexium_num--;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	i2c_del_adapter(&hexium->i2c_adapter);
3988c2ecf20Sopenharmony_ci	kfree(hexium);
3998c2ecf20Sopenharmony_ci	return 0;
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	return 0;
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic struct saa7146_extension extension;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic struct saa7146_pci_extension_data hexium_hv_pci6 = {
4108c2ecf20Sopenharmony_ci	.ext_priv = "Hexium HV-PCI6 / Orion",
4118c2ecf20Sopenharmony_ci	.ext = &extension,
4128c2ecf20Sopenharmony_ci};
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic struct saa7146_pci_extension_data hexium_orion_1svhs_3bnc = {
4158c2ecf20Sopenharmony_ci	.ext_priv = "Hexium HV-PCI6 / Orion (1 SVHS/3 BNC)",
4168c2ecf20Sopenharmony_ci	.ext = &extension,
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic struct saa7146_pci_extension_data hexium_orion_4bnc = {
4208c2ecf20Sopenharmony_ci	.ext_priv = "Hexium HV-PCI6 / Orion (4 BNC)",
4218c2ecf20Sopenharmony_ci	.ext = &extension,
4228c2ecf20Sopenharmony_ci};
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic const struct pci_device_id pci_tbl[] = {
4258c2ecf20Sopenharmony_ci	{
4268c2ecf20Sopenharmony_ci	 .vendor = PCI_VENDOR_ID_PHILIPS,
4278c2ecf20Sopenharmony_ci	 .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
4288c2ecf20Sopenharmony_ci	 .subvendor = 0x0000,
4298c2ecf20Sopenharmony_ci	 .subdevice = 0x0000,
4308c2ecf20Sopenharmony_ci	 .driver_data = (unsigned long) &hexium_hv_pci6,
4318c2ecf20Sopenharmony_ci	 },
4328c2ecf20Sopenharmony_ci	{
4338c2ecf20Sopenharmony_ci	 .vendor = PCI_VENDOR_ID_PHILIPS,
4348c2ecf20Sopenharmony_ci	 .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
4358c2ecf20Sopenharmony_ci	 .subvendor = 0x17c8,
4368c2ecf20Sopenharmony_ci	 .subdevice = 0x0101,
4378c2ecf20Sopenharmony_ci	 .driver_data = (unsigned long) &hexium_orion_1svhs_3bnc,
4388c2ecf20Sopenharmony_ci	 },
4398c2ecf20Sopenharmony_ci	{
4408c2ecf20Sopenharmony_ci	 .vendor = PCI_VENDOR_ID_PHILIPS,
4418c2ecf20Sopenharmony_ci	 .device = PCI_DEVICE_ID_PHILIPS_SAA7146,
4428c2ecf20Sopenharmony_ci	 .subvendor = 0x17c8,
4438c2ecf20Sopenharmony_ci	 .subdevice = 0x2101,
4448c2ecf20Sopenharmony_ci	 .driver_data = (unsigned long) &hexium_orion_4bnc,
4458c2ecf20Sopenharmony_ci	 },
4468c2ecf20Sopenharmony_ci	{
4478c2ecf20Sopenharmony_ci	 .vendor = 0,
4488c2ecf20Sopenharmony_ci	 }
4498c2ecf20Sopenharmony_ci};
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, pci_tbl);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic struct saa7146_ext_vv vv_data = {
4548c2ecf20Sopenharmony_ci	.inputs = HEXIUM_INPUTS,
4558c2ecf20Sopenharmony_ci	.capabilities = 0,
4568c2ecf20Sopenharmony_ci	.stds = &hexium_standards[0],
4578c2ecf20Sopenharmony_ci	.num_stds = ARRAY_SIZE(hexium_standards),
4588c2ecf20Sopenharmony_ci	.std_callback = &std_callback,
4598c2ecf20Sopenharmony_ci};
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic struct saa7146_extension extension = {
4628c2ecf20Sopenharmony_ci	.name = "hexium HV-PCI6 Orion",
4638c2ecf20Sopenharmony_ci	.flags = 0,		// SAA7146_USE_I2C_IRQ,
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	.pci_tbl = &pci_tbl[0],
4668c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	.probe = hexium_probe,
4698c2ecf20Sopenharmony_ci	.attach = hexium_attach,
4708c2ecf20Sopenharmony_ci	.detach = hexium_detach,
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	.irq_mask = 0,
4738c2ecf20Sopenharmony_ci	.irq_func = NULL,
4748c2ecf20Sopenharmony_ci};
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic int __init hexium_init_module(void)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	if (0 != saa7146_register_extension(&extension)) {
4798c2ecf20Sopenharmony_ci		DEB_S("failed to register extension\n");
4808c2ecf20Sopenharmony_ci		return -ENODEV;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	return 0;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic void __exit hexium_cleanup_module(void)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	saa7146_unregister_extension(&extension);
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_cimodule_init(hexium_init_module);
4928c2ecf20Sopenharmony_cimodule_exit(hexium_cleanup_module);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("video4linux-2 driver for Hexium Orion frame grabber cards");
4958c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
4968c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
497