Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Quickcam cameras initialization data
0004  *
0005  * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
0006  */
0007 #define MODULE_NAME "tv8532"
0008 
0009 #include "gspca.h"
0010 
0011 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
0012 MODULE_DESCRIPTION("TV8532 USB Camera Driver");
0013 MODULE_LICENSE("GPL");
0014 
0015 /* specific webcam descriptor */
0016 struct sd {
0017     struct gspca_dev gspca_dev; /* !! must be the first item */
0018 
0019     __u8 packet;
0020 };
0021 
0022 static const struct v4l2_pix_format sif_mode[] = {
0023     {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0024         .bytesperline = 176,
0025         .sizeimage = 176 * 144,
0026         .colorspace = V4L2_COLORSPACE_SRGB,
0027         .priv = 1},
0028     {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0029         .bytesperline = 352,
0030         .sizeimage = 352 * 288,
0031         .colorspace = V4L2_COLORSPACE_SRGB,
0032         .priv = 0},
0033 };
0034 
0035 /* TV-8532A (ICM532A) registers (LE) */
0036 #define R00_PART_CONTROL 0x00
0037 #define     LATENT_CHANGE   0x80
0038 #define     EXPO_CHANGE 0x04
0039 #define R01_TIMING_CONTROL_LOW 0x01
0040 #define     CMD_EEprom_Open 0x30
0041 #define     CMD_EEprom_Close 0x29
0042 #define R03_TABLE_ADDR 0x03
0043 #define R04_WTRAM_DATA_L 0x04
0044 #define R05_WTRAM_DATA_M 0x05
0045 #define R06_WTRAM_DATA_H 0x06
0046 #define R07_TABLE_LEN   0x07
0047 #define R08_RAM_WRITE_ACTION 0x08
0048 #define R0C_AD_WIDTHL   0x0c
0049 #define R0D_AD_WIDTHH   0x0d
0050 #define R0E_AD_HEIGHTL  0x0e
0051 #define R0F_AD_HEIGHTH  0x0f
0052 #define R10_AD_COL_BEGINL 0x10
0053 #define R11_AD_COL_BEGINH 0x11
0054 #define     MIRROR      0x04    /* [10] */
0055 #define R14_AD_ROW_BEGINL 0x14
0056 #define R15_AD_ROWBEGINH  0x15
0057 #define R1C_AD_EXPOSE_TIMEL 0x1c
0058 #define R20_GAIN_G1L    0x20
0059 #define R21_GAIN_G1H    0x21
0060 #define R22_GAIN_RL 0x22
0061 #define R23_GAIN_RH 0x23
0062 #define R24_GAIN_BL 0x24
0063 #define R25_GAIN_BH 0x25
0064 #define R26_GAIN_G2L    0x26
0065 #define R27_GAIN_G2H    0x27
0066 #define R28_QUANT   0x28
0067 #define R29_LINE    0x29
0068 #define R2C_POLARITY    0x2c
0069 #define R2D_POINT   0x2d
0070 #define R2E_POINTH  0x2e
0071 #define R2F_POINTB  0x2f
0072 #define R30_POINTBH 0x30
0073 #define R31_UPD     0x31
0074 #define R2A_HIGH_BUDGET 0x2a
0075 #define R2B_LOW_BUDGET  0x2b
0076 #define R34_VID     0x34
0077 #define R35_VIDH    0x35
0078 #define R36_PID     0x36
0079 #define R37_PIDH    0x37
0080 #define R39_Test1   0x39        /* GPIO */
0081 #define R3B_Test3   0x3b        /* GPIO */
0082 #define R83_AD_IDH  0x83
0083 #define R91_AD_SLOPEREG 0x91
0084 #define R94_AD_BITCONTROL 0x94
0085 
0086 static const u8 eeprom_data[][3] = {
0087 /*  dataH dataM dataL */
0088     {0x01, 0x00, 0x01},
0089     {0x01, 0x80, 0x11},
0090     {0x05, 0x00, 0x14},
0091     {0x05, 0x00, 0x1c},
0092     {0x0d, 0x00, 0x1e},
0093     {0x05, 0x00, 0x1f},
0094     {0x05, 0x05, 0x19},
0095     {0x05, 0x01, 0x1b},
0096     {0x05, 0x09, 0x1e},
0097     {0x0d, 0x89, 0x2e},
0098     {0x05, 0x89, 0x2f},
0099     {0x05, 0x0d, 0xd9},
0100     {0x05, 0x09, 0xf1},
0101 };
0102 
0103 
0104 /* write 1 byte */
0105 static void reg_w1(struct gspca_dev *gspca_dev,
0106           __u16 index, __u8 value)
0107 {
0108     gspca_dev->usb_buf[0] = value;
0109     usb_control_msg(gspca_dev->dev,
0110             usb_sndctrlpipe(gspca_dev->dev, 0),
0111             0x02,
0112             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0113             0,  /* value */
0114             index, gspca_dev->usb_buf, 1, 500);
0115 }
0116 
0117 /* write 2 bytes */
0118 static void reg_w2(struct gspca_dev *gspca_dev,
0119           u16 index, u16 value)
0120 {
0121     gspca_dev->usb_buf[0] = value;
0122     gspca_dev->usb_buf[1] = value >> 8;
0123     usb_control_msg(gspca_dev->dev,
0124             usb_sndctrlpipe(gspca_dev->dev, 0),
0125             0x02,
0126             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0127             0,  /* value */
0128             index, gspca_dev->usb_buf, 2, 500);
0129 }
0130 
0131 static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev)
0132 {
0133     int i;
0134 
0135     reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Open);
0136     for (i = 0; i < ARRAY_SIZE(eeprom_data); i++) {
0137         reg_w1(gspca_dev, R03_TABLE_ADDR, i);
0138         reg_w1(gspca_dev, R04_WTRAM_DATA_L, eeprom_data[i][2]);
0139         reg_w1(gspca_dev, R05_WTRAM_DATA_M, eeprom_data[i][1]);
0140         reg_w1(gspca_dev, R06_WTRAM_DATA_H, eeprom_data[i][0]);
0141         reg_w1(gspca_dev, R08_RAM_WRITE_ACTION, 0);
0142     }
0143     reg_w1(gspca_dev, R07_TABLE_LEN, i);
0144     reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Close);
0145 }
0146 
0147 /* this function is called at probe time */
0148 static int sd_config(struct gspca_dev *gspca_dev,
0149              const struct usb_device_id *id)
0150 {
0151     struct cam *cam;
0152 
0153     cam = &gspca_dev->cam;
0154     cam->cam_mode = sif_mode;
0155     cam->nmodes = ARRAY_SIZE(sif_mode);
0156 
0157     return 0;
0158 }
0159 
0160 static void tv_8532_setReg(struct gspca_dev *gspca_dev)
0161 {
0162     reg_w1(gspca_dev, R3B_Test3, 0x0a); /* Test0Sel = 10 */
0163     /******************************************************/
0164     reg_w1(gspca_dev, R0E_AD_HEIGHTL, 0x90);
0165     reg_w1(gspca_dev, R0F_AD_HEIGHTH, 0x01);
0166     reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, 0x018f);
0167     reg_w1(gspca_dev, R10_AD_COL_BEGINL, 0x44);
0168                         /* begin active line */
0169     reg_w1(gspca_dev, R11_AD_COL_BEGINH, 0x00);
0170                         /* mirror and digital gain */
0171     reg_w1(gspca_dev, R14_AD_ROW_BEGINL, 0x0a);
0172 
0173     reg_w1(gspca_dev, R94_AD_BITCONTROL, 0x02);
0174     reg_w1(gspca_dev, R91_AD_SLOPEREG, 0x00);
0175     reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE);
0176                         /* = 0x84 */
0177 }
0178 
0179 /* this function is called at probe and resume time */
0180 static int sd_init(struct gspca_dev *gspca_dev)
0181 {
0182     tv_8532WriteEEprom(gspca_dev);
0183 
0184     return 0;
0185 }
0186 
0187 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
0188 {
0189     reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, val);
0190     reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE);
0191                         /* 0x84 */
0192 }
0193 
0194 static void setgain(struct gspca_dev *gspca_dev, s32 val)
0195 {
0196     reg_w2(gspca_dev, R20_GAIN_G1L, val);
0197     reg_w2(gspca_dev, R22_GAIN_RL, val);
0198     reg_w2(gspca_dev, R24_GAIN_BL, val);
0199     reg_w2(gspca_dev, R26_GAIN_G2L, val);
0200 }
0201 
0202 /* -- start the camera -- */
0203 static int sd_start(struct gspca_dev *gspca_dev)
0204 {
0205     struct sd *sd = (struct sd *) gspca_dev;
0206 
0207     reg_w1(gspca_dev, R0C_AD_WIDTHL, 0xe8);     /* 0x20; 0x0c */
0208     reg_w1(gspca_dev, R0D_AD_WIDTHH, 0x03);
0209 
0210     /************************************************/
0211     reg_w1(gspca_dev, R28_QUANT, 0x90);
0212                     /* 0x72 compressed mode 0x28 */
0213     if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
0214         /* 176x144 */
0215         reg_w1(gspca_dev, R29_LINE, 0x41);
0216                     /* CIF - 2 lines/packet */
0217     } else {
0218         /* 352x288 */
0219         reg_w1(gspca_dev, R29_LINE, 0x81);
0220                     /* CIF - 2 lines/packet */
0221     }
0222     /************************************************/
0223     reg_w1(gspca_dev, R2C_POLARITY, 0x10);      /* slow clock */
0224     reg_w1(gspca_dev, R2D_POINT, 0x14);
0225     reg_w1(gspca_dev, R2E_POINTH, 0x01);
0226     reg_w1(gspca_dev, R2F_POINTB, 0x12);
0227     reg_w1(gspca_dev, R30_POINTBH, 0x01);
0228 
0229     tv_8532_setReg(gspca_dev);
0230 
0231     /************************************************/
0232     reg_w1(gspca_dev, R31_UPD, 0x01);   /* update registers */
0233     msleep(200);
0234     reg_w1(gspca_dev, R31_UPD, 0x00);   /* end update */
0235 
0236     gspca_dev->empty_packet = 0;        /* check the empty packets */
0237     sd->packet = 0;             /* ignore the first packets */
0238 
0239     return 0;
0240 }
0241 
0242 static void sd_stopN(struct gspca_dev *gspca_dev)
0243 {
0244     reg_w1(gspca_dev, R3B_Test3, 0x0b); /* Test0Sel = 11 = GPIO */
0245 }
0246 
0247 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
0248             u8 *data,           /* isoc packet */
0249             int len)            /* iso packet length */
0250 {
0251     struct sd *sd = (struct sd *) gspca_dev;
0252     int packet_type0, packet_type1;
0253 
0254     packet_type0 = packet_type1 = INTER_PACKET;
0255     if (gspca_dev->empty_packet) {
0256         gspca_dev->empty_packet = 0;
0257         sd->packet = gspca_dev->pixfmt.height / 2;
0258         packet_type0 = FIRST_PACKET;
0259     } else if (sd->packet == 0)
0260         return;         /* 2 more lines in 352x288 ! */
0261     sd->packet--;
0262     if (sd->packet == 0)
0263         packet_type1 = LAST_PACKET;
0264 
0265     /* each packet contains:
0266      * - header 2 bytes
0267      * - RGRG line
0268      * - 4 bytes
0269      * - GBGB line
0270      * - 4 bytes
0271      */
0272     gspca_frame_add(gspca_dev, packet_type0,
0273             data + 2, gspca_dev->pixfmt.width);
0274     gspca_frame_add(gspca_dev, packet_type1,
0275             data + gspca_dev->pixfmt.width + 5,
0276             gspca_dev->pixfmt.width);
0277 }
0278 
0279 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
0280 {
0281     struct gspca_dev *gspca_dev =
0282         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
0283 
0284     gspca_dev->usb_err = 0;
0285 
0286     if (!gspca_dev->streaming)
0287         return 0;
0288 
0289     switch (ctrl->id) {
0290     case V4L2_CID_EXPOSURE:
0291         setexposure(gspca_dev, ctrl->val);
0292         break;
0293     case V4L2_CID_GAIN:
0294         setgain(gspca_dev, ctrl->val);
0295         break;
0296     }
0297     return gspca_dev->usb_err;
0298 }
0299 
0300 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
0301     .s_ctrl = sd_s_ctrl,
0302 };
0303 
0304 static int sd_init_controls(struct gspca_dev *gspca_dev)
0305 {
0306     struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
0307 
0308     gspca_dev->vdev.ctrl_handler = hdl;
0309     v4l2_ctrl_handler_init(hdl, 2);
0310     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0311             V4L2_CID_EXPOSURE, 0, 0x18f, 1, 0x18f);
0312     v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0313             V4L2_CID_GAIN, 0, 0x7ff, 1, 0x100);
0314 
0315     if (hdl->error) {
0316         pr_err("Could not initialize controls\n");
0317         return hdl->error;
0318     }
0319     return 0;
0320 }
0321 
0322 /* sub-driver description */
0323 static const struct sd_desc sd_desc = {
0324     .name = MODULE_NAME,
0325     .config = sd_config,
0326     .init = sd_init,
0327     .init_controls = sd_init_controls,
0328     .start = sd_start,
0329     .stopN = sd_stopN,
0330     .pkt_scan = sd_pkt_scan,
0331 };
0332 
0333 /* -- module initialisation -- */
0334 static const struct usb_device_id device_table[] = {
0335     {USB_DEVICE(0x046d, 0x0920)},
0336     {USB_DEVICE(0x046d, 0x0921)},
0337     {USB_DEVICE(0x0545, 0x808b)},
0338     {USB_DEVICE(0x0545, 0x8333)},
0339     {USB_DEVICE(0x0923, 0x010f)},
0340     {}
0341 };
0342 
0343 MODULE_DEVICE_TABLE(usb, device_table);
0344 
0345 /* -- device connect -- */
0346 static int sd_probe(struct usb_interface *intf,
0347             const struct usb_device_id *id)
0348 {
0349     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
0350                    THIS_MODULE);
0351 }
0352 
0353 static struct usb_driver sd_driver = {
0354     .name = MODULE_NAME,
0355     .id_table = device_table,
0356     .probe = sd_probe,
0357     .disconnect = gspca_disconnect,
0358 #ifdef CONFIG_PM
0359     .suspend = gspca_suspend,
0360     .resume = gspca_resume,
0361     .reset_resume = gspca_resume,
0362 #endif
0363 };
0364 
0365 module_usb_driver(sd_driver);