2 * drivers/media/video/imx046.c
4 * Sony imx046 sensor driver
7 * Copyright (C) 2008 Hewlett Packard
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
19 #include <media/v4l2-int-device.h>
20 #include <media/imx046.h>
22 #include "imx046_regs.h"
23 #include "omap34xxcam.h"
25 #include "isp/ispcsi2.h"
28 #define IMX046_DRIVER_NAME "imx046"
29 #define IMX046_MOD_NAME "IMX046: "
34 /* from IMX046ES_registerSetting_I2C_MIPI_2lane_def_080925.xls */
35 const static struct imx046_reg initial_list
[] = {
36 {IMX046_REG_IMAGE_ORIENTATION
, 0x03, I2C_8BIT
},
37 {IMX046_REG_COARSE_INT_TIME
, 0x0120, I2C_16BIT
},
38 {IMX046_REG_ANALOG_GAIN_GLOBAL
, 0x80, I2C_16BIT
},
39 {0x300A, 0x80, I2C_8BIT
},
40 {IMX046_REG_Y_OPBADDR_START_DI
, 0x08, I2C_8BIT
},
41 {IMX046_REG_Y_OPBADDR_END_DI
, 0x37, I2C_8BIT
},
42 {IMX046_REG_CHCODE_OUTCHSINGLE
, 0x60, I2C_8BIT
},
43 {IMX046_REG_OUTIF
, 0x01, I2C_8BIT
},
44 {IMX046_REG_RGPOF_RGPOFV2
, 0x28, I2C_8BIT
},
45 {IMX046_REG_CPCKAUTOEN
, 0x00, I2C_8BIT
},
46 {IMX046_REG_RGCPVFB
, 0x60, I2C_8BIT
},
47 {IMX046_REG_RGAZPDRV
, 0x24, I2C_8BIT
},
48 {IMX046_REG_RGAZTEST
, 0x34, I2C_8BIT
},
49 {IMX046_REG_RGVSUNLV
, 0x3B, I2C_8BIT
},
50 {IMX046_REG_CLPOWER
, 0x30, I2C_8BIT
},
51 {IMX046_REG_CLPOWERSP
, 0x00, I2C_8BIT
},
52 {IMX046_REG_ACLDIRV_TVADDCLP
, 0x00, I2C_8BIT
},
53 {IMX046_REG_OPB_CTRL
, 0x0080, I2C_16BIT
},
54 {0x30AB, 0x1C, I2C_8BIT
},
55 {0x30B0, 0x32, I2C_8BIT
},
56 {0x30B2, 0x83, I2C_8BIT
},
57 {IMX046_REG_RAW10CH2V2P_LO
, 0xD8, I2C_8BIT
},
58 {IMX046_REG_RAW10CH2V2D_LO
, 0x17, I2C_8BIT
},
59 {IMX046_REG_COMP8CH1V2P_LO
, 0xCF, I2C_8BIT
},
60 {IMX046_REG_COMP8CH1V2D_LO
, 0xF1, I2C_8BIT
},
61 {IMX046_REG_RAW10CH1V2P_LO
, 0xD8, I2C_8BIT
},
62 {IMX046_REG_RAW10CH1V2D_LO
, 0x17, I2C_8BIT
},
63 {0x3302, 0x0A, I2C_8BIT
},
64 {0x3303, 0x09, I2C_8BIT
},
65 {IMX046_REG_RGTLPX
, 0x05, I2C_8BIT
},
66 {IMX046_REG_RGTCLKPREPARE
, 0x04, I2C_8BIT
},
67 {IMX046_REG_RGTCLKZERO
, 0x15, I2C_8BIT
},
68 {IMX046_REG_RGTCLKPRE
, 0x03, I2C_8BIT
},
69 {IMX046_REG_RGTCLKPOST
, 0x13, I2C_8BIT
},
70 {IMX046_REG_RGTCLKTRAIL
, 0x05, I2C_8BIT
},
71 {IMX046_REG_RGTHSEXIT
, 0x0B, I2C_8BIT
},
72 {0x302B, 0x38, I2C_8BIT
}, /* for 18Mhz xclk */
73 {I2C_REG_TERM
, I2C_VAL_TERM
, I2C_LEN_TERM
}
77 * struct imx046_sensor - main structure for storage of sensor information
78 * @pdata: access functions and data for platform level information
79 * @v4l2_int_device: V4L2 device structure structure
80 * @i2c_client: iic client device structure
81 * @pix: V4L2 pixel format information structure
82 * @timeperframe: time per frame expressed as V4L fraction
84 * @ver: imx046 chip version
85 * @fps: frames per second value
87 struct imx046_sensor
{
88 const struct imx046_platform_data
*pdata
;
89 struct v4l2_int_device
*v4l2_int_device
;
90 struct i2c_client
*i2c_client
;
91 struct v4l2_pix_format pix
;
92 struct v4l2_fract timeperframe
;
100 static struct imx046_sensor imx046
;
101 static struct i2c_driver imx046sensor_i2c_driver
;
102 static unsigned long xclk_current
= IMX046_XCLK_NOM_1
;
103 static enum imx046_image_size isize_current
= EIGHT_MP
;
105 /* list of image formats supported by imx046 sensor */
106 const static struct v4l2_fmtdesc imx046_formats
[] = {
108 .description
= "Bayer10 (RGr/GbB)",
109 .pixelformat
= V4L2_PIX_FMT_SRGGB10
,
113 #define NUM_CAPTURE_FORMATS ARRAY_SIZE(imx046_formats)
115 static u32 min_exposure_time
= 1000;
116 static u32 max_exposure_time
= 128000;
117 static enum v4l2_power current_power_state
;
119 /* Structure of Sensor settings that change with image size */
120 static struct imx046_sensor_settings sensor_settings
[] = {
121 /* NOTE: must be in same order as image_size array */
129 .vt_pix_clk_div
= 10,
136 .ths_settle_lower
= 8,
137 .ths_settle_upper
= 28,
140 .frame_len_lines_min
= 629,
141 .line_len_pck
= 3440,
146 .x_output_size
= 410,
147 .y_output_size
= 308,
164 .vt_pix_clk_div
= 10,
171 .ths_settle_lower
= 8,
172 .ths_settle_upper
= 28,
175 .frame_len_lines_min
= 629,
176 .line_len_pck
= 3440,
181 .x_output_size
= 820,
182 .y_output_size
= 616,
199 .vt_pix_clk_div
= 10,
206 .ths_settle_lower
= 13,
207 .ths_settle_upper
= 33,
210 .frame_len_lines_min
= 629,
211 .line_len_pck
= 3440,
216 .x_output_size
= 3280,
217 .y_output_size
= 618,
234 .vt_pix_clk_div
= 10,
241 .ths_settle_lower
= 13,
242 .ths_settle_upper
= 33,
245 .frame_len_lines_min
= 2510,
246 .line_len_pck
= 3440,
251 .x_output_size
= 3280,
252 .y_output_size
= 2464,
264 static struct imx046_clock_freq current_clk
;
267 struct i2c_msg
*reg_list
;
268 unsigned int list_size
;
272 * struct vcontrol - Video controls
273 * @v4l2_queryctrl: V4L2 VIDIOC_QUERYCTRL ioctl structure
274 * @current_value: current value of this control
276 static struct vcontrol
{
277 struct v4l2_queryctrl qc
;
279 } imx046sensor_video_control
[] = {
282 .id
= V4L2_CID_EXPOSURE
,
283 .type
= V4L2_CTRL_TYPE_INTEGER
,
285 .minimum
= IMX046_MIN_EXPOSURE
,
286 .maximum
= IMX046_MAX_EXPOSURE
,
287 .step
= IMX046_EXPOSURE_STEP
,
288 .default_value
= IMX046_DEF_EXPOSURE
,
290 .current_value
= IMX046_DEF_EXPOSURE
,
295 .type
= V4L2_CTRL_TYPE_INTEGER
,
296 .name
= "Analog Gain",
297 .minimum
= IMX046_EV_MIN_GAIN
,
298 .maximum
= IMX046_EV_MAX_GAIN
,
299 .step
= IMX046_EV_GAIN_STEP
,
300 .default_value
= IMX046_EV_DEF_GAIN
,
302 .current_value
= IMX046_EV_DEF_GAIN
,
306 .id
= V4L2_CID_TEST_PATTERN
,
307 .type
= V4L2_CTRL_TYPE_INTEGER
,
308 .name
= "Test Pattern",
309 .minimum
= IMX046_MIN_TEST_PATT_MODE
,
310 .maximum
= IMX046_MAX_TEST_PATT_MODE
,
311 .step
= IMX046_MODE_TEST_PATT_STEP
,
312 .default_value
= IMX046_MIN_TEST_PATT_MODE
,
314 .current_value
= IMX046_MIN_TEST_PATT_MODE
,
319 * find_vctrl - Finds the requested ID in the video control structure array
320 * @id: ID of control to search the video control array for
322 * Returns the index of the requested ID from the control structure array
329 if (id
< V4L2_CID_BASE
)
332 for (i
= (ARRAY_SIZE(imx046sensor_video_control
) - 1); i
>= 0; i
--)
333 if (imx046sensor_video_control
[i
].qc
.id
== id
)
341 * imx046_read_reg - Read a value from a register in an imx046 sensor device
342 * @client: i2c driver client structure
343 * @data_length: length of data to be read
344 * @reg: register address / offset
345 * @val: stores the value that gets read
347 * Read a value from a register in an imx046 sensor device.
348 * The value is returned in 'val'.
349 * Returns zero if successful, or non-zero otherwise.
352 imx046_read_reg(struct i2c_client
*client
, u16 data_length
, u16 reg
, u32
*val
)
355 struct i2c_msg msg
[1];
356 unsigned char data
[4];
358 if (!client
->adapter
)
360 if (data_length
!= I2C_8BIT
&& data_length
!= I2C_16BIT
361 && data_length
!= I2C_32BIT
)
364 msg
->addr
= client
->addr
;
369 /* Write addr - high byte goes out first */
370 data
[0] = (u8
) (reg
>> 8);;
371 data
[1] = (u8
) (reg
& 0xff);
372 err
= i2c_transfer(client
->adapter
, msg
, 1);
376 msg
->len
= data_length
;
377 msg
->flags
= I2C_M_RD
;
378 err
= i2c_transfer(client
->adapter
, msg
, 1);
382 /* high byte comes first */
383 if (data_length
== I2C_8BIT
)
385 else if (data_length
== I2C_16BIT
)
386 *val
= data
[1] + (data
[0] << 8);
388 *val
= data
[3] + (data
[2] << 8) +
389 (data
[1] << 16) + (data
[0] << 24);
392 v4l_err(client
, "read from offset 0x%x error %d", reg
, err
);
397 * Write a value to a register in imx046 sensor device.
398 * @client: i2c driver client structure.
399 * @reg: Address of the register to read value from.
400 * @val: Value to be written to a specific register.
401 * Returns zero if successful, or non-zero otherwise.
403 static int imx046_write_reg(struct i2c_client
*client
, u16 reg
,
404 u32 val
, u16 data_length
)
407 struct i2c_msg msg
[1];
408 unsigned char data
[6];
411 if (!client
->adapter
)
414 if (data_length
!= I2C_8BIT
&& data_length
!= I2C_16BIT
415 && data_length
!= I2C_32BIT
)
419 msg
->addr
= client
->addr
;
420 msg
->flags
= I2C_M_WR
;
421 msg
->len
= data_length
+2; /* add address bytes */
424 /* high byte goes out first */
425 data
[0] = (u8
) (reg
>> 8);
426 data
[1] = (u8
) (reg
& 0xff);
427 if (data_length
== I2C_8BIT
) {
428 data
[2] = val
& 0xff;
429 } else if (data_length
== I2C_16BIT
) {
430 data
[2] = (val
>> 8) & 0xff;
431 data
[3] = val
& 0xff;
433 data
[2] = (val
>> 24) & 0xff;
434 data
[3] = (val
>> 16) & 0xff;
435 data
[4] = (val
>> 8) & 0xff;
436 data
[5] = val
& 0xff;
439 if (data_length
== 1)
440 dev_dbg(&client
->dev
, "IMX046 Wrt:[0x%04X]=0x%02X\n",
442 else if (data_length
== 2)
443 dev_dbg(&client
->dev
, "IMX046 Wrt:[0x%04X]=0x%04X\n",
446 err
= i2c_transfer(client
->adapter
, msg
, 1);
452 v4l_info(client
, "Retrying I2C... %d", retries
);
462 * Initialize a list of imx046 registers.
463 * The list of registers is terminated by the pair of values
464 * {OV3640_REG_TERM, OV3640_VAL_TERM}.
465 * @client: i2c driver client structure.
466 * @reglist[]: List of address of the registers to write data.
467 * Returns zero if successful, or non-zero otherwise.
469 static int imx046_write_regs(struct i2c_client
*client
,
470 const struct imx046_reg reglist
[])
473 const struct imx046_reg
*list
= reglist
;
475 while (!((list
->reg
== I2C_REG_TERM
)
476 && (list
->val
== I2C_VAL_TERM
))) {
477 err
= imx046_write_reg(client
, list
->reg
,
478 list
->val
, list
->length
);
487 * imx046_find_size - Find the best match for a requested image capture size
488 * @width: requested image width in pixels
489 * @height: requested image height in pixels
491 * Find the best match for a requested image capture size. The best match
492 * is chosen as the nearest match that has the same number or fewer pixels
493 * as the requested size, or the smallest image size if the requested size
494 * has fewer pixels than the smallest image.
495 * Since the available sizes are subsampled in the vertical direction only,
496 * the routine will find the size with a height that is equal to or less
497 * than the requested height.
499 static enum imx046_image_size
imx046_find_size(unsigned int width
,
502 enum imx046_image_size isize
;
504 for (isize
= QUART_MP
; isize
<= EIGHT_MP
; isize
++) {
505 if ((imx046_sizes
[isize
].height
>= height
) &&
506 (imx046_sizes
[isize
].width
>= width
)) {
511 printk(KERN_DEBUG
"imx046_find_size: Req Size=%dx%d, "
513 width
, height
, (int)imx046_sizes
[isize
].width
,
514 (int)imx046_sizes
[isize
].height
);
520 * Set CSI2 Virtual ID.
521 * @client: i2c client driver structure
522 * @id: Virtual channel ID.
524 * Sets the channel ID which identifies data packets sent by this device
527 static int imx046_set_virtual_id(struct i2c_client
*client
, u32 id
)
529 return imx046_write_reg(client
, IMX046_REG_CCP2_CHANNEL_ID
,
530 (0x3 & id
), I2C_8BIT
);
534 * imx046_set_framerate - Sets framerate by adjusting frame_length_lines reg.
535 * @s: pointer to standard V4L2 device structure
536 * @fper: frame period numerator and denominator in seconds
538 * The maximum exposure time is also updated since it is affected by the
541 static int imx046_set_framerate(struct v4l2_int_device
*s
,
542 struct v4l2_fract
*fper
)
545 u16 isize
= isize_current
;
546 u32 frame_length_lines
, line_time_q8
;
547 struct imx046_sensor
*sensor
= s
->priv
;
548 struct i2c_client
*client
= sensor
->i2c_client
;
549 struct imx046_sensor_settings
*ss
;
551 if ((fper
->numerator
== 0) || (fper
->denominator
== 0)) {
552 /* supply a default nominal_timeperframe */
554 fper
->denominator
= IMX046_DEF_FPS
;
557 sensor
->fps
= fper
->denominator
/ fper
->numerator
;
558 if (sensor
->fps
< IMX046_MIN_FPS
) {
559 sensor
->fps
= IMX046_MIN_FPS
;
561 fper
->denominator
= sensor
->fps
;
562 } else if (sensor
->fps
> IMX046_MAX_FPS
) {
563 sensor
->fps
= IMX046_MAX_FPS
;
565 fper
->denominator
= sensor
->fps
;
568 ss
= &sensor_settings
[isize_current
];
570 line_time_q8
= ((u32
)ss
->frame
.line_len_pck
* 1000000) /
571 (current_clk
.vt_pix_clk
>> 8); /* usec's */
573 frame_length_lines
= (((u32
)fper
->numerator
* 1000000 * 256 /
574 fper
->denominator
)) / line_time_q8
;
576 /* Range check frame_length_lines */
577 if (frame_length_lines
> IMX046_MAX_FRAME_LENGTH_LINES
)
578 frame_length_lines
= IMX046_MAX_FRAME_LENGTH_LINES
;
579 else if (frame_length_lines
< ss
->frame
.frame_len_lines_min
)
580 frame_length_lines
= ss
->frame
.frame_len_lines_min
;
582 imx046_write_reg(client
, IMX046_REG_FRAME_LEN_LINES
,
583 frame_length_lines
, I2C_16BIT
);
585 sensor_settings
[isize
].frame
.frame_len_lines
= frame_length_lines
;
587 /* Update max exposure time */
588 max_exposure_time
= (line_time_q8
* (frame_length_lines
- 1)) >> 8;
590 printk(KERN_DEBUG
"IMX046 Set Framerate: fper=%d/%d, "
591 "frame_len_lines=%d, max_expT=%dus\n", fper
->numerator
,
592 fper
->denominator
, frame_length_lines
, max_exposure_time
);
598 * imx046sensor_calc_xclk - Calculate the required xclk frequency
600 * Xclk is not determined from framerate for the IMX046
602 static unsigned long imx046sensor_calc_xclk(void)
604 xclk_current
= IMX046_XCLK_NOM_1
;
610 * Sets the correct orientation based on the sensor version.
611 * IU046F2-Z version=2 orientation=3
612 * IU046F4-2D version>2 orientation=0
614 static int imx046_set_orientation(struct i2c_client
*client
, u32 ver
)
619 orient
= (ver
<= 0x2) ? 0x3 : 0x0;
620 err
= imx046_write_reg(client
, IMX046_REG_IMAGE_ORIENTATION
,
626 * imx046sensor_set_exposure_time - sets exposure time per input value
627 * @exp_time: exposure time to be set on device (in usec)
628 * @s: pointer to standard V4L2 device structure
629 * @lvc: pointer to V4L2 exposure entry in imx046sensor_video_controls array
631 * If the requested exposure time is within the allowed limits, the HW
632 * is configured to use the new exposure time, and the
633 * imx046sensor_video_control[] array is updated with the new current value.
634 * The function returns 0 upon success. Otherwise an error code is
637 int imx046sensor_set_exposure_time(u32 exp_time
, struct v4l2_int_device
*s
,
638 struct vcontrol
*lvc
)
641 struct imx046_sensor
*sensor
= s
->priv
;
642 struct i2c_client
*client
= sensor
->i2c_client
;
643 u16 coarse_int_time
= 0;
644 u32 line_time_q8
= 0;
645 struct imx046_sensor_settings
*ss
;
647 if ((current_power_state
== V4L2_POWER_ON
) || sensor
->resuming
) {
648 if (exp_time
< min_exposure_time
) {
649 v4l_err(client
, "Exposure time %d us not within"
650 " the legal range.\n", exp_time
);
651 v4l_err(client
, "Exposure time must be between"
652 " %d us and %d us\n",
653 min_exposure_time
, max_exposure_time
);
654 exp_time
= min_exposure_time
;
657 if (exp_time
> max_exposure_time
) {
658 v4l_err(client
, "Exposure time %d us not within"
659 " the legal range.\n", exp_time
);
660 v4l_err(client
, "Exposure time must be between"
661 " %d us and %d us\n",
662 min_exposure_time
, max_exposure_time
);
663 exp_time
= max_exposure_time
;
666 ss
= &sensor_settings
[isize_current
];
669 ((u32
)ss
->frame
.line_len_pck
* 1000000) /
670 (current_clk
.vt_pix_clk
>> 8); /* usec's */
672 coarse_int_time
= ((exp_time
* 256) + (line_time_q8
>> 1)) /
675 if (coarse_int_time
> ss
->frame
.frame_len_lines
- 2)
676 coarse_int_time
= ss
->frame
.frame_len_lines
- 2;
678 err
= imx046_write_reg(client
, IMX046_REG_COARSE_INT_TIME
,
679 coarse_int_time
, I2C_16BIT
);
683 v4l_err(client
, "Error setting exposure time: %d", err
);
685 i
= find_vctrl(V4L2_CID_EXPOSURE
);
687 lvc
= &imx046sensor_video_control
[i
];
688 lvc
->current_value
= exp_time
;
696 * This table describes what should be written to the sensor register for each
697 * gain value. The gain(index in the table) is in terms of 0.1EV, i.e. 10
698 * indexes in the table give 2 time more gain
700 * Elements in TS2_8_GAIN_TBL doesn't comply linearity. This is because
701 * there is nonlinear dependecy between analogue_gain_code_global and real gain
702 * value: Gain_analog = 256 / (256 - analogue_gain_code_global)
705 const u16 IMX046_EV_GAIN_TBL
[IMX046_EV_TABLE_GAIN_MAX
+ 1] = {
726 * imx046sensor_set_gain - sets sensor analog gain per input value
727 * @gain: analog gain value to be set on device
728 * @s: pointer to standard V4L2 device structure
729 * @lvc: pointer to V4L2 analog gain entry in imx046sensor_video_control array
731 * If the requested analog gain is within the allowed limits, the HW
732 * is configured to use the new gain value, and the imx046sensor_video_control
733 * array is updated with the new current value.
734 * The function returns 0 upon success. Otherwise an error code is
737 int imx046sensor_set_gain(u16 lineargain
, struct v4l2_int_device
*s
,
738 struct vcontrol
*lvc
)
742 struct imx046_sensor
*sensor
= s
->priv
;
743 struct i2c_client
*client
= sensor
->i2c_client
;
745 if (current_power_state
== V4L2_POWER_ON
|| sensor
->resuming
) {
747 if (lineargain
< IMX046_EV_MIN_GAIN
) {
748 lineargain
= IMX046_EV_MIN_GAIN
;
749 v4l_err(client
, "Gain out of legal range.");
751 if (lineargain
> IMX046_EV_MAX_GAIN
) {
752 lineargain
= IMX046_EV_MAX_GAIN
;
753 v4l_err(client
, "Gain out of legal range.");
756 reg_gain
= IMX046_EV_GAIN_TBL
[lineargain
];
758 err
= imx046_write_reg(client
, IMX046_REG_ANALOG_GAIN_GLOBAL
,
759 reg_gain
, I2C_16BIT
);
763 v4l_err(client
, "Error setting analog gain: %d", err
);
765 i
= find_vctrl(V4L2_CID_GAIN
);
767 lvc
= &imx046sensor_video_control
[i
];
768 lvc
->current_value
= lineargain
;
776 * imx046_update_clocks - calcs sensor clocks based on sensor settings.
777 * @isize: image size enum
779 int imx046_update_clocks(u32 xclk
, enum imx046_image_size isize
)
781 current_clk
.vco_clk
=
782 xclk
* sensor_settings
[isize
].clk
.pll_mult
/
783 sensor_settings
[isize
].clk
.pre_pll_div
/
784 sensor_settings
[isize
].clk
.post_pll_div
;
786 current_clk
.vt_pix_clk
= current_clk
.vco_clk
* 2 /
787 (sensor_settings
[isize
].clk
.vt_pix_clk_div
*
788 sensor_settings
[isize
].clk
.vt_sys_clk_div
);
790 if (sensor_settings
[isize
].mipi
.data_lanes
== 2)
791 current_clk
.mipi_clk
= current_clk
.vco_clk
;
793 current_clk
.mipi_clk
= current_clk
.vco_clk
/ 2;
795 current_clk
.ddr_clk
= current_clk
.mipi_clk
/ 2;
797 printk(KERN_DEBUG
"IMX046: xclk=%u, vco_clk=%u, "
798 "vt_pix_clk=%u, mipi_clk=%u, ddr_clk=%u\n",
799 xclk
, current_clk
.vco_clk
, current_clk
.vt_pix_clk
,
800 current_clk
.mipi_clk
, current_clk
.ddr_clk
);
806 * imx046_setup_pll - initializes sensor PLL registers.
807 * @c: i2c client driver structure
808 * @isize: image size enum
810 int imx046_setup_pll(struct i2c_client
*client
, enum imx046_image_size isize
)
813 u32 rgpltd
[3] = {2, 0, 1};
815 imx046_write_reg(client
, IMX046_REG_PRE_PLL_CLK_DIV
,
816 sensor_settings
[isize
].clk
.pre_pll_div
, I2C_16BIT
);
818 imx046_write_reg(client
, IMX046_REG_PLL_MULTIPLIER
,
819 sensor_settings
[isize
].clk
.pll_mult
, I2C_16BIT
);
821 imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_RGPLTD_RGCLKEN
,
823 rgpltd_reg
&= ~RGPLTD_MASK
;
824 rgpltd_reg
|= rgpltd
[sensor_settings
[isize
].clk
.post_pll_div
>> 1];
825 imx046_write_reg(client
, IMX046_REG_RGPLTD_RGCLKEN
,
826 rgpltd_reg
, I2C_8BIT
);
828 imx046_write_reg(client
, IMX046_REG_VT_PIX_CLK_DIV
,
829 sensor_settings
[isize
].clk
.vt_pix_clk_div
, I2C_16BIT
);
831 imx046_write_reg(client
, IMX046_REG_VT_SYS_CLK_DIV
,
832 sensor_settings
[isize
].clk
.vt_sys_clk_div
, I2C_16BIT
);
834 printk(KERN_DEBUG
"IMX046: pre_pll_clk_div=%u, pll_mult=%u, "
835 "rgpltd=0x%x, vt_pix_clk_div=%u, vt_sys_clk_div=%u\n",
836 sensor_settings
[isize
].clk
.pre_pll_div
,
837 sensor_settings
[isize
].clk
.pll_mult
, rgpltd_reg
,
838 sensor_settings
[isize
].clk
.vt_pix_clk_div
,
839 sensor_settings
[isize
].clk
.vt_sys_clk_div
);
845 * imx046_setup_mipi - initializes sensor & isp MIPI registers.
846 * @c: i2c client driver structure
847 * @isize: image size enum
849 int imx046_setup_mipi(struct imx046_sensor
*sensor
,
850 enum imx046_image_size isize
)
852 struct i2c_client
*client
= sensor
->i2c_client
;
854 /* NOTE: Make sure imx046_update_clocks is called 1st */
857 imx046_write_reg(client
, IMX046_REG_RGOUTSEL1
, 0x00, I2C_8BIT
);
858 imx046_write_reg(client
, IMX046_REG_TESTDI
, 0x04, I2C_8BIT
);
860 /* Set sensor Mipi timing params */
861 imx046_write_reg(client
, IMX046_REG_RGTHSTRAIL
, 0x06, I2C_8BIT
);
863 imx046_write_reg(client
, IMX046_REG_RGTHSPREPARE
,
864 sensor_settings
[isize
].mipi
.ths_prepare
, I2C_8BIT
);
866 imx046_write_reg(client
, IMX046_REG_RGTHSZERO
,
867 sensor_settings
[isize
].mipi
.ths_zero
, I2C_8BIT
);
869 /* Set number of lanes in sensor */
870 if (sensor_settings
[isize
].mipi
.data_lanes
== 2)
871 imx046_write_reg(client
, IMX046_REG_RGLANESEL
, 0x00, I2C_8BIT
);
873 imx046_write_reg(client
, IMX046_REG_RGLANESEL
, 0x01, I2C_8BIT
);
875 /* Set number of lanes in isp */
876 sensor
->pdata
->csi2_lane_count(sensor_settings
[isize
].mipi
.data_lanes
);
878 /* Send settings to ISP-CSI2 Receiver PHY */
879 sensor
->pdata
->csi2_calc_phy_cfg0(current_clk
.mipi_clk
,
880 sensor_settings
[isize
].mipi
.ths_settle_lower
,
881 sensor_settings
[isize
].mipi
.ths_settle_upper
);
883 /* Dump some registers for debug purposes */
884 printk(KERN_DEBUG
"imx:THSPREPARE=0x%02X\n",
885 sensor_settings
[isize
].mipi
.ths_prepare
);
886 printk(KERN_DEBUG
"imx:THSZERO=0x%02X\n",
887 sensor_settings
[isize
].mipi
.ths_zero
);
888 printk(KERN_DEBUG
"imx:LANESEL=0x%02X\n",
889 (sensor_settings
[isize
].mipi
.data_lanes
== 2) ? 0 : 1);
895 * imx046_configure_frame - initializes image frame registers
896 * @c: i2c client driver structure
897 * @isize: image size enum
899 int imx046_configure_frame(struct i2c_client
*client
,
900 enum imx046_image_size isize
)
904 imx046_write_reg(client
, IMX046_REG_FRAME_LEN_LINES
,
905 sensor_settings
[isize
].frame
.frame_len_lines_min
, I2C_16BIT
);
907 imx046_write_reg(client
, IMX046_REG_LINE_LEN_PCK
,
908 sensor_settings
[isize
].frame
.line_len_pck
, I2C_16BIT
);
910 imx046_write_reg(client
, IMX046_REG_X_ADDR_START
,
911 sensor_settings
[isize
].frame
.x_addr_start
, I2C_16BIT
);
913 imx046_write_reg(client
, IMX046_REG_X_ADDR_END
,
914 sensor_settings
[isize
].frame
.x_addr_end
, I2C_16BIT
);
916 imx046_write_reg(client
, IMX046_REG_Y_ADDR_START
,
917 sensor_settings
[isize
].frame
.y_addr_start
, I2C_16BIT
);
919 imx046_write_reg(client
, IMX046_REG_Y_ADDR_END
,
920 sensor_settings
[isize
].frame
.y_addr_end
, I2C_16BIT
);
922 imx046_write_reg(client
, IMX046_REG_X_OUTPUT_SIZE
,
923 sensor_settings
[isize
].frame
.x_output_size
, I2C_16BIT
);
925 imx046_write_reg(client
, IMX046_REG_Y_OUTPUT_SIZE
,
926 sensor_settings
[isize
].frame
.y_output_size
, I2C_16BIT
);
928 imx046_write_reg(client
, IMX046_REG_X_EVEN_INC
,
929 sensor_settings
[isize
].frame
.x_even_inc
, I2C_16BIT
);
931 imx046_write_reg(client
, IMX046_REG_X_ODD_INC
,
932 sensor_settings
[isize
].frame
.x_odd_inc
, I2C_16BIT
);
934 imx046_write_reg(client
, IMX046_REG_Y_EVEN_INC
,
935 sensor_settings
[isize
].frame
.y_even_inc
, I2C_16BIT
);
937 imx046_write_reg(client
, IMX046_REG_Y_ODD_INC
,
938 sensor_settings
[isize
].frame
.y_odd_inc
, I2C_16BIT
);
940 imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_PGACUR_VMODEADD
, &val
);
941 val
&= ~VMODEADD_MASK
;
942 val
|= sensor_settings
[isize
].frame
.v_mode_add
<< VMODEADD_SHIFT
;
943 imx046_write_reg(client
, IMX046_REG_PGACUR_VMODEADD
, val
, I2C_8BIT
);
945 imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_HMODEADD
, &val
);
946 val
&= ~HMODEADD_MASK
;
947 val
|= sensor_settings
[isize
].frame
.h_mode_add
<< HMODEADD_SHIFT
;
948 imx046_write_reg(client
, IMX046_REG_HMODEADD
, val
, I2C_8BIT
);
950 imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_HADDAVE
, &val
);
951 val
&= ~HADDAVE_MASK
;
952 val
|= sensor_settings
[isize
].frame
.h_add_ave
<< HADDAVE_SHIFT
;
953 imx046_write_reg(client
, IMX046_REG_HADDAVE
, val
, I2C_8BIT
);
959 * imx046_configure_test_pattern - Configure 3 possible test pattern modes
960 * @ mode: Test pattern mode. Possible modes : 1 , 2 and 4.
961 * @s: pointer to standard V4L2 device structure
962 * @lvc: pointer to V4L2 exposure entry in imx046sensor_video_controls array
964 * If the requested test pattern mode is within the allowed limits, the HW
965 * is configured for that particular test pattern, and the
966 * imx046sensor_video_control[] array is updated with the new current value.
967 * The function returns 0 upon success. Otherwise an error code is
970 int imx046_configure_test_pattern(int mode
, struct v4l2_int_device
*s
,
971 struct vcontrol
*lvc
)
973 struct imx046_sensor
*sensor
= s
->priv
;
974 struct i2c_client
*client
= sensor
->i2c_client
;
976 if ((current_power_state
== V4L2_POWER_ON
) || sensor
->resuming
) {
979 case IMX046_TEST_PATT_COLOR_BAR
:
980 case IMX046_TEST_PATT_PN9
:
982 imx046_write_reg(client
, IMX046_REG_TEST_PATT_RED
,
985 imx046_write_reg(client
, IMX046_REG_TEST_PATT_GREENR
,
988 imx046_write_reg(client
, IMX046_REG_TEST_PATT_BLUE
,
991 imx046_write_reg(client
, IMX046_REG_TEST_PATT_GREENB
,
994 case IMX046_TEST_PATT_SOLID_COLOR
:
996 imx046_write_reg(client
, IMX046_REG_TEST_PATT_RED
,
997 (IMX046_BLACK_LEVEL_AVG
& 0x00ff), I2C_16BIT
);
999 imx046_write_reg(client
, IMX046_REG_TEST_PATT_GREENR
,
1000 (IMX046_BLACK_LEVEL_AVG
& 0x00ff), I2C_16BIT
);
1002 imx046_write_reg(client
, IMX046_REG_TEST_PATT_BLUE
,
1003 (IMX046_BLACK_LEVEL_AVG
& 0x00ff), I2C_16BIT
);
1005 imx046_write_reg(client
, IMX046_REG_TEST_PATT_GREENB
,
1006 (IMX046_BLACK_LEVEL_AVG
& 0x00ff), I2C_16BIT
);
1009 /* test-pattern mode */
1010 imx046_write_reg(client
, IMX046_REG_TEST_PATT_MODE
,
1011 (mode
& 0x7), I2C_16BIT
);
1012 /* Disable sensor ISP processing */
1013 imx046_write_reg(client
, IMX046_REG_TESBYPEN
,
1014 (mode
== 0) ? 0x0 : 0x10, I2C_8BIT
);
1016 lvc
->current_value
= mode
;
1020 * imx046_configure - Configure the imx046 for the specified image mode
1021 * @s: pointer to standard V4L2 device structure
1023 * Configure the imx046 for a specified image size, pixel format, and frame
1024 * period. xclk is the frequency (in Hz) of the xclk input to the imx046.
1025 * fper is the frame period (in seconds) expressed as a fraction.
1026 * Returns zero if successful, or non-zero otherwise.
1027 * The actual frame period is returned in fper.
1029 static int imx046_configure(struct v4l2_int_device
*s
)
1031 struct imx046_sensor
*sensor
= s
->priv
;
1032 struct v4l2_pix_format
*pix
= &sensor
->pix
;
1033 struct i2c_client
*client
= sensor
->i2c_client
;
1034 enum imx046_image_size isize
;
1036 struct vcontrol
*lvc
= NULL
;
1038 isize
= imx046_find_size(pix
->width
, pix
->height
);
1039 isize_current
= isize
;
1041 err
= imx046_write_reg(client
, IMX046_REG_SW_RESET
, 0x01, I2C_8BIT
);
1044 imx046_write_regs(client
, initial_list
);
1046 imx046_update_clocks(xclk_current
, isize
);
1047 imx046_setup_pll(client
, isize
);
1049 imx046_setup_mipi(sensor
, isize
);
1051 /* configure image size and pixel format */
1052 imx046_configure_frame(client
, isize
);
1054 /* Setting of frame rate */
1055 err
= imx046_set_framerate(s
, &sensor
->timeperframe
);
1057 imx046_set_orientation(client
, sensor
->ver
);
1059 sensor
->pdata
->csi2_cfg_vp_out_ctrl(2);
1060 sensor
->pdata
->csi2_ctrl_update(false);
1062 sensor
->pdata
->csi2_cfg_virtual_id(0, IMX046_CSI2_VIRTUAL_ID
);
1063 sensor
->pdata
->csi2_ctx_update(0, false);
1064 imx046_set_virtual_id(client
, IMX046_CSI2_VIRTUAL_ID
);
1066 /* Set initial exposure and gain */
1067 i
= find_vctrl(V4L2_CID_EXPOSURE
);
1069 lvc
= &imx046sensor_video_control
[i
];
1070 imx046sensor_set_exposure_time(lvc
->current_value
,
1071 sensor
->v4l2_int_device
, lvc
);
1074 i
= find_vctrl(V4L2_CID_GAIN
);
1076 lvc
= &imx046sensor_video_control
[i
];
1077 imx046sensor_set_gain(lvc
->current_value
,
1078 sensor
->v4l2_int_device
, lvc
);
1081 i
= find_vctrl(V4L2_CID_TEST_PATTERN
);
1083 lvc
= &imx046sensor_video_control
[i
];
1084 imx046_configure_test_pattern(lvc
->current_value
,
1085 sensor
->v4l2_int_device
, lvc
);
1087 /* configure streaming ON */
1088 err
= imx046_write_reg(client
, IMX046_REG_MODE_SELECT
, 0x01, I2C_8BIT
);
1095 * imx046_detect - Detect if an imx046 is present, and if so which revision
1096 * @client: pointer to the i2c client driver structure
1098 * Detect if an imx046 is present, and if so which revision.
1099 * A device is considered to be detected if the manufacturer ID (MIDH and MIDL)
1100 * and the product ID (PID) registers match the expected values.
1101 * Any value of the version ID (VER) register is accepted.
1102 * Returns a negative error number if no device is detected, or the
1103 * non-negative value of the version ID register if a device is detected.
1106 imx046_detect(struct i2c_client
*client
)
1108 u32 model_id
, mfr_id
, rev
;
1109 struct imx046_sensor
*sensor
;
1114 sensor
= i2c_get_clientdata(client
);
1116 if (imx046_read_reg(client
, I2C_16BIT
, IMX046_REG_MODEL_ID
, &model_id
))
1118 if (imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_MFR_ID
, &mfr_id
))
1120 if (imx046_read_reg(client
, I2C_8BIT
, IMX046_REG_REV_NUMBER
, &rev
))
1123 v4l_info(client
, "model id detected 0x%x mfr 0x%x, rev# 0x%x\n",
1124 model_id
, mfr_id
, rev
);
1125 if ((model_id
!= IMX046_MOD_ID
) || (mfr_id
!= IMX046_MFR_ID
)) {
1126 /* We didn't read the values we expected, so
1127 * this must not be an IMX046.
1129 v4l_warn(client
, "model id mismatch 0x%x mfr 0x%x\n",
1138 * ioctl_queryctrl - V4L2 sensor interface handler for VIDIOC_QUERYCTRL ioctl
1139 * @s: pointer to standard V4L2 device structure
1140 * @qc: standard V4L2 VIDIOC_QUERYCTRL ioctl structure
1142 * If the requested control is supported, returns the control information
1143 * from the imx046sensor_video_control[] array.
1144 * Otherwise, returns -EINVAL if the control is not supported.
1146 static int ioctl_queryctrl(struct v4l2_int_device
*s
,
1147 struct v4l2_queryctrl
*qc
)
1151 i
= find_vctrl(qc
->id
);
1153 qc
->flags
= V4L2_CTRL_FLAG_DISABLED
;
1158 *qc
= imx046sensor_video_control
[i
].qc
;
1163 * ioctl_g_ctrl - V4L2 sensor interface handler for VIDIOC_G_CTRL ioctl
1164 * @s: pointer to standard V4L2 device structure
1165 * @vc: standard V4L2 VIDIOC_G_CTRL ioctl structure
1167 * If the requested control is supported, returns the control's current
1168 * value from the imx046sensor_video_control[] array.
1169 * Otherwise, returns -EINVAL if the control is not supported.
1171 static int ioctl_g_ctrl(struct v4l2_int_device
*s
,
1172 struct v4l2_control
*vc
)
1174 struct vcontrol
*lvc
;
1177 i
= find_vctrl(vc
->id
);
1180 lvc
= &imx046sensor_video_control
[i
];
1183 case V4L2_CID_EXPOSURE
:
1184 vc
->value
= lvc
->current_value
;
1187 vc
->value
= lvc
->current_value
;
1189 case V4L2_CID_TEST_PATTERN
:
1190 vc
->value
= lvc
->current_value
;
1198 * ioctl_s_ctrl - V4L2 sensor interface handler for VIDIOC_S_CTRL ioctl
1199 * @s: pointer to standard V4L2 device structure
1200 * @vc: standard V4L2 VIDIOC_S_CTRL ioctl structure
1202 * If the requested control is supported, sets the control's current
1203 * value in HW (and updates the imx046sensor_video_control[] array).
1204 * Otherwise, * returns -EINVAL if the control is not supported.
1206 static int ioctl_s_ctrl(struct v4l2_int_device
*s
,
1207 struct v4l2_control
*vc
)
1209 int retval
= -EINVAL
;
1211 struct vcontrol
*lvc
;
1213 i
= find_vctrl(vc
->id
);
1216 lvc
= &imx046sensor_video_control
[i
];
1219 case V4L2_CID_EXPOSURE
:
1220 retval
= imx046sensor_set_exposure_time(vc
->value
, s
, lvc
);
1223 retval
= imx046sensor_set_gain(vc
->value
, s
, lvc
);
1225 case V4L2_CID_TEST_PATTERN
:
1226 retval
= imx046_configure_test_pattern(vc
->value
, s
, lvc
);
1234 * ioctl_enum_fmt_cap - Implement the CAPTURE buffer VIDIOC_ENUM_FMT ioctl
1235 * @s: pointer to standard V4L2 device structure
1236 * @fmt: standard V4L2 VIDIOC_ENUM_FMT ioctl structure
1238 * Implement the VIDIOC_ENUM_FMT ioctl for the CAPTURE buffer type.
1240 static int ioctl_enum_fmt_cap(struct v4l2_int_device
*s
,
1241 struct v4l2_fmtdesc
*fmt
)
1243 int index
= fmt
->index
;
1244 enum v4l2_buf_type type
= fmt
->type
;
1246 memset(fmt
, 0, sizeof(*fmt
));
1250 switch (fmt
->type
) {
1251 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1252 if (index
>= NUM_CAPTURE_FORMATS
)
1259 fmt
->flags
= imx046_formats
[index
].flags
;
1260 strlcpy(fmt
->description
, imx046_formats
[index
].description
,
1261 sizeof(fmt
->description
));
1262 fmt
->pixelformat
= imx046_formats
[index
].pixelformat
;
1268 * ioctl_try_fmt_cap - Implement the CAPTURE buffer VIDIOC_TRY_FMT ioctl
1269 * @s: pointer to standard V4L2 device structure
1270 * @f: pointer to standard V4L2 VIDIOC_TRY_FMT ioctl structure
1272 * Implement the VIDIOC_TRY_FMT ioctl for the CAPTURE buffer type. This
1273 * ioctl is used to negotiate the image capture size and pixel format
1274 * without actually making it take effect.
1276 static int ioctl_try_fmt_cap(struct v4l2_int_device
*s
,
1277 struct v4l2_format
*f
)
1279 enum imx046_image_size isize
;
1281 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1282 struct imx046_sensor
*sensor
= s
->priv
;
1283 struct v4l2_pix_format
*pix2
= &sensor
->pix
;
1285 isize
= imx046_find_size(pix
->width
, pix
->height
);
1287 pix
->width
= imx046_sizes
[isize
].width
;
1288 pix
->height
= imx046_sizes
[isize
].height
;
1289 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1290 if (pix
->pixelformat
== imx046_formats
[ifmt
].pixelformat
)
1293 if (ifmt
== NUM_CAPTURE_FORMATS
)
1295 pix
->pixelformat
= imx046_formats
[ifmt
].pixelformat
;
1296 pix
->field
= V4L2_FIELD_NONE
;
1297 pix
->bytesperline
= pix
->width
* 2;
1298 pix
->sizeimage
= pix
->bytesperline
* pix
->height
;
1300 pix
->colorspace
= V4L2_COLORSPACE_SRGB
;
1306 * ioctl_s_fmt_cap - V4L2 sensor interface handler for VIDIOC_S_FMT ioctl
1307 * @s: pointer to standard V4L2 device structure
1308 * @f: pointer to standard V4L2 VIDIOC_S_FMT ioctl structure
1310 * If the requested format is supported, configures the HW to use that
1311 * format, returns error code if format not supported or HW can't be
1312 * correctly configured.
1314 static int ioctl_s_fmt_cap(struct v4l2_int_device
*s
,
1315 struct v4l2_format
*f
)
1317 struct imx046_sensor
*sensor
= s
->priv
;
1318 struct v4l2_pix_format
*pix
= &f
->fmt
.pix
;
1321 rval
= ioctl_try_fmt_cap(s
, f
);
1332 * ioctl_g_fmt_cap - V4L2 sensor interface handler for ioctl_g_fmt_cap
1333 * @s: pointer to standard V4L2 device structure
1334 * @f: pointer to standard V4L2 v4l2_format structure
1336 * Returns the sensor's current pixel format in the v4l2_format
1339 static int ioctl_g_fmt_cap(struct v4l2_int_device
*s
,
1340 struct v4l2_format
*f
)
1342 struct imx046_sensor
*sensor
= s
->priv
;
1343 f
->fmt
.pix
= sensor
->pix
;
1349 * ioctl_g_pixclk - V4L2 sensor interface handler for ioctl_g_pixclk
1350 * @s: pointer to standard V4L2 device structure
1351 * @pixclk: pointer to unsigned 32 var to store pixelclk in HZ
1353 * Returns the sensor's current pixel clock in HZ
1355 static int ioctl_priv_g_pixclk(struct v4l2_int_device
*s
, u32
*pixclk
)
1357 *pixclk
= xclk_current
;
1363 * ioctl_g_activesize - V4L2 sensor interface handler for ioctl_g_activesize
1364 * @s: pointer to standard V4L2 device structure
1365 * @pix: pointer to standard V4L2 v4l2_pix_format structure
1367 * Returns the sensor's current active image basesize.
1369 static int ioctl_priv_g_activesize(struct v4l2_int_device
*s
,
1370 struct v4l2_pix_format
*pix
)
1372 struct imx046_sensor
*sensor
= s
->priv
;
1374 pix
->width
= sensor
->pix
.width
;
1375 pix
->height
= sensor
->pix
.height
;
1381 * ioctl_g_fullsize - V4L2 sensor interface handler for ioctl_g_fullsize
1382 * @s: pointer to standard V4L2 device structure
1383 * @pix: pointer to standard V4L2 v4l2_pix_format structure
1385 * Returns the sensor's biggest image basesize.
1387 static int ioctl_priv_g_fullsize(struct v4l2_int_device
*s
,
1388 struct v4l2_pix_format
*pix
)
1390 pix
->width
= imx046_sizes
[NUM_IMAGE_SIZES
- 1].width
;
1391 pix
->height
= imx046_sizes
[NUM_IMAGE_SIZES
- 1].height
;
1397 * ioctl_g_parm - V4L2 sensor interface handler for VIDIOC_G_PARM ioctl
1398 * @s: pointer to standard V4L2 device structure
1399 * @a: pointer to standard V4L2 VIDIOC_G_PARM ioctl structure
1401 * Returns the sensor's video CAPTURE parameters.
1403 static int ioctl_g_parm(struct v4l2_int_device
*s
,
1404 struct v4l2_streamparm
*a
)
1406 struct imx046_sensor
*sensor
= s
->priv
;
1407 struct v4l2_captureparm
*cparm
= &a
->parm
.capture
;
1409 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1412 memset(a
, 0, sizeof(*a
));
1413 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1415 cparm
->capability
= V4L2_CAP_TIMEPERFRAME
;
1416 cparm
->timeperframe
= sensor
->timeperframe
;
1422 * ioctl_s_parm - V4L2 sensor interface handler for VIDIOC_S_PARM ioctl
1423 * @s: pointer to standard V4L2 device structure
1424 * @a: pointer to standard V4L2 VIDIOC_S_PARM ioctl structure
1426 * Configures the sensor to use the input parameters, if possible. If
1427 * not possible, reverts to the old parameters and returns the
1428 * appropriate error code.
1430 static int ioctl_s_parm(struct v4l2_int_device
*s
,
1431 struct v4l2_streamparm
*a
)
1433 struct imx046_sensor
*sensor
= s
->priv
;
1434 struct v4l2_fract
*timeperframe
= &a
->parm
.capture
.timeperframe
;
1436 sensor
->timeperframe
= *timeperframe
;
1437 imx046sensor_calc_xclk();
1438 *timeperframe
= sensor
->timeperframe
;
1445 * ioctl_g_priv - V4L2 sensor interface handler for vidioc_int_g_priv_num
1446 * @s: pointer to standard V4L2 device structure
1447 * @p: void pointer to hold sensor's private data address
1449 * Returns device's (sensor's) private data area address in p parameter
1451 static int ioctl_g_priv(struct v4l2_int_device
*s
, void *p
)
1453 struct imx046_sensor
*sensor
= s
->priv
;
1455 return sensor
->pdata
->priv_data_set(s
, p
);
1460 * ioctl_s_power - V4L2 sensor interface handler for vidioc_int_s_power_num
1461 * @s: pointer to standard V4L2 device structure
1462 * @on: power state to which device is to be set
1464 * Sets devices power state to requrested state, if possible.
1466 static int ioctl_s_power(struct v4l2_int_device
*s
, enum v4l2_power on
)
1468 struct imx046_sensor
*sensor
= s
->priv
;
1469 struct i2c_client
*client
= sensor
->i2c_client
;
1470 struct vcontrol
*lvc
;
1473 if ((on
== V4L2_POWER_STANDBY
) && (sensor
->state
== SENSOR_DETECTED
)) {
1474 /* imx046_write_regs(c, stream_off_list,
1475 I2C_STREAM_OFF_LIST_SIZE); */
1478 if (on
!= V4L2_POWER_ON
)
1479 sensor
->pdata
->set_xclk(s
, 0);
1481 sensor
->pdata
->set_xclk(s
, xclk_current
);
1483 rval
= sensor
->pdata
->power_set(s
, on
);
1485 v4l_err(client
, "Unable to set the power state: "
1486 IMX046_DRIVER_NAME
" sensor\n");
1487 sensor
->pdata
->set_xclk(s
, 0);
1491 if ((current_power_state
== V4L2_POWER_STANDBY
) &&
1492 (on
== V4L2_POWER_ON
) &&
1493 (sensor
->state
== SENSOR_DETECTED
)) {
1494 sensor
->resuming
= true;
1495 imx046_configure(s
);
1498 if ((on
== V4L2_POWER_ON
) && (sensor
->state
== SENSOR_NOT_DETECTED
)) {
1499 rval
= imx046_detect(client
);
1501 v4l_err(client
, "Unable to detect "
1502 IMX046_DRIVER_NAME
" sensor\n");
1503 sensor
->state
= SENSOR_NOT_DETECTED
;
1506 sensor
->state
= SENSOR_DETECTED
;
1508 v4l_info(client
, IMX046_DRIVER_NAME
1509 " chip version 0x%02x detected\n", sensor
->ver
);
1512 if (on
== V4L2_POWER_OFF
) {
1513 /* Reset defaults for controls */
1514 i
= find_vctrl(V4L2_CID_GAIN
);
1516 lvc
= &imx046sensor_video_control
[i
];
1517 lvc
->current_value
= IMX046_EV_DEF_GAIN
;
1519 i
= find_vctrl(V4L2_CID_EXPOSURE
);
1521 lvc
= &imx046sensor_video_control
[i
];
1522 lvc
->current_value
= IMX046_DEF_EXPOSURE
;
1524 i
= find_vctrl(V4L2_CID_TEST_PATTERN
);
1526 lvc
= &imx046sensor_video_control
[i
];
1527 lvc
->current_value
= IMX046_MIN_TEST_PATT_MODE
;
1531 sensor
->resuming
= false;
1532 current_power_state
= on
;
1537 * ioctl_init - V4L2 sensor interface handler for VIDIOC_INT_INIT
1538 * @s: pointer to standard V4L2 device structure
1540 * Initialize the sensor device (call imx046_configure())
1542 static int ioctl_init(struct v4l2_int_device
*s
)
1548 * ioctl_dev_exit - V4L2 sensor interface handler for vidioc_int_dev_exit_num
1549 * @s: pointer to standard V4L2 device structure
1551 * Delinitialise the dev. at slave detach. The complement of ioctl_dev_init.
1553 static int ioctl_dev_exit(struct v4l2_int_device
*s
)
1559 * ioctl_dev_init - V4L2 sensor interface handler for vidioc_int_dev_init_num
1560 * @s: pointer to standard V4L2 device structure
1562 * Initialise the device when slave attaches to the master. Returns 0 if
1563 * imx046 device could be found, otherwise returns appropriate error.
1565 static int ioctl_dev_init(struct v4l2_int_device
*s
)
1567 struct imx046_sensor
*sensor
= s
->priv
;
1568 struct i2c_client
*client
= sensor
->i2c_client
;
1571 err
= imx046_detect(client
);
1573 v4l_err(client
, "Unable to detect " IMX046_DRIVER_NAME
1579 v4l_info(client
, IMX046_DRIVER_NAME
" chip version "
1580 "0x%02x detected\n", sensor
->ver
);
1586 * ioctl_enum_framesizes - V4L2 sensor if handler for vidioc_int_enum_framesizes
1587 * @s: pointer to standard V4L2 device structure
1588 * @frms: pointer to standard V4L2 framesizes enumeration structure
1590 * Returns possible framesizes depending on choosen pixel format
1592 static int ioctl_enum_framesizes(struct v4l2_int_device
*s
,
1593 struct v4l2_frmsizeenum
*frms
)
1597 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1598 if (frms
->pixel_format
== imx046_formats
[ifmt
].pixelformat
)
1601 /* Is requested pixelformat not found on sensor? */
1602 if (ifmt
== NUM_CAPTURE_FORMATS
)
1605 /* Check that the index we are being asked for is not
1607 if (frms
->index
>= ARRAY_SIZE(imx046_sizes
))
1610 frms
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1611 frms
->discrete
.width
= imx046_sizes
[frms
->index
].width
;
1612 frms
->discrete
.height
= imx046_sizes
[frms
->index
].height
;
1617 const struct v4l2_fract imx046_frameintervals
[] = {
1618 { .numerator
= 3, .denominator
= 30 },
1619 { .numerator
= 1, .denominator
= 30 },
1622 static int ioctl_enum_frameintervals(struct v4l2_int_device
*s
,
1623 struct v4l2_frmivalenum
*frmi
)
1627 /* Check that the requested format is one we support */
1628 for (ifmt
= 0; ifmt
< NUM_CAPTURE_FORMATS
; ifmt
++) {
1629 if (frmi
->pixel_format
== imx046_formats
[ifmt
].pixelformat
)
1633 if (ifmt
== NUM_CAPTURE_FORMATS
)
1636 /* Check that the index we are being asked for is not
1638 if (frmi
->index
>= ARRAY_SIZE(imx046_frameintervals
))
1641 /* Make sure that the 8MP size reports a max of 10fps */
1642 if (frmi
->width
== 3280 && frmi
->height
== 2464) {
1643 if (frmi
->index
!= 0)
1647 frmi
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1648 frmi
->discrete
.numerator
=
1649 imx046_frameintervals
[frmi
->index
].numerator
;
1650 frmi
->discrete
.denominator
=
1651 imx046_frameintervals
[frmi
->index
].denominator
;
1656 static struct v4l2_int_ioctl_desc imx046_ioctl_desc
[] = {
1657 { .num
= vidioc_int_enum_framesizes_num
,
1658 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_framesizes
},
1659 { .num
= vidioc_int_enum_frameintervals_num
,
1660 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_frameintervals
},
1661 { .num
= vidioc_int_dev_init_num
,
1662 .func
= (v4l2_int_ioctl_func
*)ioctl_dev_init
},
1663 { .num
= vidioc_int_dev_exit_num
,
1664 .func
= (v4l2_int_ioctl_func
*)ioctl_dev_exit
},
1665 { .num
= vidioc_int_s_power_num
,
1666 .func
= (v4l2_int_ioctl_func
*)ioctl_s_power
},
1667 { .num
= vidioc_int_g_priv_num
,
1668 .func
= (v4l2_int_ioctl_func
*)ioctl_g_priv
},
1669 { .num
= vidioc_int_init_num
,
1670 .func
= (v4l2_int_ioctl_func
*)ioctl_init
},
1671 { .num
= vidioc_int_enum_fmt_cap_num
,
1672 .func
= (v4l2_int_ioctl_func
*)ioctl_enum_fmt_cap
},
1673 { .num
= vidioc_int_try_fmt_cap_num
,
1674 .func
= (v4l2_int_ioctl_func
*)ioctl_try_fmt_cap
},
1675 { .num
= vidioc_int_g_fmt_cap_num
,
1676 .func
= (v4l2_int_ioctl_func
*)ioctl_g_fmt_cap
},
1677 { .num
= vidioc_int_s_fmt_cap_num
,
1678 .func
= (v4l2_int_ioctl_func
*)ioctl_s_fmt_cap
},
1679 { .num
= vidioc_int_g_parm_num
,
1680 .func
= (v4l2_int_ioctl_func
*)ioctl_g_parm
},
1681 { .num
= vidioc_int_s_parm_num
,
1682 .func
= (v4l2_int_ioctl_func
*)ioctl_s_parm
},
1683 { .num
= vidioc_int_queryctrl_num
,
1684 .func
= (v4l2_int_ioctl_func
*)ioctl_queryctrl
},
1685 { .num
= vidioc_int_g_ctrl_num
,
1686 .func
= (v4l2_int_ioctl_func
*)ioctl_g_ctrl
},
1687 { .num
= vidioc_int_s_ctrl_num
,
1688 .func
= (v4l2_int_ioctl_func
*)ioctl_s_ctrl
},
1689 { .num
= vidioc_int_priv_g_pixclk_num
,
1690 .func
= (v4l2_int_ioctl_func
*)ioctl_priv_g_pixclk
},
1691 { .num
= vidioc_int_priv_g_activesize_num
,
1692 .func
= (v4l2_int_ioctl_func
*)ioctl_priv_g_activesize
},
1693 { .num
= vidioc_int_priv_g_fullsize_num
,
1694 .func
= (v4l2_int_ioctl_func
*)ioctl_priv_g_fullsize
},
1697 static struct v4l2_int_slave imx046_slave
= {
1698 .ioctls
= imx046_ioctl_desc
,
1699 .num_ioctls
= ARRAY_SIZE(imx046_ioctl_desc
),
1702 static struct v4l2_int_device imx046_int_device
= {
1703 .module
= THIS_MODULE
,
1704 .name
= IMX046_DRIVER_NAME
,
1706 .type
= v4l2_int_type_slave
,
1708 .slave
= &imx046_slave
,
1713 * imx046_probe - sensor driver i2c probe handler
1714 * @client: i2c driver client device structure
1716 * Register sensor as an i2c client device and V4L2
1719 static int __devinit
imx046_probe(struct i2c_client
*client
,
1720 const struct i2c_device_id
*id
)
1722 struct imx046_sensor
*sensor
= &imx046
;
1725 if (i2c_get_clientdata(client
))
1728 sensor
->pdata
= client
->dev
.platform_data
;
1730 if (!sensor
->pdata
) {
1731 v4l_err(client
, "no platform data?\n");
1735 sensor
->v4l2_int_device
= &imx046_int_device
;
1736 sensor
->i2c_client
= client
;
1738 i2c_set_clientdata(client
, sensor
);
1740 /* Make the default capture format QCIF V4L2_PIX_FMT_SRGGB10 */
1741 sensor
->pix
.width
= IMX046_IMAGE_WIDTH_MAX
;
1742 sensor
->pix
.height
= IMX046_IMAGE_HEIGHT_MAX
;
1743 sensor
->pix
.pixelformat
= V4L2_PIX_FMT_SRGGB10
;
1745 err
= v4l2_int_device_register(sensor
->v4l2_int_device
);
1747 i2c_set_clientdata(client
, NULL
);
1753 * imx046_remove - sensor driver i2c remove handler
1754 * @client: i2c driver client device structure
1756 * Unregister sensor as an i2c client device and V4L2
1757 * device. Complement of imx046_probe().
1760 imx046_remove(struct i2c_client
*client
)
1762 struct imx046_sensor
*sensor
= i2c_get_clientdata(client
);
1764 if (!client
->adapter
)
1765 return -ENODEV
; /* our client isn't attached */
1767 v4l2_int_device_unregister(sensor
->v4l2_int_device
);
1768 i2c_set_clientdata(client
, NULL
);
1773 static const struct i2c_device_id imx046_id
[] = {
1774 { IMX046_DRIVER_NAME
, 0 },
1777 MODULE_DEVICE_TABLE(i2c
, imx046_id
);
1779 static struct i2c_driver imx046sensor_i2c_driver
= {
1781 .name
= IMX046_DRIVER_NAME
,
1782 .owner
= THIS_MODULE
,
1784 .probe
= imx046_probe
,
1785 .remove
= __exit_p(imx046_remove
),
1786 .id_table
= imx046_id
,
1789 static struct imx046_sensor imx046
= {
1794 .state
= SENSOR_NOT_DETECTED
,
1798 * imx046sensor_init - sensor driver module_init handler
1800 * Registers driver as an i2c client driver. Returns 0 on success,
1801 * error code otherwise.
1803 static int __init
imx046sensor_init(void)
1807 err
= i2c_add_driver(&imx046sensor_i2c_driver
);
1809 printk(KERN_ERR
"Failed to register" IMX046_DRIVER_NAME
".\n");
1814 late_initcall(imx046sensor_init
);
1817 * imx046sensor_cleanup - sensor driver module_exit handler
1819 * Unregisters/deletes driver as an i2c client driver.
1820 * Complement of imx046sensor_init.
1822 static void __exit
imx046sensor_cleanup(void)
1824 i2c_del_driver(&imx046sensor_i2c_driver
);
1826 module_exit(imx046sensor_cleanup
);
1828 MODULE_LICENSE("GPL");
1829 MODULE_DESCRIPTION("imx046 camera sensor driver");