18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* stk-sensor.c: Driver for ov96xx sensor (used in some Syntek webcams)
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Some parts derived from ov7670.c:
78c2ecf20Sopenharmony_ci * Copyright 2006 One Laptop Per Child Association, Inc.  Written
88c2ecf20Sopenharmony_ci * by Jonathan Corbet with substantial inspiration from Mark
98c2ecf20Sopenharmony_ci * McClelland's ovcamchip code.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the GNU General
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* Controlling the sensor via the STK1125 vendor specific control interface:
178c2ecf20Sopenharmony_ci * The camera uses an OmniVision sensor and the stk1125 provides an
188c2ecf20Sopenharmony_ci * SCCB(i2c)-USB bridge which let us program the sensor.
198c2ecf20Sopenharmony_ci * In my case the sensor id is 0x9652, it can be read from sensor's register
208c2ecf20Sopenharmony_ci * 0x0A and 0x0B as follows:
218c2ecf20Sopenharmony_ci * - read register #R:
228c2ecf20Sopenharmony_ci *   output #R to index 0x0208
238c2ecf20Sopenharmony_ci *   output 0x0070 to index 0x0200
248c2ecf20Sopenharmony_ci *   input 1 byte from index 0x0201 (some kind of status register)
258c2ecf20Sopenharmony_ci *     until its value is 0x01
268c2ecf20Sopenharmony_ci *   input 1 byte from index 0x0209. This is the value of #R
278c2ecf20Sopenharmony_ci * - write value V to register #R
288c2ecf20Sopenharmony_ci *   output #R to index 0x0204
298c2ecf20Sopenharmony_ci *   output V to index 0x0205
308c2ecf20Sopenharmony_ci *   output 0x0005 to index 0x0200
318c2ecf20Sopenharmony_ci *   input 1 byte from index 0x0201 until its value becomes 0x04
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* It seems the i2c bus is controlled with these registers */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include "stk-webcam.h"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define STK_IIC_BASE		(0x0200)
418c2ecf20Sopenharmony_ci#  define STK_IIC_OP		(STK_IIC_BASE)
428c2ecf20Sopenharmony_ci#    define STK_IIC_OP_TX	(0x05)
438c2ecf20Sopenharmony_ci#    define STK_IIC_OP_RX	(0x70)
448c2ecf20Sopenharmony_ci#  define STK_IIC_STAT		(STK_IIC_BASE+1)
458c2ecf20Sopenharmony_ci#    define STK_IIC_STAT_TX_OK	(0x04)
468c2ecf20Sopenharmony_ci#    define STK_IIC_STAT_RX_OK	(0x01)
478c2ecf20Sopenharmony_ci/* I don't know what does this register.
488c2ecf20Sopenharmony_ci * when it is 0x00 or 0x01, we cannot talk to the sensor,
498c2ecf20Sopenharmony_ci * other values work */
508c2ecf20Sopenharmony_ci#  define STK_IIC_ENABLE	(STK_IIC_BASE+2)
518c2ecf20Sopenharmony_ci#    define STK_IIC_ENABLE_NO	(0x00)
528c2ecf20Sopenharmony_ci/* This is what the driver writes in windows */
538c2ecf20Sopenharmony_ci#    define STK_IIC_ENABLE_YES	(0x1e)
548c2ecf20Sopenharmony_ci/*
558c2ecf20Sopenharmony_ci * Address of the slave. Seems like the binary driver look for the
568c2ecf20Sopenharmony_ci * sensor in multiple places, attempting a reset sequence.
578c2ecf20Sopenharmony_ci * We only know about the ov9650
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci#  define STK_IIC_ADDR		(STK_IIC_BASE+3)
608c2ecf20Sopenharmony_ci#  define STK_IIC_TX_INDEX	(STK_IIC_BASE+4)
618c2ecf20Sopenharmony_ci#  define STK_IIC_TX_VALUE	(STK_IIC_BASE+5)
628c2ecf20Sopenharmony_ci#  define STK_IIC_RX_INDEX	(STK_IIC_BASE+8)
638c2ecf20Sopenharmony_ci#  define STK_IIC_RX_VALUE	(STK_IIC_BASE+9)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define MAX_RETRIES		(50)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define SENSOR_ADDRESS		(0x60)
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* From ov7670.c (These registers aren't fully accurate) */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* Registers */
728c2ecf20Sopenharmony_ci#define REG_GAIN	0x00	/* Gain lower 8 bits (rest in vref) */
738c2ecf20Sopenharmony_ci#define REG_BLUE	0x01	/* blue gain */
748c2ecf20Sopenharmony_ci#define REG_RED		0x02	/* red gain */
758c2ecf20Sopenharmony_ci#define REG_VREF	0x03	/* Pieces of GAIN, VSTART, VSTOP */
768c2ecf20Sopenharmony_ci#define REG_COM1	0x04	/* Control 1 */
778c2ecf20Sopenharmony_ci#define  COM1_CCIR656	  0x40  /* CCIR656 enable */
788c2ecf20Sopenharmony_ci#define  COM1_QFMT	  0x20  /* QVGA/QCIF format */
798c2ecf20Sopenharmony_ci#define  COM1_SKIP_0	  0x00  /* Do not skip any row */
808c2ecf20Sopenharmony_ci#define  COM1_SKIP_2	  0x04  /* Skip 2 rows of 4 */
818c2ecf20Sopenharmony_ci#define  COM1_SKIP_3	  0x08  /* Skip 3 rows of 4 */
828c2ecf20Sopenharmony_ci#define REG_BAVE	0x05	/* U/B Average level */
838c2ecf20Sopenharmony_ci#define REG_GbAVE	0x06	/* Y/Gb Average level */
848c2ecf20Sopenharmony_ci#define REG_AECHH	0x07	/* AEC MS 5 bits */
858c2ecf20Sopenharmony_ci#define REG_RAVE	0x08	/* V/R Average level */
868c2ecf20Sopenharmony_ci#define REG_COM2	0x09	/* Control 2 */
878c2ecf20Sopenharmony_ci#define  COM2_SSLEEP	  0x10	/* Soft sleep mode */
888c2ecf20Sopenharmony_ci#define REG_PID		0x0a	/* Product ID MSB */
898c2ecf20Sopenharmony_ci#define REG_VER		0x0b	/* Product ID LSB */
908c2ecf20Sopenharmony_ci#define REG_COM3	0x0c	/* Control 3 */
918c2ecf20Sopenharmony_ci#define  COM3_SWAP	  0x40	  /* Byte swap */
928c2ecf20Sopenharmony_ci#define  COM3_SCALEEN	  0x08	  /* Enable scaling */
938c2ecf20Sopenharmony_ci#define  COM3_DCWEN	  0x04	  /* Enable downsamp/crop/window */
948c2ecf20Sopenharmony_ci#define REG_COM4	0x0d	/* Control 4 */
958c2ecf20Sopenharmony_ci#define REG_COM5	0x0e	/* All "reserved" */
968c2ecf20Sopenharmony_ci#define REG_COM6	0x0f	/* Control 6 */
978c2ecf20Sopenharmony_ci#define REG_AECH	0x10	/* More bits of AEC value */
988c2ecf20Sopenharmony_ci#define REG_CLKRC	0x11	/* Clock control */
998c2ecf20Sopenharmony_ci#define   CLK_PLL	  0x80	  /* Enable internal PLL */
1008c2ecf20Sopenharmony_ci#define   CLK_EXT	  0x40	  /* Use external clock directly */
1018c2ecf20Sopenharmony_ci#define   CLK_SCALE	  0x3f	  /* Mask for internal clock scale */
1028c2ecf20Sopenharmony_ci#define REG_COM7	0x12	/* Control 7 */
1038c2ecf20Sopenharmony_ci#define   COM7_RESET	  0x80	  /* Register reset */
1048c2ecf20Sopenharmony_ci#define   COM7_FMT_MASK	  0x38
1058c2ecf20Sopenharmony_ci#define   COM7_FMT_SXGA	  0x00
1068c2ecf20Sopenharmony_ci#define   COM7_FMT_VGA	  0x40
1078c2ecf20Sopenharmony_ci#define	  COM7_FMT_CIF	  0x20	  /* CIF format */
1088c2ecf20Sopenharmony_ci#define   COM7_FMT_QVGA	  0x10	  /* QVGA format */
1098c2ecf20Sopenharmony_ci#define   COM7_FMT_QCIF	  0x08	  /* QCIF format */
1108c2ecf20Sopenharmony_ci#define	  COM7_RGB	  0x04	  /* bits 0 and 2 - RGB format */
1118c2ecf20Sopenharmony_ci#define	  COM7_YUV	  0x00	  /* YUV */
1128c2ecf20Sopenharmony_ci#define	  COM7_BAYER	  0x01	  /* Bayer format */
1138c2ecf20Sopenharmony_ci#define	  COM7_PBAYER	  0x05	  /* "Processed bayer" */
1148c2ecf20Sopenharmony_ci#define REG_COM8	0x13	/* Control 8 */
1158c2ecf20Sopenharmony_ci#define   COM8_FASTAEC	  0x80	  /* Enable fast AGC/AEC */
1168c2ecf20Sopenharmony_ci#define   COM8_AECSTEP	  0x40	  /* Unlimited AEC step size */
1178c2ecf20Sopenharmony_ci#define   COM8_BFILT	  0x20	  /* Band filter enable */
1188c2ecf20Sopenharmony_ci#define   COM8_AGC	  0x04	  /* Auto gain enable */
1198c2ecf20Sopenharmony_ci#define   COM8_AWB	  0x02	  /* White balance enable */
1208c2ecf20Sopenharmony_ci#define   COM8_AEC	  0x01	  /* Auto exposure enable */
1218c2ecf20Sopenharmony_ci#define REG_COM9	0x14	/* Control 9  - gain ceiling */
1228c2ecf20Sopenharmony_ci#define REG_COM10	0x15	/* Control 10 */
1238c2ecf20Sopenharmony_ci#define   COM10_HSYNC	  0x40	  /* HSYNC instead of HREF */
1248c2ecf20Sopenharmony_ci#define   COM10_PCLK_HB	  0x20	  /* Suppress PCLK on horiz blank */
1258c2ecf20Sopenharmony_ci#define   COM10_HREF_REV  0x08	  /* Reverse HREF */
1268c2ecf20Sopenharmony_ci#define   COM10_VS_LEAD	  0x04	  /* VSYNC on clock leading edge */
1278c2ecf20Sopenharmony_ci#define   COM10_VS_NEG	  0x02	  /* VSYNC negative */
1288c2ecf20Sopenharmony_ci#define   COM10_HS_NEG	  0x01	  /* HSYNC negative */
1298c2ecf20Sopenharmony_ci#define REG_HSTART	0x17	/* Horiz start high bits */
1308c2ecf20Sopenharmony_ci#define REG_HSTOP	0x18	/* Horiz stop high bits */
1318c2ecf20Sopenharmony_ci#define REG_VSTART	0x19	/* Vert start high bits */
1328c2ecf20Sopenharmony_ci#define REG_VSTOP	0x1a	/* Vert stop high bits */
1338c2ecf20Sopenharmony_ci#define REG_PSHFT	0x1b	/* Pixel delay after HREF */
1348c2ecf20Sopenharmony_ci#define REG_MIDH	0x1c	/* Manuf. ID high */
1358c2ecf20Sopenharmony_ci#define REG_MIDL	0x1d	/* Manuf. ID low */
1368c2ecf20Sopenharmony_ci#define REG_MVFP	0x1e	/* Mirror / vflip */
1378c2ecf20Sopenharmony_ci#define   MVFP_MIRROR	  0x20	  /* Mirror image */
1388c2ecf20Sopenharmony_ci#define   MVFP_FLIP	  0x10	  /* Vertical flip */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define REG_AEW		0x24	/* AGC upper limit */
1418c2ecf20Sopenharmony_ci#define REG_AEB		0x25	/* AGC lower limit */
1428c2ecf20Sopenharmony_ci#define REG_VPT		0x26	/* AGC/AEC fast mode op region */
1438c2ecf20Sopenharmony_ci#define REG_ADVFL	0x2d	/* Insert dummy lines (LSB) */
1448c2ecf20Sopenharmony_ci#define REG_ADVFH	0x2e	/* Insert dummy lines (MSB) */
1458c2ecf20Sopenharmony_ci#define REG_HSYST	0x30	/* HSYNC rising edge delay */
1468c2ecf20Sopenharmony_ci#define REG_HSYEN	0x31	/* HSYNC falling edge delay */
1478c2ecf20Sopenharmony_ci#define REG_HREF	0x32	/* HREF pieces */
1488c2ecf20Sopenharmony_ci#define REG_TSLB	0x3a	/* lots of stuff */
1498c2ecf20Sopenharmony_ci#define   TSLB_YLAST	  0x04	  /* UYVY or VYUY - see com13 */
1508c2ecf20Sopenharmony_ci#define   TSLB_BYTEORD	  0x08	  /* swap bytes in 16bit mode? */
1518c2ecf20Sopenharmony_ci#define REG_COM11	0x3b	/* Control 11 */
1528c2ecf20Sopenharmony_ci#define   COM11_NIGHT	  0x80	  /* NIght mode enable */
1538c2ecf20Sopenharmony_ci#define   COM11_NMFR	  0x60	  /* Two bit NM frame rate */
1548c2ecf20Sopenharmony_ci#define   COM11_HZAUTO	  0x10	  /* Auto detect 50/60 Hz */
1558c2ecf20Sopenharmony_ci#define	  COM11_50HZ	  0x08	  /* Manual 50Hz select */
1568c2ecf20Sopenharmony_ci#define   COM11_EXP	  0x02
1578c2ecf20Sopenharmony_ci#define REG_COM12	0x3c	/* Control 12 */
1588c2ecf20Sopenharmony_ci#define   COM12_HREF	  0x80	  /* HREF always */
1598c2ecf20Sopenharmony_ci#define REG_COM13	0x3d	/* Control 13 */
1608c2ecf20Sopenharmony_ci#define   COM13_GAMMA	  0x80	  /* Gamma enable */
1618c2ecf20Sopenharmony_ci#define	  COM13_UVSAT	  0x40	  /* UV saturation auto adjustment */
1628c2ecf20Sopenharmony_ci#define	  COM13_CMATRIX	  0x10	  /* Enable color matrix for RGB or YUV */
1638c2ecf20Sopenharmony_ci#define   COM13_UVSWAP	  0x01	  /* V before U - w/TSLB */
1648c2ecf20Sopenharmony_ci#define REG_COM14	0x3e	/* Control 14 */
1658c2ecf20Sopenharmony_ci#define   COM14_DCWEN	  0x10	  /* DCW/PCLK-scale enable */
1668c2ecf20Sopenharmony_ci#define REG_EDGE	0x3f	/* Edge enhancement factor */
1678c2ecf20Sopenharmony_ci#define REG_COM15	0x40	/* Control 15 */
1688c2ecf20Sopenharmony_ci#define   COM15_R10F0	  0x00	  /* Data range 10 to F0 */
1698c2ecf20Sopenharmony_ci#define	  COM15_R01FE	  0x80	  /*            01 to FE */
1708c2ecf20Sopenharmony_ci#define   COM15_R00FF	  0xc0	  /*            00 to FF */
1718c2ecf20Sopenharmony_ci#define   COM15_RGB565	  0x10	  /* RGB565 output */
1728c2ecf20Sopenharmony_ci#define   COM15_RGBFIXME	  0x20	  /* FIXME  */
1738c2ecf20Sopenharmony_ci#define   COM15_RGB555	  0x30	  /* RGB555 output */
1748c2ecf20Sopenharmony_ci#define REG_COM16	0x41	/* Control 16 */
1758c2ecf20Sopenharmony_ci#define   COM16_AWBGAIN   0x08	  /* AWB gain enable */
1768c2ecf20Sopenharmony_ci#define REG_COM17	0x42	/* Control 17 */
1778c2ecf20Sopenharmony_ci#define   COM17_AECWIN	  0xc0	  /* AEC window - must match COM4 */
1788c2ecf20Sopenharmony_ci#define   COM17_CBAR	  0x08	  /* DSP Color bar */
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/*
1818c2ecf20Sopenharmony_ci * This matrix defines how the colors are generated, must be
1828c2ecf20Sopenharmony_ci * tweaked to adjust hue and saturation.
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci * They are nine-bit signed quantities, with the sign bit
1878c2ecf20Sopenharmony_ci * stored in 0x58.  Sign for v-red is bit 0, and up from there.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_ci#define	REG_CMATRIX_BASE 0x4f
1908c2ecf20Sopenharmony_ci#define   CMATRIX_LEN 6
1918c2ecf20Sopenharmony_ci#define REG_CMATRIX_SIGN 0x58
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#define REG_BRIGHT	0x55	/* Brightness */
1958c2ecf20Sopenharmony_ci#define REG_CONTRAS	0x56	/* Contrast control */
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci#define REG_GFIX	0x69	/* Fix gain control */
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define REG_RGB444	0x8c	/* RGB 444 control */
2008c2ecf20Sopenharmony_ci#define   R444_ENABLE	  0x02	  /* Turn on RGB444, overrides 5x5 */
2018c2ecf20Sopenharmony_ci#define   R444_RGBX	  0x01	  /* Empty nibble at end */
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define REG_HAECC1	0x9f	/* Hist AEC/AGC control 1 */
2048c2ecf20Sopenharmony_ci#define REG_HAECC2	0xa0	/* Hist AEC/AGC control 2 */
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#define REG_BD50MAX	0xa5	/* 50hz banding step limit */
2078c2ecf20Sopenharmony_ci#define REG_HAECC3	0xa6	/* Hist AEC/AGC control 3 */
2088c2ecf20Sopenharmony_ci#define REG_HAECC4	0xa7	/* Hist AEC/AGC control 4 */
2098c2ecf20Sopenharmony_ci#define REG_HAECC5	0xa8	/* Hist AEC/AGC control 5 */
2108c2ecf20Sopenharmony_ci#define REG_HAECC6	0xa9	/* Hist AEC/AGC control 6 */
2118c2ecf20Sopenharmony_ci#define REG_HAECC7	0xaa	/* Hist AEC/AGC control 7 */
2128c2ecf20Sopenharmony_ci#define REG_BD60MAX	0xab	/* 60hz banding step limit */
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/* Returns 0 if OK */
2188c2ecf20Sopenharmony_cistatic int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	int i = 0;
2218c2ecf20Sopenharmony_ci	u8 tmpval = 0;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_TX_INDEX, reg))
2248c2ecf20Sopenharmony_ci		return 1;
2258c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_TX_VALUE, val))
2268c2ecf20Sopenharmony_ci		return 1;
2278c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_TX))
2288c2ecf20Sopenharmony_ci		return 1;
2298c2ecf20Sopenharmony_ci	do {
2308c2ecf20Sopenharmony_ci		if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
2318c2ecf20Sopenharmony_ci			return 1;
2328c2ecf20Sopenharmony_ci		i++;
2338c2ecf20Sopenharmony_ci	} while (tmpval == 0 && i < MAX_RETRIES);
2348c2ecf20Sopenharmony_ci	if (tmpval != STK_IIC_STAT_TX_OK) {
2358c2ecf20Sopenharmony_ci		if (tmpval)
2368c2ecf20Sopenharmony_ci			pr_err("stk_sensor_outb failed, status=0x%02x\n",
2378c2ecf20Sopenharmony_ci			       tmpval);
2388c2ecf20Sopenharmony_ci		return 1;
2398c2ecf20Sopenharmony_ci	} else
2408c2ecf20Sopenharmony_ci		return 0;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	int i = 0;
2468c2ecf20Sopenharmony_ci	u8 tmpval = 0;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_RX_INDEX, reg))
2498c2ecf20Sopenharmony_ci		return 1;
2508c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_RX))
2518c2ecf20Sopenharmony_ci		return 1;
2528c2ecf20Sopenharmony_ci	do {
2538c2ecf20Sopenharmony_ci		if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
2548c2ecf20Sopenharmony_ci			return 1;
2558c2ecf20Sopenharmony_ci		i++;
2568c2ecf20Sopenharmony_ci	} while (tmpval == 0 && i < MAX_RETRIES);
2578c2ecf20Sopenharmony_ci	if (tmpval != STK_IIC_STAT_RX_OK) {
2588c2ecf20Sopenharmony_ci		if (tmpval)
2598c2ecf20Sopenharmony_ci			pr_err("stk_sensor_inb failed, status=0x%02x\n",
2608c2ecf20Sopenharmony_ci			       tmpval);
2618c2ecf20Sopenharmony_ci		return 1;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	if (stk_camera_read_reg(dev, STK_IIC_RX_VALUE, &tmpval))
2658c2ecf20Sopenharmony_ci		return 1;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	*val = tmpval;
2688c2ecf20Sopenharmony_ci	return 0;
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic int stk_sensor_write_regvals(struct stk_camera *dev,
2728c2ecf20Sopenharmony_ci		struct regval *rv)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	int ret;
2758c2ecf20Sopenharmony_ci	if (rv == NULL)
2768c2ecf20Sopenharmony_ci		return 0;
2778c2ecf20Sopenharmony_ci	while (rv->reg != 0xff || rv->val != 0xff) {
2788c2ecf20Sopenharmony_ci		ret = stk_sensor_outb(dev, rv->reg, rv->val);
2798c2ecf20Sopenharmony_ci		if (ret != 0)
2808c2ecf20Sopenharmony_ci			return ret;
2818c2ecf20Sopenharmony_ci		rv++;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci	return 0;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ciint stk_sensor_sleep(struct stk_camera *dev)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	u8 tmp;
2898c2ecf20Sopenharmony_ci	return stk_sensor_inb(dev, REG_COM2, &tmp)
2908c2ecf20Sopenharmony_ci		|| stk_sensor_outb(dev, REG_COM2, tmp|COM2_SSLEEP);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciint stk_sensor_wakeup(struct stk_camera *dev)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	u8 tmp;
2968c2ecf20Sopenharmony_ci	return stk_sensor_inb(dev, REG_COM2, &tmp)
2978c2ecf20Sopenharmony_ci		|| stk_sensor_outb(dev, REG_COM2, tmp&~COM2_SSLEEP);
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic struct regval ov_initvals[] = {
3018c2ecf20Sopenharmony_ci	{REG_CLKRC, CLK_PLL},
3028c2ecf20Sopenharmony_ci	{REG_COM11, 0x01},
3038c2ecf20Sopenharmony_ci	{0x6a, 0x7d},
3048c2ecf20Sopenharmony_ci	{REG_AECH, 0x40},
3058c2ecf20Sopenharmony_ci	{REG_GAIN, 0x00},
3068c2ecf20Sopenharmony_ci	{REG_BLUE, 0x80},
3078c2ecf20Sopenharmony_ci	{REG_RED, 0x80},
3088c2ecf20Sopenharmony_ci	/* Do not enable fast AEC for now */
3098c2ecf20Sopenharmony_ci	/*{REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},*/
3108c2ecf20Sopenharmony_ci	{REG_COM8, COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},
3118c2ecf20Sopenharmony_ci	{0x39, 0x50}, {0x38, 0x93},
3128c2ecf20Sopenharmony_ci	{0x37, 0x00}, {0x35, 0x81},
3138c2ecf20Sopenharmony_ci	{REG_COM5, 0x20},
3148c2ecf20Sopenharmony_ci	{REG_COM1, 0x00},
3158c2ecf20Sopenharmony_ci	{REG_COM3, 0x00},
3168c2ecf20Sopenharmony_ci	{REG_COM4, 0x00},
3178c2ecf20Sopenharmony_ci	{REG_PSHFT, 0x00},
3188c2ecf20Sopenharmony_ci	{0x16, 0x07},
3198c2ecf20Sopenharmony_ci	{0x33, 0xe2}, {0x34, 0xbf},
3208c2ecf20Sopenharmony_ci	{REG_COM16, 0x00},
3218c2ecf20Sopenharmony_ci	{0x96, 0x04},
3228c2ecf20Sopenharmony_ci	/* Gamma curve values */
3238c2ecf20Sopenharmony_ci/*	{ 0x7a, 0x20 },		{ 0x7b, 0x10 },
3248c2ecf20Sopenharmony_ci	{ 0x7c, 0x1e },		{ 0x7d, 0x35 },
3258c2ecf20Sopenharmony_ci	{ 0x7e, 0x5a },		{ 0x7f, 0x69 },
3268c2ecf20Sopenharmony_ci	{ 0x80, 0x76 },		{ 0x81, 0x80 },
3278c2ecf20Sopenharmony_ci	{ 0x82, 0x88 },		{ 0x83, 0x8f },
3288c2ecf20Sopenharmony_ci	{ 0x84, 0x96 },		{ 0x85, 0xa3 },
3298c2ecf20Sopenharmony_ci	{ 0x86, 0xaf },		{ 0x87, 0xc4 },
3308c2ecf20Sopenharmony_ci	{ 0x88, 0xd7 },		{ 0x89, 0xe8 },
3318c2ecf20Sopenharmony_ci*/
3328c2ecf20Sopenharmony_ci	{REG_GFIX, 0x40},
3338c2ecf20Sopenharmony_ci	{0x8e, 0x00},
3348c2ecf20Sopenharmony_ci	{REG_COM12, 0x73},
3358c2ecf20Sopenharmony_ci	{0x8f, 0xdf}, {0x8b, 0x06},
3368c2ecf20Sopenharmony_ci	{0x8c, 0x20},
3378c2ecf20Sopenharmony_ci	{0x94, 0x88}, {0x95, 0x88},
3388c2ecf20Sopenharmony_ci/*	{REG_COM15, 0xc1}, TODO */
3398c2ecf20Sopenharmony_ci	{0x29, 0x3f},
3408c2ecf20Sopenharmony_ci	{REG_COM6, 0x42},
3418c2ecf20Sopenharmony_ci	{REG_BD50MAX, 0x80},
3428c2ecf20Sopenharmony_ci	{REG_HAECC6, 0xb8}, {REG_HAECC7, 0x92},
3438c2ecf20Sopenharmony_ci	{REG_BD60MAX, 0x0a},
3448c2ecf20Sopenharmony_ci	{0x90, 0x00}, {0x91, 0x00},
3458c2ecf20Sopenharmony_ci	{REG_HAECC1, 0x00}, {REG_HAECC2, 0x00},
3468c2ecf20Sopenharmony_ci	{REG_AEW, 0x68}, {REG_AEB, 0x5c},
3478c2ecf20Sopenharmony_ci	{REG_VPT, 0xc3},
3488c2ecf20Sopenharmony_ci	{REG_COM9, 0x2e},
3498c2ecf20Sopenharmony_ci	{0x2a, 0x00}, {0x2b, 0x00},
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	{0xff, 0xff}, /* END MARKER */
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci/* Probe the I2C bus and initialise the sensor chip */
3558c2ecf20Sopenharmony_ciint stk_sensor_init(struct stk_camera *dev)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	u8 idl = 0;
3588c2ecf20Sopenharmony_ci	u8 idh = 0;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (stk_camera_write_reg(dev, STK_IIC_ENABLE, STK_IIC_ENABLE_YES)
3618c2ecf20Sopenharmony_ci		|| stk_camera_write_reg(dev, STK_IIC_ADDR, SENSOR_ADDRESS)
3628c2ecf20Sopenharmony_ci		|| stk_sensor_outb(dev, REG_COM7, COM7_RESET)) {
3638c2ecf20Sopenharmony_ci		pr_err("Sensor resetting failed\n");
3648c2ecf20Sopenharmony_ci		return -ENODEV;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci	msleep(10);
3678c2ecf20Sopenharmony_ci	/* Read the manufacturer ID: ov = 0x7FA2 */
3688c2ecf20Sopenharmony_ci	if (stk_sensor_inb(dev, REG_MIDH, &idh)
3698c2ecf20Sopenharmony_ci	    || stk_sensor_inb(dev, REG_MIDL, &idl)) {
3708c2ecf20Sopenharmony_ci		pr_err("Strange error reading sensor ID\n");
3718c2ecf20Sopenharmony_ci		return -ENODEV;
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci	if (idh != 0x7f || idl != 0xa2) {
3748c2ecf20Sopenharmony_ci		pr_err("Huh? you don't have a sensor from ovt\n");
3758c2ecf20Sopenharmony_ci		return -ENODEV;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci	if (stk_sensor_inb(dev, REG_PID, &idh)
3788c2ecf20Sopenharmony_ci	    || stk_sensor_inb(dev, REG_VER, &idl)) {
3798c2ecf20Sopenharmony_ci		pr_err("Could not read sensor model\n");
3808c2ecf20Sopenharmony_ci		return -ENODEV;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci	stk_sensor_write_regvals(dev, ov_initvals);
3838c2ecf20Sopenharmony_ci	msleep(10);
3848c2ecf20Sopenharmony_ci	pr_info("OmniVision sensor detected, id %02X%02X at address %x\n",
3858c2ecf20Sopenharmony_ci		idh, idl, SENSOR_ADDRESS);
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci/* V4L2_PIX_FMT_UYVY */
3908c2ecf20Sopenharmony_cistatic struct regval ov_fmt_uyvy[] = {
3918c2ecf20Sopenharmony_ci	{REG_TSLB, TSLB_YLAST|0x08 },
3928c2ecf20Sopenharmony_ci	{ 0x4f, 0x80 },		/* "matrix coefficient 1" */
3938c2ecf20Sopenharmony_ci	{ 0x50, 0x80 },		/* "matrix coefficient 2" */
3948c2ecf20Sopenharmony_ci	{ 0x51, 0    },		/* vb */
3958c2ecf20Sopenharmony_ci	{ 0x52, 0x22 },		/* "matrix coefficient 4" */
3968c2ecf20Sopenharmony_ci	{ 0x53, 0x5e },		/* "matrix coefficient 5" */
3978c2ecf20Sopenharmony_ci	{ 0x54, 0x80 },		/* "matrix coefficient 6" */
3988c2ecf20Sopenharmony_ci	{REG_COM13, COM13_UVSAT|COM13_CMATRIX},
3998c2ecf20Sopenharmony_ci	{REG_COM15, COM15_R00FF },
4008c2ecf20Sopenharmony_ci	{0xff, 0xff}, /* END MARKER */
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ci/* V4L2_PIX_FMT_YUYV */
4038c2ecf20Sopenharmony_cistatic struct regval ov_fmt_yuyv[] = {
4048c2ecf20Sopenharmony_ci	{REG_TSLB, 0 },
4058c2ecf20Sopenharmony_ci	{ 0x4f, 0x80 },		/* "matrix coefficient 1" */
4068c2ecf20Sopenharmony_ci	{ 0x50, 0x80 },		/* "matrix coefficient 2" */
4078c2ecf20Sopenharmony_ci	{ 0x51, 0    },		/* vb */
4088c2ecf20Sopenharmony_ci	{ 0x52, 0x22 },		/* "matrix coefficient 4" */
4098c2ecf20Sopenharmony_ci	{ 0x53, 0x5e },		/* "matrix coefficient 5" */
4108c2ecf20Sopenharmony_ci	{ 0x54, 0x80 },		/* "matrix coefficient 6" */
4118c2ecf20Sopenharmony_ci	{REG_COM13, COM13_UVSAT|COM13_CMATRIX},
4128c2ecf20Sopenharmony_ci	{REG_COM15, COM15_R00FF },
4138c2ecf20Sopenharmony_ci	{0xff, 0xff}, /* END MARKER */
4148c2ecf20Sopenharmony_ci};
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci/* V4L2_PIX_FMT_RGB565X rrrrrggg gggbbbbb */
4178c2ecf20Sopenharmony_cistatic struct regval ov_fmt_rgbr[] = {
4188c2ecf20Sopenharmony_ci	{ REG_RGB444, 0 },	/* No RGB444 please */
4198c2ecf20Sopenharmony_ci	{REG_TSLB, 0x00},
4208c2ecf20Sopenharmony_ci	{ REG_COM1, 0x0 },
4218c2ecf20Sopenharmony_ci	{ REG_COM9, 0x38 },	/* 16x gain ceiling; 0x8 is reserved bit */
4228c2ecf20Sopenharmony_ci	{ 0x4f, 0xb3 },		/* "matrix coefficient 1" */
4238c2ecf20Sopenharmony_ci	{ 0x50, 0xb3 },		/* "matrix coefficient 2" */
4248c2ecf20Sopenharmony_ci	{ 0x51, 0    },		/* vb */
4258c2ecf20Sopenharmony_ci	{ 0x52, 0x3d },		/* "matrix coefficient 4" */
4268c2ecf20Sopenharmony_ci	{ 0x53, 0xa7 },		/* "matrix coefficient 5" */
4278c2ecf20Sopenharmony_ci	{ 0x54, 0xe4 },		/* "matrix coefficient 6" */
4288c2ecf20Sopenharmony_ci	{ REG_COM13, COM13_GAMMA },
4298c2ecf20Sopenharmony_ci	{ REG_COM15, COM15_RGB565|COM15_R00FF },
4308c2ecf20Sopenharmony_ci	{ 0xff, 0xff },
4318c2ecf20Sopenharmony_ci};
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/* V4L2_PIX_FMT_RGB565 gggbbbbb rrrrrggg */
4348c2ecf20Sopenharmony_cistatic struct regval ov_fmt_rgbp[] = {
4358c2ecf20Sopenharmony_ci	{ REG_RGB444, 0 },	/* No RGB444 please */
4368c2ecf20Sopenharmony_ci	{REG_TSLB, TSLB_BYTEORD },
4378c2ecf20Sopenharmony_ci	{ REG_COM1, 0x0 },
4388c2ecf20Sopenharmony_ci	{ REG_COM9, 0x38 },	/* 16x gain ceiling; 0x8 is reserved bit */
4398c2ecf20Sopenharmony_ci	{ 0x4f, 0xb3 },		/* "matrix coefficient 1" */
4408c2ecf20Sopenharmony_ci	{ 0x50, 0xb3 },		/* "matrix coefficient 2" */
4418c2ecf20Sopenharmony_ci	{ 0x51, 0    },		/* vb */
4428c2ecf20Sopenharmony_ci	{ 0x52, 0x3d },		/* "matrix coefficient 4" */
4438c2ecf20Sopenharmony_ci	{ 0x53, 0xa7 },		/* "matrix coefficient 5" */
4448c2ecf20Sopenharmony_ci	{ 0x54, 0xe4 },		/* "matrix coefficient 6" */
4458c2ecf20Sopenharmony_ci	{ REG_COM13, COM13_GAMMA },
4468c2ecf20Sopenharmony_ci	{ REG_COM15, COM15_RGB565|COM15_R00FF },
4478c2ecf20Sopenharmony_ci	{ 0xff, 0xff },
4488c2ecf20Sopenharmony_ci};
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci/* V4L2_PIX_FMT_SRGGB8 */
4518c2ecf20Sopenharmony_cistatic struct regval ov_fmt_bayer[] = {
4528c2ecf20Sopenharmony_ci	/* This changes color order */
4538c2ecf20Sopenharmony_ci	{REG_TSLB, 0x40}, /* BGGR */
4548c2ecf20Sopenharmony_ci	/* {REG_TSLB, 0x08}, */ /* BGGR with vertical image flipping */
4558c2ecf20Sopenharmony_ci	{REG_COM15, COM15_R00FF },
4568c2ecf20Sopenharmony_ci	{0xff, 0xff}, /* END MARKER */
4578c2ecf20Sopenharmony_ci};
4588c2ecf20Sopenharmony_ci/*
4598c2ecf20Sopenharmony_ci * Store a set of start/stop values into the camera.
4608c2ecf20Sopenharmony_ci */
4618c2ecf20Sopenharmony_cistatic int stk_sensor_set_hw(struct stk_camera *dev,
4628c2ecf20Sopenharmony_ci		int hstart, int hstop, int vstart, int vstop)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	int ret;
4658c2ecf20Sopenharmony_ci	unsigned char v;
4668c2ecf20Sopenharmony_ci/*
4678c2ecf20Sopenharmony_ci * Horizontal: 11 bits, top 8 live in hstart and hstop.  Bottom 3 of
4688c2ecf20Sopenharmony_ci * hstart are in href[2:0], bottom 3 of hstop in href[5:3].  There is
4698c2ecf20Sopenharmony_ci * a mystery "edge offset" value in the top two bits of href.
4708c2ecf20Sopenharmony_ci */
4718c2ecf20Sopenharmony_ci	ret =  stk_sensor_outb(dev, REG_HSTART, (hstart >> 3) & 0xff);
4728c2ecf20Sopenharmony_ci	ret += stk_sensor_outb(dev, REG_HSTOP, (hstop >> 3) & 0xff);
4738c2ecf20Sopenharmony_ci	ret += stk_sensor_inb(dev, REG_HREF, &v);
4748c2ecf20Sopenharmony_ci	v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
4758c2ecf20Sopenharmony_ci	msleep(10);
4768c2ecf20Sopenharmony_ci	ret += stk_sensor_outb(dev, REG_HREF, v);
4778c2ecf20Sopenharmony_ci/*
4788c2ecf20Sopenharmony_ci * Vertical: similar arrangement (note: this is different from ov7670.c)
4798c2ecf20Sopenharmony_ci */
4808c2ecf20Sopenharmony_ci	ret += stk_sensor_outb(dev, REG_VSTART, (vstart >> 3) & 0xff);
4818c2ecf20Sopenharmony_ci	ret += stk_sensor_outb(dev, REG_VSTOP, (vstop >> 3) & 0xff);
4828c2ecf20Sopenharmony_ci	ret += stk_sensor_inb(dev, REG_VREF, &v);
4838c2ecf20Sopenharmony_ci	v = (v & 0xc0) | ((vstop & 0x7) << 3) | (vstart & 0x7);
4848c2ecf20Sopenharmony_ci	msleep(10);
4858c2ecf20Sopenharmony_ci	ret += stk_sensor_outb(dev, REG_VREF, v);
4868c2ecf20Sopenharmony_ci	return ret;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ciint stk_sensor_configure(struct stk_camera *dev)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	int com7;
4938c2ecf20Sopenharmony_ci	/*
4948c2ecf20Sopenharmony_ci	 * We setup the sensor to output dummy lines in low-res modes,
4958c2ecf20Sopenharmony_ci	 * so we don't get absurdly hight framerates.
4968c2ecf20Sopenharmony_ci	 */
4978c2ecf20Sopenharmony_ci	unsigned dummylines;
4988c2ecf20Sopenharmony_ci	int flip;
4998c2ecf20Sopenharmony_ci	struct regval *rv;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	switch (dev->vsettings.mode) {
5028c2ecf20Sopenharmony_ci	case MODE_QCIF: com7 = COM7_FMT_QCIF;
5038c2ecf20Sopenharmony_ci		dummylines = 604;
5048c2ecf20Sopenharmony_ci		break;
5058c2ecf20Sopenharmony_ci	case MODE_QVGA: com7 = COM7_FMT_QVGA;
5068c2ecf20Sopenharmony_ci		dummylines = 267;
5078c2ecf20Sopenharmony_ci		break;
5088c2ecf20Sopenharmony_ci	case MODE_CIF: com7 = COM7_FMT_CIF;
5098c2ecf20Sopenharmony_ci		dummylines = 412;
5108c2ecf20Sopenharmony_ci		break;
5118c2ecf20Sopenharmony_ci	case MODE_VGA: com7 = COM7_FMT_VGA;
5128c2ecf20Sopenharmony_ci		dummylines = 11;
5138c2ecf20Sopenharmony_ci		break;
5148c2ecf20Sopenharmony_ci	case MODE_SXGA: com7 = COM7_FMT_SXGA;
5158c2ecf20Sopenharmony_ci		dummylines = 0;
5168c2ecf20Sopenharmony_ci		break;
5178c2ecf20Sopenharmony_ci	default:
5188c2ecf20Sopenharmony_ci		pr_err("Unsupported mode %d\n", dev->vsettings.mode);
5198c2ecf20Sopenharmony_ci		return -EFAULT;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	switch (dev->vsettings.palette) {
5228c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_UYVY:
5238c2ecf20Sopenharmony_ci		com7 |= COM7_YUV;
5248c2ecf20Sopenharmony_ci		rv = ov_fmt_uyvy;
5258c2ecf20Sopenharmony_ci		break;
5268c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_YUYV:
5278c2ecf20Sopenharmony_ci		com7 |= COM7_YUV;
5288c2ecf20Sopenharmony_ci		rv = ov_fmt_yuyv;
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565:
5318c2ecf20Sopenharmony_ci		com7 |= COM7_RGB;
5328c2ecf20Sopenharmony_ci		rv = ov_fmt_rgbp;
5338c2ecf20Sopenharmony_ci		break;
5348c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_RGB565X:
5358c2ecf20Sopenharmony_ci		com7 |= COM7_RGB;
5368c2ecf20Sopenharmony_ci		rv = ov_fmt_rgbr;
5378c2ecf20Sopenharmony_ci		break;
5388c2ecf20Sopenharmony_ci	case V4L2_PIX_FMT_SBGGR8:
5398c2ecf20Sopenharmony_ci		com7 |= COM7_PBAYER;
5408c2ecf20Sopenharmony_ci		rv = ov_fmt_bayer;
5418c2ecf20Sopenharmony_ci		break;
5428c2ecf20Sopenharmony_ci	default:
5438c2ecf20Sopenharmony_ci		pr_err("Unsupported colorspace\n");
5448c2ecf20Sopenharmony_ci		return -EFAULT;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci	/*FIXME sometimes the sensor go to a bad state
5478c2ecf20Sopenharmony_ci	stk_sensor_write_regvals(dev, ov_initvals); */
5488c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_COM7, com7);
5498c2ecf20Sopenharmony_ci	msleep(50);
5508c2ecf20Sopenharmony_ci	stk_sensor_write_regvals(dev, rv);
5518c2ecf20Sopenharmony_ci	flip = (dev->vsettings.vflip?MVFP_FLIP:0)
5528c2ecf20Sopenharmony_ci		| (dev->vsettings.hflip?MVFP_MIRROR:0);
5538c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_MVFP, flip);
5548c2ecf20Sopenharmony_ci	if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8
5558c2ecf20Sopenharmony_ci			&& !dev->vsettings.vflip)
5568c2ecf20Sopenharmony_ci		stk_sensor_outb(dev, REG_TSLB, 0x08);
5578c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_ADVFH, dummylines >> 8);
5588c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_ADVFL, dummylines & 0xff);
5598c2ecf20Sopenharmony_ci	msleep(50);
5608c2ecf20Sopenharmony_ci	switch (dev->vsettings.mode) {
5618c2ecf20Sopenharmony_ci	case MODE_VGA:
5628c2ecf20Sopenharmony_ci		if (stk_sensor_set_hw(dev, 302, 1582, 6, 486))
5638c2ecf20Sopenharmony_ci			pr_err("stk_sensor_set_hw failed (VGA)\n");
5648c2ecf20Sopenharmony_ci		break;
5658c2ecf20Sopenharmony_ci	case MODE_SXGA:
5668c2ecf20Sopenharmony_ci	case MODE_CIF:
5678c2ecf20Sopenharmony_ci	case MODE_QVGA:
5688c2ecf20Sopenharmony_ci	case MODE_QCIF:
5698c2ecf20Sopenharmony_ci		/*FIXME These settings seem ignored by the sensor
5708c2ecf20Sopenharmony_ci		if (stk_sensor_set_hw(dev, 220, 1500, 10, 1034))
5718c2ecf20Sopenharmony_ci			pr_err("stk_sensor_set_hw failed (SXGA)\n");
5728c2ecf20Sopenharmony_ci		*/
5738c2ecf20Sopenharmony_ci		break;
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci	msleep(10);
5768c2ecf20Sopenharmony_ci	return 0;
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ciint stk_sensor_set_brightness(struct stk_camera *dev, int br)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	if (br < 0 || br > 0xff)
5828c2ecf20Sopenharmony_ci		return -EINVAL;
5838c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_AEB, max(0x00, br - 6));
5848c2ecf20Sopenharmony_ci	stk_sensor_outb(dev, REG_AEW, min(0xff, br + 6));
5858c2ecf20Sopenharmony_ci	return 0;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
588