Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Support for the sensor part which is integrated (I think) into the
0004  * st6422 stv06xx alike bridge, as its integrated there are no i2c writes
0005  * but instead direct bridge writes.
0006  *
0007  * Copyright (c) 2009 Hans de Goede <hdegoede@redhat.com>
0008  *
0009  * Strongly based on qc-usb-messenger, which is:
0010  * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
0011  *            Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
0012  * Copyright (c) 2002, 2003 Tuukka Toivonen
0013  */
0014 
0015 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0016 
0017 #include "stv06xx_st6422.h"
0018 
0019 static struct v4l2_pix_format st6422_mode[] = {
0020     /* Note we actually get 124 lines of data, of which we skip the 4st
0021        4 as they are garbage */
0022     {
0023         162,
0024         120,
0025         V4L2_PIX_FMT_SGRBG8,
0026         V4L2_FIELD_NONE,
0027         .sizeimage = 162 * 120,
0028         .bytesperline = 162,
0029         .colorspace = V4L2_COLORSPACE_SRGB,
0030         .priv = 1
0031     },
0032     /* Note we actually get 248 lines of data, of which we skip the 4st
0033        4 as they are garbage, and we tell the app it only gets the
0034        first 240 of the 244 lines it actually gets, so that it ignores
0035        the last 4. */
0036     {
0037         324,
0038         240,
0039         V4L2_PIX_FMT_SGRBG8,
0040         V4L2_FIELD_NONE,
0041         .sizeimage = 324 * 244,
0042         .bytesperline = 324,
0043         .colorspace = V4L2_COLORSPACE_SRGB,
0044         .priv = 0
0045     },
0046 };
0047 
0048 /* V4L2 controls supported by the driver */
0049 static int setbrightness(struct sd *sd, s32 val);
0050 static int setcontrast(struct sd *sd, s32 val);
0051 static int setgain(struct sd *sd, u8 gain);
0052 static int setexposure(struct sd *sd, s16 expo);
0053 
0054 static int st6422_s_ctrl(struct v4l2_ctrl *ctrl)
0055 {
0056     struct gspca_dev *gspca_dev =
0057         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
0058     struct sd *sd = (struct sd *)gspca_dev;
0059     int err = -EINVAL;
0060 
0061     switch (ctrl->id) {
0062     case V4L2_CID_BRIGHTNESS:
0063         err = setbrightness(sd, ctrl->val);
0064         break;
0065     case V4L2_CID_CONTRAST:
0066         err = setcontrast(sd, ctrl->val);
0067         break;
0068     case V4L2_CID_GAIN:
0069         err = setgain(sd, ctrl->val);
0070         break;
0071     case V4L2_CID_EXPOSURE:
0072         err = setexposure(sd, ctrl->val);
0073         break;
0074     }
0075 
0076     /* commit settings */
0077     if (err >= 0)
0078         err = stv06xx_write_bridge(sd, 0x143f, 0x01);
0079     sd->gspca_dev.usb_err = err;
0080     return err;
0081 }
0082 
0083 static const struct v4l2_ctrl_ops st6422_ctrl_ops = {
0084     .s_ctrl = st6422_s_ctrl,
0085 };
0086 
0087 static int st6422_init_controls(struct sd *sd)
0088 {
0089     struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
0090 
0091     v4l2_ctrl_handler_init(hdl, 4);
0092     v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
0093             V4L2_CID_BRIGHTNESS, 0, 31, 1, 3);
0094     v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
0095             V4L2_CID_CONTRAST, 0, 15, 1, 11);
0096     v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
0097             V4L2_CID_EXPOSURE, 0, 1023, 1, 256);
0098     v4l2_ctrl_new_std(hdl, &st6422_ctrl_ops,
0099             V4L2_CID_GAIN, 0, 255, 1, 64);
0100 
0101     return hdl->error;
0102 }
0103 
0104 static int st6422_probe(struct sd *sd)
0105 {
0106     if (sd->bridge != BRIDGE_ST6422)
0107         return -ENODEV;
0108 
0109     pr_info("st6422 sensor detected\n");
0110 
0111     sd->gspca_dev.cam.cam_mode = st6422_mode;
0112     sd->gspca_dev.cam.nmodes = ARRAY_SIZE(st6422_mode);
0113     return 0;
0114 }
0115 
0116 static int st6422_init(struct sd *sd)
0117 {
0118     int err = 0, i;
0119 
0120     static const u16 st6422_bridge_init[][2] = {
0121         { STV_ISO_ENABLE, 0x00 }, /* disable capture */
0122         { 0x1436, 0x00 },
0123         { 0x1432, 0x03 },   /* 0x00-0x1F brightness */
0124         { 0x143a, 0xf9 },   /* 0x00-0x0F contrast */
0125         { 0x0509, 0x38 },   /* R */
0126         { 0x050a, 0x38 },   /* G */
0127         { 0x050b, 0x38 },   /* B */
0128         { 0x050c, 0x2a },
0129         { 0x050d, 0x01 },
0130 
0131 
0132         { 0x1431, 0x00 },   /* 0x00-0x07 ??? */
0133         { 0x1433, 0x34 },   /* 160x120, 0x00-0x01 night filter */
0134         { 0x1438, 0x18 },   /* 640x480 */
0135 /* 18 bayes */
0136 /* 10 compressed? */
0137 
0138         { 0x1439, 0x00 },
0139 /* anti-noise?  0xa2 gives a perfect image */
0140 
0141         { 0x143b, 0x05 },
0142         { 0x143c, 0x00 },   /* 0x00-0x01 - ??? */
0143 
0144 
0145 /* shutter time 0x0000-0x03FF */
0146 /* low value  give good picures on moving objects (but requires much light) */
0147 /* high value gives good picures in darkness (but tends to be overexposed) */
0148         { 0x143e, 0x01 },
0149         { 0x143d, 0x00 },
0150 
0151         { 0x1442, 0xe2 },
0152 /* write: 1x1x xxxx */
0153 /* read:  1x1x xxxx */
0154 /*        bit 5 == button pressed and hold if 0 */
0155 /* write 0xe2,0xea */
0156 
0157 /* 0x144a */
0158 /* 0x00 init */
0159 /* bit 7 == button has been pressed, but not handled */
0160 
0161 /* interrupt */
0162 /* if(urb->iso_frame_desc[i].status == 0x80) { */
0163 /* if(urb->iso_frame_desc[i].status == 0x88) { */
0164 
0165         { 0x1500, 0xd0 },
0166         { 0x1500, 0xd0 },
0167         { 0x1500, 0x50 },   /* 0x00 - 0xFF  0x80 == compr ? */
0168 
0169         { 0x1501, 0xaf },
0170 /* high val-> light area gets darker */
0171 /* low val -> light area gets lighter */
0172         { 0x1502, 0xc2 },
0173 /* high val-> light area gets darker */
0174 /* low val -> light area gets lighter */
0175         { 0x1503, 0x45 },
0176 /* high val-> light area gets darker */
0177 /* low val -> light area gets lighter */
0178         { 0x1505, 0x02 },
0179 /* 2  : 324x248  80352 bytes */
0180 /* 7  : 248x162  40176 bytes */
0181 /* c+f: 162*124  20088 bytes */
0182 
0183         { 0x150e, 0x8e },
0184         { 0x150f, 0x37 },
0185         { 0x15c0, 0x00 },
0186         { 0x15c3, 0x08 },   /* 0x04/0x14 ... test pictures ??? */
0187 
0188 
0189         { 0x143f, 0x01 },   /* commit settings */
0190 
0191     };
0192 
0193     for (i = 0; i < ARRAY_SIZE(st6422_bridge_init) && !err; i++) {
0194         err = stv06xx_write_bridge(sd, st6422_bridge_init[i][0],
0195                            st6422_bridge_init[i][1]);
0196     }
0197 
0198     return err;
0199 }
0200 
0201 static int setbrightness(struct sd *sd, s32 val)
0202 {
0203     /* val goes from 0 -> 31 */
0204     return stv06xx_write_bridge(sd, 0x1432, val);
0205 }
0206 
0207 static int setcontrast(struct sd *sd, s32 val)
0208 {
0209     /* Val goes from 0 -> 15 */
0210     return stv06xx_write_bridge(sd, 0x143a, val | 0xf0);
0211 }
0212 
0213 static int setgain(struct sd *sd, u8 gain)
0214 {
0215     int err;
0216 
0217     /* Set red, green, blue, gain */
0218     err = stv06xx_write_bridge(sd, 0x0509, gain);
0219     if (err < 0)
0220         return err;
0221 
0222     err = stv06xx_write_bridge(sd, 0x050a, gain);
0223     if (err < 0)
0224         return err;
0225 
0226     err = stv06xx_write_bridge(sd, 0x050b, gain);
0227     if (err < 0)
0228         return err;
0229 
0230     /* 2 mystery writes */
0231     err = stv06xx_write_bridge(sd, 0x050c, 0x2a);
0232     if (err < 0)
0233         return err;
0234 
0235     return stv06xx_write_bridge(sd, 0x050d, 0x01);
0236 }
0237 
0238 static int setexposure(struct sd *sd, s16 expo)
0239 {
0240     int err;
0241 
0242     err = stv06xx_write_bridge(sd, 0x143d, expo & 0xff);
0243     if (err < 0)
0244         return err;
0245 
0246     return stv06xx_write_bridge(sd, 0x143e, expo >> 8);
0247 }
0248 
0249 static int st6422_start(struct sd *sd)
0250 {
0251     int err;
0252     struct cam *cam = &sd->gspca_dev.cam;
0253 
0254     if (cam->cam_mode[sd->gspca_dev.curr_mode].priv)
0255         err = stv06xx_write_bridge(sd, 0x1505, 0x0f);
0256     else
0257         err = stv06xx_write_bridge(sd, 0x1505, 0x02);
0258     if (err < 0)
0259         return err;
0260 
0261     /* commit settings */
0262     err = stv06xx_write_bridge(sd, 0x143f, 0x01);
0263     return (err < 0) ? err : 0;
0264 }
0265 
0266 static int st6422_stop(struct sd *sd)
0267 {
0268     struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
0269 
0270     gspca_dbg(gspca_dev, D_STREAM, "Halting stream\n");
0271 
0272     return 0;
0273 }