1 /* 2 * Quickcam cameras initialization data 3 * 4 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 */ 21 #define MODULE_NAME "tv8532" 22 23 #include "gspca.h" 24 25 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>"); 26 MODULE_DESCRIPTION("TV8532 USB Camera Driver"); 27 MODULE_LICENSE("GPL"); 28 29 /* specific webcam descriptor */ 30 struct sd { 31 struct gspca_dev gspca_dev; /* !! must be the first item */ 32 33 __u8 packet; 34 }; 35 36 static const struct v4l2_pix_format sif_mode[] = { 37 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 38 .bytesperline = 176, 39 .sizeimage = 176 * 144, 40 .colorspace = V4L2_COLORSPACE_SRGB, 41 .priv = 1}, 42 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE, 43 .bytesperline = 352, 44 .sizeimage = 352 * 288, 45 .colorspace = V4L2_COLORSPACE_SRGB, 46 .priv = 0}, 47 }; 48 49 /* TV-8532A (ICM532A) registers (LE) */ 50 #define R00_PART_CONTROL 0x00 51 #define LATENT_CHANGE 0x80 52 #define EXPO_CHANGE 0x04 53 #define R01_TIMING_CONTROL_LOW 0x01 54 #define CMD_EEprom_Open 0x30 55 #define CMD_EEprom_Close 0x29 56 #define R03_TABLE_ADDR 0x03 57 #define R04_WTRAM_DATA_L 0x04 58 #define R05_WTRAM_DATA_M 0x05 59 #define R06_WTRAM_DATA_H 0x06 60 #define R07_TABLE_LEN 0x07 61 #define R08_RAM_WRITE_ACTION 0x08 62 #define R0C_AD_WIDTHL 0x0c 63 #define R0D_AD_WIDTHH 0x0d 64 #define R0E_AD_HEIGHTL 0x0e 65 #define R0F_AD_HEIGHTH 0x0f 66 #define R10_AD_COL_BEGINL 0x10 67 #define R11_AD_COL_BEGINH 0x11 68 #define MIRROR 0x04 /* [10] */ 69 #define R14_AD_ROW_BEGINL 0x14 70 #define R15_AD_ROWBEGINH 0x15 71 #define R1C_AD_EXPOSE_TIMEL 0x1c 72 #define R20_GAIN_G1L 0x20 73 #define R21_GAIN_G1H 0x21 74 #define R22_GAIN_RL 0x22 75 #define R23_GAIN_RH 0x23 76 #define R24_GAIN_BL 0x24 77 #define R25_GAIN_BH 0x25 78 #define R26_GAIN_G2L 0x26 79 #define R27_GAIN_G2H 0x27 80 #define R28_QUANT 0x28 81 #define R29_LINE 0x29 82 #define R2C_POLARITY 0x2c 83 #define R2D_POINT 0x2d 84 #define R2E_POINTH 0x2e 85 #define R2F_POINTB 0x2f 86 #define R30_POINTBH 0x30 87 #define R31_UPD 0x31 88 #define R2A_HIGH_BUDGET 0x2a 89 #define R2B_LOW_BUDGET 0x2b 90 #define R34_VID 0x34 91 #define R35_VIDH 0x35 92 #define R36_PID 0x36 93 #define R37_PIDH 0x37 94 #define R39_Test1 0x39 /* GPIO */ 95 #define R3B_Test3 0x3b /* GPIO */ 96 #define R83_AD_IDH 0x83 97 #define R91_AD_SLOPEREG 0x91 98 #define R94_AD_BITCONTROL 0x94 99 100 static const u8 eeprom_data[][3] = { 101 /* dataH dataM dataL */ 102 {0x01, 0x00, 0x01}, 103 {0x01, 0x80, 0x11}, 104 {0x05, 0x00, 0x14}, 105 {0x05, 0x00, 0x1c}, 106 {0x0d, 0x00, 0x1e}, 107 {0x05, 0x00, 0x1f}, 108 {0x05, 0x05, 0x19}, 109 {0x05, 0x01, 0x1b}, 110 {0x05, 0x09, 0x1e}, 111 {0x0d, 0x89, 0x2e}, 112 {0x05, 0x89, 0x2f}, 113 {0x05, 0x0d, 0xd9}, 114 {0x05, 0x09, 0xf1}, 115 }; 116 117 118 /* write 1 byte */ 119 static void reg_w1(struct gspca_dev *gspca_dev, 120 __u16 index, __u8 value) 121 { 122 gspca_dev->usb_buf[0] = value; 123 usb_control_msg(gspca_dev->dev, 124 usb_sndctrlpipe(gspca_dev->dev, 0), 125 0x02, 126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 127 0, /* value */ 128 index, gspca_dev->usb_buf, 1, 500); 129 } 130 131 /* write 2 bytes */ 132 static void reg_w2(struct gspca_dev *gspca_dev, 133 u16 index, u16 value) 134 { 135 gspca_dev->usb_buf[0] = value; 136 gspca_dev->usb_buf[1] = value >> 8; 137 usb_control_msg(gspca_dev->dev, 138 usb_sndctrlpipe(gspca_dev->dev, 0), 139 0x02, 140 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 141 0, /* value */ 142 index, gspca_dev->usb_buf, 2, 500); 143 } 144 145 static void tv_8532WriteEEprom(struct gspca_dev *gspca_dev) 146 { 147 int i; 148 149 reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Open); 150 for (i = 0; i < ARRAY_SIZE(eeprom_data); i++) { 151 reg_w1(gspca_dev, R03_TABLE_ADDR, i); 152 reg_w1(gspca_dev, R04_WTRAM_DATA_L, eeprom_data[i][2]); 153 reg_w1(gspca_dev, R05_WTRAM_DATA_M, eeprom_data[i][1]); 154 reg_w1(gspca_dev, R06_WTRAM_DATA_H, eeprom_data[i][0]); 155 reg_w1(gspca_dev, R08_RAM_WRITE_ACTION, 0); 156 } 157 reg_w1(gspca_dev, R07_TABLE_LEN, i); 158 reg_w1(gspca_dev, R01_TIMING_CONTROL_LOW, CMD_EEprom_Close); 159 } 160 161 /* this function is called at probe time */ 162 static int sd_config(struct gspca_dev *gspca_dev, 163 const struct usb_device_id *id) 164 { 165 struct cam *cam; 166 167 cam = &gspca_dev->cam; 168 cam->cam_mode = sif_mode; 169 cam->nmodes = ARRAY_SIZE(sif_mode); 170 171 return 0; 172 } 173 174 static void tv_8532_setReg(struct gspca_dev *gspca_dev) 175 { 176 reg_w1(gspca_dev, R3B_Test3, 0x0a); /* Test0Sel = 10 */ 177 /******************************************************/ 178 reg_w1(gspca_dev, R0E_AD_HEIGHTL, 0x90); 179 reg_w1(gspca_dev, R0F_AD_HEIGHTH, 0x01); 180 reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, 0x018f); 181 reg_w1(gspca_dev, R10_AD_COL_BEGINL, 0x44); 182 /* begin active line */ 183 reg_w1(gspca_dev, R11_AD_COL_BEGINH, 0x00); 184 /* mirror and digital gain */ 185 reg_w1(gspca_dev, R14_AD_ROW_BEGINL, 0x0a); 186 187 reg_w1(gspca_dev, R94_AD_BITCONTROL, 0x02); 188 reg_w1(gspca_dev, R91_AD_SLOPEREG, 0x00); 189 reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE); 190 /* = 0x84 */ 191 } 192 193 /* this function is called at probe and resume time */ 194 static int sd_init(struct gspca_dev *gspca_dev) 195 { 196 tv_8532WriteEEprom(gspca_dev); 197 198 return 0; 199 } 200 201 static void setexposure(struct gspca_dev *gspca_dev, s32 val) 202 { 203 reg_w2(gspca_dev, R1C_AD_EXPOSE_TIMEL, val); 204 reg_w1(gspca_dev, R00_PART_CONTROL, LATENT_CHANGE | EXPO_CHANGE); 205 /* 0x84 */ 206 } 207 208 static void setgain(struct gspca_dev *gspca_dev, s32 val) 209 { 210 reg_w2(gspca_dev, R20_GAIN_G1L, val); 211 reg_w2(gspca_dev, R22_GAIN_RL, val); 212 reg_w2(gspca_dev, R24_GAIN_BL, val); 213 reg_w2(gspca_dev, R26_GAIN_G2L, val); 214 } 215 216 /* -- start the camera -- */ 217 static int sd_start(struct gspca_dev *gspca_dev) 218 { 219 struct sd *sd = (struct sd *) gspca_dev; 220 221 reg_w1(gspca_dev, R0C_AD_WIDTHL, 0xe8); /* 0x20; 0x0c */ 222 reg_w1(gspca_dev, R0D_AD_WIDTHH, 0x03); 223 224 /************************************************/ 225 reg_w1(gspca_dev, R28_QUANT, 0x90); 226 /* 0x72 compressed mode 0x28 */ 227 if (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) { 228 /* 176x144 */ 229 reg_w1(gspca_dev, R29_LINE, 0x41); 230 /* CIF - 2 lines/packet */ 231 } else { 232 /* 352x288 */ 233 reg_w1(gspca_dev, R29_LINE, 0x81); 234 /* CIF - 2 lines/packet */ 235 } 236 /************************************************/ 237 reg_w1(gspca_dev, R2C_POLARITY, 0x10); /* slow clock */ 238 reg_w1(gspca_dev, R2D_POINT, 0x14); 239 reg_w1(gspca_dev, R2E_POINTH, 0x01); 240 reg_w1(gspca_dev, R2F_POINTB, 0x12); 241 reg_w1(gspca_dev, R30_POINTBH, 0x01); 242 243 tv_8532_setReg(gspca_dev); 244 245 /************************************************/ 246 reg_w1(gspca_dev, R31_UPD, 0x01); /* update registers */ 247 msleep(200); 248 reg_w1(gspca_dev, R31_UPD, 0x00); /* end update */ 249 250 gspca_dev->empty_packet = 0; /* check the empty packets */ 251 sd->packet = 0; /* ignore the first packets */ 252 253 return 0; 254 } 255 256 static void sd_stopN(struct gspca_dev *gspca_dev) 257 { 258 reg_w1(gspca_dev, R3B_Test3, 0x0b); /* Test0Sel = 11 = GPIO */ 259 } 260 261 static void sd_pkt_scan(struct gspca_dev *gspca_dev, 262 u8 *data, /* isoc packet */ 263 int len) /* iso packet length */ 264 { 265 struct sd *sd = (struct sd *) gspca_dev; 266 int packet_type0, packet_type1; 267 268 packet_type0 = packet_type1 = INTER_PACKET; 269 if (gspca_dev->empty_packet) { 270 gspca_dev->empty_packet = 0; 271 sd->packet = gspca_dev->height / 2; 272 packet_type0 = FIRST_PACKET; 273 } else if (sd->packet == 0) 274 return; /* 2 more lines in 352x288 ! */ 275 sd->packet--; 276 if (sd->packet == 0) 277 packet_type1 = LAST_PACKET; 278 279 /* each packet contains: 280 * - header 2 bytes 281 * - RGRG line 282 * - 4 bytes 283 * - GBGB line 284 * - 4 bytes 285 */ 286 gspca_frame_add(gspca_dev, packet_type0, 287 data + 2, gspca_dev->width); 288 gspca_frame_add(gspca_dev, packet_type1, 289 data + gspca_dev->width + 5, gspca_dev->width); 290 } 291 292 static int sd_s_ctrl(struct v4l2_ctrl *ctrl) 293 { 294 struct gspca_dev *gspca_dev = 295 container_of(ctrl->handler, struct gspca_dev, ctrl_handler); 296 297 gspca_dev->usb_err = 0; 298 299 if (!gspca_dev->streaming) 300 return 0; 301 302 switch (ctrl->id) { 303 case V4L2_CID_EXPOSURE: 304 setexposure(gspca_dev, ctrl->val); 305 break; 306 case V4L2_CID_GAIN: 307 setgain(gspca_dev, ctrl->val); 308 break; 309 } 310 return gspca_dev->usb_err; 311 } 312 313 static const struct v4l2_ctrl_ops sd_ctrl_ops = { 314 .s_ctrl = sd_s_ctrl, 315 }; 316 317 static int sd_init_controls(struct gspca_dev *gspca_dev) 318 { 319 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler; 320 321 gspca_dev->vdev.ctrl_handler = hdl; 322 v4l2_ctrl_handler_init(hdl, 2); 323 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 324 V4L2_CID_EXPOSURE, 0, 0x18f, 1, 0x18f); 325 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 326 V4L2_CID_GAIN, 0, 0x7ff, 1, 0x100); 327 328 if (hdl->error) { 329 pr_err("Could not initialize controls\n"); 330 return hdl->error; 331 } 332 return 0; 333 } 334 335 /* sub-driver description */ 336 static const struct sd_desc sd_desc = { 337 .name = MODULE_NAME, 338 .config = sd_config, 339 .init = sd_init, 340 .init_controls = sd_init_controls, 341 .start = sd_start, 342 .stopN = sd_stopN, 343 .pkt_scan = sd_pkt_scan, 344 }; 345 346 /* -- module initialisation -- */ 347 static const struct usb_device_id device_table[] = { 348 {USB_DEVICE(0x046d, 0x0920)}, 349 {USB_DEVICE(0x046d, 0x0921)}, 350 {USB_DEVICE(0x0545, 0x808b)}, 351 {USB_DEVICE(0x0545, 0x8333)}, 352 {USB_DEVICE(0x0923, 0x010f)}, 353 {} 354 }; 355 356 MODULE_DEVICE_TABLE(usb, device_table); 357 358 /* -- device connect -- */ 359 static int sd_probe(struct usb_interface *intf, 360 const struct usb_device_id *id) 361 { 362 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), 363 THIS_MODULE); 364 } 365 366 static struct usb_driver sd_driver = { 367 .name = MODULE_NAME, 368 .id_table = device_table, 369 .probe = sd_probe, 370 .disconnect = gspca_disconnect, 371 #ifdef CONFIG_PM 372 .suspend = gspca_suspend, 373 .resume = gspca_resume, 374 .reset_resume = gspca_resume, 375 #endif 376 }; 377 378 module_usb_driver(sd_driver); 379