18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for MT9M001 CMOS Image Sensor from Micron
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/log2.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
198c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
208c2ecf20Sopenharmony_ci#include <media/v4l2-event.h>
218c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * mt9m001 i2c address 0x5d
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* mt9m001 selected register addresses */
288c2ecf20Sopenharmony_ci#define MT9M001_CHIP_VERSION		0x00
298c2ecf20Sopenharmony_ci#define MT9M001_ROW_START		0x01
308c2ecf20Sopenharmony_ci#define MT9M001_COLUMN_START		0x02
318c2ecf20Sopenharmony_ci#define MT9M001_WINDOW_HEIGHT		0x03
328c2ecf20Sopenharmony_ci#define MT9M001_WINDOW_WIDTH		0x04
338c2ecf20Sopenharmony_ci#define MT9M001_HORIZONTAL_BLANKING	0x05
348c2ecf20Sopenharmony_ci#define MT9M001_VERTICAL_BLANKING	0x06
358c2ecf20Sopenharmony_ci#define MT9M001_OUTPUT_CONTROL		0x07
368c2ecf20Sopenharmony_ci#define MT9M001_SHUTTER_WIDTH		0x09
378c2ecf20Sopenharmony_ci#define MT9M001_FRAME_RESTART		0x0b
388c2ecf20Sopenharmony_ci#define MT9M001_SHUTTER_DELAY		0x0c
398c2ecf20Sopenharmony_ci#define MT9M001_RESET			0x0d
408c2ecf20Sopenharmony_ci#define MT9M001_READ_OPTIONS1		0x1e
418c2ecf20Sopenharmony_ci#define MT9M001_READ_OPTIONS2		0x20
428c2ecf20Sopenharmony_ci#define MT9M001_GLOBAL_GAIN		0x35
438c2ecf20Sopenharmony_ci#define MT9M001_CHIP_ENABLE		0xF1
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define MT9M001_MAX_WIDTH		1280
468c2ecf20Sopenharmony_ci#define MT9M001_MAX_HEIGHT		1024
478c2ecf20Sopenharmony_ci#define MT9M001_MIN_WIDTH		48
488c2ecf20Sopenharmony_ci#define MT9M001_MIN_HEIGHT		32
498c2ecf20Sopenharmony_ci#define MT9M001_COLUMN_SKIP		20
508c2ecf20Sopenharmony_ci#define MT9M001_ROW_SKIP		12
518c2ecf20Sopenharmony_ci#define MT9M001_DEFAULT_HBLANK		9
528c2ecf20Sopenharmony_ci#define MT9M001_DEFAULT_VBLANK		25
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* MT9M001 has only one fixed colorspace per pixelcode */
558c2ecf20Sopenharmony_cistruct mt9m001_datafmt {
568c2ecf20Sopenharmony_ci	u32	code;
578c2ecf20Sopenharmony_ci	enum v4l2_colorspace		colorspace;
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* Find a data format by a pixel code in an array */
618c2ecf20Sopenharmony_cistatic const struct mt9m001_datafmt *mt9m001_find_datafmt(
628c2ecf20Sopenharmony_ci	u32 code, const struct mt9m001_datafmt *fmt,
638c2ecf20Sopenharmony_ci	int n)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int i;
668c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
678c2ecf20Sopenharmony_ci		if (fmt[i].code == code)
688c2ecf20Sopenharmony_ci			return fmt + i;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return NULL;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
748c2ecf20Sopenharmony_ci	/*
758c2ecf20Sopenharmony_ci	 * Order important: first natively supported,
768c2ecf20Sopenharmony_ci	 * second supported with a GPIO extender
778c2ecf20Sopenharmony_ci	 */
788c2ecf20Sopenharmony_ci	{MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
798c2ecf20Sopenharmony_ci	{MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
838c2ecf20Sopenharmony_ci	/* Order important - see above */
848c2ecf20Sopenharmony_ci	{MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
858c2ecf20Sopenharmony_ci	{MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistruct mt9m001 {
898c2ecf20Sopenharmony_ci	struct v4l2_subdev subdev;
908c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
918c2ecf20Sopenharmony_ci	struct {
928c2ecf20Sopenharmony_ci		/* exposure/auto-exposure cluster */
938c2ecf20Sopenharmony_ci		struct v4l2_ctrl *autoexposure;
948c2ecf20Sopenharmony_ci		struct v4l2_ctrl *exposure;
958c2ecf20Sopenharmony_ci	};
968c2ecf20Sopenharmony_ci	bool streaming;
978c2ecf20Sopenharmony_ci	struct mutex mutex;
988c2ecf20Sopenharmony_ci	struct v4l2_rect rect;	/* Sensor window */
998c2ecf20Sopenharmony_ci	struct clk *clk;
1008c2ecf20Sopenharmony_ci	struct gpio_desc *standby_gpio;
1018c2ecf20Sopenharmony_ci	struct gpio_desc *reset_gpio;
1028c2ecf20Sopenharmony_ci	const struct mt9m001_datafmt *fmt;
1038c2ecf20Sopenharmony_ci	const struct mt9m001_datafmt *fmts;
1048c2ecf20Sopenharmony_ci	int num_fmts;
1058c2ecf20Sopenharmony_ci	unsigned int total_h;
1068c2ecf20Sopenharmony_ci	unsigned short y_skip_top;	/* Lines to skip at the top */
1078c2ecf20Sopenharmony_ci	struct media_pad pad;
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic struct mt9m001 *to_mt9m001(const struct i2c_client *client)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int reg_read(struct i2c_client *client, const u8 reg)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	return i2c_smbus_read_word_swapped(client, reg);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int reg_write(struct i2c_client *client, const u8 reg,
1218c2ecf20Sopenharmony_ci		     const u16 data)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	return i2c_smbus_write_word_swapped(client, reg, data);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int reg_set(struct i2c_client *client, const u8 reg,
1278c2ecf20Sopenharmony_ci		   const u16 data)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	int ret;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	ret = reg_read(client, reg);
1328c2ecf20Sopenharmony_ci	if (ret < 0)
1338c2ecf20Sopenharmony_ci		return ret;
1348c2ecf20Sopenharmony_ci	return reg_write(client, reg, ret | data);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int reg_clear(struct i2c_client *client, const u8 reg,
1388c2ecf20Sopenharmony_ci		     const u16 data)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int ret;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ret = reg_read(client, reg);
1438c2ecf20Sopenharmony_ci	if (ret < 0)
1448c2ecf20Sopenharmony_ci		return ret;
1458c2ecf20Sopenharmony_ci	return reg_write(client, reg, ret & ~data);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistruct mt9m001_reg {
1498c2ecf20Sopenharmony_ci	u8 reg;
1508c2ecf20Sopenharmony_ci	u16 data;
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int multi_reg_write(struct i2c_client *client,
1548c2ecf20Sopenharmony_ci			   const struct mt9m001_reg *regs, int num)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	int i;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++) {
1598c2ecf20Sopenharmony_ci		int ret = reg_write(client, regs[i].reg, regs[i].data);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		if (ret)
1628c2ecf20Sopenharmony_ci			return ret;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return 0;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic int mt9m001_init(struct i2c_client *client)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	static const struct mt9m001_reg init_regs[] = {
1718c2ecf20Sopenharmony_ci		/*
1728c2ecf20Sopenharmony_ci		 * Issue a soft reset. This returns all registers to their
1738c2ecf20Sopenharmony_ci		 * default values.
1748c2ecf20Sopenharmony_ci		 */
1758c2ecf20Sopenharmony_ci		{ MT9M001_RESET, 1 },
1768c2ecf20Sopenharmony_ci		{ MT9M001_RESET, 0 },
1778c2ecf20Sopenharmony_ci		/* Disable chip, synchronous option update */
1788c2ecf20Sopenharmony_ci		{ MT9M001_OUTPUT_CONTROL, 0 }
1798c2ecf20Sopenharmony_ci	};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "%s\n", __func__);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return multi_reg_write(client, init_regs, ARRAY_SIZE(init_regs));
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int mt9m001_apply_selection(struct v4l2_subdev *sd)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
1898c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
1908c2ecf20Sopenharmony_ci	const struct mt9m001_reg regs[] = {
1918c2ecf20Sopenharmony_ci		/* Blanking and start values - default... */
1928c2ecf20Sopenharmony_ci		{ MT9M001_HORIZONTAL_BLANKING, MT9M001_DEFAULT_HBLANK },
1938c2ecf20Sopenharmony_ci		{ MT9M001_VERTICAL_BLANKING, MT9M001_DEFAULT_VBLANK },
1948c2ecf20Sopenharmony_ci		/*
1958c2ecf20Sopenharmony_ci		 * The caller provides a supported format, as verified per
1968c2ecf20Sopenharmony_ci		 * call to .set_fmt(FORMAT_TRY).
1978c2ecf20Sopenharmony_ci		 */
1988c2ecf20Sopenharmony_ci		{ MT9M001_COLUMN_START, mt9m001->rect.left },
1998c2ecf20Sopenharmony_ci		{ MT9M001_ROW_START, mt9m001->rect.top },
2008c2ecf20Sopenharmony_ci		{ MT9M001_WINDOW_WIDTH, mt9m001->rect.width - 1 },
2018c2ecf20Sopenharmony_ci		{ MT9M001_WINDOW_HEIGHT,
2028c2ecf20Sopenharmony_ci			mt9m001->rect.height + mt9m001->y_skip_top - 1 },
2038c2ecf20Sopenharmony_ci	};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return multi_reg_write(client, regs, ARRAY_SIZE(regs));
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2118c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
2128c2ecf20Sopenharmony_ci	int ret = 0;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	mutex_lock(&mt9m001->mutex);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (mt9m001->streaming == enable)
2178c2ecf20Sopenharmony_ci		goto done;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (enable) {
2208c2ecf20Sopenharmony_ci		ret = pm_runtime_get_sync(&client->dev);
2218c2ecf20Sopenharmony_ci		if (ret < 0)
2228c2ecf20Sopenharmony_ci			goto put_unlock;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		ret = mt9m001_apply_selection(sd);
2258c2ecf20Sopenharmony_ci		if (ret)
2268c2ecf20Sopenharmony_ci			goto put_unlock;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		ret = __v4l2_ctrl_handler_setup(&mt9m001->hdl);
2298c2ecf20Sopenharmony_ci		if (ret)
2308c2ecf20Sopenharmony_ci			goto put_unlock;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		/* Switch to master "normal" mode */
2338c2ecf20Sopenharmony_ci		ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 2);
2348c2ecf20Sopenharmony_ci		if (ret < 0)
2358c2ecf20Sopenharmony_ci			goto put_unlock;
2368c2ecf20Sopenharmony_ci	} else {
2378c2ecf20Sopenharmony_ci		/* Switch to master stop sensor readout */
2388c2ecf20Sopenharmony_ci		reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
2398c2ecf20Sopenharmony_ci		pm_runtime_put(&client->dev);
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	mt9m001->streaming = enable;
2438c2ecf20Sopenharmony_cidone:
2448c2ecf20Sopenharmony_ci	mutex_unlock(&mt9m001->mutex);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return 0;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciput_unlock:
2498c2ecf20Sopenharmony_ci	pm_runtime_put(&client->dev);
2508c2ecf20Sopenharmony_ci	mutex_unlock(&mt9m001->mutex);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return ret;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic int mt9m001_set_selection(struct v4l2_subdev *sd,
2568c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
2578c2ecf20Sopenharmony_ci		struct v4l2_subdev_selection *sel)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2608c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
2618c2ecf20Sopenharmony_ci	struct v4l2_rect rect = sel->r;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
2648c2ecf20Sopenharmony_ci	    sel->target != V4L2_SEL_TGT_CROP)
2658c2ecf20Sopenharmony_ci		return -EINVAL;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (mt9m001->fmts == mt9m001_colour_fmts)
2688c2ecf20Sopenharmony_ci		/*
2698c2ecf20Sopenharmony_ci		 * Bayer format - even number of rows for simplicity,
2708c2ecf20Sopenharmony_ci		 * but let the user play with the top row.
2718c2ecf20Sopenharmony_ci		 */
2728c2ecf20Sopenharmony_ci		rect.height = ALIGN(rect.height, 2);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Datasheet requirement: see register description */
2758c2ecf20Sopenharmony_ci	rect.width = ALIGN(rect.width, 2);
2768c2ecf20Sopenharmony_ci	rect.left = ALIGN(rect.left, 2);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	rect.width = clamp_t(u32, rect.width, MT9M001_MIN_WIDTH,
2798c2ecf20Sopenharmony_ci			MT9M001_MAX_WIDTH);
2808c2ecf20Sopenharmony_ci	rect.left = clamp_t(u32, rect.left, MT9M001_COLUMN_SKIP,
2818c2ecf20Sopenharmony_ci			MT9M001_COLUMN_SKIP + MT9M001_MAX_WIDTH - rect.width);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	rect.height = clamp_t(u32, rect.height, MT9M001_MIN_HEIGHT,
2848c2ecf20Sopenharmony_ci			MT9M001_MAX_HEIGHT);
2858c2ecf20Sopenharmony_ci	rect.top = clamp_t(u32, rect.top, MT9M001_ROW_SKIP,
2868c2ecf20Sopenharmony_ci			MT9M001_ROW_SKIP + MT9M001_MAX_HEIGHT - rect.height);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	mt9m001->total_h = rect.height + mt9m001->y_skip_top +
2898c2ecf20Sopenharmony_ci			   MT9M001_DEFAULT_VBLANK;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	mt9m001->rect = rect;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	return 0;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic int mt9m001_get_selection(struct v4l2_subdev *sd,
2978c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
2988c2ecf20Sopenharmony_ci		struct v4l2_subdev_selection *sel)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3018c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
3048c2ecf20Sopenharmony_ci		return -EINVAL;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	switch (sel->target) {
3078c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP_BOUNDS:
3088c2ecf20Sopenharmony_ci		sel->r.left = MT9M001_COLUMN_SKIP;
3098c2ecf20Sopenharmony_ci		sel->r.top = MT9M001_ROW_SKIP;
3108c2ecf20Sopenharmony_ci		sel->r.width = MT9M001_MAX_WIDTH;
3118c2ecf20Sopenharmony_ci		sel->r.height = MT9M001_MAX_HEIGHT;
3128c2ecf20Sopenharmony_ci		return 0;
3138c2ecf20Sopenharmony_ci	case V4L2_SEL_TGT_CROP:
3148c2ecf20Sopenharmony_ci		sel->r = mt9m001->rect;
3158c2ecf20Sopenharmony_ci		return 0;
3168c2ecf20Sopenharmony_ci	default:
3178c2ecf20Sopenharmony_ci		return -EINVAL;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic int mt9m001_get_fmt(struct v4l2_subdev *sd,
3228c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
3238c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3268c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
3278c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf = &format->format;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (format->pad)
3308c2ecf20Sopenharmony_ci		return -EINVAL;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
3338c2ecf20Sopenharmony_ci		mf = v4l2_subdev_get_try_format(sd, cfg, 0);
3348c2ecf20Sopenharmony_ci		format->format = *mf;
3358c2ecf20Sopenharmony_ci		return 0;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	mf->width	= mt9m001->rect.width;
3398c2ecf20Sopenharmony_ci	mf->height	= mt9m001->rect.height;
3408c2ecf20Sopenharmony_ci	mf->code	= mt9m001->fmt->code;
3418c2ecf20Sopenharmony_ci	mf->colorspace	= mt9m001->fmt->colorspace;
3428c2ecf20Sopenharmony_ci	mf->field	= V4L2_FIELD_NONE;
3438c2ecf20Sopenharmony_ci	mf->ycbcr_enc	= V4L2_YCBCR_ENC_DEFAULT;
3448c2ecf20Sopenharmony_ci	mf->quantization = V4L2_QUANTIZATION_DEFAULT;
3458c2ecf20Sopenharmony_ci	mf->xfer_func	= V4L2_XFER_FUNC_DEFAULT;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	return 0;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int mt9m001_s_fmt(struct v4l2_subdev *sd,
3518c2ecf20Sopenharmony_ci			 const struct mt9m001_datafmt *fmt,
3528c2ecf20Sopenharmony_ci			 struct v4l2_mbus_framefmt *mf)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3558c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
3568c2ecf20Sopenharmony_ci	struct v4l2_subdev_selection sel = {
3578c2ecf20Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
3588c2ecf20Sopenharmony_ci		.target = V4L2_SEL_TGT_CROP,
3598c2ecf20Sopenharmony_ci		.r.left = mt9m001->rect.left,
3608c2ecf20Sopenharmony_ci		.r.top = mt9m001->rect.top,
3618c2ecf20Sopenharmony_ci		.r.width = mf->width,
3628c2ecf20Sopenharmony_ci		.r.height = mf->height,
3638c2ecf20Sopenharmony_ci	};
3648c2ecf20Sopenharmony_ci	int ret;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* No support for scaling so far, just crop. TODO: use skipping */
3678c2ecf20Sopenharmony_ci	ret = mt9m001_set_selection(sd, NULL, &sel);
3688c2ecf20Sopenharmony_ci	if (!ret) {
3698c2ecf20Sopenharmony_ci		mf->width	= mt9m001->rect.width;
3708c2ecf20Sopenharmony_ci		mf->height	= mt9m001->rect.height;
3718c2ecf20Sopenharmony_ci		mt9m001->fmt	= fmt;
3728c2ecf20Sopenharmony_ci		mf->colorspace	= fmt->colorspace;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return ret;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic int mt9m001_set_fmt(struct v4l2_subdev *sd,
3798c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
3808c2ecf20Sopenharmony_ci		struct v4l2_subdev_format *format)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf = &format->format;
3838c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3848c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
3858c2ecf20Sopenharmony_ci	const struct mt9m001_datafmt *fmt;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	if (format->pad)
3888c2ecf20Sopenharmony_ci		return -EINVAL;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
3918c2ecf20Sopenharmony_ci		MT9M001_MAX_WIDTH, 1,
3928c2ecf20Sopenharmony_ci		&mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
3938c2ecf20Sopenharmony_ci		MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (mt9m001->fmts == mt9m001_colour_fmts)
3968c2ecf20Sopenharmony_ci		mf->height = ALIGN(mf->height - 1, 2);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
3998c2ecf20Sopenharmony_ci				   mt9m001->num_fmts);
4008c2ecf20Sopenharmony_ci	if (!fmt) {
4018c2ecf20Sopenharmony_ci		fmt = mt9m001->fmt;
4028c2ecf20Sopenharmony_ci		mf->code = fmt->code;
4038c2ecf20Sopenharmony_ci	}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	mf->colorspace	= fmt->colorspace;
4068c2ecf20Sopenharmony_ci	mf->field	= V4L2_FIELD_NONE;
4078c2ecf20Sopenharmony_ci	mf->ycbcr_enc	= V4L2_YCBCR_ENC_DEFAULT;
4088c2ecf20Sopenharmony_ci	mf->quantization = V4L2_QUANTIZATION_DEFAULT;
4098c2ecf20Sopenharmony_ci	mf->xfer_func	= V4L2_XFER_FUNC_DEFAULT;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
4128c2ecf20Sopenharmony_ci		return mt9m001_s_fmt(sd, fmt, mf);
4138c2ecf20Sopenharmony_ci	cfg->try_fmt = *mf;
4148c2ecf20Sopenharmony_ci	return 0;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
4188c2ecf20Sopenharmony_cistatic int mt9m001_g_register(struct v4l2_subdev *sd,
4198c2ecf20Sopenharmony_ci			      struct v4l2_dbg_register *reg)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (reg->reg > 0xff)
4248c2ecf20Sopenharmony_ci		return -EINVAL;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	reg->size = 2;
4278c2ecf20Sopenharmony_ci	reg->val = reg_read(client, reg->reg);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if (reg->val > 0xffff)
4308c2ecf20Sopenharmony_ci		return -EIO;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	return 0;
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic int mt9m001_s_register(struct v4l2_subdev *sd,
4368c2ecf20Sopenharmony_ci			      const struct v4l2_dbg_register *reg)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	if (reg->reg > 0xff)
4418c2ecf20Sopenharmony_ci		return -EINVAL;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (reg_write(client, reg->reg, reg->val) < 0)
4448c2ecf20Sopenharmony_ci		return -EIO;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	return 0;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci#endif
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int mt9m001_power_on(struct device *dev)
4518c2ecf20Sopenharmony_ci{
4528c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
4538c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
4548c2ecf20Sopenharmony_ci	int ret;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(mt9m001->clk);
4578c2ecf20Sopenharmony_ci	if (ret)
4588c2ecf20Sopenharmony_ci		return ret;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (mt9m001->standby_gpio) {
4618c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(mt9m001->standby_gpio, 0);
4628c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	if (mt9m001->reset_gpio) {
4668c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(mt9m001->reset_gpio, 1);
4678c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
4688c2ecf20Sopenharmony_ci		gpiod_set_value_cansleep(mt9m001->reset_gpio, 0);
4698c2ecf20Sopenharmony_ci		usleep_range(1000, 2000);
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	return 0;
4738c2ecf20Sopenharmony_ci}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic int mt9m001_power_off(struct device *dev)
4768c2ecf20Sopenharmony_ci{
4778c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
4788c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(mt9m001->standby_gpio, 1);
4818c2ecf20Sopenharmony_ci	clk_disable_unprepare(mt9m001->clk);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	return 0;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_cistatic int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = container_of(ctrl->handler,
4898c2ecf20Sopenharmony_ci					       struct mt9m001, hdl);
4908c2ecf20Sopenharmony_ci	s32 min, max;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	switch (ctrl->id) {
4938c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE_AUTO:
4948c2ecf20Sopenharmony_ci		min = mt9m001->exposure->minimum;
4958c2ecf20Sopenharmony_ci		max = mt9m001->exposure->maximum;
4968c2ecf20Sopenharmony_ci		mt9m001->exposure->val =
4978c2ecf20Sopenharmony_ci			(524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
4988c2ecf20Sopenharmony_ci		break;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci	return 0;
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = container_of(ctrl->handler,
5068c2ecf20Sopenharmony_ci					       struct mt9m001, hdl);
5078c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &mt9m001->subdev;
5088c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
5098c2ecf20Sopenharmony_ci	struct v4l2_ctrl *exp = mt9m001->exposure;
5108c2ecf20Sopenharmony_ci	int data;
5118c2ecf20Sopenharmony_ci	int ret;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (!pm_runtime_get_if_in_use(&client->dev))
5148c2ecf20Sopenharmony_ci		return 0;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	switch (ctrl->id) {
5178c2ecf20Sopenharmony_ci	case V4L2_CID_VFLIP:
5188c2ecf20Sopenharmony_ci		if (ctrl->val)
5198c2ecf20Sopenharmony_ci			ret = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
5208c2ecf20Sopenharmony_ci		else
5218c2ecf20Sopenharmony_ci			ret = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
5228c2ecf20Sopenharmony_ci		break;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	case V4L2_CID_GAIN:
5258c2ecf20Sopenharmony_ci		/* See Datasheet Table 7, Gain settings. */
5268c2ecf20Sopenharmony_ci		if (ctrl->val <= ctrl->default_value) {
5278c2ecf20Sopenharmony_ci			/* Pack it into 0..1 step 0.125, register values 0..8 */
5288c2ecf20Sopenharmony_ci			unsigned long range = ctrl->default_value - ctrl->minimum;
5298c2ecf20Sopenharmony_ci			data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci			dev_dbg(&client->dev, "Setting gain %d\n", data);
5328c2ecf20Sopenharmony_ci			ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
5338c2ecf20Sopenharmony_ci		} else {
5348c2ecf20Sopenharmony_ci			/* Pack it into 1.125..15 variable step, register values 9..67 */
5358c2ecf20Sopenharmony_ci			/* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
5368c2ecf20Sopenharmony_ci			unsigned long range = ctrl->maximum - ctrl->default_value - 1;
5378c2ecf20Sopenharmony_ci			unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
5388c2ecf20Sopenharmony_ci					       111 + range / 2) / range + 9;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci			if (gain <= 32)
5418c2ecf20Sopenharmony_ci				data = gain;
5428c2ecf20Sopenharmony_ci			else if (gain <= 64)
5438c2ecf20Sopenharmony_ci				data = ((gain - 32) * 16 + 16) / 32 + 80;
5448c2ecf20Sopenharmony_ci			else
5458c2ecf20Sopenharmony_ci				data = ((gain - 64) * 7 + 28) / 56 + 96;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci			dev_dbg(&client->dev, "Setting gain from %d to %d\n",
5488c2ecf20Sopenharmony_ci				 reg_read(client, MT9M001_GLOBAL_GAIN), data);
5498c2ecf20Sopenharmony_ci			ret = reg_write(client, MT9M001_GLOBAL_GAIN, data);
5508c2ecf20Sopenharmony_ci		}
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	case V4L2_CID_EXPOSURE_AUTO:
5548c2ecf20Sopenharmony_ci		if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
5558c2ecf20Sopenharmony_ci			unsigned long range = exp->maximum - exp->minimum;
5568c2ecf20Sopenharmony_ci			unsigned long shutter = ((exp->val - (s32)exp->minimum) * 1048 +
5578c2ecf20Sopenharmony_ci						 range / 2) / range + 1;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci			dev_dbg(&client->dev,
5608c2ecf20Sopenharmony_ci				"Setting shutter width from %d to %lu\n",
5618c2ecf20Sopenharmony_ci				reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
5628c2ecf20Sopenharmony_ci			ret = reg_write(client, MT9M001_SHUTTER_WIDTH, shutter);
5638c2ecf20Sopenharmony_ci		} else {
5648c2ecf20Sopenharmony_ci			mt9m001->total_h = mt9m001->rect.height +
5658c2ecf20Sopenharmony_ci				mt9m001->y_skip_top + MT9M001_DEFAULT_VBLANK;
5668c2ecf20Sopenharmony_ci			ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
5678c2ecf20Sopenharmony_ci					mt9m001->total_h);
5688c2ecf20Sopenharmony_ci		}
5698c2ecf20Sopenharmony_ci		break;
5708c2ecf20Sopenharmony_ci	default:
5718c2ecf20Sopenharmony_ci		ret = -EINVAL;
5728c2ecf20Sopenharmony_ci		break;
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	pm_runtime_put(&client->dev);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	return ret;
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci/*
5818c2ecf20Sopenharmony_ci * Interface active, can use i2c. If it fails, it can indeed mean, that
5828c2ecf20Sopenharmony_ci * this wasn't our capture interface, so, we wait for the right one
5838c2ecf20Sopenharmony_ci */
5848c2ecf20Sopenharmony_cistatic int mt9m001_video_probe(struct i2c_client *client)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
5878c2ecf20Sopenharmony_ci	s32 data;
5888c2ecf20Sopenharmony_ci	int ret;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	/* Enable the chip */
5918c2ecf20Sopenharmony_ci	data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
5928c2ecf20Sopenharmony_ci	dev_dbg(&client->dev, "write: %d\n", data);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* Read out the chip version register */
5958c2ecf20Sopenharmony_ci	data = reg_read(client, MT9M001_CHIP_VERSION);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
5988c2ecf20Sopenharmony_ci	switch (data) {
5998c2ecf20Sopenharmony_ci	case 0x8411:
6008c2ecf20Sopenharmony_ci	case 0x8421:
6018c2ecf20Sopenharmony_ci		mt9m001->fmts = mt9m001_colour_fmts;
6028c2ecf20Sopenharmony_ci		mt9m001->num_fmts = ARRAY_SIZE(mt9m001_colour_fmts);
6038c2ecf20Sopenharmony_ci		break;
6048c2ecf20Sopenharmony_ci	case 0x8431:
6058c2ecf20Sopenharmony_ci		mt9m001->fmts = mt9m001_monochrome_fmts;
6068c2ecf20Sopenharmony_ci		mt9m001->num_fmts = ARRAY_SIZE(mt9m001_monochrome_fmts);
6078c2ecf20Sopenharmony_ci		break;
6088c2ecf20Sopenharmony_ci	default:
6098c2ecf20Sopenharmony_ci		dev_err(&client->dev,
6108c2ecf20Sopenharmony_ci			"No MT9M001 chip detected, register read %x\n", data);
6118c2ecf20Sopenharmony_ci		ret = -ENODEV;
6128c2ecf20Sopenharmony_ci		goto done;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	mt9m001->fmt = &mt9m001->fmts[0];
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
6188c2ecf20Sopenharmony_ci		 data == 0x8431 ? "C12STM" : "C12ST");
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	ret = mt9m001_init(client);
6218c2ecf20Sopenharmony_ci	if (ret < 0) {
6228c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to initialise the camera\n");
6238c2ecf20Sopenharmony_ci		goto done;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/* mt9m001_init() has reset the chip, returning registers to defaults */
6278c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_handler_setup(&mt9m001->hdl);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cidone:
6308c2ecf20Sopenharmony_ci	return ret;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
6368c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	*lines = mt9m001->y_skip_top;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	return 0;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
6448c2ecf20Sopenharmony_ci	.g_volatile_ctrl = mt9m001_g_volatile_ctrl,
6458c2ecf20Sopenharmony_ci	.s_ctrl = mt9m001_s_ctrl,
6468c2ecf20Sopenharmony_ci};
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
6498c2ecf20Sopenharmony_ci	.log_status = v4l2_ctrl_subdev_log_status,
6508c2ecf20Sopenharmony_ci	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
6518c2ecf20Sopenharmony_ci	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
6528c2ecf20Sopenharmony_ci#ifdef CONFIG_VIDEO_ADV_DEBUG
6538c2ecf20Sopenharmony_ci	.g_register	= mt9m001_g_register,
6548c2ecf20Sopenharmony_ci	.s_register	= mt9m001_s_register,
6558c2ecf20Sopenharmony_ci#endif
6568c2ecf20Sopenharmony_ci};
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic int mt9m001_init_cfg(struct v4l2_subdev *sd,
6598c2ecf20Sopenharmony_ci			    struct v4l2_subdev_pad_config *cfg)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
6628c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
6638c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *try_fmt =
6648c2ecf20Sopenharmony_ci		v4l2_subdev_get_try_format(sd, cfg, 0);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	try_fmt->width		= MT9M001_MAX_WIDTH;
6678c2ecf20Sopenharmony_ci	try_fmt->height		= MT9M001_MAX_HEIGHT;
6688c2ecf20Sopenharmony_ci	try_fmt->code		= mt9m001->fmts[0].code;
6698c2ecf20Sopenharmony_ci	try_fmt->colorspace	= mt9m001->fmts[0].colorspace;
6708c2ecf20Sopenharmony_ci	try_fmt->field		= V4L2_FIELD_NONE;
6718c2ecf20Sopenharmony_ci	try_fmt->ycbcr_enc	= V4L2_YCBCR_ENC_DEFAULT;
6728c2ecf20Sopenharmony_ci	try_fmt->quantization	= V4L2_QUANTIZATION_DEFAULT;
6738c2ecf20Sopenharmony_ci	try_fmt->xfer_func	= V4L2_XFER_FUNC_DEFAULT;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	return 0;
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_cistatic int mt9m001_enum_mbus_code(struct v4l2_subdev *sd,
6798c2ecf20Sopenharmony_ci		struct v4l2_subdev_pad_config *cfg,
6808c2ecf20Sopenharmony_ci		struct v4l2_subdev_mbus_code_enum *code)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
6838c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	if (code->pad || code->index >= mt9m001->num_fmts)
6868c2ecf20Sopenharmony_ci		return -EINVAL;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	code->code = mt9m001->fmts[code->index].code;
6898c2ecf20Sopenharmony_ci	return 0;
6908c2ecf20Sopenharmony_ci}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic int mt9m001_get_mbus_config(struct v4l2_subdev *sd,
6938c2ecf20Sopenharmony_ci				   unsigned int pad,
6948c2ecf20Sopenharmony_ci				   struct v4l2_mbus_config *cfg)
6958c2ecf20Sopenharmony_ci{
6968c2ecf20Sopenharmony_ci	/* MT9M001 has all capture_format parameters fixed */
6978c2ecf20Sopenharmony_ci	cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
6988c2ecf20Sopenharmony_ci		V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
6998c2ecf20Sopenharmony_ci		V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
7008c2ecf20Sopenharmony_ci	cfg->type = V4L2_MBUS_PARALLEL;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	return 0;
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
7068c2ecf20Sopenharmony_ci	.s_stream	= mt9m001_s_stream,
7078c2ecf20Sopenharmony_ci};
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
7108c2ecf20Sopenharmony_ci	.g_skip_top_lines	= mt9m001_g_skip_top_lines,
7118c2ecf20Sopenharmony_ci};
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops mt9m001_subdev_pad_ops = {
7148c2ecf20Sopenharmony_ci	.init_cfg	= mt9m001_init_cfg,
7158c2ecf20Sopenharmony_ci	.enum_mbus_code = mt9m001_enum_mbus_code,
7168c2ecf20Sopenharmony_ci	.get_selection	= mt9m001_get_selection,
7178c2ecf20Sopenharmony_ci	.set_selection	= mt9m001_set_selection,
7188c2ecf20Sopenharmony_ci	.get_fmt	= mt9m001_get_fmt,
7198c2ecf20Sopenharmony_ci	.set_fmt	= mt9m001_set_fmt,
7208c2ecf20Sopenharmony_ci	.get_mbus_config = mt9m001_get_mbus_config,
7218c2ecf20Sopenharmony_ci};
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops mt9m001_subdev_ops = {
7248c2ecf20Sopenharmony_ci	.core	= &mt9m001_subdev_core_ops,
7258c2ecf20Sopenharmony_ci	.video	= &mt9m001_subdev_video_ops,
7268c2ecf20Sopenharmony_ci	.sensor	= &mt9m001_subdev_sensor_ops,
7278c2ecf20Sopenharmony_ci	.pad	= &mt9m001_subdev_pad_ops,
7288c2ecf20Sopenharmony_ci};
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic int mt9m001_probe(struct i2c_client *client)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001;
7338c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
7348c2ecf20Sopenharmony_ci	int ret;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
7378c2ecf20Sopenharmony_ci		dev_warn(&adapter->dev,
7388c2ecf20Sopenharmony_ci			 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
7398c2ecf20Sopenharmony_ci		return -EIO;
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	mt9m001 = devm_kzalloc(&client->dev, sizeof(*mt9m001), GFP_KERNEL);
7438c2ecf20Sopenharmony_ci	if (!mt9m001)
7448c2ecf20Sopenharmony_ci		return -ENOMEM;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	mt9m001->clk = devm_clk_get(&client->dev, NULL);
7478c2ecf20Sopenharmony_ci	if (IS_ERR(mt9m001->clk))
7488c2ecf20Sopenharmony_ci		return PTR_ERR(mt9m001->clk);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	mt9m001->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby",
7518c2ecf20Sopenharmony_ci							GPIOD_OUT_LOW);
7528c2ecf20Sopenharmony_ci	if (IS_ERR(mt9m001->standby_gpio))
7538c2ecf20Sopenharmony_ci		return PTR_ERR(mt9m001->standby_gpio);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	mt9m001->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
7568c2ecf20Sopenharmony_ci						      GPIOD_OUT_LOW);
7578c2ecf20Sopenharmony_ci	if (IS_ERR(mt9m001->reset_gpio))
7588c2ecf20Sopenharmony_ci		return PTR_ERR(mt9m001->reset_gpio);
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
7618c2ecf20Sopenharmony_ci	mt9m001->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
7628c2ecf20Sopenharmony_ci				 V4L2_SUBDEV_FL_HAS_EVENTS;
7638c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
7648c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
7658c2ecf20Sopenharmony_ci			V4L2_CID_VFLIP, 0, 1, 1, 0);
7668c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
7678c2ecf20Sopenharmony_ci			V4L2_CID_GAIN, 0, 127, 1, 64);
7688c2ecf20Sopenharmony_ci	mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
7698c2ecf20Sopenharmony_ci			V4L2_CID_EXPOSURE, 1, 255, 1, 255);
7708c2ecf20Sopenharmony_ci	/*
7718c2ecf20Sopenharmony_ci	 * Simulated autoexposure. If enabled, we calculate shutter width
7728c2ecf20Sopenharmony_ci	 * ourselves in the driver based on vertical blanking and frame width
7738c2ecf20Sopenharmony_ci	 */
7748c2ecf20Sopenharmony_ci	mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
7758c2ecf20Sopenharmony_ci			&mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
7768c2ecf20Sopenharmony_ci			V4L2_EXPOSURE_AUTO);
7778c2ecf20Sopenharmony_ci	mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
7788c2ecf20Sopenharmony_ci	if (mt9m001->hdl.error)
7798c2ecf20Sopenharmony_ci		return mt9m001->hdl.error;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
7828c2ecf20Sopenharmony_ci					V4L2_EXPOSURE_MANUAL, true);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	mutex_init(&mt9m001->mutex);
7858c2ecf20Sopenharmony_ci	mt9m001->hdl.lock = &mt9m001->mutex;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	/* Second stage probe - when a capture adapter is there */
7888c2ecf20Sopenharmony_ci	mt9m001->y_skip_top	= 0;
7898c2ecf20Sopenharmony_ci	mt9m001->rect.left	= MT9M001_COLUMN_SKIP;
7908c2ecf20Sopenharmony_ci	mt9m001->rect.top	= MT9M001_ROW_SKIP;
7918c2ecf20Sopenharmony_ci	mt9m001->rect.width	= MT9M001_MAX_WIDTH;
7928c2ecf20Sopenharmony_ci	mt9m001->rect.height	= MT9M001_MAX_HEIGHT;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	ret = mt9m001_power_on(&client->dev);
7958c2ecf20Sopenharmony_ci	if (ret)
7968c2ecf20Sopenharmony_ci		goto error_hdl_free;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	pm_runtime_set_active(&client->dev);
7998c2ecf20Sopenharmony_ci	pm_runtime_enable(&client->dev);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	ret = mt9m001_video_probe(client);
8028c2ecf20Sopenharmony_ci	if (ret)
8038c2ecf20Sopenharmony_ci		goto error_power_off;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	mt9m001->pad.flags = MEDIA_PAD_FL_SOURCE;
8068c2ecf20Sopenharmony_ci	mt9m001->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
8078c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&mt9m001->subdev.entity, 1, &mt9m001->pad);
8088c2ecf20Sopenharmony_ci	if (ret)
8098c2ecf20Sopenharmony_ci		goto error_power_off;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	ret = v4l2_async_register_subdev(&mt9m001->subdev);
8128c2ecf20Sopenharmony_ci	if (ret)
8138c2ecf20Sopenharmony_ci		goto error_entity_cleanup;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	pm_runtime_idle(&client->dev);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	return 0;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_cierror_entity_cleanup:
8208c2ecf20Sopenharmony_ci	media_entity_cleanup(&mt9m001->subdev.entity);
8218c2ecf20Sopenharmony_cierror_power_off:
8228c2ecf20Sopenharmony_ci	pm_runtime_disable(&client->dev);
8238c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&client->dev);
8248c2ecf20Sopenharmony_ci	mt9m001_power_off(&client->dev);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cierror_hdl_free:
8278c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&mt9m001->hdl);
8288c2ecf20Sopenharmony_ci	mutex_destroy(&mt9m001->mutex);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	return ret;
8318c2ecf20Sopenharmony_ci}
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_cistatic int mt9m001_remove(struct i2c_client *client)
8348c2ecf20Sopenharmony_ci{
8358c2ecf20Sopenharmony_ci	struct mt9m001 *mt9m001 = to_mt9m001(client);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	pm_runtime_get_sync(&client->dev);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	v4l2_async_unregister_subdev(&mt9m001->subdev);
8408c2ecf20Sopenharmony_ci	media_entity_cleanup(&mt9m001->subdev.entity);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	pm_runtime_disable(&client->dev);
8438c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&client->dev);
8448c2ecf20Sopenharmony_ci	pm_runtime_put_noidle(&client->dev);
8458c2ecf20Sopenharmony_ci	mt9m001_power_off(&client->dev);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&mt9m001->hdl);
8488c2ecf20Sopenharmony_ci	mutex_destroy(&mt9m001->mutex);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	return 0;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic const struct i2c_device_id mt9m001_id[] = {
8548c2ecf20Sopenharmony_ci	{ "mt9m001", 0 },
8558c2ecf20Sopenharmony_ci	{ }
8568c2ecf20Sopenharmony_ci};
8578c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mt9m001_id);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mt9m001_pm_ops = {
8608c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(mt9m001_power_off, mt9m001_power_on, NULL)
8618c2ecf20Sopenharmony_ci};
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_cistatic const struct of_device_id mt9m001_of_match[] = {
8648c2ecf20Sopenharmony_ci	{ .compatible = "onnn,mt9m001", },
8658c2ecf20Sopenharmony_ci	{ /* sentinel */ },
8668c2ecf20Sopenharmony_ci};
8678c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt9m001_of_match);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_cistatic struct i2c_driver mt9m001_i2c_driver = {
8708c2ecf20Sopenharmony_ci	.driver = {
8718c2ecf20Sopenharmony_ci		.name = "mt9m001",
8728c2ecf20Sopenharmony_ci		.pm = &mt9m001_pm_ops,
8738c2ecf20Sopenharmony_ci		.of_match_table = mt9m001_of_match,
8748c2ecf20Sopenharmony_ci	},
8758c2ecf20Sopenharmony_ci	.probe_new	= mt9m001_probe,
8768c2ecf20Sopenharmony_ci	.remove		= mt9m001_remove,
8778c2ecf20Sopenharmony_ci	.id_table	= mt9m001_id,
8788c2ecf20Sopenharmony_ci};
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_cimodule_i2c_driver(mt9m001_i2c_driver);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Micron MT9M001 Camera driver");
8838c2ecf20Sopenharmony_ciMODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
8848c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
885