2 * Copyright (C) 2008 Sensoray Company Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/usb.h>
21 #include <linux/i2c.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
24 #include "go7007-priv.h"
27 extern int s2250loader_init(void);
28 extern void s2250loader_cleanup(void);
30 #define TLV320_ADDRESS 0x34
31 #define S2250_VIDDEC 0x86
32 #define VPX322_ADDR_ANALOGCONTROL1 0x02
33 #define VPX322_ADDR_BRIGHTNESS0 0x0127
34 #define VPX322_ADDR_BRIGHTNESS1 0x0131
35 #define VPX322_ADDR_CONTRAST0 0x0128
36 #define VPX322_ADDR_CONTRAST1 0x0132
37 #define VPX322_ADDR_HUE 0x00dc
38 #define VPX322_ADDR_SAT 0x0030
40 struct go7007_usb_board {
42 struct go7007_board_info main_info;
46 struct go7007_usb_board *board;
47 struct semaphore i2c_lock;
48 struct usb_device *usbdev;
49 struct urb *video_urbs[8];
50 struct urb *audio_urbs[8];
54 static unsigned char aud_regs[] = {
72 static unsigned char vid_regs[] = {
79 static u16 vid_regs_fp[] = {
104 /* PAL specific values */
105 static u16 vid_regs_fp_pal[] =
128 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
129 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
130 u16 value, u16 index, void *transfer_buffer, int length, int in)
132 struct go7007_usb *usb = go->hpi_context;
136 return usb_control_msg(usb->usbdev,
137 usb_rcvctrlpipe(usb->usbdev, 0), request,
138 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
139 value, index, transfer_buffer, length, timeout);
141 return usb_control_msg(usb->usbdev,
142 usb_sndctrlpipe(usb->usbdev, 0), request,
143 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
144 value, index, transfer_buffer, length, timeout);
147 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
149 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
151 struct go7007 *go = i2c_get_adapdata(client->adapter);
152 struct go7007_usb *usb;
154 int dev_addr = client->addr;
160 if (go->status == STATUS_SHUTDOWN)
163 buf = kzalloc(16, GFP_KERNEL);
167 usb = go->hpi_context;
168 if (down_interruptible(&usb->i2c_lock) != 0) {
169 printk(KERN_INFO "i2c lock failed\n");
173 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
183 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
185 struct go7007 *go = i2c_get_adapdata(client->adapter);
186 struct go7007_usb *usb;
188 struct s2250 *dec = i2c_get_clientdata(client);
193 if (go->status == STATUS_SHUTDOWN)
196 buf = kzalloc(16, GFP_KERNEL);
203 memset(buf, 0xcd, 6);
205 usb = go->hpi_context;
206 if (down_interruptible(&usb->i2c_lock) != 0) {
207 printk(KERN_INFO "i2c lock failed\n");
210 if (go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1) < 0)
215 unsigned int subaddr, val_read;
217 subaddr = (buf[4] << 8) + buf[5];
218 val_read = (buf[2] << 8) + buf[3];
219 if (val_read != val) {
220 printk(KERN_INFO "invalid fp write %x %x\n",
224 if (subaddr != addr) {
225 printk(KERN_INFO "invalid fp write addr %x %x\n",
232 /* save last 12b value */
234 dec->reg12b_val = val;
239 static int write_regs(struct i2c_client *client, u8 *regs)
243 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
244 if (write_reg(client, regs[i], regs[i+1]) < 0) {
245 printk(KERN_INFO "s2250: failed\n");
252 static int write_regs_fp(struct i2c_client *client, u16 *regs)
256 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
257 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
258 printk(KERN_INFO "s2250: failed fp\n");
266 static int s2250_command(struct i2c_client *client,
267 unsigned int cmd, void *arg)
269 struct s2250 *dec = i2c_get_clientdata(client);
277 vidsys = (dec->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
280 write_reg_fp(client, 0x20, 0x020 | vidsys);
281 write_reg_fp(client, 0x21, 0x662);
282 write_reg_fp(client, 0x140, 0x060);
285 write_reg_fp(client, 0x20, 0x040 | vidsys);
286 write_reg_fp(client, 0x21, 0x666);
287 write_reg_fp(client, 0x140, 0x060);
294 v4l2_std_id *std = arg;
297 vidsource = (dec->input == 1) ? 0x040 : 0x020;
301 write_regs_fp(client, vid_regs_fp);
302 write_reg_fp(client, 0x20, vidsource | 1);
305 write_regs_fp(client, vid_regs_fp);
306 write_regs_fp(client, vid_regs_fp_pal);
307 write_reg_fp(client, 0x20, vidsource);
314 case VIDIOC_QUERYCTRL:
316 struct v4l2_queryctrl *ctrl = arg;
317 static const u32 user_ctrls[] = {
324 static const u32 *ctrl_classes[] = {
329 ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
331 case V4L2_CID_BRIGHTNESS:
332 v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
334 case V4L2_CID_CONTRAST:
335 v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
337 case V4L2_CID_SATURATION:
338 v4l2_ctrl_query_fill(ctrl, 0, 100, 1, 50);
341 v4l2_ctrl_query_fill(ctrl, -50, 50, 1, 0);
344 ctrl->name[0] = '\0';
351 struct v4l2_control *ctrl = arg;
355 case V4L2_CID_BRIGHTNESS:
356 printk(KERN_INFO "s2250: future setting\n");
358 case V4L2_CID_CONTRAST:
359 printk(KERN_INFO "s2250: future setting\n");
362 case V4L2_CID_SATURATION:
363 if (ctrl->value > 127)
364 dec->saturation = 127;
365 else if (ctrl->value < 0)
368 dec->saturation = ctrl->value;
370 value1 = dec->saturation * 4140 / 100;
373 write_reg_fp(client, VPX322_ADDR_SAT, value1);
376 if (ctrl->value > 50)
378 else if (ctrl->value < -50)
381 dec->hue = ctrl->value;
382 /* clamp the hue range */
383 value1 = dec->hue * 280 / 50;
384 write_reg_fp(client, VPX322_ADDR_HUE, value1);
391 struct v4l2_control *ctrl = arg;
394 case V4L2_CID_BRIGHTNESS:
395 ctrl->value = dec->brightness;
397 case V4L2_CID_CONTRAST:
398 ctrl->value = dec->contrast;
400 case V4L2_CID_SATURATION:
401 ctrl->value = dec->saturation;
404 ctrl->value = dec->hue;
411 struct v4l2_format *fmt = arg;
412 if (fmt->fmt.pix.height < 640) {
413 write_reg_fp(client, 0x12b, dec->reg12b_val | 0x400);
414 write_reg_fp(client, 0x140, 0x060);
416 write_reg_fp(client, 0x12b, dec->reg12b_val & ~0x400);
417 write_reg_fp(client, 0x140, 0x060);
423 struct v4l2_audio *audio = arg;
425 memset(audio, 0, sizeof(*audio));
426 audio->index = dec->audio_input;
429 case VIDIOC_ENUMAUDIO:
431 struct v4l2_audio *audio = arg;
433 switch (audio->index) {
435 strcpy(audio->name, "Line In");
438 strcpy(audio->name, "Mic");
441 strcpy(audio->name, "Mic Boost");
444 audio->name[0] = '\0';
447 audio->capability = V4L2_AUDCAP_STEREO;
453 struct v4l2_audio *audio = arg;
455 client->addr = TLV320_ADDRESS;
456 switch (audio->index) {
458 write_reg(client, 0x08, 0x02); /* Line In */
461 write_reg(client, 0x08, 0x04); /* Mic */
464 write_reg(client, 0x08, 0x05); /* Mic Boost */
469 dec->audio_input = audio->index;
474 printk(KERN_INFO "s2250: unknown command 0x%x\n", cmd);
480 static struct i2c_driver s2250_driver;
482 static struct i2c_client s2250_client_templ = {
483 .name = "Sensoray 2250",
484 .driver = &s2250_driver,
487 static int s2250_detect(struct i2c_adapter *adapter, int addr, int kind)
489 struct i2c_client *client;
492 struct go7007 *go = i2c_get_adapdata(adapter);
493 struct go7007_usb *usb = go->hpi_context;
495 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
498 memcpy(client, &s2250_client_templ,
499 sizeof(s2250_client_templ));
500 client->adapter = adapter;
502 dec = kmalloc(sizeof(struct s2250), GFP_KERNEL);
508 dec->std = V4L2_STD_NTSC;
509 dec->brightness = 50;
511 dec->saturation = 50;
513 client->addr = TLV320_ADDRESS;
514 i2c_set_clientdata(client, dec);
517 "s2250: initializing video decoder on %s\n",
520 /* initialize the audio */
521 client->addr = TLV320_ADDRESS;
522 if (write_regs(client, aud_regs) < 0) {
524 "s2250: error initializing audio\n");
529 client->addr = S2250_VIDDEC;
530 i2c_set_clientdata(client, dec);
532 if (write_regs(client, vid_regs) < 0) {
534 "s2250: error initializing decoder\n");
539 if (write_regs_fp(client, vid_regs_fp) < 0) {
541 "s2250: error initializing decoder\n");
546 /* set default channel */
548 write_reg_fp(client, 0x20, 0x020 | 1);
549 write_reg_fp(client, 0x21, 0x662);
550 write_reg_fp(client, 0x140, 0x060);
552 /* set default audio input */
553 dec->audio_input = 0;
554 write_reg(client, 0x08, 0x02); /* Line In */
556 if (down_interruptible(&usb->i2c_lock) == 0) {
557 data = kzalloc(16, GFP_KERNEL);
560 rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
568 go7007_usb_vendor_request(go, 0x40, 0,
578 i2c_attach_client(client);
579 printk("s2250: initialized successfully\n");
583 static int s2250_detach(struct i2c_client *client)
585 struct s2250 *dec = i2c_get_clientdata(client);
588 r = i2c_detach_client(client);
597 static struct i2c_driver s2250_driver = {
599 .name = "Sensoray 2250 board driver",
601 .id = I2C_DRIVERID_S2250,
602 .detach_client = s2250_detach,
603 .command = s2250_command,
606 static int __init s2250_init(void)
610 r = s2250loader_init();
614 r = i2c_add_driver(&s2250_driver);
617 return wis_i2c_add_driver(s2250_driver.id, s2250_detect);
620 static void __exit s2250_cleanup(void)
622 wis_i2c_del_driver(s2250_detect);
623 i2c_del_driver(&s2250_driver);
625 s2250loader_cleanup();
628 module_init(s2250_init);
629 module_exit(s2250_cleanup);
632 MODULE_DESCRIPTION("Board driver for Sensoryray 2250");
633 MODULE_LICENSE("GPL v2");