aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/net/udpgro_fwd.sh
blob: c079565add39224eb99e011f941b6f0a11c1648c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

BPF_FILE="../bpf/xdp_dummy.bpf.o"
readonly BASE="ns-$(mktemp -u XXXXXX)"
readonly SRC=2
readonly DST=1
readonly DST_NAT=100
readonly NS_SRC=$BASE$SRC
readonly NS_DST=$BASE$DST

# "baremetal" network used for raw UDP traffic
readonly BM_NET_V4=192.168.1.
readonly BM_NET_V6=2001:db8::

# "overlay" network used for UDP over UDP tunnel traffic
readonly OL_NET_V4=172.16.1.
readonly OL_NET_V6=2001:db8:1::
readonly NPROCS=`nproc`

cleanup() {
	local ns
	local -r jobs="$(jobs -p)"
	[ -n "${jobs}" ] && kill -1 ${jobs} 2>/dev/null

	for ns in $NS_SRC $NS_DST; do
		ip netns del $ns 2>/dev/null
	done
}

trap cleanup EXIT

create_ns() {
	local net
	local ns

	for ns in $NS_SRC $NS_DST; do
		ip netns add $ns
		ip -n $ns link set dev lo up
	done

	ip link add name veth$SRC type veth peer name veth$DST

	for ns in $SRC $DST; do
		ip link set dev veth$ns netns $BASE$ns
		ip -n $BASE$ns link set dev veth$ns up
		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V4$ns/24
		ip -n $BASE$ns addr add dev veth$ns $BM_NET_V6$ns/64 nodad
	done
	ip -n $NS_DST link set veth$DST xdp object ${BPF_FILE} section xdp 2>/dev/null
}

create_vxlan_endpoint() {
	local -r netns=$1
	local -r bm_dev=$2
	local -r bm_rem_addr=$3
	local -r vxlan_dev=$4
	local -r vxlan_id=$5
	local -r vxlan_port=4789

	ip -n $netns link set dev $bm_dev up
	ip -n $netns link add dev $vxlan_dev type vxlan id $vxlan_id \
				dstport $vxlan_port remote $bm_rem_addr
	ip -n $netns link set dev $vxlan_dev up
}

create_vxlan_pair() {
	local ns

	create_ns

	for ns in $SRC $DST; do
		# note that 3 - $SRC == $DST and 3 - $DST == $SRC
		create_vxlan_endpoint $BASE$ns veth$ns $BM_NET_V4$((3 - $ns)) vxlan$ns 4
		ip -n $BASE$ns addr add dev vxlan$ns $OL_NET_V4$ns/24
	done
	for ns in $SRC $DST; do
		create_vxlan_endpoint $BASE$ns veth$ns $BM_NET_V6$((3 - $ns)) vxlan6$ns 6
		ip -n $BASE$ns addr add dev vxlan6$ns $OL_NET_V6$ns/24 nodad
	done
}

is_ipv6() {
	if [[ $1 =~ .*:.* ]]; then
		return 0
	fi
	return 1
}

run_test() {
	local -r msg=$1
	local -r dst=$2
	local -r pkts=$3
	local -r vxpkts=$4
	local bind=$5
	local rx_args=""
	local rx_family="-4"
	local family=-4
	local filter=IpInReceives
	local ipt=iptables

	printf "%-40s" "$msg"

	if is_ipv6 $dst; then
		# rx program does not support '-6' and implies ipv6 usage by default
		rx_family=""
		family=-6
		filter=Ip6InReceives
		ipt=ip6tables
	fi

	rx_args="$rx_family"
	[ -n "$bind" ] && rx_args="$rx_args -b $bind"

	# send a single GSO packet, segmented in 10 UDP frames.
	# Always expect 10 UDP frames on RX side as rx socket does
	# not enable GRO
	ip netns exec $NS_DST $ipt -A INPUT -p udp --dport 4789
	ip netns exec $NS_DST $ipt -A INPUT -p udp --dport 8000
	ip netns exec $NS_DST ./udpgso_bench_rx -C 1000 -R 10 -n 10 -l 1300 $rx_args &
	local spid=$!
	sleep 0.1
	ip netns exec $NS_SRC ./udpgso_bench_tx $family -M 1 -s 13000 -S 1300 -D $dst
	local retc=$?
	wait $spid
	local rets=$?
	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
		echo " fail client exit code $retc, server $rets"
		ret=1
		return
	fi

	local rcv=`ip netns exec $NS_DST $ipt"-save" -c | grep 'dport 8000' | \
							  sed -e 's/\[//' -e 's/:.*//'`
	if [ $rcv != $pkts ]; then
		echo " fail - received $rcv packets, expected $pkts"
		ret=1
		return
	fi

	local vxrcv=`ip netns exec $NS_DST $ipt"-save" -c | grep 'dport 4789' | \
							    sed -e 's/\[//' -e 's/:.*//'`

	# upper net can generate a little noise, allow some tolerance
	if [ $vxrcv -lt $vxpkts -o $vxrcv -gt $((vxpkts + 3)) ]; then
		echo " fail - received $vxrcv vxlan packets, expected $vxpkts"
		ret=1
		return
	fi
	echo " ok"
}

run_bench() {
	local -r msg=$1
	local -r dst=$2
	local family=-4

	printf "%-40s" "$msg"
	if [ $NPROCS -lt 2 ]; then
		echo " skip - needed 2 CPUs found $NPROCS"
		return
	fi

	is_ipv6 $dst && family=-6

	# bind the sender and the receiver to different CPUs to try
	# get reproducible results
	ip netns exec $NS_DST bash -c "echo 2 > /sys/class/net/veth$DST/queues/rx-0/rps_cpus"
	ip netns exec $NS_DST taskset 0x2 ./udpgso_bench_rx -C 1000 -R 10  &
	local spid=$!
	sleep 0.1
	ip netns exec $NS_SRC taskset 0x1 ./udpgso_bench_tx $family -l 3 -S 1300 -D $dst
	local retc=$?
	wait $spid
	local rets=$?
	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
		echo " fail client exit code $retc, server $rets"
		ret=1
		return
	fi
}

for family in 4 6; do
	BM_NET=$BM_NET_V4
	OL_NET=$OL_NET_V4
	IPT=iptables
	SUFFIX=24
	VXDEV=vxlan
	PING=ping

	if [ $family = 6 ]; then
		BM_NET=$BM_NET_V6
		OL_NET=$OL_NET_V6
		SUFFIX="64 nodad"
		VXDEV=vxlan6
		IPT=ip6tables
		# Use ping6 on systems where ping doesn't handle IPv6
		ping -w 1 -c 1 ::1 > /dev/null 2>&1 || PING="ping6"
	fi

	echo "IPv$family"

	create_ns
	run_test "No GRO" $BM_NET$DST 10 0
	cleanup

	create_ns
	ip netns exec $NS_DST ethtool -K veth$DST rx-gro-list on
	run_test "GRO frag list" $BM_NET$DST 1 0
	cleanup

	# UDP GRO fwd skips aggregation when find an udp socket with the GRO option
	# if there is an UDP tunnel in the running system, such lookup happen
	# take place.
	# use NAT to circumvent GRO FWD check
	create_ns
	ip -n $NS_DST addr add dev veth$DST $BM_NET$DST_NAT/$SUFFIX
	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
	ip netns exec $NS_DST $IPT -t nat -I PREROUTING -d $BM_NET$DST_NAT \
					-j DNAT --to-destination $BM_NET$DST
	run_test "GRO fwd" $BM_NET$DST_NAT 1 0 $BM_NET$DST
	cleanup

	create_ns
	run_bench "UDP fwd perf" $BM_NET$DST
	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
	run_bench "UDP GRO fwd perf" $BM_NET$DST
	cleanup

	create_vxlan_pair
	ip netns exec $NS_DST ethtool -K veth$DST rx-gro-list on
	run_test "GRO frag list over UDP tunnel" $OL_NET$DST 1 1
	cleanup

	# use NAT to circumvent GRO FWD check
	create_vxlan_pair
	ip -n $NS_DST addr add dev $VXDEV$DST $OL_NET$DST_NAT/$SUFFIX
	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
	ip netns exec $NS_DST $IPT -t nat -I PREROUTING -d $OL_NET$DST_NAT \
					-j DNAT --to-destination $OL_NET$DST

	# load arp cache before running the test to reduce the amount of
	# stray traffic on top of the UDP tunnel
	ip netns exec $NS_SRC $PING -q -c 1 $OL_NET$DST_NAT >/dev/null
	run_test "GRO fwd over UDP tunnel" $OL_NET$DST_NAT 1 1 $OL_NET$DST
	cleanup

	create_vxlan_pair
	run_bench "UDP tunnel fwd perf" $OL_NET$DST
	ip netns exec $NS_DST ethtool -K veth$DST rx-udp-gro-forwarding on
	run_bench "UDP tunnel GRO fwd perf" $OL_NET$DST
	cleanup
done

exit $ret
' href='#n1006'>1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
// SPDX-License-Identifier: GPL-2.0
/*
 * V4L2 sensor driver for Aptina MT9V111 image sensor
 * Copyright (C) 2018 Jacopo Mondi <jacopo@jmondi.org>
 *
 * Based on mt9v032 driver
 * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * Based on mt9v011 driver
 * Copyright (c) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
 */

#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/videodev2.h>
#include <linux/v4l2-mediabus.h>
#include <linux/module.h>

#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-fwnode.h>
#include <media/v4l2-image-sizes.h>
#include <media/v4l2-subdev.h>

/*
 * MT9V111 is a 1/4-Inch CMOS digital image sensor with an integrated
 * Image Flow Processing (IFP) engine and a sensor core loosely based on
 * MT9V011.
 *
 * The IFP can produce several output image formats from the sensor core
 * output. This driver currently supports only YUYV format permutations.
 *
 * The driver allows manual frame rate control through s_frame_interval subdev
 * operation or V4L2_CID_V/HBLANK controls, but it is known that the
 * auto-exposure algorithm might modify the programmed frame rate. While the
 * driver initially programs the sensor with auto-exposure and
 * auto-white-balancing enabled, it is possible to disable them and more
 * precisely control the frame rate.
 *
 * While it seems possible to instruct the auto-exposure control algorithm to
 * respect a programmed frame rate when adjusting the pixel integration time,
 * registers controlling this feature are not documented in the public
 * available sensor manual used to develop this driver (09005aef80e90084,
 * MT9V111_1.fm - Rev. G 1/05 EN).
 */

#define MT9V111_CHIP_ID_HIGH				0x82
#define MT9V111_CHIP_ID_LOW				0x3a

#define MT9V111_R01_ADDR_SPACE				0x01
#define MT9V111_R01_IFP					0x01
#define MT9V111_R01_CORE				0x04

#define MT9V111_IFP_R06_OPMODE_CTRL			0x06
#define		MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN	BIT(1)
#define		MT9V111_IFP_R06_OPMODE_CTRL_AE_EN	BIT(14)
#define MT9V111_IFP_R07_IFP_RESET			0x07
#define		MT9V111_IFP_R07_IFP_RESET_MASK		BIT(0)
#define MT9V111_IFP_R08_OUTFMT_CTRL			0x08
#define		MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER	BIT(11)
#define		MT9V111_IFP_R08_OUTFMT_CTRL_PCLK	BIT(5)
#define MT9V111_IFP_R3A_OUTFMT_CTRL2			0x3a
#define		MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR	BIT(0)
#define		MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC	BIT(1)
#define		MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK	GENMASK(2, 0)
#define MT9V111_IFP_RA5_HPAN				0xa5
#define MT9V111_IFP_RA6_HZOOM				0xa6
#define MT9V111_IFP_RA7_HOUT				0xa7
#define MT9V111_IFP_RA8_VPAN				0xa8
#define MT9V111_IFP_RA9_VZOOM				0xa9
#define MT9V111_IFP_RAA_VOUT				0xaa
#define MT9V111_IFP_DECIMATION_MASK			GENMASK(9, 0)
#define MT9V111_IFP_DECIMATION_FREEZE			BIT(15)

#define MT9V111_CORE_R03_WIN_HEIGHT			0x03
#define		MT9V111_CORE_R03_WIN_V_OFFS		2
#define MT9V111_CORE_R04_WIN_WIDTH			0x04
#define		MT9V111_CORE_R04_WIN_H_OFFS		114
#define MT9V111_CORE_R05_HBLANK				0x05
#define		MT9V111_CORE_R05_MIN_HBLANK		0x09
#define		MT9V111_CORE_R05_MAX_HBLANK		GENMASK(9, 0)
#define		MT9V111_CORE_R05_DEF_HBLANK		0x26
#define MT9V111_CORE_R06_VBLANK				0x06
#define		MT9V111_CORE_R06_MIN_VBLANK		0x03
#define		MT9V111_CORE_R06_MAX_VBLANK		GENMASK(11, 0)
#define		MT9V111_CORE_R06_DEF_VBLANK		0x04
#define MT9V111_CORE_R07_OUT_CTRL			0x07
#define		MT9V111_CORE_R07_OUT_CTRL_SAMPLE	BIT(4)
#define MT9V111_CORE_R09_PIXEL_INT			0x09
#define		MT9V111_CORE_R09_PIXEL_INT_MASK		GENMASK(11, 0)
#define MT9V111_CORE_R0D_CORE_RESET			0x0d
#define		MT9V111_CORE_R0D_CORE_RESET_MASK	BIT(0)
#define MT9V111_CORE_RFF_CHIP_VER			0xff

#define MT9V111_PIXEL_ARRAY_WIDTH			640
#define MT9V111_PIXEL_ARRAY_HEIGHT			480

#define MT9V111_MAX_CLKIN				27000000

/* The default sensor configuration at startup time. */
static const struct v4l2_mbus_framefmt mt9v111_def_fmt = {
	.width		= 640,
	.height		= 480,
	.code		= MEDIA_BUS_FMT_UYVY8_2X8,
	.field		= V4L2_FIELD_NONE,
	.colorspace	= V4L2_COLORSPACE_SRGB,
	.ycbcr_enc	= V4L2_YCBCR_ENC_601,
	.quantization	= V4L2_QUANTIZATION_LIM_RANGE,
	.xfer_func	= V4L2_XFER_FUNC_SRGB,
};

struct mt9v111_dev {
	struct device *dev;
	struct i2c_client *client;

	u8 addr_space;

	struct v4l2_subdev sd;
#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
	struct media_pad pad;
#endif

	struct v4l2_ctrl *auto_awb;
	struct v4l2_ctrl *auto_exp;
	struct v4l2_ctrl *hblank;
	struct v4l2_ctrl *vblank;
	struct v4l2_ctrl_handler ctrls;

	/* Output image format and sizes. */
	struct v4l2_mbus_framefmt fmt;
	unsigned int fps;

	/* Protects power up/down sequences. */
	struct mutex pwr_mutex;
	int pwr_count;

	/* Protects stream on/off sequences. */
	struct mutex stream_mutex;
	bool streaming;

	/* Flags to mark HW settings as not yet applied. */
	bool pending;

	/* Clock provider and system clock frequency. */
	struct clk *clk;
	u32 sysclk;

	struct gpio_desc *oe;
	struct gpio_desc *standby;
	struct gpio_desc *reset;
};

#define sd_to_mt9v111(__sd) container_of((__sd), struct mt9v111_dev, sd)

/*
 * mt9v111_mbus_fmt - List all media bus formats supported by the driver.
 *
 * Only list the media bus code here. The image sizes are freely configurable
 * in the pixel array sizes range.
 *
 * The desired frame interval, in the supported frame interval range, is
 * obtained by configuring blanking as the sensor does not have a PLL but
 * only a fixed clock divider that generates the output pixel clock.
 */
static struct mt9v111_mbus_fmt {
	u32	code;
} mt9v111_formats[] = {
	{
		.code	= MEDIA_BUS_FMT_UYVY8_2X8,
	},
	{
		.code	= MEDIA_BUS_FMT_YUYV8_2X8,
	},
	{
		.code	= MEDIA_BUS_FMT_VYUY8_2X8,
	},
	{
		.code	= MEDIA_BUS_FMT_YVYU8_2X8,
	},
};

static u32 mt9v111_frame_intervals[] = {5, 10, 15, 20, 30};

/*
 * mt9v111_frame_sizes - List sensor's supported resolutions.
 *
 * Resolution generated through decimation in the IFP block from the
 * full VGA pixel array.
 */
static struct v4l2_rect mt9v111_frame_sizes[] = {
	{
		.width	= 640,
		.height	= 480,
	},
	{
		.width	= 352,
		.height	= 288
	},
	{
		.width	= 320,
		.height	= 240,
	},
	{
		.width	= 176,
		.height	= 144,
	},
	{
		.width	= 160,
		.height	= 120,
	},
};

/* --- Device I/O access --- */

static int __mt9v111_read(struct i2c_client *c, u8 reg, u16 *val)
{
	struct i2c_msg msg[2];
	__be16 buf;
	int ret;

	msg[0].addr = c->addr;
	msg[0].flags = 0;
	msg[0].len = 1;
	msg[0].buf = &reg;

	msg[1].addr = c->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].len = 2;
	msg[1].buf = (char *)&buf;

	ret = i2c_transfer(c->adapter, msg, 2);
	if (ret < 0) {
		dev_err(&c->dev, "i2c read transfer error: %d\n", ret);
		return ret;
	}

	*val = be16_to_cpu(buf);

	dev_dbg(&c->dev, "%s: %x=%x\n", __func__, reg, *val);

	return 0;
}

static int __mt9v111_write(struct i2c_client *c, u8 reg, u16 val)
{
	struct i2c_msg msg;
	u8 buf[3] = { 0 };
	int ret;

	buf[0] = reg;
	buf[1] = val >> 8;
	buf[2] = val & 0xff;

	msg.addr = c->addr;
	msg.flags = 0;
	msg.len = 3;
	msg.buf = (char *)buf;

	dev_dbg(&c->dev, "%s: %x = %x%x\n", __func__, reg, buf[1], buf[2]);

	ret = i2c_transfer(c->adapter, &msg, 1);
	if (ret < 0) {
		dev_err(&c->dev, "i2c write transfer error: %d\n", ret);
		return ret;
	}

	return 0;
}

static int __mt9v111_addr_space_select(struct i2c_client *c, u16 addr_space)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(c);
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
	u16 val;
	int ret;

	if (mt9v111->addr_space == addr_space)
		return 0;

	ret = __mt9v111_write(c, MT9V111_R01_ADDR_SPACE, addr_space);
	if (ret)
		return ret;

	/* Verify address space has been updated */
	ret = __mt9v111_read(c, MT9V111_R01_ADDR_SPACE, &val);
	if (ret)
		return ret;

	if (val != addr_space)
		return -EINVAL;

	mt9v111->addr_space = addr_space;

	return 0;
}

static int mt9v111_read(struct i2c_client *c, u8 addr_space, u8 reg, u16 *val)
{
	int ret;

	/* Select register address space first. */
	ret = __mt9v111_addr_space_select(c, addr_space);
	if (ret)
		return ret;

	ret = __mt9v111_read(c, reg, val);
	if (ret)
		return ret;

	return 0;
}

static int mt9v111_write(struct i2c_client *c, u8 addr_space, u8 reg, u16 val)
{
	int ret;

	/* Select register address space first. */
	ret = __mt9v111_addr_space_select(c, addr_space);
	if (ret)
		return ret;

	ret = __mt9v111_write(c, reg, val);
	if (ret)
		return ret;

	return 0;
}

static int mt9v111_update(struct i2c_client *c, u8 addr_space, u8 reg,
			  u16 mask, u16 val)
{
	u16 current_val;
	int ret;

	/* Select register address space first. */
	ret = __mt9v111_addr_space_select(c, addr_space);
	if (ret)
		return ret;

	/* Read the current register value, then update it. */
	ret = __mt9v111_read(c, reg, &current_val);
	if (ret)
		return ret;

	current_val &= ~mask;
	current_val |= (val & mask);
	ret = __mt9v111_write(c, reg, current_val);
	if (ret)
		return ret;

	return 0;
}

/* --- Sensor HW operations --- */

static int __mt9v111_power_on(struct v4l2_subdev *sd)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
	int ret;

	ret = clk_prepare_enable(mt9v111->clk);
	if (ret)
		return ret;

	clk_set_rate(mt9v111->clk, mt9v111->sysclk);

	gpiod_set_value(mt9v111->standby, 0);
	usleep_range(500, 1000);

	gpiod_set_value(mt9v111->oe, 1);
	usleep_range(500, 1000);

	return 0;
}

static int __mt9v111_power_off(struct v4l2_subdev *sd)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);

	gpiod_set_value(mt9v111->oe, 0);
	usleep_range(500, 1000);

	gpiod_set_value(mt9v111->standby, 1);
	usleep_range(500, 1000);

	clk_disable_unprepare(mt9v111->clk);

	return 0;
}

static int __mt9v111_hw_reset(struct mt9v111_dev *mt9v111)
{
	if (!mt9v111->reset)
		return -EINVAL;

	gpiod_set_value(mt9v111->reset, 1);
	usleep_range(500, 1000);

	gpiod_set_value(mt9v111->reset, 0);
	usleep_range(500, 1000);

	return 0;
}

static int __mt9v111_sw_reset(struct mt9v111_dev *mt9v111)
{
	struct i2c_client *c = mt9v111->client;
	int ret;

	/* Software reset core and IFP blocks. */

	ret = mt9v111_update(c, MT9V111_R01_CORE,
			     MT9V111_CORE_R0D_CORE_RESET,
			     MT9V111_CORE_R0D_CORE_RESET_MASK, 1);
	if (ret)
		return ret;
	usleep_range(500, 1000);

	ret = mt9v111_update(c, MT9V111_R01_CORE,
			     MT9V111_CORE_R0D_CORE_RESET,
			     MT9V111_CORE_R0D_CORE_RESET_MASK, 0);
	if (ret)
		return ret;
	usleep_range(500, 1000);

	ret = mt9v111_update(c, MT9V111_R01_IFP,
			     MT9V111_IFP_R07_IFP_RESET,
			     MT9V111_IFP_R07_IFP_RESET_MASK, 1);
	if (ret)
		return ret;
	usleep_range(500, 1000);

	ret = mt9v111_update(c, MT9V111_R01_IFP,
			     MT9V111_IFP_R07_IFP_RESET,
			     MT9V111_IFP_R07_IFP_RESET_MASK, 0);
	if (ret)
		return ret;
	usleep_range(500, 1000);

	return 0;
}

static int mt9v111_calc_frame_rate(struct mt9v111_dev *mt9v111,
				   struct v4l2_fract *tpf)
{
	unsigned int fps = tpf->numerator ?
			   tpf->denominator / tpf->numerator :
			   tpf->denominator;
	unsigned int best_diff;
	unsigned int frm_cols;
	unsigned int row_pclk;
	unsigned int best_fps;
	unsigned int pclk;
	unsigned int diff;
	unsigned int idx;
	unsigned int hb;
	unsigned int vb;
	unsigned int i;
	int ret;

	/* Approximate to the closest supported frame interval. */
	best_diff = ~0L;
	for (i = 0, idx = 0; i < ARRAY_SIZE(mt9v111_frame_intervals); i++) {
		diff = abs(fps - mt9v111_frame_intervals[i]);
		if (diff < best_diff) {
			idx = i;
			best_diff = diff;
		}
	}
	fps = mt9v111_frame_intervals[idx];

	/*
	 * The sensor does not provide a PLL circuitry and pixel clock is
	 * generated dividing the master clock source by two.
	 *
	 * Trow = (W + Hblank + 114) * 2 * (1 / SYSCLK)
	 * TFrame = Trow * (H + Vblank + 2)
	 *
	 * FPS = (SYSCLK / 2) / (Trow * (H + Vblank + 2))
	 *
	 * This boils down to tune H and V blanks to best approximate the
	 * above equation.
	 *
	 * Test all available H/V blank values, until we reach the
	 * desired frame rate.
	 */
	best_fps = vb = hb = 0;
	pclk = DIV_ROUND_CLOSEST(mt9v111->sysclk, 2);
	row_pclk = MT9V111_PIXEL_ARRAY_WIDTH + 7 + MT9V111_CORE_R04_WIN_H_OFFS;
	frm_cols = MT9V111_PIXEL_ARRAY_HEIGHT + 7 + MT9V111_CORE_R03_WIN_V_OFFS;

	best_diff = ~0L;
	for (vb = MT9V111_CORE_R06_MIN_VBLANK;
	     vb < MT9V111_CORE_R06_MAX_VBLANK; vb++) {
		for (hb = MT9V111_CORE_R05_MIN_HBLANK;
		     hb < MT9V111_CORE_R05_MAX_HBLANK; hb += 10) {
			unsigned int t_frame = (row_pclk + hb) *
					       (frm_cols + vb);
			unsigned int t_fps = DIV_ROUND_CLOSEST(pclk, t_frame);

			diff = abs(fps - t_fps);
			if (diff < best_diff) {
				best_diff = diff;
				best_fps = t_fps;

				if (diff == 0)
					break;
			}
		}

		if (diff == 0)
			break;
	}

	ret = v4l2_ctrl_s_ctrl_int64(mt9v111->hblank, hb);
	if (ret)
		return ret;

	ret = v4l2_ctrl_s_ctrl_int64(mt9v111->vblank, vb);
	if (ret)
		return ret;

	tpf->numerator = 1;
	tpf->denominator = best_fps;

	return 0;
}

static int mt9v111_hw_config(struct mt9v111_dev *mt9v111)
{
	struct i2c_client *c = mt9v111->client;
	unsigned int ret;
	u16 outfmtctrl2;

	/* Force device reset. */
	ret = __mt9v111_hw_reset(mt9v111);
	if (ret == -EINVAL)
		ret = __mt9v111_sw_reset(mt9v111);
	if (ret)
		return ret;

	/* Configure internal clock sample rate. */
	ret = mt9v111->sysclk < DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ?
				mt9v111_update(c, MT9V111_R01_CORE,
					MT9V111_CORE_R07_OUT_CTRL,
					MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 1) :
				mt9v111_update(c, MT9V111_R01_CORE,
					MT9V111_CORE_R07_OUT_CTRL,
					MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 0);
	if (ret)
		return ret;

	/*
	 * Configure output image format components ordering.
	 *
	 * TODO: IFP block can also output several RGB permutations, we only
	 *	 support YUYV permutations at the moment.
	 */
	switch (mt9v111->fmt.code) {
	case MEDIA_BUS_FMT_YUYV8_2X8:
			outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC;
			break;
	case MEDIA_BUS_FMT_VYUY8_2X8:
			outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
			break;
	case MEDIA_BUS_FMT_YVYU8_2X8:
			outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC |
				      MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
			break;
	case MEDIA_BUS_FMT_UYVY8_2X8:
	default:
			outfmtctrl2 = 0;
			break;
	}

	ret = mt9v111_update(c, MT9V111_R01_IFP, MT9V111_IFP_R3A_OUTFMT_CTRL2,
			     MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK,
			     outfmtctrl2);
	if (ret)
		return ret;

	/*
	 * Do not change default sensor's core configuration:
	 * output the whole 640x480 pixel array, skip 18 columns and 6 rows.
	 *
	 * Instead, control the output image size through IFP block.
	 *
	 * TODO: No zoom&pan support. Currently we control the output image
	 *	 size only through decimation, with no zoom support.
	 */
	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA5_HPAN,
			    MT9V111_IFP_DECIMATION_FREEZE);
	if (ret)
		return ret;

	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA8_VPAN,
			    MT9V111_IFP_DECIMATION_FREEZE);
	if (ret)
		return ret;

	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA6_HZOOM,
			    MT9V111_IFP_DECIMATION_FREEZE |
			    MT9V111_PIXEL_ARRAY_WIDTH);
	if (ret)
		return ret;

	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA9_VZOOM,
			    MT9V111_IFP_DECIMATION_FREEZE |
			    MT9V111_PIXEL_ARRAY_HEIGHT);
	if (ret)
		return ret;

	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA7_HOUT,
			    MT9V111_IFP_DECIMATION_FREEZE |
			    mt9v111->fmt.width);
	if (ret)
		return ret;

	ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RAA_VOUT,
			    mt9v111->fmt.height);
	if (ret)
		return ret;

	/* Apply controls to set auto exp, auto awb and timings */
	ret = v4l2_ctrl_handler_setup(&mt9v111->ctrls);
	if (ret)
		return ret;

	/*
	 * Set pixel integration time to the whole frame time.
	 * This value controls the the shutter delay when running with AE
	 * disabled. If longer than frame time, it affects the output
	 * frame rate.
	 */
	return mt9v111_write(c, MT9V111_R01_CORE, MT9V111_CORE_R09_PIXEL_INT,
			     MT9V111_PIXEL_ARRAY_HEIGHT);
}

/* ---  V4L2 subdev operations --- */

static int mt9v111_s_power(struct v4l2_subdev *sd, int on)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
	int pwr_count;
	int ret = 0;

	mutex_lock(&mt9v111->pwr_mutex);

	/*
	 * Make sure we're transitioning from 0 to 1, or viceversa,
	 * before actually changing the power state.
	 */
	pwr_count = mt9v111->pwr_count;
	pwr_count += on ? 1 : -1;
	if (pwr_count == !!on) {
		ret = on ? __mt9v111_power_on(sd) :
			   __mt9v111_power_off(sd);
		if (!ret)
			/* All went well, updated power counter. */
			mt9v111->pwr_count = pwr_count;

		mutex_unlock(&mt9v111->pwr_mutex);

		return ret;
	}

	/*
	 * Update power counter to keep track of how many nested calls we
	 * received.
	 */
	WARN_ON(pwr_count < 0 || pwr_count > 1);
	mt9v111->pwr_count = pwr_count;

	mutex_unlock(&mt9v111->pwr_mutex);

	return ret;
}

static int mt9v111_s_stream(struct v4l2_subdev *subdev, int enable)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
	int ret;

	mutex_lock(&mt9v111->stream_mutex);

	if (mt9v111->streaming == enable) {
		mutex_unlock(&mt9v111->stream_mutex);
		return 0;
	}

	ret = mt9v111_s_power(subdev, enable);
	if (ret)
		goto error_unlock;

	if (enable && mt9v111->pending) {
		ret = mt9v111_hw_config(mt9v111);
		if (ret)
			goto error_unlock;

		/*
		 * No need to update control here as far as only H/VBLANK are
		 * supported and immediately programmed to registers in .s_ctrl
		 */

		mt9v111->pending = false;
	}

	mt9v111->streaming = enable ? true : false;
	mutex_unlock(&mt9v111->stream_mutex);

	return 0;

error_unlock:
	mutex_unlock(&mt9v111->stream_mutex);

	return ret;
}

static int mt9v111_s_frame_interval(struct v4l2_subdev *sd,
				    struct v4l2_subdev_frame_interval *ival)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
	struct v4l2_fract *tpf = &ival->interval;
	unsigned int fps = tpf->numerator ?
			   tpf->denominator / tpf->numerator :
			   tpf->denominator;
	unsigned int max_fps;

	if (!tpf->numerator)
		tpf->numerator = 1;

	mutex_lock(&mt9v111->stream_mutex);

	if (mt9v111->streaming) {
		mutex_unlock(&mt9v111->stream_mutex);
		return -EBUSY;
	}

	if (mt9v111->fps == fps) {
		mutex_unlock(&mt9v111->stream_mutex);
		return 0;
	}

	/* Make sure frame rate/image sizes constraints are respected. */
	if (mt9v111->fmt.width < QVGA_WIDTH &&
	    mt9v111->fmt.height < QVGA_HEIGHT)
		max_fps = 90;
	else if (mt9v111->fmt.width < CIF_WIDTH &&
		 mt9v111->fmt.height < CIF_HEIGHT)
		max_fps = 60;
	else
		max_fps = mt9v111->sysclk <
				DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ? 15 :
									  30;

	if (fps > max_fps) {
		mutex_unlock(&mt9v111->stream_mutex);
		return -EINVAL;
	}

	mt9v111_calc_frame_rate(mt9v111, tpf);

	mt9v111->fps = fps;
	mt9v111->pending = true;

	mutex_unlock(&mt9v111->stream_mutex);

	return 0;
}

static int mt9v111_g_frame_interval(struct v4l2_subdev *sd,
				    struct v4l2_subdev_frame_interval *ival)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
	struct v4l2_fract *tpf = &ival->interval;

	mutex_lock(&mt9v111->stream_mutex);

	tpf->numerator = 1;
	tpf->denominator = mt9v111->fps;

	mutex_unlock(&mt9v111->stream_mutex);

	return 0;
}

static struct v4l2_mbus_framefmt *__mt9v111_get_pad_format(
					struct mt9v111_dev *mt9v111,
					struct v4l2_subdev_state *sd_state,
					unsigned int pad,
					enum v4l2_subdev_format_whence which)
{
	switch (which) {
	case V4L2_SUBDEV_FORMAT_TRY:
#if IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API)
		return v4l2_subdev_get_try_format(&mt9v111->sd, sd_state, pad);
#else
		return &sd_state->pads->try_fmt;
#endif
	case V4L2_SUBDEV_FORMAT_ACTIVE:
		return &mt9v111->fmt;
	default:
		return NULL;
	}
}

static int mt9v111_enum_mbus_code(struct v4l2_subdev *subdev,
				  struct v4l2_subdev_state *sd_state,
				  struct v4l2_subdev_mbus_code_enum *code)
{
	if (code->pad || code->index > ARRAY_SIZE(mt9v111_formats) - 1)
		return -EINVAL;

	code->code = mt9v111_formats[code->index].code;

	return 0;
}

static int mt9v111_enum_frame_interval(struct v4l2_subdev *sd,
				struct v4l2_subdev_state *sd_state,
				struct v4l2_subdev_frame_interval_enum *fie)
{
	unsigned int i;

	if (fie->pad || fie->index >= ARRAY_SIZE(mt9v111_frame_intervals))
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++)
		if (fie->width == mt9v111_frame_sizes[i].width &&
		    fie->height == mt9v111_frame_sizes[i].height)
			break;

	if (i == ARRAY_SIZE(mt9v111_frame_sizes))
		return -EINVAL;

	fie->interval.numerator = 1;
	fie->interval.denominator = mt9v111_frame_intervals[fie->index];

	return 0;
}

static int mt9v111_enum_frame_size(struct v4l2_subdev *subdev,
				   struct v4l2_subdev_state *sd_state,
				   struct v4l2_subdev_frame_size_enum *fse)
{
	if (fse->pad || fse->index >= ARRAY_SIZE(mt9v111_frame_sizes))
		return -EINVAL;

	fse->min_width = mt9v111_frame_sizes[fse->index].width;
	fse->max_width = mt9v111_frame_sizes[fse->index].width;
	fse->min_height = mt9v111_frame_sizes[fse->index].height;
	fse->max_height = mt9v111_frame_sizes[fse->index].height;

	return 0;
}

static int mt9v111_get_format(struct v4l2_subdev *subdev,
			      struct v4l2_subdev_state *sd_state,
			      struct v4l2_subdev_format *format)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);

	if (format->pad)
		return -EINVAL;

	mutex_lock(&mt9v111->stream_mutex);
	format->format = *__mt9v111_get_pad_format(mt9v111, sd_state,
						   format->pad,
						   format->which);
	mutex_unlock(&mt9v111->stream_mutex);

	return 0;
}

static int mt9v111_set_format(struct v4l2_subdev *subdev,
			      struct v4l2_subdev_state *sd_state,
			      struct v4l2_subdev_format *format)
{
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
	struct v4l2_mbus_framefmt new_fmt;
	struct v4l2_mbus_framefmt *__fmt;
	unsigned int best_fit = ~0L;
	unsigned int idx = 0;
	unsigned int i;

	mutex_lock(&mt9v111->stream_mutex);
	if (mt9v111->streaming) {
		mutex_unlock(&mt9v111->stream_mutex);
		return -EBUSY;
	}

	if (format->pad) {
		mutex_unlock(&mt9v111->stream_mutex);
		return -EINVAL;
	}

	/* Update mbus format code and sizes. */
	for (i = 0; i < ARRAY_SIZE(mt9v111_formats); i++) {
		if (format->format.code == mt9v111_formats[i].code) {
			new_fmt.code = mt9v111_formats[i].code;
			break;
		}
	}
	if (i == ARRAY_SIZE(mt9v111_formats))
		new_fmt.code = mt9v111_formats[0].code;

	for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++) {
		unsigned int fit = abs(mt9v111_frame_sizes[i].width -
				       format->format.width) +
				   abs(mt9v111_frame_sizes[i].height -
				       format->format.height);
		if (fit < best_fit) {
			best_fit = fit;
			idx = i;

			if (fit == 0)
				break;
		}
	}
	new_fmt.width = mt9v111_frame_sizes[idx].width;
	new_fmt.height = mt9v111_frame_sizes[idx].height;

	/* Update the device (or pad) format if it has changed. */
	__fmt = __mt9v111_get_pad_format(mt9v111, sd_state, format->pad,
					 format->which);

	/* Format hasn't changed, stop here. */
	if (__fmt->code == new_fmt.code &&
	    __fmt->width == new_fmt.width &&
	    __fmt->height == new_fmt.height)
		goto done;

	/* Update the format and sizes, then  mark changes as pending. */
	__fmt->code = new_fmt.code;
	__fmt->width = new_fmt.width;
	__fmt->height = new_fmt.height;

	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
		mt9v111->pending = true;

	dev_dbg(mt9v111->dev, "%s: mbus_code: %x - (%ux%u)\n",
		__func__, __fmt->code, __fmt->width, __fmt->height);

done:
	format->format = *__fmt;

	mutex_unlock(&mt9v111->stream_mutex);

	return 0;
}

static int mt9v111_init_cfg(struct v4l2_subdev *subdev,
			    struct v4l2_subdev_state *sd_state)
{
	sd_state->pads->try_fmt = mt9v111_def_fmt;

	return 0;
}

static const struct v4l2_subdev_core_ops mt9v111_core_ops = {
	.s_power		= mt9v111_s_power,
};

static const struct v4l2_subdev_video_ops mt9v111_video_ops = {
	.s_stream		= mt9v111_s_stream,
	.s_frame_interval	= mt9v111_s_frame_interval,
	.g_frame_interval	= mt9v111_g_frame_interval,
};

static const struct v4l2_subdev_pad_ops mt9v111_pad_ops = {
	.init_cfg		= mt9v111_init_cfg,
	.enum_mbus_code		= mt9v111_enum_mbus_code,
	.enum_frame_size	= mt9v111_enum_frame_size,
	.enum_frame_interval	= mt9v111_enum_frame_interval,
	.get_fmt		= mt9v111_get_format,
	.set_fmt		= mt9v111_set_format,
};

static const struct v4l2_subdev_ops mt9v111_ops = {
	.core	= &mt9v111_core_ops,
	.video	= &mt9v111_video_ops,
	.pad	= &mt9v111_pad_ops,
};

#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
static const struct media_entity_operations mt9v111_subdev_entity_ops = {
	.link_validate = v4l2_subdev_link_validate,
};
#endif

/* --- V4L2 ctrl --- */
static int mt9v111_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct mt9v111_dev *mt9v111 = container_of(ctrl->handler,
						   struct mt9v111_dev,
						   ctrls);
	int ret;

	mutex_lock(&mt9v111->pwr_mutex);
	/*
	 * If sensor is powered down, just cache new control values,
	 * no actual register access.
	 */
	if (!mt9v111->pwr_count) {
		mt9v111->pending = true;
		mutex_unlock(&mt9v111->pwr_mutex);
		return 0;
	}
	mutex_unlock(&mt9v111->pwr_mutex);

	/*
	 * Flickering control gets disabled if both auto exp and auto awb
	 * are disabled too. If any of the two is enabled, enable it.
	 *
	 * Disabling flickering when ae and awb are off allows a more precise
	 * control of the programmed frame rate.
	 */
	if (mt9v111->auto_exp->is_new || mt9v111->auto_awb->is_new) {
		if (mt9v111->auto_exp->val == V4L2_EXPOSURE_MANUAL &&
		    mt9v111->auto_awb->val == V4L2_WHITE_BALANCE_MANUAL)
			ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
					     MT9V111_IFP_R08_OUTFMT_CTRL,
					     MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
					     0);
		else
			ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
					     MT9V111_IFP_R08_OUTFMT_CTRL,
					     MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
					     1);
		if (ret)
			return ret;
	}

	ret = -EINVAL;
	switch (ctrl->id) {
	case V4L2_CID_AUTO_WHITE_BALANCE:
		ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
				     MT9V111_IFP_R06_OPMODE_CTRL,
				     MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN,
				     ctrl->val == V4L2_WHITE_BALANCE_AUTO ?
				     MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN : 0);
		break;
	case V4L2_CID_EXPOSURE_AUTO:
		ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
				     MT9V111_IFP_R06_OPMODE_CTRL,
				     MT9V111_IFP_R06_OPMODE_CTRL_AE_EN,
				     ctrl->val == V4L2_EXPOSURE_AUTO ?
				     MT9V111_IFP_R06_OPMODE_CTRL_AE_EN : 0);
		break;
	case V4L2_CID_HBLANK:
		ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
				     MT9V111_CORE_R05_HBLANK,
				     MT9V111_CORE_R05_MAX_HBLANK,
				     mt9v111->hblank->val);
		break;
	case V4L2_CID_VBLANK:
		ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
				     MT9V111_CORE_R06_VBLANK,
				     MT9V111_CORE_R06_MAX_VBLANK,
				     mt9v111->vblank->val);
		break;
	}

	return ret;
}

static const struct v4l2_ctrl_ops mt9v111_ctrl_ops = {
	.s_ctrl = mt9v111_s_ctrl,
};

static int mt9v111_chip_probe(struct mt9v111_dev *mt9v111)
{
	int ret;
	u16 val;

	ret = __mt9v111_power_on(&mt9v111->sd);
	if (ret)
		return ret;

	ret = mt9v111_read(mt9v111->client, MT9V111_R01_CORE,
			   MT9V111_CORE_RFF_CHIP_VER, &val);
	if (ret)
		goto power_off;

	if ((val >> 8) != MT9V111_CHIP_ID_HIGH &&
	    (val & 0xff) != MT9V111_CHIP_ID_LOW) {
		dev_err(mt9v111->dev,
			"Unable to identify MT9V111 chip: 0x%2x%2x\n",
			val >> 8, val & 0xff);
		ret = -EIO;
		goto power_off;
	}

	dev_dbg(mt9v111->dev, "Chip identified: 0x%2x%2x\n",
		val >> 8, val & 0xff);

power_off:
	__mt9v111_power_off(&mt9v111->sd);

	return ret;
}

static int mt9v111_probe(struct i2c_client *client)
{
	struct mt9v111_dev *mt9v111;
	struct v4l2_fract tpf;
	int ret;

	mt9v111 = devm_kzalloc(&client->dev, sizeof(*mt9v111), GFP_KERNEL);
	if (!mt9v111)
		return -ENOMEM;

	mt9v111->dev = &client->dev;
	mt9v111->client = client;

	mt9v111->clk = devm_clk_get(&client->dev, NULL);
	if (IS_ERR(mt9v111->clk))
		return PTR_ERR(mt9v111->clk);

	mt9v111->sysclk = clk_get_rate(mt9v111->clk);
	if (mt9v111->sysclk > MT9V111_MAX_CLKIN)
		return -EINVAL;

	mt9v111->oe = devm_gpiod_get_optional(&client->dev, "enable",
					      GPIOD_OUT_LOW);
	if (IS_ERR(mt9v111->oe)) {
		dev_err(&client->dev, "Unable to get GPIO \"enable\": %ld\n",
			PTR_ERR(mt9v111->oe));
		return PTR_ERR(mt9v111->oe);
	}

	mt9v111->standby = devm_gpiod_get_optional(&client->dev, "standby",
						   GPIOD_OUT_HIGH);
	if (IS_ERR(mt9v111->standby)) {
		dev_err(&client->dev, "Unable to get GPIO \"standby\": %ld\n",
			PTR_ERR(mt9v111->standby));
		return PTR_ERR(mt9v111->standby);
	}

	mt9v111->reset = devm_gpiod_get_optional(&client->dev, "reset",
						 GPIOD_OUT_LOW);
	if (IS_ERR(mt9v111->reset)) {
		dev_err(&client->dev, "Unable to get GPIO \"reset\": %ld\n",
			PTR_ERR(mt9v111->reset));
		return PTR_ERR(mt9v111->reset);
	}

	mutex_init(&mt9v111->pwr_mutex);
	mutex_init(&mt9v111->stream_mutex);

	v4l2_ctrl_handler_init(&mt9v111->ctrls, 5);

	mt9v111->auto_awb = v4l2_ctrl_new_std(&mt9v111->ctrls,
					      &mt9v111_ctrl_ops,
					      V4L2_CID_AUTO_WHITE_BALANCE,
					      0, 1, 1,
					      V4L2_WHITE_BALANCE_AUTO);
	mt9v111->auto_exp = v4l2_ctrl_new_std_menu(&mt9v111->ctrls,
						   &mt9v111_ctrl_ops,
						   V4L2_CID_EXPOSURE_AUTO,
						   V4L2_EXPOSURE_MANUAL,
						   0, V4L2_EXPOSURE_AUTO);
	mt9v111->hblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
					    V4L2_CID_HBLANK,
					    MT9V111_CORE_R05_MIN_HBLANK,
					    MT9V111_CORE_R05_MAX_HBLANK, 1,
					    MT9V111_CORE_R05_DEF_HBLANK);
	mt9v111->vblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
					    V4L2_CID_VBLANK,
					    MT9V111_CORE_R06_MIN_VBLANK,
					    MT9V111_CORE_R06_MAX_VBLANK, 1,
					    MT9V111_CORE_R06_DEF_VBLANK);

	/* PIXEL_RATE is fixed: just expose it to user space. */
	v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
			  V4L2_CID_PIXEL_RATE, 0,
			  DIV_ROUND_CLOSEST(mt9v111->sysclk, 2), 1,
			  DIV_ROUND_CLOSEST(mt9v111->sysclk, 2));

	if (mt9v111->ctrls.error) {
		ret = mt9v111->ctrls.error;
		goto error_free_ctrls;
	}
	mt9v111->sd.ctrl_handler = &mt9v111->ctrls;

	/* Start with default configuration: 640x480 UYVY. */
	mt9v111->fmt	= mt9v111_def_fmt;

	/* Re-calculate blankings for 640x480@15fps. */
	mt9v111->fps		= 15;
	tpf.numerator		= 1;
	tpf.denominator		= mt9v111->fps;
	mt9v111_calc_frame_rate(mt9v111, &tpf);

	mt9v111->pwr_count	= 0;
	mt9v111->addr_space	= MT9V111_R01_IFP;
	mt9v111->pending	= true;

	v4l2_i2c_subdev_init(&mt9v111->sd, client, &mt9v111_ops);

#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
	mt9v111->sd.flags	|= V4L2_SUBDEV_FL_HAS_DEVNODE;
	mt9v111->sd.entity.ops	= &mt9v111_subdev_entity_ops;
	mt9v111->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;

	mt9v111->pad.flags	= MEDIA_PAD_FL_SOURCE;
	ret = media_entity_pads_init(&mt9v111->sd.entity, 1, &mt9v111->pad);
	if (ret)
		goto error_free_entity;
#endif

	ret = mt9v111_chip_probe(mt9v111);
	if (ret)
		goto error_free_entity;

	ret = v4l2_async_register_subdev(&mt9v111->sd);
	if (ret)
		goto error_free_entity;

	return 0;

error_free_entity:
#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&mt9v111->sd.entity);
#endif

error_free_ctrls:
	v4l2_ctrl_handler_free(&mt9v111->ctrls);

	mutex_destroy(&mt9v111->pwr_mutex);
	mutex_destroy(&mt9v111->stream_mutex);

	return ret;
}

static void mt9v111_remove(struct i2c_client *client)
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);

	v4l2_async_unregister_subdev(sd);

#if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
	media_entity_cleanup(&sd->entity);
#endif

	v4l2_ctrl_handler_free(&mt9v111->ctrls);

	mutex_destroy(&mt9v111->pwr_mutex);
	mutex_destroy(&mt9v111->stream_mutex);
}

static const struct of_device_id mt9v111_of_match[] = {
	{ .compatible = "aptina,mt9v111", },
	{ /* sentinel */ },
};

static struct i2c_driver mt9v111_driver = {
	.driver = {
		.name = "mt9v111",
		.of_match_table = mt9v111_of_match,
	},
	.probe_new	= mt9v111_probe,
	.remove		= mt9v111_remove,
};

module_i2c_driver(mt9v111_driver);

MODULE_DESCRIPTION("V4L2 sensor driver for Aptina MT9V111");
MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
MODULE_LICENSE("GPL v2");