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/usb.h>
20 #include <linux/i2c.h>
21 #include <linux/videodev2.h>
22 #include <linux/slab.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-subdev.h>
26 #include "go7007-priv.h"
28 MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
29 MODULE_LICENSE("GPL v2");
32 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
33 * Due to the unusual way these are accessed on this device we do not
34 * reuse the i2c drivers, but instead they are implemented in this
35 * driver. It would be nice to improve on this, though.
38 #define TLV320_ADDRESS 0x34
39 #define VPX322_ADDR_ANALOGCONTROL1 0x02
40 #define VPX322_ADDR_BRIGHTNESS0 0x0127
41 #define VPX322_ADDR_BRIGHTNESS1 0x0131
42 #define VPX322_ADDR_CONTRAST0 0x0128
43 #define VPX322_ADDR_CONTRAST1 0x0132
44 #define VPX322_ADDR_HUE 0x00dc
45 #define VPX322_ADDR_SAT 0x0030
47 struct go7007_usb_board
{
49 struct go7007_board_info main_info
;
53 struct go7007_usb_board
*board
;
54 struct mutex i2c_lock
;
55 struct usb_device
*usbdev
;
56 struct urb
*video_urbs
[8];
57 struct urb
*audio_urbs
[8];
61 static unsigned char aud_regs
[] = {
79 static unsigned char vid_regs
[] = {
86 static u16 vid_regs_fp
[] = {
111 /* PAL specific values */
112 static u16 vid_regs_fp_pal
[] = {
124 struct v4l2_subdev sd
;
125 struct v4l2_ctrl_handler hdl
;
134 struct i2c_client
*audio
;
137 static inline struct s2250
*to_state(struct v4l2_subdev
*sd
)
139 return container_of(sd
, struct s2250
, sd
);
142 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
143 static int go7007_usb_vendor_request(struct go7007
*go
, u16 request
,
144 u16 value
, u16 index
, void *transfer_buffer
, int length
, int in
)
146 struct go7007_usb
*usb
= go
->hpi_context
;
150 return usb_control_msg(usb
->usbdev
,
151 usb_rcvctrlpipe(usb
->usbdev
, 0), request
,
152 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
153 value
, index
, transfer_buffer
, length
, timeout
);
155 return usb_control_msg(usb
->usbdev
,
156 usb_sndctrlpipe(usb
->usbdev
, 0), request
,
157 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
158 value
, index
, transfer_buffer
, length
, timeout
);
161 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
163 static int write_reg(struct i2c_client
*client
, u8 reg
, u8 value
)
165 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
166 struct go7007_usb
*usb
;
168 int dev_addr
= client
->addr
<< 1; /* firmware wants 8-bit address */
174 if (go
->status
== STATUS_SHUTDOWN
)
177 buf
= kzalloc(16, GFP_KERNEL
);
181 usb
= go
->hpi_context
;
182 if (mutex_lock_interruptible(&usb
->i2c_lock
) != 0) {
183 dev_info(&client
->dev
, "i2c lock failed\n");
187 rc
= go7007_usb_vendor_request(go
, 0x55, dev_addr
,
192 mutex_unlock(&usb
->i2c_lock
);
197 static int write_reg_fp(struct i2c_client
*client
, u16 addr
, u16 val
)
199 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
200 struct go7007_usb
*usb
;
203 struct s2250
*dec
= i2c_get_clientdata(client
);
208 if (go
->status
== STATUS_SHUTDOWN
)
211 buf
= kzalloc(16, GFP_KERNEL
);
218 memset(buf
, 0xcd, 6);
220 usb
= go
->hpi_context
;
221 if (mutex_lock_interruptible(&usb
->i2c_lock
) != 0) {
222 dev_info(&client
->dev
, "i2c lock failed\n");
226 rc
= go7007_usb_vendor_request(go
, 0x57, addr
, val
, buf
, 16, 1);
227 mutex_unlock(&usb
->i2c_lock
);
234 unsigned int subaddr
, val_read
;
236 subaddr
= (buf
[4] << 8) + buf
[5];
237 val_read
= (buf
[2] << 8) + buf
[3];
239 if (val_read
!= val
) {
240 dev_info(&client
->dev
, "invalid fp write %x %x\n",
244 if (subaddr
!= addr
) {
245 dev_info(&client
->dev
, "invalid fp write addr %x %x\n",
254 /* save last 12b value */
256 dec
->reg12b_val
= val
;
261 static int read_reg_fp(struct i2c_client
*client
, u16 addr
, u16
*val
)
263 struct go7007
*go
= i2c_get_adapdata(client
->adapter
);
264 struct go7007_usb
*usb
;
271 if (go
->status
== STATUS_SHUTDOWN
)
274 buf
= kzalloc(16, GFP_KERNEL
);
281 memset(buf
, 0xcd, 6);
282 usb
= go
->hpi_context
;
283 if (mutex_lock_interruptible(&usb
->i2c_lock
) != 0) {
284 dev_info(&client
->dev
, "i2c lock failed\n");
288 rc
= go7007_usb_vendor_request(go
, 0x58, addr
, 0, buf
, 16, 1);
289 mutex_unlock(&usb
->i2c_lock
);
295 *val
= (buf
[0] << 8) | buf
[1];
302 static int write_regs(struct i2c_client
*client
, u8
*regs
)
306 for (i
= 0; !((regs
[i
] == 0x00) && (regs
[i
+1] == 0x00)); i
+= 2) {
307 if (write_reg(client
, regs
[i
], regs
[i
+1]) < 0) {
308 dev_info(&client
->dev
, "failed\n");
315 static int write_regs_fp(struct i2c_client
*client
, u16
*regs
)
319 for (i
= 0; !((regs
[i
] == 0x00) && (regs
[i
+1] == 0x00)); i
+= 2) {
320 if (write_reg_fp(client
, regs
[i
], regs
[i
+1]) < 0) {
321 dev_info(&client
->dev
, "failed fp\n");
329 /* ------------------------------------------------------------------------- */
331 static int s2250_s_video_routing(struct v4l2_subdev
*sd
, u32 input
, u32 output
,
334 struct s2250
*state
= to_state(sd
);
335 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
338 vidsys
= (state
->std
== V4L2_STD_NTSC
) ? 0x01 : 0x00;
341 write_reg_fp(client
, 0x20, 0x020 | vidsys
);
342 write_reg_fp(client
, 0x21, 0x662);
343 write_reg_fp(client
, 0x140, 0x060);
344 } else if (input
== 1) {
346 write_reg_fp(client
, 0x20, 0x040 | vidsys
);
347 write_reg_fp(client
, 0x21, 0x666);
348 write_reg_fp(client
, 0x140, 0x060);
352 state
->input
= input
;
356 static int s2250_s_std(struct v4l2_subdev
*sd
, v4l2_std_id norm
)
358 struct s2250
*state
= to_state(sd
);
359 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
362 vidsource
= (state
->input
== 1) ? 0x040 : 0x020;
363 if (norm
& V4L2_STD_625_50
) {
364 write_regs_fp(client
, vid_regs_fp
);
365 write_regs_fp(client
, vid_regs_fp_pal
);
366 write_reg_fp(client
, 0x20, vidsource
);
368 write_regs_fp(client
, vid_regs_fp
);
369 write_reg_fp(client
, 0x20, vidsource
| 1);
375 static int s2250_s_ctrl(struct v4l2_ctrl
*ctrl
)
377 struct s2250
*state
= container_of(ctrl
->handler
, struct s2250
, hdl
);
378 struct i2c_client
*client
= v4l2_get_subdevdata(&state
->sd
);
382 case V4L2_CID_BRIGHTNESS
:
383 read_reg_fp(client
, VPX322_ADDR_BRIGHTNESS0
, &oldvalue
);
384 write_reg_fp(client
, VPX322_ADDR_BRIGHTNESS0
,
385 ctrl
->val
| (oldvalue
& ~0xff));
386 read_reg_fp(client
, VPX322_ADDR_BRIGHTNESS1
, &oldvalue
);
387 write_reg_fp(client
, VPX322_ADDR_BRIGHTNESS1
,
388 ctrl
->val
| (oldvalue
& ~0xff));
389 write_reg_fp(client
, 0x140, 0x60);
391 case V4L2_CID_CONTRAST
:
392 read_reg_fp(client
, VPX322_ADDR_CONTRAST0
, &oldvalue
);
393 write_reg_fp(client
, VPX322_ADDR_CONTRAST0
,
394 ctrl
->val
| (oldvalue
& ~0x3f));
395 read_reg_fp(client
, VPX322_ADDR_CONTRAST1
, &oldvalue
);
396 write_reg_fp(client
, VPX322_ADDR_CONTRAST1
,
397 ctrl
->val
| (oldvalue
& ~0x3f));
398 write_reg_fp(client
, 0x140, 0x60);
400 case V4L2_CID_SATURATION
:
401 write_reg_fp(client
, VPX322_ADDR_SAT
, ctrl
->val
);
404 write_reg_fp(client
, VPX322_ADDR_HUE
, ctrl
->val
);
412 static int s2250_s_mbus_fmt(struct v4l2_subdev
*sd
,
413 struct v4l2_mbus_framefmt
*fmt
)
415 struct s2250
*state
= to_state(sd
);
416 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
418 if (fmt
->height
< 640) {
419 write_reg_fp(client
, 0x12b, state
->reg12b_val
| 0x400);
420 write_reg_fp(client
, 0x140, 0x060);
422 write_reg_fp(client
, 0x12b, state
->reg12b_val
& ~0x400);
423 write_reg_fp(client
, 0x140, 0x060);
428 static int s2250_s_audio_routing(struct v4l2_subdev
*sd
, u32 input
, u32 output
,
431 struct s2250
*state
= to_state(sd
);
435 write_reg(state
->audio
, 0x08, 0x02); /* Line In */
438 write_reg(state
->audio
, 0x08, 0x04); /* Mic */
441 write_reg(state
->audio
, 0x08, 0x05); /* Mic Boost */
446 state
->audio_input
= input
;
451 static int s2250_log_status(struct v4l2_subdev
*sd
)
453 struct s2250
*state
= to_state(sd
);
455 v4l2_info(sd
, "Standard: %s\n", state
->std
== V4L2_STD_NTSC
? "NTSC" :
456 state
->std
== V4L2_STD_PAL
? "PAL" :
457 state
->std
== V4L2_STD_SECAM
? "SECAM" :
459 v4l2_info(sd
, "Input: %s\n", state
->input
== 0 ? "Composite" :
460 state
->input
== 1 ? "S-video" :
462 v4l2_info(sd
, "Audio input: %s\n", state
->audio_input
== 0 ? "Line In" :
463 state
->audio_input
== 1 ? "Mic" :
464 state
->audio_input
== 2 ? "Mic Boost" :
466 return v4l2_ctrl_subdev_log_status(sd
);
469 /* --------------------------------------------------------------------------*/
471 static const struct v4l2_ctrl_ops s2250_ctrl_ops
= {
472 .s_ctrl
= s2250_s_ctrl
,
475 static const struct v4l2_subdev_core_ops s2250_core_ops
= {
476 .log_status
= s2250_log_status
,
477 .s_std
= s2250_s_std
,
480 static const struct v4l2_subdev_audio_ops s2250_audio_ops
= {
481 .s_routing
= s2250_s_audio_routing
,
484 static const struct v4l2_subdev_video_ops s2250_video_ops
= {
485 .s_routing
= s2250_s_video_routing
,
486 .s_mbus_fmt
= s2250_s_mbus_fmt
,
489 static const struct v4l2_subdev_ops s2250_ops
= {
490 .core
= &s2250_core_ops
,
491 .audio
= &s2250_audio_ops
,
492 .video
= &s2250_video_ops
,
495 /* --------------------------------------------------------------------------*/
497 static int s2250_probe(struct i2c_client
*client
,
498 const struct i2c_device_id
*id
)
500 struct i2c_client
*audio
;
501 struct i2c_adapter
*adapter
= client
->adapter
;
503 struct v4l2_subdev
*sd
;
505 struct go7007
*go
= i2c_get_adapdata(adapter
);
506 struct go7007_usb
*usb
= go
->hpi_context
;
508 audio
= i2c_new_dummy(adapter
, TLV320_ADDRESS
>> 1);
512 state
= kzalloc(sizeof(struct s2250
), GFP_KERNEL
);
514 i2c_unregister_device(audio
);
519 v4l2_i2c_subdev_init(sd
, client
, &s2250_ops
);
521 v4l2_info(sd
, "initializing %s at address 0x%x on %s\n",
522 "Sensoray 2250/2251", client
->addr
, client
->adapter
->name
);
524 v4l2_ctrl_handler_init(&state
->hdl
, 4);
525 v4l2_ctrl_new_std(&state
->hdl
, &s2250_ctrl_ops
,
526 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
527 v4l2_ctrl_new_std(&state
->hdl
, &s2250_ctrl_ops
,
528 V4L2_CID_CONTRAST
, 0, 0x3f, 1, 0x32);
529 v4l2_ctrl_new_std(&state
->hdl
, &s2250_ctrl_ops
,
530 V4L2_CID_SATURATION
, 0, 4094, 1, 2070);
531 v4l2_ctrl_new_std(&state
->hdl
, &s2250_ctrl_ops
,
532 V4L2_CID_HUE
, -512, 511, 1, 0);
533 sd
->ctrl_handler
= &state
->hdl
;
534 if (state
->hdl
.error
) {
535 int err
= state
->hdl
.error
;
537 v4l2_ctrl_handler_free(&state
->hdl
);
542 state
->std
= V4L2_STD_NTSC
;
543 state
->brightness
= 50;
544 state
->contrast
= 50;
545 state
->saturation
= 50;
547 state
->audio
= audio
;
549 /* initialize the audio */
550 if (write_regs(audio
, aud_regs
) < 0) {
551 dev_err(&client
->dev
, "error initializing audio\n");
555 if (write_regs(client
, vid_regs
) < 0) {
556 dev_err(&client
->dev
, "error initializing decoder\n");
559 if (write_regs_fp(client
, vid_regs_fp
) < 0) {
560 dev_err(&client
->dev
, "error initializing decoder\n");
563 /* set default channel */
565 write_reg_fp(client
, 0x20, 0x020 | 1);
566 write_reg_fp(client
, 0x21, 0x662);
567 write_reg_fp(client
, 0x140, 0x060);
569 /* set default audio input */
570 state
->audio_input
= 0;
571 write_reg(client
, 0x08, 0x02); /* Line In */
573 if (mutex_lock_interruptible(&usb
->i2c_lock
) == 0) {
574 data
= kzalloc(16, GFP_KERNEL
);
577 rc
= go7007_usb_vendor_request(go
, 0x41, 0, 0,
585 go7007_usb_vendor_request(go
, 0x40, 0,
592 mutex_unlock(&usb
->i2c_lock
);
595 v4l2_info(sd
, "initialized successfully\n");
599 i2c_unregister_device(audio
);
600 v4l2_ctrl_handler_free(&state
->hdl
);
605 static int s2250_remove(struct i2c_client
*client
)
607 struct s2250
*state
= to_state(i2c_get_clientdata(client
));
609 v4l2_device_unregister_subdev(&state
->sd
);
610 v4l2_ctrl_handler_free(&state
->hdl
);
615 static const struct i2c_device_id s2250_id
[] = {
619 MODULE_DEVICE_TABLE(i2c
, s2250_id
);
621 static struct i2c_driver s2250_driver
= {
623 .owner
= THIS_MODULE
,
626 .probe
= s2250_probe
,
627 .remove
= s2250_remove
,
628 .id_table
= s2250_id
,
631 module_i2c_driver(s2250_driver
);