xref: /linux/drivers/media/i2c/mt9p031.c (revision 5266c98b15e682694e4d79590d39416ad3bde687)
1cb7a01acSMauro Carvalho Chehab /*
2cb7a01acSMauro Carvalho Chehab  * Driver for MT9P031 CMOS Image Sensor from Aptina
3cb7a01acSMauro Carvalho Chehab  *
4cb7a01acSMauro Carvalho Chehab  * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
5cb7a01acSMauro Carvalho Chehab  * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
6cb7a01acSMauro Carvalho Chehab  * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
7cb7a01acSMauro Carvalho Chehab  *
8cb7a01acSMauro Carvalho Chehab  * Based on the MT9V032 driver and Bastian Hecht's code.
9cb7a01acSMauro Carvalho Chehab  *
10cb7a01acSMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
11cb7a01acSMauro Carvalho Chehab  * it under the terms of the GNU General Public License version 2 as
12cb7a01acSMauro Carvalho Chehab  * published by the Free Software Foundation.
13cb7a01acSMauro Carvalho Chehab  */
14cb7a01acSMauro Carvalho Chehab 
15cb7a01acSMauro Carvalho Chehab #include <linux/delay.h>
16cb7a01acSMauro Carvalho Chehab #include <linux/device.h>
17cb7a01acSMauro Carvalho Chehab #include <linux/gpio.h>
18cb7a01acSMauro Carvalho Chehab #include <linux/module.h>
19cb7a01acSMauro Carvalho Chehab #include <linux/i2c.h>
20cb7a01acSMauro Carvalho Chehab #include <linux/log2.h>
21cb7a01acSMauro Carvalho Chehab #include <linux/pm.h>
22cb7a01acSMauro Carvalho Chehab #include <linux/slab.h>
23cb7a01acSMauro Carvalho Chehab #include <linux/videodev2.h>
24cb7a01acSMauro Carvalho Chehab 
25cb7a01acSMauro Carvalho Chehab #include <media/mt9p031.h>
26cb7a01acSMauro Carvalho Chehab #include <media/v4l2-chip-ident.h>
27cb7a01acSMauro Carvalho Chehab #include <media/v4l2-ctrls.h>
28cb7a01acSMauro Carvalho Chehab #include <media/v4l2-device.h>
29cb7a01acSMauro Carvalho Chehab #include <media/v4l2-subdev.h>
30cb7a01acSMauro Carvalho Chehab 
31cb7a01acSMauro Carvalho Chehab #include "aptina-pll.h"
32cb7a01acSMauro Carvalho Chehab 
33cb7a01acSMauro Carvalho Chehab #define MT9P031_PIXEL_ARRAY_WIDTH			2752
34cb7a01acSMauro Carvalho Chehab #define MT9P031_PIXEL_ARRAY_HEIGHT			2004
35cb7a01acSMauro Carvalho Chehab 
36cb7a01acSMauro Carvalho Chehab #define MT9P031_CHIP_VERSION				0x00
37cb7a01acSMauro Carvalho Chehab #define		MT9P031_CHIP_VERSION_VALUE		0x1801
38cb7a01acSMauro Carvalho Chehab #define MT9P031_ROW_START				0x01
39cb7a01acSMauro Carvalho Chehab #define		MT9P031_ROW_START_MIN			0
40cb7a01acSMauro Carvalho Chehab #define		MT9P031_ROW_START_MAX			2004
41cb7a01acSMauro Carvalho Chehab #define		MT9P031_ROW_START_DEF			54
42cb7a01acSMauro Carvalho Chehab #define MT9P031_COLUMN_START				0x02
43cb7a01acSMauro Carvalho Chehab #define		MT9P031_COLUMN_START_MIN		0
44cb7a01acSMauro Carvalho Chehab #define		MT9P031_COLUMN_START_MAX		2750
45cb7a01acSMauro Carvalho Chehab #define		MT9P031_COLUMN_START_DEF		16
46cb7a01acSMauro Carvalho Chehab #define MT9P031_WINDOW_HEIGHT				0x03
47cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_HEIGHT_MIN		2
48cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_HEIGHT_MAX		2006
49cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_HEIGHT_DEF		1944
50cb7a01acSMauro Carvalho Chehab #define MT9P031_WINDOW_WIDTH				0x04
51cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_WIDTH_MIN		2
52cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_WIDTH_MAX		2752
53cb7a01acSMauro Carvalho Chehab #define		MT9P031_WINDOW_WIDTH_DEF		2592
54cb7a01acSMauro Carvalho Chehab #define MT9P031_HORIZONTAL_BLANK			0x05
55cb7a01acSMauro Carvalho Chehab #define		MT9P031_HORIZONTAL_BLANK_MIN		0
56cb7a01acSMauro Carvalho Chehab #define		MT9P031_HORIZONTAL_BLANK_MAX		4095
57cb7a01acSMauro Carvalho Chehab #define MT9P031_VERTICAL_BLANK				0x06
58*5266c98bSLaurent Pinchart #define		MT9P031_VERTICAL_BLANK_MIN		1
59*5266c98bSLaurent Pinchart #define		MT9P031_VERTICAL_BLANK_MAX		4096
60*5266c98bSLaurent Pinchart #define		MT9P031_VERTICAL_BLANK_DEF		26
61cb7a01acSMauro Carvalho Chehab #define MT9P031_OUTPUT_CONTROL				0x07
62cb7a01acSMauro Carvalho Chehab #define		MT9P031_OUTPUT_CONTROL_CEN		2
63cb7a01acSMauro Carvalho Chehab #define		MT9P031_OUTPUT_CONTROL_SYN		1
64cb7a01acSMauro Carvalho Chehab #define		MT9P031_OUTPUT_CONTROL_DEF		0x1f82
65cb7a01acSMauro Carvalho Chehab #define MT9P031_SHUTTER_WIDTH_UPPER			0x08
66cb7a01acSMauro Carvalho Chehab #define MT9P031_SHUTTER_WIDTH_LOWER			0x09
67cb7a01acSMauro Carvalho Chehab #define		MT9P031_SHUTTER_WIDTH_MIN		1
68cb7a01acSMauro Carvalho Chehab #define		MT9P031_SHUTTER_WIDTH_MAX		1048575
69cb7a01acSMauro Carvalho Chehab #define		MT9P031_SHUTTER_WIDTH_DEF		1943
70cb7a01acSMauro Carvalho Chehab #define	MT9P031_PLL_CONTROL				0x10
71cb7a01acSMauro Carvalho Chehab #define		MT9P031_PLL_CONTROL_PWROFF		0x0050
72cb7a01acSMauro Carvalho Chehab #define		MT9P031_PLL_CONTROL_PWRON		0x0051
73cb7a01acSMauro Carvalho Chehab #define		MT9P031_PLL_CONTROL_USEPLL		0x0052
74cb7a01acSMauro Carvalho Chehab #define	MT9P031_PLL_CONFIG_1				0x11
75cb7a01acSMauro Carvalho Chehab #define	MT9P031_PLL_CONFIG_2				0x12
76cb7a01acSMauro Carvalho Chehab #define MT9P031_PIXEL_CLOCK_CONTROL			0x0a
77cb7a01acSMauro Carvalho Chehab #define MT9P031_FRAME_RESTART				0x0b
78cb7a01acSMauro Carvalho Chehab #define MT9P031_SHUTTER_DELAY				0x0c
79cb7a01acSMauro Carvalho Chehab #define MT9P031_RST					0x0d
80cb7a01acSMauro Carvalho Chehab #define		MT9P031_RST_ENABLE			1
81cb7a01acSMauro Carvalho Chehab #define		MT9P031_RST_DISABLE			0
82cb7a01acSMauro Carvalho Chehab #define MT9P031_READ_MODE_1				0x1e
83cb7a01acSMauro Carvalho Chehab #define MT9P031_READ_MODE_2				0x20
84cb7a01acSMauro Carvalho Chehab #define		MT9P031_READ_MODE_2_ROW_MIR		(1 << 15)
85cb7a01acSMauro Carvalho Chehab #define		MT9P031_READ_MODE_2_COL_MIR		(1 << 14)
86cb7a01acSMauro Carvalho Chehab #define		MT9P031_READ_MODE_2_ROW_BLC		(1 << 6)
87cb7a01acSMauro Carvalho Chehab #define MT9P031_ROW_ADDRESS_MODE			0x22
88cb7a01acSMauro Carvalho Chehab #define MT9P031_COLUMN_ADDRESS_MODE			0x23
89cb7a01acSMauro Carvalho Chehab #define MT9P031_GLOBAL_GAIN				0x35
90cb7a01acSMauro Carvalho Chehab #define		MT9P031_GLOBAL_GAIN_MIN			8
91cb7a01acSMauro Carvalho Chehab #define		MT9P031_GLOBAL_GAIN_MAX			1024
92cb7a01acSMauro Carvalho Chehab #define		MT9P031_GLOBAL_GAIN_DEF			8
93cb7a01acSMauro Carvalho Chehab #define		MT9P031_GLOBAL_GAIN_MULT		(1 << 6)
94cb7a01acSMauro Carvalho Chehab #define MT9P031_ROW_BLACK_TARGET			0x49
95cb7a01acSMauro Carvalho Chehab #define MT9P031_ROW_BLACK_DEF_OFFSET			0x4b
96cb7a01acSMauro Carvalho Chehab #define MT9P031_GREEN1_OFFSET				0x60
97cb7a01acSMauro Carvalho Chehab #define MT9P031_GREEN2_OFFSET				0x61
98cb7a01acSMauro Carvalho Chehab #define MT9P031_BLACK_LEVEL_CALIBRATION			0x62
99cb7a01acSMauro Carvalho Chehab #define		MT9P031_BLC_MANUAL_BLC			(1 << 0)
100cb7a01acSMauro Carvalho Chehab #define MT9P031_RED_OFFSET				0x63
101cb7a01acSMauro Carvalho Chehab #define MT9P031_BLUE_OFFSET				0x64
102cb7a01acSMauro Carvalho Chehab #define MT9P031_TEST_PATTERN				0xa0
103cb7a01acSMauro Carvalho Chehab #define		MT9P031_TEST_PATTERN_SHIFT		3
104cb7a01acSMauro Carvalho Chehab #define		MT9P031_TEST_PATTERN_ENABLE		(1 << 0)
105cb7a01acSMauro Carvalho Chehab #define		MT9P031_TEST_PATTERN_DISABLE		(0 << 0)
106cb7a01acSMauro Carvalho Chehab #define MT9P031_TEST_PATTERN_GREEN			0xa1
107cb7a01acSMauro Carvalho Chehab #define MT9P031_TEST_PATTERN_RED			0xa2
108cb7a01acSMauro Carvalho Chehab #define MT9P031_TEST_PATTERN_BLUE			0xa3
109cb7a01acSMauro Carvalho Chehab 
110cb7a01acSMauro Carvalho Chehab enum mt9p031_model {
111cb7a01acSMauro Carvalho Chehab 	MT9P031_MODEL_COLOR,
112cb7a01acSMauro Carvalho Chehab 	MT9P031_MODEL_MONOCHROME,
113cb7a01acSMauro Carvalho Chehab };
114cb7a01acSMauro Carvalho Chehab 
115cb7a01acSMauro Carvalho Chehab struct mt9p031 {
116cb7a01acSMauro Carvalho Chehab 	struct v4l2_subdev subdev;
117cb7a01acSMauro Carvalho Chehab 	struct media_pad pad;
118cb7a01acSMauro Carvalho Chehab 	struct v4l2_rect crop;  /* Sensor window */
119cb7a01acSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt format;
120cb7a01acSMauro Carvalho Chehab 	struct mt9p031_platform_data *pdata;
121cb7a01acSMauro Carvalho Chehab 	struct mutex power_lock; /* lock to protect power_count */
122cb7a01acSMauro Carvalho Chehab 	int power_count;
123cb7a01acSMauro Carvalho Chehab 
124cb7a01acSMauro Carvalho Chehab 	enum mt9p031_model model;
125cb7a01acSMauro Carvalho Chehab 	struct aptina_pll pll;
126cb7a01acSMauro Carvalho Chehab 	int reset;
127cb7a01acSMauro Carvalho Chehab 
128cb7a01acSMauro Carvalho Chehab 	struct v4l2_ctrl_handler ctrls;
129cb7a01acSMauro Carvalho Chehab 	struct v4l2_ctrl *blc_auto;
130cb7a01acSMauro Carvalho Chehab 	struct v4l2_ctrl *blc_offset;
131cb7a01acSMauro Carvalho Chehab 
132cb7a01acSMauro Carvalho Chehab 	/* Registers cache */
133cb7a01acSMauro Carvalho Chehab 	u16 output_control;
134cb7a01acSMauro Carvalho Chehab 	u16 mode2;
135cb7a01acSMauro Carvalho Chehab };
136cb7a01acSMauro Carvalho Chehab 
137cb7a01acSMauro Carvalho Chehab static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd)
138cb7a01acSMauro Carvalho Chehab {
139cb7a01acSMauro Carvalho Chehab 	return container_of(sd, struct mt9p031, subdev);
140cb7a01acSMauro Carvalho Chehab }
141cb7a01acSMauro Carvalho Chehab 
142cb7a01acSMauro Carvalho Chehab static int mt9p031_read(struct i2c_client *client, u8 reg)
143cb7a01acSMauro Carvalho Chehab {
144cb7a01acSMauro Carvalho Chehab 	return i2c_smbus_read_word_swapped(client, reg);
145cb7a01acSMauro Carvalho Chehab }
146cb7a01acSMauro Carvalho Chehab 
147cb7a01acSMauro Carvalho Chehab static int mt9p031_write(struct i2c_client *client, u8 reg, u16 data)
148cb7a01acSMauro Carvalho Chehab {
149cb7a01acSMauro Carvalho Chehab 	return i2c_smbus_write_word_swapped(client, reg, data);
150cb7a01acSMauro Carvalho Chehab }
151cb7a01acSMauro Carvalho Chehab 
152cb7a01acSMauro Carvalho Chehab static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear,
153cb7a01acSMauro Carvalho Chehab 				      u16 set)
154cb7a01acSMauro Carvalho Chehab {
155cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
156cb7a01acSMauro Carvalho Chehab 	u16 value = (mt9p031->output_control & ~clear) | set;
157cb7a01acSMauro Carvalho Chehab 	int ret;
158cb7a01acSMauro Carvalho Chehab 
159cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value);
160cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
161cb7a01acSMauro Carvalho Chehab 		return ret;
162cb7a01acSMauro Carvalho Chehab 
163cb7a01acSMauro Carvalho Chehab 	mt9p031->output_control = value;
164cb7a01acSMauro Carvalho Chehab 	return 0;
165cb7a01acSMauro Carvalho Chehab }
166cb7a01acSMauro Carvalho Chehab 
167cb7a01acSMauro Carvalho Chehab static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set)
168cb7a01acSMauro Carvalho Chehab {
169cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
170cb7a01acSMauro Carvalho Chehab 	u16 value = (mt9p031->mode2 & ~clear) | set;
171cb7a01acSMauro Carvalho Chehab 	int ret;
172cb7a01acSMauro Carvalho Chehab 
173cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_READ_MODE_2, value);
174cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
175cb7a01acSMauro Carvalho Chehab 		return ret;
176cb7a01acSMauro Carvalho Chehab 
177cb7a01acSMauro Carvalho Chehab 	mt9p031->mode2 = value;
178cb7a01acSMauro Carvalho Chehab 	return 0;
179cb7a01acSMauro Carvalho Chehab }
180cb7a01acSMauro Carvalho Chehab 
181cb7a01acSMauro Carvalho Chehab static int mt9p031_reset(struct mt9p031 *mt9p031)
182cb7a01acSMauro Carvalho Chehab {
183cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
184cb7a01acSMauro Carvalho Chehab 	int ret;
185cb7a01acSMauro Carvalho Chehab 
186cb7a01acSMauro Carvalho Chehab 	/* Disable chip output, synchronous option update */
187cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE);
188cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
189cb7a01acSMauro Carvalho Chehab 		return ret;
190cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE);
191cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
192cb7a01acSMauro Carvalho Chehab 		return ret;
193cb7a01acSMauro Carvalho Chehab 
194cb7a01acSMauro Carvalho Chehab 	return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN,
195cb7a01acSMauro Carvalho Chehab 					  0);
196cb7a01acSMauro Carvalho Chehab }
197cb7a01acSMauro Carvalho Chehab 
198cb7a01acSMauro Carvalho Chehab static int mt9p031_pll_setup(struct mt9p031 *mt9p031)
199cb7a01acSMauro Carvalho Chehab {
200cb7a01acSMauro Carvalho Chehab 	static const struct aptina_pll_limits limits = {
201cb7a01acSMauro Carvalho Chehab 		.ext_clock_min = 6000000,
202cb7a01acSMauro Carvalho Chehab 		.ext_clock_max = 27000000,
203cb7a01acSMauro Carvalho Chehab 		.int_clock_min = 2000000,
204cb7a01acSMauro Carvalho Chehab 		.int_clock_max = 13500000,
205cb7a01acSMauro Carvalho Chehab 		.out_clock_min = 180000000,
206cb7a01acSMauro Carvalho Chehab 		.out_clock_max = 360000000,
207cb7a01acSMauro Carvalho Chehab 		.pix_clock_max = 96000000,
208cb7a01acSMauro Carvalho Chehab 		.n_min = 1,
209cb7a01acSMauro Carvalho Chehab 		.n_max = 64,
210cb7a01acSMauro Carvalho Chehab 		.m_min = 16,
211cb7a01acSMauro Carvalho Chehab 		.m_max = 255,
212cb7a01acSMauro Carvalho Chehab 		.p1_min = 1,
213cb7a01acSMauro Carvalho Chehab 		.p1_max = 128,
214cb7a01acSMauro Carvalho Chehab 	};
215cb7a01acSMauro Carvalho Chehab 
216cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
217cb7a01acSMauro Carvalho Chehab 	struct mt9p031_platform_data *pdata = mt9p031->pdata;
218cb7a01acSMauro Carvalho Chehab 
219cb7a01acSMauro Carvalho Chehab 	mt9p031->pll.ext_clock = pdata->ext_freq;
220cb7a01acSMauro Carvalho Chehab 	mt9p031->pll.pix_clock = pdata->target_freq;
221cb7a01acSMauro Carvalho Chehab 
222cb7a01acSMauro Carvalho Chehab 	return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll);
223cb7a01acSMauro Carvalho Chehab }
224cb7a01acSMauro Carvalho Chehab 
225cb7a01acSMauro Carvalho Chehab static int mt9p031_pll_enable(struct mt9p031 *mt9p031)
226cb7a01acSMauro Carvalho Chehab {
227cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
228cb7a01acSMauro Carvalho Chehab 	int ret;
229cb7a01acSMauro Carvalho Chehab 
230cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
231cb7a01acSMauro Carvalho Chehab 			    MT9P031_PLL_CONTROL_PWRON);
232cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
233cb7a01acSMauro Carvalho Chehab 		return ret;
234cb7a01acSMauro Carvalho Chehab 
235cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1,
236cb7a01acSMauro Carvalho Chehab 			    (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1));
237cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
238cb7a01acSMauro Carvalho Chehab 		return ret;
239cb7a01acSMauro Carvalho Chehab 
240cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1);
241cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
242cb7a01acSMauro Carvalho Chehab 		return ret;
243cb7a01acSMauro Carvalho Chehab 
244cb7a01acSMauro Carvalho Chehab 	usleep_range(1000, 2000);
245cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
246cb7a01acSMauro Carvalho Chehab 			    MT9P031_PLL_CONTROL_PWRON |
247cb7a01acSMauro Carvalho Chehab 			    MT9P031_PLL_CONTROL_USEPLL);
248cb7a01acSMauro Carvalho Chehab 	return ret;
249cb7a01acSMauro Carvalho Chehab }
250cb7a01acSMauro Carvalho Chehab 
251cb7a01acSMauro Carvalho Chehab static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031)
252cb7a01acSMauro Carvalho Chehab {
253cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
254cb7a01acSMauro Carvalho Chehab 
255cb7a01acSMauro Carvalho Chehab 	return mt9p031_write(client, MT9P031_PLL_CONTROL,
256cb7a01acSMauro Carvalho Chehab 			     MT9P031_PLL_CONTROL_PWROFF);
257cb7a01acSMauro Carvalho Chehab }
258cb7a01acSMauro Carvalho Chehab 
259cb7a01acSMauro Carvalho Chehab static int mt9p031_power_on(struct mt9p031 *mt9p031)
260cb7a01acSMauro Carvalho Chehab {
261cb7a01acSMauro Carvalho Chehab 	/* Ensure RESET_BAR is low */
262cb7a01acSMauro Carvalho Chehab 	if (mt9p031->reset != -1) {
263cb7a01acSMauro Carvalho Chehab 		gpio_set_value(mt9p031->reset, 0);
264cb7a01acSMauro Carvalho Chehab 		usleep_range(1000, 2000);
265cb7a01acSMauro Carvalho Chehab 	}
266cb7a01acSMauro Carvalho Chehab 
267cb7a01acSMauro Carvalho Chehab 	/* Emable clock */
268cb7a01acSMauro Carvalho Chehab 	if (mt9p031->pdata->set_xclk)
269cb7a01acSMauro Carvalho Chehab 		mt9p031->pdata->set_xclk(&mt9p031->subdev,
270cb7a01acSMauro Carvalho Chehab 					 mt9p031->pdata->ext_freq);
271cb7a01acSMauro Carvalho Chehab 
272cb7a01acSMauro Carvalho Chehab 	/* Now RESET_BAR must be high */
273cb7a01acSMauro Carvalho Chehab 	if (mt9p031->reset != -1) {
274cb7a01acSMauro Carvalho Chehab 		gpio_set_value(mt9p031->reset, 1);
275cb7a01acSMauro Carvalho Chehab 		usleep_range(1000, 2000);
276cb7a01acSMauro Carvalho Chehab 	}
277cb7a01acSMauro Carvalho Chehab 
278cb7a01acSMauro Carvalho Chehab 	return 0;
279cb7a01acSMauro Carvalho Chehab }
280cb7a01acSMauro Carvalho Chehab 
281cb7a01acSMauro Carvalho Chehab static void mt9p031_power_off(struct mt9p031 *mt9p031)
282cb7a01acSMauro Carvalho Chehab {
283cb7a01acSMauro Carvalho Chehab 	if (mt9p031->reset != -1) {
284cb7a01acSMauro Carvalho Chehab 		gpio_set_value(mt9p031->reset, 0);
285cb7a01acSMauro Carvalho Chehab 		usleep_range(1000, 2000);
286cb7a01acSMauro Carvalho Chehab 	}
287cb7a01acSMauro Carvalho Chehab 
288cb7a01acSMauro Carvalho Chehab 	if (mt9p031->pdata->set_xclk)
289cb7a01acSMauro Carvalho Chehab 		mt9p031->pdata->set_xclk(&mt9p031->subdev, 0);
290cb7a01acSMauro Carvalho Chehab }
291cb7a01acSMauro Carvalho Chehab 
292cb7a01acSMauro Carvalho Chehab static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on)
293cb7a01acSMauro Carvalho Chehab {
294cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
295cb7a01acSMauro Carvalho Chehab 	int ret;
296cb7a01acSMauro Carvalho Chehab 
297cb7a01acSMauro Carvalho Chehab 	if (!on) {
298cb7a01acSMauro Carvalho Chehab 		mt9p031_power_off(mt9p031);
299cb7a01acSMauro Carvalho Chehab 		return 0;
300cb7a01acSMauro Carvalho Chehab 	}
301cb7a01acSMauro Carvalho Chehab 
302cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_power_on(mt9p031);
303cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
304cb7a01acSMauro Carvalho Chehab 		return ret;
305cb7a01acSMauro Carvalho Chehab 
306cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_reset(mt9p031);
307cb7a01acSMauro Carvalho Chehab 	if (ret < 0) {
308cb7a01acSMauro Carvalho Chehab 		dev_err(&client->dev, "Failed to reset the camera\n");
309cb7a01acSMauro Carvalho Chehab 		return ret;
310cb7a01acSMauro Carvalho Chehab 	}
311cb7a01acSMauro Carvalho Chehab 
312cb7a01acSMauro Carvalho Chehab 	return v4l2_ctrl_handler_setup(&mt9p031->ctrls);
313cb7a01acSMauro Carvalho Chehab }
314cb7a01acSMauro Carvalho Chehab 
315cb7a01acSMauro Carvalho Chehab /* -----------------------------------------------------------------------------
316cb7a01acSMauro Carvalho Chehab  * V4L2 subdev video operations
317cb7a01acSMauro Carvalho Chehab  */
318cb7a01acSMauro Carvalho Chehab 
319cb7a01acSMauro Carvalho Chehab static int mt9p031_set_params(struct mt9p031 *mt9p031)
320cb7a01acSMauro Carvalho Chehab {
321cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
322cb7a01acSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *format = &mt9p031->format;
323cb7a01acSMauro Carvalho Chehab 	const struct v4l2_rect *crop = &mt9p031->crop;
324cb7a01acSMauro Carvalho Chehab 	unsigned int hblank;
325cb7a01acSMauro Carvalho Chehab 	unsigned int vblank;
326cb7a01acSMauro Carvalho Chehab 	unsigned int xskip;
327cb7a01acSMauro Carvalho Chehab 	unsigned int yskip;
328cb7a01acSMauro Carvalho Chehab 	unsigned int xbin;
329cb7a01acSMauro Carvalho Chehab 	unsigned int ybin;
330cb7a01acSMauro Carvalho Chehab 	int ret;
331cb7a01acSMauro Carvalho Chehab 
332cb7a01acSMauro Carvalho Chehab 	/* Windows position and size.
333cb7a01acSMauro Carvalho Chehab 	 *
334cb7a01acSMauro Carvalho Chehab 	 * TODO: Make sure the start coordinates and window size match the
335cb7a01acSMauro Carvalho Chehab 	 * skipping, binning and mirroring (see description of registers 2 and 4
336cb7a01acSMauro Carvalho Chehab 	 * in table 13, and Binning section on page 41).
337cb7a01acSMauro Carvalho Chehab 	 */
338cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left);
339cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
340cb7a01acSMauro Carvalho Chehab 		return ret;
341cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_ROW_START, crop->top);
342cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
343cb7a01acSMauro Carvalho Chehab 		return ret;
344cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1);
345cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
346cb7a01acSMauro Carvalho Chehab 		return ret;
347cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1);
348cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
349cb7a01acSMauro Carvalho Chehab 		return ret;
350cb7a01acSMauro Carvalho Chehab 
351cb7a01acSMauro Carvalho Chehab 	/* Row and column binning and skipping. Use the maximum binning value
352cb7a01acSMauro Carvalho Chehab 	 * compatible with the skipping settings.
353cb7a01acSMauro Carvalho Chehab 	 */
354cb7a01acSMauro Carvalho Chehab 	xskip = DIV_ROUND_CLOSEST(crop->width, format->width);
355cb7a01acSMauro Carvalho Chehab 	yskip = DIV_ROUND_CLOSEST(crop->height, format->height);
356cb7a01acSMauro Carvalho Chehab 	xbin = 1 << (ffs(xskip) - 1);
357cb7a01acSMauro Carvalho Chehab 	ybin = 1 << (ffs(yskip) - 1);
358cb7a01acSMauro Carvalho Chehab 
359cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE,
360cb7a01acSMauro Carvalho Chehab 			    ((xbin - 1) << 4) | (xskip - 1));
361cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
362cb7a01acSMauro Carvalho Chehab 		return ret;
363cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE,
364cb7a01acSMauro Carvalho Chehab 			    ((ybin - 1) << 4) | (yskip - 1));
365cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
366cb7a01acSMauro Carvalho Chehab 		return ret;
367cb7a01acSMauro Carvalho Chehab 
368cb7a01acSMauro Carvalho Chehab 	/* Blanking - use minimum value for horizontal blanking and default
369cb7a01acSMauro Carvalho Chehab 	 * value for vertical blanking.
370cb7a01acSMauro Carvalho Chehab 	 */
371*5266c98bSLaurent Pinchart 	hblank = 346 * ybin + 64 + (80 >> min_t(unsigned int, xbin, 3));
372cb7a01acSMauro Carvalho Chehab 	vblank = MT9P031_VERTICAL_BLANK_DEF;
373cb7a01acSMauro Carvalho Chehab 
374*5266c98bSLaurent Pinchart 	ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1);
375cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
376cb7a01acSMauro Carvalho Chehab 		return ret;
377*5266c98bSLaurent Pinchart 	ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1);
378cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
379cb7a01acSMauro Carvalho Chehab 		return ret;
380cb7a01acSMauro Carvalho Chehab 
381cb7a01acSMauro Carvalho Chehab 	return ret;
382cb7a01acSMauro Carvalho Chehab }
383cb7a01acSMauro Carvalho Chehab 
384cb7a01acSMauro Carvalho Chehab static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
385cb7a01acSMauro Carvalho Chehab {
386cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
387cb7a01acSMauro Carvalho Chehab 	int ret;
388cb7a01acSMauro Carvalho Chehab 
389cb7a01acSMauro Carvalho Chehab 	if (!enable) {
390cb7a01acSMauro Carvalho Chehab 		/* Stop sensor readout */
391cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_set_output_control(mt9p031,
392cb7a01acSMauro Carvalho Chehab 						 MT9P031_OUTPUT_CONTROL_CEN, 0);
393cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
394cb7a01acSMauro Carvalho Chehab 			return ret;
395cb7a01acSMauro Carvalho Chehab 
396cb7a01acSMauro Carvalho Chehab 		return mt9p031_pll_disable(mt9p031);
397cb7a01acSMauro Carvalho Chehab 	}
398cb7a01acSMauro Carvalho Chehab 
399cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_set_params(mt9p031);
400cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
401cb7a01acSMauro Carvalho Chehab 		return ret;
402cb7a01acSMauro Carvalho Chehab 
403cb7a01acSMauro Carvalho Chehab 	/* Switch to master "normal" mode */
404cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_set_output_control(mt9p031, 0,
405cb7a01acSMauro Carvalho Chehab 					 MT9P031_OUTPUT_CONTROL_CEN);
406cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
407cb7a01acSMauro Carvalho Chehab 		return ret;
408cb7a01acSMauro Carvalho Chehab 
409cb7a01acSMauro Carvalho Chehab 	return mt9p031_pll_enable(mt9p031);
410cb7a01acSMauro Carvalho Chehab }
411cb7a01acSMauro Carvalho Chehab 
412cb7a01acSMauro Carvalho Chehab static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
413cb7a01acSMauro Carvalho Chehab 				  struct v4l2_subdev_fh *fh,
414cb7a01acSMauro Carvalho Chehab 				  struct v4l2_subdev_mbus_code_enum *code)
415cb7a01acSMauro Carvalho Chehab {
416cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
417cb7a01acSMauro Carvalho Chehab 
418cb7a01acSMauro Carvalho Chehab 	if (code->pad || code->index)
419cb7a01acSMauro Carvalho Chehab 		return -EINVAL;
420cb7a01acSMauro Carvalho Chehab 
421cb7a01acSMauro Carvalho Chehab 	code->code = mt9p031->format.code;
422cb7a01acSMauro Carvalho Chehab 	return 0;
423cb7a01acSMauro Carvalho Chehab }
424cb7a01acSMauro Carvalho Chehab 
425cb7a01acSMauro Carvalho Chehab static int mt9p031_enum_frame_size(struct v4l2_subdev *subdev,
426cb7a01acSMauro Carvalho Chehab 				   struct v4l2_subdev_fh *fh,
427cb7a01acSMauro Carvalho Chehab 				   struct v4l2_subdev_frame_size_enum *fse)
428cb7a01acSMauro Carvalho Chehab {
429cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
430cb7a01acSMauro Carvalho Chehab 
431cb7a01acSMauro Carvalho Chehab 	if (fse->index >= 8 || fse->code != mt9p031->format.code)
432cb7a01acSMauro Carvalho Chehab 		return -EINVAL;
433cb7a01acSMauro Carvalho Chehab 
434cb7a01acSMauro Carvalho Chehab 	fse->min_width = MT9P031_WINDOW_WIDTH_DEF
435cb7a01acSMauro Carvalho Chehab 		       / min_t(unsigned int, 7, fse->index + 1);
436cb7a01acSMauro Carvalho Chehab 	fse->max_width = fse->min_width;
437cb7a01acSMauro Carvalho Chehab 	fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1);
438cb7a01acSMauro Carvalho Chehab 	fse->max_height = fse->min_height;
439cb7a01acSMauro Carvalho Chehab 
440cb7a01acSMauro Carvalho Chehab 	return 0;
441cb7a01acSMauro Carvalho Chehab }
442cb7a01acSMauro Carvalho Chehab 
443cb7a01acSMauro Carvalho Chehab static struct v4l2_mbus_framefmt *
444cb7a01acSMauro Carvalho Chehab __mt9p031_get_pad_format(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
445cb7a01acSMauro Carvalho Chehab 			 unsigned int pad, u32 which)
446cb7a01acSMauro Carvalho Chehab {
447cb7a01acSMauro Carvalho Chehab 	switch (which) {
448cb7a01acSMauro Carvalho Chehab 	case V4L2_SUBDEV_FORMAT_TRY:
449cb7a01acSMauro Carvalho Chehab 		return v4l2_subdev_get_try_format(fh, pad);
450cb7a01acSMauro Carvalho Chehab 	case V4L2_SUBDEV_FORMAT_ACTIVE:
451cb7a01acSMauro Carvalho Chehab 		return &mt9p031->format;
452cb7a01acSMauro Carvalho Chehab 	default:
453cb7a01acSMauro Carvalho Chehab 		return NULL;
454cb7a01acSMauro Carvalho Chehab 	}
455cb7a01acSMauro Carvalho Chehab }
456cb7a01acSMauro Carvalho Chehab 
457cb7a01acSMauro Carvalho Chehab static struct v4l2_rect *
458cb7a01acSMauro Carvalho Chehab __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
459cb7a01acSMauro Carvalho Chehab 		     unsigned int pad, u32 which)
460cb7a01acSMauro Carvalho Chehab {
461cb7a01acSMauro Carvalho Chehab 	switch (which) {
462cb7a01acSMauro Carvalho Chehab 	case V4L2_SUBDEV_FORMAT_TRY:
463cb7a01acSMauro Carvalho Chehab 		return v4l2_subdev_get_try_crop(fh, pad);
464cb7a01acSMauro Carvalho Chehab 	case V4L2_SUBDEV_FORMAT_ACTIVE:
465cb7a01acSMauro Carvalho Chehab 		return &mt9p031->crop;
466cb7a01acSMauro Carvalho Chehab 	default:
467cb7a01acSMauro Carvalho Chehab 		return NULL;
468cb7a01acSMauro Carvalho Chehab 	}
469cb7a01acSMauro Carvalho Chehab }
470cb7a01acSMauro Carvalho Chehab 
471cb7a01acSMauro Carvalho Chehab static int mt9p031_get_format(struct v4l2_subdev *subdev,
472cb7a01acSMauro Carvalho Chehab 			      struct v4l2_subdev_fh *fh,
473cb7a01acSMauro Carvalho Chehab 			      struct v4l2_subdev_format *fmt)
474cb7a01acSMauro Carvalho Chehab {
475cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
476cb7a01acSMauro Carvalho Chehab 
477cb7a01acSMauro Carvalho Chehab 	fmt->format = *__mt9p031_get_pad_format(mt9p031, fh, fmt->pad,
478cb7a01acSMauro Carvalho Chehab 						fmt->which);
479cb7a01acSMauro Carvalho Chehab 	return 0;
480cb7a01acSMauro Carvalho Chehab }
481cb7a01acSMauro Carvalho Chehab 
482cb7a01acSMauro Carvalho Chehab static int mt9p031_set_format(struct v4l2_subdev *subdev,
483cb7a01acSMauro Carvalho Chehab 			      struct v4l2_subdev_fh *fh,
484cb7a01acSMauro Carvalho Chehab 			      struct v4l2_subdev_format *format)
485cb7a01acSMauro Carvalho Chehab {
486cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
487cb7a01acSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *__format;
488cb7a01acSMauro Carvalho Chehab 	struct v4l2_rect *__crop;
489cb7a01acSMauro Carvalho Chehab 	unsigned int width;
490cb7a01acSMauro Carvalho Chehab 	unsigned int height;
491cb7a01acSMauro Carvalho Chehab 	unsigned int hratio;
492cb7a01acSMauro Carvalho Chehab 	unsigned int vratio;
493cb7a01acSMauro Carvalho Chehab 
494cb7a01acSMauro Carvalho Chehab 	__crop = __mt9p031_get_pad_crop(mt9p031, fh, format->pad,
495cb7a01acSMauro Carvalho Chehab 					format->which);
496cb7a01acSMauro Carvalho Chehab 
497cb7a01acSMauro Carvalho Chehab 	/* Clamp the width and height to avoid dividing by zero. */
498cb7a01acSMauro Carvalho Chehab 	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
499cb7a01acSMauro Carvalho Chehab 			max(__crop->width / 7, MT9P031_WINDOW_WIDTH_MIN),
500cb7a01acSMauro Carvalho Chehab 			__crop->width);
501cb7a01acSMauro Carvalho Chehab 	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
502cb7a01acSMauro Carvalho Chehab 			max(__crop->height / 8, MT9P031_WINDOW_HEIGHT_MIN),
503cb7a01acSMauro Carvalho Chehab 			__crop->height);
504cb7a01acSMauro Carvalho Chehab 
505cb7a01acSMauro Carvalho Chehab 	hratio = DIV_ROUND_CLOSEST(__crop->width, width);
506cb7a01acSMauro Carvalho Chehab 	vratio = DIV_ROUND_CLOSEST(__crop->height, height);
507cb7a01acSMauro Carvalho Chehab 
508cb7a01acSMauro Carvalho Chehab 	__format = __mt9p031_get_pad_format(mt9p031, fh, format->pad,
509cb7a01acSMauro Carvalho Chehab 					    format->which);
510cb7a01acSMauro Carvalho Chehab 	__format->width = __crop->width / hratio;
511cb7a01acSMauro Carvalho Chehab 	__format->height = __crop->height / vratio;
512cb7a01acSMauro Carvalho Chehab 
513cb7a01acSMauro Carvalho Chehab 	format->format = *__format;
514cb7a01acSMauro Carvalho Chehab 
515cb7a01acSMauro Carvalho Chehab 	return 0;
516cb7a01acSMauro Carvalho Chehab }
517cb7a01acSMauro Carvalho Chehab 
518cb7a01acSMauro Carvalho Chehab static int mt9p031_get_crop(struct v4l2_subdev *subdev,
519cb7a01acSMauro Carvalho Chehab 			    struct v4l2_subdev_fh *fh,
520cb7a01acSMauro Carvalho Chehab 			    struct v4l2_subdev_crop *crop)
521cb7a01acSMauro Carvalho Chehab {
522cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
523cb7a01acSMauro Carvalho Chehab 
524cb7a01acSMauro Carvalho Chehab 	crop->rect = *__mt9p031_get_pad_crop(mt9p031, fh, crop->pad,
525cb7a01acSMauro Carvalho Chehab 					     crop->which);
526cb7a01acSMauro Carvalho Chehab 	return 0;
527cb7a01acSMauro Carvalho Chehab }
528cb7a01acSMauro Carvalho Chehab 
529cb7a01acSMauro Carvalho Chehab static int mt9p031_set_crop(struct v4l2_subdev *subdev,
530cb7a01acSMauro Carvalho Chehab 			    struct v4l2_subdev_fh *fh,
531cb7a01acSMauro Carvalho Chehab 			    struct v4l2_subdev_crop *crop)
532cb7a01acSMauro Carvalho Chehab {
533cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
534cb7a01acSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *__format;
535cb7a01acSMauro Carvalho Chehab 	struct v4l2_rect *__crop;
536cb7a01acSMauro Carvalho Chehab 	struct v4l2_rect rect;
537cb7a01acSMauro Carvalho Chehab 
538cb7a01acSMauro Carvalho Chehab 	/* Clamp the crop rectangle boundaries and align them to a multiple of 2
539cb7a01acSMauro Carvalho Chehab 	 * pixels to ensure a GRBG Bayer pattern.
540cb7a01acSMauro Carvalho Chehab 	 */
541cb7a01acSMauro Carvalho Chehab 	rect.left = clamp(ALIGN(crop->rect.left, 2), MT9P031_COLUMN_START_MIN,
542cb7a01acSMauro Carvalho Chehab 			  MT9P031_COLUMN_START_MAX);
543cb7a01acSMauro Carvalho Chehab 	rect.top = clamp(ALIGN(crop->rect.top, 2), MT9P031_ROW_START_MIN,
544cb7a01acSMauro Carvalho Chehab 			 MT9P031_ROW_START_MAX);
545cb7a01acSMauro Carvalho Chehab 	rect.width = clamp(ALIGN(crop->rect.width, 2),
546cb7a01acSMauro Carvalho Chehab 			   MT9P031_WINDOW_WIDTH_MIN,
547cb7a01acSMauro Carvalho Chehab 			   MT9P031_WINDOW_WIDTH_MAX);
548cb7a01acSMauro Carvalho Chehab 	rect.height = clamp(ALIGN(crop->rect.height, 2),
549cb7a01acSMauro Carvalho Chehab 			    MT9P031_WINDOW_HEIGHT_MIN,
550cb7a01acSMauro Carvalho Chehab 			    MT9P031_WINDOW_HEIGHT_MAX);
551cb7a01acSMauro Carvalho Chehab 
552cb7a01acSMauro Carvalho Chehab 	rect.width = min(rect.width, MT9P031_PIXEL_ARRAY_WIDTH - rect.left);
553cb7a01acSMauro Carvalho Chehab 	rect.height = min(rect.height, MT9P031_PIXEL_ARRAY_HEIGHT - rect.top);
554cb7a01acSMauro Carvalho Chehab 
555cb7a01acSMauro Carvalho Chehab 	__crop = __mt9p031_get_pad_crop(mt9p031, fh, crop->pad, crop->which);
556cb7a01acSMauro Carvalho Chehab 
557cb7a01acSMauro Carvalho Chehab 	if (rect.width != __crop->width || rect.height != __crop->height) {
558cb7a01acSMauro Carvalho Chehab 		/* Reset the output image size if the crop rectangle size has
559cb7a01acSMauro Carvalho Chehab 		 * been modified.
560cb7a01acSMauro Carvalho Chehab 		 */
561cb7a01acSMauro Carvalho Chehab 		__format = __mt9p031_get_pad_format(mt9p031, fh, crop->pad,
562cb7a01acSMauro Carvalho Chehab 						    crop->which);
563cb7a01acSMauro Carvalho Chehab 		__format->width = rect.width;
564cb7a01acSMauro Carvalho Chehab 		__format->height = rect.height;
565cb7a01acSMauro Carvalho Chehab 	}
566cb7a01acSMauro Carvalho Chehab 
567cb7a01acSMauro Carvalho Chehab 	*__crop = rect;
568cb7a01acSMauro Carvalho Chehab 	crop->rect = rect;
569cb7a01acSMauro Carvalho Chehab 
570cb7a01acSMauro Carvalho Chehab 	return 0;
571cb7a01acSMauro Carvalho Chehab }
572cb7a01acSMauro Carvalho Chehab 
573cb7a01acSMauro Carvalho Chehab /* -----------------------------------------------------------------------------
574cb7a01acSMauro Carvalho Chehab  * V4L2 subdev control operations
575cb7a01acSMauro Carvalho Chehab  */
576cb7a01acSMauro Carvalho Chehab 
577cb7a01acSMauro Carvalho Chehab #define V4L2_CID_TEST_PATTERN		(V4L2_CID_USER_BASE | 0x1001)
578cb7a01acSMauro Carvalho Chehab #define V4L2_CID_BLC_AUTO		(V4L2_CID_USER_BASE | 0x1002)
579cb7a01acSMauro Carvalho Chehab #define V4L2_CID_BLC_TARGET_LEVEL	(V4L2_CID_USER_BASE | 0x1003)
580cb7a01acSMauro Carvalho Chehab #define V4L2_CID_BLC_ANALOG_OFFSET	(V4L2_CID_USER_BASE | 0x1004)
581cb7a01acSMauro Carvalho Chehab #define V4L2_CID_BLC_DIGITAL_OFFSET	(V4L2_CID_USER_BASE | 0x1005)
582cb7a01acSMauro Carvalho Chehab 
583cb7a01acSMauro Carvalho Chehab static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
584cb7a01acSMauro Carvalho Chehab {
585cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 =
586cb7a01acSMauro Carvalho Chehab 			container_of(ctrl->handler, struct mt9p031, ctrls);
587cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
588cb7a01acSMauro Carvalho Chehab 	u16 data;
589cb7a01acSMauro Carvalho Chehab 	int ret;
590cb7a01acSMauro Carvalho Chehab 
591cb7a01acSMauro Carvalho Chehab 	switch (ctrl->id) {
592cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_EXPOSURE:
593cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER,
594cb7a01acSMauro Carvalho Chehab 				    (ctrl->val >> 16) & 0xffff);
595cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
596cb7a01acSMauro Carvalho Chehab 			return ret;
597cb7a01acSMauro Carvalho Chehab 
598cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER,
599cb7a01acSMauro Carvalho Chehab 				     ctrl->val & 0xffff);
600cb7a01acSMauro Carvalho Chehab 
601cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_GAIN:
602cb7a01acSMauro Carvalho Chehab 		/* Gain is controlled by 2 analog stages and a digital stage.
603cb7a01acSMauro Carvalho Chehab 		 * Valid values for the 3 stages are
604cb7a01acSMauro Carvalho Chehab 		 *
605cb7a01acSMauro Carvalho Chehab 		 * Stage                Min     Max     Step
606cb7a01acSMauro Carvalho Chehab 		 * ------------------------------------------
607cb7a01acSMauro Carvalho Chehab 		 * First analog stage   x1      x2      1
608cb7a01acSMauro Carvalho Chehab 		 * Second analog stage  x1      x4      0.125
609cb7a01acSMauro Carvalho Chehab 		 * Digital stage        x1      x16     0.125
610cb7a01acSMauro Carvalho Chehab 		 *
611cb7a01acSMauro Carvalho Chehab 		 * To minimize noise, the gain stages should be used in the
612cb7a01acSMauro Carvalho Chehab 		 * second analog stage, first analog stage, digital stage order.
613cb7a01acSMauro Carvalho Chehab 		 * Gain from a previous stage should be pushed to its maximum
614cb7a01acSMauro Carvalho Chehab 		 * value before the next stage is used.
615cb7a01acSMauro Carvalho Chehab 		 */
616cb7a01acSMauro Carvalho Chehab 		if (ctrl->val <= 32) {
617cb7a01acSMauro Carvalho Chehab 			data = ctrl->val;
618cb7a01acSMauro Carvalho Chehab 		} else if (ctrl->val <= 64) {
619cb7a01acSMauro Carvalho Chehab 			ctrl->val &= ~1;
620cb7a01acSMauro Carvalho Chehab 			data = (1 << 6) | (ctrl->val >> 1);
621cb7a01acSMauro Carvalho Chehab 		} else {
622cb7a01acSMauro Carvalho Chehab 			ctrl->val &= ~7;
623cb7a01acSMauro Carvalho Chehab 			data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
624cb7a01acSMauro Carvalho Chehab 		}
625cb7a01acSMauro Carvalho Chehab 
626cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data);
627cb7a01acSMauro Carvalho Chehab 
628cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_HFLIP:
629cb7a01acSMauro Carvalho Chehab 		if (ctrl->val)
630cb7a01acSMauro Carvalho Chehab 			return mt9p031_set_mode2(mt9p031,
631cb7a01acSMauro Carvalho Chehab 					0, MT9P031_READ_MODE_2_COL_MIR);
632cb7a01acSMauro Carvalho Chehab 		else
633cb7a01acSMauro Carvalho Chehab 			return mt9p031_set_mode2(mt9p031,
634cb7a01acSMauro Carvalho Chehab 					MT9P031_READ_MODE_2_COL_MIR, 0);
635cb7a01acSMauro Carvalho Chehab 
636cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_VFLIP:
637cb7a01acSMauro Carvalho Chehab 		if (ctrl->val)
638cb7a01acSMauro Carvalho Chehab 			return mt9p031_set_mode2(mt9p031,
639cb7a01acSMauro Carvalho Chehab 					0, MT9P031_READ_MODE_2_ROW_MIR);
640cb7a01acSMauro Carvalho Chehab 		else
641cb7a01acSMauro Carvalho Chehab 			return mt9p031_set_mode2(mt9p031,
642cb7a01acSMauro Carvalho Chehab 					MT9P031_READ_MODE_2_ROW_MIR, 0);
643cb7a01acSMauro Carvalho Chehab 
644cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_TEST_PATTERN:
645cb7a01acSMauro Carvalho Chehab 		if (!ctrl->val) {
646cb7a01acSMauro Carvalho Chehab 			/* Restore the black level compensation settings. */
647cb7a01acSMauro Carvalho Chehab 			if (mt9p031->blc_auto->cur.val != 0) {
648cb7a01acSMauro Carvalho Chehab 				ret = mt9p031_s_ctrl(mt9p031->blc_auto);
649cb7a01acSMauro Carvalho Chehab 				if (ret < 0)
650cb7a01acSMauro Carvalho Chehab 					return ret;
651cb7a01acSMauro Carvalho Chehab 			}
652cb7a01acSMauro Carvalho Chehab 			if (mt9p031->blc_offset->cur.val != 0) {
653cb7a01acSMauro Carvalho Chehab 				ret = mt9p031_s_ctrl(mt9p031->blc_offset);
654cb7a01acSMauro Carvalho Chehab 				if (ret < 0)
655cb7a01acSMauro Carvalho Chehab 					return ret;
656cb7a01acSMauro Carvalho Chehab 			}
657cb7a01acSMauro Carvalho Chehab 			return mt9p031_write(client, MT9P031_TEST_PATTERN,
658cb7a01acSMauro Carvalho Chehab 					     MT9P031_TEST_PATTERN_DISABLE);
659cb7a01acSMauro Carvalho Chehab 		}
660cb7a01acSMauro Carvalho Chehab 
661cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0);
662cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
663cb7a01acSMauro Carvalho Chehab 			return ret;
664cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50);
665cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
666cb7a01acSMauro Carvalho Chehab 			return ret;
667cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0);
668cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
669cb7a01acSMauro Carvalho Chehab 			return ret;
670cb7a01acSMauro Carvalho Chehab 
671cb7a01acSMauro Carvalho Chehab 		/* Disable digital black level compensation when using a test
672cb7a01acSMauro Carvalho Chehab 		 * pattern.
673cb7a01acSMauro Carvalho Chehab 		 */
674cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC,
675cb7a01acSMauro Carvalho Chehab 					0);
676cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
677cb7a01acSMauro Carvalho Chehab 			return ret;
678cb7a01acSMauro Carvalho Chehab 
679cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0);
680cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
681cb7a01acSMauro Carvalho Chehab 			return ret;
682cb7a01acSMauro Carvalho Chehab 
683cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_TEST_PATTERN,
684cb7a01acSMauro Carvalho Chehab 				((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT)
685cb7a01acSMauro Carvalho Chehab 				| MT9P031_TEST_PATTERN_ENABLE);
686cb7a01acSMauro Carvalho Chehab 
687cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_BLC_AUTO:
688cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_set_mode2(mt9p031,
689cb7a01acSMauro Carvalho Chehab 				ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC,
690cb7a01acSMauro Carvalho Chehab 				ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0);
691cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
692cb7a01acSMauro Carvalho Chehab 			return ret;
693cb7a01acSMauro Carvalho Chehab 
694cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_BLACK_LEVEL_CALIBRATION,
695cb7a01acSMauro Carvalho Chehab 				     ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC);
696cb7a01acSMauro Carvalho Chehab 
697cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_BLC_TARGET_LEVEL:
698cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
699cb7a01acSMauro Carvalho Chehab 				     ctrl->val);
700cb7a01acSMauro Carvalho Chehab 
701cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_BLC_ANALOG_OFFSET:
702cb7a01acSMauro Carvalho Chehab 		data = ctrl->val & ((1 << 9) - 1);
703cb7a01acSMauro Carvalho Chehab 
704cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_GREEN1_OFFSET, data);
705cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
706cb7a01acSMauro Carvalho Chehab 			return ret;
707cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_GREEN2_OFFSET, data);
708cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
709cb7a01acSMauro Carvalho Chehab 			return ret;
710cb7a01acSMauro Carvalho Chehab 		ret = mt9p031_write(client, MT9P031_RED_OFFSET, data);
711cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
712cb7a01acSMauro Carvalho Chehab 			return ret;
713cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_BLUE_OFFSET, data);
714cb7a01acSMauro Carvalho Chehab 
715cb7a01acSMauro Carvalho Chehab 	case V4L2_CID_BLC_DIGITAL_OFFSET:
716cb7a01acSMauro Carvalho Chehab 		return mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET,
717cb7a01acSMauro Carvalho Chehab 				     ctrl->val & ((1 << 12) - 1));
718cb7a01acSMauro Carvalho Chehab 	}
719cb7a01acSMauro Carvalho Chehab 
720cb7a01acSMauro Carvalho Chehab 	return 0;
721cb7a01acSMauro Carvalho Chehab }
722cb7a01acSMauro Carvalho Chehab 
723cb7a01acSMauro Carvalho Chehab static struct v4l2_ctrl_ops mt9p031_ctrl_ops = {
724cb7a01acSMauro Carvalho Chehab 	.s_ctrl = mt9p031_s_ctrl,
725cb7a01acSMauro Carvalho Chehab };
726cb7a01acSMauro Carvalho Chehab 
727cb7a01acSMauro Carvalho Chehab static const char * const mt9p031_test_pattern_menu[] = {
728cb7a01acSMauro Carvalho Chehab 	"Disabled",
729cb7a01acSMauro Carvalho Chehab 	"Color Field",
730cb7a01acSMauro Carvalho Chehab 	"Horizontal Gradient",
731cb7a01acSMauro Carvalho Chehab 	"Vertical Gradient",
732cb7a01acSMauro Carvalho Chehab 	"Diagonal Gradient",
733cb7a01acSMauro Carvalho Chehab 	"Classic Test Pattern",
734cb7a01acSMauro Carvalho Chehab 	"Walking 1s",
735cb7a01acSMauro Carvalho Chehab 	"Monochrome Horizontal Bars",
736cb7a01acSMauro Carvalho Chehab 	"Monochrome Vertical Bars",
737cb7a01acSMauro Carvalho Chehab 	"Vertical Color Bars",
738cb7a01acSMauro Carvalho Chehab };
739cb7a01acSMauro Carvalho Chehab 
740cb7a01acSMauro Carvalho Chehab static const struct v4l2_ctrl_config mt9p031_ctrls[] = {
741cb7a01acSMauro Carvalho Chehab 	{
742cb7a01acSMauro Carvalho Chehab 		.ops		= &mt9p031_ctrl_ops,
743cb7a01acSMauro Carvalho Chehab 		.id		= V4L2_CID_TEST_PATTERN,
744cb7a01acSMauro Carvalho Chehab 		.type		= V4L2_CTRL_TYPE_MENU,
745cb7a01acSMauro Carvalho Chehab 		.name		= "Test Pattern",
746cb7a01acSMauro Carvalho Chehab 		.min		= 0,
747cb7a01acSMauro Carvalho Chehab 		.max		= ARRAY_SIZE(mt9p031_test_pattern_menu) - 1,
748cb7a01acSMauro Carvalho Chehab 		.step		= 0,
749cb7a01acSMauro Carvalho Chehab 		.def		= 0,
750cb7a01acSMauro Carvalho Chehab 		.flags		= 0,
751cb7a01acSMauro Carvalho Chehab 		.menu_skip_mask	= 0,
752cb7a01acSMauro Carvalho Chehab 		.qmenu		= mt9p031_test_pattern_menu,
753cb7a01acSMauro Carvalho Chehab 	}, {
754cb7a01acSMauro Carvalho Chehab 		.ops		= &mt9p031_ctrl_ops,
755cb7a01acSMauro Carvalho Chehab 		.id		= V4L2_CID_BLC_AUTO,
756cb7a01acSMauro Carvalho Chehab 		.type		= V4L2_CTRL_TYPE_BOOLEAN,
757cb7a01acSMauro Carvalho Chehab 		.name		= "BLC, Auto",
758cb7a01acSMauro Carvalho Chehab 		.min		= 0,
759cb7a01acSMauro Carvalho Chehab 		.max		= 1,
760cb7a01acSMauro Carvalho Chehab 		.step		= 1,
761cb7a01acSMauro Carvalho Chehab 		.def		= 1,
762cb7a01acSMauro Carvalho Chehab 		.flags		= 0,
763cb7a01acSMauro Carvalho Chehab 	}, {
764cb7a01acSMauro Carvalho Chehab 		.ops		= &mt9p031_ctrl_ops,
765cb7a01acSMauro Carvalho Chehab 		.id		= V4L2_CID_BLC_TARGET_LEVEL,
766cb7a01acSMauro Carvalho Chehab 		.type		= V4L2_CTRL_TYPE_INTEGER,
767cb7a01acSMauro Carvalho Chehab 		.name		= "BLC Target Level",
768cb7a01acSMauro Carvalho Chehab 		.min		= 0,
769cb7a01acSMauro Carvalho Chehab 		.max		= 4095,
770cb7a01acSMauro Carvalho Chehab 		.step		= 1,
771cb7a01acSMauro Carvalho Chehab 		.def		= 168,
772cb7a01acSMauro Carvalho Chehab 		.flags		= 0,
773cb7a01acSMauro Carvalho Chehab 	}, {
774cb7a01acSMauro Carvalho Chehab 		.ops		= &mt9p031_ctrl_ops,
775cb7a01acSMauro Carvalho Chehab 		.id		= V4L2_CID_BLC_ANALOG_OFFSET,
776cb7a01acSMauro Carvalho Chehab 		.type		= V4L2_CTRL_TYPE_INTEGER,
777cb7a01acSMauro Carvalho Chehab 		.name		= "BLC Analog Offset",
778cb7a01acSMauro Carvalho Chehab 		.min		= -255,
779cb7a01acSMauro Carvalho Chehab 		.max		= 255,
780cb7a01acSMauro Carvalho Chehab 		.step		= 1,
781cb7a01acSMauro Carvalho Chehab 		.def		= 32,
782cb7a01acSMauro Carvalho Chehab 		.flags		= 0,
783cb7a01acSMauro Carvalho Chehab 	}, {
784cb7a01acSMauro Carvalho Chehab 		.ops		= &mt9p031_ctrl_ops,
785cb7a01acSMauro Carvalho Chehab 		.id		= V4L2_CID_BLC_DIGITAL_OFFSET,
786cb7a01acSMauro Carvalho Chehab 		.type		= V4L2_CTRL_TYPE_INTEGER,
787cb7a01acSMauro Carvalho Chehab 		.name		= "BLC Digital Offset",
788cb7a01acSMauro Carvalho Chehab 		.min		= -2048,
789cb7a01acSMauro Carvalho Chehab 		.max		= 2047,
790cb7a01acSMauro Carvalho Chehab 		.step		= 1,
791cb7a01acSMauro Carvalho Chehab 		.def		= 40,
792cb7a01acSMauro Carvalho Chehab 		.flags		= 0,
793cb7a01acSMauro Carvalho Chehab 	}
794cb7a01acSMauro Carvalho Chehab };
795cb7a01acSMauro Carvalho Chehab 
796cb7a01acSMauro Carvalho Chehab /* -----------------------------------------------------------------------------
797cb7a01acSMauro Carvalho Chehab  * V4L2 subdev core operations
798cb7a01acSMauro Carvalho Chehab  */
799cb7a01acSMauro Carvalho Chehab 
800cb7a01acSMauro Carvalho Chehab static int mt9p031_set_power(struct v4l2_subdev *subdev, int on)
801cb7a01acSMauro Carvalho Chehab {
802cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
803cb7a01acSMauro Carvalho Chehab 	int ret = 0;
804cb7a01acSMauro Carvalho Chehab 
805cb7a01acSMauro Carvalho Chehab 	mutex_lock(&mt9p031->power_lock);
806cb7a01acSMauro Carvalho Chehab 
807cb7a01acSMauro Carvalho Chehab 	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
808cb7a01acSMauro Carvalho Chehab 	 * update the power state.
809cb7a01acSMauro Carvalho Chehab 	 */
810cb7a01acSMauro Carvalho Chehab 	if (mt9p031->power_count == !on) {
811cb7a01acSMauro Carvalho Chehab 		ret = __mt9p031_set_power(mt9p031, !!on);
812cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
813cb7a01acSMauro Carvalho Chehab 			goto out;
814cb7a01acSMauro Carvalho Chehab 	}
815cb7a01acSMauro Carvalho Chehab 
816cb7a01acSMauro Carvalho Chehab 	/* Update the power count. */
817cb7a01acSMauro Carvalho Chehab 	mt9p031->power_count += on ? 1 : -1;
818cb7a01acSMauro Carvalho Chehab 	WARN_ON(mt9p031->power_count < 0);
819cb7a01acSMauro Carvalho Chehab 
820cb7a01acSMauro Carvalho Chehab out:
821cb7a01acSMauro Carvalho Chehab 	mutex_unlock(&mt9p031->power_lock);
822cb7a01acSMauro Carvalho Chehab 	return ret;
823cb7a01acSMauro Carvalho Chehab }
824cb7a01acSMauro Carvalho Chehab 
825cb7a01acSMauro Carvalho Chehab /* -----------------------------------------------------------------------------
826cb7a01acSMauro Carvalho Chehab  * V4L2 subdev internal operations
827cb7a01acSMauro Carvalho Chehab  */
828cb7a01acSMauro Carvalho Chehab 
829cb7a01acSMauro Carvalho Chehab static int mt9p031_registered(struct v4l2_subdev *subdev)
830cb7a01acSMauro Carvalho Chehab {
831cb7a01acSMauro Carvalho Chehab 	struct i2c_client *client = v4l2_get_subdevdata(subdev);
832cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
833cb7a01acSMauro Carvalho Chehab 	s32 data;
834cb7a01acSMauro Carvalho Chehab 	int ret;
835cb7a01acSMauro Carvalho Chehab 
836cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_power_on(mt9p031);
837cb7a01acSMauro Carvalho Chehab 	if (ret < 0) {
838cb7a01acSMauro Carvalho Chehab 		dev_err(&client->dev, "MT9P031 power up failed\n");
839cb7a01acSMauro Carvalho Chehab 		return ret;
840cb7a01acSMauro Carvalho Chehab 	}
841cb7a01acSMauro Carvalho Chehab 
842cb7a01acSMauro Carvalho Chehab 	/* Read out the chip version register */
843cb7a01acSMauro Carvalho Chehab 	data = mt9p031_read(client, MT9P031_CHIP_VERSION);
844cb7a01acSMauro Carvalho Chehab 	if (data != MT9P031_CHIP_VERSION_VALUE) {
845cb7a01acSMauro Carvalho Chehab 		dev_err(&client->dev, "MT9P031 not detected, wrong version "
846cb7a01acSMauro Carvalho Chehab 			"0x%04x\n", data);
847cb7a01acSMauro Carvalho Chehab 		return -ENODEV;
848cb7a01acSMauro Carvalho Chehab 	}
849cb7a01acSMauro Carvalho Chehab 
850cb7a01acSMauro Carvalho Chehab 	mt9p031_power_off(mt9p031);
851cb7a01acSMauro Carvalho Chehab 
852cb7a01acSMauro Carvalho Chehab 	dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n",
853cb7a01acSMauro Carvalho Chehab 		 client->addr);
854cb7a01acSMauro Carvalho Chehab 
855cb7a01acSMauro Carvalho Chehab 	return ret;
856cb7a01acSMauro Carvalho Chehab }
857cb7a01acSMauro Carvalho Chehab 
858cb7a01acSMauro Carvalho Chehab static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
859cb7a01acSMauro Carvalho Chehab {
860cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
861cb7a01acSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt *format;
862cb7a01acSMauro Carvalho Chehab 	struct v4l2_rect *crop;
863cb7a01acSMauro Carvalho Chehab 
864cb7a01acSMauro Carvalho Chehab 	crop = v4l2_subdev_get_try_crop(fh, 0);
865cb7a01acSMauro Carvalho Chehab 	crop->left = MT9P031_COLUMN_START_DEF;
866cb7a01acSMauro Carvalho Chehab 	crop->top = MT9P031_ROW_START_DEF;
867cb7a01acSMauro Carvalho Chehab 	crop->width = MT9P031_WINDOW_WIDTH_DEF;
868cb7a01acSMauro Carvalho Chehab 	crop->height = MT9P031_WINDOW_HEIGHT_DEF;
869cb7a01acSMauro Carvalho Chehab 
870cb7a01acSMauro Carvalho Chehab 	format = v4l2_subdev_get_try_format(fh, 0);
871cb7a01acSMauro Carvalho Chehab 
872cb7a01acSMauro Carvalho Chehab 	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
873cb7a01acSMauro Carvalho Chehab 		format->code = V4L2_MBUS_FMT_Y12_1X12;
874cb7a01acSMauro Carvalho Chehab 	else
875cb7a01acSMauro Carvalho Chehab 		format->code = V4L2_MBUS_FMT_SGRBG12_1X12;
876cb7a01acSMauro Carvalho Chehab 
877cb7a01acSMauro Carvalho Chehab 	format->width = MT9P031_WINDOW_WIDTH_DEF;
878cb7a01acSMauro Carvalho Chehab 	format->height = MT9P031_WINDOW_HEIGHT_DEF;
879cb7a01acSMauro Carvalho Chehab 	format->field = V4L2_FIELD_NONE;
880cb7a01acSMauro Carvalho Chehab 	format->colorspace = V4L2_COLORSPACE_SRGB;
881cb7a01acSMauro Carvalho Chehab 
882cb7a01acSMauro Carvalho Chehab 	return mt9p031_set_power(subdev, 1);
883cb7a01acSMauro Carvalho Chehab }
884cb7a01acSMauro Carvalho Chehab 
885cb7a01acSMauro Carvalho Chehab static int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
886cb7a01acSMauro Carvalho Chehab {
887cb7a01acSMauro Carvalho Chehab 	return mt9p031_set_power(subdev, 0);
888cb7a01acSMauro Carvalho Chehab }
889cb7a01acSMauro Carvalho Chehab 
890cb7a01acSMauro Carvalho Chehab static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = {
891cb7a01acSMauro Carvalho Chehab 	.s_power        = mt9p031_set_power,
892cb7a01acSMauro Carvalho Chehab };
893cb7a01acSMauro Carvalho Chehab 
894cb7a01acSMauro Carvalho Chehab static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
895cb7a01acSMauro Carvalho Chehab 	.s_stream       = mt9p031_s_stream,
896cb7a01acSMauro Carvalho Chehab };
897cb7a01acSMauro Carvalho Chehab 
898cb7a01acSMauro Carvalho Chehab static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
899cb7a01acSMauro Carvalho Chehab 	.enum_mbus_code = mt9p031_enum_mbus_code,
900cb7a01acSMauro Carvalho Chehab 	.enum_frame_size = mt9p031_enum_frame_size,
901cb7a01acSMauro Carvalho Chehab 	.get_fmt = mt9p031_get_format,
902cb7a01acSMauro Carvalho Chehab 	.set_fmt = mt9p031_set_format,
903cb7a01acSMauro Carvalho Chehab 	.get_crop = mt9p031_get_crop,
904cb7a01acSMauro Carvalho Chehab 	.set_crop = mt9p031_set_crop,
905cb7a01acSMauro Carvalho Chehab };
906cb7a01acSMauro Carvalho Chehab 
907cb7a01acSMauro Carvalho Chehab static struct v4l2_subdev_ops mt9p031_subdev_ops = {
908cb7a01acSMauro Carvalho Chehab 	.core   = &mt9p031_subdev_core_ops,
909cb7a01acSMauro Carvalho Chehab 	.video  = &mt9p031_subdev_video_ops,
910cb7a01acSMauro Carvalho Chehab 	.pad    = &mt9p031_subdev_pad_ops,
911cb7a01acSMauro Carvalho Chehab };
912cb7a01acSMauro Carvalho Chehab 
913cb7a01acSMauro Carvalho Chehab static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = {
914cb7a01acSMauro Carvalho Chehab 	.registered = mt9p031_registered,
915cb7a01acSMauro Carvalho Chehab 	.open = mt9p031_open,
916cb7a01acSMauro Carvalho Chehab 	.close = mt9p031_close,
917cb7a01acSMauro Carvalho Chehab };
918cb7a01acSMauro Carvalho Chehab 
919cb7a01acSMauro Carvalho Chehab /* -----------------------------------------------------------------------------
920cb7a01acSMauro Carvalho Chehab  * Driver initialization and probing
921cb7a01acSMauro Carvalho Chehab  */
922cb7a01acSMauro Carvalho Chehab 
923cb7a01acSMauro Carvalho Chehab static int mt9p031_probe(struct i2c_client *client,
924cb7a01acSMauro Carvalho Chehab 			 const struct i2c_device_id *did)
925cb7a01acSMauro Carvalho Chehab {
926cb7a01acSMauro Carvalho Chehab 	struct mt9p031_platform_data *pdata = client->dev.platform_data;
927cb7a01acSMauro Carvalho Chehab 	struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
928cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031;
929cb7a01acSMauro Carvalho Chehab 	unsigned int i;
930cb7a01acSMauro Carvalho Chehab 	int ret;
931cb7a01acSMauro Carvalho Chehab 
932cb7a01acSMauro Carvalho Chehab 	if (pdata == NULL) {
933cb7a01acSMauro Carvalho Chehab 		dev_err(&client->dev, "No platform data\n");
934cb7a01acSMauro Carvalho Chehab 		return -EINVAL;
935cb7a01acSMauro Carvalho Chehab 	}
936cb7a01acSMauro Carvalho Chehab 
937cb7a01acSMauro Carvalho Chehab 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
938cb7a01acSMauro Carvalho Chehab 		dev_warn(&client->dev,
939cb7a01acSMauro Carvalho Chehab 			"I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
940cb7a01acSMauro Carvalho Chehab 		return -EIO;
941cb7a01acSMauro Carvalho Chehab 	}
942cb7a01acSMauro Carvalho Chehab 
943cb7a01acSMauro Carvalho Chehab 	mt9p031 = kzalloc(sizeof(*mt9p031), GFP_KERNEL);
944cb7a01acSMauro Carvalho Chehab 	if (mt9p031 == NULL)
945cb7a01acSMauro Carvalho Chehab 		return -ENOMEM;
946cb7a01acSMauro Carvalho Chehab 
947cb7a01acSMauro Carvalho Chehab 	mt9p031->pdata = pdata;
948cb7a01acSMauro Carvalho Chehab 	mt9p031->output_control	= MT9P031_OUTPUT_CONTROL_DEF;
949cb7a01acSMauro Carvalho Chehab 	mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC;
950cb7a01acSMauro Carvalho Chehab 	mt9p031->model = did->driver_data;
951cb7a01acSMauro Carvalho Chehab 	mt9p031->reset = -1;
952cb7a01acSMauro Carvalho Chehab 
953cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 5);
954cb7a01acSMauro Carvalho Chehab 
955cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
956cb7a01acSMauro Carvalho Chehab 			  V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN,
957cb7a01acSMauro Carvalho Chehab 			  MT9P031_SHUTTER_WIDTH_MAX, 1,
958cb7a01acSMauro Carvalho Chehab 			  MT9P031_SHUTTER_WIDTH_DEF);
959cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
960cb7a01acSMauro Carvalho Chehab 			  V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN,
961cb7a01acSMauro Carvalho Chehab 			  MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF);
962cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
963cb7a01acSMauro Carvalho Chehab 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
964cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
965cb7a01acSMauro Carvalho Chehab 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
966cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
967cb7a01acSMauro Carvalho Chehab 			  V4L2_CID_PIXEL_RATE, pdata->target_freq,
968cb7a01acSMauro Carvalho Chehab 			  pdata->target_freq, 1, pdata->target_freq);
969cb7a01acSMauro Carvalho Chehab 
970cb7a01acSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i)
971cb7a01acSMauro Carvalho Chehab 		v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL);
972cb7a01acSMauro Carvalho Chehab 
973cb7a01acSMauro Carvalho Chehab 	mt9p031->subdev.ctrl_handler = &mt9p031->ctrls;
974cb7a01acSMauro Carvalho Chehab 
975cb7a01acSMauro Carvalho Chehab 	if (mt9p031->ctrls.error) {
976cb7a01acSMauro Carvalho Chehab 		printk(KERN_INFO "%s: control initialization error %d\n",
977cb7a01acSMauro Carvalho Chehab 		       __func__, mt9p031->ctrls.error);
978cb7a01acSMauro Carvalho Chehab 		ret = mt9p031->ctrls.error;
979cb7a01acSMauro Carvalho Chehab 		goto done;
980cb7a01acSMauro Carvalho Chehab 	}
981cb7a01acSMauro Carvalho Chehab 
982cb7a01acSMauro Carvalho Chehab 	mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO);
983cb7a01acSMauro Carvalho Chehab 	mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls,
984cb7a01acSMauro Carvalho Chehab 					     V4L2_CID_BLC_DIGITAL_OFFSET);
985cb7a01acSMauro Carvalho Chehab 
986cb7a01acSMauro Carvalho Chehab 	mutex_init(&mt9p031->power_lock);
987cb7a01acSMauro Carvalho Chehab 	v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops);
988cb7a01acSMauro Carvalho Chehab 	mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops;
989cb7a01acSMauro Carvalho Chehab 
990cb7a01acSMauro Carvalho Chehab 	mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE;
991cb7a01acSMauro Carvalho Chehab 	ret = media_entity_init(&mt9p031->subdev.entity, 1, &mt9p031->pad, 0);
992cb7a01acSMauro Carvalho Chehab 	if (ret < 0)
993cb7a01acSMauro Carvalho Chehab 		goto done;
994cb7a01acSMauro Carvalho Chehab 
995cb7a01acSMauro Carvalho Chehab 	mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
996cb7a01acSMauro Carvalho Chehab 
997cb7a01acSMauro Carvalho Chehab 	mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF;
998cb7a01acSMauro Carvalho Chehab 	mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF;
999cb7a01acSMauro Carvalho Chehab 	mt9p031->crop.left = MT9P031_COLUMN_START_DEF;
1000cb7a01acSMauro Carvalho Chehab 	mt9p031->crop.top = MT9P031_ROW_START_DEF;
1001cb7a01acSMauro Carvalho Chehab 
1002cb7a01acSMauro Carvalho Chehab 	if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
1003cb7a01acSMauro Carvalho Chehab 		mt9p031->format.code = V4L2_MBUS_FMT_Y12_1X12;
1004cb7a01acSMauro Carvalho Chehab 	else
1005cb7a01acSMauro Carvalho Chehab 		mt9p031->format.code = V4L2_MBUS_FMT_SGRBG12_1X12;
1006cb7a01acSMauro Carvalho Chehab 
1007cb7a01acSMauro Carvalho Chehab 	mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF;
1008cb7a01acSMauro Carvalho Chehab 	mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF;
1009cb7a01acSMauro Carvalho Chehab 	mt9p031->format.field = V4L2_FIELD_NONE;
1010cb7a01acSMauro Carvalho Chehab 	mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB;
1011cb7a01acSMauro Carvalho Chehab 
1012cb7a01acSMauro Carvalho Chehab 	if (pdata->reset != -1) {
1013cb7a01acSMauro Carvalho Chehab 		ret = gpio_request_one(pdata->reset, GPIOF_OUT_INIT_LOW,
1014cb7a01acSMauro Carvalho Chehab 				       "mt9p031_rst");
1015cb7a01acSMauro Carvalho Chehab 		if (ret < 0)
1016cb7a01acSMauro Carvalho Chehab 			goto done;
1017cb7a01acSMauro Carvalho Chehab 
1018cb7a01acSMauro Carvalho Chehab 		mt9p031->reset = pdata->reset;
1019cb7a01acSMauro Carvalho Chehab 	}
1020cb7a01acSMauro Carvalho Chehab 
1021cb7a01acSMauro Carvalho Chehab 	ret = mt9p031_pll_setup(mt9p031);
1022cb7a01acSMauro Carvalho Chehab 
1023cb7a01acSMauro Carvalho Chehab done:
1024cb7a01acSMauro Carvalho Chehab 	if (ret < 0) {
1025cb7a01acSMauro Carvalho Chehab 		if (mt9p031->reset != -1)
1026cb7a01acSMauro Carvalho Chehab 			gpio_free(mt9p031->reset);
1027cb7a01acSMauro Carvalho Chehab 
1028cb7a01acSMauro Carvalho Chehab 		v4l2_ctrl_handler_free(&mt9p031->ctrls);
1029cb7a01acSMauro Carvalho Chehab 		media_entity_cleanup(&mt9p031->subdev.entity);
1030cb7a01acSMauro Carvalho Chehab 		kfree(mt9p031);
1031cb7a01acSMauro Carvalho Chehab 	}
1032cb7a01acSMauro Carvalho Chehab 
1033cb7a01acSMauro Carvalho Chehab 	return ret;
1034cb7a01acSMauro Carvalho Chehab }
1035cb7a01acSMauro Carvalho Chehab 
1036cb7a01acSMauro Carvalho Chehab static int mt9p031_remove(struct i2c_client *client)
1037cb7a01acSMauro Carvalho Chehab {
1038cb7a01acSMauro Carvalho Chehab 	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1039cb7a01acSMauro Carvalho Chehab 	struct mt9p031 *mt9p031 = to_mt9p031(subdev);
1040cb7a01acSMauro Carvalho Chehab 
1041cb7a01acSMauro Carvalho Chehab 	v4l2_ctrl_handler_free(&mt9p031->ctrls);
1042cb7a01acSMauro Carvalho Chehab 	v4l2_device_unregister_subdev(subdev);
1043cb7a01acSMauro Carvalho Chehab 	media_entity_cleanup(&subdev->entity);
1044cb7a01acSMauro Carvalho Chehab 	if (mt9p031->reset != -1)
1045cb7a01acSMauro Carvalho Chehab 		gpio_free(mt9p031->reset);
1046cb7a01acSMauro Carvalho Chehab 	kfree(mt9p031);
1047cb7a01acSMauro Carvalho Chehab 
1048cb7a01acSMauro Carvalho Chehab 	return 0;
1049cb7a01acSMauro Carvalho Chehab }
1050cb7a01acSMauro Carvalho Chehab 
1051cb7a01acSMauro Carvalho Chehab static const struct i2c_device_id mt9p031_id[] = {
1052cb7a01acSMauro Carvalho Chehab 	{ "mt9p031", MT9P031_MODEL_COLOR },
1053cb7a01acSMauro Carvalho Chehab 	{ "mt9p031m", MT9P031_MODEL_MONOCHROME },
1054cb7a01acSMauro Carvalho Chehab 	{ }
1055cb7a01acSMauro Carvalho Chehab };
1056cb7a01acSMauro Carvalho Chehab MODULE_DEVICE_TABLE(i2c, mt9p031_id);
1057cb7a01acSMauro Carvalho Chehab 
1058cb7a01acSMauro Carvalho Chehab static struct i2c_driver mt9p031_i2c_driver = {
1059cb7a01acSMauro Carvalho Chehab 	.driver = {
1060cb7a01acSMauro Carvalho Chehab 		.name = "mt9p031",
1061cb7a01acSMauro Carvalho Chehab 	},
1062cb7a01acSMauro Carvalho Chehab 	.probe          = mt9p031_probe,
1063cb7a01acSMauro Carvalho Chehab 	.remove         = mt9p031_remove,
1064cb7a01acSMauro Carvalho Chehab 	.id_table       = mt9p031_id,
1065cb7a01acSMauro Carvalho Chehab };
1066cb7a01acSMauro Carvalho Chehab 
1067cb7a01acSMauro Carvalho Chehab module_i2c_driver(mt9p031_i2c_driver);
1068cb7a01acSMauro Carvalho Chehab 
1069cb7a01acSMauro Carvalho Chehab MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
1070cb7a01acSMauro Carvalho Chehab MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1071cb7a01acSMauro Carvalho Chehab MODULE_LICENSE("GPL v2");
1072