1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 Sensoray Company Inc.
6 #include <linux/module.h>
9 #include <linux/videodev2.h>
10 #include <linux/slab.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-common.h>
13 #include <media/v4l2-subdev.h>
14 #include "go7007-priv.h"
16 MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
17 MODULE_LICENSE("GPL v2");
20 * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
21 * Due to the unusual way these are accessed on this device we do not
22 * reuse the i2c drivers, but instead they are implemented in this
23 * driver. It would be nice to improve on this, though.
26 #define TLV320_ADDRESS 0x34
27 #define VPX322_ADDR_ANALOGCONTROL1 0x02
28 #define VPX322_ADDR_BRIGHTNESS0 0x0127
29 #define VPX322_ADDR_BRIGHTNESS1 0x0131
30 #define VPX322_ADDR_CONTRAST0 0x0128
31 #define VPX322_ADDR_CONTRAST1 0x0132
32 #define VPX322_ADDR_HUE 0x00dc
33 #define VPX322_ADDR_SAT 0x0030
35 struct go7007_usb_board {
37 struct go7007_board_info main_info;
41 struct go7007_usb_board *board;
42 struct mutex i2c_lock;
43 struct usb_device *usbdev;
44 struct urb *video_urbs[8];
45 struct urb *audio_urbs[8];
49 static unsigned char aud_regs[] = {
67 static unsigned char vid_regs[] = {
74 static u16 vid_regs_fp[] = {
99 /* PAL specific values */
100 static u16 vid_regs_fp_pal[] = {
112 struct v4l2_subdev sd;
113 struct v4l2_ctrl_handler hdl;
122 struct i2c_client *audio;
125 static inline struct s2250 *to_state(struct v4l2_subdev *sd)
127 return container_of(sd, struct s2250, sd);
130 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
131 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
132 u16 value, u16 index, void *transfer_buffer, int length, int in)
134 struct go7007_usb *usb = go->hpi_context;
138 return usb_control_msg(usb->usbdev,
139 usb_rcvctrlpipe(usb->usbdev, 0), request,
140 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
141 value, index, transfer_buffer, length, timeout);
143 return usb_control_msg(usb->usbdev,
144 usb_sndctrlpipe(usb->usbdev, 0), request,
145 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
146 value, index, transfer_buffer, length, timeout);
149 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
151 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
153 struct go7007 *go = i2c_get_adapdata(client->adapter);
154 struct go7007_usb *usb;
156 int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
162 if (go->status == STATUS_SHUTDOWN)
165 buf = kzalloc(16, GFP_KERNEL);
169 usb = go->hpi_context;
170 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
171 dev_info(&client->dev, "i2c lock failed\n");
175 rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
180 mutex_unlock(&usb->i2c_lock);
185 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
187 struct go7007 *go = i2c_get_adapdata(client->adapter);
188 struct go7007_usb *usb;
191 struct s2250 *dec = i2c_get_clientdata(client);
196 if (go->status == STATUS_SHUTDOWN)
199 buf = kzalloc(16, GFP_KERNEL);
206 memset(buf, 0xcd, 6);
208 usb = go->hpi_context;
209 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
210 dev_info(&client->dev, "i2c lock failed\n");
214 rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
215 mutex_unlock(&usb->i2c_lock);
222 unsigned int subaddr, val_read;
224 subaddr = (buf[4] << 8) + buf[5];
225 val_read = (buf[2] << 8) + buf[3];
227 if (val_read != val) {
228 dev_info(&client->dev, "invalid fp write %x %x\n",
232 if (subaddr != addr) {
233 dev_info(&client->dev, "invalid fp write addr %x %x\n",
242 /* save last 12b value */
244 dec->reg12b_val = val;
249 static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
251 struct go7007 *go = i2c_get_adapdata(client->adapter);
252 struct go7007_usb *usb;
259 if (go->status == STATUS_SHUTDOWN)
262 buf = kzalloc(16, GFP_KERNEL);
269 memset(buf, 0xcd, 6);
270 usb = go->hpi_context;
271 if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
272 dev_info(&client->dev, "i2c lock failed\n");
276 rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
277 mutex_unlock(&usb->i2c_lock);
283 *val = (buf[0] << 8) | buf[1];
290 static int write_regs(struct i2c_client *client, u8 *regs)
294 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
295 if (write_reg(client, regs[i], regs[i+1]) < 0) {
296 dev_info(&client->dev, "failed\n");
303 static int write_regs_fp(struct i2c_client *client, u16 *regs)
307 for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
308 if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
309 dev_info(&client->dev, "failed fp\n");
317 /* ------------------------------------------------------------------------- */
319 static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
322 struct s2250 *state = to_state(sd);
323 struct i2c_client *client = v4l2_get_subdevdata(sd);
326 vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
329 write_reg_fp(client, 0x20, 0x020 | vidsys);
330 write_reg_fp(client, 0x21, 0x662);
331 write_reg_fp(client, 0x140, 0x060);
332 } else if (input == 1) {
334 write_reg_fp(client, 0x20, 0x040 | vidsys);
335 write_reg_fp(client, 0x21, 0x666);
336 write_reg_fp(client, 0x140, 0x060);
340 state->input = input;
344 static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
346 struct s2250 *state = to_state(sd);
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
350 vidsource = (state->input == 1) ? 0x040 : 0x020;
351 if (norm & V4L2_STD_625_50) {
352 write_regs_fp(client, vid_regs_fp);
353 write_regs_fp(client, vid_regs_fp_pal);
354 write_reg_fp(client, 0x20, vidsource);
356 write_regs_fp(client, vid_regs_fp);
357 write_reg_fp(client, 0x20, vidsource | 1);
363 static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
365 struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
366 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
370 case V4L2_CID_BRIGHTNESS:
371 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
372 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
373 ctrl->val | (oldvalue & ~0xff));
374 read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
375 write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
376 ctrl->val | (oldvalue & ~0xff));
377 write_reg_fp(client, 0x140, 0x60);
379 case V4L2_CID_CONTRAST:
380 read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
381 write_reg_fp(client, VPX322_ADDR_CONTRAST0,
382 ctrl->val | (oldvalue & ~0x3f));
383 read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
384 write_reg_fp(client, VPX322_ADDR_CONTRAST1,
385 ctrl->val | (oldvalue & ~0x3f));
386 write_reg_fp(client, 0x140, 0x60);
388 case V4L2_CID_SATURATION:
389 write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
392 write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
400 static int s2250_set_fmt(struct v4l2_subdev *sd,
401 struct v4l2_subdev_pad_config *cfg,
402 struct v4l2_subdev_format *format)
404 struct v4l2_mbus_framefmt *fmt = &format->format;
405 struct s2250 *state = to_state(sd);
406 struct i2c_client *client = v4l2_get_subdevdata(sd);
411 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
414 if (fmt->height < 640) {
415 write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
416 write_reg_fp(client, 0x140, 0x060);
418 write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
419 write_reg_fp(client, 0x140, 0x060);
424 static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
427 struct s2250 *state = to_state(sd);
431 write_reg(state->audio, 0x08, 0x02); /* Line In */
434 write_reg(state->audio, 0x08, 0x04); /* Mic */
437 write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
442 state->audio_input = input;
447 static int s2250_log_status(struct v4l2_subdev *sd)
449 struct s2250 *state = to_state(sd);
451 v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
452 state->std == V4L2_STD_PAL ? "PAL" :
453 state->std == V4L2_STD_SECAM ? "SECAM" :
455 v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
456 state->input == 1 ? "S-video" :
458 v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
459 state->audio_input == 1 ? "Mic" :
460 state->audio_input == 2 ? "Mic Boost" :
462 return v4l2_ctrl_subdev_log_status(sd);
465 /* --------------------------------------------------------------------------*/
467 static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
468 .s_ctrl = s2250_s_ctrl,
471 static const struct v4l2_subdev_core_ops s2250_core_ops = {
472 .log_status = s2250_log_status,
475 static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
476 .s_routing = s2250_s_audio_routing,
479 static const struct v4l2_subdev_video_ops s2250_video_ops = {
480 .s_std = s2250_s_std,
481 .s_routing = s2250_s_video_routing,
484 static const struct v4l2_subdev_pad_ops s2250_pad_ops = {
485 .set_fmt = s2250_set_fmt,
488 static const struct v4l2_subdev_ops s2250_ops = {
489 .core = &s2250_core_ops,
490 .audio = &s2250_audio_ops,
491 .video = &s2250_video_ops,
492 .pad = &s2250_pad_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;
509 audio = i2c_new_dummy_device(adapter, TLV320_ADDRESS >> 1);
511 return PTR_ERR(audio);
513 state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
515 i2c_unregister_device(audio);
520 v4l2_i2c_subdev_init(sd, client, &s2250_ops);
522 v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
523 "Sensoray 2250/2251", client->addr, client->adapter->name);
525 v4l2_ctrl_handler_init(&state->hdl, 4);
526 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
527 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
528 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
529 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
530 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
531 V4L2_CID_SATURATION, 0, 4094, 1, 2070);
532 v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
533 V4L2_CID_HUE, -512, 511, 1, 0);
534 sd->ctrl_handler = &state->hdl;
535 if (state->hdl.error) {
536 err = state->hdl.error;
540 state->std = V4L2_STD_NTSC;
541 state->brightness = 50;
542 state->contrast = 50;
543 state->saturation = 50;
545 state->audio = audio;
547 /* initialize the audio */
548 if (write_regs(audio, aud_regs) < 0) {
549 dev_err(&client->dev, "error initializing audio\n");
553 if (write_regs(client, vid_regs) < 0) {
554 dev_err(&client->dev, "error initializing decoder\n");
557 if (write_regs_fp(client, vid_regs_fp) < 0) {
558 dev_err(&client->dev, "error initializing decoder\n");
561 /* set default channel */
563 write_reg_fp(client, 0x20, 0x020 | 1);
564 write_reg_fp(client, 0x21, 0x662);
565 write_reg_fp(client, 0x140, 0x060);
567 /* set default audio input */
568 state->audio_input = 0;
569 write_reg(client, 0x08, 0x02); /* Line In */
571 if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
572 data = kzalloc(16, GFP_KERNEL);
574 int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
584 go7007_usb_vendor_request(go, 0x40, 0,
591 mutex_unlock(&usb->i2c_lock);
594 v4l2_info(sd, "initialized successfully\n");
598 i2c_unregister_device(audio);
599 v4l2_ctrl_handler_free(&state->hdl);
604 static int s2250_remove(struct i2c_client *client)
606 struct s2250 *state = to_state(i2c_get_clientdata(client));
608 i2c_unregister_device(state->audio);
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 = {
625 .probe = s2250_probe,
626 .remove = s2250_remove,
627 .id_table = s2250_id,
630 module_i2c_driver(s2250_driver);