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-chip-ident.h>
28 #include <media/v4l2-subdev.h>
30 /* OV5642 registers */
31 #define REG_CHIP_ID_HIGH 0x300a
32 #define REG_CHIP_ID_LOW 0x300b
34 #define REG_WINDOW_START_X_HIGH 0x3800
35 #define REG_WINDOW_START_X_LOW 0x3801
36 #define REG_WINDOW_START_Y_HIGH 0x3802
37 #define REG_WINDOW_START_Y_LOW 0x3803
38 #define REG_WINDOW_WIDTH_HIGH 0x3804
39 #define REG_WINDOW_WIDTH_LOW 0x3805
40 #define REG_WINDOW_HEIGHT_HIGH 0x3806
41 #define REG_WINDOW_HEIGHT_LOW 0x3807
42 #define REG_OUT_WIDTH_HIGH 0x3808
43 #define REG_OUT_WIDTH_LOW 0x3809
44 #define REG_OUT_HEIGHT_HIGH 0x380a
45 #define REG_OUT_HEIGHT_LOW 0x380b
46 #define REG_OUT_TOTAL_WIDTH_HIGH 0x380c
47 #define REG_OUT_TOTAL_WIDTH_LOW 0x380d
48 #define REG_OUT_TOTAL_HEIGHT_HIGH 0x380e
49 #define REG_OUT_TOTAL_HEIGHT_LOW 0x380f
50 #define REG_OUTPUT_FORMAT 0x4300
51 #define REG_ISP_CTRL_01 0x5001
52 #define REG_AVG_WINDOW_END_X_HIGH 0x5682
53 #define REG_AVG_WINDOW_END_X_LOW 0x5683
54 #define REG_AVG_WINDOW_END_Y_HIGH 0x5686
55 #define REG_AVG_WINDOW_END_Y_LOW 0x5687
57 /* active pixel array size */
58 #define OV5642_SENSOR_SIZE_X 2592
59 #define OV5642_SENSOR_SIZE_Y 1944
62 * About OV5642 resolution, cropping and binning:
63 * This sensor supports it all, at least in the feature description.
64 * Unfortunately, no combination of appropriate registers settings could make
65 * the chip work the intended way. As it works with predefined register lists,
66 * some undocumented registers are presumably changed there to achieve their
68 * This driver currently only works for resolutions up to 720 lines with a
69 * 1:1 scale. Hopefully these restrictions will be removed in the future.
71 #define OV5642_MAX_WIDTH OV5642_SENSOR_SIZE_X
72 #define OV5642_MAX_HEIGHT 720
75 #define OV5642_DEFAULT_WIDTH 1280
76 #define OV5642_DEFAULT_HEIGHT OV5642_MAX_HEIGHT
78 /* minimum extra blanking */
79 #define BLANKING_EXTRA_WIDTH 500
80 #define BLANKING_EXTRA_HEIGHT 20
83 * the sensor's autoexposure is buggy when setting total_height low.
84 * It tries to expose longer than 1 frame period without taking care of it
85 * and this leads to weird output. So we set 1000 lines as minimum.
87 #define BLANKING_MIN_HEIGHT 1000
94 static struct regval_list ov5642_default_regs_init[] = {
555 static struct regval_list ov5642_default_regs_finalise[] = {
588 { 0x4300, 0x32 }, /* UYVY */
604 struct ov5642_datafmt {
605 enum v4l2_mbus_pixelcode code;
606 enum v4l2_colorspace colorspace;
610 struct v4l2_subdev subdev;
611 const struct ov5642_datafmt *fmt;
612 struct v4l2_rect crop_rect;
614 /* blanking information */
619 static const struct ov5642_datafmt ov5642_colour_fmts[] = {
620 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
623 static struct ov5642 *to_ov5642(const struct i2c_client *client)
625 return container_of(i2c_get_clientdata(client), struct ov5642, subdev);
628 /* Find a data format by a pixel code in an array */
629 static const struct ov5642_datafmt
630 *ov5642_find_datafmt(enum v4l2_mbus_pixelcode code)
634 for (i = 0; i < ARRAY_SIZE(ov5642_colour_fmts); i++)
635 if (ov5642_colour_fmts[i].code == code)
636 return ov5642_colour_fmts + i;
641 static int reg_read(struct i2c_client *client, u16 reg, u8 *val)
644 /* We have 16-bit i2c addresses - care for endianess */
645 unsigned char data[2] = { reg >> 8, reg & 0xff };
647 ret = i2c_master_send(client, data, 2);
649 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
651 return ret < 0 ? ret : -EIO;
654 ret = i2c_master_recv(client, val, 1);
656 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
658 return ret < 0 ? ret : -EIO;
663 static int reg_write(struct i2c_client *client, u16 reg, u8 val)
666 unsigned char data[3] = { reg >> 8, reg & 0xff, val };
668 ret = i2c_master_send(client, data, 3);
670 dev_err(&client->dev, "%s: i2c write error, reg: %x\n",
672 return ret < 0 ? ret : -EIO;
679 * convenience function to write 16 bit register values that are split up
680 * into two consecutive high and low parts
682 static int reg_write16(struct i2c_client *client, u16 reg, u16 val16)
686 ret = reg_write(client, reg, val16 >> 8);
689 return reg_write(client, reg + 1, val16 & 0x00ff);
692 #ifdef CONFIG_VIDEO_ADV_DEBUG
693 static int ov5642_get_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
695 struct i2c_client *client = v4l2_get_subdevdata(sd);
699 if (reg->reg & ~0xffff)
704 ret = reg_read(client, reg->reg, &val);
706 reg->val = (__u64)val;
711 static int ov5642_set_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
713 struct i2c_client *client = v4l2_get_subdevdata(sd);
715 if (reg->reg & ~0xffff || reg->val & ~0xff)
718 return reg_write(client, reg->reg, reg->val);
722 static int ov5642_write_array(struct i2c_client *client,
723 struct regval_list *vals)
725 while (vals->reg_num != 0xffff || vals->value != 0xff) {
726 int ret = reg_write(client, vals->reg_num, vals->value);
731 dev_dbg(&client->dev, "Register list loaded\n");
735 static int ov5642_set_resolution(struct v4l2_subdev *sd)
737 struct i2c_client *client = v4l2_get_subdevdata(sd);
738 struct ov5642 *priv = to_ov5642(client);
739 int width = priv->crop_rect.width;
740 int height = priv->crop_rect.height;
741 int total_width = priv->total_width;
742 int total_height = priv->total_height;
743 int start_x = (OV5642_SENSOR_SIZE_X - width) / 2;
744 int start_y = (OV5642_SENSOR_SIZE_Y - height) / 2;
748 * This should set the starting point for cropping.
749 * Doesn't work so far.
751 ret = reg_write16(client, REG_WINDOW_START_X_HIGH, start_x);
753 ret = reg_write16(client, REG_WINDOW_START_Y_HIGH, start_y);
755 priv->crop_rect.left = start_x;
756 priv->crop_rect.top = start_y;
760 ret = reg_write16(client, REG_WINDOW_WIDTH_HIGH, width);
762 ret = reg_write16(client, REG_WINDOW_HEIGHT_HIGH, height);
765 priv->crop_rect.width = width;
766 priv->crop_rect.height = height;
768 /* Set the output window size. Only 1:1 scale is supported so far. */
769 ret = reg_write16(client, REG_OUT_WIDTH_HIGH, width);
771 ret = reg_write16(client, REG_OUT_HEIGHT_HIGH, height);
773 /* Total width = output size + blanking */
775 ret = reg_write16(client, REG_OUT_TOTAL_WIDTH_HIGH, total_width);
777 ret = reg_write16(client, REG_OUT_TOTAL_HEIGHT_HIGH, total_height);
779 /* Sets the window for AWB calculations */
781 ret = reg_write16(client, REG_AVG_WINDOW_END_X_HIGH, width);
783 ret = reg_write16(client, REG_AVG_WINDOW_END_Y_HIGH, height);
788 static int ov5642_try_fmt(struct v4l2_subdev *sd,
789 struct v4l2_mbus_framefmt *mf)
791 struct i2c_client *client = v4l2_get_subdevdata(sd);
792 struct ov5642 *priv = to_ov5642(client);
793 const struct ov5642_datafmt *fmt = ov5642_find_datafmt(mf->code);
795 mf->width = priv->crop_rect.width;
796 mf->height = priv->crop_rect.height;
799 mf->code = ov5642_colour_fmts[0].code;
800 mf->colorspace = ov5642_colour_fmts[0].colorspace;
803 mf->field = V4L2_FIELD_NONE;
808 static int ov5642_s_fmt(struct v4l2_subdev *sd,
809 struct v4l2_mbus_framefmt *mf)
811 struct i2c_client *client = v4l2_get_subdevdata(sd);
812 struct ov5642 *priv = to_ov5642(client);
814 /* MIPI CSI could have changed the format, double-check */
815 if (!ov5642_find_datafmt(mf->code))
818 ov5642_try_fmt(sd, mf);
819 priv->fmt = ov5642_find_datafmt(mf->code);
824 static int ov5642_g_fmt(struct v4l2_subdev *sd,
825 struct v4l2_mbus_framefmt *mf)
827 struct i2c_client *client = v4l2_get_subdevdata(sd);
828 struct ov5642 *priv = to_ov5642(client);
830 const struct ov5642_datafmt *fmt = priv->fmt;
832 mf->code = fmt->code;
833 mf->colorspace = fmt->colorspace;
834 mf->width = priv->crop_rect.width;
835 mf->height = priv->crop_rect.height;
836 mf->field = V4L2_FIELD_NONE;
841 static int ov5642_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
842 enum v4l2_mbus_pixelcode *code)
844 if (index >= ARRAY_SIZE(ov5642_colour_fmts))
847 *code = ov5642_colour_fmts[index].code;
851 static int ov5642_g_chip_ident(struct v4l2_subdev *sd,
852 struct v4l2_dbg_chip_ident *id)
854 struct i2c_client *client = v4l2_get_subdevdata(sd);
856 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
859 if (id->match.addr != client->addr)
862 id->ident = V4L2_IDENT_OV5642;
868 static int ov5642_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
870 struct i2c_client *client = v4l2_get_subdevdata(sd);
871 struct ov5642 *priv = to_ov5642(client);
872 struct v4l2_rect rect = a->c;
875 v4l_bound_align_image(&rect.width, 48, OV5642_MAX_WIDTH, 1,
876 &rect.height, 32, OV5642_MAX_HEIGHT, 1, 0);
878 priv->crop_rect.width = rect.width;
879 priv->crop_rect.height = rect.height;
880 priv->total_width = rect.width + BLANKING_EXTRA_WIDTH;
881 priv->total_height = max_t(int, rect.height +
882 BLANKING_EXTRA_HEIGHT,
883 BLANKING_MIN_HEIGHT);
884 priv->crop_rect.width = rect.width;
885 priv->crop_rect.height = rect.height;
887 ret = ov5642_write_array(client, ov5642_default_regs_init);
889 ret = ov5642_set_resolution(sd);
891 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
896 static int ov5642_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
898 struct i2c_client *client = v4l2_get_subdevdata(sd);
899 struct ov5642 *priv = to_ov5642(client);
900 struct v4l2_rect *rect = &a->c;
902 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
905 *rect = priv->crop_rect;
910 static int ov5642_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
914 a->bounds.width = OV5642_MAX_WIDTH;
915 a->bounds.height = OV5642_MAX_HEIGHT;
916 a->defrect = a->bounds;
917 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
918 a->pixelaspect.numerator = 1;
919 a->pixelaspect.denominator = 1;
924 static int ov5642_g_mbus_config(struct v4l2_subdev *sd,
925 struct v4l2_mbus_config *cfg)
927 cfg->type = V4L2_MBUS_CSI2;
928 cfg->flags = V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0 |
929 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
934 static int ov5642_s_power(struct v4l2_subdev *sd, int on)
936 struct i2c_client *client = v4l2_get_subdevdata(sd);
937 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
941 return soc_camera_power_off(&client->dev, ssdd);
943 ret = soc_camera_power_on(&client->dev, ssdd);
947 ret = ov5642_write_array(client, ov5642_default_regs_init);
949 ret = ov5642_set_resolution(sd);
951 ret = ov5642_write_array(client, ov5642_default_regs_finalise);
956 static struct v4l2_subdev_video_ops ov5642_subdev_video_ops = {
957 .s_mbus_fmt = ov5642_s_fmt,
958 .g_mbus_fmt = ov5642_g_fmt,
959 .try_mbus_fmt = ov5642_try_fmt,
960 .enum_mbus_fmt = ov5642_enum_fmt,
961 .s_crop = ov5642_s_crop,
962 .g_crop = ov5642_g_crop,
963 .cropcap = ov5642_cropcap,
964 .g_mbus_config = ov5642_g_mbus_config,
967 static struct v4l2_subdev_core_ops ov5642_subdev_core_ops = {
968 .s_power = ov5642_s_power,
969 .g_chip_ident = ov5642_g_chip_ident,
970 #ifdef CONFIG_VIDEO_ADV_DEBUG
971 .g_register = ov5642_get_register,
972 .s_register = ov5642_set_register,
976 static struct v4l2_subdev_ops ov5642_subdev_ops = {
977 .core = &ov5642_subdev_core_ops,
978 .video = &ov5642_subdev_video_ops,
981 static int ov5642_video_probe(struct i2c_client *client)
983 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
988 ret = ov5642_s_power(subdev, 1);
992 /* Read sensor Model ID */
993 ret = reg_read(client, REG_CHIP_ID_HIGH, &id_high);
999 ret = reg_read(client, REG_CHIP_ID_LOW, &id_low);
1005 dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
1015 ov5642_s_power(subdev, 0);
1019 static int ov5642_probe(struct i2c_client *client,
1020 const struct i2c_device_id *did)
1022 struct ov5642 *priv;
1023 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1026 dev_err(&client->dev, "OV5642: missing platform data!\n");
1030 priv = devm_kzalloc(&client->dev, sizeof(struct ov5642), GFP_KERNEL);
1034 v4l2_i2c_subdev_init(&priv->subdev, client, &ov5642_subdev_ops);
1036 priv->fmt = &ov5642_colour_fmts[0];
1038 priv->crop_rect.width = OV5642_DEFAULT_WIDTH;
1039 priv->crop_rect.height = OV5642_DEFAULT_HEIGHT;
1040 priv->crop_rect.left = (OV5642_MAX_WIDTH - OV5642_DEFAULT_WIDTH) / 2;
1041 priv->crop_rect.top = (OV5642_MAX_HEIGHT - OV5642_DEFAULT_HEIGHT) / 2;
1042 priv->total_width = OV5642_DEFAULT_WIDTH + BLANKING_EXTRA_WIDTH;
1043 priv->total_height = BLANKING_MIN_HEIGHT;
1045 return ov5642_video_probe(client);
1048 static int ov5642_remove(struct i2c_client *client)
1050 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
1053 ssdd->free_bus(ssdd);
1058 static const struct i2c_device_id ov5642_id[] = {
1062 MODULE_DEVICE_TABLE(i2c, ov5642_id);
1064 static struct i2c_driver ov5642_i2c_driver = {
1068 .probe = ov5642_probe,
1069 .remove = ov5642_remove,
1070 .id_table = ov5642_id,
1073 module_i2c_driver(ov5642_i2c_driver);
1075 MODULE_DESCRIPTION("Omnivision OV5642 Camera driver");
1076 MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1077 MODULE_LICENSE("GPL v2");