2 * mt9p012.c - mt9p012 sensor driver
4 * Copyright (C) 2009 Texas Instruments.
7 * Sameer Venkatraman <sameerv@ti.com>
8 * Sergio Aguirre <saaguirre@ti.com>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/i2c.h>
19 #include <linux/delay.h>
20 #include <media/v4l2-int-device.h>
22 #include <media/mt9p012.h>
23 #include "mt9p012_regs.h"
25 #define DRIVER_NAME "mt9p012"
27 /* MT9P012 has 8/16/32 registers */
28 #define MT9P012_8BIT 1
29 #define MT9P012_16BIT 2
30 #define MT9P012_32BIT 4
32 /* terminating token for reg list */
33 #define MT9P012_TOK_TERM 0xFF
35 /* delay token for reg list */
36 #define MT9P012_TOK_DELAY 100
38 /* The ID values we are looking for */
39 #define MT9P012_MOD_ID 0x2800
40 #define MT9P012_MFR_ID 0x0006
42 /* FPS Capabilities */
43 #define MT9P012_MIN_FPS 11
44 #define MT9P012_DEF_FPS 15
45 #define MT9P012_MAX_FPS 30
47 #define MT9P012_I2C_RETRY_COUNT 5
49 #define MT9P012_XCLK_NOM_1 12000000
50 #define MT9P012_XCLK_NOM_2 24000000
52 /* Still capture 5 MP */
53 #define MT9P012_IMAGE_WIDTH_MAX 2592
54 #define MT9P012_IMAGE_HEIGHT_MAX 1944
55 /* Still capture 3 MP and down to VGA, using ISP resizer */
56 #define MT9P012_IMAGE_WIDTH_MIN 2048
57 #define MT9P012_IMAGE_HEIGHT_MIN 1536
59 /* Video mode, for D1 NTSC, D1 PAL */
60 #define MT9P012_VIDEO_WIDTH_2X_BINN 1296
61 #define MT9P012_VIDEO_HEIGHT_2X_BINN 972
63 /* Sensor Video mode size for VGA, CIF, QVGA in 4x binning mode */
64 #define MT9P012_VIDEO_WIDTH_4X_BINN 648
65 #define MT9P012_VIDEO_HEIGHT_4X_BINN 486
66 /* To improve image quality in VGA */
67 #define MT9P012_CIF_PIXELS (352 * 288)
68 #define MT9P012_QQVGA_PIXELS (160 * 120)
70 /* Video mode, for QCIF, SQCIF */
71 #define MT9P012_VIDEO_WIDTH_4X_BINN_SCALED 216
72 #define MT9P012_VIDEO_HEIGHT_4X_BINN_SCALED 162
74 /* Default coarse integration times to get a good exposure */
75 #define MT9P012_COARSE_INT_TIME_216 550
76 #define MT9P012_COARSE_INT_TIME_648 550
77 #define MT9P012_COARSE_INT_TIME_216_30FPS 1350
78 #define MT9P012_COARSE_INT_TIME_648_30FPS 1350
79 #define MT9P012_COARSE_INT_TIME_1296 1000
80 #define MT9P012_COARSE_INT_TIME_3MP 1700
81 #define MT9P012_COARSE_INT_TIME_5MP 1700
82 #define MT9P012_COARSE_INT_TIME_INDEX 1
83 #define MT9P012_TST_PAT 0x0
85 /* Analog gain values */
86 #define MT9P012_MIN_GAIN 0x08
87 #define MT9P012_MAX_GAIN 0x7F
88 #define MT9P012_DEF_GAIN 0x43
89 #define MT9P012_GAIN_STEP 0x1
91 #define MT9P012_GAIN_INDEX 1
93 /* Exposure time values */
94 #define MT9P012_DEF_MIN_EXPOSURE 0x08
95 #define MT9P012_DEF_MAX_EXPOSURE 0x7F
96 #define MT9P012_DEF_EXPOSURE 0x43
97 #define MT9P012_EXPOSURE_STEP 1
100 * struct mt9p012_reg - mt9p012 register format
101 * @length: length of the register
102 * @reg: 16-bit offset to register
103 * @val: 8/16/32-bit register value
105 * Define a structure for MT9P012 register initialization values
113 enum mt9p012_image_size
{
121 #define MT9P012_NUM_IMAGE_SIZES 5
122 #define MT9P012_NUM_PIXEL_FORMATS 1
123 #define MT9P012_NUM_FPS 2 /* 2 ranges */
124 #define MT9P012_FPS_LOW_RANGE 0
125 #define MT9P012_FPS_HIGH_RANGE 1
128 * struct capture_size - image capture size information
129 * @width: image width in pixels
130 * @height: image height in pixels
132 struct mt9p012_capture_size
{
134 unsigned long height
;
138 * struct mt9p012_pll_settings - struct for storage of sensor pll values
139 * @vt_pix_clk_div: vertical pixel clock divider
140 * @vt_sys_clk_div: veritcal system clock divider
141 * @pre_pll_div: pre pll divider
142 * @fine_int_tm: fine resolution interval time
143 * @frame_lines: number of lines in frame
144 * @line_len: number of pixels in line
145 * @min_pll: minimum pll multiplier
146 * @max_pll: maximum pll multiplier
148 struct mt9p012_pll_settings
{
162 * Array of image sizes supported by MT9P012. These must be ordered from
163 * smallest image size to largest.
165 const static struct mt9p012_capture_size mt9p012_sizes
[] = {
166 { 216, 162 }, /* 4X BINNING+SCALING */
167 { 648, 486 }, /* 4X BINNING */
168 { 1296, 972 }, /* 2X BINNING */
169 { 2048, 1536}, /* 3 MP */
170 { 2592, 1944}, /* 5 MP */
173 /* PLL settings for MT9P012 */
174 enum mt9p012_pll_type
{
177 MT9P012_PLL_1296_15FPS
,
178 MT9P012_PLL_1296_30FPS
,
179 MT9P012_PLL_648_15FPS
,
180 MT9P012_PLL_648_30FPS
,
181 MT9P012_PLL_216_15FPS
,
182 MT9P012_PLL_216_30FPS
185 /* Debug functions */
187 module_param(debug
, bool, 0644);
188 MODULE_PARM_DESC(debug
, "Debug Enabled (0-1)");
191 * struct mt9p012_sensor - main structure for storage of sensor information
193 * @pdata: access functions and data for platform level information
194 * @v4l2_int_device: V4L2 device structure structure
195 * @pix: V4L2 pixel format information structure
196 * @timeperframe: time per frame expressed as V4L fraction
198 * @ver: mt9p012 chip version
199 * @fps: frames per second value
201 struct mt9p012_sensor
{
203 struct mt9p012_platform_data
*pdata
;
204 struct v4l2_int_device
*v4l2_int_device
;
205 struct v4l2_pix_format pix
;
206 struct v4l2_fract timeperframe
;
211 unsigned long xclk_current
;
214 /* list of image formats supported by mt9p012 sensor */
215 const static struct v4l2_fmtdesc mt9p012_formats
[] = {
217 .description
= "Bayer10 (GrR/BGb)",
218 .pixelformat
= V4L2_PIX_FMT_SGRBG10
,
222 #define NUM_CAPTURE_FORMATS ARRAY_SIZE(mt9p012_formats)
224 /* Enters soft standby, all settings are maintained */
225 const static struct mt9p012_reg stream_off_list
[] = {
226 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
227 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
230 /* Exits soft standby */
231 const static struct mt9p012_reg stream_on_list
[] = {
232 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x01},
233 /* Sensor datasheet says we need 1 ms to allow PLL lock */
234 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 1},
235 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
238 /* Structure which will set the exposure time */
239 static struct mt9p012_reg set_exposure_time
[] = {
240 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
241 /* less than frame_lines-1 */
242 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
, .val
= 500},
244 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
245 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
248 /* Structure to set analog gain */
249 static struct mt9p012_reg set_analog_gain
[] = {
250 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
251 {.length
= MT9P012_16BIT
, .reg
= REG_ANALOG_GAIN_GLOBAL
,
252 .val
= MT9P012_MIN_GAIN
},
254 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
255 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0},
259 * Common MT9P012 register initialization for all image sizes, pixel formats,
262 const static struct mt9p012_reg mt9p012_common
[] = {
263 {MT9P012_8BIT
, REG_SOFTWARE_RESET
, 0x01},
264 {MT9P012_TOK_DELAY
, 0x00, 5}, /* Delay = 5ms, min 2400 xcks */
265 {MT9P012_16BIT
, REG_RESET_REGISTER
, 0x10C8},
266 {MT9P012_8BIT
, REG_GROUPED_PAR_HOLD
, 0x01}, /* hold */
267 {MT9P012_16BIT
, REG_ANALOG_GAIN_GREENR
, 0x0020},
268 {MT9P012_16BIT
, REG_ANALOG_GAIN_RED
, 0x0020},
269 {MT9P012_16BIT
, REG_ANALOG_GAIN_BLUE
, 0x0020},
270 {MT9P012_16BIT
, REG_ANALOG_GAIN_GREENB
, 0x0020},
271 {MT9P012_16BIT
, REG_DIGITAL_GAIN_GREENR
, 0x0100},
272 {MT9P012_16BIT
, REG_DIGITAL_GAIN_RED
, 0x0100},
273 {MT9P012_16BIT
, REG_DIGITAL_GAIN_BLUE
, 0x0100},
274 {MT9P012_16BIT
, REG_DIGITAL_GAIN_GREENB
, 0x0100},
275 /* Recommended values for image quality, sensor Rev 1 */
276 {MT9P012_16BIT
, 0x3088, 0x6FFB},
277 {MT9P012_16BIT
, 0x308E, 0x2020},
278 {MT9P012_16BIT
, 0x309E, 0x4400},
279 {MT9P012_16BIT
, 0x30D4, 0x9080},
280 {MT9P012_16BIT
, 0x3126, 0x00FF},
281 {MT9P012_16BIT
, 0x3154, 0x1482},
282 {MT9P012_16BIT
, 0x3158, 0x97C7},
283 {MT9P012_16BIT
, 0x315A, 0x97C6},
284 {MT9P012_16BIT
, 0x3162, 0x074C},
285 {MT9P012_16BIT
, 0x3164, 0x0756},
286 {MT9P012_16BIT
, 0x3166, 0x0760},
287 {MT9P012_16BIT
, 0x316E, 0x8488},
288 {MT9P012_16BIT
, 0x3172, 0x0003},
289 {MT9P012_16BIT
, 0x30EA, 0x3F06},
290 {MT9P012_8BIT
, REG_GROUPED_PAR_HOLD
, 0x00}, /* update all at once */
291 {MT9P012_TOK_TERM
, 0, 0}
295 * mt9p012 register configuration for all combinations of pixel format and
298 /* 4X BINNING+SCALING */
299 const static struct mt9p012_reg enter_video_216_15fps
[] = {
301 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
302 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
304 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
305 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 8},
306 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 2},
307 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 2},
308 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 126},
309 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 8},
310 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 2},
311 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
,
313 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
314 .val
= MT9P012_VIDEO_WIDTH_4X_BINN_SCALED
},
315 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
316 .val
= MT9P012_VIDEO_HEIGHT_4X_BINN_SCALED
},
317 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
318 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
319 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2593},
320 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1945},
321 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x04FC},
322 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
323 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 574},
324 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 2712},
325 /* 0x10/0x30 = 0.3333 */
326 {.length
= MT9P012_16BIT
, .reg
= REG_SCALE_M
, .val
= 0x0030},
328 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0002},
329 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
330 .val
= MT9P012_COARSE_INT_TIME_216
},
332 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
333 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
336 /* Video mode, 4x binning + scaling, range 16 - 30 fps */
337 const static struct mt9p012_reg enter_video_216_30fps
[] = {
339 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
340 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
342 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
343 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 5},
344 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 2},
345 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 3},
346 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 192},
347 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 10},
348 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 2},
349 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
350 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
351 .val
= MT9P012_VIDEO_WIDTH_4X_BINN
},
352 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
353 .val
= MT9P012_VIDEO_HEIGHT_4X_BINN
},
354 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
355 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
356 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2593},
357 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1945},
358 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x04FC},
359 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
360 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 1374},
361 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 3712},
362 /* 0x10/0x30 = 0.3333 */
363 {.length
= MT9P012_16BIT
, .reg
= REG_SCALE_M
, .val
= 0x0030},
365 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0002},
366 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
367 .val
= MT9P012_COARSE_INT_TIME_216_30FPS
},
369 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
370 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
374 /*Video mode, 4x binning: 648 x 486, range 8 - 15 fps*/
375 const static struct mt9p012_reg enter_video_648_15fps
[] = {
377 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
378 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
380 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
381 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 8},
382 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 2},
383 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 2},
384 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 126},
385 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 8},
386 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 2},
387 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
388 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
389 .val
= MT9P012_VIDEO_WIDTH_4X_BINN
},
390 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
391 .val
= MT9P012_VIDEO_HEIGHT_4X_BINN
},
392 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
393 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
394 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2593},
395 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1945},
396 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x04FC},
397 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
398 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 574},
399 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 2712},
400 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0000},
401 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
402 .val
= MT9P012_COARSE_INT_TIME_648
},
404 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
405 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
408 /* Video mode, 4x binning: 648 x 486, range 16 - 30 fps */
409 const static struct mt9p012_reg enter_video_648_30fps
[] = {
411 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
412 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
414 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
415 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 5},
416 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 2},
417 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 3},
418 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 192},
419 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 10},
420 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 2},
421 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
422 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
423 .val
= MT9P012_VIDEO_WIDTH_4X_BINN
},
424 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
425 .val
= MT9P012_VIDEO_HEIGHT_4X_BINN
},
426 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
427 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
428 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2593},
429 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1945},
430 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x04FC},
431 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
432 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 1374},
433 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 3712},
434 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0000},
435 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
436 .val
= MT9P012_COARSE_INT_TIME_648_30FPS
},
438 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
439 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
442 /* Video mode, scaler off: 1296 x 972, range 11 - 21 fps*/
443 const static struct mt9p012_reg enter_video_1296_15fps
[] = {
445 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
446 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
448 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
449 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 5},
450 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 2},
451 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 3},
452 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 134},
453 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 10},
454 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 1},
455 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
456 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
457 .val
= MT9P012_VIDEO_WIDTH_2X_BINN
},
458 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
459 .val
= MT9P012_VIDEO_HEIGHT_2X_BINN
},
460 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
461 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
462 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2597},
463 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1949},
464 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x046C},
465 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
466 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 1061},
467 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 3360},
468 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0000},
469 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
470 .val
= MT9P012_COARSE_INT_TIME_1296
},
472 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
473 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
476 /* YUV (YCbCr) VGA */
477 const static struct mt9p012_reg enter_video_1296_30fps
[] = {
479 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
480 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
482 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
483 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 5},
484 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 1},
485 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 3},
486 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 134},
487 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 10},
488 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 1},
489 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
490 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
491 .val
= MT9P012_VIDEO_WIDTH_2X_BINN
},
492 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
493 .val
= MT9P012_VIDEO_HEIGHT_2X_BINN
},
494 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
495 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
496 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2597},
497 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1949},
498 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x046C},
499 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 1794},
500 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 1061},
501 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 3360},
502 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0000},
503 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
504 .val
= MT9P012_COARSE_INT_TIME_1296
},
506 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
507 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
510 const static struct mt9p012_reg enter_image_mode_3MP_10fps
[] = {
512 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
513 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
515 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
516 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 4},
517 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 1},
518 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 5},
520 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 184},
521 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 8},
522 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 1},
523 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
524 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
525 .val
= MT9P012_IMAGE_WIDTH_MIN
},
526 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
527 .val
= MT9P012_IMAGE_HEIGHT_MIN
},
528 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
529 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
530 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2599},
531 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1951},
532 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x0024},
533 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 882},
534 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 2056},
535 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 5372},
536 /* 0x10/0x14 = 0.80 */
537 {.length
= MT9P012_16BIT
, .reg
= REG_SCALE_M
, .val
= 0x0014},
539 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0002},
540 {.length
= MT9P012_16BIT
, .reg
= REG_TEST_PATTERN
,
541 .val
= MT9P012_TST_PAT
},
542 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
543 .val
= MT9P012_COARSE_INT_TIME_3MP
},
545 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
546 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
549 /* Image mode, 5 MP @ 10 fps */
550 const static struct mt9p012_reg enter_image_mode_5MP_10fps
[] = {
552 {.length
= MT9P012_8BIT
, .reg
= REG_MODE_SELECT
, .val
= 0x00},
553 {.length
= MT9P012_TOK_DELAY
, .reg
= 0x00, .val
= 100},
555 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x01},
556 {.length
= MT9P012_16BIT
, .reg
= REG_VT_PIX_CLK_DIV
, .val
= 4},
557 {.length
= MT9P012_16BIT
, .reg
= REG_VT_SYS_CLK_DIV
, .val
= 1},
558 {.length
= MT9P012_16BIT
, .reg
= REG_PRE_PLL_CLK_DIV
, .val
= 5},
560 {.length
= MT9P012_16BIT
, .reg
= REG_PLL_MULTIPLIER
, .val
= 184},
561 {.length
= MT9P012_16BIT
, .reg
= REG_OP_PIX_CLK_DIV
, .val
= 8},
562 {.length
= MT9P012_16BIT
, .reg
= REG_OP_SYS_CLK_DIV
, .val
= 1},
563 {.length
= MT9P012_16BIT
, .reg
= REG_RESERVED_MFR_3064
, .val
= 0x0805},
564 {.length
= MT9P012_16BIT
, .reg
= REG_X_OUTPUT_SIZE
,
565 .val
= MT9P012_IMAGE_WIDTH_MAX
},
566 {.length
= MT9P012_16BIT
, .reg
= REG_Y_OUTPUT_SIZE
,
567 .val
= MT9P012_IMAGE_HEIGHT_MAX
},
568 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_START
, .val
= 8},
569 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_START
, .val
= 8},
570 {.length
= MT9P012_16BIT
, .reg
= REG_X_ADDR_END
, .val
= 2599},
571 {.length
= MT9P012_16BIT
, .reg
= REG_Y_ADDR_END
, .val
= 1951},
572 {.length
= MT9P012_16BIT
, .reg
= REG_READ_MODE
, .val
= 0x0024},
573 {.length
= MT9P012_16BIT
, .reg
= REG_FINE_INT_TIME
, .val
= 882},
574 {.length
= MT9P012_16BIT
, .reg
= REG_FRAME_LEN_LINES
, .val
= 2056},
575 {.length
= MT9P012_16BIT
, .reg
= REG_LINE_LEN_PCK
, .val
= 5372},
576 {.length
= MT9P012_16BIT
, .reg
= REG_SCALE_M
, .val
= 0x0000},
578 {.length
= MT9P012_16BIT
, .reg
= REG_SCALING_MODE
, .val
= 0x0000},
579 {.length
= MT9P012_16BIT
, .reg
= REG_COARSE_INT_TIME
,
580 .val
= MT9P012_COARSE_INT_TIME_5MP
},
582 {.length
= MT9P012_8BIT
, .reg
= REG_GROUPED_PAR_HOLD
, .val
= 0x00},
583 {.length
= MT9P012_TOK_TERM
, .reg
= 0, .val
= 0}
586 static u32 min_exposure_time
;
587 static u32 max_exposure_time
;
588 static u32 pix_clk_freq
;
590 /* Structure to set frame rate */
591 static struct mt9p012_reg set_fps
[2];
594 * struct mt9p012_pll_settings - struct for storage of sensor pll values
595 * @vt_pix_clk_div: vertical pixel clock divider
596 * @vt_sys_clk_div: veritcal system clock divider
597 * @pre_pll_div: pre pll divider
598 * @fine_int_tm: fine resolution interval time
599 * @frame_lines: number of lines in frame
600 * @line_len: number of pixels in line
601 * @min_pll: minimum pll multiplier
602 * @max_pll: maximum pll multiplier
604 const static struct mt9p012_pll_settings all_pll_settings
[] = {
606 {.vt_pix_clk_div
= 4, .vt_sys_clk_div
= 1, .pre_pll_div
= 5,
607 .fine_int_tm
= 882, .frame_lines
= 2056, .line_len
= 5372,
608 .min_pll
= 160, .max_pll
= 200},
610 {.vt_pix_clk_div
= 4, .vt_sys_clk_div
= 1, .pre_pll_div
= 5,
611 .fine_int_tm
= 882, .frame_lines
= 2056, .line_len
= 5372,
612 .min_pll
= 160, .max_pll
= 200},
614 {.vt_pix_clk_div
= 5, .vt_sys_clk_div
= 2, .pre_pll_div
= 3,
615 .fine_int_tm
= 1794, .frame_lines
= 1061, .line_len
= 3360,
616 .min_pll
= 96, .max_pll
= 190},
618 {.vt_pix_clk_div
= 5, .vt_sys_clk_div
= 1, .pre_pll_div
= 3,
619 .fine_int_tm
= 1794, .frame_lines
= 1061, .line_len
= 3360,
620 .min_pll
= 96, .max_pll
= 150},
622 {.vt_pix_clk_div
= 8, .vt_sys_clk_div
= 2, .pre_pll_div
= 2,
623 .fine_int_tm
= 1794, .frame_lines
= 574, .line_len
= 2712,
624 .min_pll
= 92, .max_pll
= 128},
626 {.vt_pix_clk_div
= 5, .vt_sys_clk_div
= 2, .pre_pll_div
= 3,
627 .fine_int_tm
= 1794, .frame_lines
= 1374, .line_len
= 3712,
628 .min_pll
= 96, .max_pll
= 192},
630 {.vt_pix_clk_div
= 8, .vt_sys_clk_div
= 2, .pre_pll_div
= 2,
631 .fine_int_tm
= 1794, .frame_lines
= 574, .line_len
= 2712,
632 .min_pll
= 92, .max_pll
= 126},
634 {.vt_pix_clk_div
= 5, .vt_sys_clk_div
= 2, .pre_pll_div
= 3,
635 .fine_int_tm
= 1794, .frame_lines
= 1374, .line_len
= 3712,
636 .min_pll
= 96, .max_pll
= 192}
639 static enum mt9p012_pll_type current_pll_video
;
641 const static struct mt9p012_reg
642 *mt9p012_reg_init
[MT9P012_NUM_FPS
][MT9P012_NUM_IMAGE_SIZES
] = {
644 enter_video_216_15fps
,
645 enter_video_648_15fps
,
646 enter_video_1296_15fps
,
647 enter_image_mode_3MP_10fps
,
648 enter_image_mode_5MP_10fps
651 enter_video_216_30fps
,
652 enter_video_648_30fps
,
653 enter_video_1296_30fps
,
654 enter_image_mode_3MP_10fps
,
655 enter_image_mode_5MP_10fps
660 * struct vcontrol - Video controls
661 * @v4l2_queryctrl: V4L2 VIDIOC_QUERYCTRL ioctl structure
662 * @current_value: current value of this control
664 static struct vcontrol
{
665 struct v4l2_queryctrl qc
;
667 } video_control
[] = {
670 .id
= V4L2_CID_EXPOSURE
,
671 .type
= V4L2_CTRL_TYPE_INTEGER
,
673 .minimum
= MT9P012_DEF_MIN_EXPOSURE
,
674 .maximum
= MT9P012_DEF_MAX_EXPOSURE
,
675 .step
= MT9P012_EXPOSURE_STEP
,
676 .default_value
= MT9P012_DEF_EXPOSURE
,
678 .current_value
= MT9P012_DEF_EXPOSURE
,
683 .type
= V4L2_CTRL_TYPE_INTEGER
,
685 .minimum
= MT9P012_MIN_GAIN
,
686 .maximum
= MT9P012_MAX_GAIN
,
687 .step
= MT9P012_GAIN_STEP
,
688 .default_value
= MT9P012_DEF_GAIN
,
690 .current_value
= MT9P012_DEF_GAIN
,
695 * find_vctrl - Finds the requested ID in the video control structure array
696 * @id: ID of control to search the video control array for
698 * Returns the index of the requested ID from the control structure array
700 static int find_vctrl(int id
)
704 if (id
< V4L2_CID_BASE
)
707 for (i
= (ARRAY_SIZE(video_control
) - 1); i
>= 0; i
--)
708 if (video_control
[i
].qc
.id
== id
)
716 * mt9p012_read_reg - Read a value from a register in an mt9p012 sensor device
717 * @client: i2c driver client structure
718 * @data_length: length of data to be read
719 * @reg: register address / offset
720 * @val: stores the value that gets read
722 * Read a value from a register in an mt9p012 sensor device.
723 * The value is returned in 'val'.
724 * Returns zero if successful, or non-zero otherwise.
726 static int mt9p012_read_reg(struct i2c_client
*client
, u16 data_length
,
730 struct i2c_msg msg
[1];
731 unsigned char data
[4];
733 if (!client
->adapter
)
735 if (data_length
!= MT9P012_8BIT
&& data_length
!= MT9P012_16BIT
736 && data_length
!= MT9P012_32BIT
)
739 msg
->addr
= client
->addr
;
744 /* high byte goes out first */
745 data
[0] = (u8
) (reg
>> 8);;
746 data
[1] = (u8
) (reg
& 0xff);
747 err
= i2c_transfer(client
->adapter
, msg
, 1);
749 msg
->len
= data_length
;
750 msg
->flags
= I2C_M_RD
;
751 err
= i2c_transfer(client
->adapter
, msg
, 1);
755 /* high byte comes first */
756 if (data_length
== MT9P012_8BIT
)
758 else if (data_length
== MT9P012_16BIT
)
759 *val
= data
[1] + (data
[0] << 8);
761 *val
= data
[3] + (data
[2] << 8) +
762 (data
[1] << 16) + (data
[0] << 24);
765 v4l_dbg(1, debug
, client
, "read from offset 0x%x error %d", reg
, err
);
769 * mt9p012_write_reg - Write a value to a register in an mt9p012 sensor device
770 * @client: i2c driver client structure
771 * @data_length: length of data to be read
772 * @reg: register address / offset
773 * @val: value to be written to specified register
775 * Write a value to a register in an mt9p012 sensor device.
776 * Returns zero if successful, or non-zero otherwise.
778 static int mt9p012_write_reg(struct i2c_client
*client
, u16 data_length
,
782 struct i2c_msg msg
[1];
783 unsigned char data
[6];
786 if (!client
->adapter
)
789 if (data_length
!= MT9P012_8BIT
&& data_length
!= MT9P012_16BIT
790 && data_length
!= MT9P012_32BIT
)
794 msg
->addr
= client
->addr
;
796 msg
->len
= 2 + data_length
;
799 /* high byte goes out first */
800 data
[0] = (u8
) (reg
>> 8);;
801 data
[1] = (u8
) (reg
& 0xff);
803 if (data_length
== MT9P012_8BIT
)
804 data
[2] = (u8
) (val
& 0xff);
805 else if (data_length
== MT9P012_16BIT
) {
806 data
[2] = (u8
) (val
>> 8);
807 data
[3] = (u8
) (val
& 0xff);
809 data
[2] = (u8
) (val
>> 24);
810 data
[3] = (u8
) (val
>> 16);
811 data
[4] = (u8
) (val
>> 8);
812 data
[5] = (u8
) (val
& 0xff);
815 err
= i2c_transfer(client
->adapter
, msg
, 1);
819 v4l_dbg(1, debug
, client
, "wrote 0x%x to offset 0x%x error %d", val
,
821 if (retry
<= MT9P012_I2C_RETRY_COUNT
) {
822 v4l_warn(client
, "retry ... %d", retry
);
831 * mt9p012_write_regs - Initializes a list of MT9P012 registers
832 * @client: i2c driver client structure
833 * @reglist: list of registers to be written
835 * Initializes a list of MT9P012 registers. The list of registers is
836 * terminated by MT9P012_TOK_TERM.
838 static int mt9p012_write_regs(struct i2c_client
*client
,
839 const struct mt9p012_reg reglist
[])
842 const struct mt9p012_reg
*next
= reglist
;
844 for (; next
->length
!= MT9P012_TOK_TERM
; next
++) {
845 if (next
->length
== MT9P012_TOK_DELAY
) {
850 err
= mt9p012_write_reg(client
, next
->length
,
851 next
->reg
, next
->val
);
859 * mt9p012_calc_pll - Calculate PLL settings based on input image size
860 * @isize: enum value corresponding to image size
861 * @xclk: xclk value (calculate by mt9p012sensor_calc_xclk())
862 * @sensor: pointer to sensor device information structure
864 * Calculates sensor PLL related settings (scaler, fps, pll_multiplier,
865 * pix_clk_freq, min_exposure_time, max_exposure_time) based on input
866 * image size. It then applies the fps register settings based on
867 * these calculations.
869 static int mt9p012_calc_pll(enum mt9p012_image_size isize
, unsigned long xclk
,
870 struct mt9p012_sensor
*sensor
)
872 int err
= 0, row
= 1, i
= 0;
873 unsigned int vt_pix_clk
;
874 unsigned int pll_multiplier
;
875 unsigned int exposure_factor
, pix_clk_scaled
;
876 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
877 struct vcontrol
*lvc
;
879 /* Greater than 1296x972
882 3. Apply image mode settings
883 4. Turn Streaming ON.
886 if (isize
> MT9P012_BIN2X
) {
890 current_pll_video
= MT9P012_PLL_5MP
;
894 /* Greater than 648X486 case
896 2. If fps>21 then choose PLL for 30
897 3. If fps<21 then choose PLL for 15
899 Greater than 216X162 case
901 2. If fps>15 then choose PLL for 30
902 3. If fps<15 then choose PLL for 15
904 Greater than 0 to 216x162
906 2. If fps>15 then choose PLL for 30
907 3. If fps<15 then choose PLL for 15
910 if (isize
> MT9P012_BIN4X
) {
912 if (sensor
->fps
> 21)
913 current_pll_video
= MT9P012_PLL_1296_30FPS
;
915 current_pll_video
= MT9P012_PLL_1296_15FPS
;
916 } else if (isize
> MT9P012_BIN4XSCALE
) {
918 if (sensor
->fps
> 15)
919 current_pll_video
= MT9P012_PLL_648_30FPS
;
921 current_pll_video
= MT9P012_PLL_648_15FPS
;
924 if (sensor
->fps
> 15)
925 current_pll_video
= MT9P012_PLL_216_30FPS
;
927 current_pll_video
= MT9P012_PLL_216_15FPS
;
931 if (sensor
->scaler
&& (sensor
->fps
< 16))
932 row
= 2; /* Adjustment when using 4x binning and 12 MHz clk */
934 /* Calculate the PLL, set fps register */
935 vt_pix_clk
= sensor
->fps
*
936 all_pll_settings
[current_pll_video
].frame_lines
*
937 all_pll_settings
[current_pll_video
].line_len
;
941 * all_pll_settings
[current_pll_video
].vt_pix_clk_div
942 * all_pll_settings
[current_pll_video
].vt_sys_clk_div
944 * all_pll_settings
[current_pll_video
].pre_pll_div
) + 1;
946 if (pll_multiplier
< all_pll_settings
[current_pll_video
].min_pll
)
947 pll_multiplier
= all_pll_settings
[current_pll_video
].min_pll
;
948 else if (pll_multiplier
> all_pll_settings
[current_pll_video
].max_pll
)
949 pll_multiplier
= all_pll_settings
[current_pll_video
].max_pll
;
951 pix_clk_freq
= (xclk
/
952 (all_pll_settings
[current_pll_video
].pre_pll_div
953 * all_pll_settings
[current_pll_video
].vt_pix_clk_div
954 * all_pll_settings
[current_pll_video
].vt_sys_clk_div
955 * row
)) * pll_multiplier
;
956 min_exposure_time
= (all_pll_settings
[current_pll_video
].fine_int_tm
957 * 1000000 / pix_clk_freq
) + 1;
958 exposure_factor
= (all_pll_settings
[current_pll_video
].frame_lines
- 1)
959 * all_pll_settings
[current_pll_video
].line_len
;
960 exposure_factor
+= all_pll_settings
[current_pll_video
].fine_int_tm
;
961 exposure_factor
*= 100;
962 pix_clk_scaled
= pix_clk_freq
/ 100;
963 max_exposure_time
= (exposure_factor
/ pix_clk_scaled
) * 100;
965 /* Apply the fps settings */
966 set_fps
[0].length
= MT9P012_16BIT
;
967 set_fps
[0].reg
= REG_PLL_MULTIPLIER
;
968 set_fps
[0].val
= pll_multiplier
;
969 set_fps
[1].length
= MT9P012_TOK_TERM
;
973 /* Update min/max for query control */
974 i
= find_vctrl(V4L2_CID_EXPOSURE
);
976 lvc
= &video_control
[i
];
977 lvc
->qc
.minimum
= min_exposure_time
;
978 lvc
->qc
.maximum
= max_exposure_time
;
981 err
= mt9p012_write_regs(client
, set_fps
);
986 * mt9p012_calc_size - Find the best match for a requested image capture size
987 * @width: requested image width in pixels
988 * @height: requested image height in pixels
990 * Find the best match for a requested image capture size. The best match
991 * is chosen as the nearest match that has the same number or fewer pixels
992 * as the requested size, or the smallest image size if the requested size
993 * has fewer pixels than the smallest image.
995 static enum mt9p012_image_size
mt9p012_calc_size(unsigned int width
,
998 enum mt9p012_image_size isize
;
999 unsigned long pixels
= width
* height
;
1001 for (isize
= MT9P012_BIN4XSCALE
; isize
<= MT9P012_FIVE_MP
; isize
++) {
1002 if (mt9p012_sizes
[isize
].height
*
1003 mt9p012_sizes
[isize
].width
>= pixels
) {
1004 /* To improve image quality in VGA */
1005 if (pixels
> MT9P012_CIF_PIXELS
&&
1006 isize
== MT9P012_BIN4X
)
1007 isize
= MT9P012_BIN2X
;
1009 if ((pixels
> MT9P012_QQVGA_PIXELS
) &&
1010 (isize
== MT9P012_BIN4XSCALE
))
1011 isize
= MT9P012_BIN4X
;
1017 return MT9P012_FIVE_MP
;
1021 * mt9p012_find_isize - Find the best match for a requested image capture size
1022 * @width: requested image width in pixels
1023 * @height: requested image height in pixels
1025 * Find the best match for a requested image capture size. The best match
1026 * is chosen as the nearest match that has the same number or fewer pixels
1027 * as the requested size, or the smallest image size if the requested size
1028 * has fewer pixels than the smallest image.
1030 static enum mt9p012_image_size
mt9p012_find_isize(unsigned int width
)
1032 enum mt9p012_image_size isize
;
1034 for (isize
= MT9P012_BIN4XSCALE
; isize
<= MT9P012_FIVE_MP
; isize
++) {
1035 if (mt9p012_sizes
[isize
].width
>= width
)
1042 * mt9p012_find_fps_index - Find the best fps range match for a
1043 * requested frame rate
1044 * @fps: desired frame rate
1045 * @isize: enum value corresponding to image size
1047 * Find the best match for a requested frame rate. The best match
1048 * is chosen between two fps ranges (11 - 15 and 16 - 30 fps) depending on
1049 * the image size. For image sizes larger than BIN2X, frame rate is fixed
1052 static unsigned int mt9p012_find_fps_index(unsigned int fps
,
1053 enum mt9p012_image_size isize
)
1055 unsigned int index
= MT9P012_FPS_LOW_RANGE
;
1057 if (isize
> MT9P012_BIN4X
) {
1059 index
= MT9P012_FPS_HIGH_RANGE
;
1062 index
= MT9P012_FPS_HIGH_RANGE
;
1069 * mt9p012_calc_xclk - Calculate the required xclk frequency
1070 * @c: i2c client driver structure
1072 * Given the image capture format in pix, the nominal frame period in
1073 * timeperframe, calculate and return the required xclk frequency
1075 static unsigned long mt9p012_calc_xclk(struct i2c_client
*c
)
1077 struct mt9p012_sensor
*sensor
= i2c_get_clientdata(c
);
1078 struct v4l2_fract
*timeperframe
= &sensor
->timeperframe
;
1079 struct v4l2_pix_format
*pix
= &sensor
->pix
;
1081 if (timeperframe
->numerator
== 0 ||
1082 timeperframe
->denominator
== 0) {
1083 /* supply a default nominal_timeperframe */
1084 timeperframe
->numerator
= 1;
1085 timeperframe
->denominator
= MT9P012_DEF_FPS
;
1088 sensor
->fps
= timeperframe
->denominator
/ timeperframe
->numerator
;
1089 if (sensor
->fps
< MT9P012_MIN_FPS
)
1090 sensor
->fps
= MT9P012_MIN_FPS
;
1091 else if (sensor
->fps
> MT9P012_MAX_FPS
)
1092 sensor
->fps
= MT9P012_MAX_FPS
;
1094 timeperframe
->numerator
= 1;
1095 timeperframe
->denominator
= sensor
->fps
;
1097 if ((pix
->width
<= MT9P012_VIDEO_WIDTH_4X_BINN
) && (sensor
->fps
> 15))
1098 return MT9P012_XCLK_NOM_2
;
1100 return MT9P012_XCLK_NOM_1
;
1104 * mt9p012_configure - Configure the mt9p012 for the specified image mode
1105 * @s: pointer to standard V4L2 device structure
1107 * Configure the mt9p012 for a specified image size, pixel format, and frame
1108 * period. xclk is the frequency (in Hz) of the xclk input to the mt9p012.
1109 * fper is the frame period (in seconds) expressed as a fraction.
1110 * Returns zero if successful, or non-zero otherwise.
1111 * The actual frame period is returned in fper.
1113 static int mt9p012_configure(struct v4l2_int_device
*s
)
1115 struct mt9p012_sensor
*sensor
= s
->priv
;
1116 struct v4l2_pix_format
*pix
= &sensor
->pix
;
1117 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
1118 enum mt9p012_image_size isize
;
1119 unsigned int fps_index
;
1122 isize
= mt9p012_find_isize(pix
->width
);
1124 /* common register initialization */
1125 err
= mt9p012_write_regs(client
, mt9p012_common
);
1129 fps_index
= mt9p012_find_fps_index(sensor
->fps
, isize
);
1131 /* configure image size and pixel format */
1132 err
= mt9p012_write_regs(client
, mt9p012_reg_init
[fps_index
][isize
]);
1136 /* configure frame rate */
1137 err
= mt9p012_calc_pll(isize
, sensor
->xclk_current
, sensor
);
1141 /* configure streaming ON */
1142 err
= mt9p012_write_regs(client
, stream_on_list
);
1148 * mt9p012_detect - Detect if an mt9p012 is present, and if so which revision
1149 * @client: pointer to the i2c client driver structure
1151 * Detect if an mt9p012 is present, and if so which revision.
1152 * A device is considered to be detected if the manufacturer ID (MIDH and MIDL)
1153 * and the product ID (PID) registers match the expected values.
1154 * Any value of the version ID (VER) register is accepted.
1155 * Here are the version numbers we know about:
1156 * 0x48 --> mt9p012 Revision 1 or mt9p012 Revision 2
1157 * 0x49 --> mt9p012 Revision 3
1158 * Returns a negative error number if no device is detected, or the
1159 * non-negative value of the version ID register if a device is detected.
1161 static int mt9p012_detect(struct i2c_client
*client
)
1163 u32 model_id
, mfr_id
, rev
;
1168 if (mt9p012_read_reg(client
, MT9P012_16BIT
, REG_MODEL_ID
, &model_id
))
1170 if (mt9p012_read_reg(client
, MT9P012_8BIT
, REG_MANUFACTURER_ID
,
1173 if (mt9p012_read_reg(client
, MT9P012_8BIT
, REG_REVISION_NUMBER
, &rev
))
1176 dev_info(&client
->dev
, "model id detected 0x%x mfr 0x%x\n", model_id
,
1178 if ((model_id
!= MT9P012_MOD_ID
) || (mfr_id
!= MT9P012_MFR_ID
)) {
1179 /* We didn't read the values we expected, so
1180 * this must not be an MT9P012.
1182 dev_warn(&client
->dev
, "model id mismatch 0x%x mfr 0x%x\n",
1192 * mt9p012_set_exposure_time - sets exposure time per input value
1193 * @exp_time: exposure time to be set on device
1194 * @s: pointer to standard V4L2 device structure
1195 * @lvc: pointer to V4L2 exposure entry in video_controls array
1197 * If the requested exposure time is within the allowed limits, the HW
1198 * is configured to use the new exposure time, and the video_controls
1199 * array is updated with the new current value.
1200 * The function returns 0 upon success. Otherwise an error code is
1203 static int mt9p012_set_exposure_time(u32 exp_time
, struct v4l2_int_device
*s
,
1204 struct vcontrol
*lvc
)
1207 struct mt9p012_sensor
*sensor
= s
->priv
;
1208 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
1209 u32 coarse_int_time
= 0;
1211 if ((exp_time
< min_exposure_time
) ||
1212 (exp_time
> max_exposure_time
)) {
1213 dev_err(&client
->dev
, "Exposure time not within the "
1215 dev_err(&client
->dev
, "Min time %d us Max time %d us",
1216 min_exposure_time
, max_exposure_time
);
1219 coarse_int_time
= ((((exp_time
/ 10) * (pix_clk_freq
/ 1000)) / 1000) -
1220 (all_pll_settings
[current_pll_video
].fine_int_tm
/ 10)) /
1221 (all_pll_settings
[current_pll_video
].line_len
/ 10);
1223 dev_dbg(&client
->dev
, "coarse_int_time calculated = %d\n",
1226 set_exposure_time
[MT9P012_COARSE_INT_TIME_INDEX
].val
= coarse_int_time
;
1227 err
= mt9p012_write_regs(client
, set_exposure_time
);
1230 dev_err(&client
->dev
, "Error setting exposure time %d\n",
1233 lvc
->current_value
= exp_time
;
1239 * mt9p012_set_gain - sets sensor analog gain per input value
1240 * @gain: analog gain value to be set on device
1241 * @s: pointer to standard V4L2 device structure
1242 * @lvc: pointer to V4L2 analog gain entry in video_controls array
1244 * If the requested analog gain is within the allowed limits, the HW
1245 * is configured to use the new gain value, and the video_controls
1246 * array is updated with the new current value.
1247 * The function returns 0 upon success. Otherwise an error code is
1250 static int mt9p012_set_gain(u16 gain
, struct v4l2_int_device
*s
,
1251 struct vcontrol
*lvc
)
1254 struct mt9p012_sensor
*sensor
= s
->priv
;
1255 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
1257 if ((gain
< MT9P012_MIN_GAIN
) || (gain
> MT9P012_MAX_GAIN
)) {
1258 dev_err(&client
->dev
, "Gain not within the legal range");
1261 set_analog_gain
[MT9P012_GAIN_INDEX
].val
= gain
;
1262 err
= mt9p012_write_regs(client
, set_analog_gain
);
1264 dev_err(&client
->dev
, "Error setting gain.%d", err
);
1267 lvc
->current_value
= gain
;
1273 * ioctl_queryctrl - V4L2 sensor interface handler for VIDIOC_QUERYCTRL ioctl
1274 * @s: pointer to standard V4L2 device structure
1275 * @qc: standard V4L2 VIDIOC_QUERYCTRL ioctl structure
1277 * If the requested control is supported, returns the control information
1278 * from the video_control[] array. Otherwise, returns -EINVAL if the
1279 * control is not supported.
1281 static int ioctl_queryctrl(struct v4l2_int_device
*s
, struct v4l2_queryctrl
*qc
)
1285 i
= find_vctrl(qc
->id
);
1287 qc
->flags
= V4L2_CTRL_FLAG_DISABLED
;
1292 *qc
= video_control
[i
].qc
;
1297 * ioctl_g_ctrl - V4L2 sensor interface handler for VIDIOC_G_CTRL ioctl
1298 * @s: pointer to standard V4L2 device structure
1299 * @vc: standard V4L2 VIDIOC_G_CTRL ioctl structure
1301 * If the requested control is supported, returns the control's current
1302 * value from the video_control[] array. Otherwise, returns -EINVAL
1303 * if the control is not supported.
1305 static int ioctl_g_ctrl(struct v4l2_int_device
*s
, struct v4l2_control
*vc
)
1307 struct vcontrol
*lvc
;
1310 i
= find_vctrl(vc
->id
);
1313 lvc
= &video_control
[i
];
1316 case V4L2_CID_EXPOSURE
:
1317 vc
->value
= lvc
->current_value
;
1320 vc
->value
= lvc
->current_value
;
1328 * ioctl_s_ctrl - V4L2 sensor interface handler for VIDIOC_S_CTRL ioctl
1329 * @s: pointer to standard V4L2 device structure
1330 * @vc: standard V4L2 VIDIOC_S_CTRL ioctl structure
1332 * If the requested control is supported, sets the control's current
1333 * value in HW (and updates the video_control[] array). Otherwise,
1334 * returns -EINVAL if the control is not supported.
1336 static int ioctl_s_ctrl(struct v4l2_int_device
*s
, struct v4l2_control
*vc
)
1338 int retval
= -EINVAL
;
1340 struct vcontrol
*lvc
;
1342 i
= find_vctrl(vc
->id
);
1345 lvc
= &video_control
[i
];
1348 case V4L2_CID_EXPOSURE
:
1349 retval
= mt9p012_set_exposure_time(vc
->value
, s
, lvc
);
1352 retval
= mt9p012_set_gain(vc
->value
, s
, lvc
);
1360 * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
1361 * @s: pointer to standard V4L2 device structure
1362 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
1364 * Implement the VIDIOC_ENUM_FMT ioctl for the CAPTURE buffer type.
1366 static int ioctl_enum_fmt_cap(struct v4l2_int_device
*s
,
1367 struct v4l2_fmtdesc
*fmt
)
1369 int index
= fmt
->index
;
1370 enum v4l2_buf_type type
= fmt
->type
;
1372 memset(fmt
, 0, sizeof(*fmt
));
1376 switch (fmt
->type
) {
1377 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1378 if (index
>= NUM_CAPTURE_FORMATS
)
1385 fmt
->flags
= mt9p012_formats
[index
].flags
;
1386 strlcpy(fmt
->description
, mt9p012_formats
[index
].description
,
1387 sizeof(fmt
->description
));
1388 fmt
->pixelformat
= mt9p012_formats
[index
].pixelformat
;
1394 * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
1395 * @s: pointer to standard V4L2 device structure
1396 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
1398 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
1399 * ioctl is used to negotiate the image capture size and pixel format
1400 * without actually making it take effect.
1402 static int ioctl_try_fmt_cap(struct v4l2_int_device
*s
, struct v4l2_format
*f
)
1404 enum mt9p012_image_size isize
;
1406 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1407 struct mt9p012_sensor
*sensor
= s
->priv
;
1408 struct v4l2_pix_format
*pix2
= &sensor
->pix
;
1410 isize
= mt9p012_calc_size(pix
->width
, pix
->height
);
1412 pix
->width
= mt9p012_sizes
[isize
].width
;
1413 pix
->height
= mt9p012_sizes
[isize
].height
;
1414 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1415 if (pix
->pixelformat
== mt9p012_formats
[ifmt
].pixelformat
)
1418 if (ifmt
== NUM_CAPTURE_FORMATS
)
1420 pix
->pixelformat
= mt9p012_formats
[ifmt
].pixelformat
;
1421 pix
->field
= V4L2_FIELD_NONE
;
1422 pix
->bytesperline
= pix
->width
* 2;
1423 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
1425 switch (pix
->pixelformat
) {
1426 case V4L2_PIX_FMT_YUYV
:
1427 case V4L2_PIX_FMT_UYVY
:
1428 pix
->colorspace
= V4L2_COLORSPACE_JPEG
;
1430 case V4L2_PIX_FMT_RGB565
:
1431 case V4L2_PIX_FMT_RGB565X
:
1432 case V4L2_PIX_FMT_RGB555
:
1433 case V4L2_PIX_FMT_SGRBG10
:
1434 case V4L2_PIX_FMT_RGB555X
:
1436 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
1444 * ioctl_s_fmt_cap - V4L2 sensor interface handler for VIDIOC_S_FMT ioctl
1445 * @s: pointer to standard V4L2 device structure
1446 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1448 * If the requested format is supported, configures the HW to use that
1449 * format, returns error code if format not supported or HW can't be
1450 * correctly configured.
1452 static int ioctl_s_fmt_cap(struct v4l2_int_device
*s
, struct v4l2_format
*f
)
1454 struct mt9p012_sensor
*sensor
= s
->priv
;
1455 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1458 rval
= ioctl_try_fmt_cap(s
, f
);
1466 * ioctl_g_fmt_cap - V4L2 sensor interface handler for ioctl_g_fmt_cap
1467 * @s: pointer to standard V4L2 device structure
1468 * @f: pointer to standard V4L2 v4l2_format structure
1470 * Returns the sensor's current pixel format in the v4l2_format
1473 static int ioctl_g_fmt_cap(struct v4l2_int_device
*s
, struct v4l2_format
*f
)
1475 struct mt9p012_sensor
*sensor
= s
->priv
;
1476 f
->fmt
.pix
= sensor
->pix
;
1482 * ioctl_g_parm - V4L2 sensor interface handler for VIDIOC_G_PARM ioctl
1483 * @s: pointer to standard V4L2 device structure
1484 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1486 * Returns the sensor's video CAPTURE parameters.
1488 static int ioctl_g_parm(struct v4l2_int_device
*s
, struct v4l2_streamparm
*a
)
1490 struct mt9p012_sensor
*sensor
= s
->priv
;
1491 struct v4l2_captureparm
*cparm
= &a
->parm
.capture
;
1493 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1496 memset(a
, 0, sizeof(*a
));
1497 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1499 cparm
->capability
= V4L2_CAP_TIMEPERFRAME
;
1500 cparm
->timeperframe
= sensor
->timeperframe
;
1506 * ioctl_s_parm - V4L2 sensor interface handler for VIDIOC_S_PARM ioctl
1507 * @s: pointer to standard V4L2 device structure
1508 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1510 * Configures the sensor to use the input parameters, if possible. If
1511 * not possible, reverts to the old parameters and returns the
1512 * appropriate error code.
1514 static int ioctl_s_parm(struct v4l2_int_device
*s
, struct v4l2_streamparm
*a
)
1516 struct mt9p012_sensor
*sensor
= s
->priv
;
1517 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
1518 struct v4l2_fract
*timeperframe
= &a
->parm
.capture
.timeperframe
;
1520 sensor
->timeperframe
= *timeperframe
;
1521 sensor
->xclk_current
= mt9p012_calc_xclk(client
);
1522 *timeperframe
= sensor
->timeperframe
;
1528 * ioctl_g_priv - V4L2 sensor interface handler for vidioc_int_g_priv_num
1529 * @s: pointer to standard V4L2 device structure
1530 * @p: void pointer to hold sensor's private data address
1532 * Returns device's (sensor's) private data area address in p parameter
1534 static int ioctl_g_priv(struct v4l2_int_device
*s
, void *p
)
1536 struct mt9p012_sensor
*sensor
= s
->priv
;
1538 return sensor
->pdata
->priv_data_set(s
, p
);
1542 * ioctl_init - V4L2 sensor interface handler for VIDIOC_INT_INIT
1543 * @s: pointer to standard V4L2 device structure
1545 * Initialize the sensor device (call mt9p012_configure())
1547 static int ioctl_init(struct v4l2_int_device
*s
)
1553 * ioctl_dev_exit - V4L2 sensor interface handler for vidioc_int_dev_exit_num
1554 * @s: pointer to standard V4L2 device structure
1556 * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
1558 static int ioctl_dev_exit(struct v4l2_int_device
*s
)
1564 * ioctl_dev_init - V4L2 sensor interface handler for vidioc_int_dev_init_num
1565 * @s: pointer to standard V4L2 device structure
1567 * Initialise the device when slave attaches to the master. Returns 0 if
1568 * mt9p012 device could be found, otherwise returns appropriate error.
1570 static int ioctl_dev_init(struct v4l2_int_device
*s
)
1572 struct mt9p012_sensor
*sensor
= s
->priv
;
1573 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
1576 err
= mt9p012_detect(client
);
1578 dev_err(&client
->dev
, "Unable to detect sensor\n");
1579 sensor
->detected
= 0;
1582 sensor
->detected
= 1;
1584 dev_dbg(&client
->dev
, "Chip version 0x%02x detected\n", sensor
->ver
);
1589 * ioctl_enum_framesizes - V4L2 sensor if handler for vidioc_int_enum_framesizes
1590 * @s: pointer to standard V4L2 device structure
1591 * @frms: pointer to standard V4L2 framesizes enumeration structure
1593 * Returns possible framesizes depending on choosen pixel format
1595 static int ioctl_enum_framesizes(struct v4l2_int_device
*s
,
1596 struct v4l2_frmsizeenum
*frms
)
1600 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1601 if (frms
->pixel_format
== mt9p012_formats
[ifmt
].pixelformat
)
1604 /* Is requested pixelformat not found on sensor? */
1605 if (ifmt
== NUM_CAPTURE_FORMATS
)
1608 /* Do we already reached all discrete framesizes? */
1609 if (frms
->index
>= 5)
1612 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1613 frms
->discrete
.width
= mt9p012_sizes
[frms
->index
].width
;
1614 frms
->discrete
.height
= mt9p012_sizes
[frms
->index
].height
;
1619 const struct v4l2_fract mt9p012_frameintervals
[] = {
1620 { .numerator
= 1, .denominator
= 11 },
1621 { .numerator
= 1, .denominator
= 15 },
1622 { .numerator
= 1, .denominator
= 20 },
1623 { .numerator
= 1, .denominator
= 25 },
1624 { .numerator
= 1, .denominator
= 30 },
1627 static int ioctl_enum_frameintervals(struct v4l2_int_device
*s
,
1628 struct v4l2_frmivalenum
*frmi
)
1632 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1633 if (frmi
->pixel_format
== mt9p012_formats
[ifmt
].pixelformat
)
1636 /* Is requested pixelformat not found on sensor? */
1637 if (ifmt
== NUM_CAPTURE_FORMATS
)
1640 /* Do we already reached all discrete framesizes? */
1642 if (((frmi
->width
== mt9p012_sizes
[4].width
) &&
1643 (frmi
->height
== mt9p012_sizes
[4].height
)) ||
1644 ((frmi
->width
== mt9p012_sizes
[3].width
) &&
1645 (frmi
->height
== mt9p012_sizes
[3].height
))) {
1646 /* FIXME: The only frameinterval supported by 5MP and 3MP
1647 * capture sizes is 1/11 fps
1649 if (frmi
->index
!= 0)
1652 if (frmi
->index
>= 5)
1656 frmi
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1657 frmi
->discrete
.numerator
=
1658 mt9p012_frameintervals
[frmi
->index
].numerator
;
1659 frmi
->discrete
.denominator
=
1660 mt9p012_frameintervals
[frmi
->index
].denominator
;
1666 * ioctl_s_power - V4L2 sensor interface handler for vidioc_int_s_power_num
1667 * @s: pointer to standard V4L2 device structure
1668 * @on: power state to which device is to be set
1670 * Sets devices power state to requrested state, if possible.
1672 static int ioctl_s_power(struct v4l2_int_device
*s
, enum v4l2_power new_power
)
1674 struct mt9p012_sensor
*sensor
= s
->priv
;
1675 struct i2c_client
*c
= to_i2c_client(sensor
->dev
);
1678 switch (new_power
) {
1680 rval
= sensor
->pdata
->set_xclk(s
, sensor
->xclk_current
);
1681 if (rval
== -EINVAL
)
1683 rval
= sensor
->pdata
->power_set(s
, V4L2_POWER_ON
);
1687 if (sensor
->detected
)
1688 mt9p012_configure(s
);
1690 rval
= ioctl_dev_init(s
);
1695 case V4L2_POWER_OFF
:
1697 rval
= sensor
->pdata
->power_set(s
, V4L2_POWER_OFF
);
1698 sensor
->pdata
->set_xclk(s
, 0);
1700 case V4L2_POWER_STANDBY
:
1701 if (sensor
->detected
)
1702 mt9p012_write_regs(c
, stream_off_list
);
1703 rval
= sensor
->pdata
->power_set(s
, V4L2_POWER_STANDBY
);
1704 sensor
->pdata
->set_xclk(s
, 0);
1713 static struct v4l2_int_ioctl_desc mt9p012_ioctl_desc
[] = {
1714 { .num
= vidioc_int_enum_framesizes_num
,
1715 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_framesizes
},
1716 { .num
= vidioc_int_enum_frameintervals_num
,
1717 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_frameintervals
},
1718 { .num
= vidioc_int_dev_init_num
,
1719 .func
= (v4l2_int_ioctl_func
*)ioctl_dev_init
},
1720 { .num
= vidioc_int_dev_exit_num
,
1721 .func
= (v4l2_int_ioctl_func
*)ioctl_dev_exit
},
1722 { .num
= vidioc_int_s_power_num
,
1723 .func
= (v4l2_int_ioctl_func
*)ioctl_s_power
},
1724 { .num
= vidioc_int_g_priv_num
,
1725 .func
= (v4l2_int_ioctl_func
*)ioctl_g_priv
},
1726 { .num
= vidioc_int_init_num
,
1727 .func
= (v4l2_int_ioctl_func
*)ioctl_init
},
1728 { .num
= vidioc_int_enum_fmt_cap_num
,
1729 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_fmt_cap
},
1730 { .num
= vidioc_int_try_fmt_cap_num
,
1731 .func
= (v4l2_int_ioctl_func
*)ioctl_try_fmt_cap
},
1732 { .num
= vidioc_int_g_fmt_cap_num
,
1733 .func
= (v4l2_int_ioctl_func
*)ioctl_g_fmt_cap
},
1734 { .num
= vidioc_int_s_fmt_cap_num
,
1735 .func
= (v4l2_int_ioctl_func
*)ioctl_s_fmt_cap
},
1736 { .num
= vidioc_int_g_parm_num
,
1737 .func
= (v4l2_int_ioctl_func
*)ioctl_g_parm
},
1738 { .num
= vidioc_int_s_parm_num
,
1739 .func
= (v4l2_int_ioctl_func
*)ioctl_s_parm
},
1740 { .num
= vidioc_int_queryctrl_num
,
1741 .func
= (v4l2_int_ioctl_func
*)ioctl_queryctrl
},
1742 { .num
= vidioc_int_g_ctrl_num
,
1743 .func
= (v4l2_int_ioctl_func
*)ioctl_g_ctrl
},
1744 { .num
= vidioc_int_s_ctrl_num
,
1745 .func
= (v4l2_int_ioctl_func
*)ioctl_s_ctrl
},
1748 static struct v4l2_int_slave mt9p012_slave
= {
1749 .ioctls
= mt9p012_ioctl_desc
,
1750 .num_ioctls
= ARRAY_SIZE(mt9p012_ioctl_desc
),
1753 static struct v4l2_int_device mt9p012_int_device
= {
1754 .module
= THIS_MODULE
,
1755 .name
= DRIVER_NAME
,
1756 .type
= v4l2_int_type_slave
,
1758 .slave
= &mt9p012_slave
,
1763 * mt9p012_probe - sensor driver i2c probe handler
1764 * @client: i2c driver client device structure
1766 * Register sensor as an i2c client device and V4L2
1769 static int mt9p012_probe(struct i2c_client
*client
,
1770 const struct i2c_device_id
*id
)
1772 struct mt9p012_sensor
*sensor
;
1773 struct mt9p012_platform_data
*pdata
;
1776 if (i2c_get_clientdata(client
))
1779 pdata
= client
->dev
.platform_data
;
1781 dev_err(&client
->dev
, "no platform data?\n");
1785 sensor
= kzalloc(sizeof(*sensor
), GFP_KERNEL
);
1789 /* Don't keep pointer to platform data, copy elements instead */
1790 sensor
->pdata
= kzalloc(sizeof(*sensor
->pdata
), GFP_KERNEL
);
1791 if (!sensor
->pdata
) {
1796 sensor
->pdata
->power_set
= pdata
->power_set
;
1797 sensor
->pdata
->set_xclk
= pdata
->set_xclk
;
1798 sensor
->pdata
->priv_data_set
= pdata
->priv_data_set
;
1800 /* Set sensor default values */
1801 sensor
->timeperframe
.numerator
= 1;
1802 sensor
->timeperframe
.denominator
= 15;
1803 sensor
->xclk_current
= MT9P012_XCLK_NOM_1
;
1804 sensor
->pix
.width
= MT9P012_VIDEO_WIDTH_4X_BINN_SCALED
;
1805 sensor
->pix
.height
= MT9P012_VIDEO_WIDTH_4X_BINN_SCALED
;
1806 sensor
->pix
.pixelformat
= V4L2_PIX_FMT_SGRBG10
;
1808 sensor
->v4l2_int_device
= &mt9p012_int_device
;
1809 sensor
->v4l2_int_device
->priv
= sensor
;
1810 sensor
->dev
= &client
->dev
;
1812 i2c_set_clientdata(client
, sensor
);
1814 err
= v4l2_int_device_register(sensor
->v4l2_int_device
);
1821 i2c_set_clientdata(client
, NULL
);
1822 kfree(sensor
->pdata
);
1829 * mt9p012_remove - sensor driver i2c remove handler
1830 * @client: i2c driver client device structure
1832 * Unregister sensor as an i2c client device and V4L2
1833 * device. Complement of mt9p012_probe().
1835 static int mt9p012_remove(struct i2c_client
*client
)
1837 struct mt9p012_sensor
*sensor
= i2c_get_clientdata(client
);
1839 v4l2_int_device_unregister(sensor
->v4l2_int_device
);
1840 i2c_set_clientdata(client
, NULL
);
1841 kfree(sensor
->pdata
);
1847 static const struct i2c_device_id mt9p012_id
[] = {
1851 MODULE_DEVICE_TABLE(i2c
, mt9p012_id
);
1853 static struct i2c_driver mt9p012_i2c_driver
= {
1855 .name
= DRIVER_NAME
,
1856 .owner
= THIS_MODULE
,
1858 .probe
= mt9p012_probe
,
1859 .remove
= mt9p012_remove
,
1860 .id_table
= mt9p012_id
,
1864 * mt9p012sensor_init - sensor driver module_init handler
1866 * Registers driver as an i2c client driver. Returns 0 on success,
1867 * error code otherwise.
1869 static int __init
mt9p012_init(void)
1871 return i2c_add_driver(&mt9p012_i2c_driver
);
1873 module_init(mt9p012_init
);
1876 * mt9p012sensor_cleanup - sensor driver module_exit handler
1878 * Unregisters/deletes driver as an i2c client driver.
1879 * Complement of mt9p012sensor_init.
1881 static void __exit
mt9p012_cleanup(void)
1883 i2c_del_driver(&mt9p012_i2c_driver
);
1885 module_exit(mt9p012_cleanup
);
1887 MODULE_LICENSE("GPL");
1888 MODULE_DESCRIPTION("mt9p012 camera sensor driver");