Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2008 Sensoray Company Inc.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/usb.h>
0008 #include <linux/i2c.h>
0009 #include <linux/videodev2.h>
0010 #include <linux/slab.h>
0011 #include <media/v4l2-device.h>
0012 #include <media/v4l2-common.h>
0013 #include <media/v4l2-subdev.h>
0014 #include "go7007-priv.h"
0015 
0016 MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
0017 MODULE_LICENSE("GPL v2");
0018 
0019 /*
0020  * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
0021  * Due to the unusual way these are accessed on this device we do not
0022  * reuse the i2c drivers, but instead they are implemented in this
0023  * driver. It would be nice to improve on this, though.
0024  */
0025 
0026 #define TLV320_ADDRESS      0x34
0027 #define VPX322_ADDR_ANALOGCONTROL1  0x02
0028 #define VPX322_ADDR_BRIGHTNESS0     0x0127
0029 #define VPX322_ADDR_BRIGHTNESS1     0x0131
0030 #define VPX322_ADDR_CONTRAST0       0x0128
0031 #define VPX322_ADDR_CONTRAST1       0x0132
0032 #define VPX322_ADDR_HUE         0x00dc
0033 #define VPX322_ADDR_SAT         0x0030
0034 
0035 struct go7007_usb_board {
0036     unsigned int flags;
0037     struct go7007_board_info main_info;
0038 };
0039 
0040 struct go7007_usb {
0041     struct go7007_usb_board *board;
0042     struct mutex i2c_lock;
0043     struct usb_device *usbdev;
0044     struct urb *video_urbs[8];
0045     struct urb *audio_urbs[8];
0046     struct urb *intr_urb;
0047 };
0048 
0049 static unsigned char aud_regs[] = {
0050     0x1e, 0x00,
0051     0x00, 0x17,
0052     0x02, 0x17,
0053     0x04, 0xf9,
0054     0x06, 0xf9,
0055     0x08, 0x02,
0056     0x0a, 0x00,
0057     0x0c, 0x00,
0058     0x0a, 0x00,
0059     0x0c, 0x00,
0060     0x0e, 0x02,
0061     0x10, 0x00,
0062     0x12, 0x01,
0063     0x00, 0x00,
0064 };
0065 
0066 
0067 static unsigned char vid_regs[] = {
0068     0xF2, 0x0f,
0069     0xAA, 0x00,
0070     0xF8, 0xff,
0071     0x00, 0x00,
0072 };
0073 
0074 static u16 vid_regs_fp[] = {
0075     0x028, 0x067,
0076     0x120, 0x016,
0077     0x121, 0xcF2,
0078     0x122, 0x0F2,
0079     0x123, 0x00c,
0080     0x124, 0x2d0,
0081     0x125, 0x2e0,
0082     0x126, 0x004,
0083     0x128, 0x1E0,
0084     0x12A, 0x016,
0085     0x12B, 0x0F2,
0086     0x12C, 0x0F2,
0087     0x12D, 0x00c,
0088     0x12E, 0x2d0,
0089     0x12F, 0x2e0,
0090     0x130, 0x004,
0091     0x132, 0x1E0,
0092     0x140, 0x060,
0093     0x153, 0x00C,
0094     0x154, 0x200,
0095     0x150, 0x801,
0096     0x000, 0x000
0097 };
0098 
0099 /* PAL specific values */
0100 static u16 vid_regs_fp_pal[] = {
0101     0x120, 0x017,
0102     0x121, 0xd22,
0103     0x122, 0x122,
0104     0x12A, 0x017,
0105     0x12B, 0x122,
0106     0x12C, 0x122,
0107     0x140, 0x060,
0108     0x000, 0x000,
0109 };
0110 
0111 struct s2250 {
0112     struct v4l2_subdev sd;
0113     struct v4l2_ctrl_handler hdl;
0114     v4l2_std_id std;
0115     int input;
0116     int brightness;
0117     int contrast;
0118     int saturation;
0119     int hue;
0120     int reg12b_val;
0121     int audio_input;
0122     struct i2c_client *audio;
0123 };
0124 
0125 static inline struct s2250 *to_state(struct v4l2_subdev *sd)
0126 {
0127     return container_of(sd, struct s2250, sd);
0128 }
0129 
0130 /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
0131 static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
0132     u16 value, u16 index, void *transfer_buffer, int length, int in)
0133 {
0134     struct go7007_usb *usb = go->hpi_context;
0135     int timeout = 5000;
0136 
0137     if (in) {
0138         return usb_control_msg(usb->usbdev,
0139                 usb_rcvctrlpipe(usb->usbdev, 0), request,
0140                 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0141                 value, index, transfer_buffer, length, timeout);
0142     } else {
0143         return usb_control_msg(usb->usbdev,
0144                 usb_sndctrlpipe(usb->usbdev, 0), request,
0145                 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0146                 value, index, transfer_buffer, length, timeout);
0147     }
0148 }
0149 /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
0150 
0151 static int write_reg(struct i2c_client *client, u8 reg, u8 value)
0152 {
0153     struct go7007 *go = i2c_get_adapdata(client->adapter);
0154     struct go7007_usb *usb;
0155     int rc;
0156     int dev_addr = client->addr << 1;  /* firmware wants 8-bit address */
0157     u8 *buf;
0158 
0159     if (go == NULL)
0160         return -ENODEV;
0161 
0162     if (go->status == STATUS_SHUTDOWN)
0163         return -EBUSY;
0164 
0165     buf = kzalloc(16, GFP_KERNEL);
0166     if (buf == NULL)
0167         return -ENOMEM;
0168 
0169     usb = go->hpi_context;
0170     if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
0171         dev_info(&client->dev, "i2c lock failed\n");
0172         kfree(buf);
0173         return -EINTR;
0174     }
0175     rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
0176                        (reg<<8 | value),
0177                        buf,
0178                        16, 1);
0179 
0180     mutex_unlock(&usb->i2c_lock);
0181     kfree(buf);
0182     return rc;
0183 }
0184 
0185 static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
0186 {
0187     struct go7007 *go = i2c_get_adapdata(client->adapter);
0188     struct go7007_usb *usb;
0189     int rc;
0190     u8 *buf;
0191     struct s2250 *dec = i2c_get_clientdata(client);
0192 
0193     if (go == NULL)
0194         return -ENODEV;
0195 
0196     if (go->status == STATUS_SHUTDOWN)
0197         return -EBUSY;
0198 
0199     buf = kzalloc(16, GFP_KERNEL);
0200 
0201     if (buf == NULL)
0202         return -ENOMEM;
0203 
0204 
0205 
0206     memset(buf, 0xcd, 6);
0207 
0208     usb = go->hpi_context;
0209     if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
0210         dev_info(&client->dev, "i2c lock failed\n");
0211         kfree(buf);
0212         return -EINTR;
0213     }
0214     rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
0215     mutex_unlock(&usb->i2c_lock);
0216     if (rc < 0) {
0217         kfree(buf);
0218         return rc;
0219     }
0220 
0221     if (buf[0] == 0) {
0222         unsigned int subaddr, val_read;
0223 
0224         subaddr = (buf[4] << 8) + buf[5];
0225         val_read = (buf[2] << 8) + buf[3];
0226         kfree(buf);
0227         if (val_read != val) {
0228             dev_info(&client->dev, "invalid fp write %x %x\n",
0229                  val_read, val);
0230             return -EFAULT;
0231         }
0232         if (subaddr != addr) {
0233             dev_info(&client->dev, "invalid fp write addr %x %x\n",
0234                  subaddr, addr);
0235             return -EFAULT;
0236         }
0237     } else {
0238         kfree(buf);
0239         return -EFAULT;
0240     }
0241 
0242     /* save last 12b value */
0243     if (addr == 0x12b)
0244         dec->reg12b_val = val;
0245 
0246     return 0;
0247 }
0248 
0249 static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
0250 {
0251     struct go7007 *go = i2c_get_adapdata(client->adapter);
0252     struct go7007_usb *usb;
0253     int rc;
0254     u8 *buf;
0255 
0256     if (go == NULL)
0257         return -ENODEV;
0258 
0259     if (go->status == STATUS_SHUTDOWN)
0260         return -EBUSY;
0261 
0262     buf = kzalloc(16, GFP_KERNEL);
0263 
0264     if (buf == NULL)
0265         return -ENOMEM;
0266 
0267 
0268 
0269     memset(buf, 0xcd, 6);
0270     usb = go->hpi_context;
0271     if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
0272         dev_info(&client->dev, "i2c lock failed\n");
0273         kfree(buf);
0274         return -EINTR;
0275     }
0276     rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
0277     mutex_unlock(&usb->i2c_lock);
0278     if (rc < 0) {
0279         kfree(buf);
0280         return rc;
0281     }
0282 
0283     *val = (buf[0] << 8) | buf[1];
0284     kfree(buf);
0285 
0286     return 0;
0287 }
0288 
0289 
0290 static int write_regs(struct i2c_client *client, u8 *regs)
0291 {
0292     int i;
0293 
0294     for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
0295         if (write_reg(client, regs[i], regs[i+1]) < 0) {
0296             dev_info(&client->dev, "failed\n");
0297             return -1;
0298         }
0299     }
0300     return 0;
0301 }
0302 
0303 static int write_regs_fp(struct i2c_client *client, u16 *regs)
0304 {
0305     int i;
0306 
0307     for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
0308         if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
0309             dev_info(&client->dev, "failed fp\n");
0310             return -1;
0311         }
0312     }
0313     return 0;
0314 }
0315 
0316 
0317 /* ------------------------------------------------------------------------- */
0318 
0319 static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
0320                  u32 config)
0321 {
0322     struct s2250 *state = to_state(sd);
0323     struct i2c_client *client = v4l2_get_subdevdata(sd);
0324     int vidsys;
0325 
0326     vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
0327     if (input == 0) {
0328         /* composite */
0329         write_reg_fp(client, 0x20, 0x020 | vidsys);
0330         write_reg_fp(client, 0x21, 0x662);
0331         write_reg_fp(client, 0x140, 0x060);
0332     } else if (input == 1) {
0333         /* S-Video */
0334         write_reg_fp(client, 0x20, 0x040 | vidsys);
0335         write_reg_fp(client, 0x21, 0x666);
0336         write_reg_fp(client, 0x140, 0x060);
0337     } else {
0338         return -EINVAL;
0339     }
0340     state->input = input;
0341     return 0;
0342 }
0343 
0344 static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
0345 {
0346     struct s2250 *state = to_state(sd);
0347     struct i2c_client *client = v4l2_get_subdevdata(sd);
0348     u16 vidsource;
0349 
0350     vidsource = (state->input == 1) ? 0x040 : 0x020;
0351     if (norm & V4L2_STD_625_50) {
0352         write_regs_fp(client, vid_regs_fp);
0353         write_regs_fp(client, vid_regs_fp_pal);
0354         write_reg_fp(client, 0x20, vidsource);
0355     } else {
0356         write_regs_fp(client, vid_regs_fp);
0357         write_reg_fp(client, 0x20, vidsource | 1);
0358     }
0359     state->std = norm;
0360     return 0;
0361 }
0362 
0363 static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
0364 {
0365     struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
0366     struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
0367     u16 oldvalue;
0368 
0369     switch (ctrl->id) {
0370     case V4L2_CID_BRIGHTNESS:
0371         read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
0372         write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
0373                  ctrl->val | (oldvalue & ~0xff));
0374         read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
0375         write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
0376                  ctrl->val | (oldvalue & ~0xff));
0377         write_reg_fp(client, 0x140, 0x60);
0378         break;
0379     case V4L2_CID_CONTRAST:
0380         read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
0381         write_reg_fp(client, VPX322_ADDR_CONTRAST0,
0382                  ctrl->val | (oldvalue & ~0x3f));
0383         read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
0384         write_reg_fp(client, VPX322_ADDR_CONTRAST1,
0385                  ctrl->val | (oldvalue & ~0x3f));
0386         write_reg_fp(client, 0x140, 0x60);
0387         break;
0388     case V4L2_CID_SATURATION:
0389         write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
0390         break;
0391     case V4L2_CID_HUE:
0392         write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
0393         break;
0394     default:
0395         return -EINVAL;
0396     }
0397     return 0;
0398 }
0399 
0400 static int s2250_set_fmt(struct v4l2_subdev *sd,
0401         struct v4l2_subdev_state *sd_state,
0402         struct v4l2_subdev_format *format)
0403 {
0404     struct v4l2_mbus_framefmt *fmt = &format->format;
0405     struct s2250 *state = to_state(sd);
0406     struct i2c_client *client = v4l2_get_subdevdata(sd);
0407 
0408     if (format->pad)
0409         return -EINVAL;
0410 
0411     if (format->which == V4L2_SUBDEV_FORMAT_TRY)
0412         return 0;
0413 
0414     if (fmt->height < 640) {
0415         write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
0416         write_reg_fp(client, 0x140, 0x060);
0417     } else {
0418         write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
0419         write_reg_fp(client, 0x140, 0x060);
0420     }
0421     return 0;
0422 }
0423 
0424 static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
0425                  u32 config)
0426 {
0427     struct s2250 *state = to_state(sd);
0428 
0429     switch (input) {
0430     case 0:
0431         write_reg(state->audio, 0x08, 0x02); /* Line In */
0432         break;
0433     case 1:
0434         write_reg(state->audio, 0x08, 0x04); /* Mic */
0435         break;
0436     case 2:
0437         write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
0438         break;
0439     default:
0440         return -EINVAL;
0441     }
0442     state->audio_input = input;
0443     return 0;
0444 }
0445 
0446 
0447 static int s2250_log_status(struct v4l2_subdev *sd)
0448 {
0449     struct s2250 *state = to_state(sd);
0450 
0451     v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
0452                     state->std == V4L2_STD_PAL ? "PAL" :
0453                     state->std == V4L2_STD_SECAM ? "SECAM" :
0454                     "unknown");
0455     v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
0456                     state->input == 1 ? "S-video" :
0457                     "error");
0458     v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
0459                     state->audio_input == 1 ? "Mic" :
0460                     state->audio_input == 2 ? "Mic Boost" :
0461                     "error");
0462     return v4l2_ctrl_subdev_log_status(sd);
0463 }
0464 
0465 /* --------------------------------------------------------------------------*/
0466 
0467 static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
0468     .s_ctrl = s2250_s_ctrl,
0469 };
0470 
0471 static const struct v4l2_subdev_core_ops s2250_core_ops = {
0472     .log_status = s2250_log_status,
0473 };
0474 
0475 static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
0476     .s_routing = s2250_s_audio_routing,
0477 };
0478 
0479 static const struct v4l2_subdev_video_ops s2250_video_ops = {
0480     .s_std = s2250_s_std,
0481     .s_routing = s2250_s_video_routing,
0482 };
0483 
0484 static const struct v4l2_subdev_pad_ops s2250_pad_ops = {
0485     .set_fmt = s2250_set_fmt,
0486 };
0487 
0488 static const struct v4l2_subdev_ops s2250_ops = {
0489     .core = &s2250_core_ops,
0490     .audio = &s2250_audio_ops,
0491     .video = &s2250_video_ops,
0492     .pad = &s2250_pad_ops,
0493 };
0494 
0495 /* --------------------------------------------------------------------------*/
0496 
0497 static int s2250_probe(struct i2c_client *client,
0498                const struct i2c_device_id *id)
0499 {
0500     struct i2c_client *audio;
0501     struct i2c_adapter *adapter = client->adapter;
0502     struct s2250 *state;
0503     struct v4l2_subdev *sd;
0504     u8 *data;
0505     struct go7007 *go = i2c_get_adapdata(adapter);
0506     struct go7007_usb *usb = go->hpi_context;
0507     int err = -EIO;
0508 
0509     audio = i2c_new_dummy_device(adapter, TLV320_ADDRESS >> 1);
0510     if (IS_ERR(audio))
0511         return PTR_ERR(audio);
0512 
0513     state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
0514     if (state == NULL) {
0515         i2c_unregister_device(audio);
0516         return -ENOMEM;
0517     }
0518 
0519     sd = &state->sd;
0520     v4l2_i2c_subdev_init(sd, client, &s2250_ops);
0521 
0522     v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
0523            "Sensoray 2250/2251", client->addr, client->adapter->name);
0524 
0525     v4l2_ctrl_handler_init(&state->hdl, 4);
0526     v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
0527         V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
0528     v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
0529         V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
0530     v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
0531         V4L2_CID_SATURATION, 0, 4094, 1, 2070);
0532     v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
0533         V4L2_CID_HUE, -512, 511, 1, 0);
0534     sd->ctrl_handler = &state->hdl;
0535     if (state->hdl.error) {
0536         err = state->hdl.error;
0537         goto fail;
0538     }
0539 
0540     state->std = V4L2_STD_NTSC;
0541     state->brightness = 50;
0542     state->contrast = 50;
0543     state->saturation = 50;
0544     state->hue = 0;
0545     state->audio = audio;
0546 
0547     /* initialize the audio */
0548     if (write_regs(audio, aud_regs) < 0) {
0549         dev_err(&client->dev, "error initializing audio\n");
0550         goto fail;
0551     }
0552 
0553     if (write_regs(client, vid_regs) < 0) {
0554         dev_err(&client->dev, "error initializing decoder\n");
0555         goto fail;
0556     }
0557     if (write_regs_fp(client, vid_regs_fp) < 0) {
0558         dev_err(&client->dev, "error initializing decoder\n");
0559         goto fail;
0560     }
0561     /* set default channel */
0562     /* composite */
0563     write_reg_fp(client, 0x20, 0x020 | 1);
0564     write_reg_fp(client, 0x21, 0x662);
0565     write_reg_fp(client, 0x140, 0x060);
0566 
0567     /* set default audio input */
0568     state->audio_input = 0;
0569     write_reg(client, 0x08, 0x02); /* Line In */
0570 
0571     if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
0572         data = kzalloc(16, GFP_KERNEL);
0573         if (data != NULL) {
0574             int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
0575                                data, 16, 1);
0576 
0577             if (rc > 0) {
0578                 u8 mask;
0579 
0580                 data[0] = 0;
0581                 mask = 1<<5;
0582                 data[0] &= ~mask;
0583                 data[1] |= mask;
0584                 go7007_usb_vendor_request(go, 0x40, 0,
0585                               (data[1]<<8)
0586                               + data[1],
0587                               data, 16, 0);
0588             }
0589             kfree(data);
0590         }
0591         mutex_unlock(&usb->i2c_lock);
0592     }
0593 
0594     v4l2_info(sd, "initialized successfully\n");
0595     return 0;
0596 
0597 fail:
0598     i2c_unregister_device(audio);
0599     v4l2_ctrl_handler_free(&state->hdl);
0600     kfree(state);
0601     return err;
0602 }
0603 
0604 static int s2250_remove(struct i2c_client *client)
0605 {
0606     struct s2250 *state = to_state(i2c_get_clientdata(client));
0607 
0608     i2c_unregister_device(state->audio);
0609     v4l2_device_unregister_subdev(&state->sd);
0610     v4l2_ctrl_handler_free(&state->hdl);
0611     kfree(state);
0612     return 0;
0613 }
0614 
0615 static const struct i2c_device_id s2250_id[] = {
0616     { "s2250", 0 },
0617     { }
0618 };
0619 MODULE_DEVICE_TABLE(i2c, s2250_id);
0620 
0621 static struct i2c_driver s2250_driver = {
0622     .driver = {
0623         .name   = "s2250",
0624     },
0625     .probe      = s2250_probe,
0626     .remove     = s2250_remove,
0627     .id_table   = s2250_id,
0628 };
0629 
0630 module_i2c_driver(s2250_driver);