18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for Samsung S5K4ECGX 1/4" 5Mp CMOS Image Sensor SoC
48c2ecf20Sopenharmony_ci * with an Embedded Image Signal Processor.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2012, Linaro, Sangwook Lee <sangwook.lee@linaro.org>
78c2ecf20Sopenharmony_ci * Copyright (C) 2012, Insignal Co,. Ltd, Homin Lee <suapapa@insignal.co.kr>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on s5k6aa and noon010pc30 driver
108c2ecf20Sopenharmony_ci * Copyright (C) 2011, Samsung Electronics Co., Ltd.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/crc32.h>
158c2ecf20Sopenharmony_ci#include <linux/ctype.h>
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/firmware.h>
188c2ecf20Sopenharmony_ci#include <linux/gpio.h>
198c2ecf20Sopenharmony_ci#include <linux/i2c.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <media/media-entity.h>
268c2ecf20Sopenharmony_ci#include <media/i2c/s5k4ecgx.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
298c2ecf20Sopenharmony_ci#include <media/v4l2-mediabus.h>
308c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int debug;
338c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define S5K4ECGX_DRIVER_NAME		"s5k4ecgx"
368c2ecf20Sopenharmony_ci#define S5K4ECGX_FIRMWARE		"s5k4ecgx.bin"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Firmware revision information */
398c2ecf20Sopenharmony_ci#define REG_FW_REVISION			0x700001a6
408c2ecf20Sopenharmony_ci#define REG_FW_VERSION			0x700001a4
418c2ecf20Sopenharmony_ci#define S5K4ECGX_REVISION_1_1		0x11
428c2ecf20Sopenharmony_ci#define S5K4ECGX_FW_VERSION		0x4ec0
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* General purpose parameters */
458c2ecf20Sopenharmony_ci#define REG_USER_BRIGHTNESS		0x7000022c
468c2ecf20Sopenharmony_ci#define REG_USER_CONTRAST		0x7000022e
478c2ecf20Sopenharmony_ci#define REG_USER_SATURATION		0x70000230
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define REG_G_ENABLE_PREV		0x7000023e
508c2ecf20Sopenharmony_ci#define REG_G_ENABLE_PREV_CHG		0x70000240
518c2ecf20Sopenharmony_ci#define REG_G_NEW_CFG_SYNC		0x7000024a
528c2ecf20Sopenharmony_ci#define REG_G_PREV_IN_WIDTH		0x70000250
538c2ecf20Sopenharmony_ci#define REG_G_PREV_IN_HEIGHT		0x70000252
548c2ecf20Sopenharmony_ci#define REG_G_PREV_IN_XOFFS		0x70000254
558c2ecf20Sopenharmony_ci#define REG_G_PREV_IN_YOFFS		0x70000256
568c2ecf20Sopenharmony_ci#define REG_G_CAP_IN_WIDTH		0x70000258
578c2ecf20Sopenharmony_ci#define REG_G_CAP_IN_HEIGHT		0x7000025a
588c2ecf20Sopenharmony_ci#define REG_G_CAP_IN_XOFFS		0x7000025c
598c2ecf20Sopenharmony_ci#define REG_G_CAP_IN_YOFFS		0x7000025e
608c2ecf20Sopenharmony_ci#define REG_G_INPUTS_CHANGE_REQ		0x70000262
618c2ecf20Sopenharmony_ci#define REG_G_ACTIVE_PREV_CFG		0x70000266
628c2ecf20Sopenharmony_ci#define REG_G_PREV_CFG_CHG		0x70000268
638c2ecf20Sopenharmony_ci#define REG_G_PREV_OPEN_AFTER_CH	0x7000026a
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* Preview context register sets. n = 0...4. */
668c2ecf20Sopenharmony_ci#define PREG(n, x)			((n) * 0x30 + (x))
678c2ecf20Sopenharmony_ci#define REG_P_OUT_WIDTH(n)		PREG(n, 0x700002a6)
688c2ecf20Sopenharmony_ci#define REG_P_OUT_HEIGHT(n)		PREG(n, 0x700002a8)
698c2ecf20Sopenharmony_ci#define REG_P_FMT(n)			PREG(n, 0x700002aa)
708c2ecf20Sopenharmony_ci#define REG_P_PVI_MASK(n)		PREG(n, 0x700002b4)
718c2ecf20Sopenharmony_ci#define REG_P_FR_TIME_TYPE(n)		PREG(n, 0x700002be)
728c2ecf20Sopenharmony_ci#define  FR_TIME_DYNAMIC		0
738c2ecf20Sopenharmony_ci#define  FR_TIME_FIXED			1
748c2ecf20Sopenharmony_ci#define  FR_TIME_FIXED_ACCURATE		2
758c2ecf20Sopenharmony_ci#define REG_P_FR_TIME_Q_TYPE(n)		PREG(n, 0x700002c0)
768c2ecf20Sopenharmony_ci#define  FR_TIME_Q_DYNAMIC		0
778c2ecf20Sopenharmony_ci#define  FR_TIME_Q_BEST_FRRATE		1
788c2ecf20Sopenharmony_ci#define  FR_TIME_Q_BEST_QUALITY		2
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Frame period in 0.1 ms units */
818c2ecf20Sopenharmony_ci#define REG_P_MAX_FR_TIME(n)		PREG(n, 0x700002c2)
828c2ecf20Sopenharmony_ci#define REG_P_MIN_FR_TIME(n)		PREG(n, 0x700002c4)
838c2ecf20Sopenharmony_ci#define  US_TO_FR_TIME(__t)		((__t) / 100)
848c2ecf20Sopenharmony_ci#define REG_P_PREV_MIRROR(n)		PREG(n, 0x700002d0)
858c2ecf20Sopenharmony_ci#define REG_P_CAP_MIRROR(n)		PREG(n, 0x700002d2)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define REG_G_PREVZOOM_IN_WIDTH		0x70000494
888c2ecf20Sopenharmony_ci#define REG_G_PREVZOOM_IN_HEIGHT	0x70000496
898c2ecf20Sopenharmony_ci#define REG_G_PREVZOOM_IN_XOFFS		0x70000498
908c2ecf20Sopenharmony_ci#define REG_G_PREVZOOM_IN_YOFFS		0x7000049a
918c2ecf20Sopenharmony_ci#define REG_G_CAPZOOM_IN_WIDTH		0x7000049c
928c2ecf20Sopenharmony_ci#define REG_G_CAPZOOM_IN_HEIGHT		0x7000049e
938c2ecf20Sopenharmony_ci#define REG_G_CAPZOOM_IN_XOFFS		0x700004a0
948c2ecf20Sopenharmony_ci#define REG_G_CAPZOOM_IN_YOFFS		0x700004a2
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* n = 0...4 */
978c2ecf20Sopenharmony_ci#define REG_USER_SHARPNESS(n)		(0x70000a28 + (n) * 0xb6)
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* Reduce sharpness range for user space API */
1008c2ecf20Sopenharmony_ci#define SHARPNESS_DIV			8208
1018c2ecf20Sopenharmony_ci#define TOK_TERM			0xffffffff
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * FIXME: This is copied from s5k6aa, because of no information
1058c2ecf20Sopenharmony_ci * in the S5K4ECGX datasheet.
1068c2ecf20Sopenharmony_ci * H/W register Interface (0xd0000000 - 0xd0000fff)
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ci#define AHB_MSB_ADDR_PTR		0xfcfc
1098c2ecf20Sopenharmony_ci#define GEN_REG_OFFSH			0xd000
1108c2ecf20Sopenharmony_ci#define REG_CMDWR_ADDRH			0x0028
1118c2ecf20Sopenharmony_ci#define REG_CMDWR_ADDRL			0x002a
1128c2ecf20Sopenharmony_ci#define REG_CMDRD_ADDRH			0x002c
1138c2ecf20Sopenharmony_ci#define REG_CMDRD_ADDRL			0x002e
1148c2ecf20Sopenharmony_ci#define REG_CMDBUF0_ADDR		0x0f12
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistruct s5k4ecgx_frmsize {
1178c2ecf20Sopenharmony_ci	struct v4l2_frmsize_discrete size;
1188c2ecf20Sopenharmony_ci	/* Fixed sensor matrix crop rectangle */
1198c2ecf20Sopenharmony_ci	struct v4l2_rect input_window;
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct regval_list {
1238c2ecf20Sopenharmony_ci	u32 addr;
1248c2ecf20Sopenharmony_ci	u16 val;
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * TODO: currently only preview is supported and snapshot (capture)
1298c2ecf20Sopenharmony_ci * is not implemented yet
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_cistatic const struct s5k4ecgx_frmsize s5k4ecgx_prev_sizes[] = {
1328c2ecf20Sopenharmony_ci	{
1338c2ecf20Sopenharmony_ci		.size = { 176, 144 },
1348c2ecf20Sopenharmony_ci		.input_window = { 0x00, 0x00, 0x928, 0x780 },
1358c2ecf20Sopenharmony_ci	}, {
1368c2ecf20Sopenharmony_ci		.size = { 352, 288 },
1378c2ecf20Sopenharmony_ci		.input_window = { 0x00, 0x00, 0x928, 0x780 },
1388c2ecf20Sopenharmony_ci	}, {
1398c2ecf20Sopenharmony_ci		.size = { 640, 480 },
1408c2ecf20Sopenharmony_ci		.input_window = { 0x00, 0x00, 0xa00, 0x780 },
1418c2ecf20Sopenharmony_ci	}, {
1428c2ecf20Sopenharmony_ci		.size = { 720, 480 },
1438c2ecf20Sopenharmony_ci		.input_window = { 0x00, 0x00, 0xa00, 0x6a8 },
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define S5K4ECGX_NUM_PREV ARRAY_SIZE(s5k4ecgx_prev_sizes)
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistruct s5k4ecgx_pixfmt {
1508c2ecf20Sopenharmony_ci	u32 code;
1518c2ecf20Sopenharmony_ci	u32 colorspace;
1528c2ecf20Sopenharmony_ci	/* REG_TC_PCFG_Format register value */
1538c2ecf20Sopenharmony_ci	u16 reg_p_format;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/* By default value, output from sensor will be YUV422 0-255 */
1578c2ecf20Sopenharmony_cistatic const struct s5k4ecgx_pixfmt s5k4ecgx_formats[] = {
1588c2ecf20Sopenharmony_ci	{ MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG, 5 },
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic const char * const s5k4ecgx_supply_names[] = {
1628c2ecf20Sopenharmony_ci	/*
1638c2ecf20Sopenharmony_ci	 * Usually 2.8V is used for analog power (vdda)
1648c2ecf20Sopenharmony_ci	 * and digital IO (vddio, vdddcore)
1658c2ecf20Sopenharmony_ci	 */
1668c2ecf20Sopenharmony_ci	"vdda",
1678c2ecf20Sopenharmony_ci	"vddio",
1688c2ecf20Sopenharmony_ci	"vddcore",
1698c2ecf20Sopenharmony_ci	"vddreg", /* The internal s5k4ecgx regulator's supply (1.8V) */
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define S5K4ECGX_NUM_SUPPLIES ARRAY_SIZE(s5k4ecgx_supply_names)
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cienum s5k4ecgx_gpio_id {
1758c2ecf20Sopenharmony_ci	STBY,
1768c2ecf20Sopenharmony_ci	RSET,
1778c2ecf20Sopenharmony_ci	GPIO_NUM,
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistruct s5k4ecgx {
1818c2ecf20Sopenharmony_ci	struct v4l2_subdev sd;
1828c2ecf20Sopenharmony_ci	struct media_pad pad;
1838c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler handler;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	struct s5k4ecgx_platform_data *pdata;
1868c2ecf20Sopenharmony_ci	const struct s5k4ecgx_pixfmt *curr_pixfmt;
1878c2ecf20Sopenharmony_ci	const struct s5k4ecgx_frmsize *curr_frmsize;
1888c2ecf20Sopenharmony_ci	struct mutex lock;
1898c2ecf20Sopenharmony_ci	u8 streaming;
1908c2ecf20Sopenharmony_ci	u8 set_params;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	struct regulator_bulk_data supplies[S5K4ECGX_NUM_SUPPLIES];
1938c2ecf20Sopenharmony_ci	struct s5k4ecgx_gpio gpio[GPIO_NUM];
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic inline struct s5k4ecgx *to_s5k4ecgx(struct v4l2_subdev *sd)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	return container_of(sd, struct s5k4ecgx, sd);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int s5k4ecgx_i2c_read(struct i2c_client *client, u16 addr, u16 *val)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	u8 wbuf[2] = { addr >> 8, addr & 0xff };
2048c2ecf20Sopenharmony_ci	struct i2c_msg msg[2];
2058c2ecf20Sopenharmony_ci	u8 rbuf[2];
2068c2ecf20Sopenharmony_ci	int ret;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	msg[0].addr = client->addr;
2098c2ecf20Sopenharmony_ci	msg[0].flags = 0;
2108c2ecf20Sopenharmony_ci	msg[0].len = 2;
2118c2ecf20Sopenharmony_ci	msg[0].buf = wbuf;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	msg[1].addr = client->addr;
2148c2ecf20Sopenharmony_ci	msg[1].flags = I2C_M_RD;
2158c2ecf20Sopenharmony_ci	msg[1].len = 2;
2168c2ecf20Sopenharmony_ci	msg[1].buf = rbuf;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	ret = i2c_transfer(client->adapter, msg, 2);
2198c2ecf20Sopenharmony_ci	*val = be16_to_cpu(*((__be16 *)rbuf));
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	v4l2_dbg(4, debug, client, "i2c_read: 0x%04X : 0x%04x\n", addr, *val);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return ret == 2 ? 0 : ret;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic int s5k4ecgx_i2c_write(struct i2c_client *client, u16 addr, u16 val)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	u8 buf[4] = { addr >> 8, addr & 0xff, val >> 8, val & 0xff };
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	int ret = i2c_master_send(client, buf, 4);
2318c2ecf20Sopenharmony_ci	v4l2_dbg(4, debug, client, "i2c_write: 0x%04x : 0x%04x\n", addr, val);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return ret == 4 ? 0 : ret;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int s5k4ecgx_write(struct i2c_client *client, u32 addr, u16 val)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	u16 high = addr >> 16, low = addr & 0xffff;
2398c2ecf20Sopenharmony_ci	int ret;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	v4l2_dbg(3, debug, client, "write: 0x%08x : 0x%04x\n", addr, val);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = s5k4ecgx_i2c_write(client, REG_CMDWR_ADDRH, high);
2448c2ecf20Sopenharmony_ci	if (!ret)
2458c2ecf20Sopenharmony_ci		ret = s5k4ecgx_i2c_write(client, REG_CMDWR_ADDRL, low);
2468c2ecf20Sopenharmony_ci	if (!ret)
2478c2ecf20Sopenharmony_ci		ret = s5k4ecgx_i2c_write(client, REG_CMDBUF0_ADDR, val);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return ret;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic int s5k4ecgx_read(struct i2c_client *client, u32 addr, u16 *val)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	u16 high = addr >> 16, low =  addr & 0xffff;
2558c2ecf20Sopenharmony_ci	int ret;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	ret = s5k4ecgx_i2c_write(client, REG_CMDRD_ADDRH, high);
2588c2ecf20Sopenharmony_ci	if (!ret)
2598c2ecf20Sopenharmony_ci		ret = s5k4ecgx_i2c_write(client, REG_CMDRD_ADDRL, low);
2608c2ecf20Sopenharmony_ci	if (!ret)
2618c2ecf20Sopenharmony_ci		ret = s5k4ecgx_i2c_read(client, REG_CMDBUF0_ADDR, val);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return ret;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int s5k4ecgx_read_fw_ver(struct v4l2_subdev *sd)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2698c2ecf20Sopenharmony_ci	u16 hw_rev, fw_ver = 0;
2708c2ecf20Sopenharmony_ci	int ret;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	ret = s5k4ecgx_read(client, REG_FW_VERSION, &fw_ver);
2738c2ecf20Sopenharmony_ci	if (ret < 0 || fw_ver != S5K4ECGX_FW_VERSION) {
2748c2ecf20Sopenharmony_ci		v4l2_err(sd, "FW version check failed!\n");
2758c2ecf20Sopenharmony_ci		return -ENODEV;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	ret = s5k4ecgx_read(client, REG_FW_REVISION, &hw_rev);
2798c2ecf20Sopenharmony_ci	if (ret < 0)
2808c2ecf20Sopenharmony_ci		return ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	v4l2_info(sd, "chip found FW ver: 0x%x, HW rev: 0x%x\n",
2838c2ecf20Sopenharmony_ci						fw_ver, hw_rev);
2848c2ecf20Sopenharmony_ci	return 0;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int s5k4ecgx_set_ahb_address(struct v4l2_subdev *sd)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
2908c2ecf20Sopenharmony_ci	int ret;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* Set APB peripherals start address */
2938c2ecf20Sopenharmony_ci	ret = s5k4ecgx_i2c_write(client, AHB_MSB_ADDR_PTR, GEN_REG_OFFSH);
2948c2ecf20Sopenharmony_ci	if (ret < 0)
2958c2ecf20Sopenharmony_ci		return ret;
2968c2ecf20Sopenharmony_ci	/*
2978c2ecf20Sopenharmony_ci	 * FIXME: This is copied from s5k6aa, because of no information
2988c2ecf20Sopenharmony_ci	 * in s5k4ecgx's datasheet.
2998c2ecf20Sopenharmony_ci	 * sw_reset is activated to put device into idle status
3008c2ecf20Sopenharmony_ci	 */
3018c2ecf20Sopenharmony_ci	ret = s5k4ecgx_i2c_write(client, 0x0010, 0x0001);
3028c2ecf20Sopenharmony_ci	if (ret < 0)
3038c2ecf20Sopenharmony_ci		return ret;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	ret = s5k4ecgx_i2c_write(client, 0x1030, 0x0000);
3068c2ecf20Sopenharmony_ci	if (ret < 0)
3078c2ecf20Sopenharmony_ci		return ret;
3088c2ecf20Sopenharmony_ci	/* Halt ARM CPU */
3098c2ecf20Sopenharmony_ci	return s5k4ecgx_i2c_write(client, 0x0014, 0x0001);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci#define FW_CRC_SIZE	4
3138c2ecf20Sopenharmony_ci/* Register address, value are 4, 2 bytes */
3148c2ecf20Sopenharmony_ci#define FW_RECORD_SIZE	6
3158c2ecf20Sopenharmony_ci/*
3168c2ecf20Sopenharmony_ci * The firmware has following format:
3178c2ecf20Sopenharmony_ci * < total number of records (4 bytes + 2 bytes padding) N >,
3188c2ecf20Sopenharmony_ci * < record 0 >, ..., < record N - 1 >, < CRC32-CCITT (4-bytes) >,
3198c2ecf20Sopenharmony_ci * where "record" is a 4-byte register address followed by 2-byte
3208c2ecf20Sopenharmony_ci * register value (little endian).
3218c2ecf20Sopenharmony_ci * The firmware generator can be found in following git repository:
3228c2ecf20Sopenharmony_ci * git://git.linaro.org/people/sangwook/fimc-v4l2-app.git
3238c2ecf20Sopenharmony_ci */
3248c2ecf20Sopenharmony_cistatic int s5k4ecgx_load_firmware(struct v4l2_subdev *sd)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
3278c2ecf20Sopenharmony_ci	const struct firmware *fw;
3288c2ecf20Sopenharmony_ci	const u8 *ptr;
3298c2ecf20Sopenharmony_ci	int err, i, regs_num;
3308c2ecf20Sopenharmony_ci	u32 addr, crc, crc_file, addr_inc = 0;
3318c2ecf20Sopenharmony_ci	u16 val;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	err = request_firmware(&fw, S5K4ECGX_FIRMWARE, sd->v4l2_dev->dev);
3348c2ecf20Sopenharmony_ci	if (err) {
3358c2ecf20Sopenharmony_ci		v4l2_err(sd, "Failed to read firmware %s\n", S5K4ECGX_FIRMWARE);
3368c2ecf20Sopenharmony_ci		return err;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci	regs_num = get_unaligned_le32(fw->data);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	v4l2_dbg(3, debug, sd, "FW: %s size %zu register sets %d\n",
3418c2ecf20Sopenharmony_ci		 S5K4ECGX_FIRMWARE, fw->size, regs_num);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	regs_num++; /* Add header */
3448c2ecf20Sopenharmony_ci	if (fw->size != regs_num * FW_RECORD_SIZE + FW_CRC_SIZE) {
3458c2ecf20Sopenharmony_ci		err = -EINVAL;
3468c2ecf20Sopenharmony_ci		goto fw_out;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci	crc_file = get_unaligned_le32(fw->data + regs_num * FW_RECORD_SIZE);
3498c2ecf20Sopenharmony_ci	crc = crc32_le(~0, fw->data, regs_num * FW_RECORD_SIZE);
3508c2ecf20Sopenharmony_ci	if (crc != crc_file) {
3518c2ecf20Sopenharmony_ci		v4l2_err(sd, "FW: invalid crc (%#x:%#x)\n", crc, crc_file);
3528c2ecf20Sopenharmony_ci		err = -EINVAL;
3538c2ecf20Sopenharmony_ci		goto fw_out;
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci	ptr = fw->data + FW_RECORD_SIZE;
3568c2ecf20Sopenharmony_ci	for (i = 1; i < regs_num; i++) {
3578c2ecf20Sopenharmony_ci		addr = get_unaligned_le32(ptr);
3588c2ecf20Sopenharmony_ci		ptr += sizeof(u32);
3598c2ecf20Sopenharmony_ci		val = get_unaligned_le16(ptr);
3608c2ecf20Sopenharmony_ci		ptr += sizeof(u16);
3618c2ecf20Sopenharmony_ci		if (addr - addr_inc != 2)
3628c2ecf20Sopenharmony_ci			err = s5k4ecgx_write(client, addr, val);
3638c2ecf20Sopenharmony_ci		else
3648c2ecf20Sopenharmony_ci			err = s5k4ecgx_i2c_write(client, REG_CMDBUF0_ADDR, val);
3658c2ecf20Sopenharmony_ci		if (err)
3668c2ecf20Sopenharmony_ci			break;
3678c2ecf20Sopenharmony_ci		addr_inc = addr;
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_cifw_out:
3708c2ecf20Sopenharmony_ci	release_firmware(fw);
3718c2ecf20Sopenharmony_ci	return err;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/* Set preview and capture input window */
3758c2ecf20Sopenharmony_cistatic int s5k4ecgx_set_input_window(struct i2c_client *c,
3768c2ecf20Sopenharmony_ci				     const struct v4l2_rect *r)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	int ret;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	ret = s5k4ecgx_write(c, REG_G_PREV_IN_WIDTH, r->width);
3818c2ecf20Sopenharmony_ci	if (!ret)
3828c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREV_IN_HEIGHT, r->height);
3838c2ecf20Sopenharmony_ci	if (!ret)
3848c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREV_IN_XOFFS, r->left);
3858c2ecf20Sopenharmony_ci	if (!ret)
3868c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREV_IN_YOFFS, r->top);
3878c2ecf20Sopenharmony_ci	if (!ret)
3888c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAP_IN_WIDTH, r->width);
3898c2ecf20Sopenharmony_ci	if (!ret)
3908c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAP_IN_HEIGHT, r->height);
3918c2ecf20Sopenharmony_ci	if (!ret)
3928c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAP_IN_XOFFS, r->left);
3938c2ecf20Sopenharmony_ci	if (!ret)
3948c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAP_IN_YOFFS, r->top);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	return ret;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/* Set preview and capture zoom input window */
4008c2ecf20Sopenharmony_cistatic int s5k4ecgx_set_zoom_window(struct i2c_client *c,
4018c2ecf20Sopenharmony_ci				    const struct v4l2_rect *r)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	int ret;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_WIDTH, r->width);
4068c2ecf20Sopenharmony_ci	if (!ret)
4078c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_HEIGHT, r->height);
4088c2ecf20Sopenharmony_ci	if (!ret)
4098c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_XOFFS, r->left);
4108c2ecf20Sopenharmony_ci	if (!ret)
4118c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_PREVZOOM_IN_YOFFS, r->top);
4128c2ecf20Sopenharmony_ci	if (!ret)
4138c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_WIDTH, r->width);
4148c2ecf20Sopenharmony_ci	if (!ret)
4158c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_HEIGHT, r->height);
4168c2ecf20Sopenharmony_ci	if (!ret)
4178c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_XOFFS, r->left);
4188c2ecf20Sopenharmony_ci	if (!ret)
4198c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(c, REG_G_CAPZOOM_IN_YOFFS, r->top);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	return ret;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic int s5k4ecgx_set_output_framefmt(struct s5k4ecgx *priv)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
4278c2ecf20Sopenharmony_ci	int ret;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	ret = s5k4ecgx_write(client, REG_P_OUT_WIDTH(0),
4308c2ecf20Sopenharmony_ci			     priv->curr_frmsize->size.width);
4318c2ecf20Sopenharmony_ci	if (!ret)
4328c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_OUT_HEIGHT(0),
4338c2ecf20Sopenharmony_ci				     priv->curr_frmsize->size.height);
4348c2ecf20Sopenharmony_ci	if (!ret)
4358c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_FMT(0),
4368c2ecf20Sopenharmony_ci				     priv->curr_pixfmt->reg_p_format);
4378c2ecf20Sopenharmony_ci	return ret;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic int s5k4ecgx_init_sensor(struct v4l2_subdev *sd)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	int ret;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	ret = s5k4ecgx_set_ahb_address(sd);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* The delay is from manufacturer's settings */
4478c2ecf20Sopenharmony_ci	msleep(100);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (!ret)
4508c2ecf20Sopenharmony_ci		ret = s5k4ecgx_load_firmware(sd);
4518c2ecf20Sopenharmony_ci	if (ret)
4528c2ecf20Sopenharmony_ci		v4l2_err(sd, "Failed to write initial settings\n");
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	return ret;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic int s5k4ecgx_gpio_set_value(struct s5k4ecgx *priv, int id, u32 val)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	if (!gpio_is_valid(priv->gpio[id].gpio))
4608c2ecf20Sopenharmony_ci		return 0;
4618c2ecf20Sopenharmony_ci	gpio_set_value(priv->gpio[id].gpio, val);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	return 1;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic int __s5k4ecgx_power_on(struct s5k4ecgx *priv)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	int ret;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
4718c2ecf20Sopenharmony_ci	if (ret)
4728c2ecf20Sopenharmony_ci		return ret;
4738c2ecf20Sopenharmony_ci	usleep_range(30, 50);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/* The polarity of STBY is controlled by TSP */
4768c2ecf20Sopenharmony_ci	if (s5k4ecgx_gpio_set_value(priv, STBY, priv->gpio[STBY].level))
4778c2ecf20Sopenharmony_ci		usleep_range(30, 50);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	if (s5k4ecgx_gpio_set_value(priv, RSET, priv->gpio[RSET].level))
4808c2ecf20Sopenharmony_ci		usleep_range(30, 50);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return 0;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int __s5k4ecgx_power_off(struct s5k4ecgx *priv)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	if (s5k4ecgx_gpio_set_value(priv, RSET, !priv->gpio[RSET].level))
4888c2ecf20Sopenharmony_ci		usleep_range(30, 50);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (s5k4ecgx_gpio_set_value(priv, STBY, !priv->gpio[STBY].level))
4918c2ecf20Sopenharmony_ci		usleep_range(30, 50);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	priv->streaming = 0;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	return regulator_bulk_disable(S5K4ECGX_NUM_SUPPLIES, priv->supplies);
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci/* Find nearest matching image pixel size. */
4998c2ecf20Sopenharmony_cistatic int s5k4ecgx_try_frame_size(struct v4l2_mbus_framefmt *mf,
5008c2ecf20Sopenharmony_ci				  const struct s5k4ecgx_frmsize **size)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	unsigned int min_err = ~0;
5038c2ecf20Sopenharmony_ci	int i = ARRAY_SIZE(s5k4ecgx_prev_sizes);
5048c2ecf20Sopenharmony_ci	const struct s5k4ecgx_frmsize *fsize = &s5k4ecgx_prev_sizes[0],
5058c2ecf20Sopenharmony_ci		*match = NULL;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	while (i--) {
5088c2ecf20Sopenharmony_ci		int err = abs(fsize->size.width - mf->width)
5098c2ecf20Sopenharmony_ci				+ abs(fsize->size.height - mf->height);
5108c2ecf20Sopenharmony_ci		if (err < min_err) {
5118c2ecf20Sopenharmony_ci			min_err = err;
5128c2ecf20Sopenharmony_ci			match = fsize;
5138c2ecf20Sopenharmony_ci		}
5148c2ecf20Sopenharmony_ci		fsize++;
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci	if (match) {
5178c2ecf20Sopenharmony_ci		mf->width  = match->size.width;
5188c2ecf20Sopenharmony_ci		mf->height = match->size.height;
5198c2ecf20Sopenharmony_ci		if (size)
5208c2ecf20Sopenharmony_ci			*size = match;
5218c2ecf20Sopenharmony_ci		return 0;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	return -EINVAL;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic int s5k4ecgx_enum_mbus_code(struct v4l2_subdev *sd,
5288c2ecf20Sopenharmony_ci				   struct v4l2_subdev_pad_config *cfg,
5298c2ecf20Sopenharmony_ci				   struct v4l2_subdev_mbus_code_enum *code)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	if (code->index >= ARRAY_SIZE(s5k4ecgx_formats))
5328c2ecf20Sopenharmony_ci		return -EINVAL;
5338c2ecf20Sopenharmony_ci	code->code = s5k4ecgx_formats[code->index].code;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return 0;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int s5k4ecgx_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
5398c2ecf20Sopenharmony_ci			   struct v4l2_subdev_format *fmt)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
5428c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
5458c2ecf20Sopenharmony_ci		if (cfg) {
5468c2ecf20Sopenharmony_ci			mf = v4l2_subdev_get_try_format(sd, cfg, 0);
5478c2ecf20Sopenharmony_ci			fmt->format = *mf;
5488c2ecf20Sopenharmony_ci		}
5498c2ecf20Sopenharmony_ci		return 0;
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	mf = &fmt->format;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	mutex_lock(&priv->lock);
5558c2ecf20Sopenharmony_ci	mf->width = priv->curr_frmsize->size.width;
5568c2ecf20Sopenharmony_ci	mf->height = priv->curr_frmsize->size.height;
5578c2ecf20Sopenharmony_ci	mf->code = priv->curr_pixfmt->code;
5588c2ecf20Sopenharmony_ci	mf->colorspace = priv->curr_pixfmt->colorspace;
5598c2ecf20Sopenharmony_ci	mf->field = V4L2_FIELD_NONE;
5608c2ecf20Sopenharmony_ci	mutex_unlock(&priv->lock);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	return 0;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic const struct s5k4ecgx_pixfmt *s5k4ecgx_try_fmt(struct v4l2_subdev *sd,
5668c2ecf20Sopenharmony_ci					    struct v4l2_mbus_framefmt *mf)
5678c2ecf20Sopenharmony_ci{
5688c2ecf20Sopenharmony_ci	int i = ARRAY_SIZE(s5k4ecgx_formats);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	while (--i)
5718c2ecf20Sopenharmony_ci		if (mf->code == s5k4ecgx_formats[i].code)
5728c2ecf20Sopenharmony_ci			break;
5738c2ecf20Sopenharmony_ci	mf->code = s5k4ecgx_formats[i].code;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	return &s5k4ecgx_formats[i];
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic int s5k4ecgx_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg,
5798c2ecf20Sopenharmony_ci			    struct v4l2_subdev_format *fmt)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
5828c2ecf20Sopenharmony_ci	const struct s5k4ecgx_frmsize *fsize = NULL;
5838c2ecf20Sopenharmony_ci	const struct s5k4ecgx_pixfmt *pf;
5848c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf;
5858c2ecf20Sopenharmony_ci	int ret = 0;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	pf = s5k4ecgx_try_fmt(sd, &fmt->format);
5888c2ecf20Sopenharmony_ci	s5k4ecgx_try_frame_size(&fmt->format, &fsize);
5898c2ecf20Sopenharmony_ci	fmt->format.colorspace = V4L2_COLORSPACE_JPEG;
5908c2ecf20Sopenharmony_ci	fmt->format.field = V4L2_FIELD_NONE;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
5938c2ecf20Sopenharmony_ci		if (cfg) {
5948c2ecf20Sopenharmony_ci			mf = v4l2_subdev_get_try_format(sd, cfg, 0);
5958c2ecf20Sopenharmony_ci			*mf = fmt->format;
5968c2ecf20Sopenharmony_ci		}
5978c2ecf20Sopenharmony_ci		return 0;
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	mutex_lock(&priv->lock);
6018c2ecf20Sopenharmony_ci	if (!priv->streaming) {
6028c2ecf20Sopenharmony_ci		priv->curr_frmsize = fsize;
6038c2ecf20Sopenharmony_ci		priv->curr_pixfmt = pf;
6048c2ecf20Sopenharmony_ci		priv->set_params = 1;
6058c2ecf20Sopenharmony_ci	} else {
6068c2ecf20Sopenharmony_ci		ret = -EBUSY;
6078c2ecf20Sopenharmony_ci	}
6088c2ecf20Sopenharmony_ci	mutex_unlock(&priv->lock);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return ret;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops s5k4ecgx_pad_ops = {
6148c2ecf20Sopenharmony_ci	.enum_mbus_code	= s5k4ecgx_enum_mbus_code,
6158c2ecf20Sopenharmony_ci	.get_fmt	= s5k4ecgx_get_fmt,
6168c2ecf20Sopenharmony_ci	.set_fmt	= s5k4ecgx_set_fmt,
6178c2ecf20Sopenharmony_ci};
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci/*
6208c2ecf20Sopenharmony_ci * V4L2 subdev controls
6218c2ecf20Sopenharmony_ci */
6228c2ecf20Sopenharmony_cistatic int s5k4ecgx_s_ctrl(struct v4l2_ctrl *ctrl)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = &container_of(ctrl->handler, struct s5k4ecgx,
6258c2ecf20Sopenharmony_ci						handler)->sd;
6268c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(sd);
6278c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
6288c2ecf20Sopenharmony_ci	unsigned int i;
6298c2ecf20Sopenharmony_ci	int err = 0;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "ctrl: 0x%x, value: %d\n", ctrl->id, ctrl->val);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	mutex_lock(&priv->lock);
6348c2ecf20Sopenharmony_ci	switch (ctrl->id) {
6358c2ecf20Sopenharmony_ci	case V4L2_CID_CONTRAST:
6368c2ecf20Sopenharmony_ci		err = s5k4ecgx_write(client, REG_USER_CONTRAST, ctrl->val);
6378c2ecf20Sopenharmony_ci		break;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	case V4L2_CID_SATURATION:
6408c2ecf20Sopenharmony_ci		err = s5k4ecgx_write(client, REG_USER_SATURATION, ctrl->val);
6418c2ecf20Sopenharmony_ci		break;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	case V4L2_CID_SHARPNESS:
6448c2ecf20Sopenharmony_ci		/* TODO: Revisit, is this setting for all presets ? */
6458c2ecf20Sopenharmony_ci		for (i = 0; i < 4 && !err; i++)
6468c2ecf20Sopenharmony_ci			err = s5k4ecgx_write(client, REG_USER_SHARPNESS(i),
6478c2ecf20Sopenharmony_ci					     ctrl->val * SHARPNESS_DIV);
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	case V4L2_CID_BRIGHTNESS:
6518c2ecf20Sopenharmony_ci		err = s5k4ecgx_write(client, REG_USER_BRIGHTNESS, ctrl->val);
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci	mutex_unlock(&priv->lock);
6558c2ecf20Sopenharmony_ci	if (err < 0)
6568c2ecf20Sopenharmony_ci		v4l2_err(sd, "Failed to write s_ctrl err %d\n", err);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return err;
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops s5k4ecgx_ctrl_ops = {
6628c2ecf20Sopenharmony_ci	.s_ctrl = s5k4ecgx_s_ctrl,
6638c2ecf20Sopenharmony_ci};
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/*
6668c2ecf20Sopenharmony_ci * Reading s5k4ecgx version information
6678c2ecf20Sopenharmony_ci */
6688c2ecf20Sopenharmony_cistatic int s5k4ecgx_registered(struct v4l2_subdev *sd)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	int ret;
6718c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	mutex_lock(&priv->lock);
6748c2ecf20Sopenharmony_ci	ret = __s5k4ecgx_power_on(priv);
6758c2ecf20Sopenharmony_ci	if (!ret) {
6768c2ecf20Sopenharmony_ci		ret = s5k4ecgx_read_fw_ver(sd);
6778c2ecf20Sopenharmony_ci		__s5k4ecgx_power_off(priv);
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci	mutex_unlock(&priv->lock);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	return ret;
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci/*
6858c2ecf20Sopenharmony_ci * V4L2 subdev internal operations
6868c2ecf20Sopenharmony_ci */
6878c2ecf20Sopenharmony_cistatic int s5k4ecgx_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *mf = v4l2_subdev_get_try_format(sd, fh->pad, 0);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	mf->width = s5k4ecgx_prev_sizes[0].size.width;
6928c2ecf20Sopenharmony_ci	mf->height = s5k4ecgx_prev_sizes[0].size.height;
6938c2ecf20Sopenharmony_ci	mf->code = s5k4ecgx_formats[0].code;
6948c2ecf20Sopenharmony_ci	mf->colorspace = V4L2_COLORSPACE_JPEG;
6958c2ecf20Sopenharmony_ci	mf->field = V4L2_FIELD_NONE;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	return 0;
6988c2ecf20Sopenharmony_ci}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops s5k4ecgx_subdev_internal_ops = {
7018c2ecf20Sopenharmony_ci	.registered = s5k4ecgx_registered,
7028c2ecf20Sopenharmony_ci	.open = s5k4ecgx_open,
7038c2ecf20Sopenharmony_ci};
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic int s5k4ecgx_s_power(struct v4l2_subdev *sd, int on)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
7088c2ecf20Sopenharmony_ci	int ret;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "Switching %s\n", on ? "on" : "off");
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	if (on) {
7138c2ecf20Sopenharmony_ci		ret = __s5k4ecgx_power_on(priv);
7148c2ecf20Sopenharmony_ci		if (ret < 0)
7158c2ecf20Sopenharmony_ci			return ret;
7168c2ecf20Sopenharmony_ci		/* Time to stabilize sensor */
7178c2ecf20Sopenharmony_ci		msleep(100);
7188c2ecf20Sopenharmony_ci		ret = s5k4ecgx_init_sensor(sd);
7198c2ecf20Sopenharmony_ci		if (ret < 0)
7208c2ecf20Sopenharmony_ci			__s5k4ecgx_power_off(priv);
7218c2ecf20Sopenharmony_ci		else
7228c2ecf20Sopenharmony_ci			priv->set_params = 1;
7238c2ecf20Sopenharmony_ci	} else {
7248c2ecf20Sopenharmony_ci		ret = __s5k4ecgx_power_off(priv);
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	return ret;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic int s5k4ecgx_log_status(struct v4l2_subdev *sd)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_log_status(sd->ctrl_handler, sd->name);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	return 0;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops s5k4ecgx_core_ops = {
7388c2ecf20Sopenharmony_ci	.s_power	= s5k4ecgx_s_power,
7398c2ecf20Sopenharmony_ci	.log_status	= s5k4ecgx_log_status,
7408c2ecf20Sopenharmony_ci};
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic int __s5k4ecgx_s_params(struct s5k4ecgx *priv)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
7458c2ecf20Sopenharmony_ci	const struct v4l2_rect *crop_rect = &priv->curr_frmsize->input_window;
7468c2ecf20Sopenharmony_ci	int ret;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	ret = s5k4ecgx_set_input_window(client, crop_rect);
7498c2ecf20Sopenharmony_ci	if (!ret)
7508c2ecf20Sopenharmony_ci		ret = s5k4ecgx_set_zoom_window(client, crop_rect);
7518c2ecf20Sopenharmony_ci	if (!ret)
7528c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_G_INPUTS_CHANGE_REQ, 1);
7538c2ecf20Sopenharmony_ci	if (!ret)
7548c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, 0x70000a1e, 0x28);
7558c2ecf20Sopenharmony_ci	if (!ret)
7568c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, 0x70000ad4, 0x3c);
7578c2ecf20Sopenharmony_ci	if (!ret)
7588c2ecf20Sopenharmony_ci		ret = s5k4ecgx_set_output_framefmt(priv);
7598c2ecf20Sopenharmony_ci	if (!ret)
7608c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_PVI_MASK(0), 0x52);
7618c2ecf20Sopenharmony_ci	if (!ret)
7628c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_FR_TIME_TYPE(0),
7638c2ecf20Sopenharmony_ci				     FR_TIME_DYNAMIC);
7648c2ecf20Sopenharmony_ci	if (!ret)
7658c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_FR_TIME_Q_TYPE(0),
7668c2ecf20Sopenharmony_ci				     FR_TIME_Q_BEST_FRRATE);
7678c2ecf20Sopenharmony_ci	if (!ret)
7688c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client,  REG_P_MIN_FR_TIME(0),
7698c2ecf20Sopenharmony_ci				     US_TO_FR_TIME(33300));
7708c2ecf20Sopenharmony_ci	if (!ret)
7718c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_MAX_FR_TIME(0),
7728c2ecf20Sopenharmony_ci				     US_TO_FR_TIME(66600));
7738c2ecf20Sopenharmony_ci	if (!ret)
7748c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_PREV_MIRROR(0), 0);
7758c2ecf20Sopenharmony_ci	if (!ret)
7768c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_P_CAP_MIRROR(0), 0);
7778c2ecf20Sopenharmony_ci	if (!ret)
7788c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_G_ACTIVE_PREV_CFG, 0);
7798c2ecf20Sopenharmony_ci	if (!ret)
7808c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_G_PREV_OPEN_AFTER_CH, 1);
7818c2ecf20Sopenharmony_ci	if (!ret)
7828c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_G_NEW_CFG_SYNC, 1);
7838c2ecf20Sopenharmony_ci	if (!ret)
7848c2ecf20Sopenharmony_ci		ret = s5k4ecgx_write(client, REG_G_PREV_CFG_CHG, 1);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	return ret;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_cistatic int __s5k4ecgx_s_stream(struct s5k4ecgx *priv, int on)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	struct i2c_client *client = v4l2_get_subdevdata(&priv->sd);
7928c2ecf20Sopenharmony_ci	int ret;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (on && priv->set_params) {
7958c2ecf20Sopenharmony_ci		ret = __s5k4ecgx_s_params(priv);
7968c2ecf20Sopenharmony_ci		if (ret < 0)
7978c2ecf20Sopenharmony_ci			return ret;
7988c2ecf20Sopenharmony_ci		priv->set_params = 0;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci	/*
8018c2ecf20Sopenharmony_ci	 * This enables/disables preview stream only. Capture requests
8028c2ecf20Sopenharmony_ci	 * are not supported yet.
8038c2ecf20Sopenharmony_ci	 */
8048c2ecf20Sopenharmony_ci	ret = s5k4ecgx_write(client, REG_G_ENABLE_PREV, on);
8058c2ecf20Sopenharmony_ci	if (ret < 0)
8068c2ecf20Sopenharmony_ci		return ret;
8078c2ecf20Sopenharmony_ci	return s5k4ecgx_write(client, REG_G_ENABLE_PREV_CHG, 1);
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic int s5k4ecgx_s_stream(struct v4l2_subdev *sd, int on)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
8138c2ecf20Sopenharmony_ci	int ret = 0;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	v4l2_dbg(1, debug, sd, "Turn streaming %s\n", on ? "on" : "off");
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	mutex_lock(&priv->lock);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	if (priv->streaming == !on) {
8208c2ecf20Sopenharmony_ci		ret = __s5k4ecgx_s_stream(priv, on);
8218c2ecf20Sopenharmony_ci		if (!ret)
8228c2ecf20Sopenharmony_ci			priv->streaming = on & 1;
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	mutex_unlock(&priv->lock);
8268c2ecf20Sopenharmony_ci	return ret;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops s5k4ecgx_video_ops = {
8308c2ecf20Sopenharmony_ci	.s_stream = s5k4ecgx_s_stream,
8318c2ecf20Sopenharmony_ci};
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops s5k4ecgx_ops = {
8348c2ecf20Sopenharmony_ci	.core = &s5k4ecgx_core_ops,
8358c2ecf20Sopenharmony_ci	.pad = &s5k4ecgx_pad_ops,
8368c2ecf20Sopenharmony_ci	.video = &s5k4ecgx_video_ops,
8378c2ecf20Sopenharmony_ci};
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci/*
8408c2ecf20Sopenharmony_ci * GPIO setup
8418c2ecf20Sopenharmony_ci */
8428c2ecf20Sopenharmony_cistatic int s5k4ecgx_config_gpio(int nr, int val, const char *name)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	unsigned long flags = val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
8458c2ecf20Sopenharmony_ci	int ret;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	if (!gpio_is_valid(nr))
8488c2ecf20Sopenharmony_ci		return 0;
8498c2ecf20Sopenharmony_ci	ret = gpio_request_one(nr, flags, name);
8508c2ecf20Sopenharmony_ci	if (!ret)
8518c2ecf20Sopenharmony_ci		gpio_export(nr, 0);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return ret;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic void s5k4ecgx_free_gpios(struct s5k4ecgx *priv)
8578c2ecf20Sopenharmony_ci{
8588c2ecf20Sopenharmony_ci	int i;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(priv->gpio); i++) {
8618c2ecf20Sopenharmony_ci		if (!gpio_is_valid(priv->gpio[i].gpio))
8628c2ecf20Sopenharmony_ci			continue;
8638c2ecf20Sopenharmony_ci		gpio_free(priv->gpio[i].gpio);
8648c2ecf20Sopenharmony_ci		priv->gpio[i].gpio = -EINVAL;
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic int s5k4ecgx_config_gpios(struct s5k4ecgx *priv,
8698c2ecf20Sopenharmony_ci				  const struct s5k4ecgx_platform_data *pdata)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	const struct s5k4ecgx_gpio *gpio = &pdata->gpio_stby;
8728c2ecf20Sopenharmony_ci	int ret;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	priv->gpio[STBY].gpio = -EINVAL;
8758c2ecf20Sopenharmony_ci	priv->gpio[RSET].gpio  = -EINVAL;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_STBY");
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	if (ret) {
8808c2ecf20Sopenharmony_ci		s5k4ecgx_free_gpios(priv);
8818c2ecf20Sopenharmony_ci		return ret;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci	priv->gpio[STBY] = *gpio;
8848c2ecf20Sopenharmony_ci	if (gpio_is_valid(gpio->gpio))
8858c2ecf20Sopenharmony_ci		gpio_set_value(gpio->gpio, 0);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	gpio = &pdata->gpio_reset;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	ret = s5k4ecgx_config_gpio(gpio->gpio, gpio->level, "S5K4ECGX_RST");
8908c2ecf20Sopenharmony_ci	if (ret) {
8918c2ecf20Sopenharmony_ci		s5k4ecgx_free_gpios(priv);
8928c2ecf20Sopenharmony_ci		return ret;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci	priv->gpio[RSET] = *gpio;
8958c2ecf20Sopenharmony_ci	if (gpio_is_valid(gpio->gpio))
8968c2ecf20Sopenharmony_ci		gpio_set_value(gpio->gpio, 0);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	return 0;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic int s5k4ecgx_init_v4l2_ctrls(struct s5k4ecgx *priv)
9028c2ecf20Sopenharmony_ci{
9038c2ecf20Sopenharmony_ci	const struct v4l2_ctrl_ops *ops = &s5k4ecgx_ctrl_ops;
9048c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler *hdl = &priv->handler;
9058c2ecf20Sopenharmony_ci	int ret;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	ret = v4l2_ctrl_handler_init(hdl, 4);
9088c2ecf20Sopenharmony_ci	if (ret)
9098c2ecf20Sopenharmony_ci		return ret;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BRIGHTNESS, -208, 127, 1, 0);
9128c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, -127, 127, 1, 0);
9138c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, -127, 127, 1, 0);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	/* Sharpness default is 24612, and then (24612/SHARPNESS_DIV) = 2 */
9168c2ecf20Sopenharmony_ci	v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SHARPNESS, -32704/SHARPNESS_DIV,
9178c2ecf20Sopenharmony_ci			  24612/SHARPNESS_DIV, 1, 2);
9188c2ecf20Sopenharmony_ci	if (hdl->error) {
9198c2ecf20Sopenharmony_ci		ret = hdl->error;
9208c2ecf20Sopenharmony_ci		v4l2_ctrl_handler_free(hdl);
9218c2ecf20Sopenharmony_ci		return ret;
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci	priv->sd.ctrl_handler = hdl;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	return 0;
9268c2ecf20Sopenharmony_ci};
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_cistatic int s5k4ecgx_probe(struct i2c_client *client,
9298c2ecf20Sopenharmony_ci			  const struct i2c_device_id *id)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	struct s5k4ecgx_platform_data *pdata = client->dev.platform_data;
9328c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
9338c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv;
9348c2ecf20Sopenharmony_ci	int ret, i;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (pdata == NULL) {
9378c2ecf20Sopenharmony_ci		dev_err(&client->dev, "platform data is missing!\n");
9388c2ecf20Sopenharmony_ci		return -EINVAL;
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	priv = devm_kzalloc(&client->dev, sizeof(struct s5k4ecgx), GFP_KERNEL);
9428c2ecf20Sopenharmony_ci	if (!priv)
9438c2ecf20Sopenharmony_ci		return -ENOMEM;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	mutex_init(&priv->lock);
9468c2ecf20Sopenharmony_ci	priv->streaming = 0;
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	sd = &priv->sd;
9498c2ecf20Sopenharmony_ci	/* Registering subdev */
9508c2ecf20Sopenharmony_ci	v4l2_i2c_subdev_init(sd, client, &s5k4ecgx_ops);
9518c2ecf20Sopenharmony_ci	/* Static name; NEVER use in new drivers! */
9528c2ecf20Sopenharmony_ci	strscpy(sd->name, S5K4ECGX_DRIVER_NAME, sizeof(sd->name));
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	sd->internal_ops = &s5k4ecgx_subdev_internal_ops;
9558c2ecf20Sopenharmony_ci	/* Support v4l2 sub-device user space API */
9568c2ecf20Sopenharmony_ci	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	priv->pad.flags = MEDIA_PAD_FL_SOURCE;
9598c2ecf20Sopenharmony_ci	sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
9608c2ecf20Sopenharmony_ci	ret = media_entity_pads_init(&sd->entity, 1, &priv->pad);
9618c2ecf20Sopenharmony_ci	if (ret)
9628c2ecf20Sopenharmony_ci		return ret;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	ret = s5k4ecgx_config_gpios(priv, pdata);
9658c2ecf20Sopenharmony_ci	if (ret) {
9668c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to set gpios\n");
9678c2ecf20Sopenharmony_ci		goto out_err1;
9688c2ecf20Sopenharmony_ci	}
9698c2ecf20Sopenharmony_ci	for (i = 0; i < S5K4ECGX_NUM_SUPPLIES; i++)
9708c2ecf20Sopenharmony_ci		priv->supplies[i].supply = s5k4ecgx_supply_names[i];
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(&client->dev, S5K4ECGX_NUM_SUPPLIES,
9738c2ecf20Sopenharmony_ci				 priv->supplies);
9748c2ecf20Sopenharmony_ci	if (ret) {
9758c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to get regulators\n");
9768c2ecf20Sopenharmony_ci		goto out_err2;
9778c2ecf20Sopenharmony_ci	}
9788c2ecf20Sopenharmony_ci	ret = s5k4ecgx_init_v4l2_ctrls(priv);
9798c2ecf20Sopenharmony_ci	if (ret)
9808c2ecf20Sopenharmony_ci		goto out_err2;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	priv->curr_pixfmt = &s5k4ecgx_formats[0];
9838c2ecf20Sopenharmony_ci	priv->curr_frmsize = &s5k4ecgx_prev_sizes[0];
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return 0;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ciout_err2:
9888c2ecf20Sopenharmony_ci	s5k4ecgx_free_gpios(priv);
9898c2ecf20Sopenharmony_ciout_err1:
9908c2ecf20Sopenharmony_ci	media_entity_cleanup(&priv->sd.entity);
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	return ret;
9938c2ecf20Sopenharmony_ci}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_cistatic int s5k4ecgx_remove(struct i2c_client *client)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd = i2c_get_clientdata(client);
9988c2ecf20Sopenharmony_ci	struct s5k4ecgx *priv = to_s5k4ecgx(sd);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	mutex_destroy(&priv->lock);
10018c2ecf20Sopenharmony_ci	s5k4ecgx_free_gpios(priv);
10028c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(sd);
10038c2ecf20Sopenharmony_ci	v4l2_ctrl_handler_free(&priv->handler);
10048c2ecf20Sopenharmony_ci	media_entity_cleanup(&sd->entity);
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	return 0;
10078c2ecf20Sopenharmony_ci}
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_cistatic const struct i2c_device_id s5k4ecgx_id[] = {
10108c2ecf20Sopenharmony_ci	{ S5K4ECGX_DRIVER_NAME, 0 },
10118c2ecf20Sopenharmony_ci	{}
10128c2ecf20Sopenharmony_ci};
10138c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, s5k4ecgx_id);
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_cistatic struct i2c_driver v4l2_i2c_driver = {
10168c2ecf20Sopenharmony_ci	.driver = {
10178c2ecf20Sopenharmony_ci		.name = S5K4ECGX_DRIVER_NAME,
10188c2ecf20Sopenharmony_ci	},
10198c2ecf20Sopenharmony_ci	.probe = s5k4ecgx_probe,
10208c2ecf20Sopenharmony_ci	.remove = s5k4ecgx_remove,
10218c2ecf20Sopenharmony_ci	.id_table = s5k4ecgx_id,
10228c2ecf20Sopenharmony_ci};
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_cimodule_i2c_driver(v4l2_i2c_driver);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Samsung S5K4ECGX 5MP SOC camera");
10278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sangwook Lee <sangwook.lee@linaro.org>");
10288c2ecf20Sopenharmony_ciMODULE_AUTHOR("Seok-Young Jang <quartz.jang@samsung.com>");
10298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
10308c2ecf20Sopenharmony_ciMODULE_FIRMWARE(S5K4ECGX_FIRMWARE);
1031