Path: blob/master/drivers/media/video/hexium_gemini.c
17540 views
/*1hexium_gemini.c - v4l2 driver for Hexium Gemini frame grabber cards23Visit http://www.mihu.de/linux/saa7146/ and follow the link4to "hexium" for further details about this card.56Copyright (C) 2003 Michael Hunold <[email protected]>78This program is free software; you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation; either version 2 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program; if not, write to the Free Software20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.21*/2223#define DEBUG_VARIABLE debug2425#include <media/saa7146_vv.h>2627static int debug;28module_param(debug, int, 0);29MODULE_PARM_DESC(debug, "debug verbosity");3031/* global variables */32static int hexium_num;3334#define HEXIUM_GEMINI 435#define HEXIUM_GEMINI_DUAL 53637#define HEXIUM_INPUTS 938static struct v4l2_input hexium_inputs[HEXIUM_INPUTS] = {39{ 0, "CVBS 1", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },40{ 1, "CVBS 2", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },41{ 2, "CVBS 3", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },42{ 3, "CVBS 4", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },43{ 4, "CVBS 5", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },44{ 5, "CVBS 6", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },45{ 6, "Y/C 1", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },46{ 7, "Y/C 2", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },47{ 8, "Y/C 3", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG|V4L2_STD_NTSC_M, 0, V4L2_IN_CAP_STD },48};4950#define HEXIUM_AUDIOS 05152struct hexium_data53{54s8 adr;55u8 byte;56};5758#define HEXIUM_CONTROLS 159static struct v4l2_queryctrl hexium_controls[] = {60{ V4L2_CID_PRIVATE_BASE, V4L2_CTRL_TYPE_BOOLEAN, "B/W", 0, 1, 1, 0, 0 },61};6263#define HEXIUM_GEMINI_V_1_0 164#define HEXIUM_GEMINI_DUAL_V_1_0 26566struct hexium67{68int type;6970struct video_device *video_dev;71struct i2c_adapter i2c_adapter;7273int cur_input; /* current input */74v4l2_std_id cur_std; /* current standard */75int cur_bw; /* current black/white status */76};7778/* Samsung KS0127B decoder default registers */79static u8 hexium_ks0127b[0x100]={80/*00*/ 0x00,0x52,0x30,0x40,0x01,0x0C,0x2A,0x10,81/*08*/ 0x00,0x00,0x00,0x60,0x00,0x00,0x0F,0x06,82/*10*/ 0x00,0x00,0xE4,0xC0,0x00,0x00,0x00,0x00,83/*18*/ 0x14,0x9B,0xFE,0xFF,0xFC,0xFF,0x03,0x22,84/*20*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,85/*28*/ 0x00,0x00,0x00,0x00,0x00,0x2C,0x9B,0x00,86/*30*/ 0x00,0x00,0x10,0x80,0x80,0x10,0x80,0x80,87/*38*/ 0x01,0x04,0x00,0x00,0x00,0x29,0xC0,0x00,88/*40*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,89/*48*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,90/*50*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,91/*58*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,92/*60*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,93/*68*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,94/*70*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,95/*78*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,96/*80*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,97/*88*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,98/*90*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,99/*98*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,100/*A0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,101/*A8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,102/*B0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,103/*B8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,104/*C0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,105/*C8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,106/*D0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,107/*D8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,108/*E0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,109/*E8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,110/*F0*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,111/*F8*/ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00112};113114static struct hexium_data hexium_pal[] = {115{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }116};117118static struct hexium_data hexium_pal_bw[] = {119{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }120};121122static struct hexium_data hexium_ntsc[] = {123{ 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }124};125126static struct hexium_data hexium_ntsc_bw[] = {127{ 0x01, 0x53 }, { 0x12, 0x04 }, { 0x2D, 0x23 }, { 0x2E, 0x81 }, { -1 , 0xFF }128};129130static struct hexium_data hexium_secam[] = {131{ 0x01, 0x52 }, { 0x12, 0x64 }, { 0x2D, 0x2C }, { 0x2E, 0x9B }, { -1 , 0xFF }132};133134static struct hexium_data hexium_input_select[] = {135{ 0x02, 0x60 },136{ 0x02, 0x64 },137{ 0x02, 0x61 },138{ 0x02, 0x65 },139{ 0x02, 0x62 },140{ 0x02, 0x66 },141{ 0x02, 0x68 },142{ 0x02, 0x69 },143{ 0x02, 0x6A },144};145146/* fixme: h_offset = 0 for Hexium Gemini *Dual*, which147are currently *not* supported*/148static struct saa7146_standard hexium_standards[] = {149{150.name = "PAL", .id = V4L2_STD_PAL,151.v_offset = 28, .v_field = 288,152.h_offset = 1, .h_pixels = 680,153.v_max_out = 576, .h_max_out = 768,154}, {155.name = "NTSC", .id = V4L2_STD_NTSC,156.v_offset = 28, .v_field = 240,157.h_offset = 1, .h_pixels = 640,158.v_max_out = 480, .h_max_out = 640,159}, {160.name = "SECAM", .id = V4L2_STD_SECAM,161.v_offset = 28, .v_field = 288,162.h_offset = 1, .h_pixels = 720,163.v_max_out = 576, .h_max_out = 768,164}165};166167/* bring hardware to a sane state. this has to be done, just in case someone168wants to capture from this device before it has been properly initialized.169the capture engine would badly fail, because no valid signal arrives on the170saa7146, thus leading to timeouts and stuff. */171static int hexium_init_done(struct saa7146_dev *dev)172{173struct hexium *hexium = (struct hexium *) dev->ext_priv;174union i2c_smbus_data data;175int i = 0;176177DEB_D(("hexium_init_done called.\n"));178179/* initialize the helper ics to useful values */180for (i = 0; i < sizeof(hexium_ks0127b); i++) {181data.byte = hexium_ks0127b[i];182if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, i, I2C_SMBUS_BYTE_DATA, &data)) {183printk("hexium_gemini: hexium_init_done() failed for address 0x%02x\n", i);184}185}186187return 0;188}189190static int hexium_set_input(struct hexium *hexium, int input)191{192union i2c_smbus_data data;193194DEB_D((".\n"));195196data.byte = hexium_input_select[input].byte;197if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, hexium_input_select[input].adr, I2C_SMBUS_BYTE_DATA, &data)) {198return -1;199}200201return 0;202}203204static int hexium_set_standard(struct hexium *hexium, struct hexium_data *vdec)205{206union i2c_smbus_data data;207int i = 0;208209DEB_D((".\n"));210211while (vdec[i].adr != -1) {212data.byte = vdec[i].byte;213if (0 != i2c_smbus_xfer(&hexium->i2c_adapter, 0x6c, 0, I2C_SMBUS_WRITE, vdec[i].adr, I2C_SMBUS_BYTE_DATA, &data)) {214printk("hexium_init_done: hexium_set_standard() failed for address 0x%02x\n", i);215return -1;216}217i++;218}219return 0;220}221222static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *i)223{224DEB_EE(("VIDIOC_ENUMINPUT %d.\n", i->index));225226if (i->index >= HEXIUM_INPUTS)227return -EINVAL;228229memcpy(i, &hexium_inputs[i->index], sizeof(struct v4l2_input));230231DEB_D(("v4l2_ioctl: VIDIOC_ENUMINPUT %d.\n", i->index));232return 0;233}234235static int vidioc_g_input(struct file *file, void *fh, unsigned int *input)236{237struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;238struct hexium *hexium = (struct hexium *) dev->ext_priv;239240*input = hexium->cur_input;241242DEB_D(("VIDIOC_G_INPUT: %d\n", *input));243return 0;244}245246static int vidioc_s_input(struct file *file, void *fh, unsigned int input)247{248struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;249struct hexium *hexium = (struct hexium *) dev->ext_priv;250251DEB_EE(("VIDIOC_S_INPUT %d.\n", input));252253if (input >= HEXIUM_INPUTS)254return -EINVAL;255256hexium->cur_input = input;257hexium_set_input(hexium, input);258return 0;259}260261/* the saa7146 provides some controls (brightness, contrast, saturation)262which gets registered *after* this function. because of this we have263to return with a value != 0 even if the function succeeded.. */264static int vidioc_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qc)265{266struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;267int i;268269for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {270if (hexium_controls[i].id == qc->id) {271*qc = hexium_controls[i];272DEB_D(("VIDIOC_QUERYCTRL %d.\n", qc->id));273return 0;274}275}276return dev->ext_vv_data->core_ops->vidioc_queryctrl(file, fh, qc);277}278279static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *vc)280{281struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;282struct hexium *hexium = (struct hexium *) dev->ext_priv;283int i;284285for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {286if (hexium_controls[i].id == vc->id)287break;288}289290if (i < 0)291return dev->ext_vv_data->core_ops->vidioc_g_ctrl(file, fh, vc);292293if (vc->id == V4L2_CID_PRIVATE_BASE) {294vc->value = hexium->cur_bw;295DEB_D(("VIDIOC_G_CTRL BW:%d.\n", vc->value));296return 0;297}298return -EINVAL;299}300301static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *vc)302{303struct saa7146_dev *dev = ((struct saa7146_fh *)fh)->dev;304struct hexium *hexium = (struct hexium *) dev->ext_priv;305int i = 0;306307for (i = HEXIUM_CONTROLS - 1; i >= 0; i--) {308if (hexium_controls[i].id == vc->id)309break;310}311312if (i < 0)313return dev->ext_vv_data->core_ops->vidioc_s_ctrl(file, fh, vc);314315if (vc->id == V4L2_CID_PRIVATE_BASE)316hexium->cur_bw = vc->value;317318DEB_D(("VIDIOC_S_CTRL BW:%d.\n", hexium->cur_bw));319320if (0 == hexium->cur_bw && V4L2_STD_PAL == hexium->cur_std) {321hexium_set_standard(hexium, hexium_pal);322return 0;323}324if (0 == hexium->cur_bw && V4L2_STD_NTSC == hexium->cur_std) {325hexium_set_standard(hexium, hexium_ntsc);326return 0;327}328if (0 == hexium->cur_bw && V4L2_STD_SECAM == hexium->cur_std) {329hexium_set_standard(hexium, hexium_secam);330return 0;331}332if (1 == hexium->cur_bw && V4L2_STD_PAL == hexium->cur_std) {333hexium_set_standard(hexium, hexium_pal_bw);334return 0;335}336if (1 == hexium->cur_bw && V4L2_STD_NTSC == hexium->cur_std) {337hexium_set_standard(hexium, hexium_ntsc_bw);338return 0;339}340if (1 == hexium->cur_bw && V4L2_STD_SECAM == hexium->cur_std)341/* fixme: is there no bw secam mode? */342return -EINVAL;343344return -EINVAL;345}346347348static struct saa7146_ext_vv vv_data;349350/* this function only gets called when the probing was successful */351static int hexium_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)352{353struct hexium *hexium = (struct hexium *) dev->ext_priv;354int ret;355356DEB_EE((".\n"));357358hexium = kzalloc(sizeof(struct hexium), GFP_KERNEL);359if (NULL == hexium) {360printk("hexium_gemini: not enough kernel memory in hexium_attach().\n");361return -ENOMEM;362}363dev->ext_priv = hexium;364365/* enable i2c-port pins */366saa7146_write(dev, MC1, (MASK_08 | MASK_24 | MASK_10 | MASK_26));367368hexium->i2c_adapter = (struct i2c_adapter) {369.name = "hexium gemini",370};371saa7146_i2c_adapter_prepare(dev, &hexium->i2c_adapter, SAA7146_I2C_BUS_BIT_RATE_480);372if (i2c_add_adapter(&hexium->i2c_adapter) < 0) {373DEB_S(("cannot register i2c-device. skipping.\n"));374kfree(hexium);375return -EFAULT;376}377378/* set HWControl GPIO number 2 */379saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);380381saa7146_write(dev, DD1_INIT, 0x07000700);382saa7146_write(dev, DD1_STREAM_B, 0x00000000);383saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));384385/* the rest */386hexium->cur_input = 0;387hexium_init_done(dev);388389hexium_set_standard(hexium, hexium_pal);390hexium->cur_std = V4L2_STD_PAL;391392hexium_set_input(hexium, 0);393hexium->cur_input = 0;394395saa7146_vv_init(dev, &vv_data);396vv_data.ops.vidioc_queryctrl = vidioc_queryctrl;397vv_data.ops.vidioc_g_ctrl = vidioc_g_ctrl;398vv_data.ops.vidioc_s_ctrl = vidioc_s_ctrl;399vv_data.ops.vidioc_enum_input = vidioc_enum_input;400vv_data.ops.vidioc_g_input = vidioc_g_input;401vv_data.ops.vidioc_s_input = vidioc_s_input;402ret = saa7146_register_device(&hexium->video_dev, dev, "hexium gemini", VFL_TYPE_GRABBER);403if (ret < 0) {404printk("hexium_gemini: cannot register capture v4l2 device. skipping.\n");405return ret;406}407408printk("hexium_gemini: found 'hexium gemini' frame grabber-%d.\n", hexium_num);409hexium_num++;410411return 0;412}413414static int hexium_detach(struct saa7146_dev *dev)415{416struct hexium *hexium = (struct hexium *) dev->ext_priv;417418DEB_EE(("dev:%p\n", dev));419420saa7146_unregister_device(&hexium->video_dev, dev);421saa7146_vv_release(dev);422423hexium_num--;424425i2c_del_adapter(&hexium->i2c_adapter);426kfree(hexium);427return 0;428}429430static int std_callback(struct saa7146_dev *dev, struct saa7146_standard *std)431{432struct hexium *hexium = (struct hexium *) dev->ext_priv;433434if (V4L2_STD_PAL == std->id) {435hexium_set_standard(hexium, hexium_pal);436hexium->cur_std = V4L2_STD_PAL;437return 0;438} else if (V4L2_STD_NTSC == std->id) {439hexium_set_standard(hexium, hexium_ntsc);440hexium->cur_std = V4L2_STD_NTSC;441return 0;442} else if (V4L2_STD_SECAM == std->id) {443hexium_set_standard(hexium, hexium_secam);444hexium->cur_std = V4L2_STD_SECAM;445return 0;446}447448return -1;449}450451static struct saa7146_extension hexium_extension;452453static struct saa7146_pci_extension_data hexium_gemini_4bnc = {454.ext_priv = "Hexium Gemini (4 BNC)",455.ext = &hexium_extension,456};457458static struct saa7146_pci_extension_data hexium_gemini_dual_4bnc = {459.ext_priv = "Hexium Gemini Dual (4 BNC)",460.ext = &hexium_extension,461};462463static struct pci_device_id pci_tbl[] = {464{465.vendor = PCI_VENDOR_ID_PHILIPS,466.device = PCI_DEVICE_ID_PHILIPS_SAA7146,467.subvendor = 0x17c8,468.subdevice = 0x2401,469.driver_data = (unsigned long) &hexium_gemini_4bnc,470},471{472.vendor = PCI_VENDOR_ID_PHILIPS,473.device = PCI_DEVICE_ID_PHILIPS_SAA7146,474.subvendor = 0x17c8,475.subdevice = 0x2402,476.driver_data = (unsigned long) &hexium_gemini_dual_4bnc,477},478{479.vendor = 0,480}481};482483MODULE_DEVICE_TABLE(pci, pci_tbl);484485static struct saa7146_ext_vv vv_data = {486.inputs = HEXIUM_INPUTS,487.capabilities = 0,488.stds = &hexium_standards[0],489.num_stds = sizeof(hexium_standards) / sizeof(struct saa7146_standard),490.std_callback = &std_callback,491};492493static struct saa7146_extension hexium_extension = {494.name = "hexium gemini",495.flags = SAA7146_USE_I2C_IRQ,496497.pci_tbl = &pci_tbl[0],498.module = THIS_MODULE,499500.attach = hexium_attach,501.detach = hexium_detach,502503.irq_mask = 0,504.irq_func = NULL,505};506507static int __init hexium_init_module(void)508{509if (0 != saa7146_register_extension(&hexium_extension)) {510DEB_S(("failed to register extension.\n"));511return -ENODEV;512}513514return 0;515}516517static void __exit hexium_cleanup_module(void)518{519saa7146_unregister_extension(&hexium_extension);520}521522module_init(hexium_init_module);523module_exit(hexium_cleanup_module);524525MODULE_DESCRIPTION("video4linux-2 driver for Hexium Gemini frame grabber cards");526MODULE_AUTHOR("Michael Hunold <[email protected]>");527MODULE_LICENSE("GPL");528529530