1 // SPDX-License-Identifier: GPL-2.0
3 * A V4L2 driver for Sony IMX219 cameras.
4 * Copyright (C) 2019, Raspberry Pi (Trading) Ltd
6 * Based on Sony imx258 camera driver
7 * Copyright (C) 2018 Intel Corporation
9 * DT / fwnode changes, and regulator / GPIO control taken from imx214 driver
10 * Copyright 2018 Qtechnology A/S
12 * Flip handling taken from the Sony IMX319 driver.
13 * Copyright (C) 2018 Intel Corporation
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/module.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-fwnode.h>
28 #include <media/v4l2-mediabus.h>
29 #include <asm/unaligned.h>
31 #define IMX219_REG_VALUE_08BIT 1
32 #define IMX219_REG_VALUE_16BIT 2
34 #define IMX219_REG_MODE_SELECT 0x0100
35 #define IMX219_MODE_STANDBY 0x00
36 #define IMX219_MODE_STREAMING 0x01
39 #define IMX219_REG_CHIP_ID 0x0000
40 #define IMX219_CHIP_ID 0x0219
42 /* External clock frequency is 24.0M */
43 #define IMX219_XCLK_FREQ 24000000
45 /* Pixel rate is fixed at 182.4M for all the modes */
46 #define IMX219_PIXEL_RATE 182400000
48 #define IMX219_DEFAULT_LINK_FREQ 456000000
50 /* V_TIMING internal */
51 #define IMX219_REG_VTS 0x0160
52 #define IMX219_VTS_15FPS 0x0dc6
53 #define IMX219_VTS_30FPS_1080P 0x06e3
54 #define IMX219_VTS_30FPS_BINNED 0x06e3
55 #define IMX219_VTS_30FPS_640x480 0x06e3
56 #define IMX219_VTS_MAX 0xffff
58 #define IMX219_VBLANK_MIN 4
61 #define IMX219_FLL_MIN 0x08a6
62 #define IMX219_FLL_MAX 0xffff
63 #define IMX219_FLL_STEP 1
64 #define IMX219_FLL_DEFAULT 0x0c98
66 /* HBLANK control - read only */
67 #define IMX219_PPL_DEFAULT 3448
69 /* Exposure control */
70 #define IMX219_REG_EXPOSURE 0x015a
71 #define IMX219_EXPOSURE_MIN 4
72 #define IMX219_EXPOSURE_STEP 1
73 #define IMX219_EXPOSURE_DEFAULT 0x640
74 #define IMX219_EXPOSURE_MAX 65535
76 /* Analog gain control */
77 #define IMX219_REG_ANALOG_GAIN 0x0157
78 #define IMX219_ANA_GAIN_MIN 0
79 #define IMX219_ANA_GAIN_MAX 232
80 #define IMX219_ANA_GAIN_STEP 1
81 #define IMX219_ANA_GAIN_DEFAULT 0x0
83 /* Digital gain control */
84 #define IMX219_REG_DIGITAL_GAIN 0x0158
85 #define IMX219_DGTL_GAIN_MIN 0x0100
86 #define IMX219_DGTL_GAIN_MAX 0x0fff
87 #define IMX219_DGTL_GAIN_DEFAULT 0x0100
88 #define IMX219_DGTL_GAIN_STEP 1
90 #define IMX219_REG_ORIENTATION 0x0172
92 /* Test Pattern Control */
93 #define IMX219_REG_TEST_PATTERN 0x0600
94 #define IMX219_TEST_PATTERN_DISABLE 0
95 #define IMX219_TEST_PATTERN_SOLID_COLOR 1
96 #define IMX219_TEST_PATTERN_COLOR_BARS 2
97 #define IMX219_TEST_PATTERN_GREY_COLOR 3
98 #define IMX219_TEST_PATTERN_PN9 4
100 /* Test pattern colour components */
101 #define IMX219_REG_TESTP_RED 0x0602
102 #define IMX219_REG_TESTP_GREENR 0x0604
103 #define IMX219_REG_TESTP_BLUE 0x0606
104 #define IMX219_REG_TESTP_GREENB 0x0608
105 #define IMX219_TESTP_COLOUR_MIN 0
106 #define IMX219_TESTP_COLOUR_MAX 0x03ff
107 #define IMX219_TESTP_COLOUR_STEP 1
108 #define IMX219_TESTP_RED_DEFAULT IMX219_TESTP_COLOUR_MAX
109 #define IMX219_TESTP_GREENR_DEFAULT 0
110 #define IMX219_TESTP_BLUE_DEFAULT 0
111 #define IMX219_TESTP_GREENB_DEFAULT 0
113 /* IMX219 native and active pixel array size. */
114 #define IMX219_NATIVE_WIDTH 3296U
115 #define IMX219_NATIVE_HEIGHT 2480U
116 #define IMX219_PIXEL_ARRAY_LEFT 8U
117 #define IMX219_PIXEL_ARRAY_TOP 8U
118 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
119 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
126 struct imx219_reg_list {
127 unsigned int num_of_regs;
128 const struct imx219_reg *regs;
131 /* Mode : resolution and related config&values */
138 /* Analog crop rectangle. */
139 struct v4l2_rect crop;
142 unsigned int vts_def;
144 /* Default register values */
145 struct imx219_reg_list reg_list;
149 * Register sets lifted off the i2C interface from the Raspberry Pi firmware
151 * 3280x2464 = mode 2, 1920x1080 = mode 1, 1640x1232 = mode 4, 640x480 = mode 7.
153 static const struct imx219_reg mode_3280x2464_regs[] = {
210 static const struct imx219_reg mode_1920_1080_regs[] = {
269 static const struct imx219_reg mode_1640_1232_regs[] = {
326 static const struct imx219_reg mode_640_480_regs[] = {
383 static const struct imx219_reg raw8_framefmt_regs[] = {
389 static const struct imx219_reg raw10_framefmt_regs[] = {
395 static const char * const imx219_test_pattern_menu[] = {
403 static const int imx219_test_pattern_val[] = {
404 IMX219_TEST_PATTERN_DISABLE,
405 IMX219_TEST_PATTERN_COLOR_BARS,
406 IMX219_TEST_PATTERN_SOLID_COLOR,
407 IMX219_TEST_PATTERN_GREY_COLOR,
408 IMX219_TEST_PATTERN_PN9,
411 /* regulator supplies */
412 static const char * const imx219_supply_name[] = {
413 /* Supplies can be enabled in any order */
414 "VANA", /* Analog (2.8V) supply */
415 "VDIG", /* Digital Core (1.8V) supply */
416 "VDDL", /* IF (1.2V) supply */
419 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
422 * The supported formats.
423 * This table MUST contain 4 entries per format, to cover the various flip
424 * combinations in the order
430 static const u32 codes[] = {
431 MEDIA_BUS_FMT_SRGGB10_1X10,
432 MEDIA_BUS_FMT_SGRBG10_1X10,
433 MEDIA_BUS_FMT_SGBRG10_1X10,
434 MEDIA_BUS_FMT_SBGGR10_1X10,
436 MEDIA_BUS_FMT_SRGGB8_1X8,
437 MEDIA_BUS_FMT_SGRBG8_1X8,
438 MEDIA_BUS_FMT_SGBRG8_1X8,
439 MEDIA_BUS_FMT_SBGGR8_1X8,
443 * Initialisation delay between XCLR low->high and the moment when the sensor
444 * can start capture (i.e. can leave software stanby) must be not less than:
445 * t4 + max(t5, t6 + <time to initialize the sensor register over I2C>)
447 * t4 is fixed, and is max 200uS,
448 * t5 is fixed, and is 6000uS,
449 * t6 depends on the sensor external clock, and is max 32000 clock periods.
450 * As per sensor datasheet, the external clock must be from 6MHz to 27MHz.
451 * So for any acceptable external clock t6 is always within the range of
452 * 1185 to 5333 uS, and is always less than t5.
453 * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then
454 * initialize the sensor over I2C, and then exit the software standby.
456 * This start-up time can be optimized a bit more, if we start the writes
457 * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor
458 * initialization over I2C may complete before (t4+t5) expires, and we must
459 * ensure that capture is not started before (t4+t5).
461 * This delay doesn't account for the power supply startup time. If needed,
462 * this should be taken care of via the regulator framework. E.g. in the
463 * case of DT for regulator-fixed one should define the startup-delay-us
466 #define IMX219_XCLR_MIN_DELAY_US 6200
467 #define IMX219_XCLR_DELAY_RANGE_US 1000
470 static const struct imx219_mode supported_modes[] = {
472 /* 8MPix 15fps mode */
476 .left = IMX219_PIXEL_ARRAY_LEFT,
477 .top = IMX219_PIXEL_ARRAY_TOP,
481 .vts_def = IMX219_VTS_15FPS,
483 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
484 .regs = mode_3280x2464_regs,
488 /* 1080P 30fps cropped */
497 .vts_def = IMX219_VTS_30FPS_1080P,
499 .num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
500 .regs = mode_1920_1080_regs,
504 /* 2x2 binned 30fps mode */
508 .left = IMX219_PIXEL_ARRAY_LEFT,
509 .top = IMX219_PIXEL_ARRAY_TOP,
513 .vts_def = IMX219_VTS_30FPS_BINNED,
515 .num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
516 .regs = mode_1640_1232_regs,
520 /* 640x480 30fps mode */
529 .vts_def = IMX219_VTS_30FPS_640x480,
531 .num_of_regs = ARRAY_SIZE(mode_640_480_regs),
532 .regs = mode_640_480_regs,
538 struct v4l2_subdev sd;
539 struct media_pad pad;
541 struct v4l2_mbus_framefmt fmt;
543 struct clk *xclk; /* system clock to IMX219 */
546 struct gpio_desc *reset_gpio;
547 struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES];
549 struct v4l2_ctrl_handler ctrl_handler;
551 struct v4l2_ctrl *pixel_rate;
552 struct v4l2_ctrl *exposure;
553 struct v4l2_ctrl *vflip;
554 struct v4l2_ctrl *hflip;
555 struct v4l2_ctrl *vblank;
556 struct v4l2_ctrl *hblank;
559 const struct imx219_mode *mode;
562 * Mutex for serialized access:
563 * Protect sensor module set pad format and start/stop streaming safely.
567 /* Streaming on/off */
571 static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd)
573 return container_of(_sd, struct imx219, sd);
576 /* Read registers up to 2 at a time */
577 static int imx219_read_reg(struct imx219 *imx219, u16 reg, u32 len, u32 *val)
579 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
580 struct i2c_msg msgs[2];
581 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
582 u8 data_buf[4] = { 0, };
588 /* Write register address */
589 msgs[0].addr = client->addr;
591 msgs[0].len = ARRAY_SIZE(addr_buf);
592 msgs[0].buf = addr_buf;
594 /* Read data from register */
595 msgs[1].addr = client->addr;
596 msgs[1].flags = I2C_M_RD;
598 msgs[1].buf = &data_buf[4 - len];
600 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
601 if (ret != ARRAY_SIZE(msgs))
604 *val = get_unaligned_be32(data_buf);
609 /* Write registers up to 2 at a time */
610 static int imx219_write_reg(struct imx219 *imx219, u16 reg, u32 len, u32 val)
612 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
618 put_unaligned_be16(reg, buf);
619 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
620 if (i2c_master_send(client, buf, len + 2) != len + 2)
626 /* Write a list of registers */
627 static int imx219_write_regs(struct imx219 *imx219,
628 const struct imx219_reg *regs, u32 len)
630 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
634 for (i = 0; i < len; i++) {
635 ret = imx219_write_reg(imx219, regs[i].address, 1, regs[i].val);
637 dev_err_ratelimited(&client->dev,
638 "Failed to write reg 0x%4.4x. error = %d\n",
639 regs[i].address, ret);
648 /* Get bayer order based on flip setting. */
649 static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
653 lockdep_assert_held(&imx219->mutex);
655 for (i = 0; i < ARRAY_SIZE(codes); i++)
656 if (codes[i] == code)
659 if (i >= ARRAY_SIZE(codes))
662 i = (i & ~3) | (imx219->vflip->val ? 2 : 0) |
663 (imx219->hflip->val ? 1 : 0);
668 static void imx219_set_default_format(struct imx219 *imx219)
670 struct v4l2_mbus_framefmt *fmt;
673 fmt->code = MEDIA_BUS_FMT_SRGGB10_1X10;
674 fmt->colorspace = V4L2_COLORSPACE_SRGB;
675 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
676 fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
679 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
680 fmt->width = supported_modes[0].width;
681 fmt->height = supported_modes[0].height;
682 fmt->field = V4L2_FIELD_NONE;
685 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
687 struct imx219 *imx219 = to_imx219(sd);
688 struct v4l2_mbus_framefmt *try_fmt =
689 v4l2_subdev_get_try_format(sd, fh->pad, 0);
690 struct v4l2_rect *try_crop;
692 mutex_lock(&imx219->mutex);
694 /* Initialize try_fmt */
695 try_fmt->width = supported_modes[0].width;
696 try_fmt->height = supported_modes[0].height;
697 try_fmt->code = imx219_get_format_code(imx219,
698 MEDIA_BUS_FMT_SRGGB10_1X10);
699 try_fmt->field = V4L2_FIELD_NONE;
701 /* Initialize try_crop rectangle. */
702 try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
703 try_crop->top = IMX219_PIXEL_ARRAY_TOP;
704 try_crop->left = IMX219_PIXEL_ARRAY_LEFT;
705 try_crop->width = IMX219_PIXEL_ARRAY_WIDTH;
706 try_crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
708 mutex_unlock(&imx219->mutex);
713 static int imx219_set_ctrl(struct v4l2_ctrl *ctrl)
715 struct imx219 *imx219 =
716 container_of(ctrl->handler, struct imx219, ctrl_handler);
717 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
720 if (ctrl->id == V4L2_CID_VBLANK) {
721 int exposure_max, exposure_def;
723 /* Update max exposure while meeting expected vblanking */
724 exposure_max = imx219->mode->height + ctrl->val - 4;
725 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
726 exposure_max : IMX219_EXPOSURE_DEFAULT;
727 __v4l2_ctrl_modify_range(imx219->exposure,
728 imx219->exposure->minimum,
729 exposure_max, imx219->exposure->step,
734 * Applying V4L2 control value only happens
735 * when power is up for streaming
737 if (pm_runtime_get_if_in_use(&client->dev) == 0)
741 case V4L2_CID_ANALOGUE_GAIN:
742 ret = imx219_write_reg(imx219, IMX219_REG_ANALOG_GAIN,
743 IMX219_REG_VALUE_08BIT, ctrl->val);
745 case V4L2_CID_EXPOSURE:
746 ret = imx219_write_reg(imx219, IMX219_REG_EXPOSURE,
747 IMX219_REG_VALUE_16BIT, ctrl->val);
749 case V4L2_CID_DIGITAL_GAIN:
750 ret = imx219_write_reg(imx219, IMX219_REG_DIGITAL_GAIN,
751 IMX219_REG_VALUE_16BIT, ctrl->val);
753 case V4L2_CID_TEST_PATTERN:
754 ret = imx219_write_reg(imx219, IMX219_REG_TEST_PATTERN,
755 IMX219_REG_VALUE_16BIT,
756 imx219_test_pattern_val[ctrl->val]);
760 ret = imx219_write_reg(imx219, IMX219_REG_ORIENTATION, 1,
762 imx219->vflip->val << 1);
764 case V4L2_CID_VBLANK:
765 ret = imx219_write_reg(imx219, IMX219_REG_VTS,
766 IMX219_REG_VALUE_16BIT,
767 imx219->mode->height + ctrl->val);
769 case V4L2_CID_TEST_PATTERN_RED:
770 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_RED,
771 IMX219_REG_VALUE_16BIT, ctrl->val);
773 case V4L2_CID_TEST_PATTERN_GREENR:
774 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENR,
775 IMX219_REG_VALUE_16BIT, ctrl->val);
777 case V4L2_CID_TEST_PATTERN_BLUE:
778 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_BLUE,
779 IMX219_REG_VALUE_16BIT, ctrl->val);
781 case V4L2_CID_TEST_PATTERN_GREENB:
782 ret = imx219_write_reg(imx219, IMX219_REG_TESTP_GREENB,
783 IMX219_REG_VALUE_16BIT, ctrl->val);
786 dev_info(&client->dev,
787 "ctrl(id:0x%x,val:0x%x) is not handled\n",
788 ctrl->id, ctrl->val);
793 pm_runtime_put(&client->dev);
798 static const struct v4l2_ctrl_ops imx219_ctrl_ops = {
799 .s_ctrl = imx219_set_ctrl,
802 static int imx219_enum_mbus_code(struct v4l2_subdev *sd,
803 struct v4l2_subdev_pad_config *cfg,
804 struct v4l2_subdev_mbus_code_enum *code)
806 struct imx219 *imx219 = to_imx219(sd);
808 if (code->index >= (ARRAY_SIZE(codes) / 4))
811 code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
816 static int imx219_enum_frame_size(struct v4l2_subdev *sd,
817 struct v4l2_subdev_pad_config *cfg,
818 struct v4l2_subdev_frame_size_enum *fse)
820 struct imx219 *imx219 = to_imx219(sd);
822 if (fse->index >= ARRAY_SIZE(supported_modes))
825 if (fse->code != imx219_get_format_code(imx219, fse->code))
828 fse->min_width = supported_modes[fse->index].width;
829 fse->max_width = fse->min_width;
830 fse->min_height = supported_modes[fse->index].height;
831 fse->max_height = fse->min_height;
836 static void imx219_reset_colorspace(struct v4l2_mbus_framefmt *fmt)
838 fmt->colorspace = V4L2_COLORSPACE_SRGB;
839 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
840 fmt->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
843 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
846 static void imx219_update_pad_format(struct imx219 *imx219,
847 const struct imx219_mode *mode,
848 struct v4l2_subdev_format *fmt)
850 fmt->format.width = mode->width;
851 fmt->format.height = mode->height;
852 fmt->format.field = V4L2_FIELD_NONE;
853 imx219_reset_colorspace(&fmt->format);
856 static int __imx219_get_pad_format(struct imx219 *imx219,
857 struct v4l2_subdev_pad_config *cfg,
858 struct v4l2_subdev_format *fmt)
860 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
861 struct v4l2_mbus_framefmt *try_fmt =
862 v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad);
863 /* update the code which could change due to vflip or hflip: */
864 try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
865 fmt->format = *try_fmt;
867 imx219_update_pad_format(imx219, imx219->mode, fmt);
868 fmt->format.code = imx219_get_format_code(imx219,
875 static int imx219_get_pad_format(struct v4l2_subdev *sd,
876 struct v4l2_subdev_pad_config *cfg,
877 struct v4l2_subdev_format *fmt)
879 struct imx219 *imx219 = to_imx219(sd);
882 mutex_lock(&imx219->mutex);
883 ret = __imx219_get_pad_format(imx219, cfg, fmt);
884 mutex_unlock(&imx219->mutex);
889 static int imx219_set_pad_format(struct v4l2_subdev *sd,
890 struct v4l2_subdev_pad_config *cfg,
891 struct v4l2_subdev_format *fmt)
893 struct imx219 *imx219 = to_imx219(sd);
894 const struct imx219_mode *mode;
895 struct v4l2_mbus_framefmt *framefmt;
896 int exposure_max, exposure_def, hblank;
899 mutex_lock(&imx219->mutex);
901 for (i = 0; i < ARRAY_SIZE(codes); i++)
902 if (codes[i] == fmt->format.code)
904 if (i >= ARRAY_SIZE(codes))
907 /* Bayer order varies with flips */
908 fmt->format.code = imx219_get_format_code(imx219, codes[i]);
910 mode = v4l2_find_nearest_size(supported_modes,
911 ARRAY_SIZE(supported_modes),
913 fmt->format.width, fmt->format.height);
914 imx219_update_pad_format(imx219, mode, fmt);
915 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
916 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
917 *framefmt = fmt->format;
918 } else if (imx219->mode != mode ||
919 imx219->fmt.code != fmt->format.code) {
920 imx219->fmt = fmt->format;
922 /* Update limits and set FPS to default */
923 __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
924 IMX219_VTS_MAX - mode->height, 1,
925 mode->vts_def - mode->height);
926 __v4l2_ctrl_s_ctrl(imx219->vblank,
927 mode->vts_def - mode->height);
928 /* Update max exposure while meeting expected vblanking */
929 exposure_max = mode->vts_def - 4;
930 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
931 exposure_max : IMX219_EXPOSURE_DEFAULT;
932 __v4l2_ctrl_modify_range(imx219->exposure,
933 imx219->exposure->minimum,
934 exposure_max, imx219->exposure->step,
937 * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
938 * depends on mode->width only, and is not changeble in any
939 * way other than changing the mode.
941 hblank = IMX219_PPL_DEFAULT - mode->width;
942 __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
946 mutex_unlock(&imx219->mutex);
951 static int imx219_set_framefmt(struct imx219 *imx219)
953 switch (imx219->fmt.code) {
954 case MEDIA_BUS_FMT_SRGGB8_1X8:
955 case MEDIA_BUS_FMT_SGRBG8_1X8:
956 case MEDIA_BUS_FMT_SGBRG8_1X8:
957 case MEDIA_BUS_FMT_SBGGR8_1X8:
958 return imx219_write_regs(imx219, raw8_framefmt_regs,
959 ARRAY_SIZE(raw8_framefmt_regs));
961 case MEDIA_BUS_FMT_SRGGB10_1X10:
962 case MEDIA_BUS_FMT_SGRBG10_1X10:
963 case MEDIA_BUS_FMT_SGBRG10_1X10:
964 case MEDIA_BUS_FMT_SBGGR10_1X10:
965 return imx219_write_regs(imx219, raw10_framefmt_regs,
966 ARRAY_SIZE(raw10_framefmt_regs));
972 static const struct v4l2_rect *
973 __imx219_get_pad_crop(struct imx219 *imx219, struct v4l2_subdev_pad_config *cfg,
974 unsigned int pad, enum v4l2_subdev_format_whence which)
977 case V4L2_SUBDEV_FORMAT_TRY:
978 return v4l2_subdev_get_try_crop(&imx219->sd, cfg, pad);
979 case V4L2_SUBDEV_FORMAT_ACTIVE:
980 return &imx219->mode->crop;
986 static int imx219_get_selection(struct v4l2_subdev *sd,
987 struct v4l2_subdev_pad_config *cfg,
988 struct v4l2_subdev_selection *sel)
990 switch (sel->target) {
991 case V4L2_SEL_TGT_CROP: {
992 struct imx219 *imx219 = to_imx219(sd);
994 mutex_lock(&imx219->mutex);
995 sel->r = *__imx219_get_pad_crop(imx219, cfg, sel->pad,
997 mutex_unlock(&imx219->mutex);
1002 case V4L2_SEL_TGT_NATIVE_SIZE:
1005 sel->r.width = IMX219_NATIVE_WIDTH;
1006 sel->r.height = IMX219_NATIVE_HEIGHT;
1010 case V4L2_SEL_TGT_CROP_DEFAULT:
1011 case V4L2_SEL_TGT_CROP_BOUNDS:
1012 sel->r.top = IMX219_PIXEL_ARRAY_TOP;
1013 sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
1014 sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
1015 sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
1023 static int imx219_start_streaming(struct imx219 *imx219)
1025 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1026 const struct imx219_reg_list *reg_list;
1029 ret = pm_runtime_get_sync(&client->dev);
1031 pm_runtime_put_noidle(&client->dev);
1035 /* Apply default values of current mode */
1036 reg_list = &imx219->mode->reg_list;
1037 ret = imx219_write_regs(imx219, reg_list->regs, reg_list->num_of_regs);
1039 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1043 ret = imx219_set_framefmt(imx219);
1045 dev_err(&client->dev, "%s failed to set frame format: %d\n",
1050 /* Apply customized values from user */
1051 ret = __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler);
1055 /* set stream on register */
1056 ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1057 IMX219_REG_VALUE_08BIT, IMX219_MODE_STREAMING);
1061 /* vflip and hflip cannot change during streaming */
1062 __v4l2_ctrl_grab(imx219->vflip, true);
1063 __v4l2_ctrl_grab(imx219->hflip, true);
1068 pm_runtime_put(&client->dev);
1072 static void imx219_stop_streaming(struct imx219 *imx219)
1074 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1077 /* set stream off register */
1078 ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1079 IMX219_REG_VALUE_08BIT, IMX219_MODE_STANDBY);
1081 dev_err(&client->dev, "%s failed to set stream\n", __func__);
1083 __v4l2_ctrl_grab(imx219->vflip, false);
1084 __v4l2_ctrl_grab(imx219->hflip, false);
1086 pm_runtime_put(&client->dev);
1089 static int imx219_set_stream(struct v4l2_subdev *sd, int enable)
1091 struct imx219 *imx219 = to_imx219(sd);
1094 mutex_lock(&imx219->mutex);
1095 if (imx219->streaming == enable) {
1096 mutex_unlock(&imx219->mutex);
1102 * Apply default & customized values
1103 * and then start streaming.
1105 ret = imx219_start_streaming(imx219);
1109 imx219_stop_streaming(imx219);
1112 imx219->streaming = enable;
1114 mutex_unlock(&imx219->mutex);
1119 mutex_unlock(&imx219->mutex);
1124 /* Power/clock management functions */
1125 static int imx219_power_on(struct device *dev)
1127 struct i2c_client *client = to_i2c_client(dev);
1128 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1129 struct imx219 *imx219 = to_imx219(sd);
1132 ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
1135 dev_err(&client->dev, "%s: failed to enable regulators\n",
1140 ret = clk_prepare_enable(imx219->xclk);
1142 dev_err(&client->dev, "%s: failed to enable clock\n",
1147 gpiod_set_value_cansleep(imx219->reset_gpio, 1);
1148 usleep_range(IMX219_XCLR_MIN_DELAY_US,
1149 IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US);
1154 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1159 static int imx219_power_off(struct device *dev)
1161 struct i2c_client *client = to_i2c_client(dev);
1162 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1163 struct imx219 *imx219 = to_imx219(sd);
1165 gpiod_set_value_cansleep(imx219->reset_gpio, 0);
1166 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies);
1167 clk_disable_unprepare(imx219->xclk);
1172 static int __maybe_unused imx219_suspend(struct device *dev)
1174 struct i2c_client *client = to_i2c_client(dev);
1175 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1176 struct imx219 *imx219 = to_imx219(sd);
1178 if (imx219->streaming)
1179 imx219_stop_streaming(imx219);
1184 static int __maybe_unused imx219_resume(struct device *dev)
1186 struct i2c_client *client = to_i2c_client(dev);
1187 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1188 struct imx219 *imx219 = to_imx219(sd);
1191 if (imx219->streaming) {
1192 ret = imx219_start_streaming(imx219);
1200 imx219_stop_streaming(imx219);
1201 imx219->streaming = false;
1206 static int imx219_get_regulators(struct imx219 *imx219)
1208 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1211 for (i = 0; i < IMX219_NUM_SUPPLIES; i++)
1212 imx219->supplies[i].supply = imx219_supply_name[i];
1214 return devm_regulator_bulk_get(&client->dev,
1215 IMX219_NUM_SUPPLIES,
1219 /* Verify chip ID */
1220 static int imx219_identify_module(struct imx219 *imx219)
1222 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1226 ret = imx219_read_reg(imx219, IMX219_REG_CHIP_ID,
1227 IMX219_REG_VALUE_16BIT, &val);
1229 dev_err(&client->dev, "failed to read chip id %x\n",
1234 if (val != IMX219_CHIP_ID) {
1235 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1236 IMX219_CHIP_ID, val);
1243 static const struct v4l2_subdev_core_ops imx219_core_ops = {
1244 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1245 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1248 static const struct v4l2_subdev_video_ops imx219_video_ops = {
1249 .s_stream = imx219_set_stream,
1252 static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
1253 .enum_mbus_code = imx219_enum_mbus_code,
1254 .get_fmt = imx219_get_pad_format,
1255 .set_fmt = imx219_set_pad_format,
1256 .get_selection = imx219_get_selection,
1257 .enum_frame_size = imx219_enum_frame_size,
1260 static const struct v4l2_subdev_ops imx219_subdev_ops = {
1261 .core = &imx219_core_ops,
1262 .video = &imx219_video_ops,
1263 .pad = &imx219_pad_ops,
1266 static const struct v4l2_subdev_internal_ops imx219_internal_ops = {
1267 .open = imx219_open,
1270 /* Initialize control handlers */
1271 static int imx219_init_controls(struct imx219 *imx219)
1273 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
1274 struct v4l2_ctrl_handler *ctrl_hdlr;
1275 unsigned int height = imx219->mode->height;
1276 struct v4l2_fwnode_device_properties props;
1277 int exposure_max, exposure_def, hblank;
1280 ctrl_hdlr = &imx219->ctrl_handler;
1281 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 11);
1285 mutex_init(&imx219->mutex);
1286 ctrl_hdlr->lock = &imx219->mutex;
1288 /* By default, PIXEL_RATE is read only */
1289 imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1290 V4L2_CID_PIXEL_RATE,
1292 IMX219_PIXEL_RATE, 1,
1295 /* Initial vblank/hblank/exposure parameters based on current mode */
1296 imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1297 V4L2_CID_VBLANK, IMX219_VBLANK_MIN,
1298 IMX219_VTS_MAX - height, 1,
1299 imx219->mode->vts_def - height);
1300 hblank = IMX219_PPL_DEFAULT - imx219->mode->width;
1301 imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1302 V4L2_CID_HBLANK, hblank, hblank,
1305 imx219->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1306 exposure_max = imx219->mode->vts_def - 4;
1307 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
1308 exposure_max : IMX219_EXPOSURE_DEFAULT;
1309 imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1311 IMX219_EXPOSURE_MIN, exposure_max,
1312 IMX219_EXPOSURE_STEP,
1315 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1316 IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX,
1317 IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT);
1319 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1320 IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX,
1321 IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT);
1323 imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1324 V4L2_CID_HFLIP, 0, 1, 1, 0);
1326 imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1328 imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1329 V4L2_CID_VFLIP, 0, 1, 1, 0);
1331 imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1333 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops,
1334 V4L2_CID_TEST_PATTERN,
1335 ARRAY_SIZE(imx219_test_pattern_menu) - 1,
1336 0, 0, imx219_test_pattern_menu);
1337 for (i = 0; i < 4; i++) {
1339 * The assumption is that
1340 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1
1341 * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2
1342 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3
1344 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops,
1345 V4L2_CID_TEST_PATTERN_RED + i,
1346 IMX219_TESTP_COLOUR_MIN,
1347 IMX219_TESTP_COLOUR_MAX,
1348 IMX219_TESTP_COLOUR_STEP,
1349 IMX219_TESTP_COLOUR_MAX);
1350 /* The "Solid color" pattern is white by default */
1353 if (ctrl_hdlr->error) {
1354 ret = ctrl_hdlr->error;
1355 dev_err(&client->dev, "%s control init failed (%d)\n",
1360 ret = v4l2_fwnode_device_parse(&client->dev, &props);
1364 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx219_ctrl_ops,
1369 imx219->sd.ctrl_handler = ctrl_hdlr;
1374 v4l2_ctrl_handler_free(ctrl_hdlr);
1375 mutex_destroy(&imx219->mutex);
1380 static void imx219_free_controls(struct imx219 *imx219)
1382 v4l2_ctrl_handler_free(imx219->sd.ctrl_handler);
1383 mutex_destroy(&imx219->mutex);
1386 static int imx219_check_hwcfg(struct device *dev)
1388 struct fwnode_handle *endpoint;
1389 struct v4l2_fwnode_endpoint ep_cfg = {
1390 .bus_type = V4L2_MBUS_CSI2_DPHY
1394 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1396 dev_err(dev, "endpoint node not found\n");
1400 if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) {
1401 dev_err(dev, "could not parse endpoint\n");
1405 /* Check the number of MIPI CSI2 data lanes */
1406 if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2) {
1407 dev_err(dev, "only 2 data lanes are currently supported\n");
1411 /* Check the link frequency set in device tree */
1412 if (!ep_cfg.nr_of_link_frequencies) {
1413 dev_err(dev, "link-frequency property not found in DT\n");
1417 if (ep_cfg.nr_of_link_frequencies != 1 ||
1418 ep_cfg.link_frequencies[0] != IMX219_DEFAULT_LINK_FREQ) {
1419 dev_err(dev, "Link frequency not supported: %lld\n",
1420 ep_cfg.link_frequencies[0]);
1427 v4l2_fwnode_endpoint_free(&ep_cfg);
1428 fwnode_handle_put(endpoint);
1433 static int imx219_probe(struct i2c_client *client)
1435 struct device *dev = &client->dev;
1436 struct imx219 *imx219;
1439 imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL);
1443 v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops);
1445 /* Check the hardware configuration in device tree */
1446 if (imx219_check_hwcfg(dev))
1449 /* Get system clock (xclk) */
1450 imx219->xclk = devm_clk_get(dev, NULL);
1451 if (IS_ERR(imx219->xclk)) {
1452 dev_err(dev, "failed to get xclk\n");
1453 return PTR_ERR(imx219->xclk);
1456 imx219->xclk_freq = clk_get_rate(imx219->xclk);
1457 if (imx219->xclk_freq != IMX219_XCLK_FREQ) {
1458 dev_err(dev, "xclk frequency not supported: %d Hz\n",
1463 ret = imx219_get_regulators(imx219);
1465 dev_err(dev, "failed to get regulators\n");
1469 /* Request optional enable pin */
1470 imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1474 * The sensor must be powered for imx219_identify_module()
1475 * to be able to read the CHIP_ID register
1477 ret = imx219_power_on(dev);
1481 ret = imx219_identify_module(imx219);
1483 goto error_power_off;
1485 /* Set default mode to max resolution */
1486 imx219->mode = &supported_modes[0];
1488 /* sensor doesn't enter LP-11 state upon power up until and unless
1489 * streaming is started, so upon power up switch the modes to:
1490 * streaming -> standby
1492 ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1493 IMX219_REG_VALUE_08BIT, IMX219_MODE_STREAMING);
1495 goto error_power_off;
1496 usleep_range(100, 110);
1498 /* put sensor back to standby mode */
1499 ret = imx219_write_reg(imx219, IMX219_REG_MODE_SELECT,
1500 IMX219_REG_VALUE_08BIT, IMX219_MODE_STANDBY);
1502 goto error_power_off;
1503 usleep_range(100, 110);
1505 ret = imx219_init_controls(imx219);
1507 goto error_power_off;
1509 /* Initialize subdev */
1510 imx219->sd.internal_ops = &imx219_internal_ops;
1511 imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1512 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1514 /* Initialize source pad */
1515 imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
1517 /* Initialize default format */
1518 imx219_set_default_format(imx219);
1520 ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
1522 dev_err(dev, "failed to init entity pads: %d\n", ret);
1523 goto error_handler_free;
1526 ret = v4l2_async_register_subdev_sensor_common(&imx219->sd);
1528 dev_err(dev, "failed to register sensor sub-device: %d\n", ret);
1529 goto error_media_entity;
1532 /* Enable runtime PM and turn off the device */
1533 pm_runtime_set_active(dev);
1534 pm_runtime_enable(dev);
1535 pm_runtime_idle(dev);
1540 media_entity_cleanup(&imx219->sd.entity);
1543 imx219_free_controls(imx219);
1546 imx219_power_off(dev);
1551 static int imx219_remove(struct i2c_client *client)
1553 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1554 struct imx219 *imx219 = to_imx219(sd);
1556 v4l2_async_unregister_subdev(sd);
1557 media_entity_cleanup(&sd->entity);
1558 imx219_free_controls(imx219);
1560 pm_runtime_disable(&client->dev);
1561 if (!pm_runtime_status_suspended(&client->dev))
1562 imx219_power_off(&client->dev);
1563 pm_runtime_set_suspended(&client->dev);
1568 static const struct of_device_id imx219_dt_ids[] = {
1569 { .compatible = "sony,imx219" },
1572 MODULE_DEVICE_TABLE(of, imx219_dt_ids);
1574 static const struct dev_pm_ops imx219_pm_ops = {
1575 SET_SYSTEM_SLEEP_PM_OPS(imx219_suspend, imx219_resume)
1576 SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL)
1579 static struct i2c_driver imx219_i2c_driver = {
1582 .of_match_table = imx219_dt_ids,
1583 .pm = &imx219_pm_ops,
1585 .probe_new = imx219_probe,
1586 .remove = imx219_remove,
1589 module_i2c_driver(imx219_i2c_driver);
1591 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1592 MODULE_DESCRIPTION("Sony IMX219 sensor driver");
1593 MODULE_LICENSE("GPL v2");