2 * Driver for OV5642 CMOS Image Sensor from Omnivision
4 * Copyright (C) 2011, Bastian Hecht <hechtb@gmail.com>
6 * Based on Sony IMX074 Camera Driver
7 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
9 * Based on Omnivision OV7670 Camera Driver
10 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <linux/module.h>
24 #include <linux/v4l2-mediabus.h>
26 #include <media/soc_camera.h>
27 #include <media/v4l2-subdev.h>
29 /* OV5642 registers */
30 #define REG_CHIP_ID_HIGH 0x300a
31 #define REG_CHIP_ID_LOW 0x300b
33 #define REG_WINDOW_START_X_HIGH 0x3800
34 #define REG_WINDOW_START_X_LOW 0x3801
35 #define REG_WINDOW_START_Y_HIGH 0x3802
36 #define REG_WINDOW_START_Y_LOW 0x3803
37 #define REG_WINDOW_WIDTH_HIGH 0x3804
38 #define REG_WINDOW_WIDTH_LOW 0x3805
39 #define REG_WINDOW_HEIGHT_HIGH 0x3806
40 #define REG_WINDOW_HEIGHT_LOW 0x3807
41 #define REG_OUT_WIDTH_HIGH 0x3808
42 #define REG_OUT_WIDTH_LOW 0x3809
43 #define REG_OUT_HEIGHT_HIGH 0x380a
44 #define REG_OUT_HEIGHT_LOW 0x380b
45 #define REG_OUT_TOTAL_WIDTH_HIGH 0x380c
46 #define REG_OUT_TOTAL_WIDTH_LOW 0x380d
47 #define REG_OUT_TOTAL_HEIGHT_HIGH 0x380e
48 #define REG_OUT_TOTAL_HEIGHT_LOW 0x380f
49 #define REG_OUTPUT_FORMAT 0x4300
50 #define REG_ISP_CTRL_01 0x5001
51 #define REG_AVG_WINDOW_END_X_HIGH 0x5682
52 #define REG_AVG_WINDOW_END_X_LOW 0x5683
53 #define REG_AVG_WINDOW_END_Y_HIGH 0x5686
54 #define REG_AVG_WINDOW_END_Y_LOW 0x5687
56 /* active pixel array size */
57 #define OV5642_SENSOR_SIZE_X 2592
58 #define OV5642_SENSOR_SIZE_Y 1944
61 * About OV5642 resolution, cropping and binning:
62 * This sensor supports it all, at least in the feature description.
63 * Unfortunately, no combination of appropriate registers settings could make
64 * the chip work the intended way. As it works with predefined register lists,
65 * some undocumented registers are presumably changed there to achieve their
67 * This driver currently only works for resolutions up to 720 lines with a
68 * 1:1 scale. Hopefully these restrictions will be removed in the future.
70 #define OV5642_MAX_WIDTH OV5642_SENSOR_SIZE_X
71 #define OV5642_MAX_HEIGHT 720
74 #define OV5642_DEFAULT_WIDTH 1280
75 #define OV5642_DEFAULT_HEIGHT OV5642_MAX_HEIGHT
77 /* minimum extra blanking */
78 #define BLANKING_EXTRA_WIDTH 500
79 #define BLANKING_EXTRA_HEIGHT 20
82 * the sensor's autoexposure is buggy when setting total_height low.
83 * It tries to expose longer than 1 frame period without taking care of it
84 * and this leads to weird output. So we set 1000 lines as minimum.
86 #define BLANKING_MIN_HEIGHT 1000
93 static struct regval_list ov5642_default_regs_init[] = {
554 static struct regval_list ov5642_default_regs_finalise[] = {
587 { 0x4300, 0x32 }, /* UYVY */
603 struct ov5642_datafmt {
604 enum v4l2_mbus_pixelcode code;
605 enum v4l2_colorspace colorspace;
609 struct v4l2_subdev subdev;
610 const struct ov5642_datafmt *fmt;
611 struct v4l2_rect crop_rect;
613 /* blanking information */
618 static const struct ov5642_datafmt ov5642_colour_fmts[] = {
619 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
622 static struct ov5642 *to_ov5642(const struct i2c_client *client)
624 return container_of(i2c_get_clientdata(client), struct ov5642, subdev);
627 /* Find a data format by a pixel code in an array */
628 static const struct ov5642_datafmt
629 *ov5642_find_datafmt(enum v4l2_mbus_pixelcode code)
633 for (i = 0; i < ARRAY_SIZE(ov5642_colour_fmts); i++)
634 if (ov5642_colour_fmts[i].code == code)
635 return ov5642_colour_fmts + i;
640 static int reg_read(struct i2c_client *client, u16 reg, u8 *val)
643 /* We have 16-bit i2c addresses - care for endianess */
644 unsigned char data[2] = { reg >> 8, reg & 0xff };
646 ret = i2c_master_send(client, data, 2);
648 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
650 return ret < 0 ? ret : -EIO;
653 ret = i2c_master_recv(client, val, 1);
655 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
657 return ret < 0 ? ret : -EIO;
662 static int reg_write(struct i2c_client *client, u16 reg, u8 val)
665 unsigned char data[3] = { reg >> 8, reg & 0xff, val };
667 ret = i2c_master_send(client, data, 3);
669 dev_err(&client->dev, "%s: i2c write error, reg: %x\n",
671 return ret < 0 ? ret : -EIO;
678 * convenience function to write 16 bit register values that are split up
679 * into two consecutive high and low parts
681 static int reg_write16(struct i2c_client *client, u16 reg, u16 val16)
685 ret = reg_write(client, reg, val16 >> 8);
688 return reg_write(client, reg + 1, val16 & 0x00ff);
691 #ifdef CONFIG_VIDEO_ADV_DEBUG
692 static int ov5642_get_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
694 struct i2c_client *client = v4l2_get_subdevdata(sd);
698 if (reg->reg & ~0xffff)
703 ret = reg_read(client, reg->reg, &val);
705 reg->val = (__u64)val;
710 static int ov5642_set_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
712 struct i2c_client *client = v4l2_get_subdevdata(sd);
714 if (reg->reg & ~0xffff || reg->val & ~0xff)
717 return reg_write(client, reg->reg, reg->val);
721 static int ov5642_write_array(struct i2c_client *client,
722 struct regval_list *vals)
724 while (vals->reg_num != 0xffff || vals->value != 0xff) {
725 int ret = reg_write(client, vals->reg_num, vals->value);
730 dev_dbg(&client->dev, "Register list loaded\n");
734 static int ov5642_set_resolution(struct v4l2_subdev *sd)
736 struct i2c_client *client = v4l2_get_subdevdata(sd);
737 struct ov5642 *priv = to_ov5642(client);
738 int width = priv->crop_rect.width;
739 int height = priv->crop_rect.height;
740 int total_width = priv->total_width;
741 int total_height = priv->total_height;
742 int start_x = (OV5642_SENSOR_SIZE_X - width) / 2;
743 int start_y = (OV5642_SENSOR_SIZE_Y - height) / 2;
747 * This should set the starting point for cropping.
748 * Doesn't work so far.
750 ret = reg_write16(client, REG_WINDOW_START_X_HIGH, start_x);
752 ret = reg_write16(client, REG_WINDOW_START_Y_HIGH, start_y);
754 priv->crop_rect.left = start_x;
755 priv->crop_rect.top = start_y;
759 ret = reg_write16(client, REG_WINDOW_WIDTH_HIGH, width);
761 ret = reg_write16(client, REG_WINDOW_HEIGHT_HIGH, height);
764 priv->crop_rect.width = width;
765 priv->crop_rect.height = height;
767 /* Set the output window size. Only 1:1 scale is supported so far. */
768 ret = reg_write16(client, REG_OUT_WIDTH_HIGH, width);
770 ret = reg_write16(client, REG_OUT_HEIGHT_HIGH, height);
772 /* Total width = output size + blanking */
774 ret = reg_write16(client, REG_OUT_TOTAL_WIDTH_HIGH, total_width);
776 ret = reg_write16(client, REG_OUT_TOTAL_HEIGHT_HIGH, total_height);
778 /* Sets the window for AWB calculations */
780 ret = reg_write16(client, REG_AVG_WINDOW_END_X_HIGH, width);
782 ret = reg_write16(client, REG_AVG_WINDOW_END_Y_HIGH, height);
787 static int ov5642_try_fmt(struct v4l2_subdev *sd,
788 struct v4l2_mbus_framefmt *mf)
790 struct i2c_client *client = v4l2_get_subdevdata(sd);
791 struct ov5642 *priv = to_ov5642(client);
792 const struct ov5642_datafmt *fmt = ov5642_find_datafmt(mf->code);
794 mf->width = priv->crop_rect.width;
795 mf->height = priv->crop_rect.height;
798 mf->code = ov5642_colour_fmts[0].code;
799 mf->colorspace = ov5642_colour_fmts[0].colorspace;
802 mf->field = V4L2_FIELD_NONE;
807 static int ov5642_s_fmt(struct v4l2_subdev *sd,
808 struct v4l2_mbus_framefmt *mf)
810 struct i2c_client *client = v4l2_get_subdevdata(sd);
811 struct ov5642 *priv = to_ov5642(client);
813 /* MIPI CSI could have changed the format, double-check */
814 if (!ov5642_find_datafmt(mf->code))
817 ov5642_try_fmt(sd, mf);
818 priv->fmt = ov5642_find_datafmt(mf->code);
823 static int ov5642_g_fmt(struct v4l2_subdev *sd,
824 struct v4l2_mbus_framefmt *mf)
826 struct i2c_client *client = v4l2_get_subdevdata(sd);
827 struct ov5642 *priv = to_ov5642(client);
829 const struct ov5642_datafmt *fmt = priv->fmt;
831 mf->code = fmt->code;
832 mf->colorspace = fmt->colorspace;
833 mf->width = priv->crop_rect.width;
834 mf->height = priv->crop_rect.height;
835 mf->field = V4L2_FIELD_NONE;
840 static int ov5642_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
841 enum v4l2_mbus_pixelcode *code)
843 if (index >= ARRAY_SIZE(ov5642_colour_fmts))
846 *code = ov5642_colour_fmts[index].code;
850 static int ov5642_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
852 struct i2c_client *client = v4l2_get_subdevdata(sd);
853 struct ov5642 *priv = to_ov5642(client);
854 struct v4l2_rect rect = a->c;
857 v4l_bound_align_image(&rect.width, 48, OV5642_MAX_WIDTH, 1,
858 &rect.height, 32, OV5642_MAX_HEIGHT, 1, 0);
860 priv->crop_rect.width = rect.width;
861 priv->crop_rect.height = rect.height;
862 priv->total_width = rect.width + BLANKING_EXTRA_WIDTH;
863 priv->total_height = max_t(int, rect.height +
864 BLANKING_EXTRA_HEIGHT,
865 BLANKING_MIN_HEIGHT);
866 priv->crop_rect.width = rect.width;
867 priv->crop_rect.height = rect.height;
869 ret = ov5642_write_array(client, ov5642_default_regs_init);
871 ret = ov5642_set_resolution(sd);
873 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
878 static int ov5642_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
880 struct i2c_client *client = v4l2_get_subdevdata(sd);
881 struct ov5642 *priv = to_ov5642(client);
882 struct v4l2_rect *rect = &a->c;
884 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
887 *rect = priv->crop_rect;
892 static int ov5642_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
896 a->bounds.width = OV5642_MAX_WIDTH;
897 a->bounds.height = OV5642_MAX_HEIGHT;
898 a->defrect = a->bounds;
899 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
900 a->pixelaspect.numerator = 1;
901 a->pixelaspect.denominator = 1;
906 static int ov5642_g_mbus_config(struct v4l2_subdev *sd,
907 struct v4l2_mbus_config *cfg)
909 cfg->type = V4L2_MBUS_CSI2;
910 cfg->flags = V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0 |
911 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
916 static int ov5642_s_power(struct v4l2_subdev *sd, int on)
918 struct i2c_client *client = v4l2_get_subdevdata(sd);
919 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
923 return soc_camera_power_off(&client->dev, ssdd);
925 ret = soc_camera_power_on(&client->dev, ssdd);
929 ret = ov5642_write_array(client, ov5642_default_regs_init);
931 ret = ov5642_set_resolution(sd);
933 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
938 static struct v4l2_subdev_video_ops ov5642_subdev_video_ops = {
939 .s_mbus_fmt = ov5642_s_fmt,
940 .g_mbus_fmt = ov5642_g_fmt,
941 .try_mbus_fmt = ov5642_try_fmt,
942 .enum_mbus_fmt = ov5642_enum_fmt,
943 .s_crop = ov5642_s_crop,
944 .g_crop = ov5642_g_crop,
945 .cropcap = ov5642_cropcap,
946 .g_mbus_config = ov5642_g_mbus_config,
949 static struct v4l2_subdev_core_ops ov5642_subdev_core_ops = {
950 .s_power = ov5642_s_power,
951 #ifdef CONFIG_VIDEO_ADV_DEBUG
952 .g_register = ov5642_get_register,
953 .s_register = ov5642_set_register,
957 static struct v4l2_subdev_ops ov5642_subdev_ops = {
958 .core = &ov5642_subdev_core_ops,
959 .video = &ov5642_subdev_video_ops,
962 static int ov5642_video_probe(struct i2c_client *client)
964 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
969 ret = ov5642_s_power(subdev, 1);
973 /* Read sensor Model ID */
974 ret = reg_read(client, REG_CHIP_ID_HIGH, &id_high);
980 ret = reg_read(client, REG_CHIP_ID_LOW, &id_low);
986 dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
996 ov5642_s_power(subdev, 0);
1000 static int ov5642_probe(struct i2c_client *client,
1001 const struct i2c_device_id *did)
1003 struct ov5642 *priv;
1004 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1007 dev_err(&client->dev, "OV5642: missing platform data!\n");
1011 priv = devm_kzalloc(&client->dev, sizeof(struct ov5642), GFP_KERNEL);
1015 v4l2_i2c_subdev_init(&priv->subdev, client, &ov5642_subdev_ops);
1017 priv->fmt = &ov5642_colour_fmts[0];
1019 priv->crop_rect.width = OV5642_DEFAULT_WIDTH;
1020 priv->crop_rect.height = OV5642_DEFAULT_HEIGHT;
1021 priv->crop_rect.left = (OV5642_MAX_WIDTH - OV5642_DEFAULT_WIDTH) / 2;
1022 priv->crop_rect.top = (OV5642_MAX_HEIGHT - OV5642_DEFAULT_HEIGHT) / 2;
1023 priv->total_width = OV5642_DEFAULT_WIDTH + BLANKING_EXTRA_WIDTH;
1024 priv->total_height = BLANKING_MIN_HEIGHT;
1026 return ov5642_video_probe(client);
1029 static int ov5642_remove(struct i2c_client *client)
1031 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1034 ssdd->free_bus(ssdd);
1039 static const struct i2c_device_id ov5642_id[] = {
1043 MODULE_DEVICE_TABLE(i2c, ov5642_id);
1045 static struct i2c_driver ov5642_i2c_driver = {
1049 .probe = ov5642_probe,
1050 .remove = ov5642_remove,
1051 .id_table = ov5642_id,
1054 module_i2c_driver(ov5642_i2c_driver);
1056 MODULE_DESCRIPTION("Omnivision OV5642 Camera driver");
1057 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1058 MODULE_LICENSE("GPL v2");