18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for MT9T001 CMOS Image Sensor from Aptina (Micron)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011, 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/i2c.h>
148c2ecf20Sopenharmony_ci#include <linux/log2.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
198c2ecf20Sopenharmony_ci#include <linux/v4l2-mediabus.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <media/i2c/mt9t001.h>
228c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
238c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
248c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define MT9T001_PIXEL_ARRAY_HEIGHT			1568
278c2ecf20Sopenharmony_ci#define MT9T001_PIXEL_ARRAY_WIDTH			2112
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define MT9T001_CHIP_VERSION				0x00
308c2ecf20Sopenharmony_ci#define		MT9T001_CHIP_ID				0x1621
318c2ecf20Sopenharmony_ci#define MT9T001_ROW_START				0x01
328c2ecf20Sopenharmony_ci#define		MT9T001_ROW_START_MIN			0
338c2ecf20Sopenharmony_ci#define		MT9T001_ROW_START_DEF			20
348c2ecf20Sopenharmony_ci#define		MT9T001_ROW_START_MAX			1534
358c2ecf20Sopenharmony_ci#define MT9T001_COLUMN_START				0x02
368c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_START_MIN		0
378c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_START_DEF		32
388c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_START_MAX		2046
398c2ecf20Sopenharmony_ci#define MT9T001_WINDOW_HEIGHT				0x03
408c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_HEIGHT_MIN		1
418c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_HEIGHT_DEF		1535
428c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_HEIGHT_MAX		1567
438c2ecf20Sopenharmony_ci#define MT9T001_WINDOW_WIDTH				0x04
448c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_WIDTH_MIN		1
458c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_WIDTH_DEF		2047
468c2ecf20Sopenharmony_ci#define		MT9T001_WINDOW_WIDTH_MAX		2111
478c2ecf20Sopenharmony_ci#define MT9T001_HORIZONTAL_BLANKING			0x05
488c2ecf20Sopenharmony_ci#define		MT9T001_HORIZONTAL_BLANKING_MIN		21
498c2ecf20Sopenharmony_ci#define		MT9T001_HORIZONTAL_BLANKING_MAX		1023
508c2ecf20Sopenharmony_ci#define MT9T001_VERTICAL_BLANKING			0x06
518c2ecf20Sopenharmony_ci#define		MT9T001_VERTICAL_BLANKING_MIN		3
528c2ecf20Sopenharmony_ci#define		MT9T001_VERTICAL_BLANKING_MAX		1023
538c2ecf20Sopenharmony_ci#define MT9T001_OUTPUT_CONTROL				0x07
548c2ecf20Sopenharmony_ci#define		MT9T001_OUTPUT_CONTROL_SYNC		(1 << 0)
558c2ecf20Sopenharmony_ci#define		MT9T001_OUTPUT_CONTROL_CHIP_ENABLE	(1 << 1)
568c2ecf20Sopenharmony_ci#define		MT9T001_OUTPUT_CONTROL_TEST_DATA	(1 << 6)
578c2ecf20Sopenharmony_ci#define		MT9T001_OUTPUT_CONTROL_DEF		0x0002
588c2ecf20Sopenharmony_ci#define MT9T001_SHUTTER_WIDTH_HIGH			0x08
598c2ecf20Sopenharmony_ci#define MT9T001_SHUTTER_WIDTH_LOW			0x09
608c2ecf20Sopenharmony_ci#define		MT9T001_SHUTTER_WIDTH_MIN		1
618c2ecf20Sopenharmony_ci#define		MT9T001_SHUTTER_WIDTH_DEF		1561
628c2ecf20Sopenharmony_ci#define		MT9T001_SHUTTER_WIDTH_MAX		(1024 * 1024)
638c2ecf20Sopenharmony_ci#define MT9T001_PIXEL_CLOCK				0x0a
648c2ecf20Sopenharmony_ci#define		MT9T001_PIXEL_CLOCK_INVERT		(1 << 15)
658c2ecf20Sopenharmony_ci#define		MT9T001_PIXEL_CLOCK_SHIFT_MASK		(7 << 8)
668c2ecf20Sopenharmony_ci#define		MT9T001_PIXEL_CLOCK_SHIFT_SHIFT		8
678c2ecf20Sopenharmony_ci#define		MT9T001_PIXEL_CLOCK_DIVIDE_MASK		(0x7f << 0)
688c2ecf20Sopenharmony_ci#define MT9T001_FRAME_RESTART				0x0b
698c2ecf20Sopenharmony_ci#define MT9T001_SHUTTER_DELAY				0x0c
708c2ecf20Sopenharmony_ci#define		MT9T001_SHUTTER_DELAY_MAX		2047
718c2ecf20Sopenharmony_ci#define MT9T001_RESET					0x0d
728c2ecf20Sopenharmony_ci#define MT9T001_READ_MODE1				0x1e
738c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_SNAPSHOT		(1 << 8)
748c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_STROBE_ENABLE		(1 << 9)
758c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_STROBE_WIDTH		(1 << 10)
768c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_STROBE_OVERRIDE	(1 << 11)
778c2ecf20Sopenharmony_ci#define MT9T001_READ_MODE2				0x20
788c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_BAD_FRAMES		(1 << 0)
798c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_LINE_VALID_CONTINUOUS	(1 << 9)
808c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_LINE_VALID_FRAME	(1 << 10)
818c2ecf20Sopenharmony_ci#define MT9T001_READ_MODE3				0x21
828c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_GLOBAL_RESET		(1 << 0)
838c2ecf20Sopenharmony_ci#define		MT9T001_READ_MODE_GHST_CTL		(1 << 1)
848c2ecf20Sopenharmony_ci#define MT9T001_ROW_ADDRESS_MODE			0x22
858c2ecf20Sopenharmony_ci#define		MT9T001_ROW_SKIP_MASK			(7 << 0)
868c2ecf20Sopenharmony_ci#define		MT9T001_ROW_BIN_MASK			(3 << 3)
878c2ecf20Sopenharmony_ci#define		MT9T001_ROW_BIN_SHIFT			3
888c2ecf20Sopenharmony_ci#define MT9T001_COLUMN_ADDRESS_MODE			0x23
898c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_SKIP_MASK		(7 << 0)
908c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_BIN_MASK			(3 << 3)
918c2ecf20Sopenharmony_ci#define		MT9T001_COLUMN_BIN_SHIFT		3
928c2ecf20Sopenharmony_ci#define MT9T001_GREEN1_GAIN				0x2b
938c2ecf20Sopenharmony_ci#define MT9T001_BLUE_GAIN				0x2c
948c2ecf20Sopenharmony_ci#define MT9T001_RED_GAIN				0x2d
958c2ecf20Sopenharmony_ci#define MT9T001_GREEN2_GAIN				0x2e
968c2ecf20Sopenharmony_ci#define MT9T001_TEST_DATA				0x32
978c2ecf20Sopenharmony_ci#define MT9T001_GLOBAL_GAIN				0x35
988c2ecf20Sopenharmony_ci#define		MT9T001_GLOBAL_GAIN_MIN			8
998c2ecf20Sopenharmony_ci#define		MT9T001_GLOBAL_GAIN_MAX			1024
1008c2ecf20Sopenharmony_ci#define MT9T001_BLACK_LEVEL				0x49
1018c2ecf20Sopenharmony_ci#define MT9T001_ROW_BLACK_DEFAULT_OFFSET		0x4b
1028c2ecf20Sopenharmony_ci#define MT9T001_BLC_DELTA_THRESHOLDS			0x5d
1038c2ecf20Sopenharmony_ci#define MT9T001_CAL_THRESHOLDS				0x5f
1048c2ecf20Sopenharmony_ci#define MT9T001_GREEN1_OFFSET				0x60
1058c2ecf20Sopenharmony_ci#define MT9T001_GREEN2_OFFSET				0x61
1068c2ecf20Sopenharmony_ci#define MT9T001_BLACK_LEVEL_CALIBRATION			0x62
1078c2ecf20Sopenharmony_ci#define		MT9T001_BLACK_LEVEL_OVERRIDE		(1 << 0)
1088c2ecf20Sopenharmony_ci#define		MT9T001_BLACK_LEVEL_DISABLE_OFFSET	(1 << 1)
1098c2ecf20Sopenharmony_ci#define		MT9T001_BLACK_LEVEL_RECALCULATE		(1 << 12)
1108c2ecf20Sopenharmony_ci#define		MT9T001_BLACK_LEVEL_LOCK_RED_BLUE	(1 << 13)
1118c2ecf20Sopenharmony_ci#define		MT9T001_BLACK_LEVEL_LOCK_GREEN		(1 << 14)
1128c2ecf20Sopenharmony_ci#define MT9T001_RED_OFFSET				0x63
1138c2ecf20Sopenharmony_ci#define MT9T001_BLUE_OFFSET				0x64
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistruct mt9t001 {
1168c2ecf20Sopenharmony_ci	struct v4l2_subdev subdev;
1178c2ecf20Sopenharmony_ci	struct media_pad pad;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	struct clk *clk;
1208c2ecf20Sopenharmony_ci	struct regulator_bulk_data regulators[2];
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	struct mutex power_lock; /* lock to protect power_count */
1238c2ecf20Sopenharmony_ci	int power_count;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt format;
1268c2ecf20Sopenharmony_ci	struct v4l2_rect crop;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler ctrls;
1298c2ecf20Sopenharmony_ci	struct v4l2_ctrl *gains[4];
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	u16 output_control;
1328c2ecf20Sopenharmony_ci	u16 black_level;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline struct mt9t001 *to_mt9t001(struct v4l2_subdev *sd)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return container_of(sd, struct mt9t001, subdev);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int mt9t001_read(struct i2c_client *client, u8 reg)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return i2c_smbus_read_word_swapped(client, reg);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int mt9t001_write(struct i2c_client *client, u8 reg, u16 data)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	return i2c_smbus_write_word_swapped(client, reg, data);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int mt9t001_set_output_control(struct mt9t001 *mt9t001, u16 clear,
1518c2ecf20Sopenharmony_ci				      u16 set)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
1548c2ecf20Sopenharmony_ci	u16 value = (mt9t001->output_control & ~clear) | set;
1558c2ecf20Sopenharmony_ci	int ret;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (value == mt9t001->output_control)
1588c2ecf20Sopenharmony_ci		return 0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_OUTPUT_CONTROL, value);
1618c2ecf20Sopenharmony_ci	if (ret < 0)
1628c2ecf20Sopenharmony_ci		return ret;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	mt9t001->output_control = value;
1658c2ecf20Sopenharmony_ci	return 0;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic int mt9t001_reset(struct mt9t001 *mt9t001)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
1718c2ecf20Sopenharmony_ci	int ret;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Reset the chip and stop data read out */
1748c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_RESET, 1);
1758c2ecf20Sopenharmony_ci	if (ret < 0)
1768c2ecf20Sopenharmony_ci		return ret;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_RESET, 0);
1798c2ecf20Sopenharmony_ci	if (ret < 0)
1808c2ecf20Sopenharmony_ci		return ret;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	mt9t001->output_control = MT9T001_OUTPUT_CONTROL_DEF;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return mt9t001_set_output_control(mt9t001,
1858c2ecf20Sopenharmony_ci					  MT9T001_OUTPUT_CONTROL_CHIP_ENABLE,
1868c2ecf20Sopenharmony_ci					  0);
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic int mt9t001_power_on(struct mt9t001 *mt9t001)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	int ret;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Bring up the supplies */
1948c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(ARRAY_SIZE(mt9t001->regulators),
1958c2ecf20Sopenharmony_ci				   mt9t001->regulators);
1968c2ecf20Sopenharmony_ci	if (ret < 0)
1978c2ecf20Sopenharmony_ci		return ret;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* Enable clock */
2008c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(mt9t001->clk);
2018c2ecf20Sopenharmony_ci	if (ret < 0)
2028c2ecf20Sopenharmony_ci		regulator_bulk_disable(ARRAY_SIZE(mt9t001->regulators),
2038c2ecf20Sopenharmony_ci				       mt9t001->regulators);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return ret;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic void mt9t001_power_off(struct mt9t001 *mt9t001)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	regulator_bulk_disable(ARRAY_SIZE(mt9t001->regulators),
2118c2ecf20Sopenharmony_ci			       mt9t001->regulators);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	clk_disable_unprepare(mt9t001->clk);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic int __mt9t001_set_power(struct mt9t001 *mt9t001, bool on)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
2198c2ecf20Sopenharmony_ci	int ret;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (!on) {
2228c2ecf20Sopenharmony_ci		mt9t001_power_off(mt9t001);
2238c2ecf20Sopenharmony_ci		return 0;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ret = mt9t001_power_on(mt9t001);
2278c2ecf20Sopenharmony_ci	if (ret < 0)
2288c2ecf20Sopenharmony_ci		return ret;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	ret = mt9t001_reset(mt9t001);
2318c2ecf20Sopenharmony_ci	if (ret < 0) {
2328c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to reset the camera\n");
2338c2ecf20Sopenharmony_ci		goto e_power;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_handler_setup(&mt9t001->ctrls);
2378c2ecf20Sopenharmony_ci	if (ret < 0) {
2388c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to set up control handlers\n");
2398c2ecf20Sopenharmony_ci		goto e_power;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cie_power:
2458c2ecf20Sopenharmony_ci	mt9t001_power_off(mt9t001);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	return ret;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
2518c2ecf20Sopenharmony_ci * V4L2 subdev video operations
2528c2ecf20Sopenharmony_ci */
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic struct v4l2_mbus_framefmt *
2558c2ecf20Sopenharmony_ci__mt9t001_get_pad_format(struct mt9t001 *mt9t001, struct v4l2_subdev_pad_config *cfg,
2568c2ecf20Sopenharmony_ci			 unsigned int pad, enum v4l2_subdev_format_whence which)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	switch (which) {
2598c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
2608c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_format(&mt9t001->subdev, cfg, pad);
2618c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
2628c2ecf20Sopenharmony_ci		return &mt9t001->format;
2638c2ecf20Sopenharmony_ci	default:
2648c2ecf20Sopenharmony_ci		return NULL;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic struct v4l2_rect *
2698c2ecf20Sopenharmony_ci__mt9t001_get_pad_crop(struct mt9t001 *mt9t001, struct v4l2_subdev_pad_config *cfg,
2708c2ecf20Sopenharmony_ci		       unsigned int pad, enum v4l2_subdev_format_whence which)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	switch (which) {
2738c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_TRY:
2748c2ecf20Sopenharmony_ci		return v4l2_subdev_get_try_crop(&mt9t001->subdev, cfg, pad);
2758c2ecf20Sopenharmony_ci	case V4L2_SUBDEV_FORMAT_ACTIVE:
2768c2ecf20Sopenharmony_ci		return &mt9t001->crop;
2778c2ecf20Sopenharmony_ci	default:
2788c2ecf20Sopenharmony_ci		return NULL;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int mt9t001_s_stream(struct v4l2_subdev *subdev, int enable)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	const u16 mode = MT9T001_OUTPUT_CONTROL_CHIP_ENABLE;
2858c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(subdev);
2868c2ecf20Sopenharmony_ci	struct mt9t001_platform_data *pdata = client->dev.platform_data;
2878c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
2888c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *format = &mt9t001->format;
2898c2ecf20Sopenharmony_ci	struct v4l2_rect *crop = &mt9t001->crop;
2908c2ecf20Sopenharmony_ci	unsigned int hratio;
2918c2ecf20Sopenharmony_ci	unsigned int vratio;
2928c2ecf20Sopenharmony_ci	int ret;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (!enable)
2958c2ecf20Sopenharmony_ci		return mt9t001_set_output_control(mt9t001, mode, 0);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/* Configure the pixel clock polarity */
2988c2ecf20Sopenharmony_ci	if (pdata->clk_pol) {
2998c2ecf20Sopenharmony_ci		ret  = mt9t001_write(client, MT9T001_PIXEL_CLOCK,
3008c2ecf20Sopenharmony_ci				     MT9T001_PIXEL_CLOCK_INVERT);
3018c2ecf20Sopenharmony_ci		if (ret < 0)
3028c2ecf20Sopenharmony_ci			return ret;
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* Configure the window size and row/column bin */
3068c2ecf20Sopenharmony_ci	hratio = DIV_ROUND_CLOSEST(crop->width, format->width);
3078c2ecf20Sopenharmony_ci	vratio = DIV_ROUND_CLOSEST(crop->height, format->height);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_ROW_ADDRESS_MODE, hratio - 1);
3108c2ecf20Sopenharmony_ci	if (ret < 0)
3118c2ecf20Sopenharmony_ci		return ret;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_COLUMN_ADDRESS_MODE, vratio - 1);
3148c2ecf20Sopenharmony_ci	if (ret < 0)
3158c2ecf20Sopenharmony_ci		return ret;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_COLUMN_START, crop->left);
3188c2ecf20Sopenharmony_ci	if (ret < 0)
3198c2ecf20Sopenharmony_ci		return ret;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_ROW_START, crop->top);
3228c2ecf20Sopenharmony_ci	if (ret < 0)
3238c2ecf20Sopenharmony_ci		return ret;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_WINDOW_WIDTH, crop->width - 1);
3268c2ecf20Sopenharmony_ci	if (ret < 0)
3278c2ecf20Sopenharmony_ci		return ret;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	ret = mt9t001_write(client, MT9T001_WINDOW_HEIGHT, crop->height - 1);
3308c2ecf20Sopenharmony_ci	if (ret < 0)
3318c2ecf20Sopenharmony_ci		return ret;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Switch to master "normal" mode */
3348c2ecf20Sopenharmony_ci	return mt9t001_set_output_control(mt9t001, 0, mode);
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int mt9t001_enum_mbus_code(struct v4l2_subdev *subdev,
3388c2ecf20Sopenharmony_ci				  struct v4l2_subdev_pad_config *cfg,
3398c2ecf20Sopenharmony_ci				  struct v4l2_subdev_mbus_code_enum *code)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	if (code->index > 0)
3428c2ecf20Sopenharmony_ci		return -EINVAL;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic int mt9t001_enum_frame_size(struct v4l2_subdev *subdev,
3498c2ecf20Sopenharmony_ci				   struct v4l2_subdev_pad_config *cfg,
3508c2ecf20Sopenharmony_ci				   struct v4l2_subdev_frame_size_enum *fse)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	if (fse->index >= 8 || fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
3538c2ecf20Sopenharmony_ci		return -EINVAL;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	fse->min_width = (MT9T001_WINDOW_WIDTH_DEF + 1) / fse->index;
3568c2ecf20Sopenharmony_ci	fse->max_width = fse->min_width;
3578c2ecf20Sopenharmony_ci	fse->min_height = (MT9T001_WINDOW_HEIGHT_DEF + 1) / fse->index;
3588c2ecf20Sopenharmony_ci	fse->max_height = fse->min_height;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	return 0;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int mt9t001_get_format(struct v4l2_subdev *subdev,
3648c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
3658c2ecf20Sopenharmony_ci			      struct v4l2_subdev_format *format)
3668c2ecf20Sopenharmony_ci{
3678c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	format->format = *__mt9t001_get_pad_format(mt9t001, cfg, format->pad,
3708c2ecf20Sopenharmony_ci						   format->which);
3718c2ecf20Sopenharmony_ci	return 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic int mt9t001_set_format(struct v4l2_subdev *subdev,
3758c2ecf20Sopenharmony_ci			      struct v4l2_subdev_pad_config *cfg,
3768c2ecf20Sopenharmony_ci			      struct v4l2_subdev_format *format)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
3798c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *__format;
3808c2ecf20Sopenharmony_ci	struct v4l2_rect *__crop;
3818c2ecf20Sopenharmony_ci	unsigned int width;
3828c2ecf20Sopenharmony_ci	unsigned int height;
3838c2ecf20Sopenharmony_ci	unsigned int hratio;
3848c2ecf20Sopenharmony_ci	unsigned int vratio;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	__crop = __mt9t001_get_pad_crop(mt9t001, cfg, format->pad,
3878c2ecf20Sopenharmony_ci					format->which);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	/* Clamp the width and height to avoid dividing by zero. */
3908c2ecf20Sopenharmony_ci	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
3918c2ecf20Sopenharmony_ci			max_t(unsigned int, __crop->width / 8,
3928c2ecf20Sopenharmony_ci			      MT9T001_WINDOW_HEIGHT_MIN + 1),
3938c2ecf20Sopenharmony_ci			__crop->width);
3948c2ecf20Sopenharmony_ci	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
3958c2ecf20Sopenharmony_ci			 max_t(unsigned int, __crop->height / 8,
3968c2ecf20Sopenharmony_ci			       MT9T001_WINDOW_HEIGHT_MIN + 1),
3978c2ecf20Sopenharmony_ci			 __crop->height);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	hratio = DIV_ROUND_CLOSEST(__crop->width, width);
4008c2ecf20Sopenharmony_ci	vratio = DIV_ROUND_CLOSEST(__crop->height, height);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	__format = __mt9t001_get_pad_format(mt9t001, cfg, format->pad,
4038c2ecf20Sopenharmony_ci					    format->which);
4048c2ecf20Sopenharmony_ci	__format->width = __crop->width / hratio;
4058c2ecf20Sopenharmony_ci	__format->height = __crop->height / vratio;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	format->format = *__format;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	return 0;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int mt9t001_get_selection(struct v4l2_subdev *subdev,
4138c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
4148c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP)
4198c2ecf20Sopenharmony_ci		return -EINVAL;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	sel->r = *__mt9t001_get_pad_crop(mt9t001, cfg, sel->pad, sel->which);
4228c2ecf20Sopenharmony_ci	return 0;
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic int mt9t001_set_selection(struct v4l2_subdev *subdev,
4268c2ecf20Sopenharmony_ci				 struct v4l2_subdev_pad_config *cfg,
4278c2ecf20Sopenharmony_ci				 struct v4l2_subdev_selection *sel)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
4308c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *__format;
4318c2ecf20Sopenharmony_ci	struct v4l2_rect *__crop;
4328c2ecf20Sopenharmony_ci	struct v4l2_rect rect;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (sel->target != V4L2_SEL_TGT_CROP)
4358c2ecf20Sopenharmony_ci		return -EINVAL;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Clamp the crop rectangle boundaries and align them to a multiple of 2
4388c2ecf20Sopenharmony_ci	 * pixels.
4398c2ecf20Sopenharmony_ci	 */
4408c2ecf20Sopenharmony_ci	rect.left = clamp(ALIGN(sel->r.left, 2),
4418c2ecf20Sopenharmony_ci			  MT9T001_COLUMN_START_MIN,
4428c2ecf20Sopenharmony_ci			  MT9T001_COLUMN_START_MAX);
4438c2ecf20Sopenharmony_ci	rect.top = clamp(ALIGN(sel->r.top, 2),
4448c2ecf20Sopenharmony_ci			 MT9T001_ROW_START_MIN,
4458c2ecf20Sopenharmony_ci			 MT9T001_ROW_START_MAX);
4468c2ecf20Sopenharmony_ci	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
4478c2ecf20Sopenharmony_ci			     MT9T001_WINDOW_WIDTH_MIN + 1,
4488c2ecf20Sopenharmony_ci			     MT9T001_WINDOW_WIDTH_MAX + 1);
4498c2ecf20Sopenharmony_ci	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
4508c2ecf20Sopenharmony_ci			      MT9T001_WINDOW_HEIGHT_MIN + 1,
4518c2ecf20Sopenharmony_ci			      MT9T001_WINDOW_HEIGHT_MAX + 1);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	rect.width = min_t(unsigned int, rect.width,
4548c2ecf20Sopenharmony_ci			   MT9T001_PIXEL_ARRAY_WIDTH - rect.left);
4558c2ecf20Sopenharmony_ci	rect.height = min_t(unsigned int, rect.height,
4568c2ecf20Sopenharmony_ci			    MT9T001_PIXEL_ARRAY_HEIGHT - rect.top);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	__crop = __mt9t001_get_pad_crop(mt9t001, cfg, sel->pad, sel->which);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (rect.width != __crop->width || rect.height != __crop->height) {
4618c2ecf20Sopenharmony_ci		/* Reset the output image size if the crop rectangle size has
4628c2ecf20Sopenharmony_ci		 * been modified.
4638c2ecf20Sopenharmony_ci		 */
4648c2ecf20Sopenharmony_ci		__format = __mt9t001_get_pad_format(mt9t001, cfg, sel->pad,
4658c2ecf20Sopenharmony_ci						    sel->which);
4668c2ecf20Sopenharmony_ci		__format->width = rect.width;
4678c2ecf20Sopenharmony_ci		__format->height = rect.height;
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	*__crop = rect;
4718c2ecf20Sopenharmony_ci	sel->r = rect;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	return 0;
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
4778c2ecf20Sopenharmony_ci * V4L2 subdev control operations
4788c2ecf20Sopenharmony_ci */
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci#define V4L2_CID_TEST_PATTERN_COLOR	(V4L2_CID_USER_BASE | 0x1001)
4818c2ecf20Sopenharmony_ci#define V4L2_CID_BLACK_LEVEL_AUTO	(V4L2_CID_USER_BASE | 0x1002)
4828c2ecf20Sopenharmony_ci#define V4L2_CID_BLACK_LEVEL_OFFSET	(V4L2_CID_USER_BASE | 0x1003)
4838c2ecf20Sopenharmony_ci#define V4L2_CID_BLACK_LEVEL_CALIBRATE	(V4L2_CID_USER_BASE | 0x1004)
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci#define V4L2_CID_GAIN_RED		(V4L2_CTRL_CLASS_CAMERA | 0x1001)
4868c2ecf20Sopenharmony_ci#define V4L2_CID_GAIN_GREEN_RED		(V4L2_CTRL_CLASS_CAMERA | 0x1002)
4878c2ecf20Sopenharmony_ci#define V4L2_CID_GAIN_GREEN_BLUE	(V4L2_CTRL_CLASS_CAMERA | 0x1003)
4888c2ecf20Sopenharmony_ci#define V4L2_CID_GAIN_BLUE		(V4L2_CTRL_CLASS_CAMERA | 0x1004)
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_cistatic u16 mt9t001_gain_value(s32 *gain)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	/* Gain is controlled by 2 analog stages and a digital stage. Valid
4938c2ecf20Sopenharmony_ci	 * values for the 3 stages are
4948c2ecf20Sopenharmony_ci	 *
4958c2ecf20Sopenharmony_ci	 * Stage		Min	Max	Step
4968c2ecf20Sopenharmony_ci	 * ------------------------------------------
4978c2ecf20Sopenharmony_ci	 * First analog stage	x1	x2	1
4988c2ecf20Sopenharmony_ci	 * Second analog stage	x1	x4	0.125
4998c2ecf20Sopenharmony_ci	 * Digital stage	x1	x16	0.125
5008c2ecf20Sopenharmony_ci	 *
5018c2ecf20Sopenharmony_ci	 * To minimize noise, the gain stages should be used in the second
5028c2ecf20Sopenharmony_ci	 * analog stage, first analog stage, digital stage order. Gain from a
5038c2ecf20Sopenharmony_ci	 * previous stage should be pushed to its maximum value before the next
5048c2ecf20Sopenharmony_ci	 * stage is used.
5058c2ecf20Sopenharmony_ci	 */
5068c2ecf20Sopenharmony_ci	if (*gain <= 32)
5078c2ecf20Sopenharmony_ci		return *gain;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	if (*gain <= 64) {
5108c2ecf20Sopenharmony_ci		*gain &= ~1;
5118c2ecf20Sopenharmony_ci		return (1 << 6) | (*gain >> 1);
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	*gain &= ~7;
5158c2ecf20Sopenharmony_ci	return ((*gain - 64) << 5) | (1 << 6) | 32;
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int mt9t001_ctrl_freeze(struct mt9t001 *mt9t001, bool freeze)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	return mt9t001_set_output_control(mt9t001,
5218c2ecf20Sopenharmony_ci		freeze ? 0 : MT9T001_OUTPUT_CONTROL_SYNC,
5228c2ecf20Sopenharmony_ci		freeze ? MT9T001_OUTPUT_CONTROL_SYNC : 0);
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int mt9t001_s_ctrl(struct v4l2_ctrl *ctrl)
5268c2ecf20Sopenharmony_ci{
5278c2ecf20Sopenharmony_ci	static const u8 gains[4] = {
5288c2ecf20Sopenharmony_ci		MT9T001_RED_GAIN, MT9T001_GREEN1_GAIN,
5298c2ecf20Sopenharmony_ci		MT9T001_GREEN2_GAIN, MT9T001_BLUE_GAIN
5308c2ecf20Sopenharmony_ci	};
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 =
5338c2ecf20Sopenharmony_ci			container_of(ctrl->handler, struct mt9t001, ctrls);
5348c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&mt9t001->subdev);
5358c2ecf20Sopenharmony_ci	unsigned int count;
5368c2ecf20Sopenharmony_ci	unsigned int i;
5378c2ecf20Sopenharmony_ci	u16 value;
5388c2ecf20Sopenharmony_ci	int ret;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	switch (ctrl->id) {
5418c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN_RED:
5428c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN_GREEN_RED:
5438c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN_GREEN_BLUE:
5448c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN_BLUE:
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		/* Disable control updates if more than one control has changed
5478c2ecf20Sopenharmony_ci		 * in the cluster.
5488c2ecf20Sopenharmony_ci		 */
5498c2ecf20Sopenharmony_ci		for (i = 0, count = 0; i < 4; ++i) {
5508c2ecf20Sopenharmony_ci			struct v4l2_ctrl *gain = mt9t001->gains[i];
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci			if (gain->val != gain->cur.val)
5538c2ecf20Sopenharmony_ci				count++;
5548c2ecf20Sopenharmony_ci		}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci		if (count > 1) {
5578c2ecf20Sopenharmony_ci			ret = mt9t001_ctrl_freeze(mt9t001, true);
5588c2ecf20Sopenharmony_ci			if (ret < 0)
5598c2ecf20Sopenharmony_ci				return ret;
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci		/* Update the gain controls. */
5638c2ecf20Sopenharmony_ci		for (i = 0; i < 4; ++i) {
5648c2ecf20Sopenharmony_ci			struct v4l2_ctrl *gain = mt9t001->gains[i];
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci			if (gain->val == gain->cur.val)
5678c2ecf20Sopenharmony_ci				continue;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci			value = mt9t001_gain_value(&gain->val);
5708c2ecf20Sopenharmony_ci			ret = mt9t001_write(client, gains[i], value);
5718c2ecf20Sopenharmony_ci			if (ret < 0) {
5728c2ecf20Sopenharmony_ci				mt9t001_ctrl_freeze(mt9t001, false);
5738c2ecf20Sopenharmony_ci				return ret;
5748c2ecf20Sopenharmony_ci			}
5758c2ecf20Sopenharmony_ci		}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci		/* Enable control updates. */
5788c2ecf20Sopenharmony_ci		if (count > 1) {
5798c2ecf20Sopenharmony_ci			ret = mt9t001_ctrl_freeze(mt9t001, false);
5808c2ecf20Sopenharmony_ci			if (ret < 0)
5818c2ecf20Sopenharmony_ci				return ret;
5828c2ecf20Sopenharmony_ci		}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci		break;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE:
5878c2ecf20Sopenharmony_ci		ret = mt9t001_write(client, MT9T001_SHUTTER_WIDTH_LOW,
5888c2ecf20Sopenharmony_ci				    ctrl->val & 0xffff);
5898c2ecf20Sopenharmony_ci		if (ret < 0)
5908c2ecf20Sopenharmony_ci			return ret;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci		return mt9t001_write(client, MT9T001_SHUTTER_WIDTH_HIGH,
5938c2ecf20Sopenharmony_ci				     ctrl->val >> 16);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	case V4L2_CID_TEST_PATTERN:
5968c2ecf20Sopenharmony_ci		return mt9t001_set_output_control(mt9t001,
5978c2ecf20Sopenharmony_ci			ctrl->val ? 0 : MT9T001_OUTPUT_CONTROL_TEST_DATA,
5988c2ecf20Sopenharmony_ci			ctrl->val ? MT9T001_OUTPUT_CONTROL_TEST_DATA : 0);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	case V4L2_CID_TEST_PATTERN_COLOR:
6018c2ecf20Sopenharmony_ci		return mt9t001_write(client, MT9T001_TEST_DATA, ctrl->val << 2);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	case V4L2_CID_BLACK_LEVEL_AUTO:
6048c2ecf20Sopenharmony_ci		value = ctrl->val ? 0 : MT9T001_BLACK_LEVEL_OVERRIDE;
6058c2ecf20Sopenharmony_ci		ret = mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
6068c2ecf20Sopenharmony_ci				    value);
6078c2ecf20Sopenharmony_ci		if (ret < 0)
6088c2ecf20Sopenharmony_ci			return ret;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci		mt9t001->black_level = value;
6118c2ecf20Sopenharmony_ci		break;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	case V4L2_CID_BLACK_LEVEL_OFFSET:
6148c2ecf20Sopenharmony_ci		ret = mt9t001_write(client, MT9T001_GREEN1_OFFSET, ctrl->val);
6158c2ecf20Sopenharmony_ci		if (ret < 0)
6168c2ecf20Sopenharmony_ci			return ret;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		ret = mt9t001_write(client, MT9T001_GREEN2_OFFSET, ctrl->val);
6198c2ecf20Sopenharmony_ci		if (ret < 0)
6208c2ecf20Sopenharmony_ci			return ret;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		ret = mt9t001_write(client, MT9T001_RED_OFFSET, ctrl->val);
6238c2ecf20Sopenharmony_ci		if (ret < 0)
6248c2ecf20Sopenharmony_ci			return ret;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci		return mt9t001_write(client, MT9T001_BLUE_OFFSET, ctrl->val);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	case V4L2_CID_BLACK_LEVEL_CALIBRATE:
6298c2ecf20Sopenharmony_ci		return mt9t001_write(client, MT9T001_BLACK_LEVEL_CALIBRATION,
6308c2ecf20Sopenharmony_ci				     MT9T001_BLACK_LEVEL_RECALCULATE |
6318c2ecf20Sopenharmony_ci				     mt9t001->black_level);
6328c2ecf20Sopenharmony_ci	}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	return 0;
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops mt9t001_ctrl_ops = {
6388c2ecf20Sopenharmony_ci	.s_ctrl = mt9t001_s_ctrl,
6398c2ecf20Sopenharmony_ci};
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cistatic const char * const mt9t001_test_pattern_menu[] = {
6428c2ecf20Sopenharmony_ci	"Disabled",
6438c2ecf20Sopenharmony_ci	"Enabled",
6448c2ecf20Sopenharmony_ci};
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9t001_ctrls[] = {
6478c2ecf20Sopenharmony_ci	{
6488c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
6498c2ecf20Sopenharmony_ci		.id		= V4L2_CID_TEST_PATTERN_COLOR,
6508c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
6518c2ecf20Sopenharmony_ci		.name		= "Test Pattern Color",
6528c2ecf20Sopenharmony_ci		.min		= 0,
6538c2ecf20Sopenharmony_ci		.max		= 1023,
6548c2ecf20Sopenharmony_ci		.step		= 1,
6558c2ecf20Sopenharmony_ci		.def		= 0,
6568c2ecf20Sopenharmony_ci		.flags		= 0,
6578c2ecf20Sopenharmony_ci	}, {
6588c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
6598c2ecf20Sopenharmony_ci		.id		= V4L2_CID_BLACK_LEVEL_AUTO,
6608c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_BOOLEAN,
6618c2ecf20Sopenharmony_ci		.name		= "Black Level, Auto",
6628c2ecf20Sopenharmony_ci		.min		= 0,
6638c2ecf20Sopenharmony_ci		.max		= 1,
6648c2ecf20Sopenharmony_ci		.step		= 1,
6658c2ecf20Sopenharmony_ci		.def		= 1,
6668c2ecf20Sopenharmony_ci		.flags		= 0,
6678c2ecf20Sopenharmony_ci	}, {
6688c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
6698c2ecf20Sopenharmony_ci		.id		= V4L2_CID_BLACK_LEVEL_OFFSET,
6708c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
6718c2ecf20Sopenharmony_ci		.name		= "Black Level, Offset",
6728c2ecf20Sopenharmony_ci		.min		= -256,
6738c2ecf20Sopenharmony_ci		.max		= 255,
6748c2ecf20Sopenharmony_ci		.step		= 1,
6758c2ecf20Sopenharmony_ci		.def		= 32,
6768c2ecf20Sopenharmony_ci		.flags		= 0,
6778c2ecf20Sopenharmony_ci	}, {
6788c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
6798c2ecf20Sopenharmony_ci		.id		= V4L2_CID_BLACK_LEVEL_CALIBRATE,
6808c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_BUTTON,
6818c2ecf20Sopenharmony_ci		.name		= "Black Level, Calibrate",
6828c2ecf20Sopenharmony_ci		.min		= 0,
6838c2ecf20Sopenharmony_ci		.max		= 0,
6848c2ecf20Sopenharmony_ci		.step		= 0,
6858c2ecf20Sopenharmony_ci		.def		= 0,
6868c2ecf20Sopenharmony_ci		.flags		= V4L2_CTRL_FLAG_WRITE_ONLY,
6878c2ecf20Sopenharmony_ci	},
6888c2ecf20Sopenharmony_ci};
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9t001_gains[] = {
6918c2ecf20Sopenharmony_ci	{
6928c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
6938c2ecf20Sopenharmony_ci		.id		= V4L2_CID_GAIN_RED,
6948c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
6958c2ecf20Sopenharmony_ci		.name		= "Gain, Red",
6968c2ecf20Sopenharmony_ci		.min		= MT9T001_GLOBAL_GAIN_MIN,
6978c2ecf20Sopenharmony_ci		.max		= MT9T001_GLOBAL_GAIN_MAX,
6988c2ecf20Sopenharmony_ci		.step		= 1,
6998c2ecf20Sopenharmony_ci		.def		= MT9T001_GLOBAL_GAIN_MIN,
7008c2ecf20Sopenharmony_ci		.flags		= 0,
7018c2ecf20Sopenharmony_ci	}, {
7028c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
7038c2ecf20Sopenharmony_ci		.id		= V4L2_CID_GAIN_GREEN_RED,
7048c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7058c2ecf20Sopenharmony_ci		.name		= "Gain, Green (R)",
7068c2ecf20Sopenharmony_ci		.min		= MT9T001_GLOBAL_GAIN_MIN,
7078c2ecf20Sopenharmony_ci		.max		= MT9T001_GLOBAL_GAIN_MAX,
7088c2ecf20Sopenharmony_ci		.step		= 1,
7098c2ecf20Sopenharmony_ci		.def		= MT9T001_GLOBAL_GAIN_MIN,
7108c2ecf20Sopenharmony_ci		.flags		= 0,
7118c2ecf20Sopenharmony_ci	}, {
7128c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
7138c2ecf20Sopenharmony_ci		.id		= V4L2_CID_GAIN_GREEN_BLUE,
7148c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7158c2ecf20Sopenharmony_ci		.name		= "Gain, Green (B)",
7168c2ecf20Sopenharmony_ci		.min		= MT9T001_GLOBAL_GAIN_MIN,
7178c2ecf20Sopenharmony_ci		.max		= MT9T001_GLOBAL_GAIN_MAX,
7188c2ecf20Sopenharmony_ci		.step		= 1,
7198c2ecf20Sopenharmony_ci		.def		= MT9T001_GLOBAL_GAIN_MIN,
7208c2ecf20Sopenharmony_ci		.flags		= 0,
7218c2ecf20Sopenharmony_ci	}, {
7228c2ecf20Sopenharmony_ci		.ops		= &mt9t001_ctrl_ops,
7238c2ecf20Sopenharmony_ci		.id		= V4L2_CID_GAIN_BLUE,
7248c2ecf20Sopenharmony_ci		.type		= V4L2_CTRL_TYPE_INTEGER,
7258c2ecf20Sopenharmony_ci		.name		= "Gain, Blue",
7268c2ecf20Sopenharmony_ci		.min		= MT9T001_GLOBAL_GAIN_MIN,
7278c2ecf20Sopenharmony_ci		.max		= MT9T001_GLOBAL_GAIN_MAX,
7288c2ecf20Sopenharmony_ci		.step		= 1,
7298c2ecf20Sopenharmony_ci		.def		= MT9T001_GLOBAL_GAIN_MIN,
7308c2ecf20Sopenharmony_ci		.flags		= 0,
7318c2ecf20Sopenharmony_ci	},
7328c2ecf20Sopenharmony_ci};
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
7358c2ecf20Sopenharmony_ci * V4L2 subdev core operations
7368c2ecf20Sopenharmony_ci */
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic int mt9t001_set_power(struct v4l2_subdev *subdev, int on)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
7418c2ecf20Sopenharmony_ci	int ret = 0;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	mutex_lock(&mt9t001->power_lock);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
7468c2ecf20Sopenharmony_ci	 * update the power state.
7478c2ecf20Sopenharmony_ci	 */
7488c2ecf20Sopenharmony_ci	if (mt9t001->power_count == !on) {
7498c2ecf20Sopenharmony_ci		ret = __mt9t001_set_power(mt9t001, !!on);
7508c2ecf20Sopenharmony_ci		if (ret < 0)
7518c2ecf20Sopenharmony_ci			goto out;
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	/* Update the power count. */
7558c2ecf20Sopenharmony_ci	mt9t001->power_count += on ? 1 : -1;
7568c2ecf20Sopenharmony_ci	WARN_ON(mt9t001->power_count < 0);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ciout:
7598c2ecf20Sopenharmony_ci	mutex_unlock(&mt9t001->power_lock);
7608c2ecf20Sopenharmony_ci	return ret;
7618c2ecf20Sopenharmony_ci}
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci/* -----------------------------------------------------------------------------
7648c2ecf20Sopenharmony_ci * V4L2 subdev internal operations
7658c2ecf20Sopenharmony_ci */
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic int mt9t001_registered(struct v4l2_subdev *subdev)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(subdev);
7708c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
7718c2ecf20Sopenharmony_ci	s32 data;
7728c2ecf20Sopenharmony_ci	int ret;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	ret = mt9t001_power_on(mt9t001);
7758c2ecf20Sopenharmony_ci	if (ret < 0) {
7768c2ecf20Sopenharmony_ci		dev_err(&client->dev, "MT9T001 power up failed\n");
7778c2ecf20Sopenharmony_ci		return ret;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	/* Read out the chip version register */
7818c2ecf20Sopenharmony_ci	data = mt9t001_read(client, MT9T001_CHIP_VERSION);
7828c2ecf20Sopenharmony_ci	mt9t001_power_off(mt9t001);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (data != MT9T001_CHIP_ID) {
7858c2ecf20Sopenharmony_ci		dev_err(&client->dev,
7868c2ecf20Sopenharmony_ci			"MT9T001 not detected, wrong version 0x%04x\n", data);
7878c2ecf20Sopenharmony_ci		return -ENODEV;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	dev_info(&client->dev, "MT9T001 detected at address 0x%02x\n",
7918c2ecf20Sopenharmony_ci		 client->addr);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	return 0;
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cistatic int mt9t001_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *format;
7998c2ecf20Sopenharmony_ci	struct v4l2_rect *crop;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
8028c2ecf20Sopenharmony_ci	crop->left = MT9T001_COLUMN_START_DEF;
8038c2ecf20Sopenharmony_ci	crop->top = MT9T001_ROW_START_DEF;
8048c2ecf20Sopenharmony_ci	crop->width = MT9T001_WINDOW_WIDTH_DEF + 1;
8058c2ecf20Sopenharmony_ci	crop->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
8088c2ecf20Sopenharmony_ci	format->code = MEDIA_BUS_FMT_SGRBG10_1X10;
8098c2ecf20Sopenharmony_ci	format->width = MT9T001_WINDOW_WIDTH_DEF + 1;
8108c2ecf20Sopenharmony_ci	format->height = MT9T001_WINDOW_HEIGHT_DEF + 1;
8118c2ecf20Sopenharmony_ci	format->field = V4L2_FIELD_NONE;
8128c2ecf20Sopenharmony_ci	format->colorspace = V4L2_COLORSPACE_SRGB;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	return mt9t001_set_power(subdev, 1);
8158c2ecf20Sopenharmony_ci}
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cistatic int mt9t001_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	return mt9t001_set_power(subdev, 0);
8208c2ecf20Sopenharmony_ci}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops mt9t001_subdev_core_ops = {
8238c2ecf20Sopenharmony_ci	.s_power = mt9t001_set_power,
8248c2ecf20Sopenharmony_ci};
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops mt9t001_subdev_video_ops = {
8278c2ecf20Sopenharmony_ci	.s_stream = mt9t001_s_stream,
8288c2ecf20Sopenharmony_ci};
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops mt9t001_subdev_pad_ops = {
8318c2ecf20Sopenharmony_ci	.enum_mbus_code = mt9t001_enum_mbus_code,
8328c2ecf20Sopenharmony_ci	.enum_frame_size = mt9t001_enum_frame_size,
8338c2ecf20Sopenharmony_ci	.get_fmt = mt9t001_get_format,
8348c2ecf20Sopenharmony_ci	.set_fmt = mt9t001_set_format,
8358c2ecf20Sopenharmony_ci	.get_selection = mt9t001_get_selection,
8368c2ecf20Sopenharmony_ci	.set_selection = mt9t001_set_selection,
8378c2ecf20Sopenharmony_ci};
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops mt9t001_subdev_ops = {
8408c2ecf20Sopenharmony_ci	.core = &mt9t001_subdev_core_ops,
8418c2ecf20Sopenharmony_ci	.video = &mt9t001_subdev_video_ops,
8428c2ecf20Sopenharmony_ci	.pad = &mt9t001_subdev_pad_ops,
8438c2ecf20Sopenharmony_ci};
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops mt9t001_subdev_internal_ops = {
8468c2ecf20Sopenharmony_ci	.registered = mt9t001_registered,
8478c2ecf20Sopenharmony_ci	.open = mt9t001_open,
8488c2ecf20Sopenharmony_ci	.close = mt9t001_close,
8498c2ecf20Sopenharmony_ci};
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic int mt9t001_probe(struct i2c_client *client,
8528c2ecf20Sopenharmony_ci			 const struct i2c_device_id *did)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct mt9t001_platform_data *pdata = client->dev.platform_data;
8558c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001;
8568c2ecf20Sopenharmony_ci	unsigned int i;
8578c2ecf20Sopenharmony_ci	int ret;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	if (pdata == NULL) {
8608c2ecf20Sopenharmony_ci		dev_err(&client->dev, "No platform data\n");
8618c2ecf20Sopenharmony_ci		return -EINVAL;
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(client->adapter,
8658c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_WORD_DATA)) {
8668c2ecf20Sopenharmony_ci		dev_warn(&client->adapter->dev,
8678c2ecf20Sopenharmony_ci			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
8688c2ecf20Sopenharmony_ci		return -EIO;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	mt9t001 = devm_kzalloc(&client->dev, sizeof(*mt9t001), GFP_KERNEL);
8728c2ecf20Sopenharmony_ci	if (!mt9t001)
8738c2ecf20Sopenharmony_ci		return -ENOMEM;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	mutex_init(&mt9t001->power_lock);
8768c2ecf20Sopenharmony_ci	mt9t001->output_control = MT9T001_OUTPUT_CONTROL_DEF;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	mt9t001->regulators[0].supply = "vdd";
8798c2ecf20Sopenharmony_ci	mt9t001->regulators[1].supply = "vaa";
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(&client->dev, 2, mt9t001->regulators);
8828c2ecf20Sopenharmony_ci	if (ret < 0) {
8838c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unable to get regulators\n");
8848c2ecf20Sopenharmony_ci		return ret;
8858c2ecf20Sopenharmony_ci	}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	mt9t001->clk = devm_clk_get(&client->dev, NULL);
8888c2ecf20Sopenharmony_ci	if (IS_ERR(mt9t001->clk)) {
8898c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Unable to get clock\n");
8908c2ecf20Sopenharmony_ci		return PTR_ERR(mt9t001->clk);
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&mt9t001->ctrls, ARRAY_SIZE(mt9t001_ctrls) +
8948c2ecf20Sopenharmony_ci						ARRAY_SIZE(mt9t001_gains) + 4);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
8978c2ecf20Sopenharmony_ci			  V4L2_CID_EXPOSURE, MT9T001_SHUTTER_WIDTH_MIN,
8988c2ecf20Sopenharmony_ci			  MT9T001_SHUTTER_WIDTH_MAX, 1,
8998c2ecf20Sopenharmony_ci			  MT9T001_SHUTTER_WIDTH_DEF);
9008c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
9018c2ecf20Sopenharmony_ci			  V4L2_CID_BLACK_LEVEL, 1, 1, 1, 1);
9028c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9t001->ctrls, &mt9t001_ctrl_ops,
9038c2ecf20Sopenharmony_ci			  V4L2_CID_PIXEL_RATE, pdata->ext_clk, pdata->ext_clk,
9048c2ecf20Sopenharmony_ci			  1, pdata->ext_clk);
9058c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std_menu_items(&mt9t001->ctrls, &mt9t001_ctrl_ops,
9068c2ecf20Sopenharmony_ci			V4L2_CID_TEST_PATTERN,
9078c2ecf20Sopenharmony_ci			ARRAY_SIZE(mt9t001_test_pattern_menu) - 1, 0,
9088c2ecf20Sopenharmony_ci			0, mt9t001_test_pattern_menu);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mt9t001_ctrls); ++i)
9118c2ecf20Sopenharmony_ci		v4l2_ctrl_new_custom(&mt9t001->ctrls, &mt9t001_ctrls[i], NULL);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mt9t001_gains); ++i)
9148c2ecf20Sopenharmony_ci		mt9t001->gains[i] = v4l2_ctrl_new_custom(&mt9t001->ctrls,
9158c2ecf20Sopenharmony_ci			&mt9t001_gains[i], NULL);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	v4l2_ctrl_cluster(ARRAY_SIZE(mt9t001_gains), mt9t001->gains);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	mt9t001->subdev.ctrl_handler = &mt9t001->ctrls;
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (mt9t001->ctrls.error) {
9228c2ecf20Sopenharmony_ci		printk(KERN_INFO "%s: control initialization error %d\n",
9238c2ecf20Sopenharmony_ci		       __func__, mt9t001->ctrls.error);
9248c2ecf20Sopenharmony_ci		ret = -EINVAL;
9258c2ecf20Sopenharmony_ci		goto done;
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	mt9t001->crop.left = MT9T001_COLUMN_START_DEF;
9298c2ecf20Sopenharmony_ci	mt9t001->crop.top = MT9T001_ROW_START_DEF;
9308c2ecf20Sopenharmony_ci	mt9t001->crop.width = MT9T001_WINDOW_WIDTH_DEF + 1;
9318c2ecf20Sopenharmony_ci	mt9t001->crop.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	mt9t001->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
9348c2ecf20Sopenharmony_ci	mt9t001->format.width = MT9T001_WINDOW_WIDTH_DEF + 1;
9358c2ecf20Sopenharmony_ci	mt9t001->format.height = MT9T001_WINDOW_HEIGHT_DEF + 1;
9368c2ecf20Sopenharmony_ci	mt9t001->format.field = V4L2_FIELD_NONE;
9378c2ecf20Sopenharmony_ci	mt9t001->format.colorspace = V4L2_COLORSPACE_SRGB;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(&mt9t001->subdev, client, &mt9t001_subdev_ops);
9408c2ecf20Sopenharmony_ci	mt9t001->subdev.internal_ops = &mt9t001_subdev_internal_ops;
9418c2ecf20Sopenharmony_ci	mt9t001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	mt9t001->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
9448c2ecf20Sopenharmony_ci	mt9t001->pad.flags = MEDIA_PAD_FL_SOURCE;
9458c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&mt9t001->subdev.entity, 1, &mt9t001->pad);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_cidone:
9488c2ecf20Sopenharmony_ci	if (ret < 0) {
9498c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(&mt9t001->ctrls);
9508c2ecf20Sopenharmony_ci		media_entity_cleanup(&mt9t001->subdev.entity);
9518c2ecf20Sopenharmony_ci	}
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	return ret;
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_cistatic int mt9t001_remove(struct i2c_client *client)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
9598c2ecf20Sopenharmony_ci	struct mt9t001 *mt9t001 = to_mt9t001(subdev);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&mt9t001->ctrls);
9628c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(subdev);
9638c2ecf20Sopenharmony_ci	media_entity_cleanup(&subdev->entity);
9648c2ecf20Sopenharmony_ci	return 0;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic const struct i2c_device_id mt9t001_id[] = {
9688c2ecf20Sopenharmony_ci	{ "mt9t001", 0 },
9698c2ecf20Sopenharmony_ci	{ }
9708c2ecf20Sopenharmony_ci};
9718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mt9t001_id);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic struct i2c_driver mt9t001_driver = {
9748c2ecf20Sopenharmony_ci	.driver = {
9758c2ecf20Sopenharmony_ci		.name = "mt9t001",
9768c2ecf20Sopenharmony_ci	},
9778c2ecf20Sopenharmony_ci	.probe		= mt9t001_probe,
9788c2ecf20Sopenharmony_ci	.remove		= mt9t001_remove,
9798c2ecf20Sopenharmony_ci	.id_table	= mt9t001_id,
9808c2ecf20Sopenharmony_ci};
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_cimodule_i2c_driver(mt9t001_driver);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aptina (Micron) MT9T001 Camera driver");
9858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
9868c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
987