1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/acpi.h>
5 #include <linux/delay.h>
7 #include <linux/module.h>
8 #include <linux/pm_runtime.h>
9 #include <media/v4l2-ctrls.h>
10 #include <media/v4l2-device.h>
11 #include <asm/unaligned.h>
13 #define IMX258_REG_VALUE_08BIT 1
14 #define IMX258_REG_VALUE_16BIT 2
16 #define IMX258_REG_MODE_SELECT 0x0100
17 #define IMX258_MODE_STANDBY 0x00
18 #define IMX258_MODE_STREAMING 0x01
21 #define IMX258_REG_CHIP_ID 0x0016
22 #define IMX258_CHIP_ID 0x0258
24 /* V_TIMING internal */
25 #define IMX258_VTS_30FPS 0x0c98
26 #define IMX258_VTS_30FPS_2K 0x0638
27 #define IMX258_VTS_30FPS_VGA 0x034c
28 #define IMX258_VTS_MAX 0xffff
31 #define IMX258_FLL_MIN 0x08a6
32 #define IMX258_FLL_MAX 0xffff
33 #define IMX258_FLL_STEP 1
34 #define IMX258_FLL_DEFAULT 0x0c98
36 /* HBLANK control - read only */
37 #define IMX258_PPL_DEFAULT 5352
39 /* Exposure control */
40 #define IMX258_REG_EXPOSURE 0x0202
41 #define IMX258_EXPOSURE_MIN 4
42 #define IMX258_EXPOSURE_STEP 1
43 #define IMX258_EXPOSURE_DEFAULT 0x640
44 #define IMX258_EXPOSURE_MAX 65535
46 /* Analog gain control */
47 #define IMX258_REG_ANALOG_GAIN 0x0204
48 #define IMX258_ANA_GAIN_MIN 0
49 #define IMX258_ANA_GAIN_MAX 0x1fff
50 #define IMX258_ANA_GAIN_STEP 1
51 #define IMX258_ANA_GAIN_DEFAULT 0x0
53 /* Digital gain control */
54 #define IMX258_REG_GR_DIGITAL_GAIN 0x020e
55 #define IMX258_REG_R_DIGITAL_GAIN 0x0210
56 #define IMX258_REG_B_DIGITAL_GAIN 0x0212
57 #define IMX258_REG_GB_DIGITAL_GAIN 0x0214
58 #define IMX258_DGTL_GAIN_MIN 0
59 #define IMX258_DGTL_GAIN_MAX 4096 /* Max = 0xFFF */
60 #define IMX258_DGTL_GAIN_DEFAULT 1024
61 #define IMX258_DGTL_GAIN_STEP 1
63 /* Test Pattern Control */
64 #define IMX258_REG_TEST_PATTERN 0x0600
67 #define REG_MIRROR_FLIP_CONTROL 0x0101
68 #define REG_CONFIG_MIRROR_FLIP 0x03
69 #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
76 struct imx258_reg_list
{
78 const struct imx258_reg
*regs
;
81 /* Link frequency config */
82 struct imx258_link_freq_config
{
85 /* PLL registers for this link frequency */
86 struct imx258_reg_list reg_list
;
89 /* Mode : resolution and related config&values */
100 /* Index of Link frequency config to be used */
102 /* Default register values */
103 struct imx258_reg_list reg_list
;
106 /* 4208x3118 needs 1267Mbps/lane, 4 lanes */
107 static const struct imx258_reg mipi_data_rate_1267mbps
[] = {
125 static const struct imx258_reg mipi_data_rate_640mbps
[] = {
143 static const struct imx258_reg mode_4208x3118_regs
[] = {
262 static const struct imx258_reg mode_2104_1560_regs
[] = {
381 static const struct imx258_reg mode_1048_780_regs
[] = {
500 static const char * const imx258_test_pattern_menu
[] = {
503 "Eight Vertical Colour Bars",
504 "Colour Bars With Fade to Grey",
505 "Pseudorandom Sequence (PN9)",
508 /* Configurations for supported link frequencies */
509 #define IMX258_LINK_FREQ_634MHZ 633600000ULL
510 #define IMX258_LINK_FREQ_320MHZ 320000000ULL
513 IMX258_LINK_FREQ_1267MBPS
,
514 IMX258_LINK_FREQ_640MBPS
,
518 * pixel_rate = link_freq * data-rate * nr_of_lanes / bits_per_sample
519 * data rate => double data rate; number of lanes => 4; bits per pixel => 10
521 static u64
link_freq_to_pixel_rate(u64 f
)
529 /* Menu items for LINK_FREQ V4L2 control */
530 static const s64 link_freq_menu_items
[] = {
531 IMX258_LINK_FREQ_634MHZ
,
532 IMX258_LINK_FREQ_320MHZ
,
535 /* Link frequency configs */
536 static const struct imx258_link_freq_config link_freq_configs
[] = {
537 [IMX258_LINK_FREQ_1267MBPS
] = {
538 .pixels_per_line
= IMX258_PPL_DEFAULT
,
540 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_1267mbps
),
541 .regs
= mipi_data_rate_1267mbps
,
544 [IMX258_LINK_FREQ_640MBPS
] = {
545 .pixels_per_line
= IMX258_PPL_DEFAULT
,
547 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_640mbps
),
548 .regs
= mipi_data_rate_640mbps
,
554 static const struct imx258_mode supported_modes
[] = {
558 .vts_def
= IMX258_VTS_30FPS
,
559 .vts_min
= IMX258_VTS_30FPS
,
561 .num_of_regs
= ARRAY_SIZE(mode_4208x3118_regs
),
562 .regs
= mode_4208x3118_regs
,
564 .link_freq_index
= IMX258_LINK_FREQ_1267MBPS
,
569 .vts_def
= IMX258_VTS_30FPS_2K
,
570 .vts_min
= IMX258_VTS_30FPS_2K
,
572 .num_of_regs
= ARRAY_SIZE(mode_2104_1560_regs
),
573 .regs
= mode_2104_1560_regs
,
575 .link_freq_index
= IMX258_LINK_FREQ_640MBPS
,
580 .vts_def
= IMX258_VTS_30FPS_VGA
,
581 .vts_min
= IMX258_VTS_30FPS_VGA
,
583 .num_of_regs
= ARRAY_SIZE(mode_1048_780_regs
),
584 .regs
= mode_1048_780_regs
,
586 .link_freq_index
= IMX258_LINK_FREQ_640MBPS
,
591 struct v4l2_subdev sd
;
592 struct media_pad pad
;
594 struct v4l2_ctrl_handler ctrl_handler
;
596 struct v4l2_ctrl
*link_freq
;
597 struct v4l2_ctrl
*pixel_rate
;
598 struct v4l2_ctrl
*vblank
;
599 struct v4l2_ctrl
*hblank
;
600 struct v4l2_ctrl
*exposure
;
603 const struct imx258_mode
*cur_mode
;
606 * Mutex for serialized access:
607 * Protect sensor module set pad format and start/stop streaming safely.
611 /* Streaming on/off */
615 static inline struct imx258
*to_imx258(struct v4l2_subdev
*_sd
)
617 return container_of(_sd
, struct imx258
, sd
);
620 /* Read registers up to 2 at a time */
621 static int imx258_read_reg(struct imx258
*imx258
, u16 reg
, u32 len
, u32
*val
)
623 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
624 struct i2c_msg msgs
[2];
625 u8 addr_buf
[2] = { reg
>> 8, reg
& 0xff };
626 u8 data_buf
[4] = { 0, };
632 /* Write register address */
633 msgs
[0].addr
= client
->addr
;
635 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
636 msgs
[0].buf
= addr_buf
;
638 /* Read data from register */
639 msgs
[1].addr
= client
->addr
;
640 msgs
[1].flags
= I2C_M_RD
;
642 msgs
[1].buf
= &data_buf
[4 - len
];
644 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
645 if (ret
!= ARRAY_SIZE(msgs
))
648 *val
= get_unaligned_be32(data_buf
);
653 /* Write registers up to 2 at a time */
654 static int imx258_write_reg(struct imx258
*imx258
, u16 reg
, u32 len
, u32 val
)
656 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
662 put_unaligned_be16(reg
, buf
);
663 put_unaligned_be32(val
<< (8 * (4 - len
)), buf
+ 2);
664 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
670 /* Write a list of registers */
671 static int imx258_write_regs(struct imx258
*imx258
,
672 const struct imx258_reg
*regs
, u32 len
)
674 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
678 for (i
= 0; i
< len
; i
++) {
679 ret
= imx258_write_reg(imx258
, regs
[i
].address
, 1,
684 "Failed to write reg 0x%4.4x. error = %d\n",
685 regs
[i
].address
, ret
);
694 /* Open sub-device */
695 static int imx258_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
697 struct v4l2_mbus_framefmt
*try_fmt
=
698 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0);
700 /* Initialize try_fmt */
701 try_fmt
->width
= supported_modes
[0].width
;
702 try_fmt
->height
= supported_modes
[0].height
;
703 try_fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
704 try_fmt
->field
= V4L2_FIELD_NONE
;
709 static int imx258_update_digital_gain(struct imx258
*imx258
, u32 len
, u32 val
)
713 ret
= imx258_write_reg(imx258
, IMX258_REG_GR_DIGITAL_GAIN
,
714 IMX258_REG_VALUE_16BIT
,
718 ret
= imx258_write_reg(imx258
, IMX258_REG_GB_DIGITAL_GAIN
,
719 IMX258_REG_VALUE_16BIT
,
723 ret
= imx258_write_reg(imx258
, IMX258_REG_R_DIGITAL_GAIN
,
724 IMX258_REG_VALUE_16BIT
,
728 ret
= imx258_write_reg(imx258
, IMX258_REG_B_DIGITAL_GAIN
,
729 IMX258_REG_VALUE_16BIT
,
736 static int imx258_set_ctrl(struct v4l2_ctrl
*ctrl
)
738 struct imx258
*imx258
=
739 container_of(ctrl
->handler
, struct imx258
, ctrl_handler
);
740 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
744 * Applying V4L2 control value only happens
745 * when power is up for streaming
747 if (pm_runtime_get_if_in_use(&client
->dev
) == 0)
751 case V4L2_CID_ANALOGUE_GAIN
:
752 ret
= imx258_write_reg(imx258
, IMX258_REG_ANALOG_GAIN
,
753 IMX258_REG_VALUE_16BIT
,
756 case V4L2_CID_EXPOSURE
:
757 ret
= imx258_write_reg(imx258
, IMX258_REG_EXPOSURE
,
758 IMX258_REG_VALUE_16BIT
,
761 case V4L2_CID_DIGITAL_GAIN
:
762 ret
= imx258_update_digital_gain(imx258
, IMX258_REG_VALUE_16BIT
,
765 case V4L2_CID_TEST_PATTERN
:
766 ret
= imx258_write_reg(imx258
, IMX258_REG_TEST_PATTERN
,
767 IMX258_REG_VALUE_16BIT
,
769 ret
= imx258_write_reg(imx258
, REG_MIRROR_FLIP_CONTROL
,
770 IMX258_REG_VALUE_08BIT
,
771 !ctrl
->val
? REG_CONFIG_MIRROR_FLIP
:
772 REG_CONFIG_FLIP_TEST_PATTERN
);
775 dev_info(&client
->dev
,
776 "ctrl(id:0x%x,val:0x%x) is not handled\n",
777 ctrl
->id
, ctrl
->val
);
782 pm_runtime_put(&client
->dev
);
787 static const struct v4l2_ctrl_ops imx258_ctrl_ops
= {
788 .s_ctrl
= imx258_set_ctrl
,
791 static int imx258_enum_mbus_code(struct v4l2_subdev
*sd
,
792 struct v4l2_subdev_pad_config
*cfg
,
793 struct v4l2_subdev_mbus_code_enum
*code
)
795 /* Only one bayer order(GRBG) is supported */
799 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
804 static int imx258_enum_frame_size(struct v4l2_subdev
*sd
,
805 struct v4l2_subdev_pad_config
*cfg
,
806 struct v4l2_subdev_frame_size_enum
*fse
)
808 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
811 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
814 fse
->min_width
= supported_modes
[fse
->index
].width
;
815 fse
->max_width
= fse
->min_width
;
816 fse
->min_height
= supported_modes
[fse
->index
].height
;
817 fse
->max_height
= fse
->min_height
;
822 static void imx258_update_pad_format(const struct imx258_mode
*mode
,
823 struct v4l2_subdev_format
*fmt
)
825 fmt
->format
.width
= mode
->width
;
826 fmt
->format
.height
= mode
->height
;
827 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
828 fmt
->format
.field
= V4L2_FIELD_NONE
;
831 static int __imx258_get_pad_format(struct imx258
*imx258
,
832 struct v4l2_subdev_pad_config
*cfg
,
833 struct v4l2_subdev_format
*fmt
)
835 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
836 fmt
->format
= *v4l2_subdev_get_try_format(&imx258
->sd
, cfg
,
839 imx258_update_pad_format(imx258
->cur_mode
, fmt
);
844 static int imx258_get_pad_format(struct v4l2_subdev
*sd
,
845 struct v4l2_subdev_pad_config
*cfg
,
846 struct v4l2_subdev_format
*fmt
)
848 struct imx258
*imx258
= to_imx258(sd
);
851 mutex_lock(&imx258
->mutex
);
852 ret
= __imx258_get_pad_format(imx258
, cfg
, fmt
);
853 mutex_unlock(&imx258
->mutex
);
858 static int imx258_set_pad_format(struct v4l2_subdev
*sd
,
859 struct v4l2_subdev_pad_config
*cfg
,
860 struct v4l2_subdev_format
*fmt
)
862 struct imx258
*imx258
= to_imx258(sd
);
863 const struct imx258_mode
*mode
;
864 struct v4l2_mbus_framefmt
*framefmt
;
871 mutex_lock(&imx258
->mutex
);
873 /* Only one raw bayer(GBRG) order is supported */
874 fmt
->format
.code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
876 mode
= v4l2_find_nearest_size(supported_modes
,
877 ARRAY_SIZE(supported_modes
), width
, height
,
878 fmt
->format
.width
, fmt
->format
.height
);
879 imx258_update_pad_format(mode
, fmt
);
880 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
881 framefmt
= v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
882 *framefmt
= fmt
->format
;
884 imx258
->cur_mode
= mode
;
885 __v4l2_ctrl_s_ctrl(imx258
->link_freq
, mode
->link_freq_index
);
887 link_freq
= link_freq_menu_items
[mode
->link_freq_index
];
888 pixel_rate
= link_freq_to_pixel_rate(link_freq
);
889 __v4l2_ctrl_s_ctrl_int64(imx258
->pixel_rate
, pixel_rate
);
890 /* Update limits and set FPS to default */
891 vblank_def
= imx258
->cur_mode
->vts_def
-
892 imx258
->cur_mode
->height
;
893 vblank_min
= imx258
->cur_mode
->vts_min
-
894 imx258
->cur_mode
->height
;
895 __v4l2_ctrl_modify_range(
896 imx258
->vblank
, vblank_min
,
897 IMX258_VTS_MAX
- imx258
->cur_mode
->height
, 1,
899 __v4l2_ctrl_s_ctrl(imx258
->vblank
, vblank_def
);
901 link_freq_configs
[mode
->link_freq_index
].pixels_per_line
902 - imx258
->cur_mode
->width
;
903 __v4l2_ctrl_modify_range(imx258
->hblank
, h_blank
,
904 h_blank
, 1, h_blank
);
907 mutex_unlock(&imx258
->mutex
);
912 /* Start streaming */
913 static int imx258_start_streaming(struct imx258
*imx258
)
915 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
916 const struct imx258_reg_list
*reg_list
;
917 int ret
, link_freq_index
;
920 link_freq_index
= imx258
->cur_mode
->link_freq_index
;
921 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
922 ret
= imx258_write_regs(imx258
, reg_list
->regs
, reg_list
->num_of_regs
);
924 dev_err(&client
->dev
, "%s failed to set plls\n", __func__
);
928 /* Apply default values of current mode */
929 reg_list
= &imx258
->cur_mode
->reg_list
;
930 ret
= imx258_write_regs(imx258
, reg_list
->regs
, reg_list
->num_of_regs
);
932 dev_err(&client
->dev
, "%s failed to set mode\n", __func__
);
936 /* Set Orientation be 180 degree */
937 ret
= imx258_write_reg(imx258
, REG_MIRROR_FLIP_CONTROL
,
938 IMX258_REG_VALUE_08BIT
, REG_CONFIG_MIRROR_FLIP
);
940 dev_err(&client
->dev
, "%s failed to set orientation\n",
945 /* Apply customized values from user */
946 ret
= __v4l2_ctrl_handler_setup(imx258
->sd
.ctrl_handler
);
950 /* set stream on register */
951 return imx258_write_reg(imx258
, IMX258_REG_MODE_SELECT
,
952 IMX258_REG_VALUE_08BIT
,
953 IMX258_MODE_STREAMING
);
957 static int imx258_stop_streaming(struct imx258
*imx258
)
959 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
962 /* set stream off register */
963 ret
= imx258_write_reg(imx258
, IMX258_REG_MODE_SELECT
,
964 IMX258_REG_VALUE_08BIT
, IMX258_MODE_STANDBY
);
966 dev_err(&client
->dev
, "%s failed to set stream\n", __func__
);
969 * Return success even if it was an error, as there is nothing the
970 * caller can do about it.
975 static int imx258_set_stream(struct v4l2_subdev
*sd
, int enable
)
977 struct imx258
*imx258
= to_imx258(sd
);
978 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
981 mutex_lock(&imx258
->mutex
);
982 if (imx258
->streaming
== enable
) {
983 mutex_unlock(&imx258
->mutex
);
988 ret
= pm_runtime_get_sync(&client
->dev
);
990 pm_runtime_put_noidle(&client
->dev
);
995 * Apply default & customized values
996 * and then start streaming.
998 ret
= imx258_start_streaming(imx258
);
1002 imx258_stop_streaming(imx258
);
1003 pm_runtime_put(&client
->dev
);
1006 imx258
->streaming
= enable
;
1007 mutex_unlock(&imx258
->mutex
);
1012 pm_runtime_put(&client
->dev
);
1014 mutex_unlock(&imx258
->mutex
);
1019 static int __maybe_unused
imx258_suspend(struct device
*dev
)
1021 struct i2c_client
*client
= to_i2c_client(dev
);
1022 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1023 struct imx258
*imx258
= to_imx258(sd
);
1025 if (imx258
->streaming
)
1026 imx258_stop_streaming(imx258
);
1031 static int __maybe_unused
imx258_resume(struct device
*dev
)
1033 struct i2c_client
*client
= to_i2c_client(dev
);
1034 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1035 struct imx258
*imx258
= to_imx258(sd
);
1038 if (imx258
->streaming
) {
1039 ret
= imx258_start_streaming(imx258
);
1047 imx258_stop_streaming(imx258
);
1048 imx258
->streaming
= 0;
1052 /* Verify chip ID */
1053 static int imx258_identify_module(struct imx258
*imx258
)
1055 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
1059 ret
= imx258_read_reg(imx258
, IMX258_REG_CHIP_ID
,
1060 IMX258_REG_VALUE_16BIT
, &val
);
1062 dev_err(&client
->dev
, "failed to read chip id %x\n",
1067 if (val
!= IMX258_CHIP_ID
) {
1068 dev_err(&client
->dev
, "chip id mismatch: %x!=%x\n",
1069 IMX258_CHIP_ID
, val
);
1076 static const struct v4l2_subdev_video_ops imx258_video_ops
= {
1077 .s_stream
= imx258_set_stream
,
1080 static const struct v4l2_subdev_pad_ops imx258_pad_ops
= {
1081 .enum_mbus_code
= imx258_enum_mbus_code
,
1082 .get_fmt
= imx258_get_pad_format
,
1083 .set_fmt
= imx258_set_pad_format
,
1084 .enum_frame_size
= imx258_enum_frame_size
,
1087 static const struct v4l2_subdev_ops imx258_subdev_ops
= {
1088 .video
= &imx258_video_ops
,
1089 .pad
= &imx258_pad_ops
,
1092 static const struct v4l2_subdev_internal_ops imx258_internal_ops
= {
1093 .open
= imx258_open
,
1096 /* Initialize control handlers */
1097 static int imx258_init_controls(struct imx258
*imx258
)
1099 struct i2c_client
*client
= v4l2_get_subdevdata(&imx258
->sd
);
1100 struct v4l2_ctrl_handler
*ctrl_hdlr
;
1107 ctrl_hdlr
= &imx258
->ctrl_handler
;
1108 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
1112 mutex_init(&imx258
->mutex
);
1113 ctrl_hdlr
->lock
= &imx258
->mutex
;
1114 imx258
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
1117 ARRAY_SIZE(link_freq_menu_items
) - 1,
1119 link_freq_menu_items
);
1121 if (imx258
->link_freq
)
1122 imx258
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1124 pixel_rate_max
= link_freq_to_pixel_rate(link_freq_menu_items
[0]);
1125 pixel_rate_min
= link_freq_to_pixel_rate(link_freq_menu_items
[1]);
1126 /* By default, PIXEL_RATE is read only */
1127 imx258
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &imx258_ctrl_ops
,
1128 V4L2_CID_PIXEL_RATE
,
1129 pixel_rate_min
, pixel_rate_max
,
1133 vblank_def
= imx258
->cur_mode
->vts_def
- imx258
->cur_mode
->height
;
1134 vblank_min
= imx258
->cur_mode
->vts_min
- imx258
->cur_mode
->height
;
1135 imx258
->vblank
= v4l2_ctrl_new_std(
1136 ctrl_hdlr
, &imx258_ctrl_ops
, V4L2_CID_VBLANK
,
1138 IMX258_VTS_MAX
- imx258
->cur_mode
->height
, 1,
1142 imx258
->vblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1144 imx258
->hblank
= v4l2_ctrl_new_std(
1145 ctrl_hdlr
, &imx258_ctrl_ops
, V4L2_CID_HBLANK
,
1146 IMX258_PPL_DEFAULT
- imx258
->cur_mode
->width
,
1147 IMX258_PPL_DEFAULT
- imx258
->cur_mode
->width
,
1149 IMX258_PPL_DEFAULT
- imx258
->cur_mode
->width
);
1152 imx258
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1154 imx258
->exposure
= v4l2_ctrl_new_std(
1155 ctrl_hdlr
, &imx258_ctrl_ops
,
1156 V4L2_CID_EXPOSURE
, IMX258_EXPOSURE_MIN
,
1157 IMX258_EXPOSURE_MAX
, IMX258_EXPOSURE_STEP
,
1158 IMX258_EXPOSURE_DEFAULT
);
1160 v4l2_ctrl_new_std(ctrl_hdlr
, &imx258_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
1161 IMX258_ANA_GAIN_MIN
, IMX258_ANA_GAIN_MAX
,
1162 IMX258_ANA_GAIN_STEP
, IMX258_ANA_GAIN_DEFAULT
);
1164 v4l2_ctrl_new_std(ctrl_hdlr
, &imx258_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
1165 IMX258_DGTL_GAIN_MIN
, IMX258_DGTL_GAIN_MAX
,
1166 IMX258_DGTL_GAIN_STEP
,
1167 IMX258_DGTL_GAIN_DEFAULT
);
1169 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &imx258_ctrl_ops
,
1170 V4L2_CID_TEST_PATTERN
,
1171 ARRAY_SIZE(imx258_test_pattern_menu
) - 1,
1172 0, 0, imx258_test_pattern_menu
);
1174 if (ctrl_hdlr
->error
) {
1175 ret
= ctrl_hdlr
->error
;
1176 dev_err(&client
->dev
, "%s control init failed (%d)\n",
1181 imx258
->sd
.ctrl_handler
= ctrl_hdlr
;
1186 v4l2_ctrl_handler_free(ctrl_hdlr
);
1187 mutex_destroy(&imx258
->mutex
);
1192 static void imx258_free_controls(struct imx258
*imx258
)
1194 v4l2_ctrl_handler_free(imx258
->sd
.ctrl_handler
);
1195 mutex_destroy(&imx258
->mutex
);
1198 static int imx258_probe(struct i2c_client
*client
)
1200 struct imx258
*imx258
;
1204 device_property_read_u32(&client
->dev
, "clock-frequency", &val
);
1205 if (val
!= 19200000)
1209 * Check that the device is mounted upside down. The driver only
1210 * supports a single pixel order right now.
1212 ret
= device_property_read_u32(&client
->dev
, "rotation", &val
);
1213 if (ret
|| val
!= 180)
1216 imx258
= devm_kzalloc(&client
->dev
, sizeof(*imx258
), GFP_KERNEL
);
1220 /* Initialize subdev */
1221 v4l2_i2c_subdev_init(&imx258
->sd
, client
, &imx258_subdev_ops
);
1223 /* Check module identity */
1224 ret
= imx258_identify_module(imx258
);
1228 /* Set default mode to max resolution */
1229 imx258
->cur_mode
= &supported_modes
[0];
1231 ret
= imx258_init_controls(imx258
);
1235 /* Initialize subdev */
1236 imx258
->sd
.internal_ops
= &imx258_internal_ops
;
1237 imx258
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1238 imx258
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1240 /* Initialize source pad */
1241 imx258
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1243 ret
= media_entity_pads_init(&imx258
->sd
.entity
, 1, &imx258
->pad
);
1245 goto error_handler_free
;
1247 ret
= v4l2_async_register_subdev_sensor_common(&imx258
->sd
);
1249 goto error_media_entity
;
1251 pm_runtime_set_active(&client
->dev
);
1252 pm_runtime_enable(&client
->dev
);
1253 pm_runtime_idle(&client
->dev
);
1258 media_entity_cleanup(&imx258
->sd
.entity
);
1261 imx258_free_controls(imx258
);
1266 static int imx258_remove(struct i2c_client
*client
)
1268 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1269 struct imx258
*imx258
= to_imx258(sd
);
1271 v4l2_async_unregister_subdev(sd
);
1272 media_entity_cleanup(&sd
->entity
);
1273 imx258_free_controls(imx258
);
1275 pm_runtime_disable(&client
->dev
);
1276 pm_runtime_set_suspended(&client
->dev
);
1281 static const struct dev_pm_ops imx258_pm_ops
= {
1282 SET_SYSTEM_SLEEP_PM_OPS(imx258_suspend
, imx258_resume
)
1286 static const struct acpi_device_id imx258_acpi_ids
[] = {
1291 MODULE_DEVICE_TABLE(acpi
, imx258_acpi_ids
);
1294 static struct i2c_driver imx258_i2c_driver
= {
1297 .pm
= &imx258_pm_ops
,
1298 .acpi_match_table
= ACPI_PTR(imx258_acpi_ids
),
1300 .probe_new
= imx258_probe
,
1301 .remove
= imx258_remove
,
1304 module_i2c_driver(imx258_i2c_driver
);
1306 MODULE_AUTHOR("Yeh, Andy <andy.yeh@intel.com>");
1307 MODULE_AUTHOR("Chiang, Alan");
1308 MODULE_AUTHOR("Chen, Jason");
1309 MODULE_DESCRIPTION("Sony IMX258 sensor driver");
1310 MODULE_LICENSE("GPL v2");