2 * V4L2 SoC Camera driver for OmniVision OV6650 Camera Sensor
4 * Copyright (C) 2010 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
6 * Based on OmniVision OV96xx Camera Driver
7 * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
9 * Based on ov772x camera driver:
10 * Copyright (C) 2008 Renesas Solutions Corp.
11 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
13 * Based on ov7670 and soc_camera_platform driver,
14 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
15 * Copyright (C) 2008 Magnus Damm
16 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
18 * Hardware specific bits initialy based on former work by Matt Callow
19 * drivers/media/video/omap/sensor_ov6650.c
20 * Copyright (C) 2006 Matt Callow
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
27 #include <linux/bitops.h>
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
33 #include <media/soc_camera.h>
34 #include <media/v4l2-chip-ident.h>
37 /* Register definitions */
38 #define REG_GAIN 0x00 /* range 00 - 3F */
41 #define REG_SAT 0x03 /* [7:4] saturation [0:3] reserved */
42 #define REG_HUE 0x04 /* [7:6] rsrvd [5] hue en [4:0] hue */
50 #define REG_CLKRC 0x11 /* Data Format and Internal Clock */
51 /* [7:6] Input system clock (MHz)*/
52 /* 00=8, 01=12, 10=16, 11=24 */
53 /* [5:0]: Internal Clock Pre-Scaler */
54 #define REG_COMA 0x12 /* [7] Reset */
59 #define REG_HSTRT 0x17
60 #define REG_HSTOP 0x18
61 #define REG_VSTRT 0x19
62 #define REG_VSTOP 0x1a
63 #define REG_PSHFT 0x1b
66 #define REG_HSYNS 0x1e
67 #define REG_HSYNE 0x1f
79 #define REG_FRARL 0x2b
86 #define REG_FRAJH 0x32
87 #define REG_FRAJL 0x33
89 #define REG_L1AEC 0x35
100 #define REG_SPCE 0x68
101 #define REG_ADCL 0x69
103 #define REG_RMCO 0x6c
104 #define REG_GMCO 0x6d
105 #define REG_BMCO 0x6e
108 /* Register bits, values, etc. */
109 #define OV6650_PIDH 0x66 /* high byte of product ID number */
110 #define OV6650_PIDL 0x50 /* low byte of product ID number */
111 #define OV6650_MIDH 0x7F /* high byte of mfg ID */
112 #define OV6650_MIDL 0xA2 /* low byte of mfg ID */
114 #define DEF_GAIN 0x00
115 #define DEF_BLUE 0x80
119 #define SAT_MASK (0xf << SAT_SHIFT)
120 #define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
122 #define HUE_EN BIT(5)
123 #define HUE_MASK 0x1f
125 #define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
127 #define DEF_AECH 0x4D
129 #define CLKRC_6MHz 0x00
130 #define CLKRC_12MHz 0x40
131 #define CLKRC_16MHz 0x80
132 #define CLKRC_24MHz 0xc0
133 #define CLKRC_DIV_MASK 0x3f
134 #define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
136 #define COMA_RESET BIT(7)
137 #define COMA_QCIF BIT(5)
138 #define COMA_RAW_RGB BIT(4)
139 #define COMA_RGB BIT(3)
140 #define COMA_BW BIT(2)
141 #define COMA_WORD_SWAP BIT(1)
142 #define COMA_BYTE_SWAP BIT(0)
143 #define DEF_COMA 0x00
145 #define COMB_FLIP_V BIT(7)
146 #define COMB_FLIP_H BIT(5)
147 #define COMB_BAND_FILTER BIT(4)
148 #define COMB_AWB BIT(2)
149 #define COMB_AGC BIT(1)
150 #define COMB_AEC BIT(0)
151 #define DEF_COMB 0x5f
153 #define COML_ONE_CHANNEL BIT(7)
155 #define DEF_HSTRT 0x24
156 #define DEF_HSTOP 0xd4
157 #define DEF_VSTRT 0x04
158 #define DEF_VSTOP 0x94
160 #define COMF_HREF_LOW BIT(4)
162 #define COMJ_PCLK_RISING BIT(4)
163 #define COMJ_VSYNC_HIGH BIT(0)
165 /* supported resolutions */
166 #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
167 #define W_CIF (W_QCIF << 1)
168 #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
169 #define H_CIF (H_QCIF << 1)
171 #define FRAME_RATE_MAX 30
180 struct v4l2_subdev subdev
;
195 bool half_scale
; /* scale down output by 2 */
196 struct v4l2_rect rect
; /* sensor cropping window */
197 unsigned long pclk_limit
; /* from host */
198 unsigned long pclk_max
; /* from resolution and format */
199 struct v4l2_fract tpf
; /* as requested with s_parm */
200 enum v4l2_mbus_pixelcode code
;
201 enum v4l2_colorspace colorspace
;
205 static enum v4l2_mbus_pixelcode ov6650_codes
[] = {
206 V4L2_MBUS_FMT_YUYV8_2X8
,
207 V4L2_MBUS_FMT_UYVY8_2X8
,
208 V4L2_MBUS_FMT_YVYU8_2X8
,
209 V4L2_MBUS_FMT_VYUY8_2X8
,
210 V4L2_MBUS_FMT_SBGGR8_1X8
,
211 V4L2_MBUS_FMT_Y8_1X8
,
214 static const struct v4l2_queryctrl ov6650_controls
[] = {
216 .id
= V4L2_CID_AUTOGAIN
,
217 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
226 .type
= V4L2_CTRL_TYPE_INTEGER
,
231 .default_value
= DEF_GAIN
,
234 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
235 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
243 .id
= V4L2_CID_BLUE_BALANCE
,
244 .type
= V4L2_CTRL_TYPE_INTEGER
,
249 .default_value
= DEF_BLUE
,
252 .id
= V4L2_CID_RED_BALANCE
,
253 .type
= V4L2_CTRL_TYPE_INTEGER
,
258 .default_value
= DEF_RED
,
261 .id
= V4L2_CID_SATURATION
,
262 .type
= V4L2_CTRL_TYPE_INTEGER
,
263 .name
= "Saturation",
267 .default_value
= 0x8,
271 .type
= V4L2_CTRL_TYPE_INTEGER
,
276 .default_value
= DEF_HUE
,
279 .id
= V4L2_CID_BRIGHTNESS
,
280 .type
= V4L2_CTRL_TYPE_INTEGER
,
281 .name
= "Brightness",
285 .default_value
= 0x80,
288 .id
= V4L2_CID_EXPOSURE_AUTO
,
289 .type
= V4L2_CTRL_TYPE_INTEGER
,
297 .id
= V4L2_CID_EXPOSURE
,
298 .type
= V4L2_CTRL_TYPE_INTEGER
,
303 .default_value
= DEF_AECH
,
306 .id
= V4L2_CID_GAMMA
,
307 .type
= V4L2_CTRL_TYPE_INTEGER
,
312 .default_value
= 0x12,
315 .id
= V4L2_CID_VFLIP
,
316 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
317 .name
= "Flip Vertically",
324 .id
= V4L2_CID_HFLIP
,
325 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
326 .name
= "Flip Horizontally",
334 /* read a register */
335 static int ov6650_reg_read(struct i2c_client
*client
, u8 reg
, u8
*val
)
339 struct i2c_msg msg
= {
340 .addr
= client
->addr
,
346 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
350 msg
.flags
= I2C_M_RD
;
351 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
359 dev_err(&client
->dev
, "Failed reading register 0x%02x!\n", reg
);
363 /* write a register */
364 static int ov6650_reg_write(struct i2c_client
*client
, u8 reg
, u8 val
)
367 unsigned char data
[2] = { reg
, val
};
368 struct i2c_msg msg
= {
369 .addr
= client
->addr
,
375 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
379 dev_err(&client
->dev
, "Failed writing register 0x%02x!\n", reg
);
386 /* Read a register, alter its bits, write it back */
387 static int ov6650_reg_rmw(struct i2c_client
*client
, u8 reg
, u8 set
, u8 mask
)
392 ret
= ov6650_reg_read(client
, reg
, &val
);
394 dev_err(&client
->dev
,
395 "[Read]-Modify-Write of register 0x%02x failed!\n",
403 ret
= ov6650_reg_write(client
, reg
, val
);
405 dev_err(&client
->dev
,
406 "Read-Modify-[Write] of register 0x%02x failed!\n",
412 static struct ov6650
*to_ov6650(const struct i2c_client
*client
)
414 return container_of(i2c_get_clientdata(client
), struct ov6650
, subdev
);
417 /* Start/Stop streaming from the device */
418 static int ov6650_s_stream(struct v4l2_subdev
*sd
, int enable
)
423 /* Alter bus settings on camera side */
424 static int ov6650_set_bus_param(struct soc_camera_device
*icd
,
427 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
428 struct i2c_client
*client
= to_i2c_client(to_soc_camera_control(icd
));
431 flags
= soc_camera_apply_sensor_flags(icl
, flags
);
433 if (flags
& SOCAM_PCLK_SAMPLE_RISING
)
434 ret
= ov6650_reg_rmw(client
, REG_COMJ
, COMJ_PCLK_RISING
, 0);
436 ret
= ov6650_reg_rmw(client
, REG_COMJ
, 0, COMJ_PCLK_RISING
);
440 if (flags
& SOCAM_HSYNC_ACTIVE_LOW
)
441 ret
= ov6650_reg_rmw(client
, REG_COMF
, COMF_HREF_LOW
, 0);
443 ret
= ov6650_reg_rmw(client
, REG_COMF
, 0, COMF_HREF_LOW
);
447 if (flags
& SOCAM_VSYNC_ACTIVE_HIGH
)
448 ret
= ov6650_reg_rmw(client
, REG_COMJ
, COMJ_VSYNC_HIGH
, 0);
450 ret
= ov6650_reg_rmw(client
, REG_COMJ
, 0, COMJ_VSYNC_HIGH
);
455 /* Request bus settings on camera side */
456 static unsigned long ov6650_query_bus_param(struct soc_camera_device
*icd
)
458 struct soc_camera_link
*icl
= to_soc_camera_link(icd
);
460 unsigned long flags
= SOCAM_MASTER
|
461 SOCAM_PCLK_SAMPLE_RISING
| SOCAM_PCLK_SAMPLE_FALLING
|
462 SOCAM_HSYNC_ACTIVE_HIGH
| SOCAM_HSYNC_ACTIVE_LOW
|
463 SOCAM_VSYNC_ACTIVE_HIGH
| SOCAM_VSYNC_ACTIVE_LOW
|
464 SOCAM_DATA_ACTIVE_HIGH
| SOCAM_DATAWIDTH_8
;
466 return soc_camera_apply_sensor_flags(icl
, flags
);
469 /* Get status of additional camera capabilities */
470 static int ov6650_g_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
472 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
473 struct ov6650
*priv
= to_ov6650(client
);
478 case V4L2_CID_AUTOGAIN
:
479 ctrl
->value
= priv
->agc
;
483 ret
= ov6650_reg_read(client
, REG_GAIN
, ®
);
486 ctrl
->value
= priv
->gain
;
489 case V4L2_CID_AUTO_WHITE_BALANCE
:
490 ctrl
->value
= priv
->awb
;
492 case V4L2_CID_BLUE_BALANCE
:
494 ret
= ov6650_reg_read(client
, REG_BLUE
, ®
);
497 ctrl
->value
= priv
->blue
;
500 case V4L2_CID_RED_BALANCE
:
502 ret
= ov6650_reg_read(client
, REG_RED
, ®
);
505 ctrl
->value
= priv
->red
;
508 case V4L2_CID_SATURATION
:
509 ctrl
->value
= priv
->saturation
;
512 ctrl
->value
= priv
->hue
;
514 case V4L2_CID_BRIGHTNESS
:
515 ctrl
->value
= priv
->brightness
;
517 case V4L2_CID_EXPOSURE_AUTO
:
518 ctrl
->value
= priv
->aec
;
520 case V4L2_CID_EXPOSURE
:
522 ret
= ov6650_reg_read(client
, REG_AECH
, ®
);
525 ctrl
->value
= priv
->exposure
;
529 ctrl
->value
= priv
->gamma
;
532 ctrl
->value
= priv
->vflip
;
535 ctrl
->value
= priv
->hflip
;
541 /* Set status of additional camera capabilities */
542 static int ov6650_s_ctrl(struct v4l2_subdev
*sd
, struct v4l2_control
*ctrl
)
544 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
545 struct ov6650
*priv
= to_ov6650(client
);
549 case V4L2_CID_AUTOGAIN
:
550 ret
= ov6650_reg_rmw(client
, REG_COMB
,
551 ctrl
->value
? COMB_AGC
: 0, COMB_AGC
);
553 priv
->agc
= ctrl
->value
;
556 ret
= ov6650_reg_write(client
, REG_GAIN
, ctrl
->value
);
558 priv
->gain
= ctrl
->value
;
560 case V4L2_CID_AUTO_WHITE_BALANCE
:
561 ret
= ov6650_reg_rmw(client
, REG_COMB
,
562 ctrl
->value
? COMB_AWB
: 0, COMB_AWB
);
564 priv
->awb
= ctrl
->value
;
566 case V4L2_CID_BLUE_BALANCE
:
567 ret
= ov6650_reg_write(client
, REG_BLUE
, ctrl
->value
);
569 priv
->blue
= ctrl
->value
;
571 case V4L2_CID_RED_BALANCE
:
572 ret
= ov6650_reg_write(client
, REG_RED
, ctrl
->value
);
574 priv
->red
= ctrl
->value
;
576 case V4L2_CID_SATURATION
:
577 ret
= ov6650_reg_rmw(client
, REG_SAT
, SET_SAT(ctrl
->value
),
580 priv
->saturation
= ctrl
->value
;
583 ret
= ov6650_reg_rmw(client
, REG_HUE
, SET_HUE(ctrl
->value
),
586 priv
->hue
= ctrl
->value
;
588 case V4L2_CID_BRIGHTNESS
:
589 ret
= ov6650_reg_write(client
, REG_BRT
, ctrl
->value
);
591 priv
->brightness
= ctrl
->value
;
593 case V4L2_CID_EXPOSURE_AUTO
:
594 switch (ctrl
->value
) {
595 case V4L2_EXPOSURE_AUTO
:
596 ret
= ov6650_reg_rmw(client
, REG_COMB
, COMB_AEC
, 0);
599 ret
= ov6650_reg_rmw(client
, REG_COMB
, 0, COMB_AEC
);
603 priv
->aec
= ctrl
->value
;
605 case V4L2_CID_EXPOSURE
:
606 ret
= ov6650_reg_write(client
, REG_AECH
, ctrl
->value
);
608 priv
->exposure
= ctrl
->value
;
611 ret
= ov6650_reg_write(client
, REG_GAM1
, ctrl
->value
);
613 priv
->gamma
= ctrl
->value
;
616 ret
= ov6650_reg_rmw(client
, REG_COMB
,
617 ctrl
->value
? COMB_FLIP_V
: 0, COMB_FLIP_V
);
619 priv
->vflip
= ctrl
->value
;
622 ret
= ov6650_reg_rmw(client
, REG_COMB
,
623 ctrl
->value
? COMB_FLIP_H
: 0, COMB_FLIP_H
);
625 priv
->hflip
= ctrl
->value
;
632 /* Get chip identification */
633 static int ov6650_g_chip_ident(struct v4l2_subdev
*sd
,
634 struct v4l2_dbg_chip_ident
*id
)
636 id
->ident
= V4L2_IDENT_OV6650
;
642 #ifdef CONFIG_VIDEO_ADV_DEBUG
643 static int ov6650_get_register(struct v4l2_subdev
*sd
,
644 struct v4l2_dbg_register
*reg
)
646 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
650 if (reg
->reg
& ~0xff)
655 ret
= ov6650_reg_read(client
, reg
->reg
, &val
);
657 reg
->val
= (__u64
)val
;
662 static int ov6650_set_register(struct v4l2_subdev
*sd
,
663 struct v4l2_dbg_register
*reg
)
665 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
667 if (reg
->reg
& ~0xff || reg
->val
& ~0xff)
670 return ov6650_reg_write(client
, reg
->reg
, reg
->val
);
674 static int ov6650_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
676 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
677 struct ov6650
*priv
= to_ov6650(client
);
679 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
685 static int ov6650_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
687 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
688 struct ov6650
*priv
= to_ov6650(client
);
689 struct v4l2_rect
*rect
= &a
->c
;
692 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
695 rect
->left
= ALIGN(rect
->left
, 2);
696 rect
->width
= ALIGN(rect
->width
, 2);
697 rect
->top
= ALIGN(rect
->top
, 2);
698 rect
->height
= ALIGN(rect
->height
, 2);
699 soc_camera_limit_side(&rect
->left
, &rect
->width
,
700 DEF_HSTRT
<< 1, 2, W_CIF
);
701 soc_camera_limit_side(&rect
->top
, &rect
->height
,
702 DEF_VSTRT
<< 1, 2, H_CIF
);
704 ret
= ov6650_reg_write(client
, REG_HSTRT
, rect
->left
>> 1);
706 priv
->rect
.left
= rect
->left
;
707 ret
= ov6650_reg_write(client
, REG_HSTOP
,
708 (rect
->left
+ rect
->width
) >> 1);
711 priv
->rect
.width
= rect
->width
;
712 ret
= ov6650_reg_write(client
, REG_VSTRT
, rect
->top
>> 1);
715 priv
->rect
.top
= rect
->top
;
716 ret
= ov6650_reg_write(client
, REG_VSTOP
,
717 (rect
->top
+ rect
->height
) >> 1);
720 priv
->rect
.height
= rect
->height
;
725 static int ov6650_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
727 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
730 a
->bounds
.left
= DEF_HSTRT
<< 1;
731 a
->bounds
.top
= DEF_VSTRT
<< 1;
732 a
->bounds
.width
= W_CIF
;
733 a
->bounds
.height
= H_CIF
;
734 a
->defrect
= a
->bounds
;
735 a
->pixelaspect
.numerator
= 1;
736 a
->pixelaspect
.denominator
= 1;
741 static int ov6650_g_fmt(struct v4l2_subdev
*sd
,
742 struct v4l2_mbus_framefmt
*mf
)
744 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
745 struct ov6650
*priv
= to_ov6650(client
);
747 mf
->width
= priv
->rect
.width
>> priv
->half_scale
;
748 mf
->height
= priv
->rect
.height
>> priv
->half_scale
;
749 mf
->code
= priv
->code
;
750 mf
->colorspace
= priv
->colorspace
;
751 mf
->field
= V4L2_FIELD_NONE
;
756 static bool is_unscaled_ok(int width
, int height
, struct v4l2_rect
*rect
)
758 return width
> rect
->width
>> 1 || height
> rect
->height
>> 1;
761 static u8
to_clkrc(struct v4l2_fract
*timeperframe
,
762 unsigned long pclk_limit
, unsigned long pclk_max
)
766 if (timeperframe
->numerator
&& timeperframe
->denominator
)
767 pclk
= pclk_max
* timeperframe
->denominator
/
768 (FRAME_RATE_MAX
* timeperframe
->numerator
);
772 if (pclk_limit
&& pclk_limit
< pclk
)
775 return (pclk_max
- 1) / pclk
;
778 /* set the format we will capture in */
779 static int ov6650_s_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*mf
)
781 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
782 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
783 struct soc_camera_sense
*sense
= icd
->sense
;
784 struct ov6650
*priv
= to_ov6650(client
);
785 bool half_scale
= !is_unscaled_ok(mf
->width
, mf
->height
, &priv
->rect
);
786 struct v4l2_crop a
= {
787 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
789 .left
= priv
->rect
.left
+ (priv
->rect
.width
>> 1) -
790 (mf
->width
>> (1 - half_scale
)),
791 .top
= priv
->rect
.top
+ (priv
->rect
.height
>> 1) -
792 (mf
->height
>> (1 - half_scale
)),
793 .width
= mf
->width
<< half_scale
,
794 .height
= mf
->height
<< half_scale
,
797 enum v4l2_mbus_pixelcode code
= mf
->code
;
798 unsigned long mclk
, pclk
;
799 u8 coma_set
= 0, coma_mask
= 0, coml_set
, coml_mask
, clkrc
;
802 /* select color matrix configuration for given color encoding */
804 case V4L2_MBUS_FMT_Y8_1X8
:
805 dev_dbg(&client
->dev
, "pixel format GREY8_1X8\n");
806 coma_mask
|= COMA_RGB
| COMA_WORD_SWAP
| COMA_BYTE_SWAP
;
809 case V4L2_MBUS_FMT_YUYV8_2X8
:
810 dev_dbg(&client
->dev
, "pixel format YUYV8_2X8_LE\n");
811 coma_mask
|= COMA_RGB
| COMA_BW
| COMA_BYTE_SWAP
;
812 coma_set
|= COMA_WORD_SWAP
;
814 case V4L2_MBUS_FMT_YVYU8_2X8
:
815 dev_dbg(&client
->dev
, "pixel format YVYU8_2X8_LE (untested)\n");
816 coma_mask
|= COMA_RGB
| COMA_BW
| COMA_WORD_SWAP
|
819 case V4L2_MBUS_FMT_UYVY8_2X8
:
820 dev_dbg(&client
->dev
, "pixel format YUYV8_2X8_BE\n");
822 coma_mask
|= COMA_RGB
| COMA_BW
| COMA_WORD_SWAP
;
823 coma_set
|= COMA_BYTE_SWAP
;
825 coma_mask
|= COMA_RGB
| COMA_BW
;
826 coma_set
|= COMA_BYTE_SWAP
| COMA_WORD_SWAP
;
829 case V4L2_MBUS_FMT_VYUY8_2X8
:
830 dev_dbg(&client
->dev
, "pixel format YVYU8_2X8_BE (untested)\n");
832 coma_mask
|= COMA_RGB
| COMA_BW
;
833 coma_set
|= COMA_BYTE_SWAP
| COMA_WORD_SWAP
;
835 coma_mask
|= COMA_RGB
| COMA_BW
| COMA_WORD_SWAP
;
836 coma_set
|= COMA_BYTE_SWAP
;
839 case V4L2_MBUS_FMT_SBGGR8_1X8
:
840 dev_dbg(&client
->dev
, "pixel format SBGGR8_1X8 (untested)\n");
841 coma_mask
|= COMA_BW
| COMA_BYTE_SWAP
| COMA_WORD_SWAP
;
842 coma_set
|= COMA_RAW_RGB
| COMA_RGB
;
845 dev_err(&client
->dev
, "Pixel format not handled: 0x%x\n", code
);
850 if (code
== V4L2_MBUS_FMT_Y8_1X8
||
851 code
== V4L2_MBUS_FMT_SBGGR8_1X8
) {
852 coml_mask
= COML_ONE_CHANNEL
;
854 priv
->pclk_max
= 4000000;
857 coml_set
= COML_ONE_CHANNEL
;
858 priv
->pclk_max
= 8000000;
861 if (code
== V4L2_MBUS_FMT_SBGGR8_1X8
)
862 priv
->colorspace
= V4L2_COLORSPACE_SRGB
;
864 priv
->colorspace
= V4L2_COLORSPACE_JPEG
;
867 dev_dbg(&client
->dev
, "max resolution: QCIF\n");
868 coma_set
|= COMA_QCIF
;
871 dev_dbg(&client
->dev
, "max resolution: CIF\n");
872 coma_mask
|= COMA_QCIF
;
874 priv
->half_scale
= half_scale
;
877 if (sense
->master_clock
== 8000000) {
878 dev_dbg(&client
->dev
, "8MHz input clock\n");
880 } else if (sense
->master_clock
== 12000000) {
881 dev_dbg(&client
->dev
, "12MHz input clock\n");
883 } else if (sense
->master_clock
== 16000000) {
884 dev_dbg(&client
->dev
, "16MHz input clock\n");
886 } else if (sense
->master_clock
== 24000000) {
887 dev_dbg(&client
->dev
, "24MHz input clock\n");
890 dev_err(&client
->dev
,
891 "unspported input clock, check platform data\n");
894 mclk
= sense
->master_clock
;
895 priv
->pclk_limit
= sense
->pixel_clock_max
;
899 priv
->pclk_limit
= 0;
900 dev_dbg(&client
->dev
, "using default 24MHz input clock\n");
903 clkrc
|= to_clkrc(&priv
->tpf
, priv
->pclk_limit
, priv
->pclk_max
);
905 pclk
= priv
->pclk_max
/ GET_CLKRC_DIV(clkrc
);
906 dev_dbg(&client
->dev
, "pixel clock divider: %ld.%ld\n",
907 mclk
/ pclk
, 10 * mclk
% pclk
/ pclk
);
909 ret
= ov6650_s_crop(sd
, &a
);
911 ret
= ov6650_reg_rmw(client
, REG_COMA
, coma_set
, coma_mask
);
913 ret
= ov6650_reg_write(client
, REG_CLKRC
, clkrc
);
915 ret
= ov6650_reg_rmw(client
, REG_COML
, coml_set
, coml_mask
);
918 mf
->colorspace
= priv
->colorspace
;
919 mf
->width
= priv
->rect
.width
>> half_scale
;
920 mf
->height
= priv
->rect
.height
>> half_scale
;
926 static int ov6650_try_fmt(struct v4l2_subdev
*sd
,
927 struct v4l2_mbus_framefmt
*mf
)
929 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
930 struct ov6650
*priv
= to_ov6650(client
);
932 if (is_unscaled_ok(mf
->width
, mf
->height
, &priv
->rect
))
933 v4l_bound_align_image(&mf
->width
, 2, W_CIF
, 1,
934 &mf
->height
, 2, H_CIF
, 1, 0);
936 mf
->field
= V4L2_FIELD_NONE
;
939 case V4L2_MBUS_FMT_Y10_1X10
:
940 mf
->code
= V4L2_MBUS_FMT_Y8_1X8
;
941 case V4L2_MBUS_FMT_Y8_1X8
:
942 case V4L2_MBUS_FMT_YVYU8_2X8
:
943 case V4L2_MBUS_FMT_YUYV8_2X8
:
944 case V4L2_MBUS_FMT_VYUY8_2X8
:
945 case V4L2_MBUS_FMT_UYVY8_2X8
:
946 mf
->colorspace
= V4L2_COLORSPACE_JPEG
;
949 mf
->code
= V4L2_MBUS_FMT_SBGGR8_1X8
;
950 case V4L2_MBUS_FMT_SBGGR8_1X8
:
951 mf
->colorspace
= V4L2_COLORSPACE_SRGB
;
958 static int ov6650_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
959 enum v4l2_mbus_pixelcode
*code
)
961 if (index
>= ARRAY_SIZE(ov6650_codes
))
964 *code
= ov6650_codes
[index
];
968 static int ov6650_g_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
970 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
971 struct ov6650
*priv
= to_ov6650(client
);
972 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
974 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
977 memset(cp
, 0, sizeof(*cp
));
978 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
979 cp
->timeperframe
.numerator
= GET_CLKRC_DIV(to_clkrc(&priv
->tpf
,
980 priv
->pclk_limit
, priv
->pclk_max
));
981 cp
->timeperframe
.denominator
= FRAME_RATE_MAX
;
983 dev_dbg(&client
->dev
, "Frame interval: %u/%u s\n",
984 cp
->timeperframe
.numerator
, cp
->timeperframe
.denominator
);
989 static int ov6650_s_parm(struct v4l2_subdev
*sd
, struct v4l2_streamparm
*parms
)
991 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
992 struct ov6650
*priv
= to_ov6650(client
);
993 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
994 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
998 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1001 if (cp
->extendedmode
!= 0)
1004 if (tpf
->numerator
== 0 || tpf
->denominator
== 0)
1005 div
= 1; /* Reset to full rate */
1007 div
= (tpf
->numerator
* FRAME_RATE_MAX
) / tpf
->denominator
;
1011 else if (div
> GET_CLKRC_DIV(CLKRC_DIV_MASK
))
1012 div
= GET_CLKRC_DIV(CLKRC_DIV_MASK
);
1015 * Keep result to be used as tpf limit
1016 * for subseqent clock divider calculations
1018 priv
->tpf
.numerator
= div
;
1019 priv
->tpf
.denominator
= FRAME_RATE_MAX
;
1021 clkrc
= to_clkrc(&priv
->tpf
, priv
->pclk_limit
, priv
->pclk_max
);
1023 ret
= ov6650_reg_rmw(client
, REG_CLKRC
, clkrc
, CLKRC_DIV_MASK
);
1025 tpf
->numerator
= GET_CLKRC_DIV(clkrc
);
1026 tpf
->denominator
= FRAME_RATE_MAX
;
1032 /* Soft reset the camera. This has nothing to do with the RESET pin! */
1033 static int ov6650_reset(struct i2c_client
*client
)
1037 dev_dbg(&client
->dev
, "reset\n");
1039 ret
= ov6650_reg_rmw(client
, REG_COMA
, COMA_RESET
, 0);
1041 dev_err(&client
->dev
,
1042 "An error occurred while entering soft reset!\n");
1047 /* program default register values */
1048 static int ov6650_prog_dflt(struct i2c_client
*client
)
1052 dev_dbg(&client
->dev
, "initializing\n");
1054 ret
= ov6650_reg_write(client
, REG_COMA
, 0); /* ~COMA_RESET */
1056 ret
= ov6650_reg_rmw(client
, REG_COMB
, 0, COMB_BAND_FILTER
);
1061 static int ov6650_video_probe(struct soc_camera_device
*icd
,
1062 struct i2c_client
*client
)
1064 u8 pidh
, pidl
, midh
, midl
;
1068 * check and show product ID and manufacturer ID
1070 ret
= ov6650_reg_read(client
, REG_PIDH
, &pidh
);
1072 ret
= ov6650_reg_read(client
, REG_PIDL
, &pidl
);
1074 ret
= ov6650_reg_read(client
, REG_MIDH
, &midh
);
1076 ret
= ov6650_reg_read(client
, REG_MIDL
, &midl
);
1081 if ((pidh
!= OV6650_PIDH
) || (pidl
!= OV6650_PIDL
)) {
1082 dev_err(&client
->dev
, "Product ID error 0x%02x:0x%02x\n",
1087 dev_info(&client
->dev
,
1088 "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
1089 pidh
, pidl
, midh
, midl
);
1091 ret
= ov6650_reset(client
);
1093 ret
= ov6650_prog_dflt(client
);
1098 static struct soc_camera_ops ov6650_ops
= {
1099 .set_bus_param
= ov6650_set_bus_param
,
1100 .query_bus_param
= ov6650_query_bus_param
,
1101 .controls
= ov6650_controls
,
1102 .num_controls
= ARRAY_SIZE(ov6650_controls
),
1105 static struct v4l2_subdev_core_ops ov6650_core_ops
= {
1106 .g_ctrl
= ov6650_g_ctrl
,
1107 .s_ctrl
= ov6650_s_ctrl
,
1108 .g_chip_ident
= ov6650_g_chip_ident
,
1109 #ifdef CONFIG_VIDEO_ADV_DEBUG
1110 .g_register
= ov6650_get_register
,
1111 .s_register
= ov6650_set_register
,
1115 static struct v4l2_subdev_video_ops ov6650_video_ops
= {
1116 .s_stream
= ov6650_s_stream
,
1117 .g_mbus_fmt
= ov6650_g_fmt
,
1118 .s_mbus_fmt
= ov6650_s_fmt
,
1119 .try_mbus_fmt
= ov6650_try_fmt
,
1120 .enum_mbus_fmt
= ov6650_enum_fmt
,
1121 .cropcap
= ov6650_cropcap
,
1122 .g_crop
= ov6650_g_crop
,
1123 .s_crop
= ov6650_s_crop
,
1124 .g_parm
= ov6650_g_parm
,
1125 .s_parm
= ov6650_s_parm
,
1128 static struct v4l2_subdev_ops ov6650_subdev_ops
= {
1129 .core
= &ov6650_core_ops
,
1130 .video
= &ov6650_video_ops
,
1134 * i2c_driver function
1136 static int ov6650_probe(struct i2c_client
*client
,
1137 const struct i2c_device_id
*did
)
1139 struct ov6650
*priv
;
1140 struct soc_camera_device
*icd
= client
->dev
.platform_data
;
1141 struct soc_camera_link
*icl
;
1145 dev_err(&client
->dev
, "Missing soc-camera data!\n");
1149 icl
= to_soc_camera_link(icd
);
1151 dev_err(&client
->dev
, "Missing platform_data for driver\n");
1155 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1157 dev_err(&client
->dev
,
1158 "Failed to allocate memory for private data!\n");
1162 v4l2_i2c_subdev_init(&priv
->subdev
, client
, &ov6650_subdev_ops
);
1164 icd
->ops
= &ov6650_ops
;
1166 priv
->rect
.left
= DEF_HSTRT
<< 1;
1167 priv
->rect
.top
= DEF_VSTRT
<< 1;
1168 priv
->rect
.width
= W_CIF
;
1169 priv
->rect
.height
= H_CIF
;
1170 priv
->half_scale
= false;
1171 priv
->code
= V4L2_MBUS_FMT_YUYV8_2X8
;
1172 priv
->colorspace
= V4L2_COLORSPACE_JPEG
;
1174 ret
= ov6650_video_probe(icd
, client
);
1184 static int ov6650_remove(struct i2c_client
*client
)
1186 struct ov6650
*priv
= to_ov6650(client
);
1192 static const struct i2c_device_id ov6650_id
[] = {
1196 MODULE_DEVICE_TABLE(i2c
, ov6650_id
);
1198 static struct i2c_driver ov6650_i2c_driver
= {
1202 .probe
= ov6650_probe
,
1203 .remove
= ov6650_remove
,
1204 .id_table
= ov6650_id
,
1207 static int __init
ov6650_module_init(void)
1209 return i2c_add_driver(&ov6650_i2c_driver
);
1212 static void __exit
ov6650_module_exit(void)
1214 i2c_del_driver(&ov6650_i2c_driver
);
1217 module_init(ov6650_module_init
);
1218 module_exit(ov6650_module_exit
);
1220 MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
1221 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
1222 MODULE_LICENSE("GPL v2");