1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * V4L2 sensor driver for Aptina MT9V111 image sensor
4*4882a593Smuzhiyun * Copyright (C) 2018 Jacopo Mondi <jacopo@jmondi.org>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Based on mt9v032 driver
7*4882a593Smuzhiyun * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8*4882a593Smuzhiyun * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Based on mt9v011 driver
11*4882a593Smuzhiyun * Copyright (c) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/clk.h>
15*4882a593Smuzhiyun #include <linux/delay.h>
16*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
17*4882a593Smuzhiyun #include <linux/i2c.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/videodev2.h>
21*4882a593Smuzhiyun #include <linux/v4l2-mediabus.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
25*4882a593Smuzhiyun #include <media/v4l2-device.h>
26*4882a593Smuzhiyun #include <media/v4l2-fwnode.h>
27*4882a593Smuzhiyun #include <media/v4l2-image-sizes.h>
28*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * MT9V111 is a 1/4-Inch CMOS digital image sensor with an integrated
32*4882a593Smuzhiyun * Image Flow Processing (IFP) engine and a sensor core loosely based on
33*4882a593Smuzhiyun * MT9V011.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * The IFP can produce several output image formats from the sensor core
36*4882a593Smuzhiyun * output. This driver currently supports only YUYV format permutations.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * The driver allows manual frame rate control through s_frame_interval subdev
39*4882a593Smuzhiyun * operation or V4L2_CID_V/HBLANK controls, but it is known that the
40*4882a593Smuzhiyun * auto-exposure algorithm might modify the programmed frame rate. While the
41*4882a593Smuzhiyun * driver initially programs the sensor with auto-exposure and
42*4882a593Smuzhiyun * auto-white-balancing enabled, it is possible to disable them and more
43*4882a593Smuzhiyun * precisely control the frame rate.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * While it seems possible to instruct the auto-exposure control algorithm to
46*4882a593Smuzhiyun * respect a programmed frame rate when adjusting the pixel integration time,
47*4882a593Smuzhiyun * registers controlling this feature are not documented in the public
48*4882a593Smuzhiyun * available sensor manual used to develop this driver (09005aef80e90084,
49*4882a593Smuzhiyun * MT9V111_1.fm - Rev. G 1/05 EN).
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun #define MT9V111_CHIP_ID_HIGH 0x82
53*4882a593Smuzhiyun #define MT9V111_CHIP_ID_LOW 0x3a
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #define MT9V111_R01_ADDR_SPACE 0x01
56*4882a593Smuzhiyun #define MT9V111_R01_IFP 0x01
57*4882a593Smuzhiyun #define MT9V111_R01_CORE 0x04
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define MT9V111_IFP_R06_OPMODE_CTRL 0x06
60*4882a593Smuzhiyun #define MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN BIT(1)
61*4882a593Smuzhiyun #define MT9V111_IFP_R06_OPMODE_CTRL_AE_EN BIT(14)
62*4882a593Smuzhiyun #define MT9V111_IFP_R07_IFP_RESET 0x07
63*4882a593Smuzhiyun #define MT9V111_IFP_R07_IFP_RESET_MASK BIT(0)
64*4882a593Smuzhiyun #define MT9V111_IFP_R08_OUTFMT_CTRL 0x08
65*4882a593Smuzhiyun #define MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER BIT(11)
66*4882a593Smuzhiyun #define MT9V111_IFP_R08_OUTFMT_CTRL_PCLK BIT(5)
67*4882a593Smuzhiyun #define MT9V111_IFP_R3A_OUTFMT_CTRL2 0x3a
68*4882a593Smuzhiyun #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR BIT(0)
69*4882a593Smuzhiyun #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC BIT(1)
70*4882a593Smuzhiyun #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK GENMASK(2, 0)
71*4882a593Smuzhiyun #define MT9V111_IFP_RA5_HPAN 0xa5
72*4882a593Smuzhiyun #define MT9V111_IFP_RA6_HZOOM 0xa6
73*4882a593Smuzhiyun #define MT9V111_IFP_RA7_HOUT 0xa7
74*4882a593Smuzhiyun #define MT9V111_IFP_RA8_VPAN 0xa8
75*4882a593Smuzhiyun #define MT9V111_IFP_RA9_VZOOM 0xa9
76*4882a593Smuzhiyun #define MT9V111_IFP_RAA_VOUT 0xaa
77*4882a593Smuzhiyun #define MT9V111_IFP_DECIMATION_MASK GENMASK(9, 0)
78*4882a593Smuzhiyun #define MT9V111_IFP_DECIMATION_FREEZE BIT(15)
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun #define MT9V111_CORE_R03_WIN_HEIGHT 0x03
81*4882a593Smuzhiyun #define MT9V111_CORE_R03_WIN_V_OFFS 2
82*4882a593Smuzhiyun #define MT9V111_CORE_R04_WIN_WIDTH 0x04
83*4882a593Smuzhiyun #define MT9V111_CORE_R04_WIN_H_OFFS 114
84*4882a593Smuzhiyun #define MT9V111_CORE_R05_HBLANK 0x05
85*4882a593Smuzhiyun #define MT9V111_CORE_R05_MIN_HBLANK 0x09
86*4882a593Smuzhiyun #define MT9V111_CORE_R05_MAX_HBLANK GENMASK(9, 0)
87*4882a593Smuzhiyun #define MT9V111_CORE_R05_DEF_HBLANK 0x26
88*4882a593Smuzhiyun #define MT9V111_CORE_R06_VBLANK 0x06
89*4882a593Smuzhiyun #define MT9V111_CORE_R06_MIN_VBLANK 0x03
90*4882a593Smuzhiyun #define MT9V111_CORE_R06_MAX_VBLANK GENMASK(11, 0)
91*4882a593Smuzhiyun #define MT9V111_CORE_R06_DEF_VBLANK 0x04
92*4882a593Smuzhiyun #define MT9V111_CORE_R07_OUT_CTRL 0x07
93*4882a593Smuzhiyun #define MT9V111_CORE_R07_OUT_CTRL_SAMPLE BIT(4)
94*4882a593Smuzhiyun #define MT9V111_CORE_R09_PIXEL_INT 0x09
95*4882a593Smuzhiyun #define MT9V111_CORE_R09_PIXEL_INT_MASK GENMASK(11, 0)
96*4882a593Smuzhiyun #define MT9V111_CORE_R0D_CORE_RESET 0x0d
97*4882a593Smuzhiyun #define MT9V111_CORE_R0D_CORE_RESET_MASK BIT(0)
98*4882a593Smuzhiyun #define MT9V111_CORE_RFF_CHIP_VER 0xff
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun #define MT9V111_PIXEL_ARRAY_WIDTH 640
101*4882a593Smuzhiyun #define MT9V111_PIXEL_ARRAY_HEIGHT 480
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun #define MT9V111_MAX_CLKIN 27000000
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* The default sensor configuration at startup time. */
106*4882a593Smuzhiyun static const struct v4l2_mbus_framefmt mt9v111_def_fmt = {
107*4882a593Smuzhiyun .width = 640,
108*4882a593Smuzhiyun .height = 480,
109*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_UYVY8_2X8,
110*4882a593Smuzhiyun .field = V4L2_FIELD_NONE,
111*4882a593Smuzhiyun .colorspace = V4L2_COLORSPACE_SRGB,
112*4882a593Smuzhiyun .ycbcr_enc = V4L2_YCBCR_ENC_601,
113*4882a593Smuzhiyun .quantization = V4L2_QUANTIZATION_LIM_RANGE,
114*4882a593Smuzhiyun .xfer_func = V4L2_XFER_FUNC_SRGB,
115*4882a593Smuzhiyun };
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun struct mt9v111_dev {
118*4882a593Smuzhiyun struct device *dev;
119*4882a593Smuzhiyun struct i2c_client *client;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun u8 addr_space;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun struct v4l2_subdev sd;
124*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
125*4882a593Smuzhiyun struct media_pad pad;
126*4882a593Smuzhiyun #endif
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun struct v4l2_ctrl *auto_awb;
129*4882a593Smuzhiyun struct v4l2_ctrl *auto_exp;
130*4882a593Smuzhiyun struct v4l2_ctrl *hblank;
131*4882a593Smuzhiyun struct v4l2_ctrl *vblank;
132*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrls;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /* Output image format and sizes. */
135*4882a593Smuzhiyun struct v4l2_mbus_framefmt fmt;
136*4882a593Smuzhiyun unsigned int fps;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Protects power up/down sequences. */
139*4882a593Smuzhiyun struct mutex pwr_mutex;
140*4882a593Smuzhiyun int pwr_count;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Protects stream on/off sequences. */
143*4882a593Smuzhiyun struct mutex stream_mutex;
144*4882a593Smuzhiyun bool streaming;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Flags to mark HW settings as not yet applied. */
147*4882a593Smuzhiyun bool pending;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Clock provider and system clock frequency. */
150*4882a593Smuzhiyun struct clk *clk;
151*4882a593Smuzhiyun u32 sysclk;
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun struct gpio_desc *oe;
154*4882a593Smuzhiyun struct gpio_desc *standby;
155*4882a593Smuzhiyun struct gpio_desc *reset;
156*4882a593Smuzhiyun };
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun #define sd_to_mt9v111(__sd) container_of((__sd), struct mt9v111_dev, sd)
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun * mt9v111_mbus_fmt - List all media bus formats supported by the driver.
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * Only list the media bus code here. The image sizes are freely configurable
164*4882a593Smuzhiyun * in the pixel array sizes range.
165*4882a593Smuzhiyun *
166*4882a593Smuzhiyun * The desired frame interval, in the supported frame interval range, is
167*4882a593Smuzhiyun * obtained by configuring blanking as the sensor does not have a PLL but
168*4882a593Smuzhiyun * only a fixed clock divider that generates the output pixel clock.
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun static struct mt9v111_mbus_fmt {
171*4882a593Smuzhiyun u32 code;
172*4882a593Smuzhiyun } mt9v111_formats[] = {
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_UYVY8_2X8,
175*4882a593Smuzhiyun },
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_YUYV8_2X8,
178*4882a593Smuzhiyun },
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_VYUY8_2X8,
181*4882a593Smuzhiyun },
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun .code = MEDIA_BUS_FMT_YVYU8_2X8,
184*4882a593Smuzhiyun },
185*4882a593Smuzhiyun };
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun static u32 mt9v111_frame_intervals[] = {5, 10, 15, 20, 30};
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * mt9v111_frame_sizes - List sensor's supported resolutions.
191*4882a593Smuzhiyun *
192*4882a593Smuzhiyun * Resolution generated through decimation in the IFP block from the
193*4882a593Smuzhiyun * full VGA pixel array.
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun static struct v4l2_rect mt9v111_frame_sizes[] = {
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun .width = 640,
198*4882a593Smuzhiyun .height = 480,
199*4882a593Smuzhiyun },
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun .width = 352,
202*4882a593Smuzhiyun .height = 288
203*4882a593Smuzhiyun },
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun .width = 320,
206*4882a593Smuzhiyun .height = 240,
207*4882a593Smuzhiyun },
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun .width = 176,
210*4882a593Smuzhiyun .height = 144,
211*4882a593Smuzhiyun },
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun .width = 160,
214*4882a593Smuzhiyun .height = 120,
215*4882a593Smuzhiyun },
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* --- Device I/O access --- */
219*4882a593Smuzhiyun
__mt9v111_read(struct i2c_client * c,u8 reg,u16 * val)220*4882a593Smuzhiyun static int __mt9v111_read(struct i2c_client *c, u8 reg, u16 *val)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct i2c_msg msg[2];
223*4882a593Smuzhiyun __be16 buf;
224*4882a593Smuzhiyun int ret;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun msg[0].addr = c->addr;
227*4882a593Smuzhiyun msg[0].flags = 0;
228*4882a593Smuzhiyun msg[0].len = 1;
229*4882a593Smuzhiyun msg[0].buf = ®
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun msg[1].addr = c->addr;
232*4882a593Smuzhiyun msg[1].flags = I2C_M_RD;
233*4882a593Smuzhiyun msg[1].len = 2;
234*4882a593Smuzhiyun msg[1].buf = (char *)&buf;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun ret = i2c_transfer(c->adapter, msg, 2);
237*4882a593Smuzhiyun if (ret < 0) {
238*4882a593Smuzhiyun dev_err(&c->dev, "i2c read transfer error: %d\n", ret);
239*4882a593Smuzhiyun return ret;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun *val = be16_to_cpu(buf);
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun dev_dbg(&c->dev, "%s: %x=%x\n", __func__, reg, *val);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun return 0;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
__mt9v111_write(struct i2c_client * c,u8 reg,u16 val)249*4882a593Smuzhiyun static int __mt9v111_write(struct i2c_client *c, u8 reg, u16 val)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct i2c_msg msg;
252*4882a593Smuzhiyun u8 buf[3] = { 0 };
253*4882a593Smuzhiyun int ret;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun buf[0] = reg;
256*4882a593Smuzhiyun buf[1] = val >> 8;
257*4882a593Smuzhiyun buf[2] = val & 0xff;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun msg.addr = c->addr;
260*4882a593Smuzhiyun msg.flags = 0;
261*4882a593Smuzhiyun msg.len = 3;
262*4882a593Smuzhiyun msg.buf = (char *)buf;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun dev_dbg(&c->dev, "%s: %x = %x%x\n", __func__, reg, buf[1], buf[2]);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun ret = i2c_transfer(c->adapter, &msg, 1);
267*4882a593Smuzhiyun if (ret < 0) {
268*4882a593Smuzhiyun dev_err(&c->dev, "i2c write transfer error: %d\n", ret);
269*4882a593Smuzhiyun return ret;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun return 0;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
__mt9v111_addr_space_select(struct i2c_client * c,u16 addr_space)275*4882a593Smuzhiyun static int __mt9v111_addr_space_select(struct i2c_client *c, u16 addr_space)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun struct v4l2_subdev *sd = i2c_get_clientdata(c);
278*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
279*4882a593Smuzhiyun u16 val;
280*4882a593Smuzhiyun int ret;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun if (mt9v111->addr_space == addr_space)
283*4882a593Smuzhiyun return 0;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun ret = __mt9v111_write(c, MT9V111_R01_ADDR_SPACE, addr_space);
286*4882a593Smuzhiyun if (ret)
287*4882a593Smuzhiyun return ret;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* Verify address space has been updated */
290*4882a593Smuzhiyun ret = __mt9v111_read(c, MT9V111_R01_ADDR_SPACE, &val);
291*4882a593Smuzhiyun if (ret)
292*4882a593Smuzhiyun return ret;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (val != addr_space)
295*4882a593Smuzhiyun return -EINVAL;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun mt9v111->addr_space = addr_space;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return 0;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
mt9v111_read(struct i2c_client * c,u8 addr_space,u8 reg,u16 * val)302*4882a593Smuzhiyun static int mt9v111_read(struct i2c_client *c, u8 addr_space, u8 reg, u16 *val)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun int ret;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* Select register address space first. */
307*4882a593Smuzhiyun ret = __mt9v111_addr_space_select(c, addr_space);
308*4882a593Smuzhiyun if (ret)
309*4882a593Smuzhiyun return ret;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun ret = __mt9v111_read(c, reg, val);
312*4882a593Smuzhiyun if (ret)
313*4882a593Smuzhiyun return ret;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun return 0;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
mt9v111_write(struct i2c_client * c,u8 addr_space,u8 reg,u16 val)318*4882a593Smuzhiyun static int mt9v111_write(struct i2c_client *c, u8 addr_space, u8 reg, u16 val)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun int ret;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /* Select register address space first. */
323*4882a593Smuzhiyun ret = __mt9v111_addr_space_select(c, addr_space);
324*4882a593Smuzhiyun if (ret)
325*4882a593Smuzhiyun return ret;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun ret = __mt9v111_write(c, reg, val);
328*4882a593Smuzhiyun if (ret)
329*4882a593Smuzhiyun return ret;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun return 0;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
mt9v111_update(struct i2c_client * c,u8 addr_space,u8 reg,u16 mask,u16 val)334*4882a593Smuzhiyun static int mt9v111_update(struct i2c_client *c, u8 addr_space, u8 reg,
335*4882a593Smuzhiyun u16 mask, u16 val)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun u16 current_val;
338*4882a593Smuzhiyun int ret;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /* Select register address space first. */
341*4882a593Smuzhiyun ret = __mt9v111_addr_space_select(c, addr_space);
342*4882a593Smuzhiyun if (ret)
343*4882a593Smuzhiyun return ret;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /* Read the current register value, then update it. */
346*4882a593Smuzhiyun ret = __mt9v111_read(c, reg, ¤t_val);
347*4882a593Smuzhiyun if (ret)
348*4882a593Smuzhiyun return ret;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun current_val &= ~mask;
351*4882a593Smuzhiyun current_val |= (val & mask);
352*4882a593Smuzhiyun ret = __mt9v111_write(c, reg, current_val);
353*4882a593Smuzhiyun if (ret)
354*4882a593Smuzhiyun return ret;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun return 0;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /* --- Sensor HW operations --- */
360*4882a593Smuzhiyun
__mt9v111_power_on(struct v4l2_subdev * sd)361*4882a593Smuzhiyun static int __mt9v111_power_on(struct v4l2_subdev *sd)
362*4882a593Smuzhiyun {
363*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
364*4882a593Smuzhiyun int ret;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun ret = clk_prepare_enable(mt9v111->clk);
367*4882a593Smuzhiyun if (ret)
368*4882a593Smuzhiyun return ret;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun clk_set_rate(mt9v111->clk, mt9v111->sysclk);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun gpiod_set_value(mt9v111->standby, 0);
373*4882a593Smuzhiyun usleep_range(500, 1000);
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun gpiod_set_value(mt9v111->oe, 1);
376*4882a593Smuzhiyun usleep_range(500, 1000);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun return 0;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
__mt9v111_power_off(struct v4l2_subdev * sd)381*4882a593Smuzhiyun static int __mt9v111_power_off(struct v4l2_subdev *sd)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun gpiod_set_value(mt9v111->oe, 0);
386*4882a593Smuzhiyun usleep_range(500, 1000);
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun gpiod_set_value(mt9v111->standby, 1);
389*4882a593Smuzhiyun usleep_range(500, 1000);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun clk_disable_unprepare(mt9v111->clk);
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun return 0;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
__mt9v111_hw_reset(struct mt9v111_dev * mt9v111)396*4882a593Smuzhiyun static int __mt9v111_hw_reset(struct mt9v111_dev *mt9v111)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun if (!mt9v111->reset)
399*4882a593Smuzhiyun return -EINVAL;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun gpiod_set_value(mt9v111->reset, 1);
402*4882a593Smuzhiyun usleep_range(500, 1000);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun gpiod_set_value(mt9v111->reset, 0);
405*4882a593Smuzhiyun usleep_range(500, 1000);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun return 0;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
__mt9v111_sw_reset(struct mt9v111_dev * mt9v111)410*4882a593Smuzhiyun static int __mt9v111_sw_reset(struct mt9v111_dev *mt9v111)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun struct i2c_client *c = mt9v111->client;
413*4882a593Smuzhiyun int ret;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun /* Software reset core and IFP blocks. */
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun ret = mt9v111_update(c, MT9V111_R01_CORE,
418*4882a593Smuzhiyun MT9V111_CORE_R0D_CORE_RESET,
419*4882a593Smuzhiyun MT9V111_CORE_R0D_CORE_RESET_MASK, 1);
420*4882a593Smuzhiyun if (ret)
421*4882a593Smuzhiyun return ret;
422*4882a593Smuzhiyun usleep_range(500, 1000);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun ret = mt9v111_update(c, MT9V111_R01_CORE,
425*4882a593Smuzhiyun MT9V111_CORE_R0D_CORE_RESET,
426*4882a593Smuzhiyun MT9V111_CORE_R0D_CORE_RESET_MASK, 0);
427*4882a593Smuzhiyun if (ret)
428*4882a593Smuzhiyun return ret;
429*4882a593Smuzhiyun usleep_range(500, 1000);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun ret = mt9v111_update(c, MT9V111_R01_IFP,
432*4882a593Smuzhiyun MT9V111_IFP_R07_IFP_RESET,
433*4882a593Smuzhiyun MT9V111_IFP_R07_IFP_RESET_MASK, 1);
434*4882a593Smuzhiyun if (ret)
435*4882a593Smuzhiyun return ret;
436*4882a593Smuzhiyun usleep_range(500, 1000);
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun ret = mt9v111_update(c, MT9V111_R01_IFP,
439*4882a593Smuzhiyun MT9V111_IFP_R07_IFP_RESET,
440*4882a593Smuzhiyun MT9V111_IFP_R07_IFP_RESET_MASK, 0);
441*4882a593Smuzhiyun if (ret)
442*4882a593Smuzhiyun return ret;
443*4882a593Smuzhiyun usleep_range(500, 1000);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun return 0;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
mt9v111_calc_frame_rate(struct mt9v111_dev * mt9v111,struct v4l2_fract * tpf)448*4882a593Smuzhiyun static int mt9v111_calc_frame_rate(struct mt9v111_dev *mt9v111,
449*4882a593Smuzhiyun struct v4l2_fract *tpf)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun unsigned int fps = tpf->numerator ?
452*4882a593Smuzhiyun tpf->denominator / tpf->numerator :
453*4882a593Smuzhiyun tpf->denominator;
454*4882a593Smuzhiyun unsigned int best_diff;
455*4882a593Smuzhiyun unsigned int frm_cols;
456*4882a593Smuzhiyun unsigned int row_pclk;
457*4882a593Smuzhiyun unsigned int best_fps;
458*4882a593Smuzhiyun unsigned int pclk;
459*4882a593Smuzhiyun unsigned int diff;
460*4882a593Smuzhiyun unsigned int idx;
461*4882a593Smuzhiyun unsigned int hb;
462*4882a593Smuzhiyun unsigned int vb;
463*4882a593Smuzhiyun unsigned int i;
464*4882a593Smuzhiyun int ret;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun /* Approximate to the closest supported frame interval. */
467*4882a593Smuzhiyun best_diff = ~0L;
468*4882a593Smuzhiyun for (i = 0, idx = 0; i < ARRAY_SIZE(mt9v111_frame_intervals); i++) {
469*4882a593Smuzhiyun diff = abs(fps - mt9v111_frame_intervals[i]);
470*4882a593Smuzhiyun if (diff < best_diff) {
471*4882a593Smuzhiyun idx = i;
472*4882a593Smuzhiyun best_diff = diff;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun fps = mt9v111_frame_intervals[idx];
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /*
478*4882a593Smuzhiyun * The sensor does not provide a PLL circuitry and pixel clock is
479*4882a593Smuzhiyun * generated dividing the master clock source by two.
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * Trow = (W + Hblank + 114) * 2 * (1 / SYSCLK)
482*4882a593Smuzhiyun * TFrame = Trow * (H + Vblank + 2)
483*4882a593Smuzhiyun *
484*4882a593Smuzhiyun * FPS = (SYSCLK / 2) / (Trow * (H + Vblank + 2))
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * This boils down to tune H and V blanks to best approximate the
487*4882a593Smuzhiyun * above equation.
488*4882a593Smuzhiyun *
489*4882a593Smuzhiyun * Test all available H/V blank values, until we reach the
490*4882a593Smuzhiyun * desired frame rate.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun best_fps = vb = hb = 0;
493*4882a593Smuzhiyun pclk = DIV_ROUND_CLOSEST(mt9v111->sysclk, 2);
494*4882a593Smuzhiyun row_pclk = MT9V111_PIXEL_ARRAY_WIDTH + 7 + MT9V111_CORE_R04_WIN_H_OFFS;
495*4882a593Smuzhiyun frm_cols = MT9V111_PIXEL_ARRAY_HEIGHT + 7 + MT9V111_CORE_R03_WIN_V_OFFS;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun best_diff = ~0L;
498*4882a593Smuzhiyun for (vb = MT9V111_CORE_R06_MIN_VBLANK;
499*4882a593Smuzhiyun vb < MT9V111_CORE_R06_MAX_VBLANK; vb++) {
500*4882a593Smuzhiyun for (hb = MT9V111_CORE_R05_MIN_HBLANK;
501*4882a593Smuzhiyun hb < MT9V111_CORE_R05_MAX_HBLANK; hb += 10) {
502*4882a593Smuzhiyun unsigned int t_frame = (row_pclk + hb) *
503*4882a593Smuzhiyun (frm_cols + vb);
504*4882a593Smuzhiyun unsigned int t_fps = DIV_ROUND_CLOSEST(pclk, t_frame);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun diff = abs(fps - t_fps);
507*4882a593Smuzhiyun if (diff < best_diff) {
508*4882a593Smuzhiyun best_diff = diff;
509*4882a593Smuzhiyun best_fps = t_fps;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun if (diff == 0)
512*4882a593Smuzhiyun break;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun if (diff == 0)
517*4882a593Smuzhiyun break;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun ret = v4l2_ctrl_s_ctrl_int64(mt9v111->hblank, hb);
521*4882a593Smuzhiyun if (ret)
522*4882a593Smuzhiyun return ret;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun ret = v4l2_ctrl_s_ctrl_int64(mt9v111->vblank, vb);
525*4882a593Smuzhiyun if (ret)
526*4882a593Smuzhiyun return ret;
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun tpf->numerator = 1;
529*4882a593Smuzhiyun tpf->denominator = best_fps;
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun return 0;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
mt9v111_hw_config(struct mt9v111_dev * mt9v111)534*4882a593Smuzhiyun static int mt9v111_hw_config(struct mt9v111_dev *mt9v111)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun struct i2c_client *c = mt9v111->client;
537*4882a593Smuzhiyun unsigned int ret;
538*4882a593Smuzhiyun u16 outfmtctrl2;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /* Force device reset. */
541*4882a593Smuzhiyun ret = __mt9v111_hw_reset(mt9v111);
542*4882a593Smuzhiyun if (ret == -EINVAL)
543*4882a593Smuzhiyun ret = __mt9v111_sw_reset(mt9v111);
544*4882a593Smuzhiyun if (ret)
545*4882a593Smuzhiyun return ret;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /* Configure internal clock sample rate. */
548*4882a593Smuzhiyun ret = mt9v111->sysclk < DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ?
549*4882a593Smuzhiyun mt9v111_update(c, MT9V111_R01_CORE,
550*4882a593Smuzhiyun MT9V111_CORE_R07_OUT_CTRL,
551*4882a593Smuzhiyun MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 1) :
552*4882a593Smuzhiyun mt9v111_update(c, MT9V111_R01_CORE,
553*4882a593Smuzhiyun MT9V111_CORE_R07_OUT_CTRL,
554*4882a593Smuzhiyun MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 0);
555*4882a593Smuzhiyun if (ret)
556*4882a593Smuzhiyun return ret;
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /*
559*4882a593Smuzhiyun * Configure output image format components ordering.
560*4882a593Smuzhiyun *
561*4882a593Smuzhiyun * TODO: IFP block can also output several RGB permutations, we only
562*4882a593Smuzhiyun * support YUYV permutations at the moment.
563*4882a593Smuzhiyun */
564*4882a593Smuzhiyun switch (mt9v111->fmt.code) {
565*4882a593Smuzhiyun case MEDIA_BUS_FMT_YUYV8_2X8:
566*4882a593Smuzhiyun outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC;
567*4882a593Smuzhiyun break;
568*4882a593Smuzhiyun case MEDIA_BUS_FMT_VYUY8_2X8:
569*4882a593Smuzhiyun outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
570*4882a593Smuzhiyun break;
571*4882a593Smuzhiyun case MEDIA_BUS_FMT_YVYU8_2X8:
572*4882a593Smuzhiyun outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC |
573*4882a593Smuzhiyun MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
574*4882a593Smuzhiyun break;
575*4882a593Smuzhiyun case MEDIA_BUS_FMT_UYVY8_2X8:
576*4882a593Smuzhiyun default:
577*4882a593Smuzhiyun outfmtctrl2 = 0;
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun ret = mt9v111_update(c, MT9V111_R01_IFP, MT9V111_IFP_R3A_OUTFMT_CTRL2,
582*4882a593Smuzhiyun MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK,
583*4882a593Smuzhiyun outfmtctrl2);
584*4882a593Smuzhiyun if (ret)
585*4882a593Smuzhiyun return ret;
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun * Do not change default sensor's core configuration:
589*4882a593Smuzhiyun * output the whole 640x480 pixel array, skip 18 columns and 6 rows.
590*4882a593Smuzhiyun *
591*4882a593Smuzhiyun * Instead, control the output image size through IFP block.
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * TODO: No zoom&pan support. Currently we control the output image
594*4882a593Smuzhiyun * size only through decimation, with no zoom support.
595*4882a593Smuzhiyun */
596*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA5_HPAN,
597*4882a593Smuzhiyun MT9V111_IFP_DECIMATION_FREEZE);
598*4882a593Smuzhiyun if (ret)
599*4882a593Smuzhiyun return ret;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA8_VPAN,
602*4882a593Smuzhiyun MT9V111_IFP_DECIMATION_FREEZE);
603*4882a593Smuzhiyun if (ret)
604*4882a593Smuzhiyun return ret;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA6_HZOOM,
607*4882a593Smuzhiyun MT9V111_IFP_DECIMATION_FREEZE |
608*4882a593Smuzhiyun MT9V111_PIXEL_ARRAY_WIDTH);
609*4882a593Smuzhiyun if (ret)
610*4882a593Smuzhiyun return ret;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA9_VZOOM,
613*4882a593Smuzhiyun MT9V111_IFP_DECIMATION_FREEZE |
614*4882a593Smuzhiyun MT9V111_PIXEL_ARRAY_HEIGHT);
615*4882a593Smuzhiyun if (ret)
616*4882a593Smuzhiyun return ret;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA7_HOUT,
619*4882a593Smuzhiyun MT9V111_IFP_DECIMATION_FREEZE |
620*4882a593Smuzhiyun mt9v111->fmt.width);
621*4882a593Smuzhiyun if (ret)
622*4882a593Smuzhiyun return ret;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RAA_VOUT,
625*4882a593Smuzhiyun mt9v111->fmt.height);
626*4882a593Smuzhiyun if (ret)
627*4882a593Smuzhiyun return ret;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /* Apply controls to set auto exp, auto awb and timings */
630*4882a593Smuzhiyun ret = v4l2_ctrl_handler_setup(&mt9v111->ctrls);
631*4882a593Smuzhiyun if (ret)
632*4882a593Smuzhiyun return ret;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /*
635*4882a593Smuzhiyun * Set pixel integration time to the whole frame time.
636*4882a593Smuzhiyun * This value controls the the shutter delay when running with AE
637*4882a593Smuzhiyun * disabled. If longer than frame time, it affects the output
638*4882a593Smuzhiyun * frame rate.
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun return mt9v111_write(c, MT9V111_R01_CORE, MT9V111_CORE_R09_PIXEL_INT,
641*4882a593Smuzhiyun MT9V111_PIXEL_ARRAY_HEIGHT);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /* --- V4L2 subdev operations --- */
645*4882a593Smuzhiyun
mt9v111_s_power(struct v4l2_subdev * sd,int on)646*4882a593Smuzhiyun static int mt9v111_s_power(struct v4l2_subdev *sd, int on)
647*4882a593Smuzhiyun {
648*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
649*4882a593Smuzhiyun int pwr_count;
650*4882a593Smuzhiyun int ret = 0;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun mutex_lock(&mt9v111->pwr_mutex);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun /*
655*4882a593Smuzhiyun * Make sure we're transitioning from 0 to 1, or viceversa,
656*4882a593Smuzhiyun * before actually changing the power state.
657*4882a593Smuzhiyun */
658*4882a593Smuzhiyun pwr_count = mt9v111->pwr_count;
659*4882a593Smuzhiyun pwr_count += on ? 1 : -1;
660*4882a593Smuzhiyun if (pwr_count == !!on) {
661*4882a593Smuzhiyun ret = on ? __mt9v111_power_on(sd) :
662*4882a593Smuzhiyun __mt9v111_power_off(sd);
663*4882a593Smuzhiyun if (!ret)
664*4882a593Smuzhiyun /* All went well, updated power counter. */
665*4882a593Smuzhiyun mt9v111->pwr_count = pwr_count;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun mutex_unlock(&mt9v111->pwr_mutex);
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun return ret;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /*
673*4882a593Smuzhiyun * Update power counter to keep track of how many nested calls we
674*4882a593Smuzhiyun * received.
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun WARN_ON(pwr_count < 0 || pwr_count > 1);
677*4882a593Smuzhiyun mt9v111->pwr_count = pwr_count;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun mutex_unlock(&mt9v111->pwr_mutex);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun return ret;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun
mt9v111_s_stream(struct v4l2_subdev * subdev,int enable)684*4882a593Smuzhiyun static int mt9v111_s_stream(struct v4l2_subdev *subdev, int enable)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
687*4882a593Smuzhiyun int ret;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun mutex_lock(&mt9v111->stream_mutex);
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun if (mt9v111->streaming == enable) {
692*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
693*4882a593Smuzhiyun return 0;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun ret = mt9v111_s_power(subdev, enable);
697*4882a593Smuzhiyun if (ret)
698*4882a593Smuzhiyun goto error_unlock;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun if (enable && mt9v111->pending) {
701*4882a593Smuzhiyun ret = mt9v111_hw_config(mt9v111);
702*4882a593Smuzhiyun if (ret)
703*4882a593Smuzhiyun goto error_unlock;
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /*
706*4882a593Smuzhiyun * No need to update control here as far as only H/VBLANK are
707*4882a593Smuzhiyun * supported and immediately programmed to registers in .s_ctrl
708*4882a593Smuzhiyun */
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun mt9v111->pending = false;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun mt9v111->streaming = enable ? true : false;
714*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun return 0;
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun error_unlock:
719*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun return ret;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun
mt9v111_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * ival)724*4882a593Smuzhiyun static int mt9v111_s_frame_interval(struct v4l2_subdev *sd,
725*4882a593Smuzhiyun struct v4l2_subdev_frame_interval *ival)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
728*4882a593Smuzhiyun struct v4l2_fract *tpf = &ival->interval;
729*4882a593Smuzhiyun unsigned int fps = tpf->numerator ?
730*4882a593Smuzhiyun tpf->denominator / tpf->numerator :
731*4882a593Smuzhiyun tpf->denominator;
732*4882a593Smuzhiyun unsigned int max_fps;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun if (!tpf->numerator)
735*4882a593Smuzhiyun tpf->numerator = 1;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun mutex_lock(&mt9v111->stream_mutex);
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun if (mt9v111->streaming) {
740*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
741*4882a593Smuzhiyun return -EBUSY;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun if (mt9v111->fps == fps) {
745*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
746*4882a593Smuzhiyun return 0;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* Make sure frame rate/image sizes constraints are respected. */
750*4882a593Smuzhiyun if (mt9v111->fmt.width < QVGA_WIDTH &&
751*4882a593Smuzhiyun mt9v111->fmt.height < QVGA_HEIGHT)
752*4882a593Smuzhiyun max_fps = 90;
753*4882a593Smuzhiyun else if (mt9v111->fmt.width < CIF_WIDTH &&
754*4882a593Smuzhiyun mt9v111->fmt.height < CIF_HEIGHT)
755*4882a593Smuzhiyun max_fps = 60;
756*4882a593Smuzhiyun else
757*4882a593Smuzhiyun max_fps = mt9v111->sysclk <
758*4882a593Smuzhiyun DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ? 15 :
759*4882a593Smuzhiyun 30;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (fps > max_fps) {
762*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
763*4882a593Smuzhiyun return -EINVAL;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun mt9v111_calc_frame_rate(mt9v111, tpf);
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun mt9v111->fps = fps;
769*4882a593Smuzhiyun mt9v111->pending = true;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun return 0;
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun
mt9v111_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * ival)776*4882a593Smuzhiyun static int mt9v111_g_frame_interval(struct v4l2_subdev *sd,
777*4882a593Smuzhiyun struct v4l2_subdev_frame_interval *ival)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
780*4882a593Smuzhiyun struct v4l2_fract *tpf = &ival->interval;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun mutex_lock(&mt9v111->stream_mutex);
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun tpf->numerator = 1;
785*4882a593Smuzhiyun tpf->denominator = mt9v111->fps;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun return 0;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun
__mt9v111_get_pad_format(struct mt9v111_dev * mt9v111,struct v4l2_subdev_pad_config * cfg,unsigned int pad,enum v4l2_subdev_format_whence which)792*4882a593Smuzhiyun static struct v4l2_mbus_framefmt *__mt9v111_get_pad_format(
793*4882a593Smuzhiyun struct mt9v111_dev *mt9v111,
794*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
795*4882a593Smuzhiyun unsigned int pad,
796*4882a593Smuzhiyun enum v4l2_subdev_format_whence which)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun switch (which) {
799*4882a593Smuzhiyun case V4L2_SUBDEV_FORMAT_TRY:
800*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_VIDEO_V4L2_SUBDEV_API)
801*4882a593Smuzhiyun return v4l2_subdev_get_try_format(&mt9v111->sd, cfg, pad);
802*4882a593Smuzhiyun #else
803*4882a593Smuzhiyun return &cfg->try_fmt;
804*4882a593Smuzhiyun #endif
805*4882a593Smuzhiyun case V4L2_SUBDEV_FORMAT_ACTIVE:
806*4882a593Smuzhiyun return &mt9v111->fmt;
807*4882a593Smuzhiyun default:
808*4882a593Smuzhiyun return NULL;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun
mt9v111_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)812*4882a593Smuzhiyun static int mt9v111_enum_mbus_code(struct v4l2_subdev *subdev,
813*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
814*4882a593Smuzhiyun struct v4l2_subdev_mbus_code_enum *code)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun if (code->pad || code->index > ARRAY_SIZE(mt9v111_formats) - 1)
817*4882a593Smuzhiyun return -EINVAL;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun code->code = mt9v111_formats[code->index].code;
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun return 0;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun
mt9v111_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)824*4882a593Smuzhiyun static int mt9v111_enum_frame_interval(struct v4l2_subdev *sd,
825*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
826*4882a593Smuzhiyun struct v4l2_subdev_frame_interval_enum *fie)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun unsigned int i;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun if (fie->pad || fie->index >= ARRAY_SIZE(mt9v111_frame_intervals))
831*4882a593Smuzhiyun return -EINVAL;
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++)
834*4882a593Smuzhiyun if (fie->width == mt9v111_frame_sizes[i].width &&
835*4882a593Smuzhiyun fie->height == mt9v111_frame_sizes[i].height)
836*4882a593Smuzhiyun break;
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun if (i == ARRAY_SIZE(mt9v111_frame_sizes))
839*4882a593Smuzhiyun return -EINVAL;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun fie->interval.numerator = 1;
842*4882a593Smuzhiyun fie->interval.denominator = mt9v111_frame_intervals[fie->index];
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun return 0;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
mt9v111_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)847*4882a593Smuzhiyun static int mt9v111_enum_frame_size(struct v4l2_subdev *subdev,
848*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
849*4882a593Smuzhiyun struct v4l2_subdev_frame_size_enum *fse)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun if (fse->pad || fse->index >= ARRAY_SIZE(mt9v111_frame_sizes))
852*4882a593Smuzhiyun return -EINVAL;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun fse->min_width = mt9v111_frame_sizes[fse->index].width;
855*4882a593Smuzhiyun fse->max_width = mt9v111_frame_sizes[fse->index].width;
856*4882a593Smuzhiyun fse->min_height = mt9v111_frame_sizes[fse->index].height;
857*4882a593Smuzhiyun fse->max_height = mt9v111_frame_sizes[fse->index].height;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun return 0;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun
mt9v111_get_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)862*4882a593Smuzhiyun static int mt9v111_get_format(struct v4l2_subdev *subdev,
863*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
864*4882a593Smuzhiyun struct v4l2_subdev_format *format)
865*4882a593Smuzhiyun {
866*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun if (format->pad)
869*4882a593Smuzhiyun return -EINVAL;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun mutex_lock(&mt9v111->stream_mutex);
872*4882a593Smuzhiyun format->format = *__mt9v111_get_pad_format(mt9v111, cfg, format->pad,
873*4882a593Smuzhiyun format->which);
874*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
875*4882a593Smuzhiyun
876*4882a593Smuzhiyun return 0;
877*4882a593Smuzhiyun }
878*4882a593Smuzhiyun
mt9v111_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)879*4882a593Smuzhiyun static int mt9v111_set_format(struct v4l2_subdev *subdev,
880*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg,
881*4882a593Smuzhiyun struct v4l2_subdev_format *format)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
884*4882a593Smuzhiyun struct v4l2_mbus_framefmt new_fmt;
885*4882a593Smuzhiyun struct v4l2_mbus_framefmt *__fmt;
886*4882a593Smuzhiyun unsigned int best_fit = ~0L;
887*4882a593Smuzhiyun unsigned int idx = 0;
888*4882a593Smuzhiyun unsigned int i;
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun mutex_lock(&mt9v111->stream_mutex);
891*4882a593Smuzhiyun if (mt9v111->streaming) {
892*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
893*4882a593Smuzhiyun return -EBUSY;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun if (format->pad) {
897*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
898*4882a593Smuzhiyun return -EINVAL;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun /* Update mbus format code and sizes. */
902*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(mt9v111_formats); i++) {
903*4882a593Smuzhiyun if (format->format.code == mt9v111_formats[i].code) {
904*4882a593Smuzhiyun new_fmt.code = mt9v111_formats[i].code;
905*4882a593Smuzhiyun break;
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun if (i == ARRAY_SIZE(mt9v111_formats))
909*4882a593Smuzhiyun new_fmt.code = mt9v111_formats[0].code;
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++) {
912*4882a593Smuzhiyun unsigned int fit = abs(mt9v111_frame_sizes[i].width -
913*4882a593Smuzhiyun format->format.width) +
914*4882a593Smuzhiyun abs(mt9v111_frame_sizes[i].height -
915*4882a593Smuzhiyun format->format.height);
916*4882a593Smuzhiyun if (fit < best_fit) {
917*4882a593Smuzhiyun best_fit = fit;
918*4882a593Smuzhiyun idx = i;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun if (fit == 0)
921*4882a593Smuzhiyun break;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun new_fmt.width = mt9v111_frame_sizes[idx].width;
925*4882a593Smuzhiyun new_fmt.height = mt9v111_frame_sizes[idx].height;
926*4882a593Smuzhiyun
927*4882a593Smuzhiyun /* Update the device (or pad) format if it has changed. */
928*4882a593Smuzhiyun __fmt = __mt9v111_get_pad_format(mt9v111, cfg, format->pad,
929*4882a593Smuzhiyun format->which);
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun /* Format hasn't changed, stop here. */
932*4882a593Smuzhiyun if (__fmt->code == new_fmt.code &&
933*4882a593Smuzhiyun __fmt->width == new_fmt.width &&
934*4882a593Smuzhiyun __fmt->height == new_fmt.height)
935*4882a593Smuzhiyun goto done;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun /* Update the format and sizes, then mark changes as pending. */
938*4882a593Smuzhiyun __fmt->code = new_fmt.code;
939*4882a593Smuzhiyun __fmt->width = new_fmt.width;
940*4882a593Smuzhiyun __fmt->height = new_fmt.height;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
943*4882a593Smuzhiyun mt9v111->pending = true;
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun dev_dbg(mt9v111->dev, "%s: mbus_code: %x - (%ux%u)\n",
946*4882a593Smuzhiyun __func__, __fmt->code, __fmt->width, __fmt->height);
947*4882a593Smuzhiyun
948*4882a593Smuzhiyun done:
949*4882a593Smuzhiyun format->format = *__fmt;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun mutex_unlock(&mt9v111->stream_mutex);
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun return 0;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun
mt9v111_init_cfg(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg)956*4882a593Smuzhiyun static int mt9v111_init_cfg(struct v4l2_subdev *subdev,
957*4882a593Smuzhiyun struct v4l2_subdev_pad_config *cfg)
958*4882a593Smuzhiyun {
959*4882a593Smuzhiyun cfg->try_fmt = mt9v111_def_fmt;
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun return 0;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops mt9v111_core_ops = {
965*4882a593Smuzhiyun .s_power = mt9v111_s_power,
966*4882a593Smuzhiyun };
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun static const struct v4l2_subdev_video_ops mt9v111_video_ops = {
969*4882a593Smuzhiyun .s_stream = mt9v111_s_stream,
970*4882a593Smuzhiyun .s_frame_interval = mt9v111_s_frame_interval,
971*4882a593Smuzhiyun .g_frame_interval = mt9v111_g_frame_interval,
972*4882a593Smuzhiyun };
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops mt9v111_pad_ops = {
975*4882a593Smuzhiyun .init_cfg = mt9v111_init_cfg,
976*4882a593Smuzhiyun .enum_mbus_code = mt9v111_enum_mbus_code,
977*4882a593Smuzhiyun .enum_frame_size = mt9v111_enum_frame_size,
978*4882a593Smuzhiyun .enum_frame_interval = mt9v111_enum_frame_interval,
979*4882a593Smuzhiyun .get_fmt = mt9v111_get_format,
980*4882a593Smuzhiyun .set_fmt = mt9v111_set_format,
981*4882a593Smuzhiyun };
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun static const struct v4l2_subdev_ops mt9v111_ops = {
984*4882a593Smuzhiyun .core = &mt9v111_core_ops,
985*4882a593Smuzhiyun .video = &mt9v111_video_ops,
986*4882a593Smuzhiyun .pad = &mt9v111_pad_ops,
987*4882a593Smuzhiyun };
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
990*4882a593Smuzhiyun static const struct media_entity_operations mt9v111_subdev_entity_ops = {
991*4882a593Smuzhiyun .link_validate = v4l2_subdev_link_validate,
992*4882a593Smuzhiyun };
993*4882a593Smuzhiyun #endif
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun /* --- V4L2 ctrl --- */
mt9v111_s_ctrl(struct v4l2_ctrl * ctrl)996*4882a593Smuzhiyun static int mt9v111_s_ctrl(struct v4l2_ctrl *ctrl)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = container_of(ctrl->handler,
999*4882a593Smuzhiyun struct mt9v111_dev,
1000*4882a593Smuzhiyun ctrls);
1001*4882a593Smuzhiyun int ret;
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun mutex_lock(&mt9v111->pwr_mutex);
1004*4882a593Smuzhiyun /*
1005*4882a593Smuzhiyun * If sensor is powered down, just cache new control values,
1006*4882a593Smuzhiyun * no actual register access.
1007*4882a593Smuzhiyun */
1008*4882a593Smuzhiyun if (!mt9v111->pwr_count) {
1009*4882a593Smuzhiyun mt9v111->pending = true;
1010*4882a593Smuzhiyun mutex_unlock(&mt9v111->pwr_mutex);
1011*4882a593Smuzhiyun return 0;
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun mutex_unlock(&mt9v111->pwr_mutex);
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /*
1016*4882a593Smuzhiyun * Flickering control gets disabled if both auto exp and auto awb
1017*4882a593Smuzhiyun * are disabled too. If any of the two is enabled, enable it.
1018*4882a593Smuzhiyun *
1019*4882a593Smuzhiyun * Disabling flickering when ae and awb are off allows a more precise
1020*4882a593Smuzhiyun * control of the programmed frame rate.
1021*4882a593Smuzhiyun */
1022*4882a593Smuzhiyun if (mt9v111->auto_exp->is_new || mt9v111->auto_awb->is_new) {
1023*4882a593Smuzhiyun if (mt9v111->auto_exp->val == V4L2_EXPOSURE_MANUAL &&
1024*4882a593Smuzhiyun mt9v111->auto_awb->val == V4L2_WHITE_BALANCE_MANUAL)
1025*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
1026*4882a593Smuzhiyun MT9V111_IFP_R08_OUTFMT_CTRL,
1027*4882a593Smuzhiyun MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
1028*4882a593Smuzhiyun 0);
1029*4882a593Smuzhiyun else
1030*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
1031*4882a593Smuzhiyun MT9V111_IFP_R08_OUTFMT_CTRL,
1032*4882a593Smuzhiyun MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
1033*4882a593Smuzhiyun 1);
1034*4882a593Smuzhiyun if (ret)
1035*4882a593Smuzhiyun return ret;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun ret = -EINVAL;
1039*4882a593Smuzhiyun switch (ctrl->id) {
1040*4882a593Smuzhiyun case V4L2_CID_AUTO_WHITE_BALANCE:
1041*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
1042*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL,
1043*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN,
1044*4882a593Smuzhiyun ctrl->val == V4L2_WHITE_BALANCE_AUTO ?
1045*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN : 0);
1046*4882a593Smuzhiyun break;
1047*4882a593Smuzhiyun case V4L2_CID_EXPOSURE_AUTO:
1048*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
1049*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL,
1050*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL_AE_EN,
1051*4882a593Smuzhiyun ctrl->val == V4L2_EXPOSURE_AUTO ?
1052*4882a593Smuzhiyun MT9V111_IFP_R06_OPMODE_CTRL_AE_EN : 0);
1053*4882a593Smuzhiyun break;
1054*4882a593Smuzhiyun case V4L2_CID_HBLANK:
1055*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
1056*4882a593Smuzhiyun MT9V111_CORE_R05_HBLANK,
1057*4882a593Smuzhiyun MT9V111_CORE_R05_MAX_HBLANK,
1058*4882a593Smuzhiyun mt9v111->hblank->val);
1059*4882a593Smuzhiyun break;
1060*4882a593Smuzhiyun case V4L2_CID_VBLANK:
1061*4882a593Smuzhiyun ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
1062*4882a593Smuzhiyun MT9V111_CORE_R06_VBLANK,
1063*4882a593Smuzhiyun MT9V111_CORE_R06_MAX_VBLANK,
1064*4882a593Smuzhiyun mt9v111->vblank->val);
1065*4882a593Smuzhiyun break;
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun return ret;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun static const struct v4l2_ctrl_ops mt9v111_ctrl_ops = {
1072*4882a593Smuzhiyun .s_ctrl = mt9v111_s_ctrl,
1073*4882a593Smuzhiyun };
1074*4882a593Smuzhiyun
mt9v111_chip_probe(struct mt9v111_dev * mt9v111)1075*4882a593Smuzhiyun static int mt9v111_chip_probe(struct mt9v111_dev *mt9v111)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun int ret;
1078*4882a593Smuzhiyun u16 val;
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun ret = __mt9v111_power_on(&mt9v111->sd);
1081*4882a593Smuzhiyun if (ret)
1082*4882a593Smuzhiyun return ret;
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun ret = mt9v111_read(mt9v111->client, MT9V111_R01_CORE,
1085*4882a593Smuzhiyun MT9V111_CORE_RFF_CHIP_VER, &val);
1086*4882a593Smuzhiyun if (ret)
1087*4882a593Smuzhiyun goto power_off;
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun if ((val >> 8) != MT9V111_CHIP_ID_HIGH &&
1090*4882a593Smuzhiyun (val & 0xff) != MT9V111_CHIP_ID_LOW) {
1091*4882a593Smuzhiyun dev_err(mt9v111->dev,
1092*4882a593Smuzhiyun "Unable to identify MT9V111 chip: 0x%2x%2x\n",
1093*4882a593Smuzhiyun val >> 8, val & 0xff);
1094*4882a593Smuzhiyun ret = -EIO;
1095*4882a593Smuzhiyun goto power_off;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun dev_dbg(mt9v111->dev, "Chip identified: 0x%2x%2x\n",
1099*4882a593Smuzhiyun val >> 8, val & 0xff);
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun power_off:
1102*4882a593Smuzhiyun __mt9v111_power_off(&mt9v111->sd);
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun return ret;
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun
mt9v111_probe(struct i2c_client * client)1107*4882a593Smuzhiyun static int mt9v111_probe(struct i2c_client *client)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun struct mt9v111_dev *mt9v111;
1110*4882a593Smuzhiyun struct v4l2_fract tpf;
1111*4882a593Smuzhiyun int ret;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun mt9v111 = devm_kzalloc(&client->dev, sizeof(*mt9v111), GFP_KERNEL);
1114*4882a593Smuzhiyun if (!mt9v111)
1115*4882a593Smuzhiyun return -ENOMEM;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun mt9v111->dev = &client->dev;
1118*4882a593Smuzhiyun mt9v111->client = client;
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun mt9v111->clk = devm_clk_get(&client->dev, NULL);
1121*4882a593Smuzhiyun if (IS_ERR(mt9v111->clk))
1122*4882a593Smuzhiyun return PTR_ERR(mt9v111->clk);
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun mt9v111->sysclk = clk_get_rate(mt9v111->clk);
1125*4882a593Smuzhiyun if (mt9v111->sysclk > MT9V111_MAX_CLKIN)
1126*4882a593Smuzhiyun return -EINVAL;
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun mt9v111->oe = devm_gpiod_get_optional(&client->dev, "enable",
1129*4882a593Smuzhiyun GPIOD_OUT_LOW);
1130*4882a593Smuzhiyun if (IS_ERR(mt9v111->oe)) {
1131*4882a593Smuzhiyun dev_err(&client->dev, "Unable to get GPIO \"enable\": %ld\n",
1132*4882a593Smuzhiyun PTR_ERR(mt9v111->oe));
1133*4882a593Smuzhiyun return PTR_ERR(mt9v111->oe);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun mt9v111->standby = devm_gpiod_get_optional(&client->dev, "standby",
1137*4882a593Smuzhiyun GPIOD_OUT_HIGH);
1138*4882a593Smuzhiyun if (IS_ERR(mt9v111->standby)) {
1139*4882a593Smuzhiyun dev_err(&client->dev, "Unable to get GPIO \"standby\": %ld\n",
1140*4882a593Smuzhiyun PTR_ERR(mt9v111->standby));
1141*4882a593Smuzhiyun return PTR_ERR(mt9v111->standby);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun mt9v111->reset = devm_gpiod_get_optional(&client->dev, "reset",
1145*4882a593Smuzhiyun GPIOD_OUT_LOW);
1146*4882a593Smuzhiyun if (IS_ERR(mt9v111->reset)) {
1147*4882a593Smuzhiyun dev_err(&client->dev, "Unable to get GPIO \"reset\": %ld\n",
1148*4882a593Smuzhiyun PTR_ERR(mt9v111->reset));
1149*4882a593Smuzhiyun return PTR_ERR(mt9v111->reset);
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun mutex_init(&mt9v111->pwr_mutex);
1153*4882a593Smuzhiyun mutex_init(&mt9v111->stream_mutex);
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun v4l2_ctrl_handler_init(&mt9v111->ctrls, 5);
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun mt9v111->auto_awb = v4l2_ctrl_new_std(&mt9v111->ctrls,
1158*4882a593Smuzhiyun &mt9v111_ctrl_ops,
1159*4882a593Smuzhiyun V4L2_CID_AUTO_WHITE_BALANCE,
1160*4882a593Smuzhiyun 0, 1, 1,
1161*4882a593Smuzhiyun V4L2_WHITE_BALANCE_AUTO);
1162*4882a593Smuzhiyun mt9v111->auto_exp = v4l2_ctrl_new_std_menu(&mt9v111->ctrls,
1163*4882a593Smuzhiyun &mt9v111_ctrl_ops,
1164*4882a593Smuzhiyun V4L2_CID_EXPOSURE_AUTO,
1165*4882a593Smuzhiyun V4L2_EXPOSURE_MANUAL,
1166*4882a593Smuzhiyun 0, V4L2_EXPOSURE_AUTO);
1167*4882a593Smuzhiyun mt9v111->hblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
1168*4882a593Smuzhiyun V4L2_CID_HBLANK,
1169*4882a593Smuzhiyun MT9V111_CORE_R05_MIN_HBLANK,
1170*4882a593Smuzhiyun MT9V111_CORE_R05_MAX_HBLANK, 1,
1171*4882a593Smuzhiyun MT9V111_CORE_R05_DEF_HBLANK);
1172*4882a593Smuzhiyun mt9v111->vblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
1173*4882a593Smuzhiyun V4L2_CID_VBLANK,
1174*4882a593Smuzhiyun MT9V111_CORE_R06_MIN_VBLANK,
1175*4882a593Smuzhiyun MT9V111_CORE_R06_MAX_VBLANK, 1,
1176*4882a593Smuzhiyun MT9V111_CORE_R06_DEF_VBLANK);
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun /* PIXEL_RATE is fixed: just expose it to user space. */
1179*4882a593Smuzhiyun v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
1180*4882a593Smuzhiyun V4L2_CID_PIXEL_RATE, 0,
1181*4882a593Smuzhiyun DIV_ROUND_CLOSEST(mt9v111->sysclk, 2), 1,
1182*4882a593Smuzhiyun DIV_ROUND_CLOSEST(mt9v111->sysclk, 2));
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun if (mt9v111->ctrls.error) {
1185*4882a593Smuzhiyun ret = mt9v111->ctrls.error;
1186*4882a593Smuzhiyun goto error_free_ctrls;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun mt9v111->sd.ctrl_handler = &mt9v111->ctrls;
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun /* Start with default configuration: 640x480 UYVY. */
1191*4882a593Smuzhiyun mt9v111->fmt = mt9v111_def_fmt;
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun /* Re-calculate blankings for 640x480@15fps. */
1194*4882a593Smuzhiyun mt9v111->fps = 15;
1195*4882a593Smuzhiyun tpf.numerator = 1;
1196*4882a593Smuzhiyun tpf.denominator = mt9v111->fps;
1197*4882a593Smuzhiyun mt9v111_calc_frame_rate(mt9v111, &tpf);
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun mt9v111->pwr_count = 0;
1200*4882a593Smuzhiyun mt9v111->addr_space = MT9V111_R01_IFP;
1201*4882a593Smuzhiyun mt9v111->pending = true;
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun v4l2_i2c_subdev_init(&mt9v111->sd, client, &mt9v111_ops);
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1206*4882a593Smuzhiyun mt9v111->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1207*4882a593Smuzhiyun mt9v111->sd.entity.ops = &mt9v111_subdev_entity_ops;
1208*4882a593Smuzhiyun mt9v111->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun mt9v111->pad.flags = MEDIA_PAD_FL_SOURCE;
1211*4882a593Smuzhiyun ret = media_entity_pads_init(&mt9v111->sd.entity, 1, &mt9v111->pad);
1212*4882a593Smuzhiyun if (ret)
1213*4882a593Smuzhiyun goto error_free_entity;
1214*4882a593Smuzhiyun #endif
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun ret = mt9v111_chip_probe(mt9v111);
1217*4882a593Smuzhiyun if (ret)
1218*4882a593Smuzhiyun goto error_free_entity;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun ret = v4l2_async_register_subdev(&mt9v111->sd);
1221*4882a593Smuzhiyun if (ret)
1222*4882a593Smuzhiyun goto error_free_entity;
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun return 0;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun error_free_entity:
1227*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1228*4882a593Smuzhiyun media_entity_cleanup(&mt9v111->sd.entity);
1229*4882a593Smuzhiyun #endif
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun error_free_ctrls:
1232*4882a593Smuzhiyun v4l2_ctrl_handler_free(&mt9v111->ctrls);
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun mutex_destroy(&mt9v111->pwr_mutex);
1235*4882a593Smuzhiyun mutex_destroy(&mt9v111->stream_mutex);
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun return ret;
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun
mt9v111_remove(struct i2c_client * client)1240*4882a593Smuzhiyun static int mt9v111_remove(struct i2c_client *client)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun struct v4l2_subdev *sd = i2c_get_clientdata(client);
1243*4882a593Smuzhiyun struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun v4l2_async_unregister_subdev(sd);
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MEDIA_CONTROLLER)
1248*4882a593Smuzhiyun media_entity_cleanup(&sd->entity);
1249*4882a593Smuzhiyun #endif
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun v4l2_ctrl_handler_free(&mt9v111->ctrls);
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun mutex_destroy(&mt9v111->pwr_mutex);
1254*4882a593Smuzhiyun mutex_destroy(&mt9v111->stream_mutex);
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun devm_gpiod_put(mt9v111->dev, mt9v111->oe);
1257*4882a593Smuzhiyun devm_gpiod_put(mt9v111->dev, mt9v111->standby);
1258*4882a593Smuzhiyun devm_gpiod_put(mt9v111->dev, mt9v111->reset);
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun devm_clk_put(mt9v111->dev, mt9v111->clk);
1261*4882a593Smuzhiyun
1262*4882a593Smuzhiyun return 0;
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun static const struct of_device_id mt9v111_of_match[] = {
1266*4882a593Smuzhiyun { .compatible = "aptina,mt9v111", },
1267*4882a593Smuzhiyun { /* sentinel */ },
1268*4882a593Smuzhiyun };
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun static struct i2c_driver mt9v111_driver = {
1271*4882a593Smuzhiyun .driver = {
1272*4882a593Smuzhiyun .name = "mt9v111",
1273*4882a593Smuzhiyun .of_match_table = mt9v111_of_match,
1274*4882a593Smuzhiyun },
1275*4882a593Smuzhiyun .probe_new = mt9v111_probe,
1276*4882a593Smuzhiyun .remove = mt9v111_remove,
1277*4882a593Smuzhiyun };
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun module_i2c_driver(mt9v111_driver);
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun MODULE_DESCRIPTION("V4L2 sensor driver for Aptina MT9V111");
1282*4882a593Smuzhiyun MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
1283*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1284