1 // SPDX-License-Identifier: GPL-2.0
3 * imx214.c - imx214 sensor driver
5 * Copyright 2018 Qtechnology A/S
7 * Ricardo Ribalda <ribalda@kernel.org>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <media/media-entity.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-fwnode.h>
20 #include <media/v4l2-subdev.h>
22 #define IMX214_DEFAULT_CLK_FREQ 24000000
23 #define IMX214_DEFAULT_LINK_FREQ 480000000
24 #define IMX214_DEFAULT_PIXEL_RATE ((IMX214_DEFAULT_LINK_FREQ * 8LL) / 10)
26 #define IMX214_MBUS_CODE MEDIA_BUS_FMT_SRGGB10_1X10
28 static const char * const imx214_supply_name
[] = {
34 #define IMX214_NUM_SUPPLIES ARRAY_SIZE(imx214_supply_name)
39 struct regmap
*regmap
;
41 struct v4l2_subdev sd
;
43 struct v4l2_mbus_framefmt fmt
;
44 struct v4l2_rect crop
;
46 struct v4l2_ctrl_handler ctrls
;
47 struct v4l2_ctrl
*pixel_rate
;
48 struct v4l2_ctrl
*link_freq
;
49 struct v4l2_ctrl
*exposure
;
50 struct v4l2_ctrl
*unit_size
;
52 struct regulator_bulk_data supplies
[IMX214_NUM_SUPPLIES
];
54 struct gpio_desc
*enable_gpio
;
57 * Serialize control access, get/set format, get selection
58 * and start streaming.
71 IMX214_TABLE_WAIT_MS
= 0,
77 /*From imx214_mode_tbls.h*/
78 static const struct reg_8 mode_4096x2304
[] = {
177 {IMX214_TABLE_WAIT_MS
, 10},
179 {IMX214_TABLE_END
, 0x00}
182 static const struct reg_8 mode_1920x1080
[] = {
281 {IMX214_TABLE_WAIT_MS
, 10},
283 {IMX214_TABLE_END
, 0x00}
286 static const struct reg_8 mode_table_common
[] = {
289 /* software standby settings */
295 /* external clock setting */
375 /* CNR parameter setting */
378 /* Moire reduction */
381 /* image enhancement */
403 {IMX214_TABLE_END
, 0x00}
407 * Declare modes in order, from biggest
408 * to smallest height.
410 static const struct imx214_mode
{
413 const struct reg_8
*reg_table
;
418 .reg_table
= mode_4096x2304
,
423 .reg_table
= mode_1920x1080
,
427 static inline struct imx214
*to_imx214(struct v4l2_subdev
*sd
)
429 return container_of(sd
, struct imx214
, sd
);
432 static int __maybe_unused
imx214_power_on(struct device
*dev
)
434 struct i2c_client
*client
= to_i2c_client(dev
);
435 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
436 struct imx214
*imx214
= to_imx214(sd
);
439 ret
= regulator_bulk_enable(IMX214_NUM_SUPPLIES
, imx214
->supplies
);
441 dev_err(imx214
->dev
, "failed to enable regulators: %d\n", ret
);
445 usleep_range(2000, 3000);
447 ret
= clk_prepare_enable(imx214
->xclk
);
449 regulator_bulk_disable(IMX214_NUM_SUPPLIES
, imx214
->supplies
);
450 dev_err(imx214
->dev
, "clk prepare enable failed\n");
454 gpiod_set_value_cansleep(imx214
->enable_gpio
, 1);
455 usleep_range(12000, 15000);
460 static int __maybe_unused
imx214_power_off(struct device
*dev
)
462 struct i2c_client
*client
= to_i2c_client(dev
);
463 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
464 struct imx214
*imx214
= to_imx214(sd
);
466 gpiod_set_value_cansleep(imx214
->enable_gpio
, 0);
468 clk_disable_unprepare(imx214
->xclk
);
470 regulator_bulk_disable(IMX214_NUM_SUPPLIES
, imx214
->supplies
);
471 usleep_range(10, 20);
476 static int imx214_enum_mbus_code(struct v4l2_subdev
*sd
,
477 struct v4l2_subdev_pad_config
*cfg
,
478 struct v4l2_subdev_mbus_code_enum
*code
)
483 code
->code
= IMX214_MBUS_CODE
;
488 static int imx214_enum_frame_size(struct v4l2_subdev
*subdev
,
489 struct v4l2_subdev_pad_config
*cfg
,
490 struct v4l2_subdev_frame_size_enum
*fse
)
492 if (fse
->code
!= IMX214_MBUS_CODE
)
495 if (fse
->index
>= ARRAY_SIZE(imx214_modes
))
498 fse
->min_width
= fse
->max_width
= imx214_modes
[fse
->index
].width
;
499 fse
->min_height
= fse
->max_height
= imx214_modes
[fse
->index
].height
;
504 #ifdef CONFIG_VIDEO_ADV_DEBUG
505 static int imx214_s_register(struct v4l2_subdev
*subdev
,
506 const struct v4l2_dbg_register
*reg
)
508 struct imx214
*imx214
= container_of(subdev
, struct imx214
, sd
);
510 return regmap_write(imx214
->regmap
, reg
->reg
, reg
->val
);
513 static int imx214_g_register(struct v4l2_subdev
*subdev
,
514 struct v4l2_dbg_register
*reg
)
516 struct imx214
*imx214
= container_of(subdev
, struct imx214
, sd
);
521 ret
= regmap_read(imx214
->regmap
, reg
->reg
, &aux
);
528 static const struct v4l2_subdev_core_ops imx214_core_ops
= {
529 #ifdef CONFIG_VIDEO_ADV_DEBUG
530 .g_register
= imx214_g_register
,
531 .s_register
= imx214_s_register
,
535 static struct v4l2_mbus_framefmt
*
536 __imx214_get_pad_format(struct imx214
*imx214
,
537 struct v4l2_subdev_pad_config
*cfg
,
539 enum v4l2_subdev_format_whence which
)
542 case V4L2_SUBDEV_FORMAT_TRY
:
543 return v4l2_subdev_get_try_format(&imx214
->sd
, cfg
, pad
);
544 case V4L2_SUBDEV_FORMAT_ACTIVE
:
551 static int imx214_get_format(struct v4l2_subdev
*sd
,
552 struct v4l2_subdev_pad_config
*cfg
,
553 struct v4l2_subdev_format
*format
)
555 struct imx214
*imx214
= to_imx214(sd
);
557 mutex_lock(&imx214
->mutex
);
558 format
->format
= *__imx214_get_pad_format(imx214
, cfg
, format
->pad
,
560 mutex_unlock(&imx214
->mutex
);
565 static struct v4l2_rect
*
566 __imx214_get_pad_crop(struct imx214
*imx214
, struct v4l2_subdev_pad_config
*cfg
,
567 unsigned int pad
, enum v4l2_subdev_format_whence which
)
570 case V4L2_SUBDEV_FORMAT_TRY
:
571 return v4l2_subdev_get_try_crop(&imx214
->sd
, cfg
, pad
);
572 case V4L2_SUBDEV_FORMAT_ACTIVE
:
573 return &imx214
->crop
;
579 static int imx214_set_format(struct v4l2_subdev
*sd
,
580 struct v4l2_subdev_pad_config
*cfg
,
581 struct v4l2_subdev_format
*format
)
583 struct imx214
*imx214
= to_imx214(sd
);
584 struct v4l2_mbus_framefmt
*__format
;
585 struct v4l2_rect
*__crop
;
586 const struct imx214_mode
*mode
;
588 mutex_lock(&imx214
->mutex
);
590 __crop
= __imx214_get_pad_crop(imx214
, cfg
, format
->pad
, format
->which
);
592 mode
= v4l2_find_nearest_size(imx214_modes
,
593 ARRAY_SIZE(imx214_modes
), width
, height
,
594 format
->format
.width
,
595 format
->format
.height
);
597 __crop
->width
= mode
->width
;
598 __crop
->height
= mode
->height
;
600 __format
= __imx214_get_pad_format(imx214
, cfg
, format
->pad
,
602 __format
->width
= __crop
->width
;
603 __format
->height
= __crop
->height
;
604 __format
->code
= IMX214_MBUS_CODE
;
605 __format
->field
= V4L2_FIELD_NONE
;
606 __format
->colorspace
= V4L2_COLORSPACE_SRGB
;
607 __format
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(__format
->colorspace
);
608 __format
->quantization
= V4L2_MAP_QUANTIZATION_DEFAULT(true,
609 __format
->colorspace
, __format
->ycbcr_enc
);
610 __format
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(__format
->colorspace
);
612 format
->format
= *__format
;
614 mutex_unlock(&imx214
->mutex
);
619 static int imx214_get_selection(struct v4l2_subdev
*sd
,
620 struct v4l2_subdev_pad_config
*cfg
,
621 struct v4l2_subdev_selection
*sel
)
623 struct imx214
*imx214
= to_imx214(sd
);
625 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
628 mutex_lock(&imx214
->mutex
);
629 sel
->r
= *__imx214_get_pad_crop(imx214
, cfg
, sel
->pad
,
631 mutex_unlock(&imx214
->mutex
);
635 static int imx214_entity_init_cfg(struct v4l2_subdev
*subdev
,
636 struct v4l2_subdev_pad_config
*cfg
)
638 struct v4l2_subdev_format fmt
= { };
640 fmt
.which
= cfg
? V4L2_SUBDEV_FORMAT_TRY
: V4L2_SUBDEV_FORMAT_ACTIVE
;
641 fmt
.format
.width
= imx214_modes
[0].width
;
642 fmt
.format
.height
= imx214_modes
[0].height
;
644 imx214_set_format(subdev
, cfg
, &fmt
);
649 static int imx214_set_ctrl(struct v4l2_ctrl
*ctrl
)
651 struct imx214
*imx214
= container_of(ctrl
->handler
,
652 struct imx214
, ctrls
);
657 * Applying V4L2 control value only happens
658 * when power is up for streaming
660 if (!pm_runtime_get_if_in_use(imx214
->dev
))
664 case V4L2_CID_EXPOSURE
:
666 vals
[0] = ctrl
->val
>> 8;
667 ret
= regmap_bulk_write(imx214
->regmap
, 0x202, vals
, 2);
669 dev_err(imx214
->dev
, "Error %d\n", ret
);
677 pm_runtime_put(imx214
->dev
);
682 static const struct v4l2_ctrl_ops imx214_ctrl_ops
= {
683 .s_ctrl
= imx214_set_ctrl
,
687 static int imx214_write_table(struct imx214
*imx214
,
688 const struct reg_8 table
[])
694 for (; table
->addr
!= IMX214_TABLE_END
; table
++) {
695 if (table
->addr
== IMX214_TABLE_WAIT_MS
) {
696 usleep_range(table
->val
* 1000,
697 table
->val
* 1000 + 500);
701 for (i
= 0; i
< MAX_CMD
; i
++) {
702 if (table
[i
].addr
!= (table
[0].addr
+ i
))
704 vals
[i
] = table
[i
].val
;
707 ret
= regmap_bulk_write(imx214
->regmap
, table
->addr
, vals
, i
);
710 dev_err(imx214
->dev
, "write_table error: %d\n", ret
);
720 static int imx214_start_streaming(struct imx214
*imx214
)
722 const struct imx214_mode
*mode
;
725 mutex_lock(&imx214
->mutex
);
726 ret
= imx214_write_table(imx214
, mode_table_common
);
728 dev_err(imx214
->dev
, "could not sent common table %d\n", ret
);
732 mode
= v4l2_find_nearest_size(imx214_modes
,
733 ARRAY_SIZE(imx214_modes
), width
, height
,
734 imx214
->fmt
.width
, imx214
->fmt
.height
);
735 ret
= imx214_write_table(imx214
, mode
->reg_table
);
737 dev_err(imx214
->dev
, "could not sent mode table %d\n", ret
);
740 ret
= __v4l2_ctrl_handler_setup(&imx214
->ctrls
);
742 dev_err(imx214
->dev
, "could not sync v4l2 controls\n");
745 ret
= regmap_write(imx214
->regmap
, 0x100, 1);
747 dev_err(imx214
->dev
, "could not sent start table %d\n", ret
);
751 mutex_unlock(&imx214
->mutex
);
755 mutex_unlock(&imx214
->mutex
);
759 static int imx214_stop_streaming(struct imx214
*imx214
)
763 ret
= regmap_write(imx214
->regmap
, 0x100, 0);
765 dev_err(imx214
->dev
, "could not sent stop table %d\n", ret
);
770 static int imx214_s_stream(struct v4l2_subdev
*subdev
, int enable
)
772 struct imx214
*imx214
= to_imx214(subdev
);
775 if (imx214
->streaming
== enable
)
779 ret
= pm_runtime_get_sync(imx214
->dev
);
781 pm_runtime_put_noidle(imx214
->dev
);
785 ret
= imx214_start_streaming(imx214
);
789 ret
= imx214_stop_streaming(imx214
);
792 pm_runtime_put(imx214
->dev
);
795 imx214
->streaming
= enable
;
799 pm_runtime_put(imx214
->dev
);
803 static int imx214_g_frame_interval(struct v4l2_subdev
*subdev
,
804 struct v4l2_subdev_frame_interval
*fival
)
806 fival
->interval
.numerator
= 1;
807 fival
->interval
.denominator
= IMX214_FPS
;
812 static int imx214_enum_frame_interval(struct v4l2_subdev
*subdev
,
813 struct v4l2_subdev_pad_config
*cfg
,
814 struct v4l2_subdev_frame_interval_enum
*fie
)
816 const struct imx214_mode
*mode
;
821 mode
= v4l2_find_nearest_size(imx214_modes
,
822 ARRAY_SIZE(imx214_modes
), width
, height
,
823 fie
->width
, fie
->height
);
825 fie
->code
= IMX214_MBUS_CODE
;
826 fie
->width
= mode
->width
;
827 fie
->height
= mode
->height
;
828 fie
->interval
.numerator
= 1;
829 fie
->interval
.denominator
= IMX214_FPS
;
834 static const struct v4l2_subdev_video_ops imx214_video_ops
= {
835 .s_stream
= imx214_s_stream
,
836 .g_frame_interval
= imx214_g_frame_interval
,
837 .s_frame_interval
= imx214_g_frame_interval
,
840 static const struct v4l2_subdev_pad_ops imx214_subdev_pad_ops
= {
841 .enum_mbus_code
= imx214_enum_mbus_code
,
842 .enum_frame_size
= imx214_enum_frame_size
,
843 .enum_frame_interval
= imx214_enum_frame_interval
,
844 .get_fmt
= imx214_get_format
,
845 .set_fmt
= imx214_set_format
,
846 .get_selection
= imx214_get_selection
,
847 .init_cfg
= imx214_entity_init_cfg
,
850 static const struct v4l2_subdev_ops imx214_subdev_ops
= {
851 .core
= &imx214_core_ops
,
852 .video
= &imx214_video_ops
,
853 .pad
= &imx214_subdev_pad_ops
,
856 static const struct regmap_config sensor_regmap_config
= {
859 .cache_type
= REGCACHE_RBTREE
,
862 static int imx214_get_regulators(struct device
*dev
, struct imx214
*imx214
)
866 for (i
= 0; i
< IMX214_NUM_SUPPLIES
; i
++)
867 imx214
->supplies
[i
].supply
= imx214_supply_name
[i
];
869 return devm_regulator_bulk_get(dev
, IMX214_NUM_SUPPLIES
,
873 static int imx214_parse_fwnode(struct device
*dev
)
875 struct fwnode_handle
*endpoint
;
876 struct v4l2_fwnode_endpoint bus_cfg
= {
877 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
882 endpoint
= fwnode_graph_get_next_endpoint(dev_fwnode(dev
), NULL
);
884 dev_err(dev
, "endpoint node not found\n");
888 ret
= v4l2_fwnode_endpoint_alloc_parse(endpoint
, &bus_cfg
);
890 dev_err(dev
, "parsing endpoint node failed\n");
894 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++)
895 if (bus_cfg
.link_frequencies
[i
] == IMX214_DEFAULT_LINK_FREQ
)
898 if (i
== bus_cfg
.nr_of_link_frequencies
) {
899 dev_err(dev
, "link-frequencies %d not supported, Please review your DT\n",
900 IMX214_DEFAULT_LINK_FREQ
);
906 v4l2_fwnode_endpoint_free(&bus_cfg
);
907 fwnode_handle_put(endpoint
);
911 static int __maybe_unused
imx214_suspend(struct device
*dev
)
913 struct i2c_client
*client
= to_i2c_client(dev
);
914 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
915 struct imx214
*imx214
= to_imx214(sd
);
917 if (imx214
->streaming
)
918 imx214_stop_streaming(imx214
);
923 static int __maybe_unused
imx214_resume(struct device
*dev
)
925 struct i2c_client
*client
= to_i2c_client(dev
);
926 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
927 struct imx214
*imx214
= to_imx214(sd
);
930 if (imx214
->streaming
) {
931 ret
= imx214_start_streaming(imx214
);
939 imx214_stop_streaming(imx214
);
940 imx214
->streaming
= 0;
944 static int imx214_probe(struct i2c_client
*client
)
946 struct device
*dev
= &client
->dev
;
947 struct imx214
*imx214
;
948 static const s64 link_freq
[] = {
949 IMX214_DEFAULT_LINK_FREQ
,
951 static const struct v4l2_area unit_size
= {
957 ret
= imx214_parse_fwnode(dev
);
961 imx214
= devm_kzalloc(dev
, sizeof(*imx214
), GFP_KERNEL
);
967 imx214
->xclk
= devm_clk_get(dev
, NULL
);
968 if (IS_ERR(imx214
->xclk
)) {
969 dev_err(dev
, "could not get xclk");
970 return PTR_ERR(imx214
->xclk
);
973 ret
= clk_set_rate(imx214
->xclk
, IMX214_DEFAULT_CLK_FREQ
);
975 dev_err(dev
, "could not set xclk frequency\n");
979 ret
= imx214_get_regulators(dev
, imx214
);
981 dev_err(dev
, "cannot get regulators\n");
985 imx214
->enable_gpio
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_LOW
);
986 if (IS_ERR(imx214
->enable_gpio
)) {
987 dev_err(dev
, "cannot get enable gpio\n");
988 return PTR_ERR(imx214
->enable_gpio
);
991 imx214
->regmap
= devm_regmap_init_i2c(client
, &sensor_regmap_config
);
992 if (IS_ERR(imx214
->regmap
)) {
993 dev_err(dev
, "regmap init failed\n");
994 return PTR_ERR(imx214
->regmap
);
997 v4l2_i2c_subdev_init(&imx214
->sd
, client
, &imx214_subdev_ops
);
1000 * Enable power initially, to avoid warnings
1001 * from clk_disable on power_off
1003 imx214_power_on(imx214
->dev
);
1005 pm_runtime_set_active(imx214
->dev
);
1006 pm_runtime_enable(imx214
->dev
);
1007 pm_runtime_idle(imx214
->dev
);
1009 v4l2_ctrl_handler_init(&imx214
->ctrls
, 3);
1011 imx214
->pixel_rate
= v4l2_ctrl_new_std(&imx214
->ctrls
, NULL
,
1012 V4L2_CID_PIXEL_RATE
, 0,
1013 IMX214_DEFAULT_PIXEL_RATE
, 1,
1014 IMX214_DEFAULT_PIXEL_RATE
);
1015 imx214
->link_freq
= v4l2_ctrl_new_int_menu(&imx214
->ctrls
, NULL
,
1017 ARRAY_SIZE(link_freq
) - 1,
1019 if (imx214
->link_freq
)
1020 imx214
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1024 * Values obtained reverse engineering blobs and/or devices.
1025 * Ranges and functionality might be wrong.
1027 * Sony, please release some register set documentation for the
1030 * Yours sincerely, Ricardo.
1032 imx214
->exposure
= v4l2_ctrl_new_std(&imx214
->ctrls
, &imx214_ctrl_ops
,
1034 0, 3184, 1, 0x0c70);
1036 imx214
->unit_size
= v4l2_ctrl_new_std_compound(&imx214
->ctrls
,
1038 V4L2_CID_UNIT_CELL_SIZE
,
1039 v4l2_ctrl_ptr_create((void *)&unit_size
));
1040 ret
= imx214
->ctrls
.error
;
1042 dev_err(&client
->dev
, "%s control init failed (%d)\n",
1047 imx214
->sd
.ctrl_handler
= &imx214
->ctrls
;
1048 mutex_init(&imx214
->mutex
);
1049 imx214
->ctrls
.lock
= &imx214
->mutex
;
1051 imx214
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1052 imx214
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1053 imx214
->sd
.dev
= &client
->dev
;
1054 imx214
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1056 ret
= media_entity_pads_init(&imx214
->sd
.entity
, 1, &imx214
->pad
);
1058 dev_err(dev
, "could not register media entity\n");
1062 imx214_entity_init_cfg(&imx214
->sd
, NULL
);
1064 ret
= v4l2_async_register_subdev_sensor_common(&imx214
->sd
);
1066 dev_err(dev
, "could not register v4l2 device\n");
1073 media_entity_cleanup(&imx214
->sd
.entity
);
1075 mutex_destroy(&imx214
->mutex
);
1076 v4l2_ctrl_handler_free(&imx214
->ctrls
);
1077 pm_runtime_disable(imx214
->dev
);
1082 static int imx214_remove(struct i2c_client
*client
)
1084 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1085 struct imx214
*imx214
= to_imx214(sd
);
1087 v4l2_async_unregister_subdev(&imx214
->sd
);
1088 media_entity_cleanup(&imx214
->sd
.entity
);
1089 v4l2_ctrl_handler_free(&imx214
->ctrls
);
1091 pm_runtime_disable(&client
->dev
);
1092 pm_runtime_set_suspended(&client
->dev
);
1094 mutex_destroy(&imx214
->mutex
);
1099 static const struct of_device_id imx214_of_match
[] = {
1100 { .compatible
= "sony,imx214" },
1103 MODULE_DEVICE_TABLE(of
, imx214_of_match
);
1105 static const struct dev_pm_ops imx214_pm_ops
= {
1106 SET_SYSTEM_SLEEP_PM_OPS(imx214_suspend
, imx214_resume
)
1107 SET_RUNTIME_PM_OPS(imx214_power_off
, imx214_power_on
, NULL
)
1110 static struct i2c_driver imx214_i2c_driver
= {
1112 .of_match_table
= imx214_of_match
,
1113 .pm
= &imx214_pm_ops
,
1116 .probe_new
= imx214_probe
,
1117 .remove
= imx214_remove
,
1120 module_i2c_driver(imx214_i2c_driver
);
1122 MODULE_DESCRIPTION("Sony IMX214 Camera driver");
1123 MODULE_AUTHOR("Ricardo Ribalda <ribalda@kernel.org>");
1124 MODULE_LICENSE("GPL v2");