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>
32 #include <media/soc_camera.h>
33 #include <media/v4l2-chip-ident.h>
36 /* Register definitions */
37 #define REG_GAIN 0x00 /* range 00 - 3F */
40 #define REG_SAT 0x03 /* [7:4] saturation [0:3] reserved */
41 #define REG_HUE 0x04 /* [7:6] rsrvd [5] hue en [4:0] hue */
49 #define REG_CLKRC 0x11 /* Data Format and Internal Clock */
50 /* [7:6] Input system clock (MHz)*/
51 /* 00=8, 01=12, 10=16, 11=24 */
52 /* [5:0]: Internal Clock Pre-Scaler */
53 #define REG_COMA 0x12 /* [7] Reset */
58 #define REG_HSTRT 0x17
59 #define REG_HSTOP 0x18
60 #define REG_VSTRT 0x19
61 #define REG_VSTOP 0x1a
62 #define REG_PSHFT 0x1b
65 #define REG_HSYNS 0x1e
66 #define REG_HSYNE 0x1f
78 #define REG_FRARL 0x2b
85 #define REG_FRAJH 0x32
86 #define REG_FRAJL 0x33
88 #define REG_L1AEC 0x35
100 #define REG_ADCL 0x69
102 #define REG_RMCO 0x6c
103 #define REG_GMCO 0x6d
104 #define REG_BMCO 0x6e
107 /* Register bits, values, etc. */
108 #define OV6650_PIDH 0x66 /* high byte of product ID number */
109 #define OV6650_PIDL 0x50 /* low byte of product ID number */
110 #define OV6650_MIDH 0x7F /* high byte of mfg ID */
111 #define OV6650_MIDL 0xA2 /* low byte of mfg ID */
113 #define DEF_GAIN 0x00
114 #define DEF_BLUE 0x80
118 #define SAT_MASK (0xf << SAT_SHIFT)
119 #define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
121 #define HUE_EN BIT(5)
122 #define HUE_MASK 0x1f
124 #define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
126 #define DEF_AECH 0x4D
128 #define CLKRC_6MHz 0x00
129 #define CLKRC_12MHz 0x40
130 #define CLKRC_16MHz 0x80
131 #define CLKRC_24MHz 0xc0
132 #define CLKRC_DIV_MASK 0x3f
133 #define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
135 #define COMA_RESET BIT(7)
136 #define COMA_QCIF BIT(5)
137 #define COMA_RAW_RGB BIT(4)
138 #define COMA_RGB BIT(3)
139 #define COMA_BW BIT(2)
140 #define COMA_WORD_SWAP BIT(1)
141 #define COMA_BYTE_SWAP BIT(0)
142 #define DEF_COMA 0x00
144 #define COMB_FLIP_V BIT(7)
145 #define COMB_FLIP_H BIT(5)
146 #define COMB_BAND_FILTER BIT(4)
147 #define COMB_AWB BIT(2)
148 #define COMB_AGC BIT(1)
149 #define COMB_AEC BIT(0)
150 #define DEF_COMB 0x5f
152 #define COML_ONE_CHANNEL BIT(7)
154 #define DEF_HSTRT 0x24
155 #define DEF_HSTOP 0xd4
156 #define DEF_VSTRT 0x04
157 #define DEF_VSTOP 0x94
159 #define COMF_HREF_LOW BIT(4)
161 #define COMJ_PCLK_RISING BIT(4)
162 #define COMJ_VSYNC_HIGH BIT(0)
164 /* supported resolutions */
165 #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
166 #define W_CIF (W_QCIF << 1)
167 #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
168 #define H_CIF (H_QCIF << 1)
170 #define FRAME_RATE_MAX 30
179 struct v4l2_subdev subdev;
194 bool half_scale; /* scale down output by 2 */
195 struct v4l2_rect rect; /* sensor cropping window */
196 unsigned long pclk_limit; /* from host */
197 unsigned long pclk_max; /* from resolution and format */
198 struct v4l2_fract tpf; /* as requested with s_parm */
199 enum v4l2_mbus_pixelcode code;
200 enum v4l2_colorspace colorspace;
204 static enum v4l2_mbus_pixelcode ov6650_codes[] = {
205 V4L2_MBUS_FMT_YUYV8_2X8,
206 V4L2_MBUS_FMT_UYVY8_2X8,
207 V4L2_MBUS_FMT_YVYU8_2X8,
208 V4L2_MBUS_FMT_VYUY8_2X8,
209 V4L2_MBUS_FMT_SBGGR8_1X8,
210 V4L2_MBUS_FMT_GREY8_1X8,
213 static const struct v4l2_queryctrl ov6650_controls[] = {
215 .id = V4L2_CID_AUTOGAIN,
216 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .type = V4L2_CTRL_TYPE_INTEGER,
230 .default_value = DEF_GAIN,
233 .id = V4L2_CID_AUTO_WHITE_BALANCE,
234 .type = V4L2_CTRL_TYPE_BOOLEAN,
242 .id = V4L2_CID_BLUE_BALANCE,
243 .type = V4L2_CTRL_TYPE_INTEGER,
248 .default_value = DEF_BLUE,
251 .id = V4L2_CID_RED_BALANCE,
252 .type = V4L2_CTRL_TYPE_INTEGER,
257 .default_value = DEF_RED,
260 .id = V4L2_CID_SATURATION,
261 .type = V4L2_CTRL_TYPE_INTEGER,
262 .name = "Saturation",
266 .default_value = 0x8,
270 .type = V4L2_CTRL_TYPE_INTEGER,
275 .default_value = DEF_HUE,
278 .id = V4L2_CID_BRIGHTNESS,
279 .type = V4L2_CTRL_TYPE_INTEGER,
280 .name = "Brightness",
284 .default_value = 0x80,
287 .id = V4L2_CID_EXPOSURE_AUTO,
288 .type = V4L2_CTRL_TYPE_INTEGER,
296 .id = V4L2_CID_EXPOSURE,
297 .type = V4L2_CTRL_TYPE_INTEGER,
302 .default_value = DEF_AECH,
305 .id = V4L2_CID_GAMMA,
306 .type = V4L2_CTRL_TYPE_INTEGER,
311 .default_value = 0x12,
314 .id = V4L2_CID_VFLIP,
315 .type = V4L2_CTRL_TYPE_BOOLEAN,
316 .name = "Flip Vertically",
323 .id = V4L2_CID_HFLIP,
324 .type = V4L2_CTRL_TYPE_BOOLEAN,
325 .name = "Flip Horizontally",
333 /* read a register */
334 static int ov6650_reg_read(struct i2c_client *client, u8 reg, u8 *val)
338 struct i2c_msg msg = {
339 .addr = client->addr,
345 ret = i2c_transfer(client->adapter, &msg, 1);
349 msg.flags = I2C_M_RD;
350 ret = i2c_transfer(client->adapter, &msg, 1);
358 dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
362 /* write a register */
363 static int ov6650_reg_write(struct i2c_client *client, u8 reg, u8 val)
366 unsigned char data[2] = { reg, val };
367 struct i2c_msg msg = {
368 .addr = client->addr,
374 ret = i2c_transfer(client->adapter, &msg, 1);
378 dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
385 /* Read a register, alter its bits, write it back */
386 static int ov6650_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 mask)
391 ret = ov6650_reg_read(client, reg, &val);
393 dev_err(&client->dev,
394 "[Read]-Modify-Write of register 0x%02x failed!\n",
402 ret = ov6650_reg_write(client, reg, val);
404 dev_err(&client->dev,
405 "Read-Modify-[Write] of register 0x%02x failed!\n",
411 static struct ov6650 *to_ov6650(const struct i2c_client *client)
413 return container_of(i2c_get_clientdata(client), struct ov6650, subdev);
416 /* Start/Stop streaming from the device */
417 static int ov6650_s_stream(struct v4l2_subdev *sd, int enable)
422 /* Alter bus settings on camera side */
423 static int ov6650_set_bus_param(struct soc_camera_device *icd,
426 struct soc_camera_link *icl = to_soc_camera_link(icd);
427 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
430 flags = soc_camera_apply_sensor_flags(icl, flags);
432 if (flags & SOCAM_PCLK_SAMPLE_RISING)
433 ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
435 ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
439 if (flags & SOCAM_HSYNC_ACTIVE_LOW)
440 ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
442 ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
446 if (flags & SOCAM_VSYNC_ACTIVE_HIGH)
447 ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
449 ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
454 /* Request bus settings on camera side */
455 static unsigned long ov6650_query_bus_param(struct soc_camera_device *icd)
457 struct soc_camera_link *icl = to_soc_camera_link(icd);
459 unsigned long flags = SOCAM_MASTER |
460 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
461 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
462 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
463 SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
465 return soc_camera_apply_sensor_flags(icl, flags);
468 /* Get status of additional camera capabilities */
469 static int ov6650_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
471 struct i2c_client *client = v4l2_get_subdevdata(sd);
472 struct ov6650 *priv = to_ov6650(client);
477 case V4L2_CID_AUTOGAIN:
478 ctrl->value = priv->agc;
482 ret = ov6650_reg_read(client, REG_GAIN, ®);
485 ctrl->value = priv->gain;
488 case V4L2_CID_AUTO_WHITE_BALANCE:
489 ctrl->value = priv->awb;
491 case V4L2_CID_BLUE_BALANCE:
493 ret = ov6650_reg_read(client, REG_BLUE, ®);
496 ctrl->value = priv->blue;
499 case V4L2_CID_RED_BALANCE:
501 ret = ov6650_reg_read(client, REG_RED, ®);
504 ctrl->value = priv->red;
507 case V4L2_CID_SATURATION:
508 ctrl->value = priv->saturation;
511 ctrl->value = priv->hue;
513 case V4L2_CID_BRIGHTNESS:
514 ctrl->value = priv->brightness;
516 case V4L2_CID_EXPOSURE_AUTO:
517 ctrl->value = priv->aec;
519 case V4L2_CID_EXPOSURE:
521 ret = ov6650_reg_read(client, REG_AECH, ®);
524 ctrl->value = priv->exposure;
528 ctrl->value = priv->gamma;
531 ctrl->value = priv->vflip;
534 ctrl->value = priv->hflip;
540 /* Set status of additional camera capabilities */
541 static int ov6650_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
543 struct i2c_client *client = v4l2_get_subdevdata(sd);
544 struct ov6650 *priv = to_ov6650(client);
548 case V4L2_CID_AUTOGAIN:
549 ret = ov6650_reg_rmw(client, REG_COMB,
550 ctrl->value ? COMB_AGC : 0, COMB_AGC);
552 priv->agc = ctrl->value;
555 ret = ov6650_reg_write(client, REG_GAIN, ctrl->value);
557 priv->gain = ctrl->value;
559 case V4L2_CID_AUTO_WHITE_BALANCE:
560 ret = ov6650_reg_rmw(client, REG_COMB,
561 ctrl->value ? COMB_AWB : 0, COMB_AWB);
563 priv->awb = ctrl->value;
565 case V4L2_CID_BLUE_BALANCE:
566 ret = ov6650_reg_write(client, REG_BLUE, ctrl->value);
568 priv->blue = ctrl->value;
570 case V4L2_CID_RED_BALANCE:
571 ret = ov6650_reg_write(client, REG_RED, ctrl->value);
573 priv->red = ctrl->value;
575 case V4L2_CID_SATURATION:
576 ret = ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->value),
579 priv->saturation = ctrl->value;
582 ret = ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->value),
585 priv->hue = ctrl->value;
587 case V4L2_CID_BRIGHTNESS:
588 ret = ov6650_reg_write(client, REG_BRT, ctrl->value);
590 priv->brightness = ctrl->value;
592 case V4L2_CID_EXPOSURE_AUTO:
593 switch (ctrl->value) {
594 case V4L2_EXPOSURE_AUTO:
595 ret = ov6650_reg_rmw(client, REG_COMB, COMB_AEC, 0);
598 ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_AEC);
602 priv->aec = ctrl->value;
604 case V4L2_CID_EXPOSURE:
605 ret = ov6650_reg_write(client, REG_AECH, ctrl->value);
607 priv->exposure = ctrl->value;
610 ret = ov6650_reg_write(client, REG_GAM1, ctrl->value);
612 priv->gamma = ctrl->value;
615 ret = ov6650_reg_rmw(client, REG_COMB,
616 ctrl->value ? COMB_FLIP_V : 0, COMB_FLIP_V);
618 priv->vflip = ctrl->value;
621 ret = ov6650_reg_rmw(client, REG_COMB,
622 ctrl->value ? COMB_FLIP_H : 0, COMB_FLIP_H);
624 priv->hflip = ctrl->value;
631 /* Get chip identification */
632 static int ov6650_g_chip_ident(struct v4l2_subdev *sd,
633 struct v4l2_dbg_chip_ident *id)
635 id->ident = V4L2_IDENT_OV6650;
641 #ifdef CONFIG_VIDEO_ADV_DEBUG
642 static int ov6650_get_register(struct v4l2_subdev *sd,
643 struct v4l2_dbg_register *reg)
645 struct i2c_client *client = v4l2_get_subdevdata(sd);
649 if (reg->reg & ~0xff)
654 ret = ov6650_reg_read(client, reg->reg, &val);
656 reg->val = (__u64)val;
661 static int ov6650_set_register(struct v4l2_subdev *sd,
662 struct v4l2_dbg_register *reg)
664 struct i2c_client *client = v4l2_get_subdevdata(sd);
666 if (reg->reg & ~0xff || reg->val & ~0xff)
669 return ov6650_reg_write(client, reg->reg, reg->val);
673 static int ov6650_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
675 struct i2c_client *client = v4l2_get_subdevdata(sd);
676 struct ov6650 *priv = to_ov6650(client);
678 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
684 static int ov6650_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
686 struct i2c_client *client = v4l2_get_subdevdata(sd);
687 struct ov6650 *priv = to_ov6650(client);
688 struct v4l2_rect *rect = &a->c;
691 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 rect->left = ALIGN(rect->left, 2);
695 rect->width = ALIGN(rect->width, 2);
696 rect->top = ALIGN(rect->top, 2);
697 rect->height = ALIGN(rect->height, 2);
698 soc_camera_limit_side(&rect->left, &rect->width,
699 DEF_HSTRT << 1, 2, W_CIF);
700 soc_camera_limit_side(&rect->top, &rect->height,
701 DEF_VSTRT << 1, 2, H_CIF);
703 ret = ov6650_reg_write(client, REG_HSTRT, rect->left >> 1);
705 priv->rect.left = rect->left;
706 ret = ov6650_reg_write(client, REG_HSTOP,
707 (rect->left + rect->width) >> 1);
710 priv->rect.width = rect->width;
711 ret = ov6650_reg_write(client, REG_VSTRT, rect->top >> 1);
714 priv->rect.top = rect->top;
715 ret = ov6650_reg_write(client, REG_VSTOP,
716 (rect->top + rect->height) >> 1);
719 priv->rect.height = rect->height;
724 static int ov6650_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
726 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
729 a->bounds.left = DEF_HSTRT << 1;
730 a->bounds.top = DEF_VSTRT << 1;
731 a->bounds.width = W_CIF;
732 a->bounds.height = H_CIF;
733 a->defrect = a->bounds;
734 a->pixelaspect.numerator = 1;
735 a->pixelaspect.denominator = 1;
740 static int ov6650_g_fmt(struct v4l2_subdev *sd,
741 struct v4l2_mbus_framefmt *mf)
743 struct i2c_client *client = v4l2_get_subdevdata(sd);
744 struct ov6650 *priv = to_ov6650(client);
746 mf->width = priv->rect.width >> priv->half_scale;
747 mf->height = priv->rect.height >> priv->half_scale;
748 mf->code = priv->code;
749 mf->colorspace = priv->colorspace;
750 mf->field = V4L2_FIELD_NONE;
755 static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
757 return width > rect->width >> 1 || height > rect->height >> 1;
760 static u8 to_clkrc(struct v4l2_fract *timeperframe,
761 unsigned long pclk_limit, unsigned long pclk_max)
765 if (timeperframe->numerator && timeperframe->denominator)
766 pclk = pclk_max * timeperframe->denominator /
767 (FRAME_RATE_MAX * timeperframe->numerator);
771 if (pclk_limit && pclk_limit < pclk)
774 return (pclk_max - 1) / pclk;
777 /* set the format we will capture in */
778 static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
780 struct i2c_client *client = v4l2_get_subdevdata(sd);
781 struct soc_camera_device *icd = client->dev.platform_data;
782 struct soc_camera_sense *sense = icd->sense;
783 struct ov6650 *priv = to_ov6650(client);
784 bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
785 struct v4l2_crop a = {
786 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
788 .left = priv->rect.left + (priv->rect.width >> 1) -
789 (mf->width >> (1 - half_scale)),
790 .top = priv->rect.top + (priv->rect.height >> 1) -
791 (mf->height >> (1 - half_scale)),
792 .width = mf->width << half_scale,
793 .height = mf->height << half_scale,
796 enum v4l2_mbus_pixelcode code = mf->code;
797 unsigned long mclk, pclk;
798 u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
801 /* select color matrix configuration for given color encoding */
803 case V4L2_MBUS_FMT_GREY8_1X8:
804 dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
805 coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
808 case V4L2_MBUS_FMT_YUYV8_2X8:
809 dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
810 coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
811 coma_set |= COMA_WORD_SWAP;
813 case V4L2_MBUS_FMT_YVYU8_2X8:
814 dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
815 coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
818 case V4L2_MBUS_FMT_UYVY8_2X8:
819 dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
821 coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
822 coma_set |= COMA_BYTE_SWAP;
824 coma_mask |= COMA_RGB | COMA_BW;
825 coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
828 case V4L2_MBUS_FMT_VYUY8_2X8:
829 dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
831 coma_mask |= COMA_RGB | COMA_BW;
832 coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
834 coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
835 coma_set |= COMA_BYTE_SWAP;
838 case V4L2_MBUS_FMT_SBGGR8_1X8:
839 dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
840 coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
841 coma_set |= COMA_RAW_RGB | COMA_RGB;
844 dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
849 if (code == V4L2_MBUS_FMT_GREY8_1X8 ||
850 code == V4L2_MBUS_FMT_SBGGR8_1X8) {
851 coml_mask = COML_ONE_CHANNEL;
853 priv->pclk_max = 4000000;
856 coml_set = COML_ONE_CHANNEL;
857 priv->pclk_max = 8000000;
860 if (code == V4L2_MBUS_FMT_SBGGR8_1X8)
861 priv->colorspace = V4L2_COLORSPACE_SRGB;
863 priv->colorspace = V4L2_COLORSPACE_JPEG;
866 dev_dbg(&client->dev, "max resolution: QCIF\n");
867 coma_set |= COMA_QCIF;
870 dev_dbg(&client->dev, "max resolution: CIF\n");
871 coma_mask |= COMA_QCIF;
873 priv->half_scale = half_scale;
876 if (sense->master_clock == 8000000) {
877 dev_dbg(&client->dev, "8MHz input clock\n");
879 } else if (sense->master_clock == 12000000) {
880 dev_dbg(&client->dev, "12MHz input clock\n");
882 } else if (sense->master_clock == 16000000) {
883 dev_dbg(&client->dev, "16MHz input clock\n");
885 } else if (sense->master_clock == 24000000) {
886 dev_dbg(&client->dev, "24MHz input clock\n");
889 dev_err(&client->dev,
890 "unspported input clock, check platform data\n");
893 mclk = sense->master_clock;
894 priv->pclk_limit = sense->pixel_clock_max;
898 priv->pclk_limit = 0;
899 dev_dbg(&client->dev, "using default 24MHz input clock\n");
902 clkrc |= to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
904 pclk = priv->pclk_max / GET_CLKRC_DIV(clkrc);
905 dev_dbg(&client->dev, "pixel clock divider: %ld.%ld\n",
906 mclk / pclk, 10 * mclk % pclk / pclk);
908 ret = ov6650_s_crop(sd, &a);
910 ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
912 ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
914 ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
917 mf->colorspace = priv->colorspace;
918 mf->width = priv->rect.width >> half_scale;
919 mf->height = priv->rect.height >> half_scale;
925 static int ov6650_try_fmt(struct v4l2_subdev *sd,
926 struct v4l2_mbus_framefmt *mf)
928 struct i2c_client *client = v4l2_get_subdevdata(sd);
929 struct ov6650 *priv = to_ov6650(client);
931 if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
932 v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
933 &mf->height, 2, H_CIF, 1, 0);
935 mf->field = V4L2_FIELD_NONE;
938 case V4L2_MBUS_FMT_Y10_1X10:
939 mf->code = V4L2_MBUS_FMT_GREY8_1X8;
940 case V4L2_MBUS_FMT_GREY8_1X8:
941 case V4L2_MBUS_FMT_YVYU8_2X8:
942 case V4L2_MBUS_FMT_YUYV8_2X8:
943 case V4L2_MBUS_FMT_VYUY8_2X8:
944 case V4L2_MBUS_FMT_UYVY8_2X8:
945 mf->colorspace = V4L2_COLORSPACE_JPEG;
948 mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
949 case V4L2_MBUS_FMT_SBGGR8_1X8:
950 mf->colorspace = V4L2_COLORSPACE_SRGB;
957 static int ov6650_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
958 enum v4l2_mbus_pixelcode *code)
960 if (index >= ARRAY_SIZE(ov6650_codes))
963 *code = ov6650_codes[index];
967 static int ov6650_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
969 struct i2c_client *client = v4l2_get_subdevdata(sd);
970 struct ov6650 *priv = to_ov6650(client);
971 struct v4l2_captureparm *cp = &parms->parm.capture;
973 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
976 memset(cp, 0, sizeof(*cp));
977 cp->capability = V4L2_CAP_TIMEPERFRAME;
978 cp->timeperframe.numerator = GET_CLKRC_DIV(to_clkrc(&priv->tpf,
979 priv->pclk_limit, priv->pclk_max));
980 cp->timeperframe.denominator = FRAME_RATE_MAX;
982 dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
983 cp->timeperframe.numerator, cp->timeperframe.denominator);
988 static int ov6650_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
990 struct i2c_client *client = v4l2_get_subdevdata(sd);
991 struct ov6650 *priv = to_ov6650(client);
992 struct v4l2_captureparm *cp = &parms->parm.capture;
993 struct v4l2_fract *tpf = &cp->timeperframe;
997 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1000 if (cp->extendedmode != 0)
1003 if (tpf->numerator == 0 || tpf->denominator == 0)
1004 div = 1; /* Reset to full rate */
1006 div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
1010 else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
1011 div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
1014 * Keep result to be used as tpf limit
1015 * for subseqent clock divider calculations
1017 priv->tpf.numerator = div;
1018 priv->tpf.denominator = FRAME_RATE_MAX;
1020 clkrc = to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
1022 ret = ov6650_reg_rmw(client, REG_CLKRC, clkrc, CLKRC_DIV_MASK);
1024 tpf->numerator = GET_CLKRC_DIV(clkrc);
1025 tpf->denominator = FRAME_RATE_MAX;
1031 /* Soft reset the camera. This has nothing to do with the RESET pin! */
1032 static int ov6650_reset(struct i2c_client *client)
1036 dev_dbg(&client->dev, "reset\n");
1038 ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
1040 dev_err(&client->dev,
1041 "An error occured while entering soft reset!\n");
1046 /* program default register values */
1047 static int ov6650_prog_dflt(struct i2c_client *client)
1051 dev_dbg(&client->dev, "initializing\n");
1053 ret = ov6650_reg_write(client, REG_COMA, 0); /* ~COMA_RESET */
1055 ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
1060 static int ov6650_video_probe(struct soc_camera_device *icd,
1061 struct i2c_client *client)
1063 u8 pidh, pidl, midh, midl;
1067 * check and show product ID and manufacturer ID
1069 ret = ov6650_reg_read(client, REG_PIDH, &pidh);
1071 ret = ov6650_reg_read(client, REG_PIDL, &pidl);
1073 ret = ov6650_reg_read(client, REG_MIDH, &midh);
1075 ret = ov6650_reg_read(client, REG_MIDL, &midl);
1080 if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
1081 dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
1086 dev_info(&client->dev,
1087 "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
1088 pidh, pidl, midh, midl);
1090 ret = ov6650_reset(client);
1092 ret = ov6650_prog_dflt(client);
1097 static struct soc_camera_ops ov6650_ops = {
1098 .set_bus_param = ov6650_set_bus_param,
1099 .query_bus_param = ov6650_query_bus_param,
1100 .controls = ov6650_controls,
1101 .num_controls = ARRAY_SIZE(ov6650_controls),
1104 static struct v4l2_subdev_core_ops ov6650_core_ops = {
1105 .g_ctrl = ov6650_g_ctrl,
1106 .s_ctrl = ov6650_s_ctrl,
1107 .g_chip_ident = ov6650_g_chip_ident,
1108 #ifdef CONFIG_VIDEO_ADV_DEBUG
1109 .g_register = ov6650_get_register,
1110 .s_register = ov6650_set_register,
1114 static struct v4l2_subdev_video_ops ov6650_video_ops = {
1115 .s_stream = ov6650_s_stream,
1116 .g_mbus_fmt = ov6650_g_fmt,
1117 .s_mbus_fmt = ov6650_s_fmt,
1118 .try_mbus_fmt = ov6650_try_fmt,
1119 .enum_mbus_fmt = ov6650_enum_fmt,
1120 .cropcap = ov6650_cropcap,
1121 .g_crop = ov6650_g_crop,
1122 .s_crop = ov6650_s_crop,
1123 .g_parm = ov6650_g_parm,
1124 .s_parm = ov6650_s_parm,
1127 static struct v4l2_subdev_ops ov6650_subdev_ops = {
1128 .core = &ov6650_core_ops,
1129 .video = &ov6650_video_ops,
1133 * i2c_driver function
1135 static int ov6650_probe(struct i2c_client *client,
1136 const struct i2c_device_id *did)
1138 struct ov6650 *priv;
1139 struct soc_camera_device *icd = client->dev.platform_data;
1140 struct soc_camera_link *icl;
1144 dev_err(&client->dev, "Missing soc-camera data!\n");
1148 icl = to_soc_camera_link(icd);
1150 dev_err(&client->dev, "Missing platform_data for driver\n");
1154 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1156 dev_err(&client->dev,
1157 "Failed to allocate memory for private data!\n");
1161 v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
1163 icd->ops = &ov6650_ops;
1165 priv->rect.left = DEF_HSTRT << 1;
1166 priv->rect.top = DEF_VSTRT << 1;
1167 priv->rect.width = W_CIF;
1168 priv->rect.height = H_CIF;
1169 priv->half_scale = false;
1170 priv->code = V4L2_MBUS_FMT_YUYV8_2X8;
1171 priv->colorspace = V4L2_COLORSPACE_JPEG;
1173 ret = ov6650_video_probe(icd, client);
1183 static int ov6650_remove(struct i2c_client *client)
1185 struct ov6650 *priv = to_ov6650(client);
1191 static const struct i2c_device_id ov6650_id[] = {
1195 MODULE_DEVICE_TABLE(i2c, ov6650_id);
1197 static struct i2c_driver ov6650_i2c_driver = {
1201 .probe = ov6650_probe,
1202 .remove = ov6650_remove,
1203 .id_table = ov6650_id,
1206 static int __init ov6650_module_init(void)
1208 return i2c_add_driver(&ov6650_i2c_driver);
1211 static void __exit ov6650_module_exit(void)
1213 i2c_del_driver(&ov6650_i2c_driver);
1216 module_init(ov6650_module_init);
1217 module_exit(ov6650_module_exit);
1219 MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
1220 MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
1221 MODULE_LICENSE("GPL v2");