18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for MT9V022, MT9V024, MT9V032, and MT9V034 CMOS Image Sensors
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on the MT9M001 driver,
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/clk.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
158c2ecf20Sopenharmony_ci#include <linux/i2c.h>
168c2ecf20Sopenharmony_ci#include <linux/log2.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/of_graph.h>
208c2ecf20Sopenharmony_ci#include <linux/regmap.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
238c2ecf20Sopenharmony_ci#include <linux/v4l2-mediabus.h>
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <media/i2c/mt9v032.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
298c2ecf20Sopenharmony_ci#include <media/v4l2-fwnode.h>
308c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* The first four rows are black rows. The active area spans 753x481 pixels. */
338c2ecf20Sopenharmony_ci#define MT9V032_PIXEL_ARRAY_HEIGHT			485
348c2ecf20Sopenharmony_ci#define MT9V032_PIXEL_ARRAY_WIDTH			753
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define MT9V032_SYSCLK_FREQ_DEF				26600000
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define MT9V032_CHIP_VERSION				0x00
398c2ecf20Sopenharmony_ci#define		MT9V032_CHIP_ID_REV1			0x1311
408c2ecf20Sopenharmony_ci#define		MT9V032_CHIP_ID_REV3			0x1313
418c2ecf20Sopenharmony_ci#define		MT9V034_CHIP_ID_REV1			0X1324
428c2ecf20Sopenharmony_ci#define MT9V032_COLUMN_START				0x01
438c2ecf20Sopenharmony_ci#define		MT9V032_COLUMN_START_MIN		1
448c2ecf20Sopenharmony_ci#define		MT9V032_COLUMN_START_DEF		1
458c2ecf20Sopenharmony_ci#define		MT9V032_COLUMN_START_MAX		752
468c2ecf20Sopenharmony_ci#define MT9V032_ROW_START				0x02
478c2ecf20Sopenharmony_ci#define		MT9V032_ROW_START_MIN			4
488c2ecf20Sopenharmony_ci#define		MT9V032_ROW_START_DEF			5
498c2ecf20Sopenharmony_ci#define		MT9V032_ROW_START_MAX			482
508c2ecf20Sopenharmony_ci#define MT9V032_WINDOW_HEIGHT				0x03
518c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_HEIGHT_MIN		1
528c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_HEIGHT_DEF		480
538c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_HEIGHT_MAX		480
548c2ecf20Sopenharmony_ci#define MT9V032_WINDOW_WIDTH				0x04
558c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_WIDTH_MIN		1
568c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_WIDTH_DEF		752
578c2ecf20Sopenharmony_ci#define		MT9V032_WINDOW_WIDTH_MAX		752
588c2ecf20Sopenharmony_ci#define MT9V032_HORIZONTAL_BLANKING			0x05
598c2ecf20Sopenharmony_ci#define		MT9V032_HORIZONTAL_BLANKING_MIN		43
608c2ecf20Sopenharmony_ci#define		MT9V034_HORIZONTAL_BLANKING_MIN		61
618c2ecf20Sopenharmony_ci#define		MT9V032_HORIZONTAL_BLANKING_DEF		94
628c2ecf20Sopenharmony_ci#define		MT9V032_HORIZONTAL_BLANKING_MAX		1023
638c2ecf20Sopenharmony_ci#define MT9V032_VERTICAL_BLANKING			0x06
648c2ecf20Sopenharmony_ci#define		MT9V032_VERTICAL_BLANKING_MIN		4
658c2ecf20Sopenharmony_ci#define		MT9V034_VERTICAL_BLANKING_MIN		2
668c2ecf20Sopenharmony_ci#define		MT9V032_VERTICAL_BLANKING_DEF		45
678c2ecf20Sopenharmony_ci#define		MT9V032_VERTICAL_BLANKING_MAX		3000
688c2ecf20Sopenharmony_ci#define		MT9V034_VERTICAL_BLANKING_MAX		32288
698c2ecf20Sopenharmony_ci#define MT9V032_CHIP_CONTROL				0x07
708c2ecf20Sopenharmony_ci#define		MT9V032_CHIP_CONTROL_MASTER_MODE	(1 << 3)
718c2ecf20Sopenharmony_ci#define		MT9V032_CHIP_CONTROL_DOUT_ENABLE	(1 << 7)
728c2ecf20Sopenharmony_ci#define		MT9V032_CHIP_CONTROL_SEQUENTIAL		(1 << 8)
738c2ecf20Sopenharmony_ci#define MT9V032_SHUTTER_WIDTH1				0x08
748c2ecf20Sopenharmony_ci#define MT9V032_SHUTTER_WIDTH2				0x09
758c2ecf20Sopenharmony_ci#define MT9V032_SHUTTER_WIDTH_CONTROL			0x0a
768c2ecf20Sopenharmony_ci#define MT9V032_TOTAL_SHUTTER_WIDTH			0x0b
778c2ecf20Sopenharmony_ci#define		MT9V032_TOTAL_SHUTTER_WIDTH_MIN		1
788c2ecf20Sopenharmony_ci#define		MT9V034_TOTAL_SHUTTER_WIDTH_MIN		0
798c2ecf20Sopenharmony_ci#define		MT9V032_TOTAL_SHUTTER_WIDTH_DEF		480
808c2ecf20Sopenharmony_ci#define		MT9V032_TOTAL_SHUTTER_WIDTH_MAX		32767
818c2ecf20Sopenharmony_ci#define		MT9V034_TOTAL_SHUTTER_WIDTH_MAX		32765
828c2ecf20Sopenharmony_ci#define MT9V032_RESET					0x0c
838c2ecf20Sopenharmony_ci#define MT9V032_READ_MODE				0x0d
848c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_ROW_BIN_MASK		(3 << 0)
858c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_ROW_BIN_SHIFT		0
868c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_COLUMN_BIN_MASK	(3 << 2)
878c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_COLUMN_BIN_SHIFT	2
888c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_ROW_FLIP		(1 << 4)
898c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_COLUMN_FLIP		(1 << 5)
908c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_DARK_COLUMNS		(1 << 6)
918c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_DARK_ROWS		(1 << 7)
928c2ecf20Sopenharmony_ci#define		MT9V032_READ_MODE_RESERVED		0x0300
938c2ecf20Sopenharmony_ci#define MT9V032_PIXEL_OPERATION_MODE			0x0f
948c2ecf20Sopenharmony_ci#define		MT9V034_PIXEL_OPERATION_MODE_HDR	(1 << 0)
958c2ecf20Sopenharmony_ci#define		MT9V034_PIXEL_OPERATION_MODE_COLOR	(1 << 1)
968c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_OPERATION_MODE_COLOR	(1 << 2)
978c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_OPERATION_MODE_HDR	(1 << 6)
988c2ecf20Sopenharmony_ci#define MT9V032_ANALOG_GAIN				0x35
998c2ecf20Sopenharmony_ci#define		MT9V032_ANALOG_GAIN_MIN			16
1008c2ecf20Sopenharmony_ci#define		MT9V032_ANALOG_GAIN_DEF			16
1018c2ecf20Sopenharmony_ci#define		MT9V032_ANALOG_GAIN_MAX			64
1028c2ecf20Sopenharmony_ci#define MT9V032_MAX_ANALOG_GAIN				0x36
1038c2ecf20Sopenharmony_ci#define		MT9V032_MAX_ANALOG_GAIN_MAX		127
1048c2ecf20Sopenharmony_ci#define MT9V032_FRAME_DARK_AVERAGE			0x42
1058c2ecf20Sopenharmony_ci#define MT9V032_DARK_AVG_THRESH				0x46
1068c2ecf20Sopenharmony_ci#define		MT9V032_DARK_AVG_LOW_THRESH_MASK	(255 << 0)
1078c2ecf20Sopenharmony_ci#define		MT9V032_DARK_AVG_LOW_THRESH_SHIFT	0
1088c2ecf20Sopenharmony_ci#define		MT9V032_DARK_AVG_HIGH_THRESH_MASK	(255 << 8)
1098c2ecf20Sopenharmony_ci#define		MT9V032_DARK_AVG_HIGH_THRESH_SHIFT	8
1108c2ecf20Sopenharmony_ci#define MT9V032_ROW_NOISE_CORR_CONTROL			0x70
1118c2ecf20Sopenharmony_ci#define		MT9V034_ROW_NOISE_CORR_ENABLE		(1 << 0)
1128c2ecf20Sopenharmony_ci#define		MT9V034_ROW_NOISE_CORR_USE_BLK_AVG	(1 << 1)
1138c2ecf20Sopenharmony_ci#define		MT9V032_ROW_NOISE_CORR_ENABLE		(1 << 5)
1148c2ecf20Sopenharmony_ci#define		MT9V032_ROW_NOISE_CORR_USE_BLK_AVG	(1 << 7)
1158c2ecf20Sopenharmony_ci#define MT9V032_PIXEL_CLOCK				0x74
1168c2ecf20Sopenharmony_ci#define MT9V034_PIXEL_CLOCK				0x72
1178c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_CLOCK_INV_LINE		(1 << 0)
1188c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_CLOCK_INV_FRAME		(1 << 1)
1198c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_CLOCK_XOR_LINE		(1 << 2)
1208c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_CLOCK_CONT_LINE		(1 << 3)
1218c2ecf20Sopenharmony_ci#define		MT9V032_PIXEL_CLOCK_INV_PXL_CLK		(1 << 4)
1228c2ecf20Sopenharmony_ci#define MT9V032_TEST_PATTERN				0x7f
1238c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_DATA_MASK		(1023 << 0)
1248c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_DATA_SHIFT		0
1258c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_USE_DATA		(1 << 10)
1268c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_GRAY_MASK		(3 << 11)
1278c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_GRAY_NONE		(0 << 11)
1288c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_GRAY_VERTICAL	(1 << 11)
1298c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_GRAY_HORIZONTAL	(2 << 11)
1308c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_GRAY_DIAGONAL	(3 << 11)
1318c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_ENABLE		(1 << 13)
1328c2ecf20Sopenharmony_ci#define		MT9V032_TEST_PATTERN_FLIP		(1 << 14)
1338c2ecf20Sopenharmony_ci#define MT9V032_AEGC_DESIRED_BIN			0xa5
1348c2ecf20Sopenharmony_ci#define MT9V032_AEC_UPDATE_FREQUENCY			0xa6
1358c2ecf20Sopenharmony_ci#define MT9V032_AEC_LPF					0xa8
1368c2ecf20Sopenharmony_ci#define MT9V032_AGC_UPDATE_FREQUENCY			0xa9
1378c2ecf20Sopenharmony_ci#define MT9V032_AGC_LPF					0xaa
1388c2ecf20Sopenharmony_ci#define MT9V032_AEC_AGC_ENABLE				0xaf
1398c2ecf20Sopenharmony_ci#define		MT9V032_AEC_ENABLE			(1 << 0)
1408c2ecf20Sopenharmony_ci#define		MT9V032_AGC_ENABLE			(1 << 1)
1418c2ecf20Sopenharmony_ci#define MT9V034_AEC_MAX_SHUTTER_WIDTH			0xad
1428c2ecf20Sopenharmony_ci#define MT9V032_AEC_MAX_SHUTTER_WIDTH			0xbd
1438c2ecf20Sopenharmony_ci#define MT9V032_THERMAL_INFO				0xc1
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cienum mt9v032_model {
1468c2ecf20Sopenharmony_ci	MT9V032_MODEL_V022_COLOR,	/* MT9V022IX7ATC */
1478c2ecf20Sopenharmony_ci	MT9V032_MODEL_V022_MONO,	/* MT9V022IX7ATM */
1488c2ecf20Sopenharmony_ci	MT9V032_MODEL_V024_COLOR,	/* MT9V024IA7XTC */
1498c2ecf20Sopenharmony_ci	MT9V032_MODEL_V024_MONO,	/* MT9V024IA7XTM */
1508c2ecf20Sopenharmony_ci	MT9V032_MODEL_V032_COLOR,	/* MT9V032C12STM */
1518c2ecf20Sopenharmony_ci	MT9V032_MODEL_V032_MONO,	/* MT9V032C12STC */
1528c2ecf20Sopenharmony_ci	MT9V032_MODEL_V034_COLOR,
1538c2ecf20Sopenharmony_ci	MT9V032_MODEL_V034_MONO,
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistruct mt9v032_model_version {
1578c2ecf20Sopenharmony_ci	unsigned int version;
1588c2ecf20Sopenharmony_ci	const char *name;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistruct mt9v032_model_data {
1628c2ecf20Sopenharmony_ci	unsigned int min_row_time;
1638c2ecf20Sopenharmony_ci	unsigned int min_hblank;
1648c2ecf20Sopenharmony_ci	unsigned int min_vblank;
1658c2ecf20Sopenharmony_ci	unsigned int max_vblank;
1668c2ecf20Sopenharmony_ci	unsigned int min_shutter;
1678c2ecf20Sopenharmony_ci	unsigned int max_shutter;
1688c2ecf20Sopenharmony_ci	unsigned int pclk_reg;
1698c2ecf20Sopenharmony_ci	unsigned int aec_max_shutter_reg;
1708c2ecf20Sopenharmony_ci	const struct v4l2_ctrl_config * const aec_max_shutter_v4l2_ctrl;
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct mt9v032_model_info {
1748c2ecf20Sopenharmony_ci	const struct mt9v032_model_data *data;
1758c2ecf20Sopenharmony_ci	bool color;
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic const struct mt9v032_model_version mt9v032_versions[] = {
1798c2ecf20Sopenharmony_ci	{ MT9V032_CHIP_ID_REV1, "MT9V022/MT9V032 rev1/2" },
1808c2ecf20Sopenharmony_ci	{ MT9V032_CHIP_ID_REV3, "MT9V022/MT9V032 rev3" },
1818c2ecf20Sopenharmony_ci	{ MT9V034_CHIP_ID_REV1, "MT9V024/MT9V034 rev1" },
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistruct mt9v032 {
1858c2ecf20Sopenharmony_ci	struct v4l2_subdev subdev;
1868c2ecf20Sopenharmony_ci	struct media_pad pad;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt format;
1898c2ecf20Sopenharmony_ci	struct v4l2_rect crop;
1908c2ecf20Sopenharmony_ci	unsigned int hratio;
1918c2ecf20Sopenharmony_ci	unsigned int vratio;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler ctrls;
1948c2ecf20Sopenharmony_ci	struct {
1958c2ecf20Sopenharmony_ci		struct v4l2_ctrl *link_freq;
1968c2ecf20Sopenharmony_ci		struct v4l2_ctrl *pixel_rate;
1978c2ecf20Sopenharmony_ci	};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	struct mutex power_lock;
2008c2ecf20Sopenharmony_ci	int power_count;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	struct regmap *regmap;
2038c2ecf20Sopenharmony_ci	struct clk *clk;
2048c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
2058c2ecf20Sopenharmony_ci	struct gpio_desc *standby_gpio;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	struct mt9v032_platform_data *pdata;
2088c2ecf20Sopenharmony_ci	const struct mt9v032_model_info *model;
2098c2ecf20Sopenharmony_ci	const struct mt9v032_model_version *version;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	u32 sysclk;
2128c2ecf20Sopenharmony_ci	u16 aec_agc;
2138c2ecf20Sopenharmony_ci	u16 hblank;
2148c2ecf20Sopenharmony_ci	struct {
2158c2ecf20Sopenharmony_ci		struct v4l2_ctrl *test_pattern;
2168c2ecf20Sopenharmony_ci		struct v4l2_ctrl *test_pattern_color;
2178c2ecf20Sopenharmony_ci	};
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic struct mt9v032 *to_mt9v032(struct v4l2_subdev *sd)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return container_of(sd, struct mt9v032, subdev);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int
2268c2ecf20Sopenharmony_cimt9v032_update_aec_agc(struct mt9v032 *mt9v032, u16 which, int enable)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct regmap *map = mt9v032->regmap;
2298c2ecf20Sopenharmony_ci	u16 value = mt9v032->aec_agc;
2308c2ecf20Sopenharmony_ci	int ret;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (enable)
2338c2ecf20Sopenharmony_ci		value |= which;
2348c2ecf20Sopenharmony_ci	else
2358c2ecf20Sopenharmony_ci		value &= ~which;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_AEC_AGC_ENABLE, value);
2388c2ecf20Sopenharmony_ci	if (ret < 0)
2398c2ecf20Sopenharmony_ci		return ret;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	mt9v032->aec_agc = value;
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int
2468c2ecf20Sopenharmony_cimt9v032_update_hblank(struct mt9v032 *mt9v032)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct v4l2_rect *crop = &mt9v032->crop;
2498c2ecf20Sopenharmony_ci	unsigned int min_hblank = mt9v032->model->data->min_hblank;
2508c2ecf20Sopenharmony_ci	unsigned int hblank;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	if (mt9v032->version->version == MT9V034_CHIP_ID_REV1)
2538c2ecf20Sopenharmony_ci		min_hblank += (mt9v032->hratio - 1) * 10;
2548c2ecf20Sopenharmony_ci	min_hblank = max_t(int, mt9v032->model->data->min_row_time - crop->width,
2558c2ecf20Sopenharmony_ci			   min_hblank);
2568c2ecf20Sopenharmony_ci	hblank = max_t(unsigned int, mt9v032->hblank, min_hblank);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	return regmap_write(mt9v032->regmap, MT9V032_HORIZONTAL_BLANKING,
2598c2ecf20Sopenharmony_ci			    hblank);
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic int mt9v032_power_on(struct mt9v032 *mt9v032)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	struct regmap *map = mt9v032->regmap;
2658c2ecf20Sopenharmony_ci	int ret;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(mt9v032->reset_gpio, 1);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk);
2708c2ecf20Sopenharmony_ci	if (ret < 0)
2718c2ecf20Sopenharmony_ci		return ret;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	/* System clock has to be enabled before releasing the reset */
2748c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(mt9v032->clk);
2758c2ecf20Sopenharmony_ci	if (ret)
2768c2ecf20Sopenharmony_ci		return ret;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	udelay(1);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	if (mt9v032->reset_gpio) {
2818c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(mt9v032->reset_gpio, 0);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci		/* After releasing reset we need to wait 10 clock cycles
2848c2ecf20Sopenharmony_ci		 * before accessing the sensor over I2C. As the minimum SYSCLK
2858c2ecf20Sopenharmony_ci		 * frequency is 13MHz, waiting 1µs will be enough in the worst
2868c2ecf20Sopenharmony_ci		 * case.
2878c2ecf20Sopenharmony_ci		 */
2888c2ecf20Sopenharmony_ci		udelay(1);
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Reset the chip and stop data read out */
2928c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_RESET, 1);
2938c2ecf20Sopenharmony_ci	if (ret < 0)
2948c2ecf20Sopenharmony_ci		goto err;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_RESET, 0);
2978c2ecf20Sopenharmony_ci	if (ret < 0)
2988c2ecf20Sopenharmony_ci		goto err;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_CHIP_CONTROL,
3018c2ecf20Sopenharmony_ci			   MT9V032_CHIP_CONTROL_MASTER_MODE);
3028c2ecf20Sopenharmony_ci	if (ret < 0)
3038c2ecf20Sopenharmony_ci		goto err;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return 0;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cierr:
3088c2ecf20Sopenharmony_ci	clk_disable_unprepare(mt9v032->clk);
3098c2ecf20Sopenharmony_ci	return ret;
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic void mt9v032_power_off(struct mt9v032 *mt9v032)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	clk_disable_unprepare(mt9v032->clk);
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int __mt9v032_set_power(struct mt9v032 *mt9v032, bool on)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct regmap *map = mt9v032->regmap;
3208c2ecf20Sopenharmony_ci	int ret;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (!on) {
3238c2ecf20Sopenharmony_ci		mt9v032_power_off(mt9v032);
3248c2ecf20Sopenharmony_ci		return 0;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	ret = mt9v032_power_on(mt9v032);
3288c2ecf20Sopenharmony_ci	if (ret < 0)
3298c2ecf20Sopenharmony_ci		return ret;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/* Configure the pixel clock polarity */
3328c2ecf20Sopenharmony_ci	if (mt9v032->pdata && mt9v032->pdata->clk_pol) {
3338c2ecf20Sopenharmony_ci		ret = regmap_write(map, mt9v032->model->data->pclk_reg,
3348c2ecf20Sopenharmony_ci				MT9V032_PIXEL_CLOCK_INV_PXL_CLK);
3358c2ecf20Sopenharmony_ci		if (ret < 0)
3368c2ecf20Sopenharmony_ci			return ret;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* Disable the noise correction algorithm and restore the controls. */
3408c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_ROW_NOISE_CORR_CONTROL, 0);
3418c2ecf20Sopenharmony_ci	if (ret < 0)
3428c2ecf20Sopenharmony_ci		return ret;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return v4l2_ctrl_handler_setup(&mt9v032->ctrls);
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
3488c2ecf20Sopenharmony_ci * V4L2 subdev video operations
3498c2ecf20Sopenharmony_ci */
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic struct v4l2_mbus_framefmt *
3528c2ecf20Sopenharmony_ci__mt9v032_get_pad_format(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
3538c2ecf20Sopenharmony_ci			 unsigned int pad, enum v4l2_subdev_format_whence which)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	switch (which) {
3568c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
3578c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_format(&mt9v032->subdev, cfg, pad);
3588c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
3598c2ecf20Sopenharmony_ci		return &mt9v032->format;
3608c2ecf20Sopenharmony_ci	default:
3618c2ecf20Sopenharmony_ci		return NULL;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic struct v4l2_rect *
3668c2ecf20Sopenharmony_ci__mt9v032_get_pad_crop(struct mt9v032 *mt9v032, struct v4l2_subdev_pad_config *cfg,
3678c2ecf20Sopenharmony_ci		       unsigned int pad, enum v4l2_subdev_format_whence which)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	switch (which) {
3708c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
3718c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_crop(&mt9v032->subdev, cfg, pad);
3728c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
3738c2ecf20Sopenharmony_ci		return &mt9v032->crop;
3748c2ecf20Sopenharmony_ci	default:
3758c2ecf20Sopenharmony_ci		return NULL;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic int mt9v032_s_stream(struct v4l2_subdev *subdev, int enable)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	const u16 mode = MT9V032_CHIP_CONTROL_DOUT_ENABLE
3828c2ecf20Sopenharmony_ci		       | MT9V032_CHIP_CONTROL_SEQUENTIAL;
3838c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
3848c2ecf20Sopenharmony_ci	struct v4l2_rect *crop = &mt9v032->crop;
3858c2ecf20Sopenharmony_ci	struct regmap *map = mt9v032->regmap;
3868c2ecf20Sopenharmony_ci	unsigned int hbin;
3878c2ecf20Sopenharmony_ci	unsigned int vbin;
3888c2ecf20Sopenharmony_ci	int ret;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	if (!enable)
3918c2ecf20Sopenharmony_ci		return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, 0);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	/* Configure the window size and row/column bin */
3948c2ecf20Sopenharmony_ci	hbin = fls(mt9v032->hratio) - 1;
3958c2ecf20Sopenharmony_ci	vbin = fls(mt9v032->vratio) - 1;
3968c2ecf20Sopenharmony_ci	ret = regmap_update_bits(map, MT9V032_READ_MODE,
3978c2ecf20Sopenharmony_ci				 ~MT9V032_READ_MODE_RESERVED,
3988c2ecf20Sopenharmony_ci				 hbin << MT9V032_READ_MODE_COLUMN_BIN_SHIFT |
3998c2ecf20Sopenharmony_ci				 vbin << MT9V032_READ_MODE_ROW_BIN_SHIFT);
4008c2ecf20Sopenharmony_ci	if (ret < 0)
4018c2ecf20Sopenharmony_ci		return ret;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_COLUMN_START, crop->left);
4048c2ecf20Sopenharmony_ci	if (ret < 0)
4058c2ecf20Sopenharmony_ci		return ret;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_ROW_START, crop->top);
4088c2ecf20Sopenharmony_ci	if (ret < 0)
4098c2ecf20Sopenharmony_ci		return ret;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_WINDOW_WIDTH, crop->width);
4128c2ecf20Sopenharmony_ci	if (ret < 0)
4138c2ecf20Sopenharmony_ci		return ret;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	ret = regmap_write(map, MT9V032_WINDOW_HEIGHT, crop->height);
4168c2ecf20Sopenharmony_ci	if (ret < 0)
4178c2ecf20Sopenharmony_ci		return ret;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	ret = mt9v032_update_hblank(mt9v032);
4208c2ecf20Sopenharmony_ci	if (ret < 0)
4218c2ecf20Sopenharmony_ci		return ret;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	/* Switch to master "normal" mode */
4248c2ecf20Sopenharmony_ci	return regmap_update_bits(map, MT9V032_CHIP_CONTROL, mode, mode);
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
4288c2ecf20Sopenharmony_ci				  struct v4l2_subdev_pad_config *cfg,
4298c2ecf20Sopenharmony_ci				  struct v4l2_subdev_mbus_code_enum *code)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (code->index > 0)
4348c2ecf20Sopenharmony_ci		return -EINVAL;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	code->code = mt9v032->format.code;
4378c2ecf20Sopenharmony_ci	return 0;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
4418c2ecf20Sopenharmony_ci				   struct v4l2_subdev_pad_config *cfg,
4428c2ecf20Sopenharmony_ci				   struct v4l2_subdev_frame_size_enum *fse)
4438c2ecf20Sopenharmony_ci{
4448c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	if (fse->index >= 3)
4478c2ecf20Sopenharmony_ci		return -EINVAL;
4488c2ecf20Sopenharmony_ci	if (mt9v032->format.code != fse->code)
4498c2ecf20Sopenharmony_ci		return -EINVAL;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
4528c2ecf20Sopenharmony_ci	fse->max_width = fse->min_width;
4538c2ecf20Sopenharmony_ci	fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index);
4548c2ecf20Sopenharmony_ci	fse->max_height = fse->min_height;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	return 0;
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic int mt9v032_get_format(struct v4l2_subdev *subdev,
4608c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
4618c2ecf20Sopenharmony_ci			      struct v4l2_subdev_format *format)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	format->format = *__mt9v032_get_pad_format(mt9v032, cfg, format->pad,
4668c2ecf20Sopenharmony_ci						   format->which);
4678c2ecf20Sopenharmony_ci	return 0;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic void mt9v032_configure_pixel_rate(struct mt9v032 *mt9v032)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev);
4738c2ecf20Sopenharmony_ci	int ret;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate,
4768c2ecf20Sopenharmony_ci				     mt9v032->sysclk / mt9v032->hratio);
4778c2ecf20Sopenharmony_ci	if (ret < 0)
4788c2ecf20Sopenharmony_ci		dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret);
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	/* Compute the power-of-two binning factor closest to the input size to
4848c2ecf20Sopenharmony_ci	 * output size ratio. Given that the output size is bounded by input/4
4858c2ecf20Sopenharmony_ci	 * and input, a generic implementation would be an ineffective luxury.
4868c2ecf20Sopenharmony_ci	 */
4878c2ecf20Sopenharmony_ci	if (output * 3 > input * 2)
4888c2ecf20Sopenharmony_ci		return 1;
4898c2ecf20Sopenharmony_ci	if (output * 3 > input)
4908c2ecf20Sopenharmony_ci		return 2;
4918c2ecf20Sopenharmony_ci	return 4;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic int mt9v032_set_format(struct v4l2_subdev *subdev,
4958c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
4968c2ecf20Sopenharmony_ci			      struct v4l2_subdev_format *format)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
4998c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *__format;
5008c2ecf20Sopenharmony_ci	struct v4l2_rect *__crop;
5018c2ecf20Sopenharmony_ci	unsigned int width;
5028c2ecf20Sopenharmony_ci	unsigned int height;
5038c2ecf20Sopenharmony_ci	unsigned int hratio;
5048c2ecf20Sopenharmony_ci	unsigned int vratio;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	__crop = __mt9v032_get_pad_crop(mt9v032, cfg, format->pad,
5078c2ecf20Sopenharmony_ci					format->which);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	/* Clamp the width and height to avoid dividing by zero. */
5108c2ecf20Sopenharmony_ci	width = clamp(ALIGN(format->format.width, 2),
5118c2ecf20Sopenharmony_ci		      max_t(unsigned int, __crop->width / 4,
5128c2ecf20Sopenharmony_ci			    MT9V032_WINDOW_WIDTH_MIN),
5138c2ecf20Sopenharmony_ci		      __crop->width);
5148c2ecf20Sopenharmony_ci	height = clamp(ALIGN(format->format.height, 2),
5158c2ecf20Sopenharmony_ci		       max_t(unsigned int, __crop->height / 4,
5168c2ecf20Sopenharmony_ci			     MT9V032_WINDOW_HEIGHT_MIN),
5178c2ecf20Sopenharmony_ci		       __crop->height);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	hratio = mt9v032_calc_ratio(__crop->width, width);
5208c2ecf20Sopenharmony_ci	vratio = mt9v032_calc_ratio(__crop->height, height);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	__format = __mt9v032_get_pad_format(mt9v032, cfg, format->pad,
5238c2ecf20Sopenharmony_ci					    format->which);
5248c2ecf20Sopenharmony_ci	__format->width = __crop->width / hratio;
5258c2ecf20Sopenharmony_ci	__format->height = __crop->height / vratio;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
5288c2ecf20Sopenharmony_ci		mt9v032->hratio = hratio;
5298c2ecf20Sopenharmony_ci		mt9v032->vratio = vratio;
5308c2ecf20Sopenharmony_ci		mt9v032_configure_pixel_rate(mt9v032);
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	format->format = *__format;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return 0;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int mt9v032_get_selection(struct v4l2_subdev *subdev,
5398c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
5408c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP)
5458c2ecf20Sopenharmony_ci		return -EINVAL;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	sel->r = *__mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
5488c2ecf20Sopenharmony_ci	return 0;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cistatic int mt9v032_set_selection(struct v4l2_subdev *subdev,
5528c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
5538c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
5548c2ecf20Sopenharmony_ci{
5558c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
5568c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *__format;
5578c2ecf20Sopenharmony_ci	struct v4l2_rect *__crop;
5588c2ecf20Sopenharmony_ci	struct v4l2_rect rect;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP)
5618c2ecf20Sopenharmony_ci		return -EINVAL;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	/* Clamp the crop rectangle boundaries and align them to a non multiple
5648c2ecf20Sopenharmony_ci	 * of 2 pixels to ensure a GRBG Bayer pattern.
5658c2ecf20Sopenharmony_ci	 */
5668c2ecf20Sopenharmony_ci	rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1,
5678c2ecf20Sopenharmony_ci			  MT9V032_COLUMN_START_MIN,
5688c2ecf20Sopenharmony_ci			  MT9V032_COLUMN_START_MAX);
5698c2ecf20Sopenharmony_ci	rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1,
5708c2ecf20Sopenharmony_ci			 MT9V032_ROW_START_MIN,
5718c2ecf20Sopenharmony_ci			 MT9V032_ROW_START_MAX);
5728c2ecf20Sopenharmony_ci	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
5738c2ecf20Sopenharmony_ci			     MT9V032_WINDOW_WIDTH_MIN,
5748c2ecf20Sopenharmony_ci			     MT9V032_WINDOW_WIDTH_MAX);
5758c2ecf20Sopenharmony_ci	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
5768c2ecf20Sopenharmony_ci			      MT9V032_WINDOW_HEIGHT_MIN,
5778c2ecf20Sopenharmony_ci			      MT9V032_WINDOW_HEIGHT_MAX);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	rect.width = min_t(unsigned int,
5808c2ecf20Sopenharmony_ci			   rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left);
5818c2ecf20Sopenharmony_ci	rect.height = min_t(unsigned int,
5828c2ecf20Sopenharmony_ci			    rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	__crop = __mt9v032_get_pad_crop(mt9v032, cfg, sel->pad, sel->which);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	if (rect.width != __crop->width || rect.height != __crop->height) {
5878c2ecf20Sopenharmony_ci		/* Reset the output image size if the crop rectangle size has
5888c2ecf20Sopenharmony_ci		 * been modified.
5898c2ecf20Sopenharmony_ci		 */
5908c2ecf20Sopenharmony_ci		__format = __mt9v032_get_pad_format(mt9v032, cfg, sel->pad,
5918c2ecf20Sopenharmony_ci						    sel->which);
5928c2ecf20Sopenharmony_ci		__format->width = rect.width;
5938c2ecf20Sopenharmony_ci		__format->height = rect.height;
5948c2ecf20Sopenharmony_ci		if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
5958c2ecf20Sopenharmony_ci			mt9v032->hratio = 1;
5968c2ecf20Sopenharmony_ci			mt9v032->vratio = 1;
5978c2ecf20Sopenharmony_ci			mt9v032_configure_pixel_rate(mt9v032);
5988c2ecf20Sopenharmony_ci		}
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	*__crop = rect;
6028c2ecf20Sopenharmony_ci	sel->r = rect;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	return 0;
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
6088c2ecf20Sopenharmony_ci * V4L2 subdev control operations
6098c2ecf20Sopenharmony_ci */
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci#define V4L2_CID_TEST_PATTERN_COLOR	(V4L2_CID_USER_BASE | 0x1001)
6128c2ecf20Sopenharmony_ci/*
6138c2ecf20Sopenharmony_ci * Value between 1 and 64 to set the desired bin. This is effectively a measure
6148c2ecf20Sopenharmony_ci * of how bright the image is supposed to be. Both AGC and AEC try to reach
6158c2ecf20Sopenharmony_ci * this.
6168c2ecf20Sopenharmony_ci */
6178c2ecf20Sopenharmony_ci#define V4L2_CID_AEGC_DESIRED_BIN	(V4L2_CID_USER_BASE | 0x1002)
6188c2ecf20Sopenharmony_ci/*
6198c2ecf20Sopenharmony_ci * LPF is the low pass filter capability of the chip. Both AEC and AGC have
6208c2ecf20Sopenharmony_ci * this setting. This limits the speed in which AGC/AEC adjust their settings.
6218c2ecf20Sopenharmony_ci * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
6228c2ecf20Sopenharmony_ci *
6238c2ecf20Sopenharmony_ci * if |(calculated new exp - current exp)| > (current exp / 4)
6248c2ecf20Sopenharmony_ci *	next exp = calculated new exp
6258c2ecf20Sopenharmony_ci * else
6268c2ecf20Sopenharmony_ci *	next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
6278c2ecf20Sopenharmony_ci */
6288c2ecf20Sopenharmony_ci#define V4L2_CID_AEC_LPF		(V4L2_CID_USER_BASE | 0x1003)
6298c2ecf20Sopenharmony_ci#define V4L2_CID_AGC_LPF		(V4L2_CID_USER_BASE | 0x1004)
6308c2ecf20Sopenharmony_ci/*
6318c2ecf20Sopenharmony_ci * Value between 0 and 15. This is the number of frames being skipped before
6328c2ecf20Sopenharmony_ci * updating the auto exposure/gain.
6338c2ecf20Sopenharmony_ci */
6348c2ecf20Sopenharmony_ci#define V4L2_CID_AEC_UPDATE_INTERVAL	(V4L2_CID_USER_BASE | 0x1005)
6358c2ecf20Sopenharmony_ci#define V4L2_CID_AGC_UPDATE_INTERVAL	(V4L2_CID_USER_BASE | 0x1006)
6368c2ecf20Sopenharmony_ci/*
6378c2ecf20Sopenharmony_ci * Maximum shutter width used for AEC.
6388c2ecf20Sopenharmony_ci */
6398c2ecf20Sopenharmony_ci#define V4L2_CID_AEC_MAX_SHUTTER_WIDTH	(V4L2_CID_USER_BASE | 0x1007)
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic int mt9v032_s_ctrl(struct v4l2_ctrl *ctrl)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 =
6448c2ecf20Sopenharmony_ci			container_of(ctrl->handler, struct mt9v032, ctrls);
6458c2ecf20Sopenharmony_ci	struct regmap *map = mt9v032->regmap;
6468c2ecf20Sopenharmony_ci	u32 freq;
6478c2ecf20Sopenharmony_ci	u16 data;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	switch (ctrl->id) {
6508c2ecf20Sopenharmony_ci	case V4L2_CID_AUTOGAIN:
6518c2ecf20Sopenharmony_ci		return mt9v032_update_aec_agc(mt9v032, MT9V032_AGC_ENABLE,
6528c2ecf20Sopenharmony_ci					      ctrl->val);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
6558c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_ANALOG_GAIN, ctrl->val);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE_AUTO:
6588c2ecf20Sopenharmony_ci		return mt9v032_update_aec_agc(mt9v032, MT9V032_AEC_ENABLE,
6598c2ecf20Sopenharmony_ci					      !ctrl->val);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE:
6628c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_TOTAL_SHUTTER_WIDTH,
6638c2ecf20Sopenharmony_ci				    ctrl->val);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	case V4L2_CID_HBLANK:
6668c2ecf20Sopenharmony_ci		mt9v032->hblank = ctrl->val;
6678c2ecf20Sopenharmony_ci		return mt9v032_update_hblank(mt9v032);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	case V4L2_CID_VBLANK:
6708c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_VERTICAL_BLANKING,
6718c2ecf20Sopenharmony_ci				    ctrl->val);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	case V4L2_CID_PIXEL_RATE:
6748c2ecf20Sopenharmony_ci	case V4L2_CID_LINK_FREQ:
6758c2ecf20Sopenharmony_ci		if (mt9v032->link_freq == NULL)
6768c2ecf20Sopenharmony_ci			break;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci		freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val];
6798c2ecf20Sopenharmony_ci		*mt9v032->pixel_rate->p_new.p_s64 = freq;
6808c2ecf20Sopenharmony_ci		mt9v032->sysclk = freq;
6818c2ecf20Sopenharmony_ci		break;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	case V4L2_CID_TEST_PATTERN:
6848c2ecf20Sopenharmony_ci		switch (mt9v032->test_pattern->val) {
6858c2ecf20Sopenharmony_ci		case 0:
6868c2ecf20Sopenharmony_ci			data = 0;
6878c2ecf20Sopenharmony_ci			break;
6888c2ecf20Sopenharmony_ci		case 1:
6898c2ecf20Sopenharmony_ci			data = MT9V032_TEST_PATTERN_GRAY_VERTICAL
6908c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_ENABLE;
6918c2ecf20Sopenharmony_ci			break;
6928c2ecf20Sopenharmony_ci		case 2:
6938c2ecf20Sopenharmony_ci			data = MT9V032_TEST_PATTERN_GRAY_HORIZONTAL
6948c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_ENABLE;
6958c2ecf20Sopenharmony_ci			break;
6968c2ecf20Sopenharmony_ci		case 3:
6978c2ecf20Sopenharmony_ci			data = MT9V032_TEST_PATTERN_GRAY_DIAGONAL
6988c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_ENABLE;
6998c2ecf20Sopenharmony_ci			break;
7008c2ecf20Sopenharmony_ci		default:
7018c2ecf20Sopenharmony_ci			data = (mt9v032->test_pattern_color->val <<
7028c2ecf20Sopenharmony_ci				MT9V032_TEST_PATTERN_DATA_SHIFT)
7038c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_USE_DATA
7048c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_ENABLE
7058c2ecf20Sopenharmony_ci			     | MT9V032_TEST_PATTERN_FLIP;
7068c2ecf20Sopenharmony_ci			break;
7078c2ecf20Sopenharmony_ci		}
7088c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_TEST_PATTERN, data);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	case V4L2_CID_AEGC_DESIRED_BIN:
7118c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	case V4L2_CID_AEC_LPF:
7148c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_AEC_LPF, ctrl->val);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	case V4L2_CID_AGC_LPF:
7178c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_AGC_LPF, ctrl->val);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	case V4L2_CID_AEC_UPDATE_INTERVAL:
7208c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_AEC_UPDATE_FREQUENCY,
7218c2ecf20Sopenharmony_ci				    ctrl->val);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	case V4L2_CID_AGC_UPDATE_INTERVAL:
7248c2ecf20Sopenharmony_ci		return regmap_write(map, MT9V032_AGC_UPDATE_FREQUENCY,
7258c2ecf20Sopenharmony_ci				    ctrl->val);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	case V4L2_CID_AEC_MAX_SHUTTER_WIDTH:
7288c2ecf20Sopenharmony_ci		return regmap_write(map,
7298c2ecf20Sopenharmony_ci				    mt9v032->model->data->aec_max_shutter_reg,
7308c2ecf20Sopenharmony_ci				    ctrl->val);
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	return 0;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops mt9v032_ctrl_ops = {
7378c2ecf20Sopenharmony_ci	.s_ctrl = mt9v032_s_ctrl,
7388c2ecf20Sopenharmony_ci};
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cistatic const char * const mt9v032_test_pattern_menu[] = {
7418c2ecf20Sopenharmony_ci	"Disabled",
7428c2ecf20Sopenharmony_ci	"Gray Vertical Shade",
7438c2ecf20Sopenharmony_ci	"Gray Horizontal Shade",
7448c2ecf20Sopenharmony_ci	"Gray Diagonal Shade",
7458c2ecf20Sopenharmony_ci	"Plain",
7468c2ecf20Sopenharmony_ci};
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9v032_test_pattern_color = {
7498c2ecf20Sopenharmony_ci	.ops		= &mt9v032_ctrl_ops,
7508c2ecf20Sopenharmony_ci	.id		= V4L2_CID_TEST_PATTERN_COLOR,
7518c2ecf20Sopenharmony_ci	.type		= V4L2_CTRL_TYPE_INTEGER,
7528c2ecf20Sopenharmony_ci	.name		= "Test Pattern Color",
7538c2ecf20Sopenharmony_ci	.min		= 0,
7548c2ecf20Sopenharmony_ci	.max		= 1023,
7558c2ecf20Sopenharmony_ci	.step		= 1,
7568c2ecf20Sopenharmony_ci	.def		= 0,
7578c2ecf20Sopenharmony_ci	.flags		= 0,
7588c2ecf20Sopenharmony_ci};
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9v032_aegc_controls[] = {
7618c2ecf20Sopenharmony_ci	{
7628c2ecf20Sopenharmony_ci		.ops		= &mt9v032_ctrl_ops,
7638c2ecf20Sopenharmony_ci		.id		= V4L2_CID_AEGC_DESIRED_BIN,
7648c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7658c2ecf20Sopenharmony_ci		.name		= "AEC/AGC Desired Bin",
7668c2ecf20Sopenharmony_ci		.min		= 1,
7678c2ecf20Sopenharmony_ci		.max		= 64,
7688c2ecf20Sopenharmony_ci		.step		= 1,
7698c2ecf20Sopenharmony_ci		.def		= 58,
7708c2ecf20Sopenharmony_ci		.flags		= 0,
7718c2ecf20Sopenharmony_ci	}, {
7728c2ecf20Sopenharmony_ci		.ops		= &mt9v032_ctrl_ops,
7738c2ecf20Sopenharmony_ci		.id		= V4L2_CID_AEC_LPF,
7748c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7758c2ecf20Sopenharmony_ci		.name		= "AEC Low Pass Filter",
7768c2ecf20Sopenharmony_ci		.min		= 0,
7778c2ecf20Sopenharmony_ci		.max		= 2,
7788c2ecf20Sopenharmony_ci		.step		= 1,
7798c2ecf20Sopenharmony_ci		.def		= 0,
7808c2ecf20Sopenharmony_ci		.flags		= 0,
7818c2ecf20Sopenharmony_ci	}, {
7828c2ecf20Sopenharmony_ci		.ops		= &mt9v032_ctrl_ops,
7838c2ecf20Sopenharmony_ci		.id		= V4L2_CID_AGC_LPF,
7848c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7858c2ecf20Sopenharmony_ci		.name		= "AGC Low Pass Filter",
7868c2ecf20Sopenharmony_ci		.min		= 0,
7878c2ecf20Sopenharmony_ci		.max		= 2,
7888c2ecf20Sopenharmony_ci		.step		= 1,
7898c2ecf20Sopenharmony_ci		.def		= 2,
7908c2ecf20Sopenharmony_ci		.flags		= 0,
7918c2ecf20Sopenharmony_ci	}, {
7928c2ecf20Sopenharmony_ci		.ops		= &mt9v032_ctrl_ops,
7938c2ecf20Sopenharmony_ci		.id		= V4L2_CID_AEC_UPDATE_INTERVAL,
7948c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7958c2ecf20Sopenharmony_ci		.name		= "AEC Update Interval",
7968c2ecf20Sopenharmony_ci		.min		= 0,
7978c2ecf20Sopenharmony_ci		.max		= 16,
7988c2ecf20Sopenharmony_ci		.step		= 1,
7998c2ecf20Sopenharmony_ci		.def		= 2,
8008c2ecf20Sopenharmony_ci		.flags		= 0,
8018c2ecf20Sopenharmony_ci	}, {
8028c2ecf20Sopenharmony_ci		.ops		= &mt9v032_ctrl_ops,
8038c2ecf20Sopenharmony_ci		.id		= V4L2_CID_AGC_UPDATE_INTERVAL,
8048c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
8058c2ecf20Sopenharmony_ci		.name		= "AGC Update Interval",
8068c2ecf20Sopenharmony_ci		.min		= 0,
8078c2ecf20Sopenharmony_ci		.max		= 16,
8088c2ecf20Sopenharmony_ci		.step		= 1,
8098c2ecf20Sopenharmony_ci		.def		= 2,
8108c2ecf20Sopenharmony_ci		.flags		= 0,
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci};
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9v032_aec_max_shutter_width = {
8158c2ecf20Sopenharmony_ci	.ops		= &mt9v032_ctrl_ops,
8168c2ecf20Sopenharmony_ci	.id		= V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
8178c2ecf20Sopenharmony_ci	.type		= V4L2_CTRL_TYPE_INTEGER,
8188c2ecf20Sopenharmony_ci	.name		= "AEC Max Shutter Width",
8198c2ecf20Sopenharmony_ci	.min		= 1,
8208c2ecf20Sopenharmony_ci	.max		= 2047,
8218c2ecf20Sopenharmony_ci	.step		= 1,
8228c2ecf20Sopenharmony_ci	.def		= 480,
8238c2ecf20Sopenharmony_ci	.flags		= 0,
8248c2ecf20Sopenharmony_ci};
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9v034_aec_max_shutter_width = {
8278c2ecf20Sopenharmony_ci	.ops		= &mt9v032_ctrl_ops,
8288c2ecf20Sopenharmony_ci	.id		= V4L2_CID_AEC_MAX_SHUTTER_WIDTH,
8298c2ecf20Sopenharmony_ci	.type		= V4L2_CTRL_TYPE_INTEGER,
8308c2ecf20Sopenharmony_ci	.name		= "AEC Max Shutter Width",
8318c2ecf20Sopenharmony_ci	.min		= 1,
8328c2ecf20Sopenharmony_ci	.max		= 32765,
8338c2ecf20Sopenharmony_ci	.step		= 1,
8348c2ecf20Sopenharmony_ci	.def		= 480,
8358c2ecf20Sopenharmony_ci	.flags		= 0,
8368c2ecf20Sopenharmony_ci};
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
8398c2ecf20Sopenharmony_ci * V4L2 subdev core operations
8408c2ecf20Sopenharmony_ci */
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_cistatic int mt9v032_set_power(struct v4l2_subdev *subdev, int on)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
8458c2ecf20Sopenharmony_ci	int ret = 0;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	mutex_lock(&mt9v032->power_lock);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
8508c2ecf20Sopenharmony_ci	 * update the power state.
8518c2ecf20Sopenharmony_ci	 */
8528c2ecf20Sopenharmony_ci	if (mt9v032->power_count == !on) {
8538c2ecf20Sopenharmony_ci		ret = __mt9v032_set_power(mt9v032, !!on);
8548c2ecf20Sopenharmony_ci		if (ret < 0)
8558c2ecf20Sopenharmony_ci			goto done;
8568c2ecf20Sopenharmony_ci	}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	/* Update the power count. */
8598c2ecf20Sopenharmony_ci	mt9v032->power_count += on ? 1 : -1;
8608c2ecf20Sopenharmony_ci	WARN_ON(mt9v032->power_count < 0);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_cidone:
8638c2ecf20Sopenharmony_ci	mutex_unlock(&mt9v032->power_lock);
8648c2ecf20Sopenharmony_ci	return ret;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
8688c2ecf20Sopenharmony_ci * V4L2 subdev internal operations
8698c2ecf20Sopenharmony_ci */
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic int mt9v032_registered(struct v4l2_subdev *subdev)
8728c2ecf20Sopenharmony_ci{
8738c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(subdev);
8748c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
8758c2ecf20Sopenharmony_ci	unsigned int i;
8768c2ecf20Sopenharmony_ci	u32 version;
8778c2ecf20Sopenharmony_ci	int ret;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n",
8808c2ecf20Sopenharmony_ci			client->addr);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	ret = mt9v032_power_on(mt9v032);
8838c2ecf20Sopenharmony_ci	if (ret < 0) {
8848c2ecf20Sopenharmony_ci		dev_err(&client->dev, "MT9V032 power up failed\n");
8858c2ecf20Sopenharmony_ci		return ret;
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	/* Read and check the sensor version */
8898c2ecf20Sopenharmony_ci	ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	mt9v032_power_off(mt9v032);
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	if (ret < 0) {
8948c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed reading chip version\n");
8958c2ecf20Sopenharmony_ci		return ret;
8968c2ecf20Sopenharmony_ci	}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mt9v032_versions); ++i) {
8998c2ecf20Sopenharmony_ci		if (mt9v032_versions[i].version == version) {
9008c2ecf20Sopenharmony_ci			mt9v032->version = &mt9v032_versions[i];
9018c2ecf20Sopenharmony_ci			break;
9028c2ecf20Sopenharmony_ci		}
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	if (mt9v032->version == NULL) {
9068c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unsupported chip version 0x%04x\n",
9078c2ecf20Sopenharmony_ci			version);
9088c2ecf20Sopenharmony_ci		return -ENODEV;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	dev_info(&client->dev, "%s detected at address 0x%02x\n",
9128c2ecf20Sopenharmony_ci		 mt9v032->version->name, client->addr);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	mt9v032_configure_pixel_rate(mt9v032);
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	return ret;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic int mt9v032_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
9228c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
9238c2ecf20Sopenharmony_ci	struct v4l2_rect *crop;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
9268c2ecf20Sopenharmony_ci	crop->left = MT9V032_COLUMN_START_DEF;
9278c2ecf20Sopenharmony_ci	crop->top = MT9V032_ROW_START_DEF;
9288c2ecf20Sopenharmony_ci	crop->width = MT9V032_WINDOW_WIDTH_DEF;
9298c2ecf20Sopenharmony_ci	crop->height = MT9V032_WINDOW_HEIGHT_DEF;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	if (mt9v032->model->color)
9348c2ecf20Sopenharmony_ci		format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
9358c2ecf20Sopenharmony_ci	else
9368c2ecf20Sopenharmony_ci		format->code = MEDIA_BUS_FMT_Y10_1X10;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	format->width = MT9V032_WINDOW_WIDTH_DEF;
9398c2ecf20Sopenharmony_ci	format->height = MT9V032_WINDOW_HEIGHT_DEF;
9408c2ecf20Sopenharmony_ci	format->field = V4L2_FIELD_NONE;
9418c2ecf20Sopenharmony_ci	format->colorspace = V4L2_COLORSPACE_SRGB;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	return mt9v032_set_power(subdev, 1);
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_cistatic int mt9v032_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	return mt9v032_set_power(subdev, 0);
9498c2ecf20Sopenharmony_ci}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops mt9v032_subdev_core_ops = {
9528c2ecf20Sopenharmony_ci	.s_power	= mt9v032_set_power,
9538c2ecf20Sopenharmony_ci};
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops mt9v032_subdev_video_ops = {
9568c2ecf20Sopenharmony_ci	.s_stream	= mt9v032_s_stream,
9578c2ecf20Sopenharmony_ci};
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops mt9v032_subdev_pad_ops = {
9608c2ecf20Sopenharmony_ci	.enum_mbus_code = mt9v032_enum_mbus_code,
9618c2ecf20Sopenharmony_ci	.enum_frame_size = mt9v032_enum_frame_size,
9628c2ecf20Sopenharmony_ci	.get_fmt = mt9v032_get_format,
9638c2ecf20Sopenharmony_ci	.set_fmt = mt9v032_set_format,
9648c2ecf20Sopenharmony_ci	.get_selection = mt9v032_get_selection,
9658c2ecf20Sopenharmony_ci	.set_selection = mt9v032_set_selection,
9668c2ecf20Sopenharmony_ci};
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops mt9v032_subdev_ops = {
9698c2ecf20Sopenharmony_ci	.core	= &mt9v032_subdev_core_ops,
9708c2ecf20Sopenharmony_ci	.video	= &mt9v032_subdev_video_ops,
9718c2ecf20Sopenharmony_ci	.pad	= &mt9v032_subdev_pad_ops,
9728c2ecf20Sopenharmony_ci};
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops mt9v032_subdev_internal_ops = {
9758c2ecf20Sopenharmony_ci	.registered = mt9v032_registered,
9768c2ecf20Sopenharmony_ci	.open = mt9v032_open,
9778c2ecf20Sopenharmony_ci	.close = mt9v032_close,
9788c2ecf20Sopenharmony_ci};
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cistatic const struct regmap_config mt9v032_regmap_config = {
9818c2ecf20Sopenharmony_ci	.reg_bits = 8,
9828c2ecf20Sopenharmony_ci	.val_bits = 16,
9838c2ecf20Sopenharmony_ci	.max_register = 0xff,
9848c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
9858c2ecf20Sopenharmony_ci};
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
9888c2ecf20Sopenharmony_ci * Driver initialization and probing
9898c2ecf20Sopenharmony_ci */
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_cistatic struct mt9v032_platform_data *
9928c2ecf20Sopenharmony_cimt9v032_get_pdata(struct i2c_client *client)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	struct mt9v032_platform_data *pdata = NULL;
9958c2ecf20Sopenharmony_ci	struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
9968c2ecf20Sopenharmony_ci	struct device_node *np;
9978c2ecf20Sopenharmony_ci	struct property *prop;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
10008c2ecf20Sopenharmony_ci		return client->dev.platform_data;
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
10038c2ecf20Sopenharmony_ci	if (!np)
10048c2ecf20Sopenharmony_ci		return NULL;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	if (v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &endpoint) < 0)
10078c2ecf20Sopenharmony_ci		goto done;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
10108c2ecf20Sopenharmony_ci	if (!pdata)
10118c2ecf20Sopenharmony_ci		goto done;
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	prop = of_find_property(np, "link-frequencies", NULL);
10148c2ecf20Sopenharmony_ci	if (prop) {
10158c2ecf20Sopenharmony_ci		u64 *link_freqs;
10168c2ecf20Sopenharmony_ci		size_t size = prop->length / sizeof(*link_freqs);
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci		link_freqs = devm_kcalloc(&client->dev, size,
10198c2ecf20Sopenharmony_ci					  sizeof(*link_freqs), GFP_KERNEL);
10208c2ecf20Sopenharmony_ci		if (!link_freqs)
10218c2ecf20Sopenharmony_ci			goto done;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci		if (of_property_read_u64_array(np, "link-frequencies",
10248c2ecf20Sopenharmony_ci					       link_freqs, size) < 0)
10258c2ecf20Sopenharmony_ci			goto done;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci		pdata->link_freqs = link_freqs;
10288c2ecf20Sopenharmony_ci		pdata->link_def_freq = link_freqs[0];
10298c2ecf20Sopenharmony_ci	}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	pdata->clk_pol = !!(endpoint.bus.parallel.flags &
10328c2ecf20Sopenharmony_ci			    V4L2_MBUS_PCLK_SAMPLE_RISING);
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_cidone:
10358c2ecf20Sopenharmony_ci	of_node_put(np);
10368c2ecf20Sopenharmony_ci	return pdata;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic int mt9v032_probe(struct i2c_client *client,
10408c2ecf20Sopenharmony_ci		const struct i2c_device_id *did)
10418c2ecf20Sopenharmony_ci{
10428c2ecf20Sopenharmony_ci	struct mt9v032_platform_data *pdata = mt9v032_get_pdata(client);
10438c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032;
10448c2ecf20Sopenharmony_ci	unsigned int i;
10458c2ecf20Sopenharmony_ci	int ret;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL);
10488c2ecf20Sopenharmony_ci	if (!mt9v032)
10498c2ecf20Sopenharmony_ci		return -ENOMEM;
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config);
10528c2ecf20Sopenharmony_ci	if (IS_ERR(mt9v032->regmap))
10538c2ecf20Sopenharmony_ci		return PTR_ERR(mt9v032->regmap);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	mt9v032->clk = devm_clk_get(&client->dev, NULL);
10568c2ecf20Sopenharmony_ci	if (IS_ERR(mt9v032->clk))
10578c2ecf20Sopenharmony_ci		return PTR_ERR(mt9v032->clk);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	mt9v032->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
10608c2ecf20Sopenharmony_ci						      GPIOD_OUT_HIGH);
10618c2ecf20Sopenharmony_ci	if (IS_ERR(mt9v032->reset_gpio))
10628c2ecf20Sopenharmony_ci		return PTR_ERR(mt9v032->reset_gpio);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	mt9v032->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
10658c2ecf20Sopenharmony_ci							GPIOD_OUT_LOW);
10668c2ecf20Sopenharmony_ci	if (IS_ERR(mt9v032->standby_gpio))
10678c2ecf20Sopenharmony_ci		return PTR_ERR(mt9v032->standby_gpio);
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	mutex_init(&mt9v032->power_lock);
10708c2ecf20Sopenharmony_ci	mt9v032->pdata = pdata;
10718c2ecf20Sopenharmony_ci	mt9v032->model = (const void *)did->driver_data;
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 +
10748c2ecf20Sopenharmony_ci			       ARRAY_SIZE(mt9v032_aegc_controls));
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10778c2ecf20Sopenharmony_ci			  V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
10788c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10798c2ecf20Sopenharmony_ci			  V4L2_CID_GAIN, MT9V032_ANALOG_GAIN_MIN,
10808c2ecf20Sopenharmony_ci			  MT9V032_ANALOG_GAIN_MAX, 1, MT9V032_ANALOG_GAIN_DEF);
10818c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10828c2ecf20Sopenharmony_ci			       V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 0,
10838c2ecf20Sopenharmony_ci			       V4L2_EXPOSURE_AUTO);
10848c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10858c2ecf20Sopenharmony_ci			  V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter,
10868c2ecf20Sopenharmony_ci			  mt9v032->model->data->max_shutter, 1,
10878c2ecf20Sopenharmony_ci			  MT9V032_TOTAL_SHUTTER_WIDTH_DEF);
10888c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10898c2ecf20Sopenharmony_ci			  V4L2_CID_HBLANK, mt9v032->model->data->min_hblank,
10908c2ecf20Sopenharmony_ci			  MT9V032_HORIZONTAL_BLANKING_MAX, 1,
10918c2ecf20Sopenharmony_ci			  MT9V032_HORIZONTAL_BLANKING_DEF);
10928c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
10938c2ecf20Sopenharmony_ci			  V4L2_CID_VBLANK, mt9v032->model->data->min_vblank,
10948c2ecf20Sopenharmony_ci			  mt9v032->model->data->max_vblank, 1,
10958c2ecf20Sopenharmony_ci			  MT9V032_VERTICAL_BLANKING_DEF);
10968c2ecf20Sopenharmony_ci	mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls,
10978c2ecf20Sopenharmony_ci				&mt9v032_ctrl_ops, V4L2_CID_TEST_PATTERN,
10988c2ecf20Sopenharmony_ci				ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0,
10998c2ecf20Sopenharmony_ci				mt9v032_test_pattern_menu);
11008c2ecf20Sopenharmony_ci	mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls,
11018c2ecf20Sopenharmony_ci				      &mt9v032_test_pattern_color, NULL);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	v4l2_ctrl_new_custom(&mt9v032->ctrls,
11048c2ecf20Sopenharmony_ci			     mt9v032->model->data->aec_max_shutter_v4l2_ctrl,
11058c2ecf20Sopenharmony_ci			     NULL);
11068c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mt9v032_aegc_controls); ++i)
11078c2ecf20Sopenharmony_ci		v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i],
11088c2ecf20Sopenharmony_ci				     NULL);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	v4l2_ctrl_cluster(2, &mt9v032->test_pattern);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	mt9v032->pixel_rate =
11138c2ecf20Sopenharmony_ci		v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops,
11148c2ecf20Sopenharmony_ci				  V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	if (pdata && pdata->link_freqs) {
11178c2ecf20Sopenharmony_ci		unsigned int def = 0;
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci		for (i = 0; pdata->link_freqs[i]; ++i) {
11208c2ecf20Sopenharmony_ci			if (pdata->link_freqs[i] == pdata->link_def_freq)
11218c2ecf20Sopenharmony_ci				def = i;
11228c2ecf20Sopenharmony_ci		}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci		mt9v032->link_freq =
11258c2ecf20Sopenharmony_ci			v4l2_ctrl_new_int_menu(&mt9v032->ctrls,
11268c2ecf20Sopenharmony_ci					       &mt9v032_ctrl_ops,
11278c2ecf20Sopenharmony_ci					       V4L2_CID_LINK_FREQ, i - 1, def,
11288c2ecf20Sopenharmony_ci					       pdata->link_freqs);
11298c2ecf20Sopenharmony_ci		v4l2_ctrl_cluster(2, &mt9v032->link_freq);
11308c2ecf20Sopenharmony_ci	}
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	mt9v032->subdev.ctrl_handler = &mt9v032->ctrls;
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	if (mt9v032->ctrls.error) {
11368c2ecf20Sopenharmony_ci		dev_err(&client->dev, "control initialization error %d\n",
11378c2ecf20Sopenharmony_ci			mt9v032->ctrls.error);
11388c2ecf20Sopenharmony_ci		ret = mt9v032->ctrls.error;
11398c2ecf20Sopenharmony_ci		goto err;
11408c2ecf20Sopenharmony_ci	}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	mt9v032->crop.left = MT9V032_COLUMN_START_DEF;
11438c2ecf20Sopenharmony_ci	mt9v032->crop.top = MT9V032_ROW_START_DEF;
11448c2ecf20Sopenharmony_ci	mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF;
11458c2ecf20Sopenharmony_ci	mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (mt9v032->model->color)
11488c2ecf20Sopenharmony_ci		mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
11498c2ecf20Sopenharmony_ci	else
11508c2ecf20Sopenharmony_ci		mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10;
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF;
11538c2ecf20Sopenharmony_ci	mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF;
11548c2ecf20Sopenharmony_ci	mt9v032->format.field = V4L2_FIELD_NONE;
11558c2ecf20Sopenharmony_ci	mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB;
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	mt9v032->hratio = 1;
11588c2ecf20Sopenharmony_ci	mt9v032->vratio = 1;
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE;
11618c2ecf20Sopenharmony_ci	mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF;
11628c2ecf20Sopenharmony_ci	mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF;
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops);
11658c2ecf20Sopenharmony_ci	mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops;
11668c2ecf20Sopenharmony_ci	mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	mt9v032->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
11698c2ecf20Sopenharmony_ci	mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE;
11708c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&mt9v032->subdev.entity, 1, &mt9v032->pad);
11718c2ecf20Sopenharmony_ci	if (ret < 0)
11728c2ecf20Sopenharmony_ci		goto err;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	mt9v032->subdev.dev = &client->dev;
11758c2ecf20Sopenharmony_ci	ret = v4l2_async_register_subdev(&mt9v032->subdev);
11768c2ecf20Sopenharmony_ci	if (ret < 0)
11778c2ecf20Sopenharmony_ci		goto err;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	return 0;
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_cierr:
11828c2ecf20Sopenharmony_ci	media_entity_cleanup(&mt9v032->subdev.entity);
11838c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&mt9v032->ctrls);
11848c2ecf20Sopenharmony_ci	return ret;
11858c2ecf20Sopenharmony_ci}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_cistatic int mt9v032_remove(struct i2c_client *client)
11888c2ecf20Sopenharmony_ci{
11898c2ecf20Sopenharmony_ci	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
11908c2ecf20Sopenharmony_ci	struct mt9v032 *mt9v032 = to_mt9v032(subdev);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(subdev);
11938c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&mt9v032->ctrls);
11948c2ecf20Sopenharmony_ci	media_entity_cleanup(&subdev->entity);
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	return 0;
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_cistatic const struct mt9v032_model_data mt9v032_model_data[] = {
12008c2ecf20Sopenharmony_ci	{
12018c2ecf20Sopenharmony_ci		/* MT9V022, MT9V032 revisions 1/2/3 */
12028c2ecf20Sopenharmony_ci		.min_row_time = 660,
12038c2ecf20Sopenharmony_ci		.min_hblank = MT9V032_HORIZONTAL_BLANKING_MIN,
12048c2ecf20Sopenharmony_ci		.min_vblank = MT9V032_VERTICAL_BLANKING_MIN,
12058c2ecf20Sopenharmony_ci		.max_vblank = MT9V032_VERTICAL_BLANKING_MAX,
12068c2ecf20Sopenharmony_ci		.min_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MIN,
12078c2ecf20Sopenharmony_ci		.max_shutter = MT9V032_TOTAL_SHUTTER_WIDTH_MAX,
12088c2ecf20Sopenharmony_ci		.pclk_reg = MT9V032_PIXEL_CLOCK,
12098c2ecf20Sopenharmony_ci		.aec_max_shutter_reg = MT9V032_AEC_MAX_SHUTTER_WIDTH,
12108c2ecf20Sopenharmony_ci		.aec_max_shutter_v4l2_ctrl = &mt9v032_aec_max_shutter_width,
12118c2ecf20Sopenharmony_ci	}, {
12128c2ecf20Sopenharmony_ci		/* MT9V024, MT9V034 */
12138c2ecf20Sopenharmony_ci		.min_row_time = 690,
12148c2ecf20Sopenharmony_ci		.min_hblank = MT9V034_HORIZONTAL_BLANKING_MIN,
12158c2ecf20Sopenharmony_ci		.min_vblank = MT9V034_VERTICAL_BLANKING_MIN,
12168c2ecf20Sopenharmony_ci		.max_vblank = MT9V034_VERTICAL_BLANKING_MAX,
12178c2ecf20Sopenharmony_ci		.min_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MIN,
12188c2ecf20Sopenharmony_ci		.max_shutter = MT9V034_TOTAL_SHUTTER_WIDTH_MAX,
12198c2ecf20Sopenharmony_ci		.pclk_reg = MT9V034_PIXEL_CLOCK,
12208c2ecf20Sopenharmony_ci		.aec_max_shutter_reg = MT9V034_AEC_MAX_SHUTTER_WIDTH,
12218c2ecf20Sopenharmony_ci		.aec_max_shutter_v4l2_ctrl = &mt9v034_aec_max_shutter_width,
12228c2ecf20Sopenharmony_ci	},
12238c2ecf20Sopenharmony_ci};
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_cistatic const struct mt9v032_model_info mt9v032_models[] = {
12268c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V022_COLOR] = {
12278c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[0],
12288c2ecf20Sopenharmony_ci		.color = true,
12298c2ecf20Sopenharmony_ci	},
12308c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V022_MONO] = {
12318c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[0],
12328c2ecf20Sopenharmony_ci		.color = false,
12338c2ecf20Sopenharmony_ci	},
12348c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V024_COLOR] = {
12358c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[1],
12368c2ecf20Sopenharmony_ci		.color = true,
12378c2ecf20Sopenharmony_ci	},
12388c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V024_MONO] = {
12398c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[1],
12408c2ecf20Sopenharmony_ci		.color = false,
12418c2ecf20Sopenharmony_ci	},
12428c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V032_COLOR] = {
12438c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[0],
12448c2ecf20Sopenharmony_ci		.color = true,
12458c2ecf20Sopenharmony_ci	},
12468c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V032_MONO] = {
12478c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[0],
12488c2ecf20Sopenharmony_ci		.color = false,
12498c2ecf20Sopenharmony_ci	},
12508c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V034_COLOR] = {
12518c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[1],
12528c2ecf20Sopenharmony_ci		.color = true,
12538c2ecf20Sopenharmony_ci	},
12548c2ecf20Sopenharmony_ci	[MT9V032_MODEL_V034_MONO] = {
12558c2ecf20Sopenharmony_ci		.data = &mt9v032_model_data[1],
12568c2ecf20Sopenharmony_ci		.color = false,
12578c2ecf20Sopenharmony_ci	},
12588c2ecf20Sopenharmony_ci};
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_cistatic const struct i2c_device_id mt9v032_id[] = {
12618c2ecf20Sopenharmony_ci	{ "mt9v022", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_COLOR] },
12628c2ecf20Sopenharmony_ci	{ "mt9v022m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V022_MONO] },
12638c2ecf20Sopenharmony_ci	{ "mt9v024", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_COLOR] },
12648c2ecf20Sopenharmony_ci	{ "mt9v024m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V024_MONO] },
12658c2ecf20Sopenharmony_ci	{ "mt9v032", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_COLOR] },
12668c2ecf20Sopenharmony_ci	{ "mt9v032m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V032_MONO] },
12678c2ecf20Sopenharmony_ci	{ "mt9v034", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_COLOR] },
12688c2ecf20Sopenharmony_ci	{ "mt9v034m", (kernel_ulong_t)&mt9v032_models[MT9V032_MODEL_V034_MONO] },
12698c2ecf20Sopenharmony_ci	{ }
12708c2ecf20Sopenharmony_ci};
12718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mt9v032_id);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF)
12748c2ecf20Sopenharmony_cistatic const struct of_device_id mt9v032_of_match[] = {
12758c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v022" },
12768c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v022m" },
12778c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v024" },
12788c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v024m" },
12798c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v032" },
12808c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v032m" },
12818c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v034" },
12828c2ecf20Sopenharmony_ci	{ .compatible = "aptina,mt9v034m" },
12838c2ecf20Sopenharmony_ci	{ /* Sentinel */ }
12848c2ecf20Sopenharmony_ci};
12858c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt9v032_of_match);
12868c2ecf20Sopenharmony_ci#endif
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_cistatic struct i2c_driver mt9v032_driver = {
12898c2ecf20Sopenharmony_ci	.driver = {
12908c2ecf20Sopenharmony_ci		.name = "mt9v032",
12918c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mt9v032_of_match),
12928c2ecf20Sopenharmony_ci	},
12938c2ecf20Sopenharmony_ci	.probe		= mt9v032_probe,
12948c2ecf20Sopenharmony_ci	.remove		= mt9v032_remove,
12958c2ecf20Sopenharmony_ci	.id_table	= mt9v032_id,
12968c2ecf20Sopenharmony_ci};
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_cimodule_i2c_driver(mt9v032_driver);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aptina MT9V032 Camera driver");
13018c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
13028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1303