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 "s2250-loader.h"
25 #include "go7007-priv.h"
28 #define TLV320_ADDRESS 0x34
29 #define VPX322_ADDR_ANALOGCONTROL1 0x02
30 #define VPX322_ADDR_BRIGHTNESS0 0x0127
31 #define VPX322_ADDR_BRIGHTNESS1 0x0131
32 #define VPX322_ADDR_CONTRAST0 0x0128
33 #define VPX322_ADDR_CONTRAST1 0x0132
34 #define VPX322_ADDR_HUE 0x00dc
35 #define VPX322_ADDR_SAT 0x0030
37 struct go7007_usb_board
{
39 struct go7007_board_info main_info
;
43 struct go7007_usb_board
*board
;
44 struct mutex i2c_lock
;
45 struct usb_device
*usbdev
;
46 struct urb
*video_urbs
[8];
47 struct urb
*audio_urbs
[8];
51 static unsigned char aud_regs
[] = {
69 static unsigned char vid_regs
[] = {
76 static u16 vid_regs_fp
[] = {
101 /* PAL specific values */
102 static u16 vid_regs_fp_pal
[] =
123 struct i2c_client
*audio
;
126 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
127 static int go7007_usb_vendor_request(struct go7007
*go
, u16 request
,
128 u16 value
, u16 index
, void *transfer_buffer
, int length
, int in
)
130 struct go7007_usb
*usb
= go
->hpi_context
;
134 return usb_control_msg(usb
->usbdev
,
135 usb_rcvctrlpipe(usb
->usbdev
, 0), request
,
136 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
137 value
, index
, transfer_buffer
, length
, timeout
);
139 return usb_control_msg(usb
->usbdev
,
140 usb_sndctrlpipe(usb
->usbdev
, 0), request
,
141 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
142 value
, index
, transfer_buffer
, length
, timeout
);
145 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
147 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
)
149 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
150 struct go7007_usb
*usb
;
152 int dev_addr
= client
->addr
;
158 if (go
->status
== STATUS_SHUTDOWN
)
161 buf
= kzalloc(16, GFP_KERNEL
);
165 usb
= go
->hpi_context
;
166 if (mutex_lock_interruptible(&usb
->i2c_lock
) != 0) {
167 printk(KERN_INFO
"i2c lock failed\n");
171 rc
= go7007_usb_vendor_request(go
, 0x55, dev_addr
,
176 mutex_unlock(&usb
->i2c_lock
);
181 static int write_reg_fp(struct i2c_client
*client
, u16 addr
, u16 val
)
183 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
184 struct go7007_usb
*usb
;
186 struct s2250
*dec
= i2c_get_clientdata(client
);
191 if (go
->status
== STATUS_SHUTDOWN
)
194 buf
= kzalloc(16, GFP_KERNEL
);
201 memset(buf
, 0xcd, 6);
203 usb
= go
->hpi_context
;
204 if (mutex_lock_interruptible(&usb
->i2c_lock
) != 0) {
205 printk(KERN_INFO
"i2c lock failed\n");
209 if (go7007_usb_vendor_request(go
, 0x57, addr
, val
, buf
, 16, 1) < 0) {
214 mutex_unlock(&usb
->i2c_lock
);
216 unsigned int subaddr
, val_read
;
218 subaddr
= (buf
[4] << 8) + buf
[5];
219 val_read
= (buf
[2] << 8) + buf
[3];
221 if (val_read
!= val
) {
222 printk(KERN_INFO
"invalid fp write %x %x\n",
226 if (subaddr
!= addr
) {
227 printk(KERN_INFO
"invalid fp write addr %x %x\n",
236 /* save last 12b value */
238 dec
->reg12b_val
= val
;
243 static int read_reg_fp(struct i2c_client
*client
, u16 addr
, u16
*val
)
245 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
246 struct go7007_usb
*usb
;
252 if (go
->status
== STATUS_SHUTDOWN
)
255 buf
= kzalloc(16, GFP_KERNEL
);
262 memset(buf
, 0xcd, 6);
263 usb
= go
->hpi_context
;
264 if (down_interruptible(&usb
->i2c_lock
) != 0) {
265 printk(KERN_INFO
"i2c lock failed\n");
269 if (go7007_usb_vendor_request(go
, 0x58, addr
, 0, buf
, 16, 1) < 0) {
275 *val
= (buf
[0] << 8) | buf
[1];
282 static int write_regs(struct i2c_client
*client
, u8
*regs
)
286 for (i
= 0; !((regs
[i
] == 0x00) && (regs
[i
+1] == 0x00)); i
+= 2) {
287 if (write_reg(client
, regs
[i
], regs
[i
+1]) < 0) {
288 printk(KERN_INFO
"s2250: failed\n");
295 static int write_regs_fp(struct i2c_client
*client
, u16
*regs
)
299 for (i
= 0; !((regs
[i
] == 0x00) && (regs
[i
+1] == 0x00)); i
+= 2) {
300 if (write_reg_fp(client
, regs
[i
], regs
[i
+1]) < 0) {
301 printk(KERN_INFO
"s2250: failed fp\n");
309 static int s2250_command(struct i2c_client
*client
,
310 unsigned int cmd
, void *arg
)
312 struct s2250
*dec
= i2c_get_clientdata(client
);
320 vidsys
= (dec
->std
== V4L2_STD_NTSC
) ? 0x01 : 0x00;
323 write_reg_fp(client
, 0x20, 0x020 | vidsys
);
324 write_reg_fp(client
, 0x21, 0x662);
325 write_reg_fp(client
, 0x140, 0x060);
328 write_reg_fp(client
, 0x20, 0x040 | vidsys
);
329 write_reg_fp(client
, 0x21, 0x666);
330 write_reg_fp(client
, 0x140, 0x060);
337 v4l2_std_id
*std
= arg
;
340 vidsource
= (dec
->input
== 1) ? 0x040 : 0x020;
344 write_regs_fp(client
, vid_regs_fp
);
345 write_reg_fp(client
, 0x20, vidsource
| 1);
348 write_regs_fp(client
, vid_regs_fp
);
349 write_regs_fp(client
, vid_regs_fp_pal
);
350 write_reg_fp(client
, 0x20, vidsource
);
357 case VIDIOC_QUERYCTRL
:
359 struct v4l2_queryctrl
*ctrl
= arg
;
360 static const u32 user_ctrls
[] = {
367 static const u32
*ctrl_classes
[] = {
372 ctrl
->id
= v4l2_ctrl_next(ctrl_classes
, ctrl
->id
);
374 case V4L2_CID_BRIGHTNESS
:
375 v4l2_ctrl_query_fill(ctrl
, 0, 100, 1, 50);
377 case V4L2_CID_CONTRAST
:
378 v4l2_ctrl_query_fill(ctrl
, 0, 100, 1, 50);
380 case V4L2_CID_SATURATION
:
381 v4l2_ctrl_query_fill(ctrl
, 0, 100, 1, 50);
384 v4l2_ctrl_query_fill(ctrl
, -50, 50, 1, 0);
387 ctrl
->name
[0] = '\0';
394 struct v4l2_control
*ctrl
= arg
;
399 case V4L2_CID_BRIGHTNESS
:
400 if (ctrl
->value
> 100)
401 dec
->brightness
= 100;
402 else if (ctrl
->value
< 0)
405 dec
->brightness
= ctrl
->value
;
406 value1
= (dec
->brightness
- 50) * 255 / 100;
407 read_reg_fp(client
, VPX322_ADDR_BRIGHTNESS0
, &oldvalue
);
408 write_reg_fp(client
, VPX322_ADDR_BRIGHTNESS0
,
409 value1
| (oldvalue
& ~0xff));
410 read_reg_fp(client
, VPX322_ADDR_BRIGHTNESS1
, &oldvalue
);
411 write_reg_fp(client
, VPX322_ADDR_BRIGHTNESS1
,
412 value1
| (oldvalue
& ~0xff));
413 write_reg_fp(client
, 0x140, 0x60);
415 case V4L2_CID_CONTRAST
:
416 if (ctrl
->value
> 100)
418 else if (ctrl
->value
< 0)
421 dec
->contrast
= ctrl
->value
;
422 value1
= dec
->contrast
* 0x40 / 100;
424 value1
= 0x3f; /* max */
425 read_reg_fp(client
, VPX322_ADDR_CONTRAST0
, &oldvalue
);
426 write_reg_fp(client
, VPX322_ADDR_CONTRAST0
,
427 value1
| (oldvalue
& ~0x3f));
428 read_reg_fp(client
, VPX322_ADDR_CONTRAST1
, &oldvalue
);
429 write_reg_fp(client
, VPX322_ADDR_CONTRAST1
,
430 value1
| (oldvalue
& ~0x3f));
431 write_reg_fp(client
, 0x140, 0x60);
433 case V4L2_CID_SATURATION
:
434 if (ctrl
->value
> 127)
435 dec
->saturation
= 127;
436 else if (ctrl
->value
< 0)
439 dec
->saturation
= ctrl
->value
;
441 value1
= dec
->saturation
* 4140 / 100;
444 write_reg_fp(client
, VPX322_ADDR_SAT
, value1
);
447 if (ctrl
->value
> 50)
449 else if (ctrl
->value
< -50)
452 dec
->hue
= ctrl
->value
;
453 /* clamp the hue range */
454 value1
= dec
->hue
* 280 / 50;
455 write_reg_fp(client
, VPX322_ADDR_HUE
, value1
);
462 struct v4l2_control
*ctrl
= arg
;
465 case V4L2_CID_BRIGHTNESS
:
466 ctrl
->value
= dec
->brightness
;
468 case V4L2_CID_CONTRAST
:
469 ctrl
->value
= dec
->contrast
;
471 case V4L2_CID_SATURATION
:
472 ctrl
->value
= dec
->saturation
;
475 ctrl
->value
= dec
->hue
;
482 struct v4l2_format
*fmt
= arg
;
483 if (fmt
->fmt
.pix
.height
< 640) {
484 write_reg_fp(client
, 0x12b, dec
->reg12b_val
| 0x400);
485 write_reg_fp(client
, 0x140, 0x060);
487 write_reg_fp(client
, 0x12b, dec
->reg12b_val
& ~0x400);
488 write_reg_fp(client
, 0x140, 0x060);
494 struct v4l2_audio
*audio
= arg
;
496 memset(audio
, 0, sizeof(*audio
));
497 audio
->index
= dec
->audio_input
;
500 case VIDIOC_ENUMAUDIO
:
502 struct v4l2_audio
*audio
= arg
;
504 switch (audio
->index
) {
506 strcpy(audio
->name
, "Line In");
509 strcpy(audio
->name
, "Mic");
512 strcpy(audio
->name
, "Mic Boost");
515 audio
->name
[0] = '\0';
518 audio
->capability
= V4L2_AUDCAP_STEREO
;
524 struct v4l2_audio
*audio
= arg
;
526 switch (audio
->index
) {
528 write_reg(dec
->audio
, 0x08, 0x02); /* Line In */
531 write_reg(dec
->audio
, 0x08, 0x04); /* Mic */
534 write_reg(dec
->audio
, 0x08, 0x05); /* Mic Boost */
539 dec
->audio_input
= audio
->index
;
544 printk(KERN_INFO
"s2250: unknown command 0x%x\n", cmd
);
550 static int s2250_probe(struct i2c_client
*client
,
551 const struct i2c_device_id
*id
)
553 struct i2c_client
*audio
;
554 struct i2c_adapter
*adapter
= client
->adapter
;
557 struct go7007
*go
= i2c_get_adapdata(adapter
);
558 struct go7007_usb
*usb
= go
->hpi_context
;
560 audio
= i2c_new_dummy(adapter
, TLV320_ADDRESS
>> 1);
564 dec
= kmalloc(sizeof(struct s2250
), GFP_KERNEL
);
566 i2c_unregister_device(audio
);
570 dec
->std
= V4L2_STD_NTSC
;
571 dec
->brightness
= 50;
573 dec
->saturation
= 50;
576 i2c_set_clientdata(client
, dec
);
579 "s2250: initializing video decoder on %s\n",
582 /* initialize the audio */
583 if (write_regs(audio
, aud_regs
) < 0) {
585 "s2250: error initializing audio\n");
586 i2c_unregister_device(audio
);
591 if (write_regs(client
, vid_regs
) < 0) {
593 "s2250: error initializing decoder\n");
594 i2c_unregister_device(audio
);
598 if (write_regs_fp(client
, vid_regs_fp
) < 0) {
600 "s2250: error initializing decoder\n");
601 i2c_unregister_device(audio
);
605 /* set default channel */
607 write_reg_fp(client
, 0x20, 0x020 | 1);
608 write_reg_fp(client
, 0x21, 0x662);
609 write_reg_fp(client
, 0x140, 0x060);
611 /* set default audio input */
612 dec
->audio_input
= 0;
613 write_reg(client
, 0x08, 0x02); /* Line In */
615 if (mutex_lock_interruptible(&usb
->i2c_lock
) == 0) {
616 data
= kzalloc(16, GFP_KERNEL
);
619 rc
= go7007_usb_vendor_request(go
, 0x41, 0, 0,
627 go7007_usb_vendor_request(go
, 0x40, 0,
634 mutex_unlock(&usb
->i2c_lock
);
637 printk("s2250: initialized successfully\n");
641 static int s2250_remove(struct i2c_client
*client
)
643 struct s2250
*dec
= i2c_get_clientdata(client
);
645 i2c_set_clientdata(client
, NULL
);
646 i2c_unregister_device(dec
->audio
);
651 static struct i2c_device_id s2250_id
[] = {
652 { "s2250_board", 0 },
656 static struct i2c_driver s2250_driver
= {
658 .name
= "Sensoray 2250 board driver",
660 .probe
= s2250_probe
,
661 .remove
= s2250_remove
,
662 .command
= s2250_command
,
663 .id_table
= s2250_id
,
666 static int __init
s2250_init(void)
670 r
= s2250loader_init();
674 r
= i2c_add_driver(&s2250_driver
);
676 s2250loader_cleanup();
681 static void __exit
s2250_cleanup(void)
683 i2c_del_driver(&s2250_driver
);
685 s2250loader_cleanup();
688 module_init(s2250_init
);
689 module_exit(s2250_cleanup
);
692 MODULE_DESCRIPTION("Board driver for Sensoryray 2250");
693 MODULE_LICENSE("GPL v2");