18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * V4L2 subdevice driver for OmniVision OV6650 Camera Sensor
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on OmniVision OV96xx Camera Driver
88c2ecf20Sopenharmony_ci * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Based on ov772x camera driver:
118c2ecf20Sopenharmony_ci * Copyright (C) 2008 Renesas Solutions Corp.
128c2ecf20Sopenharmony_ci * Kuninori Morimoto <morimoto.kuninori@renesas.com>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Based on ov7670 and soc_camera_platform driver,
158c2ecf20Sopenharmony_ci * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
168c2ecf20Sopenharmony_ci * Copyright (C) 2008 Magnus Damm
178c2ecf20Sopenharmony_ci * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Hardware specific bits initially based on former work by Matt Callow
208c2ecf20Sopenharmony_ci * drivers/media/video/omap/sensor_ov6650.c
218c2ecf20Sopenharmony_ci * Copyright (C) 2006 Matt Callow
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/bitops.h>
258c2ecf20Sopenharmony_ci#include <linux/delay.h>
268c2ecf20Sopenharmony_ci#include <linux/i2c.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/v4l2-mediabus.h>
298c2ecf20Sopenharmony_ci#include <linux/module.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <media/v4l2-clk.h>
328c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
338c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Register definitions */
368c2ecf20Sopenharmony_ci#define REG_GAIN		0x00	/* range 00 - 3F */
378c2ecf20Sopenharmony_ci#define REG_BLUE		0x01
388c2ecf20Sopenharmony_ci#define REG_RED			0x02
398c2ecf20Sopenharmony_ci#define REG_SAT			0x03	/* [7:4] saturation [0:3] reserved */
408c2ecf20Sopenharmony_ci#define REG_HUE			0x04	/* [7:6] rsrvd [5] hue en [4:0] hue */
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define REG_BRT			0x06
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define REG_PIDH		0x0a
458c2ecf20Sopenharmony_ci#define REG_PIDL		0x0b
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define REG_AECH		0x10
488c2ecf20Sopenharmony_ci#define REG_CLKRC		0x11	/* Data Format and Internal Clock */
498c2ecf20Sopenharmony_ci					/* [7:6] Input system clock (MHz)*/
508c2ecf20Sopenharmony_ci					/*   00=8, 01=12, 10=16, 11=24 */
518c2ecf20Sopenharmony_ci					/* [5:0]: Internal Clock Pre-Scaler */
528c2ecf20Sopenharmony_ci#define REG_COMA		0x12	/* [7] Reset */
538c2ecf20Sopenharmony_ci#define REG_COMB		0x13
548c2ecf20Sopenharmony_ci#define REG_COMC		0x14
558c2ecf20Sopenharmony_ci#define REG_COMD		0x15
568c2ecf20Sopenharmony_ci#define REG_COML		0x16
578c2ecf20Sopenharmony_ci#define REG_HSTRT		0x17
588c2ecf20Sopenharmony_ci#define REG_HSTOP		0x18
598c2ecf20Sopenharmony_ci#define REG_VSTRT		0x19
608c2ecf20Sopenharmony_ci#define REG_VSTOP		0x1a
618c2ecf20Sopenharmony_ci#define REG_PSHFT		0x1b
628c2ecf20Sopenharmony_ci#define REG_MIDH		0x1c
638c2ecf20Sopenharmony_ci#define REG_MIDL		0x1d
648c2ecf20Sopenharmony_ci#define REG_HSYNS		0x1e
658c2ecf20Sopenharmony_ci#define REG_HSYNE		0x1f
668c2ecf20Sopenharmony_ci#define REG_COME		0x20
678c2ecf20Sopenharmony_ci#define REG_YOFF		0x21
688c2ecf20Sopenharmony_ci#define REG_UOFF		0x22
698c2ecf20Sopenharmony_ci#define REG_VOFF		0x23
708c2ecf20Sopenharmony_ci#define REG_AEW			0x24
718c2ecf20Sopenharmony_ci#define REG_AEB			0x25
728c2ecf20Sopenharmony_ci#define REG_COMF		0x26
738c2ecf20Sopenharmony_ci#define REG_COMG		0x27
748c2ecf20Sopenharmony_ci#define REG_COMH		0x28
758c2ecf20Sopenharmony_ci#define REG_COMI		0x29
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define REG_FRARL		0x2b
788c2ecf20Sopenharmony_ci#define REG_COMJ		0x2c
798c2ecf20Sopenharmony_ci#define REG_COMK		0x2d
808c2ecf20Sopenharmony_ci#define REG_AVGY		0x2e
818c2ecf20Sopenharmony_ci#define REG_REF0		0x2f
828c2ecf20Sopenharmony_ci#define REG_REF1		0x30
838c2ecf20Sopenharmony_ci#define REG_REF2		0x31
848c2ecf20Sopenharmony_ci#define REG_FRAJH		0x32
858c2ecf20Sopenharmony_ci#define REG_FRAJL		0x33
868c2ecf20Sopenharmony_ci#define REG_FACT		0x34
878c2ecf20Sopenharmony_ci#define REG_L1AEC		0x35
888c2ecf20Sopenharmony_ci#define REG_AVGU		0x36
898c2ecf20Sopenharmony_ci#define REG_AVGV		0x37
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define REG_SPCB		0x60
928c2ecf20Sopenharmony_ci#define REG_SPCC		0x61
938c2ecf20Sopenharmony_ci#define REG_GAM1		0x62
948c2ecf20Sopenharmony_ci#define REG_GAM2		0x63
958c2ecf20Sopenharmony_ci#define REG_GAM3		0x64
968c2ecf20Sopenharmony_ci#define REG_SPCD		0x65
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define REG_SPCE		0x68
998c2ecf20Sopenharmony_ci#define REG_ADCL		0x69
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define REG_RMCO		0x6c
1028c2ecf20Sopenharmony_ci#define REG_GMCO		0x6d
1038c2ecf20Sopenharmony_ci#define REG_BMCO		0x6e
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* Register bits, values, etc. */
1078c2ecf20Sopenharmony_ci#define OV6650_PIDH		0x66	/* high byte of product ID number */
1088c2ecf20Sopenharmony_ci#define OV6650_PIDL		0x50	/* low byte of product ID number */
1098c2ecf20Sopenharmony_ci#define OV6650_MIDH		0x7F	/* high byte of mfg ID */
1108c2ecf20Sopenharmony_ci#define OV6650_MIDL		0xA2	/* low byte of mfg ID */
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define DEF_GAIN		0x00
1138c2ecf20Sopenharmony_ci#define DEF_BLUE		0x80
1148c2ecf20Sopenharmony_ci#define DEF_RED			0x80
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define SAT_SHIFT		4
1178c2ecf20Sopenharmony_ci#define SAT_MASK		(0xf << SAT_SHIFT)
1188c2ecf20Sopenharmony_ci#define SET_SAT(x)		(((x) << SAT_SHIFT) & SAT_MASK)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define HUE_EN			BIT(5)
1218c2ecf20Sopenharmony_ci#define HUE_MASK		0x1f
1228c2ecf20Sopenharmony_ci#define DEF_HUE			0x10
1238c2ecf20Sopenharmony_ci#define SET_HUE(x)		(HUE_EN | ((x) & HUE_MASK))
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define DEF_AECH		0x4D
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define CLKRC_8MHz		0x00
1288c2ecf20Sopenharmony_ci#define CLKRC_12MHz		0x40
1298c2ecf20Sopenharmony_ci#define CLKRC_16MHz		0x80
1308c2ecf20Sopenharmony_ci#define CLKRC_24MHz		0xc0
1318c2ecf20Sopenharmony_ci#define CLKRC_DIV_MASK		0x3f
1328c2ecf20Sopenharmony_ci#define GET_CLKRC_DIV(x)	(((x) & CLKRC_DIV_MASK) + 1)
1338c2ecf20Sopenharmony_ci#define DEF_CLKRC		0x00
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#define COMA_RESET		BIT(7)
1368c2ecf20Sopenharmony_ci#define COMA_QCIF		BIT(5)
1378c2ecf20Sopenharmony_ci#define COMA_RAW_RGB		BIT(4)
1388c2ecf20Sopenharmony_ci#define COMA_RGB		BIT(3)
1398c2ecf20Sopenharmony_ci#define COMA_BW			BIT(2)
1408c2ecf20Sopenharmony_ci#define COMA_WORD_SWAP		BIT(1)
1418c2ecf20Sopenharmony_ci#define COMA_BYTE_SWAP		BIT(0)
1428c2ecf20Sopenharmony_ci#define DEF_COMA		0x00
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define COMB_FLIP_V		BIT(7)
1458c2ecf20Sopenharmony_ci#define COMB_FLIP_H		BIT(5)
1468c2ecf20Sopenharmony_ci#define COMB_BAND_FILTER	BIT(4)
1478c2ecf20Sopenharmony_ci#define COMB_AWB		BIT(2)
1488c2ecf20Sopenharmony_ci#define COMB_AGC		BIT(1)
1498c2ecf20Sopenharmony_ci#define COMB_AEC		BIT(0)
1508c2ecf20Sopenharmony_ci#define DEF_COMB		0x5f
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define COML_ONE_CHANNEL	BIT(7)
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define DEF_HSTRT		0x24
1558c2ecf20Sopenharmony_ci#define DEF_HSTOP		0xd4
1568c2ecf20Sopenharmony_ci#define DEF_VSTRT		0x04
1578c2ecf20Sopenharmony_ci#define DEF_VSTOP		0x94
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define COMF_HREF_LOW		BIT(4)
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#define COMJ_PCLK_RISING	BIT(4)
1628c2ecf20Sopenharmony_ci#define COMJ_VSYNC_HIGH		BIT(0)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* supported resolutions */
1658c2ecf20Sopenharmony_ci#define W_QCIF			(DEF_HSTOP - DEF_HSTRT)
1668c2ecf20Sopenharmony_ci#define W_CIF			(W_QCIF << 1)
1678c2ecf20Sopenharmony_ci#define H_QCIF			(DEF_VSTOP - DEF_VSTRT)
1688c2ecf20Sopenharmony_ci#define H_CIF			(H_QCIF << 1)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define FRAME_RATE_MAX		30
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct ov6650_reg {
1748c2ecf20Sopenharmony_ci	u8	reg;
1758c2ecf20Sopenharmony_ci	u8	val;
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistruct ov6650 {
1798c2ecf20Sopenharmony_ci	struct v4l2_subdev	subdev;
1808c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
1818c2ecf20Sopenharmony_ci	struct {
1828c2ecf20Sopenharmony_ci		/* exposure/autoexposure cluster */
1838c2ecf20Sopenharmony_ci		struct v4l2_ctrl *autoexposure;
1848c2ecf20Sopenharmony_ci		struct v4l2_ctrl *exposure;
1858c2ecf20Sopenharmony_ci	};
1868c2ecf20Sopenharmony_ci	struct {
1878c2ecf20Sopenharmony_ci		/* gain/autogain cluster */
1888c2ecf20Sopenharmony_ci		struct v4l2_ctrl *autogain;
1898c2ecf20Sopenharmony_ci		struct v4l2_ctrl *gain;
1908c2ecf20Sopenharmony_ci	};
1918c2ecf20Sopenharmony_ci	struct {
1928c2ecf20Sopenharmony_ci		/* blue/red/autowhitebalance cluster */
1938c2ecf20Sopenharmony_ci		struct v4l2_ctrl *autowb;
1948c2ecf20Sopenharmony_ci		struct v4l2_ctrl *blue;
1958c2ecf20Sopenharmony_ci		struct v4l2_ctrl *red;
1968c2ecf20Sopenharmony_ci	};
1978c2ecf20Sopenharmony_ci	struct v4l2_clk		*clk;
1988c2ecf20Sopenharmony_ci	bool			half_scale;	/* scale down output by 2 */
1998c2ecf20Sopenharmony_ci	struct v4l2_rect	rect;		/* sensor cropping window */
2008c2ecf20Sopenharmony_ci	struct v4l2_fract	tpf;		/* as requested with s_frame_interval */
2018c2ecf20Sopenharmony_ci	u32 code;
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistruct ov6650_xclk {
2058c2ecf20Sopenharmony_ci	unsigned long	rate;
2068c2ecf20Sopenharmony_ci	u8		clkrc;
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic const struct ov6650_xclk ov6650_xclk[] = {
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	.rate	= 8000000,
2128c2ecf20Sopenharmony_ci	.clkrc	= CLKRC_8MHz,
2138c2ecf20Sopenharmony_ci},
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	.rate	= 12000000,
2168c2ecf20Sopenharmony_ci	.clkrc	= CLKRC_12MHz,
2178c2ecf20Sopenharmony_ci},
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	.rate	= 16000000,
2208c2ecf20Sopenharmony_ci	.clkrc	= CLKRC_16MHz,
2218c2ecf20Sopenharmony_ci},
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	.rate	= 24000000,
2248c2ecf20Sopenharmony_ci	.clkrc	= CLKRC_24MHz,
2258c2ecf20Sopenharmony_ci},
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic u32 ov6650_codes[] = {
2298c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_YUYV8_2X8,
2308c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_UYVY8_2X8,
2318c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_YVYU8_2X8,
2328c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_VYUY8_2X8,
2338c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_SBGGR8_1X8,
2348c2ecf20Sopenharmony_ci	MEDIA_BUS_FMT_Y8_1X8,
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic const struct v4l2_mbus_framefmt ov6650_def_fmt = {
2388c2ecf20Sopenharmony_ci	.width		= W_CIF,
2398c2ecf20Sopenharmony_ci	.height		= H_CIF,
2408c2ecf20Sopenharmony_ci	.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
2418c2ecf20Sopenharmony_ci	.colorspace	= V4L2_COLORSPACE_SRGB,
2428c2ecf20Sopenharmony_ci	.field		= V4L2_FIELD_NONE,
2438c2ecf20Sopenharmony_ci	.ycbcr_enc	= V4L2_YCBCR_ENC_DEFAULT,
2448c2ecf20Sopenharmony_ci	.quantization	= V4L2_QUANTIZATION_DEFAULT,
2458c2ecf20Sopenharmony_ci	.xfer_func	= V4L2_XFER_FUNC_DEFAULT,
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/* read a register */
2498c2ecf20Sopenharmony_cistatic int ov6650_reg_read(struct i2c_client *client, u8 reg, u8 *val)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int ret;
2528c2ecf20Sopenharmony_ci	u8 data = reg;
2538c2ecf20Sopenharmony_ci	struct i2c_msg msg = {
2548c2ecf20Sopenharmony_ci		.addr	= client->addr,
2558c2ecf20Sopenharmony_ci		.flags	= 0,
2568c2ecf20Sopenharmony_ci		.len	= 1,
2578c2ecf20Sopenharmony_ci		.buf	= &data,
2588c2ecf20Sopenharmony_ci	};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, &msg, 1);
2618c2ecf20Sopenharmony_ci	if (ret < 0)
2628c2ecf20Sopenharmony_ci		goto err;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	msg.flags = I2C_M_RD;
2658c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, &msg, 1);
2668c2ecf20Sopenharmony_ci	if (ret < 0)
2678c2ecf20Sopenharmony_ci		goto err;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	*val = data;
2708c2ecf20Sopenharmony_ci	return 0;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cierr:
2738c2ecf20Sopenharmony_ci	dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
2748c2ecf20Sopenharmony_ci	return ret;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/* write a register */
2788c2ecf20Sopenharmony_cistatic int ov6650_reg_write(struct i2c_client *client, u8 reg, u8 val)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int ret;
2818c2ecf20Sopenharmony_ci	unsigned char data[2] = { reg, val };
2828c2ecf20Sopenharmony_ci	struct i2c_msg msg = {
2838c2ecf20Sopenharmony_ci		.addr	= client->addr,
2848c2ecf20Sopenharmony_ci		.flags	= 0,
2858c2ecf20Sopenharmony_ci		.len	= 2,
2868c2ecf20Sopenharmony_ci		.buf	= data,
2878c2ecf20Sopenharmony_ci	};
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, &msg, 1);
2908c2ecf20Sopenharmony_ci	udelay(100);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (ret < 0) {
2938c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
2948c2ecf20Sopenharmony_ci		return ret;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci	return 0;
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/* Read a register, alter its bits, write it back */
3018c2ecf20Sopenharmony_cistatic int ov6650_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 mask)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	u8 val;
3048c2ecf20Sopenharmony_ci	int ret;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	ret = ov6650_reg_read(client, reg, &val);
3078c2ecf20Sopenharmony_ci	if (ret) {
3088c2ecf20Sopenharmony_ci		dev_err(&client->dev,
3098c2ecf20Sopenharmony_ci			"[Read]-Modify-Write of register 0x%02x failed!\n",
3108c2ecf20Sopenharmony_ci			reg);
3118c2ecf20Sopenharmony_ci		return ret;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	val &= ~mask;
3158c2ecf20Sopenharmony_ci	val |= set;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	ret = ov6650_reg_write(client, reg, val);
3188c2ecf20Sopenharmony_ci	if (ret)
3198c2ecf20Sopenharmony_ci		dev_err(&client->dev,
3208c2ecf20Sopenharmony_ci			"Read-Modify-[Write] of register 0x%02x failed!\n",
3218c2ecf20Sopenharmony_ci			reg);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	return ret;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic struct ov6650 *to_ov6650(const struct i2c_client *client)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	return container_of(i2c_get_clientdata(client), struct ov6650, subdev);
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/* Start/Stop streaming from the device */
3328c2ecf20Sopenharmony_cistatic int ov6650_s_stream(struct v4l2_subdev *sd, int enable)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	return 0;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci/* Get status of additional camera capabilities */
3388c2ecf20Sopenharmony_cistatic int ov6550_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
3418c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &priv->subdev;
3428c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3438c2ecf20Sopenharmony_ci	uint8_t reg, reg2;
3448c2ecf20Sopenharmony_ci	int ret;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	switch (ctrl->id) {
3478c2ecf20Sopenharmony_ci	case V4L2_CID_AUTOGAIN:
3488c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_GAIN, &reg);
3498c2ecf20Sopenharmony_ci		if (!ret)
3508c2ecf20Sopenharmony_ci			priv->gain->val = reg;
3518c2ecf20Sopenharmony_ci		return ret;
3528c2ecf20Sopenharmony_ci	case V4L2_CID_AUTO_WHITE_BALANCE:
3538c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_BLUE, &reg);
3548c2ecf20Sopenharmony_ci		if (!ret)
3558c2ecf20Sopenharmony_ci			ret = ov6650_reg_read(client, REG_RED, &reg2);
3568c2ecf20Sopenharmony_ci		if (!ret) {
3578c2ecf20Sopenharmony_ci			priv->blue->val = reg;
3588c2ecf20Sopenharmony_ci			priv->red->val = reg2;
3598c2ecf20Sopenharmony_ci		}
3608c2ecf20Sopenharmony_ci		return ret;
3618c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE_AUTO:
3628c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_AECH, &reg);
3638c2ecf20Sopenharmony_ci		if (!ret)
3648c2ecf20Sopenharmony_ci			priv->exposure->val = reg;
3658c2ecf20Sopenharmony_ci		return ret;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci	return -EINVAL;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/* Set status of additional camera capabilities */
3718c2ecf20Sopenharmony_cistatic int ov6550_s_ctrl(struct v4l2_ctrl *ctrl)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
3748c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &priv->subdev;
3758c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3768c2ecf20Sopenharmony_ci	int ret;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	switch (ctrl->id) {
3798c2ecf20Sopenharmony_ci	case V4L2_CID_AUTOGAIN:
3808c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMB,
3818c2ecf20Sopenharmony_ci				ctrl->val ? COMB_AGC : 0, COMB_AGC);
3828c2ecf20Sopenharmony_ci		if (!ret && !ctrl->val)
3838c2ecf20Sopenharmony_ci			ret = ov6650_reg_write(client, REG_GAIN, priv->gain->val);
3848c2ecf20Sopenharmony_ci		return ret;
3858c2ecf20Sopenharmony_ci	case V4L2_CID_AUTO_WHITE_BALANCE:
3868c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMB,
3878c2ecf20Sopenharmony_ci				ctrl->val ? COMB_AWB : 0, COMB_AWB);
3888c2ecf20Sopenharmony_ci		if (!ret && !ctrl->val) {
3898c2ecf20Sopenharmony_ci			ret = ov6650_reg_write(client, REG_BLUE, priv->blue->val);
3908c2ecf20Sopenharmony_ci			if (!ret)
3918c2ecf20Sopenharmony_ci				ret = ov6650_reg_write(client, REG_RED,
3928c2ecf20Sopenharmony_ci							priv->red->val);
3938c2ecf20Sopenharmony_ci		}
3948c2ecf20Sopenharmony_ci		return ret;
3958c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
3968c2ecf20Sopenharmony_ci		return ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->val),
3978c2ecf20Sopenharmony_ci				SAT_MASK);
3988c2ecf20Sopenharmony_ci	case V4L2_CID_HUE:
3998c2ecf20Sopenharmony_ci		return ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->val),
4008c2ecf20Sopenharmony_ci				HUE_MASK);
4018c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
4028c2ecf20Sopenharmony_ci		return ov6650_reg_write(client, REG_BRT, ctrl->val);
4038c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE_AUTO:
4048c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMB, ctrl->val ==
4058c2ecf20Sopenharmony_ci				V4L2_EXPOSURE_AUTO ? COMB_AEC : 0, COMB_AEC);
4068c2ecf20Sopenharmony_ci		if (!ret && ctrl->val == V4L2_EXPOSURE_MANUAL)
4078c2ecf20Sopenharmony_ci			ret = ov6650_reg_write(client, REG_AECH,
4088c2ecf20Sopenharmony_ci						priv->exposure->val);
4098c2ecf20Sopenharmony_ci		return ret;
4108c2ecf20Sopenharmony_ci	case V4L2_CID_GAMMA:
4118c2ecf20Sopenharmony_ci		return ov6650_reg_write(client, REG_GAM1, ctrl->val);
4128c2ecf20Sopenharmony_ci	case V4L2_CID_VFLIP:
4138c2ecf20Sopenharmony_ci		return ov6650_reg_rmw(client, REG_COMB,
4148c2ecf20Sopenharmony_ci				ctrl->val ? COMB_FLIP_V : 0, COMB_FLIP_V);
4158c2ecf20Sopenharmony_ci	case V4L2_CID_HFLIP:
4168c2ecf20Sopenharmony_ci		return ov6650_reg_rmw(client, REG_COMB,
4178c2ecf20Sopenharmony_ci				ctrl->val ? COMB_FLIP_H : 0, COMB_FLIP_H);
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	return -EINVAL;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
4248c2ecf20Sopenharmony_cistatic int ov6650_get_register(struct v4l2_subdev *sd,
4258c2ecf20Sopenharmony_ci				struct v4l2_dbg_register *reg)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4288c2ecf20Sopenharmony_ci	int ret;
4298c2ecf20Sopenharmony_ci	u8 val;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	if (reg->reg & ~0xff)
4328c2ecf20Sopenharmony_ci		return -EINVAL;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	reg->size = 1;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	ret = ov6650_reg_read(client, reg->reg, &val);
4378c2ecf20Sopenharmony_ci	if (!ret)
4388c2ecf20Sopenharmony_ci		reg->val = (__u64)val;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	return ret;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic int ov6650_set_register(struct v4l2_subdev *sd,
4448c2ecf20Sopenharmony_ci				const struct v4l2_dbg_register *reg)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (reg->reg & ~0xff || reg->val & ~0xff)
4498c2ecf20Sopenharmony_ci		return -EINVAL;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return ov6650_reg_write(client, reg->reg, reg->val);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci#endif
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_cistatic int ov6650_s_power(struct v4l2_subdev *sd, int on)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4588c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
4598c2ecf20Sopenharmony_ci	int ret = 0;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (on)
4628c2ecf20Sopenharmony_ci		ret = v4l2_clk_enable(priv->clk);
4638c2ecf20Sopenharmony_ci	else
4648c2ecf20Sopenharmony_ci		v4l2_clk_disable(priv->clk);
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	return ret;
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic int ov6650_get_selection(struct v4l2_subdev *sd,
4708c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
4718c2ecf20Sopenharmony_ci		struct v4l2_subdev_selection *sel)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4748c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
4778c2ecf20Sopenharmony_ci		return -EINVAL;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	switch (sel->target) {
4808c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP_BOUNDS:
4818c2ecf20Sopenharmony_ci		sel->r.left = DEF_HSTRT << 1;
4828c2ecf20Sopenharmony_ci		sel->r.top = DEF_VSTRT << 1;
4838c2ecf20Sopenharmony_ci		sel->r.width = W_CIF;
4848c2ecf20Sopenharmony_ci		sel->r.height = H_CIF;
4858c2ecf20Sopenharmony_ci		return 0;
4868c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP:
4878c2ecf20Sopenharmony_ci		sel->r = priv->rect;
4888c2ecf20Sopenharmony_ci		return 0;
4898c2ecf20Sopenharmony_ci	default:
4908c2ecf20Sopenharmony_ci		return -EINVAL;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic int ov6650_set_selection(struct v4l2_subdev *sd,
4958c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
4968c2ecf20Sopenharmony_ci		struct v4l2_subdev_selection *sel)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4998c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
5008c2ecf20Sopenharmony_ci	int ret;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
5038c2ecf20Sopenharmony_ci	    sel->target != V4L2_SEL_TGT_CROP)
5048c2ecf20Sopenharmony_ci		return -EINVAL;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	v4l_bound_align_image(&sel->r.width, 2, W_CIF, 1,
5078c2ecf20Sopenharmony_ci			      &sel->r.height, 2, H_CIF, 1, 0);
5088c2ecf20Sopenharmony_ci	v4l_bound_align_image(&sel->r.left, DEF_HSTRT << 1,
5098c2ecf20Sopenharmony_ci			      (DEF_HSTRT << 1) + W_CIF - (__s32)sel->r.width, 1,
5108c2ecf20Sopenharmony_ci			      &sel->r.top, DEF_VSTRT << 1,
5118c2ecf20Sopenharmony_ci			      (DEF_VSTRT << 1) + H_CIF - (__s32)sel->r.height,
5128c2ecf20Sopenharmony_ci			      1, 0);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	ret = ov6650_reg_write(client, REG_HSTRT, sel->r.left >> 1);
5158c2ecf20Sopenharmony_ci	if (!ret) {
5168c2ecf20Sopenharmony_ci		priv->rect.width += priv->rect.left - sel->r.left;
5178c2ecf20Sopenharmony_ci		priv->rect.left = sel->r.left;
5188c2ecf20Sopenharmony_ci		ret = ov6650_reg_write(client, REG_HSTOP,
5198c2ecf20Sopenharmony_ci				       (sel->r.left + sel->r.width) >> 1);
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	if (!ret) {
5228c2ecf20Sopenharmony_ci		priv->rect.width = sel->r.width;
5238c2ecf20Sopenharmony_ci		ret = ov6650_reg_write(client, REG_VSTRT, sel->r.top >> 1);
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci	if (!ret) {
5268c2ecf20Sopenharmony_ci		priv->rect.height += priv->rect.top - sel->r.top;
5278c2ecf20Sopenharmony_ci		priv->rect.top = sel->r.top;
5288c2ecf20Sopenharmony_ci		ret = ov6650_reg_write(client, REG_VSTOP,
5298c2ecf20Sopenharmony_ci				       (sel->r.top + sel->r.height) >> 1);
5308c2ecf20Sopenharmony_ci	}
5318c2ecf20Sopenharmony_ci	if (!ret)
5328c2ecf20Sopenharmony_ci		priv->rect.height = sel->r.height;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	return ret;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic int ov6650_get_fmt(struct v4l2_subdev *sd,
5388c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
5398c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf = &format->format;
5428c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
5438c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	if (format->pad)
5468c2ecf20Sopenharmony_ci		return -EINVAL;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* initialize response with default media bus frame format */
5498c2ecf20Sopenharmony_ci	*mf = ov6650_def_fmt;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* update media bus format code and frame size */
5528c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
5538c2ecf20Sopenharmony_ci		mf->width = cfg->try_fmt.width;
5548c2ecf20Sopenharmony_ci		mf->height = cfg->try_fmt.height;
5558c2ecf20Sopenharmony_ci		mf->code = cfg->try_fmt.code;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	} else {
5588c2ecf20Sopenharmony_ci		mf->width = priv->rect.width >> priv->half_scale;
5598c2ecf20Sopenharmony_ci		mf->height = priv->rect.height >> priv->half_scale;
5608c2ecf20Sopenharmony_ci		mf->code = priv->code;
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci	return 0;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	return width > rect->width >> 1 || height > rect->height >> 1;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci#define to_clkrc(div)	((div) - 1)
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci/* set the format we will capture in */
5738c2ecf20Sopenharmony_cistatic int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
5768c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
5778c2ecf20Sopenharmony_ci	bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
5788c2ecf20Sopenharmony_ci	struct v4l2_subdev_selection sel = {
5798c2ecf20Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
5808c2ecf20Sopenharmony_ci		.target = V4L2_SEL_TGT_CROP,
5818c2ecf20Sopenharmony_ci		.r.left = priv->rect.left + (priv->rect.width >> 1) -
5828c2ecf20Sopenharmony_ci			(mf->width >> (1 - half_scale)),
5838c2ecf20Sopenharmony_ci		.r.top = priv->rect.top + (priv->rect.height >> 1) -
5848c2ecf20Sopenharmony_ci			(mf->height >> (1 - half_scale)),
5858c2ecf20Sopenharmony_ci		.r.width = mf->width << half_scale,
5868c2ecf20Sopenharmony_ci		.r.height = mf->height << half_scale,
5878c2ecf20Sopenharmony_ci	};
5888c2ecf20Sopenharmony_ci	u32 code = mf->code;
5898c2ecf20Sopenharmony_ci	u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask;
5908c2ecf20Sopenharmony_ci	int ret;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/* select color matrix configuration for given color encoding */
5938c2ecf20Sopenharmony_ci	switch (code) {
5948c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_Y8_1X8:
5958c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
5968c2ecf20Sopenharmony_ci		coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
5978c2ecf20Sopenharmony_ci		coma_set |= COMA_BW;
5988c2ecf20Sopenharmony_ci		break;
5998c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YUYV8_2X8:
6008c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
6018c2ecf20Sopenharmony_ci		coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
6028c2ecf20Sopenharmony_ci		coma_set |= COMA_WORD_SWAP;
6038c2ecf20Sopenharmony_ci		break;
6048c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YVYU8_2X8:
6058c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
6068c2ecf20Sopenharmony_ci		coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
6078c2ecf20Sopenharmony_ci				COMA_BYTE_SWAP;
6088c2ecf20Sopenharmony_ci		break;
6098c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_UYVY8_2X8:
6108c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
6118c2ecf20Sopenharmony_ci		if (half_scale) {
6128c2ecf20Sopenharmony_ci			coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
6138c2ecf20Sopenharmony_ci			coma_set |= COMA_BYTE_SWAP;
6148c2ecf20Sopenharmony_ci		} else {
6158c2ecf20Sopenharmony_ci			coma_mask |= COMA_RGB | COMA_BW;
6168c2ecf20Sopenharmony_ci			coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
6178c2ecf20Sopenharmony_ci		}
6188c2ecf20Sopenharmony_ci		break;
6198c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_VYUY8_2X8:
6208c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
6218c2ecf20Sopenharmony_ci		if (half_scale) {
6228c2ecf20Sopenharmony_ci			coma_mask |= COMA_RGB | COMA_BW;
6238c2ecf20Sopenharmony_ci			coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
6248c2ecf20Sopenharmony_ci		} else {
6258c2ecf20Sopenharmony_ci			coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
6268c2ecf20Sopenharmony_ci			coma_set |= COMA_BYTE_SWAP;
6278c2ecf20Sopenharmony_ci		}
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_SBGGR8_1X8:
6308c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
6318c2ecf20Sopenharmony_ci		coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
6328c2ecf20Sopenharmony_ci		coma_set |= COMA_RAW_RGB | COMA_RGB;
6338c2ecf20Sopenharmony_ci		break;
6348c2ecf20Sopenharmony_ci	default:
6358c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
6368c2ecf20Sopenharmony_ci		return -EINVAL;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	if (code == MEDIA_BUS_FMT_Y8_1X8 ||
6408c2ecf20Sopenharmony_ci			code == MEDIA_BUS_FMT_SBGGR8_1X8) {
6418c2ecf20Sopenharmony_ci		coml_mask = COML_ONE_CHANNEL;
6428c2ecf20Sopenharmony_ci		coml_set = 0;
6438c2ecf20Sopenharmony_ci	} else {
6448c2ecf20Sopenharmony_ci		coml_mask = 0;
6458c2ecf20Sopenharmony_ci		coml_set = COML_ONE_CHANNEL;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (half_scale) {
6498c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "max resolution: QCIF\n");
6508c2ecf20Sopenharmony_ci		coma_set |= COMA_QCIF;
6518c2ecf20Sopenharmony_ci	} else {
6528c2ecf20Sopenharmony_ci		dev_dbg(&client->dev, "max resolution: CIF\n");
6538c2ecf20Sopenharmony_ci		coma_mask |= COMA_QCIF;
6548c2ecf20Sopenharmony_ci	}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	ret = ov6650_set_selection(sd, NULL, &sel);
6578c2ecf20Sopenharmony_ci	if (!ret)
6588c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
6598c2ecf20Sopenharmony_ci	if (!ret) {
6608c2ecf20Sopenharmony_ci		priv->half_scale = half_scale;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci	if (!ret)
6658c2ecf20Sopenharmony_ci		priv->code = code;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	return ret;
6688c2ecf20Sopenharmony_ci}
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_cistatic int ov6650_set_fmt(struct v4l2_subdev *sd,
6718c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
6728c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf = &format->format;
6758c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
6768c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (format->pad)
6798c2ecf20Sopenharmony_ci		return -EINVAL;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
6828c2ecf20Sopenharmony_ci		v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
6838c2ecf20Sopenharmony_ci				&mf->height, 2, H_CIF, 1, 0);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	switch (mf->code) {
6868c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_Y10_1X10:
6878c2ecf20Sopenharmony_ci		mf->code = MEDIA_BUS_FMT_Y8_1X8;
6888c2ecf20Sopenharmony_ci		fallthrough;
6898c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_Y8_1X8:
6908c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YVYU8_2X8:
6918c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_YUYV8_2X8:
6928c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_VYUY8_2X8:
6938c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_UYVY8_2X8:
6948c2ecf20Sopenharmony_ci		break;
6958c2ecf20Sopenharmony_ci	default:
6968c2ecf20Sopenharmony_ci		mf->code = MEDIA_BUS_FMT_SBGGR8_1X8;
6978c2ecf20Sopenharmony_ci		fallthrough;
6988c2ecf20Sopenharmony_ci	case MEDIA_BUS_FMT_SBGGR8_1X8:
6998c2ecf20Sopenharmony_ci		break;
7008c2ecf20Sopenharmony_ci	}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
7038c2ecf20Sopenharmony_ci		/* store media bus format code and frame size in pad config */
7048c2ecf20Sopenharmony_ci		cfg->try_fmt.width = mf->width;
7058c2ecf20Sopenharmony_ci		cfg->try_fmt.height = mf->height;
7068c2ecf20Sopenharmony_ci		cfg->try_fmt.code = mf->code;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		/* return default mbus frame format updated with pad config */
7098c2ecf20Sopenharmony_ci		*mf = ov6650_def_fmt;
7108c2ecf20Sopenharmony_ci		mf->width = cfg->try_fmt.width;
7118c2ecf20Sopenharmony_ci		mf->height = cfg->try_fmt.height;
7128c2ecf20Sopenharmony_ci		mf->code = cfg->try_fmt.code;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	} else {
7158c2ecf20Sopenharmony_ci		/* apply new media bus format code and frame size */
7168c2ecf20Sopenharmony_ci		int ret = ov6650_s_fmt(sd, mf);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci		if (ret)
7198c2ecf20Sopenharmony_ci			return ret;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		/* return default format updated with active size and code */
7228c2ecf20Sopenharmony_ci		*mf = ov6650_def_fmt;
7238c2ecf20Sopenharmony_ci		mf->width = priv->rect.width >> priv->half_scale;
7248c2ecf20Sopenharmony_ci		mf->height = priv->rect.height >> priv->half_scale;
7258c2ecf20Sopenharmony_ci		mf->code = priv->code;
7268c2ecf20Sopenharmony_ci	}
7278c2ecf20Sopenharmony_ci	return 0;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic int ov6650_enum_mbus_code(struct v4l2_subdev *sd,
7318c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
7328c2ecf20Sopenharmony_ci		struct v4l2_subdev_mbus_code_enum *code)
7338c2ecf20Sopenharmony_ci{
7348c2ecf20Sopenharmony_ci	if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes))
7358c2ecf20Sopenharmony_ci		return -EINVAL;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	code->code = ov6650_codes[code->index];
7388c2ecf20Sopenharmony_ci	return 0;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic int ov6650_g_frame_interval(struct v4l2_subdev *sd,
7428c2ecf20Sopenharmony_ci				   struct v4l2_subdev_frame_interval *ival)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
7458c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	ival->interval = priv->tpf;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
7508c2ecf20Sopenharmony_ci		ival->interval.numerator, ival->interval.denominator);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	return 0;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cistatic int ov6650_s_frame_interval(struct v4l2_subdev *sd,
7568c2ecf20Sopenharmony_ci				   struct v4l2_subdev_frame_interval *ival)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
7598c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
7608c2ecf20Sopenharmony_ci	struct v4l2_fract *tpf = &ival->interval;
7618c2ecf20Sopenharmony_ci	int div, ret;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	if (tpf->numerator == 0 || tpf->denominator == 0)
7648c2ecf20Sopenharmony_ci		div = 1;  /* Reset to full rate */
7658c2ecf20Sopenharmony_ci	else
7668c2ecf20Sopenharmony_ci		div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	if (div == 0)
7698c2ecf20Sopenharmony_ci		div = 1;
7708c2ecf20Sopenharmony_ci	else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
7718c2ecf20Sopenharmony_ci		div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	ret = ov6650_reg_rmw(client, REG_CLKRC, to_clkrc(div), CLKRC_DIV_MASK);
7748c2ecf20Sopenharmony_ci	if (!ret) {
7758c2ecf20Sopenharmony_ci		priv->tpf.numerator = div;
7768c2ecf20Sopenharmony_ci		priv->tpf.denominator = FRAME_RATE_MAX;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci		*tpf = priv->tpf;
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	return ret;
7828c2ecf20Sopenharmony_ci}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci/* Soft reset the camera. This has nothing to do with the RESET pin! */
7858c2ecf20Sopenharmony_cistatic int ov6650_reset(struct i2c_client *client)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	int ret;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "reset\n");
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
7928c2ecf20Sopenharmony_ci	if (ret)
7938c2ecf20Sopenharmony_ci		dev_err(&client->dev,
7948c2ecf20Sopenharmony_ci			"An error occurred while entering soft reset!\n");
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	return ret;
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci/* program default register values */
8008c2ecf20Sopenharmony_cistatic int ov6650_prog_dflt(struct i2c_client *client, u8 clkrc)
8018c2ecf20Sopenharmony_ci{
8028c2ecf20Sopenharmony_ci	int ret;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "initializing\n");
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	ret = ov6650_reg_write(client, REG_COMA, 0);	/* ~COMA_RESET */
8078c2ecf20Sopenharmony_ci	if (!ret)
8088c2ecf20Sopenharmony_ci		ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
8098c2ecf20Sopenharmony_ci	if (!ret)
8108c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	return ret;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic int ov6650_video_probe(struct v4l2_subdev *sd)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
8188c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
8198c2ecf20Sopenharmony_ci	const struct ov6650_xclk *xclk = NULL;
8208c2ecf20Sopenharmony_ci	unsigned long rate;
8218c2ecf20Sopenharmony_ci	u8 pidh, pidl, midh, midl;
8228c2ecf20Sopenharmony_ci	int i, ret = 0;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	priv->clk = v4l2_clk_get(&client->dev, NULL);
8258c2ecf20Sopenharmony_ci	if (IS_ERR(priv->clk)) {
8268c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->clk);
8278c2ecf20Sopenharmony_ci		dev_err(&client->dev, "v4l2_clk request err: %d\n", ret);
8288c2ecf20Sopenharmony_ci		return ret;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	rate = v4l2_clk_get_rate(priv->clk);
8328c2ecf20Sopenharmony_ci	for (i = 0; rate && i < ARRAY_SIZE(ov6650_xclk); i++) {
8338c2ecf20Sopenharmony_ci		if (rate != ov6650_xclk[i].rate)
8348c2ecf20Sopenharmony_ci			continue;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci		xclk = &ov6650_xclk[i];
8378c2ecf20Sopenharmony_ci		dev_info(&client->dev, "using host default clock rate %lukHz\n",
8388c2ecf20Sopenharmony_ci			 rate / 1000);
8398c2ecf20Sopenharmony_ci		break;
8408c2ecf20Sopenharmony_ci	}
8418c2ecf20Sopenharmony_ci	for (i = 0; !xclk && i < ARRAY_SIZE(ov6650_xclk); i++) {
8428c2ecf20Sopenharmony_ci		ret = v4l2_clk_set_rate(priv->clk, ov6650_xclk[i].rate);
8438c2ecf20Sopenharmony_ci		if (ret || v4l2_clk_get_rate(priv->clk) != ov6650_xclk[i].rate)
8448c2ecf20Sopenharmony_ci			continue;
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		xclk = &ov6650_xclk[i];
8478c2ecf20Sopenharmony_ci		dev_info(&client->dev, "using negotiated clock rate %lukHz\n",
8488c2ecf20Sopenharmony_ci			 xclk->rate / 1000);
8498c2ecf20Sopenharmony_ci		break;
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci	if (!xclk) {
8528c2ecf20Sopenharmony_ci		dev_err(&client->dev, "unable to get supported clock rate\n");
8538c2ecf20Sopenharmony_ci		if (!ret)
8548c2ecf20Sopenharmony_ci			ret = -EINVAL;
8558c2ecf20Sopenharmony_ci		goto eclkput;
8568c2ecf20Sopenharmony_ci	}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	ret = ov6650_s_power(sd, 1);
8598c2ecf20Sopenharmony_ci	if (ret < 0)
8608c2ecf20Sopenharmony_ci		goto eclkput;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	msleep(20);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	/*
8658c2ecf20Sopenharmony_ci	 * check and show product ID and manufacturer ID
8668c2ecf20Sopenharmony_ci	 */
8678c2ecf20Sopenharmony_ci	ret = ov6650_reg_read(client, REG_PIDH, &pidh);
8688c2ecf20Sopenharmony_ci	if (!ret)
8698c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_PIDL, &pidl);
8708c2ecf20Sopenharmony_ci	if (!ret)
8718c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_MIDH, &midh);
8728c2ecf20Sopenharmony_ci	if (!ret)
8738c2ecf20Sopenharmony_ci		ret = ov6650_reg_read(client, REG_MIDL, &midl);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (ret)
8768c2ecf20Sopenharmony_ci		goto done;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
8798c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
8808c2ecf20Sopenharmony_ci				pidh, pidl);
8818c2ecf20Sopenharmony_ci		ret = -ENODEV;
8828c2ecf20Sopenharmony_ci		goto done;
8838c2ecf20Sopenharmony_ci	}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	dev_info(&client->dev,
8868c2ecf20Sopenharmony_ci		"ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
8878c2ecf20Sopenharmony_ci		pidh, pidl, midh, midl);
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	ret = ov6650_reset(client);
8908c2ecf20Sopenharmony_ci	if (!ret)
8918c2ecf20Sopenharmony_ci		ret = ov6650_prog_dflt(client, xclk->clkrc);
8928c2ecf20Sopenharmony_ci	if (!ret) {
8938c2ecf20Sopenharmony_ci		struct v4l2_mbus_framefmt mf = ov6650_def_fmt;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci		ret = ov6650_s_fmt(sd, &mf);
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci	if (!ret)
8988c2ecf20Sopenharmony_ci		ret = v4l2_ctrl_handler_setup(&priv->hdl);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cidone:
9018c2ecf20Sopenharmony_ci	ov6650_s_power(sd, 0);
9028c2ecf20Sopenharmony_ci	if (!ret)
9038c2ecf20Sopenharmony_ci		return 0;
9048c2ecf20Sopenharmony_cieclkput:
9058c2ecf20Sopenharmony_ci	v4l2_clk_put(priv->clk);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	return ret;
9088c2ecf20Sopenharmony_ci}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
9118c2ecf20Sopenharmony_ci	.g_volatile_ctrl = ov6550_g_volatile_ctrl,
9128c2ecf20Sopenharmony_ci	.s_ctrl = ov6550_s_ctrl,
9138c2ecf20Sopenharmony_ci};
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops ov6650_core_ops = {
9168c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
9178c2ecf20Sopenharmony_ci	.g_register		= ov6650_get_register,
9188c2ecf20Sopenharmony_ci	.s_register		= ov6650_set_register,
9198c2ecf20Sopenharmony_ci#endif
9208c2ecf20Sopenharmony_ci	.s_power		= ov6650_s_power,
9218c2ecf20Sopenharmony_ci};
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/* Request bus settings on camera side */
9248c2ecf20Sopenharmony_cistatic int ov6650_get_mbus_config(struct v4l2_subdev *sd,
9258c2ecf20Sopenharmony_ci				  unsigned int pad,
9268c2ecf20Sopenharmony_ci				  struct v4l2_mbus_config *cfg)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
9298c2ecf20Sopenharmony_ci	u8 comj, comf;
9308c2ecf20Sopenharmony_ci	int ret;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	ret = ov6650_reg_read(client, REG_COMJ, &comj);
9338c2ecf20Sopenharmony_ci	if (ret)
9348c2ecf20Sopenharmony_ci		return ret;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	ret = ov6650_reg_read(client, REG_COMF, &comf);
9378c2ecf20Sopenharmony_ci	if (ret)
9388c2ecf20Sopenharmony_ci		return ret;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH
9418c2ecf20Sopenharmony_ci		   | ((comj & COMJ_VSYNC_HIGH)  ? V4L2_MBUS_VSYNC_ACTIVE_HIGH
9428c2ecf20Sopenharmony_ci						: V4L2_MBUS_VSYNC_ACTIVE_LOW)
9438c2ecf20Sopenharmony_ci		   | ((comf & COMF_HREF_LOW)    ? V4L2_MBUS_HSYNC_ACTIVE_LOW
9448c2ecf20Sopenharmony_ci						: V4L2_MBUS_HSYNC_ACTIVE_HIGH)
9458c2ecf20Sopenharmony_ci		   | ((comj & COMJ_PCLK_RISING) ? V4L2_MBUS_PCLK_SAMPLE_RISING
9468c2ecf20Sopenharmony_ci						: V4L2_MBUS_PCLK_SAMPLE_FALLING);
9478c2ecf20Sopenharmony_ci	cfg->type = V4L2_MBUS_PARALLEL;
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	return 0;
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/* Alter bus settings on camera side */
9538c2ecf20Sopenharmony_cistatic int ov6650_set_mbus_config(struct v4l2_subdev *sd,
9548c2ecf20Sopenharmony_ci				  unsigned int pad,
9558c2ecf20Sopenharmony_ci				  struct v4l2_mbus_config *cfg)
9568c2ecf20Sopenharmony_ci{
9578c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
9588c2ecf20Sopenharmony_ci	int ret = 0;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	if (cfg->flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
9618c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
9628c2ecf20Sopenharmony_ci	else if (cfg->flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
9638c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
9648c2ecf20Sopenharmony_ci	if (ret)
9658c2ecf20Sopenharmony_ci		return ret;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	if (cfg->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
9688c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
9698c2ecf20Sopenharmony_ci	else if (cfg->flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
9708c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
9718c2ecf20Sopenharmony_ci	if (ret)
9728c2ecf20Sopenharmony_ci		return ret;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	if (cfg->flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
9758c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
9768c2ecf20Sopenharmony_ci	else if (cfg->flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
9778c2ecf20Sopenharmony_ci		ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
9788c2ecf20Sopenharmony_ci	if (ret)
9798c2ecf20Sopenharmony_ci		return ret;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	/*
9828c2ecf20Sopenharmony_ci	 * Update the configuration to report what is actually applied to
9838c2ecf20Sopenharmony_ci	 * the hardware.
9848c2ecf20Sopenharmony_ci	 */
9858c2ecf20Sopenharmony_ci	return ov6650_get_mbus_config(sd, pad, cfg);
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops ov6650_video_ops = {
9898c2ecf20Sopenharmony_ci	.s_stream	= ov6650_s_stream,
9908c2ecf20Sopenharmony_ci	.g_frame_interval = ov6650_g_frame_interval,
9918c2ecf20Sopenharmony_ci	.s_frame_interval = ov6650_s_frame_interval,
9928c2ecf20Sopenharmony_ci};
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops ov6650_pad_ops = {
9958c2ecf20Sopenharmony_ci	.enum_mbus_code = ov6650_enum_mbus_code,
9968c2ecf20Sopenharmony_ci	.get_selection	= ov6650_get_selection,
9978c2ecf20Sopenharmony_ci	.set_selection	= ov6650_set_selection,
9988c2ecf20Sopenharmony_ci	.get_fmt	= ov6650_get_fmt,
9998c2ecf20Sopenharmony_ci	.set_fmt	= ov6650_set_fmt,
10008c2ecf20Sopenharmony_ci	.get_mbus_config = ov6650_get_mbus_config,
10018c2ecf20Sopenharmony_ci	.set_mbus_config = ov6650_set_mbus_config,
10028c2ecf20Sopenharmony_ci};
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops ov6650_subdev_ops = {
10058c2ecf20Sopenharmony_ci	.core	= &ov6650_core_ops,
10068c2ecf20Sopenharmony_ci	.video	= &ov6650_video_ops,
10078c2ecf20Sopenharmony_ci	.pad	= &ov6650_pad_ops,
10088c2ecf20Sopenharmony_ci};
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops ov6650_internal_ops = {
10118c2ecf20Sopenharmony_ci	.registered = ov6650_video_probe,
10128c2ecf20Sopenharmony_ci};
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci/*
10158c2ecf20Sopenharmony_ci * i2c_driver function
10168c2ecf20Sopenharmony_ci */
10178c2ecf20Sopenharmony_cistatic int ov6650_probe(struct i2c_client *client,
10188c2ecf20Sopenharmony_ci			const struct i2c_device_id *did)
10198c2ecf20Sopenharmony_ci{
10208c2ecf20Sopenharmony_ci	struct ov6650 *priv;
10218c2ecf20Sopenharmony_ci	int ret;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
10248c2ecf20Sopenharmony_ci	if (!priv)
10258c2ecf20Sopenharmony_ci		return -ENOMEM;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
10288c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&priv->hdl, 13);
10298c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10308c2ecf20Sopenharmony_ci			V4L2_CID_VFLIP, 0, 1, 1, 0);
10318c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10328c2ecf20Sopenharmony_ci			V4L2_CID_HFLIP, 0, 1, 1, 0);
10338c2ecf20Sopenharmony_ci	priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10348c2ecf20Sopenharmony_ci			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
10358c2ecf20Sopenharmony_ci	priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10368c2ecf20Sopenharmony_ci			V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
10378c2ecf20Sopenharmony_ci	priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10388c2ecf20Sopenharmony_ci			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
10398c2ecf20Sopenharmony_ci	priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10408c2ecf20Sopenharmony_ci			V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
10418c2ecf20Sopenharmony_ci	priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10428c2ecf20Sopenharmony_ci			V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
10438c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10448c2ecf20Sopenharmony_ci			V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
10458c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10468c2ecf20Sopenharmony_ci			V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
10478c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10488c2ecf20Sopenharmony_ci			V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
10498c2ecf20Sopenharmony_ci	priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
10508c2ecf20Sopenharmony_ci			&ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
10518c2ecf20Sopenharmony_ci			V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
10528c2ecf20Sopenharmony_ci	priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10538c2ecf20Sopenharmony_ci			V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
10548c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
10558c2ecf20Sopenharmony_ci			V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	priv->subdev.ctrl_handler = &priv->hdl;
10588c2ecf20Sopenharmony_ci	if (priv->hdl.error) {
10598c2ecf20Sopenharmony_ci		ret = priv->hdl.error;
10608c2ecf20Sopenharmony_ci		goto ectlhdlfree;
10618c2ecf20Sopenharmony_ci	}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
10648c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
10658c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
10668c2ecf20Sopenharmony_ci				V4L2_EXPOSURE_MANUAL, true);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	priv->rect.left	  = DEF_HSTRT << 1;
10698c2ecf20Sopenharmony_ci	priv->rect.top	  = DEF_VSTRT << 1;
10708c2ecf20Sopenharmony_ci	priv->rect.width  = W_CIF;
10718c2ecf20Sopenharmony_ci	priv->rect.height = H_CIF;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	/* Hardware default frame interval */
10748c2ecf20Sopenharmony_ci	priv->tpf.numerator   = GET_CLKRC_DIV(DEF_CLKRC);
10758c2ecf20Sopenharmony_ci	priv->tpf.denominator = FRAME_RATE_MAX;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	priv->subdev.internal_ops = &ov6650_internal_ops;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	ret = v4l2_async_register_subdev(&priv->subdev);
10808c2ecf20Sopenharmony_ci	if (!ret)
10818c2ecf20Sopenharmony_ci		return 0;
10828c2ecf20Sopenharmony_ciectlhdlfree:
10838c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&priv->hdl);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	return ret;
10868c2ecf20Sopenharmony_ci}
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_cistatic int ov6650_remove(struct i2c_client *client)
10898c2ecf20Sopenharmony_ci{
10908c2ecf20Sopenharmony_ci	struct ov6650 *priv = to_ov6650(client);
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	v4l2_clk_put(priv->clk);
10938c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(&priv->subdev);
10948c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&priv->hdl);
10958c2ecf20Sopenharmony_ci	return 0;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic const struct i2c_device_id ov6650_id[] = {
10998c2ecf20Sopenharmony_ci	{ "ov6650", 0 },
11008c2ecf20Sopenharmony_ci	{ }
11018c2ecf20Sopenharmony_ci};
11028c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ov6650_id);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic struct i2c_driver ov6650_i2c_driver = {
11058c2ecf20Sopenharmony_ci	.driver = {
11068c2ecf20Sopenharmony_ci		.name = "ov6650",
11078c2ecf20Sopenharmony_ci	},
11088c2ecf20Sopenharmony_ci	.probe    = ov6650_probe,
11098c2ecf20Sopenharmony_ci	.remove   = ov6650_remove,
11108c2ecf20Sopenharmony_ci	.id_table = ov6650_id,
11118c2ecf20Sopenharmony_ci};
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_cimodule_i2c_driver(ov6650_i2c_driver);
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("V4L2 subdevice driver for OmniVision OV6650 camera sensor");
11168c2ecf20Sopenharmony_ciMODULE_AUTHOR("Janusz Krzysztofik <jmkrzyszt@gmail.com");
11178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1118