18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/** 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * GSPCA sub driver for W996[78]CF JPEG USB Dual Mode Camera Chip. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * This module is adapted from the in kernel v4l1 w9968cf driver: 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Copyright (C) 2002-2004 by Luca Risolia <luca.risolia@studio.unibo.it> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci/* Note this is not a stand alone driver, it gets included in ov519.c, this 148c2ecf20Sopenharmony_ci is a bit of a hack, but it needs the driver code for a lot of different 158c2ecf20Sopenharmony_ci ov sensors which is already present in ov519.c (the old v4l1 driver used 168c2ecf20Sopenharmony_ci the ovchipcam framework). When we have the time we really should move 178c2ecf20Sopenharmony_ci the sensor drivers to v4l2 sub drivers, and properly split of this 188c2ecf20Sopenharmony_ci driver from ov519.c */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define W9968CF_I2C_BUS_DELAY 4 /* delay in us for I2C bit r/w operations */ 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define Y_QUANTABLE (&sd->jpeg_hdr[JPEG_QT0_OFFSET]) 258c2ecf20Sopenharmony_ci#define UV_QUANTABLE (&sd->jpeg_hdr[JPEG_QT1_OFFSET]) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic const struct v4l2_pix_format w9968cf_vga_mode[] = { 288c2ecf20Sopenharmony_ci {160, 120, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE, 298c2ecf20Sopenharmony_ci .bytesperline = 160 * 2, 308c2ecf20Sopenharmony_ci .sizeimage = 160 * 120 * 2, 318c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_JPEG}, 328c2ecf20Sopenharmony_ci {176, 144, V4L2_PIX_FMT_UYVY, V4L2_FIELD_NONE, 338c2ecf20Sopenharmony_ci .bytesperline = 176 * 2, 348c2ecf20Sopenharmony_ci .sizeimage = 176 * 144 * 2, 358c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_JPEG}, 368c2ecf20Sopenharmony_ci {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 378c2ecf20Sopenharmony_ci .bytesperline = 320 * 2, 388c2ecf20Sopenharmony_ci .sizeimage = 320 * 240 * 2, 398c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_JPEG}, 408c2ecf20Sopenharmony_ci {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 418c2ecf20Sopenharmony_ci .bytesperline = 352 * 2, 428c2ecf20Sopenharmony_ci .sizeimage = 352 * 288 * 2, 438c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_JPEG}, 448c2ecf20Sopenharmony_ci {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, 458c2ecf20Sopenharmony_ci .bytesperline = 640 * 2, 468c2ecf20Sopenharmony_ci .sizeimage = 640 * 480 * 2, 478c2ecf20Sopenharmony_ci .colorspace = V4L2_COLORSPACE_JPEG}, 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_cistatic void reg_w(struct sd *sd, u16 index, u16 value); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------- 538c2ecf20Sopenharmony_ci Write 64-bit data to the fast serial bus registers. 548c2ecf20Sopenharmony_ci Return 0 on success, -1 otherwise. 558c2ecf20Sopenharmony_ci --------------------------------------------------------------------------*/ 568c2ecf20Sopenharmony_cistatic void w9968cf_write_fsb(struct sd *sd, u16* data) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct usb_device *udev = sd->gspca_dev.dev; 598c2ecf20Sopenharmony_ci u16 value; 608c2ecf20Sopenharmony_ci int ret; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (sd->gspca_dev.usb_err < 0) 638c2ecf20Sopenharmony_ci return; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci value = *data++; 668c2ecf20Sopenharmony_ci memcpy(sd->gspca_dev.usb_buf, data, 6); 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci /* Avoid things going to fast for the bridge with a xhci host */ 698c2ecf20Sopenharmony_ci udelay(150); 708c2ecf20Sopenharmony_ci ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0, 718c2ecf20Sopenharmony_ci USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE, 728c2ecf20Sopenharmony_ci value, 0x06, sd->gspca_dev.usb_buf, 6, 500); 738c2ecf20Sopenharmony_ci if (ret < 0) { 748c2ecf20Sopenharmony_ci pr_err("Write FSB registers failed (%d)\n", ret); 758c2ecf20Sopenharmony_ci sd->gspca_dev.usb_err = ret; 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------- 808c2ecf20Sopenharmony_ci Write data to the serial bus control register. 818c2ecf20Sopenharmony_ci Return 0 on success, a negative number otherwise. 828c2ecf20Sopenharmony_ci --------------------------------------------------------------------------*/ 838c2ecf20Sopenharmony_cistatic void w9968cf_write_sb(struct sd *sd, u16 value) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci int ret; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (sd->gspca_dev.usb_err < 0) 888c2ecf20Sopenharmony_ci return; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci /* Avoid things going to fast for the bridge with a xhci host */ 918c2ecf20Sopenharmony_ci udelay(150); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci /* We don't use reg_w here, as that would cause all writes when 948c2ecf20Sopenharmony_ci bitbanging i2c to be logged, making the logs impossible to read */ 958c2ecf20Sopenharmony_ci ret = usb_control_msg(sd->gspca_dev.dev, 968c2ecf20Sopenharmony_ci usb_sndctrlpipe(sd->gspca_dev.dev, 0), 978c2ecf20Sopenharmony_ci 0, 988c2ecf20Sopenharmony_ci USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 998c2ecf20Sopenharmony_ci value, 0x01, NULL, 0, 500); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci udelay(W9968CF_I2C_BUS_DELAY); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (ret < 0) { 1048c2ecf20Sopenharmony_ci pr_err("Write SB reg [01] %04x failed\n", value); 1058c2ecf20Sopenharmony_ci sd->gspca_dev.usb_err = ret; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------- 1108c2ecf20Sopenharmony_ci Read data from the serial bus control register. 1118c2ecf20Sopenharmony_ci Return 0 on success, a negative number otherwise. 1128c2ecf20Sopenharmony_ci --------------------------------------------------------------------------*/ 1138c2ecf20Sopenharmony_cistatic int w9968cf_read_sb(struct sd *sd) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci int ret; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (sd->gspca_dev.usb_err < 0) 1188c2ecf20Sopenharmony_ci return -1; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* Avoid things going to fast for the bridge with a xhci host */ 1218c2ecf20Sopenharmony_ci udelay(150); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* We don't use reg_r here, as the w9968cf is special and has 16 1248c2ecf20Sopenharmony_ci bit registers instead of 8 bit */ 1258c2ecf20Sopenharmony_ci ret = usb_control_msg(sd->gspca_dev.dev, 1268c2ecf20Sopenharmony_ci usb_rcvctrlpipe(sd->gspca_dev.dev, 0), 1278c2ecf20Sopenharmony_ci 1, 1288c2ecf20Sopenharmony_ci USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1298c2ecf20Sopenharmony_ci 0, 0x01, sd->gspca_dev.usb_buf, 2, 500); 1308c2ecf20Sopenharmony_ci if (ret >= 0) { 1318c2ecf20Sopenharmony_ci ret = sd->gspca_dev.usb_buf[0] | 1328c2ecf20Sopenharmony_ci (sd->gspca_dev.usb_buf[1] << 8); 1338c2ecf20Sopenharmony_ci } else { 1348c2ecf20Sopenharmony_ci pr_err("Read SB reg [01] failed\n"); 1358c2ecf20Sopenharmony_ci sd->gspca_dev.usb_err = ret; 1368c2ecf20Sopenharmony_ci /* 1378c2ecf20Sopenharmony_ci * Make sure the buffer is zeroed to avoid uninitialized 1388c2ecf20Sopenharmony_ci * values. 1398c2ecf20Sopenharmony_ci */ 1408c2ecf20Sopenharmony_ci memset(sd->gspca_dev.usb_buf, 0, 2); 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci udelay(W9968CF_I2C_BUS_DELAY); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci return ret; 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------- 1498c2ecf20Sopenharmony_ci Upload quantization tables for the JPEG compression. 1508c2ecf20Sopenharmony_ci This function is called by w9968cf_start_transfer(). 1518c2ecf20Sopenharmony_ci Return 0 on success, a negative number otherwise. 1528c2ecf20Sopenharmony_ci --------------------------------------------------------------------------*/ 1538c2ecf20Sopenharmony_cistatic void w9968cf_upload_quantizationtables(struct sd *sd) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci u16 a, b; 1568c2ecf20Sopenharmony_ci int i, j; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci reg_w(sd, 0x39, 0x0010); /* JPEG clock enable */ 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci for (i = 0, j = 0; i < 32; i++, j += 2) { 1618c2ecf20Sopenharmony_ci a = Y_QUANTABLE[j] | ((unsigned)(Y_QUANTABLE[j + 1]) << 8); 1628c2ecf20Sopenharmony_ci b = UV_QUANTABLE[j] | ((unsigned)(UV_QUANTABLE[j + 1]) << 8); 1638c2ecf20Sopenharmony_ci reg_w(sd, 0x40 + i, a); 1648c2ecf20Sopenharmony_ci reg_w(sd, 0x60 + i, b); 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci reg_w(sd, 0x39, 0x0012); /* JPEG encoder enable */ 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci/**************************************************************************** 1708c2ecf20Sopenharmony_ci * Low-level I2C I/O functions. * 1718c2ecf20Sopenharmony_ci * The adapter supports the following I2C transfer functions: * 1728c2ecf20Sopenharmony_ci * i2c_adap_fastwrite_byte_data() (at 400 kHz bit frequency only) * 1738c2ecf20Sopenharmony_ci * i2c_adap_read_byte_data() * 1748c2ecf20Sopenharmony_ci * i2c_adap_read_byte() * 1758c2ecf20Sopenharmony_ci ****************************************************************************/ 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic void w9968cf_smbus_start(struct sd *sd) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */ 1808c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */ 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic void w9968cf_smbus_stop(struct sd *sd) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0010); /* SDE=1, SDA=0, SCL=0 */ 1868c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0011); /* SDE=1, SDA=0, SCL=1 */ 1878c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */ 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic void w9968cf_smbus_write_byte(struct sd *sd, u8 v) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci u8 bit; 1938c2ecf20Sopenharmony_ci int sda; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci for (bit = 0 ; bit < 8 ; bit++) { 1968c2ecf20Sopenharmony_ci sda = (v & 0x80) ? 2 : 0; 1978c2ecf20Sopenharmony_ci v <<= 1; 1988c2ecf20Sopenharmony_ci /* SDE=1, SDA=sda, SCL=0 */ 1998c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x10 | sda); 2008c2ecf20Sopenharmony_ci /* SDE=1, SDA=sda, SCL=1 */ 2018c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x11 | sda); 2028c2ecf20Sopenharmony_ci /* SDE=1, SDA=sda, SCL=0 */ 2038c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x10 | sda); 2048c2ecf20Sopenharmony_ci } 2058c2ecf20Sopenharmony_ci} 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_cistatic void w9968cf_smbus_read_byte(struct sd *sd, u8 *v) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci u8 bit; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* No need to ensure SDA is high as we are always called after 2128c2ecf20Sopenharmony_ci read_ack which ends with SDA high */ 2138c2ecf20Sopenharmony_ci *v = 0; 2148c2ecf20Sopenharmony_ci for (bit = 0 ; bit < 8 ; bit++) { 2158c2ecf20Sopenharmony_ci *v <<= 1; 2168c2ecf20Sopenharmony_ci /* SDE=1, SDA=1, SCL=1 */ 2178c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0013); 2188c2ecf20Sopenharmony_ci *v |= (w9968cf_read_sb(sd) & 0x0008) ? 1 : 0; 2198c2ecf20Sopenharmony_ci /* SDE=1, SDA=1, SCL=0 */ 2208c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0012); 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic void w9968cf_smbus_write_nack(struct sd *sd) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci /* No need to ensure SDA is high as we are always called after 2278c2ecf20Sopenharmony_ci read_byte which ends with SDA high */ 2288c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */ 2298c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */ 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic void w9968cf_smbus_read_ack(struct sd *sd) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 2358c2ecf20Sopenharmony_ci int sda; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* Ensure SDA is high before raising clock to avoid a spurious stop */ 2388c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */ 2398c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0013); /* SDE=1, SDA=1, SCL=1 */ 2408c2ecf20Sopenharmony_ci sda = w9968cf_read_sb(sd); 2418c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0012); /* SDE=1, SDA=1, SCL=0 */ 2428c2ecf20Sopenharmony_ci if (sda >= 0 && (sda & 0x08)) { 2438c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_USBI, "Did not receive i2c ACK\n"); 2448c2ecf20Sopenharmony_ci sd->gspca_dev.usb_err = -EIO; 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci/* SMBus protocol: S Addr Wr [A] Subaddr [A] Value [A] P */ 2498c2ecf20Sopenharmony_cistatic void w9968cf_i2c_w(struct sd *sd, u8 reg, u8 value) 2508c2ecf20Sopenharmony_ci{ 2518c2ecf20Sopenharmony_ci struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 2528c2ecf20Sopenharmony_ci u16* data = (u16 *)sd->gspca_dev.usb_buf; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci data[0] = 0x082f | ((sd->sensor_addr & 0x80) ? 0x1500 : 0x0); 2558c2ecf20Sopenharmony_ci data[0] |= (sd->sensor_addr & 0x40) ? 0x4000 : 0x0; 2568c2ecf20Sopenharmony_ci data[1] = 0x2082 | ((sd->sensor_addr & 0x40) ? 0x0005 : 0x0); 2578c2ecf20Sopenharmony_ci data[1] |= (sd->sensor_addr & 0x20) ? 0x0150 : 0x0; 2588c2ecf20Sopenharmony_ci data[1] |= (sd->sensor_addr & 0x10) ? 0x5400 : 0x0; 2598c2ecf20Sopenharmony_ci data[2] = 0x8208 | ((sd->sensor_addr & 0x08) ? 0x0015 : 0x0); 2608c2ecf20Sopenharmony_ci data[2] |= (sd->sensor_addr & 0x04) ? 0x0540 : 0x0; 2618c2ecf20Sopenharmony_ci data[2] |= (sd->sensor_addr & 0x02) ? 0x5000 : 0x0; 2628c2ecf20Sopenharmony_ci data[3] = 0x1d20 | ((sd->sensor_addr & 0x02) ? 0x0001 : 0x0); 2638c2ecf20Sopenharmony_ci data[3] |= (sd->sensor_addr & 0x01) ? 0x0054 : 0x0; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci w9968cf_write_fsb(sd, data); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci data[0] = 0x8208 | ((reg & 0x80) ? 0x0015 : 0x0); 2688c2ecf20Sopenharmony_ci data[0] |= (reg & 0x40) ? 0x0540 : 0x0; 2698c2ecf20Sopenharmony_ci data[0] |= (reg & 0x20) ? 0x5000 : 0x0; 2708c2ecf20Sopenharmony_ci data[1] = 0x0820 | ((reg & 0x20) ? 0x0001 : 0x0); 2718c2ecf20Sopenharmony_ci data[1] |= (reg & 0x10) ? 0x0054 : 0x0; 2728c2ecf20Sopenharmony_ci data[1] |= (reg & 0x08) ? 0x1500 : 0x0; 2738c2ecf20Sopenharmony_ci data[1] |= (reg & 0x04) ? 0x4000 : 0x0; 2748c2ecf20Sopenharmony_ci data[2] = 0x2082 | ((reg & 0x04) ? 0x0005 : 0x0); 2758c2ecf20Sopenharmony_ci data[2] |= (reg & 0x02) ? 0x0150 : 0x0; 2768c2ecf20Sopenharmony_ci data[2] |= (reg & 0x01) ? 0x5400 : 0x0; 2778c2ecf20Sopenharmony_ci data[3] = 0x001d; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci w9968cf_write_fsb(sd, data); 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci data[0] = 0x8208 | ((value & 0x80) ? 0x0015 : 0x0); 2828c2ecf20Sopenharmony_ci data[0] |= (value & 0x40) ? 0x0540 : 0x0; 2838c2ecf20Sopenharmony_ci data[0] |= (value & 0x20) ? 0x5000 : 0x0; 2848c2ecf20Sopenharmony_ci data[1] = 0x0820 | ((value & 0x20) ? 0x0001 : 0x0); 2858c2ecf20Sopenharmony_ci data[1] |= (value & 0x10) ? 0x0054 : 0x0; 2868c2ecf20Sopenharmony_ci data[1] |= (value & 0x08) ? 0x1500 : 0x0; 2878c2ecf20Sopenharmony_ci data[1] |= (value & 0x04) ? 0x4000 : 0x0; 2888c2ecf20Sopenharmony_ci data[2] = 0x2082 | ((value & 0x04) ? 0x0005 : 0x0); 2898c2ecf20Sopenharmony_ci data[2] |= (value & 0x02) ? 0x0150 : 0x0; 2908c2ecf20Sopenharmony_ci data[2] |= (value & 0x01) ? 0x5400 : 0x0; 2918c2ecf20Sopenharmony_ci data[3] = 0xfe1d; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci w9968cf_write_fsb(sd, data); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_USBO, "i2c 0x%02x -> [0x%02x]\n", value, reg); 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci/* SMBus protocol: S Addr Wr [A] Subaddr [A] P S Addr+1 Rd [A] [Value] NA P */ 2998c2ecf20Sopenharmony_cistatic int w9968cf_i2c_r(struct sd *sd, u8 reg) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 3028c2ecf20Sopenharmony_ci int ret = 0; 3038c2ecf20Sopenharmony_ci u8 value; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci /* Fast serial bus data control disable */ 3068c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0013); /* don't change ! */ 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci w9968cf_smbus_start(sd); 3098c2ecf20Sopenharmony_ci w9968cf_smbus_write_byte(sd, sd->sensor_addr); 3108c2ecf20Sopenharmony_ci w9968cf_smbus_read_ack(sd); 3118c2ecf20Sopenharmony_ci w9968cf_smbus_write_byte(sd, reg); 3128c2ecf20Sopenharmony_ci w9968cf_smbus_read_ack(sd); 3138c2ecf20Sopenharmony_ci w9968cf_smbus_stop(sd); 3148c2ecf20Sopenharmony_ci w9968cf_smbus_start(sd); 3158c2ecf20Sopenharmony_ci w9968cf_smbus_write_byte(sd, sd->sensor_addr + 1); 3168c2ecf20Sopenharmony_ci w9968cf_smbus_read_ack(sd); 3178c2ecf20Sopenharmony_ci w9968cf_smbus_read_byte(sd, &value); 3188c2ecf20Sopenharmony_ci /* signal we don't want to read anymore, the v4l1 driver used to 3198c2ecf20Sopenharmony_ci send an ack here which is very wrong! (and then fixed 3208c2ecf20Sopenharmony_ci the issues this gave by retrying reads) */ 3218c2ecf20Sopenharmony_ci w9968cf_smbus_write_nack(sd); 3228c2ecf20Sopenharmony_ci w9968cf_smbus_stop(sd); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci /* Fast serial bus data control re-enable */ 3258c2ecf20Sopenharmony_ci w9968cf_write_sb(sd, 0x0030); 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci if (sd->gspca_dev.usb_err >= 0) { 3288c2ecf20Sopenharmony_ci ret = value; 3298c2ecf20Sopenharmony_ci gspca_dbg(gspca_dev, D_USBI, "i2c [0x%02X] -> 0x%02X\n", 3308c2ecf20Sopenharmony_ci reg, value); 3318c2ecf20Sopenharmony_ci } else 3328c2ecf20Sopenharmony_ci gspca_err(gspca_dev, "i2c read [0x%02x] failed\n", reg); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci return ret; 3358c2ecf20Sopenharmony_ci} 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------- 3388c2ecf20Sopenharmony_ci Turn on the LED on some webcams. A beep should be heard too. 3398c2ecf20Sopenharmony_ci Return 0 on success, a negative number otherwise. 3408c2ecf20Sopenharmony_ci --------------------------------------------------------------------------*/ 3418c2ecf20Sopenharmony_cistatic void w9968cf_configure(struct sd *sd) 3428c2ecf20Sopenharmony_ci{ 3438c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xff00); /* power-down */ 3448c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xbf17); /* reset everything */ 3458c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xbf10); /* normal operation */ 3468c2ecf20Sopenharmony_ci reg_w(sd, 0x01, 0x0010); /* serial bus, SDS high */ 3478c2ecf20Sopenharmony_ci reg_w(sd, 0x01, 0x0000); /* serial bus, SDS low */ 3488c2ecf20Sopenharmony_ci reg_w(sd, 0x01, 0x0010); /* ..high 'beep-beep' */ 3498c2ecf20Sopenharmony_ci reg_w(sd, 0x01, 0x0030); /* Set sda scl to FSB mode */ 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci sd->stopped = 1; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic void w9968cf_init(struct sd *sd) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci unsigned long hw_bufsize = sd->sif ? (352 * 288 * 2) : (640 * 480 * 2), 3578c2ecf20Sopenharmony_ci y0 = 0x0000, 3588c2ecf20Sopenharmony_ci u0 = y0 + hw_bufsize / 2, 3598c2ecf20Sopenharmony_ci v0 = u0 + hw_bufsize / 4, 3608c2ecf20Sopenharmony_ci y1 = v0 + hw_bufsize / 4, 3618c2ecf20Sopenharmony_ci u1 = y1 + hw_bufsize / 2, 3628c2ecf20Sopenharmony_ci v1 = u1 + hw_bufsize / 4; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xff00); /* power off */ 3658c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xbf10); /* power on */ 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci reg_w(sd, 0x03, 0x405d); /* DRAM timings */ 3688c2ecf20Sopenharmony_ci reg_w(sd, 0x04, 0x0030); /* SDRAM timings */ 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci reg_w(sd, 0x20, y0 & 0xffff); /* Y buf.0, low */ 3718c2ecf20Sopenharmony_ci reg_w(sd, 0x21, y0 >> 16); /* Y buf.0, high */ 3728c2ecf20Sopenharmony_ci reg_w(sd, 0x24, u0 & 0xffff); /* U buf.0, low */ 3738c2ecf20Sopenharmony_ci reg_w(sd, 0x25, u0 >> 16); /* U buf.0, high */ 3748c2ecf20Sopenharmony_ci reg_w(sd, 0x28, v0 & 0xffff); /* V buf.0, low */ 3758c2ecf20Sopenharmony_ci reg_w(sd, 0x29, v0 >> 16); /* V buf.0, high */ 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci reg_w(sd, 0x22, y1 & 0xffff); /* Y buf.1, low */ 3788c2ecf20Sopenharmony_ci reg_w(sd, 0x23, y1 >> 16); /* Y buf.1, high */ 3798c2ecf20Sopenharmony_ci reg_w(sd, 0x26, u1 & 0xffff); /* U buf.1, low */ 3808c2ecf20Sopenharmony_ci reg_w(sd, 0x27, u1 >> 16); /* U buf.1, high */ 3818c2ecf20Sopenharmony_ci reg_w(sd, 0x2a, v1 & 0xffff); /* V buf.1, low */ 3828c2ecf20Sopenharmony_ci reg_w(sd, 0x2b, v1 >> 16); /* V buf.1, high */ 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci reg_w(sd, 0x32, y1 & 0xffff); /* JPEG buf 0 low */ 3858c2ecf20Sopenharmony_ci reg_w(sd, 0x33, y1 >> 16); /* JPEG buf 0 high */ 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci reg_w(sd, 0x34, y1 & 0xffff); /* JPEG buf 1 low */ 3888c2ecf20Sopenharmony_ci reg_w(sd, 0x35, y1 >> 16); /* JPEG bug 1 high */ 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci reg_w(sd, 0x36, 0x0000);/* JPEG restart interval */ 3918c2ecf20Sopenharmony_ci reg_w(sd, 0x37, 0x0804);/*JPEG VLE FIFO threshold*/ 3928c2ecf20Sopenharmony_ci reg_w(sd, 0x38, 0x0000);/* disable hw up-scaling */ 3938c2ecf20Sopenharmony_ci reg_w(sd, 0x3f, 0x0000); /* JPEG/MCTL test data */ 3948c2ecf20Sopenharmony_ci} 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_cistatic void w9968cf_set_crop_window(struct sd *sd) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci int start_cropx, start_cropy, x, y, fw, fh, cw, ch, 3998c2ecf20Sopenharmony_ci max_width, max_height; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci if (sd->sif) { 4028c2ecf20Sopenharmony_ci max_width = 352; 4038c2ecf20Sopenharmony_ci max_height = 288; 4048c2ecf20Sopenharmony_ci } else { 4058c2ecf20Sopenharmony_ci max_width = 640; 4068c2ecf20Sopenharmony_ci max_height = 480; 4078c2ecf20Sopenharmony_ci } 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (sd->sensor == SEN_OV7620) { 4108c2ecf20Sopenharmony_ci /* 4118c2ecf20Sopenharmony_ci * Sigh, this is dependend on the clock / framerate changes 4128c2ecf20Sopenharmony_ci * made by the frequency control, sick. 4138c2ecf20Sopenharmony_ci * 4148c2ecf20Sopenharmony_ci * Note we cannot use v4l2_ctrl_g_ctrl here, as we get called 4158c2ecf20Sopenharmony_ci * from ov519.c:setfreq() with the ctrl lock held! 4168c2ecf20Sopenharmony_ci */ 4178c2ecf20Sopenharmony_ci if (sd->freq->val == 1) { 4188c2ecf20Sopenharmony_ci start_cropx = 277; 4198c2ecf20Sopenharmony_ci start_cropy = 37; 4208c2ecf20Sopenharmony_ci } else { 4218c2ecf20Sopenharmony_ci start_cropx = 105; 4228c2ecf20Sopenharmony_ci start_cropy = 37; 4238c2ecf20Sopenharmony_ci } 4248c2ecf20Sopenharmony_ci } else { 4258c2ecf20Sopenharmony_ci start_cropx = 320; 4268c2ecf20Sopenharmony_ci start_cropy = 35; 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci /* Work around to avoid FP arithmetic */ 4308c2ecf20Sopenharmony_ci #define SC(x) ((x) << 10) 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci /* Scaling factors */ 4338c2ecf20Sopenharmony_ci fw = SC(sd->gspca_dev.pixfmt.width) / max_width; 4348c2ecf20Sopenharmony_ci fh = SC(sd->gspca_dev.pixfmt.height) / max_height; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci cw = (fw >= fh) ? max_width : SC(sd->gspca_dev.pixfmt.width) / fh; 4378c2ecf20Sopenharmony_ci ch = (fw >= fh) ? SC(sd->gspca_dev.pixfmt.height) / fw : max_height; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci sd->sensor_width = max_width; 4408c2ecf20Sopenharmony_ci sd->sensor_height = max_height; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci x = (max_width - cw) / 2; 4438c2ecf20Sopenharmony_ci y = (max_height - ch) / 2; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci reg_w(sd, 0x10, start_cropx + x); 4468c2ecf20Sopenharmony_ci reg_w(sd, 0x11, start_cropy + y); 4478c2ecf20Sopenharmony_ci reg_w(sd, 0x12, start_cropx + x + cw); 4488c2ecf20Sopenharmony_ci reg_w(sd, 0x13, start_cropy + y + ch); 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_cistatic void w9968cf_mode_init_regs(struct sd *sd) 4528c2ecf20Sopenharmony_ci{ 4538c2ecf20Sopenharmony_ci int val, vs_polarity, hs_polarity; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci w9968cf_set_crop_window(sd); 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci reg_w(sd, 0x14, sd->gspca_dev.pixfmt.width); 4588c2ecf20Sopenharmony_ci reg_w(sd, 0x15, sd->gspca_dev.pixfmt.height); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci /* JPEG width & height */ 4618c2ecf20Sopenharmony_ci reg_w(sd, 0x30, sd->gspca_dev.pixfmt.width); 4628c2ecf20Sopenharmony_ci reg_w(sd, 0x31, sd->gspca_dev.pixfmt.height); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci /* Y & UV frame buffer strides (in WORD) */ 4658c2ecf20Sopenharmony_ci if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat == 4668c2ecf20Sopenharmony_ci V4L2_PIX_FMT_JPEG) { 4678c2ecf20Sopenharmony_ci reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width / 2); 4688c2ecf20Sopenharmony_ci reg_w(sd, 0x2d, sd->gspca_dev.pixfmt.width / 4); 4698c2ecf20Sopenharmony_ci } else 4708c2ecf20Sopenharmony_ci reg_w(sd, 0x2c, sd->gspca_dev.pixfmt.width); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xbf17); /* reset everything */ 4738c2ecf20Sopenharmony_ci reg_w(sd, 0x00, 0xbf10); /* normal operation */ 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci /* Transfer size in WORDS (for UYVY format only) */ 4768c2ecf20Sopenharmony_ci val = sd->gspca_dev.pixfmt.width * sd->gspca_dev.pixfmt.height; 4778c2ecf20Sopenharmony_ci reg_w(sd, 0x3d, val & 0xffff); /* low bits */ 4788c2ecf20Sopenharmony_ci reg_w(sd, 0x3e, val >> 16); /* high bits */ 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat == 4818c2ecf20Sopenharmony_ci V4L2_PIX_FMT_JPEG) { 4828c2ecf20Sopenharmony_ci /* We may get called multiple times (usb isoc bw negotiat.) */ 4838c2ecf20Sopenharmony_ci jpeg_define(sd->jpeg_hdr, sd->gspca_dev.pixfmt.height, 4848c2ecf20Sopenharmony_ci sd->gspca_dev.pixfmt.width, 0x22); /* JPEG 420 */ 4858c2ecf20Sopenharmony_ci jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual)); 4868c2ecf20Sopenharmony_ci w9968cf_upload_quantizationtables(sd); 4878c2ecf20Sopenharmony_ci v4l2_ctrl_grab(sd->jpegqual, true); 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci /* Video Capture Control Register */ 4918c2ecf20Sopenharmony_ci if (sd->sensor == SEN_OV7620) { 4928c2ecf20Sopenharmony_ci /* Seems to work around a bug in the image sensor */ 4938c2ecf20Sopenharmony_ci vs_polarity = 1; 4948c2ecf20Sopenharmony_ci hs_polarity = 1; 4958c2ecf20Sopenharmony_ci } else { 4968c2ecf20Sopenharmony_ci vs_polarity = 1; 4978c2ecf20Sopenharmony_ci hs_polarity = 0; 4988c2ecf20Sopenharmony_ci } 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci val = (vs_polarity << 12) | (hs_polarity << 11); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci /* NOTE: We may not have enough memory to do double buffering while 5038c2ecf20Sopenharmony_ci doing compression (amount of memory differs per model cam). 5048c2ecf20Sopenharmony_ci So we use the second image buffer also as jpeg stream buffer 5058c2ecf20Sopenharmony_ci (see w9968cf_init), and disable double buffering. */ 5068c2ecf20Sopenharmony_ci if (w9968cf_vga_mode[sd->gspca_dev.curr_mode].pixelformat == 5078c2ecf20Sopenharmony_ci V4L2_PIX_FMT_JPEG) { 5088c2ecf20Sopenharmony_ci /* val |= 0x0002; YUV422P */ 5098c2ecf20Sopenharmony_ci val |= 0x0003; /* YUV420P */ 5108c2ecf20Sopenharmony_ci } else 5118c2ecf20Sopenharmony_ci val |= 0x0080; /* Enable HW double buffering */ 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci /* val |= 0x0020; enable clamping */ 5148c2ecf20Sopenharmony_ci /* val |= 0x0008; enable (1-2-1) filter */ 5158c2ecf20Sopenharmony_ci /* val |= 0x000c; enable (2-3-6-3-2) filter */ 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci val |= 0x8000; /* capt. enable */ 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci reg_w(sd, 0x16, val); 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci sd->gspca_dev.empty_packet = 0; 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_cistatic void w9968cf_stop0(struct sd *sd) 5258c2ecf20Sopenharmony_ci{ 5268c2ecf20Sopenharmony_ci v4l2_ctrl_grab(sd->jpegqual, false); 5278c2ecf20Sopenharmony_ci reg_w(sd, 0x39, 0x0000); /* disable JPEG encoder */ 5288c2ecf20Sopenharmony_ci reg_w(sd, 0x16, 0x0000); /* stop video capture */ 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci/* The w9968cf docs say that a 0 sized packet means EOF (and also SOF 5328c2ecf20Sopenharmony_ci for the next frame). This seems to simply not be true when operating 5338c2ecf20Sopenharmony_ci in JPEG mode, in this case there may be empty packets within the 5348c2ecf20Sopenharmony_ci frame. So in JPEG mode use the JPEG SOI marker to detect SOF. 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci Note to make things even more interesting the w9968cf sends *PLANAR* jpeg, 5378c2ecf20Sopenharmony_ci to be precise it sends: SOI, SOF, DRI, SOS, Y-data, SOS, U-data, SOS, 5388c2ecf20Sopenharmony_ci V-data, EOI. */ 5398c2ecf20Sopenharmony_cistatic void w9968cf_pkt_scan(struct gspca_dev *gspca_dev, 5408c2ecf20Sopenharmony_ci u8 *data, /* isoc packet */ 5418c2ecf20Sopenharmony_ci int len) /* iso packet length */ 5428c2ecf20Sopenharmony_ci{ 5438c2ecf20Sopenharmony_ci struct sd *sd = (struct sd *) gspca_dev; 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci if (w9968cf_vga_mode[gspca_dev->curr_mode].pixelformat == 5468c2ecf20Sopenharmony_ci V4L2_PIX_FMT_JPEG) { 5478c2ecf20Sopenharmony_ci if (len >= 2 && 5488c2ecf20Sopenharmony_ci data[0] == 0xff && 5498c2ecf20Sopenharmony_ci data[1] == 0xd8) { 5508c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, LAST_PACKET, 5518c2ecf20Sopenharmony_ci NULL, 0); 5528c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, FIRST_PACKET, 5538c2ecf20Sopenharmony_ci sd->jpeg_hdr, JPEG_HDR_SZ); 5548c2ecf20Sopenharmony_ci /* Strip the ff d8, our own header (which adds 5558c2ecf20Sopenharmony_ci huffman and quantization tables) already has this */ 5568c2ecf20Sopenharmony_ci len -= 2; 5578c2ecf20Sopenharmony_ci data += 2; 5588c2ecf20Sopenharmony_ci } 5598c2ecf20Sopenharmony_ci } else { 5608c2ecf20Sopenharmony_ci /* In UYVY mode an empty packet signals EOF */ 5618c2ecf20Sopenharmony_ci if (gspca_dev->empty_packet) { 5628c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, LAST_PACKET, 5638c2ecf20Sopenharmony_ci NULL, 0); 5648c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, FIRST_PACKET, 5658c2ecf20Sopenharmony_ci NULL, 0); 5668c2ecf20Sopenharmony_ci gspca_dev->empty_packet = 0; 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci } 5698c2ecf20Sopenharmony_ci gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 5708c2ecf20Sopenharmony_ci} 571