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.
14 #include <linux/module.h>
15 #include <linux/usb.h>
16 #include <linux/i2c.h>
17 #include <linux/videodev2.h>
18 #include <linux/slab.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-subdev.h>
22 #include "go7007-priv.h"
24 MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
25 MODULE_LICENSE("GPL v2");
28 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
29 * Due to the unusual way these are accessed on this device we do not
30 * reuse the i2c drivers, but instead they are implemented in this
31 * driver. It would be nice to improve on this, though.
34 #define TLV320_ADDRESS 0x34
35 #define VPX322_ADDR_ANALOGCONTROL1 0x02
36 #define VPX322_ADDR_BRIGHTNESS0 0x0127
37 #define VPX322_ADDR_BRIGHTNESS1 0x0131
38 #define VPX322_ADDR_CONTRAST0 0x0128
39 #define VPX322_ADDR_CONTRAST1 0x0132
40 #define VPX322_ADDR_HUE 0x00dc
41 #define VPX322_ADDR_SAT 0x0030
43 struct go7007_usb_board {
45 struct go7007_board_info main_info;
49 struct go7007_usb_board *board;
50 struct mutex i2c_lock;
51 struct usb_device *usbdev;
52 struct urb *video_urbs[8];
53 struct urb *audio_urbs[8];
57 static unsigned char aud_regs[] = {
75 static unsigned char vid_regs[] = {
82 static u16 vid_regs_fp[] = {
107 /* PAL specific values */
108 static u16 vid_regs_fp_pal[] = {
120 struct v4l2_subdev sd;
121 struct v4l2_ctrl_handler hdl;
130 struct i2c_client *audio;
133 static inline struct s2250 *to_state(struct v4l2_subdev *sd)
135 return container_of(sd, struct s2250, sd);
138 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
139 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
140 u16 value, u16 index, void *transfer_buffer, int length, int in)
142 struct go7007_usb *usb = go->hpi_context;
146 return usb_control_msg(usb->usbdev,
147 usb_rcvctrlpipe(usb->usbdev, 0), request,
148 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
149 value, index, transfer_buffer, length, timeout);
151 return usb_control_msg(usb->usbdev,
152 usb_sndctrlpipe(usb->usbdev, 0), request,
153 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
154 value, index, transfer_buffer, length, timeout);
157 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
159 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
161 struct go7007 *go = i2c_get_adapdata(client->adapter);
162 struct go7007_usb *usb;
164 int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
170 if (go->status == STATUS_SHUTDOWN)
173 buf = kzalloc(16, GFP_KERNEL);
177 usb = go->hpi_context;
178 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
179 dev_info(&client->dev, "i2c lock failed\n");
183 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
188 mutex_unlock(&usb->i2c_lock);
193 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
195 struct go7007 *go = i2c_get_adapdata(client->adapter);
196 struct go7007_usb *usb;
199 struct s2250 *dec = i2c_get_clientdata(client);
204 if (go->status == STATUS_SHUTDOWN)
207 buf = kzalloc(16, GFP_KERNEL);
214 memset(buf, 0xcd, 6);
216 usb = go->hpi_context;
217 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
218 dev_info(&client->dev, "i2c lock failed\n");
222 rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
223 mutex_unlock(&usb->i2c_lock);
230 unsigned int subaddr, val_read;
232 subaddr = (buf[4] << 8) + buf[5];
233 val_read = (buf[2] << 8) + buf[3];
235 if (val_read != val) {
236 dev_info(&client->dev, "invalid fp write %x %x\n",
240 if (subaddr != addr) {
241 dev_info(&client->dev, "invalid fp write addr %x %x\n",
250 /* save last 12b value */
252 dec->reg12b_val = val;
257 static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
259 struct go7007 *go = i2c_get_adapdata(client->adapter);
260 struct go7007_usb *usb;
267 if (go->status == STATUS_SHUTDOWN)
270 buf = kzalloc(16, GFP_KERNEL);
277 memset(buf, 0xcd, 6);
278 usb = go->hpi_context;
279 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
280 dev_info(&client->dev, "i2c lock failed\n");
284 rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
285 mutex_unlock(&usb->i2c_lock);
291 *val = (buf[0] << 8) | buf[1];
298 static int write_regs(struct i2c_client *client, u8 *regs)
302 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
303 if (write_reg(client, regs[i], regs[i+1]) < 0) {
304 dev_info(&client->dev, "failed\n");
311 static int write_regs_fp(struct i2c_client *client, u16 *regs)
315 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
316 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
317 dev_info(&client->dev, "failed fp\n");
325 /* ------------------------------------------------------------------------- */
327 static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
330 struct s2250 *state = to_state(sd);
331 struct i2c_client *client = v4l2_get_subdevdata(sd);
334 vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
337 write_reg_fp(client, 0x20, 0x020 | vidsys);
338 write_reg_fp(client, 0x21, 0x662);
339 write_reg_fp(client, 0x140, 0x060);
340 } else if (input == 1) {
342 write_reg_fp(client, 0x20, 0x040 | vidsys);
343 write_reg_fp(client, 0x21, 0x666);
344 write_reg_fp(client, 0x140, 0x060);
348 state->input = input;
352 static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
354 struct s2250 *state = to_state(sd);
355 struct i2c_client *client = v4l2_get_subdevdata(sd);
358 vidsource = (state->input == 1) ? 0x040 : 0x020;
359 if (norm & V4L2_STD_625_50) {
360 write_regs_fp(client, vid_regs_fp);
361 write_regs_fp(client, vid_regs_fp_pal);
362 write_reg_fp(client, 0x20, vidsource);
364 write_regs_fp(client, vid_regs_fp);
365 write_reg_fp(client, 0x20, vidsource | 1);
371 static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
373 struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
374 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
378 case V4L2_CID_BRIGHTNESS:
379 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
380 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
381 ctrl->val | (oldvalue & ~0xff));
382 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
383 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
384 ctrl->val | (oldvalue & ~0xff));
385 write_reg_fp(client, 0x140, 0x60);
387 case V4L2_CID_CONTRAST:
388 read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
389 write_reg_fp(client, VPX322_ADDR_CONTRAST0,
390 ctrl->val | (oldvalue & ~0x3f));
391 read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
392 write_reg_fp(client, VPX322_ADDR_CONTRAST1,
393 ctrl->val | (oldvalue & ~0x3f));
394 write_reg_fp(client, 0x140, 0x60);
396 case V4L2_CID_SATURATION:
397 write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
400 write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
408 static int s2250_set_fmt(struct v4l2_subdev *sd,
409 struct v4l2_subdev_pad_config *cfg,
410 struct v4l2_subdev_format *format)
412 struct v4l2_mbus_framefmt *fmt = &format->format;
413 struct s2250 *state = to_state(sd);
414 struct i2c_client *client = v4l2_get_subdevdata(sd);
419 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
422 if (fmt->height < 640) {
423 write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
424 write_reg_fp(client, 0x140, 0x060);
426 write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
427 write_reg_fp(client, 0x140, 0x060);
432 static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
435 struct s2250 *state = to_state(sd);
439 write_reg(state->audio, 0x08, 0x02); /* Line In */
442 write_reg(state->audio, 0x08, 0x04); /* Mic */
445 write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
450 state->audio_input = input;
455 static int s2250_log_status(struct v4l2_subdev *sd)
457 struct s2250 *state = to_state(sd);
459 v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
460 state->std == V4L2_STD_PAL ? "PAL" :
461 state->std == V4L2_STD_SECAM ? "SECAM" :
463 v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
464 state->input == 1 ? "S-video" :
466 v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
467 state->audio_input == 1 ? "Mic" :
468 state->audio_input == 2 ? "Mic Boost" :
470 return v4l2_ctrl_subdev_log_status(sd);
473 /* --------------------------------------------------------------------------*/
475 static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
476 .s_ctrl = s2250_s_ctrl,
479 static const struct v4l2_subdev_core_ops s2250_core_ops = {
480 .log_status = s2250_log_status,
483 static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
484 .s_routing = s2250_s_audio_routing,
487 static const struct v4l2_subdev_video_ops s2250_video_ops = {
488 .s_std = s2250_s_std,
489 .s_routing = s2250_s_video_routing,
492 static const struct v4l2_subdev_pad_ops s2250_pad_ops = {
493 .set_fmt = s2250_set_fmt,
496 static const struct v4l2_subdev_ops s2250_ops = {
497 .core = &s2250_core_ops,
498 .audio = &s2250_audio_ops,
499 .video = &s2250_video_ops,
500 .pad = &s2250_pad_ops,
503 /* --------------------------------------------------------------------------*/
505 static int s2250_probe(struct i2c_client *client,
506 const struct i2c_device_id *id)
508 struct i2c_client *audio;
509 struct i2c_adapter *adapter = client->adapter;
511 struct v4l2_subdev *sd;
513 struct go7007 *go = i2c_get_adapdata(adapter);
514 struct go7007_usb *usb = go->hpi_context;
517 audio = i2c_new_dummy(adapter, TLV320_ADDRESS >> 1);
521 state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
523 i2c_unregister_device(audio);
528 v4l2_i2c_subdev_init(sd, client, &s2250_ops);
530 v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
531 "Sensoray 2250/2251", client->addr, client->adapter->name);
533 v4l2_ctrl_handler_init(&state->hdl, 4);
534 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
535 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
536 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
537 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
538 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
539 V4L2_CID_SATURATION, 0, 4094, 1, 2070);
540 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
541 V4L2_CID_HUE, -512, 511, 1, 0);
542 sd->ctrl_handler = &state->hdl;
543 if (state->hdl.error) {
544 err = state->hdl.error;
548 state->std = V4L2_STD_NTSC;
549 state->brightness = 50;
550 state->contrast = 50;
551 state->saturation = 50;
553 state->audio = audio;
555 /* initialize the audio */
556 if (write_regs(audio, aud_regs) < 0) {
557 dev_err(&client->dev, "error initializing audio\n");
561 if (write_regs(client, vid_regs) < 0) {
562 dev_err(&client->dev, "error initializing decoder\n");
565 if (write_regs_fp(client, vid_regs_fp) < 0) {
566 dev_err(&client->dev, "error initializing decoder\n");
569 /* set default channel */
571 write_reg_fp(client, 0x20, 0x020 | 1);
572 write_reg_fp(client, 0x21, 0x662);
573 write_reg_fp(client, 0x140, 0x060);
575 /* set default audio input */
576 state->audio_input = 0;
577 write_reg(client, 0x08, 0x02); /* Line In */
579 if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
580 data = kzalloc(16, GFP_KERNEL);
582 int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
592 go7007_usb_vendor_request(go, 0x40, 0,
599 mutex_unlock(&usb->i2c_lock);
602 v4l2_info(sd, "initialized successfully\n");
606 i2c_unregister_device(audio);
607 v4l2_ctrl_handler_free(&state->hdl);
612 static int s2250_remove(struct i2c_client *client)
614 struct s2250 *state = to_state(i2c_get_clientdata(client));
616 v4l2_device_unregister_subdev(&state->sd);
617 v4l2_ctrl_handler_free(&state->hdl);
622 static const struct i2c_device_id s2250_id[] = {
626 MODULE_DEVICE_TABLE(i2c, s2250_id);
628 static struct i2c_driver s2250_driver = {
632 .probe = s2250_probe,
633 .remove = s2250_remove,
634 .id_table = s2250_id,
637 module_i2c_driver(s2250_driver);