1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022 Intel Corporation.
4 #include <linux/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
17 #define OG01A1B_REG_VALUE_08BIT 1
18 #define OG01A1B_REG_VALUE_16BIT 2
19 #define OG01A1B_REG_VALUE_24BIT 3
21 #define OG01A1B_LINK_FREQ_500MHZ 500000000ULL
22 #define OG01A1B_SCLK 120000000LL
23 #define OG01A1B_MCLK 19200000
24 #define OG01A1B_DATA_LANES 2
25 #define OG01A1B_RGB_DEPTH 10
27 #define OG01A1B_REG_CHIP_ID 0x300a
28 #define OG01A1B_CHIP_ID 0x470141
30 #define OG01A1B_REG_MODE_SELECT 0x0100
31 #define OG01A1B_MODE_STANDBY 0x00
32 #define OG01A1B_MODE_STREAMING 0x01
34 /* vertical-timings from sensor */
35 #define OG01A1B_REG_VTS 0x380e
36 #define OG01A1B_VTS_120FPS 0x0498
37 #define OG01A1B_VTS_120FPS_MIN 0x0498
38 #define OG01A1B_VTS_MAX 0x7fff
40 /* horizontal-timings from sensor */
41 #define OG01A1B_REG_HTS 0x380c
43 /* Exposure controls from sensor */
44 #define OG01A1B_REG_EXPOSURE 0x3501
45 #define OG01A1B_EXPOSURE_MIN 1
46 #define OG01A1B_EXPOSURE_MAX_MARGIN 14
47 #define OG01A1B_EXPOSURE_STEP 1
49 /* Analog gain controls from sensor */
50 #define OG01A1B_REG_ANALOG_GAIN 0x3508
51 #define OG01A1B_ANAL_GAIN_MIN 16
52 #define OG01A1B_ANAL_GAIN_MAX 248 /* Max = 15.5x */
53 #define OG01A1B_ANAL_GAIN_STEP 1
55 /* Digital gain controls from sensor */
56 #define OG01A1B_REG_DIG_GAIN 0x350a
57 #define OG01A1B_DGTL_GAIN_MIN 1024
58 #define OG01A1B_DGTL_GAIN_MAX 16384 /* Max = 16x */
59 #define OG01A1B_DGTL_GAIN_STEP 1
60 #define OG01A1B_DGTL_GAIN_DEFAULT 1024
63 #define OG01A1B_REG_GROUP_ACCESS 0x3208
64 #define OG01A1B_GROUP_HOLD_START 0x0
65 #define OG01A1B_GROUP_HOLD_END 0x10
66 #define OG01A1B_GROUP_HOLD_LAUNCH 0xa0
68 /* Test Pattern Control */
69 #define OG01A1B_REG_TEST_PATTERN 0x5100
70 #define OG01A1B_TEST_PATTERN_ENABLE BIT(7)
71 #define OG01A1B_TEST_PATTERN_BAR_SHIFT 2
73 #define to_og01a1b(_sd) container_of(_sd, struct og01a1b, sd)
76 OG01A1B_LINK_FREQ_1000MBPS
,
84 struct og01a1b_reg_list
{
86 const struct og01a1b_reg
*regs
;
89 struct og01a1b_link_freq_config
{
90 const struct og01a1b_reg_list reg_list
;
94 /* Frame width in pixels */
97 /* Frame height in pixels */
100 /* Horizontal timining size */
103 /* Default vertical timining size */
106 /* Min vertical timining size */
109 /* Link frequency needed for this resolution */
112 /* Sensor register settings for this resolution */
113 const struct og01a1b_reg_list reg_list
;
116 static const struct og01a1b_reg mipi_data_rate_1000mbps
[] = {
126 static const struct og01a1b_reg mode_1280x1024_regs
[] = {
387 static const char * const og01a1b_test_pattern_menu
[] = {
389 "Standard Color Bar",
390 "Top-Bottom Darker Color Bar",
391 "Right-Left Darker Color Bar",
392 "Bottom-Top Darker Color Bar"
395 static const s64 link_freq_menu_items
[] = {
396 OG01A1B_LINK_FREQ_500MHZ
,
399 static const struct og01a1b_link_freq_config link_freq_configs
[] = {
400 [OG01A1B_LINK_FREQ_1000MBPS
] = {
402 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_1000mbps
),
403 .regs
= mipi_data_rate_1000mbps
,
408 static const struct og01a1b_mode supported_modes
[] = {
413 .vts_def
= OG01A1B_VTS_120FPS
,
414 .vts_min
= OG01A1B_VTS_120FPS_MIN
,
416 .num_of_regs
= ARRAY_SIZE(mode_1280x1024_regs
),
417 .regs
= mode_1280x1024_regs
,
419 .link_freq_index
= OG01A1B_LINK_FREQ_1000MBPS
,
425 struct gpio_desc
*reset_gpio
;
426 struct regulator
*avdd
;
427 struct regulator
*dovdd
;
428 struct regulator
*dvdd
;
430 struct v4l2_subdev sd
;
431 struct media_pad pad
;
432 struct v4l2_ctrl_handler ctrl_handler
;
435 struct v4l2_ctrl
*link_freq
;
436 struct v4l2_ctrl
*pixel_rate
;
437 struct v4l2_ctrl
*vblank
;
438 struct v4l2_ctrl
*hblank
;
439 struct v4l2_ctrl
*exposure
;
442 const struct og01a1b_mode
*cur_mode
;
444 /* To serialize asynchronus callbacks */
448 static u64
to_pixel_rate(u32 f_index
)
450 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OG01A1B_DATA_LANES
;
452 do_div(pixel_rate
, OG01A1B_RGB_DEPTH
);
457 static u64
to_pixels_per_line(u32 hts
, u32 f_index
)
459 u64 ppl
= hts
* to_pixel_rate(f_index
);
461 do_div(ppl
, OG01A1B_SCLK
);
466 static int og01a1b_read_reg(struct og01a1b
*og01a1b
, u16 reg
, u16 len
, u32
*val
)
468 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
469 struct i2c_msg msgs
[2];
471 u8 data_buf
[4] = {0};
477 put_unaligned_be16(reg
, addr_buf
);
478 msgs
[0].addr
= client
->addr
;
480 msgs
[0].len
= sizeof(addr_buf
);
481 msgs
[0].buf
= addr_buf
;
482 msgs
[1].addr
= client
->addr
;
483 msgs
[1].flags
= I2C_M_RD
;
485 msgs
[1].buf
= &data_buf
[4 - len
];
487 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
488 if (ret
!= ARRAY_SIZE(msgs
))
491 *val
= get_unaligned_be32(data_buf
);
496 static int og01a1b_write_reg(struct og01a1b
*og01a1b
, u16 reg
, u16 len
, u32 val
)
498 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
504 put_unaligned_be16(reg
, buf
);
505 put_unaligned_be32(val
<< 8 * (4 - len
), buf
+ 2);
506 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
512 static int og01a1b_write_reg_list(struct og01a1b
*og01a1b
,
513 const struct og01a1b_reg_list
*r_list
)
515 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
519 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
520 ret
= og01a1b_write_reg(og01a1b
, r_list
->regs
[i
].address
, 1,
521 r_list
->regs
[i
].val
);
523 dev_err_ratelimited(&client
->dev
,
524 "failed to write reg 0x%4.4x. error = %d",
525 r_list
->regs
[i
].address
, ret
);
533 static int og01a1b_test_pattern(struct og01a1b
*og01a1b
, u32 pattern
)
536 pattern
= (pattern
- 1) << OG01A1B_TEST_PATTERN_BAR_SHIFT
|
537 OG01A1B_TEST_PATTERN_ENABLE
;
539 return og01a1b_write_reg(og01a1b
, OG01A1B_REG_TEST_PATTERN
,
540 OG01A1B_REG_VALUE_08BIT
, pattern
);
543 static int og01a1b_set_ctrl(struct v4l2_ctrl
*ctrl
)
545 struct og01a1b
*og01a1b
= container_of(ctrl
->handler
,
546 struct og01a1b
, ctrl_handler
);
547 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
551 /* Propagate change of current control to all related controls */
552 if (ctrl
->id
== V4L2_CID_VBLANK
) {
553 /* Update max exposure while meeting expected vblanking */
554 exposure_max
= og01a1b
->cur_mode
->height
+ ctrl
->val
-
555 OG01A1B_EXPOSURE_MAX_MARGIN
;
556 __v4l2_ctrl_modify_range(og01a1b
->exposure
,
557 og01a1b
->exposure
->minimum
,
558 exposure_max
, og01a1b
->exposure
->step
,
562 /* V4L2 controls values will be applied only when power is already up */
563 if (!pm_runtime_get_if_in_use(&client
->dev
))
567 case V4L2_CID_ANALOGUE_GAIN
:
568 ret
= og01a1b_write_reg(og01a1b
, OG01A1B_REG_ANALOG_GAIN
,
569 OG01A1B_REG_VALUE_16BIT
,
573 case V4L2_CID_DIGITAL_GAIN
:
574 ret
= og01a1b_write_reg(og01a1b
, OG01A1B_REG_DIG_GAIN
,
575 OG01A1B_REG_VALUE_24BIT
,
579 case V4L2_CID_EXPOSURE
:
580 ret
= og01a1b_write_reg(og01a1b
, OG01A1B_REG_EXPOSURE
,
581 OG01A1B_REG_VALUE_16BIT
, ctrl
->val
);
584 case V4L2_CID_VBLANK
:
585 ret
= og01a1b_write_reg(og01a1b
, OG01A1B_REG_VTS
,
586 OG01A1B_REG_VALUE_16BIT
,
587 og01a1b
->cur_mode
->height
+ ctrl
->val
);
590 case V4L2_CID_TEST_PATTERN
:
591 ret
= og01a1b_test_pattern(og01a1b
, ctrl
->val
);
599 pm_runtime_put(&client
->dev
);
604 static const struct v4l2_ctrl_ops og01a1b_ctrl_ops
= {
605 .s_ctrl
= og01a1b_set_ctrl
,
608 static int og01a1b_init_controls(struct og01a1b
*og01a1b
)
610 struct v4l2_ctrl_handler
*ctrl_hdlr
;
611 s64 exposure_max
, h_blank
;
614 ctrl_hdlr
= &og01a1b
->ctrl_handler
;
615 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
619 ctrl_hdlr
->lock
= &og01a1b
->mutex
;
620 og01a1b
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
,
624 (link_freq_menu_items
) - 1,
625 0, link_freq_menu_items
);
626 if (og01a1b
->link_freq
)
627 og01a1b
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
629 og01a1b
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
,
630 V4L2_CID_PIXEL_RATE
, 0,
632 (OG01A1B_LINK_FREQ_1000MBPS
),
635 (OG01A1B_LINK_FREQ_1000MBPS
));
636 og01a1b
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
,
638 og01a1b
->cur_mode
->vts_min
-
639 og01a1b
->cur_mode
->height
,
641 og01a1b
->cur_mode
->height
, 1,
642 og01a1b
->cur_mode
->vts_def
-
643 og01a1b
->cur_mode
->height
);
644 h_blank
= to_pixels_per_line(og01a1b
->cur_mode
->hts
,
645 og01a1b
->cur_mode
->link_freq_index
) -
646 og01a1b
->cur_mode
->width
;
647 og01a1b
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
,
648 V4L2_CID_HBLANK
, h_blank
, h_blank
,
651 og01a1b
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
653 v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
654 OG01A1B_ANAL_GAIN_MIN
, OG01A1B_ANAL_GAIN_MAX
,
655 OG01A1B_ANAL_GAIN_STEP
, OG01A1B_ANAL_GAIN_MIN
);
656 v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
657 OG01A1B_DGTL_GAIN_MIN
, OG01A1B_DGTL_GAIN_MAX
,
658 OG01A1B_DGTL_GAIN_STEP
, OG01A1B_DGTL_GAIN_DEFAULT
);
659 exposure_max
= (og01a1b
->cur_mode
->vts_def
-
660 OG01A1B_EXPOSURE_MAX_MARGIN
);
661 og01a1b
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &og01a1b_ctrl_ops
,
663 OG01A1B_EXPOSURE_MIN
,
665 OG01A1B_EXPOSURE_STEP
,
667 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &og01a1b_ctrl_ops
,
668 V4L2_CID_TEST_PATTERN
,
669 ARRAY_SIZE(og01a1b_test_pattern_menu
) - 1,
670 0, 0, og01a1b_test_pattern_menu
);
672 if (ctrl_hdlr
->error
)
673 return ctrl_hdlr
->error
;
675 og01a1b
->sd
.ctrl_handler
= ctrl_hdlr
;
680 static void og01a1b_update_pad_format(const struct og01a1b_mode
*mode
,
681 struct v4l2_mbus_framefmt
*fmt
)
683 fmt
->width
= mode
->width
;
684 fmt
->height
= mode
->height
;
685 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
686 fmt
->field
= V4L2_FIELD_NONE
;
689 static int og01a1b_start_streaming(struct og01a1b
*og01a1b
)
691 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
692 const struct og01a1b_reg_list
*reg_list
;
693 int link_freq_index
, ret
;
695 link_freq_index
= og01a1b
->cur_mode
->link_freq_index
;
696 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
698 ret
= og01a1b_write_reg_list(og01a1b
, reg_list
);
700 dev_err(&client
->dev
, "failed to set plls");
704 reg_list
= &og01a1b
->cur_mode
->reg_list
;
705 ret
= og01a1b_write_reg_list(og01a1b
, reg_list
);
707 dev_err(&client
->dev
, "failed to set mode");
711 ret
= __v4l2_ctrl_handler_setup(og01a1b
->sd
.ctrl_handler
);
715 ret
= og01a1b_write_reg(og01a1b
, OG01A1B_REG_MODE_SELECT
,
716 OG01A1B_REG_VALUE_08BIT
,
717 OG01A1B_MODE_STREAMING
);
719 dev_err(&client
->dev
, "failed to set stream");
726 static void og01a1b_stop_streaming(struct og01a1b
*og01a1b
)
728 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
730 if (og01a1b_write_reg(og01a1b
, OG01A1B_REG_MODE_SELECT
,
731 OG01A1B_REG_VALUE_08BIT
, OG01A1B_MODE_STANDBY
))
732 dev_err(&client
->dev
, "failed to set stream");
735 static int og01a1b_set_stream(struct v4l2_subdev
*sd
, int enable
)
737 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
738 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
741 mutex_lock(&og01a1b
->mutex
);
743 ret
= pm_runtime_resume_and_get(&client
->dev
);
745 mutex_unlock(&og01a1b
->mutex
);
749 ret
= og01a1b_start_streaming(og01a1b
);
752 og01a1b_stop_streaming(og01a1b
);
753 pm_runtime_put(&client
->dev
);
756 og01a1b_stop_streaming(og01a1b
);
757 pm_runtime_put(&client
->dev
);
760 mutex_unlock(&og01a1b
->mutex
);
765 static int og01a1b_set_format(struct v4l2_subdev
*sd
,
766 struct v4l2_subdev_state
*sd_state
,
767 struct v4l2_subdev_format
*fmt
)
769 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
770 const struct og01a1b_mode
*mode
;
771 s32 vblank_def
, h_blank
;
773 mode
= v4l2_find_nearest_size(supported_modes
,
774 ARRAY_SIZE(supported_modes
), width
,
775 height
, fmt
->format
.width
,
778 mutex_lock(&og01a1b
->mutex
);
779 og01a1b_update_pad_format(mode
, &fmt
->format
);
780 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
781 *v4l2_subdev_state_get_format(sd_state
, fmt
->pad
) = fmt
->format
;
783 og01a1b
->cur_mode
= mode
;
784 __v4l2_ctrl_s_ctrl(og01a1b
->link_freq
, mode
->link_freq_index
);
785 __v4l2_ctrl_s_ctrl_int64(og01a1b
->pixel_rate
,
786 to_pixel_rate(mode
->link_freq_index
));
788 /* Update limits and set FPS to default */
789 vblank_def
= mode
->vts_def
- mode
->height
;
790 __v4l2_ctrl_modify_range(og01a1b
->vblank
,
791 mode
->vts_min
- mode
->height
,
792 OG01A1B_VTS_MAX
- mode
->height
, 1,
794 __v4l2_ctrl_s_ctrl(og01a1b
->vblank
, vblank_def
);
795 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
797 __v4l2_ctrl_modify_range(og01a1b
->hblank
, h_blank
, h_blank
, 1,
801 mutex_unlock(&og01a1b
->mutex
);
806 static int og01a1b_get_format(struct v4l2_subdev
*sd
,
807 struct v4l2_subdev_state
*sd_state
,
808 struct v4l2_subdev_format
*fmt
)
810 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
812 mutex_lock(&og01a1b
->mutex
);
813 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
814 fmt
->format
= *v4l2_subdev_state_get_format(sd_state
,
817 og01a1b_update_pad_format(og01a1b
->cur_mode
, &fmt
->format
);
819 mutex_unlock(&og01a1b
->mutex
);
824 static int og01a1b_enum_mbus_code(struct v4l2_subdev
*sd
,
825 struct v4l2_subdev_state
*sd_state
,
826 struct v4l2_subdev_mbus_code_enum
*code
)
831 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
836 static int og01a1b_enum_frame_size(struct v4l2_subdev
*sd
,
837 struct v4l2_subdev_state
*sd_state
,
838 struct v4l2_subdev_frame_size_enum
*fse
)
840 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
843 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
846 fse
->min_width
= supported_modes
[fse
->index
].width
;
847 fse
->max_width
= fse
->min_width
;
848 fse
->min_height
= supported_modes
[fse
->index
].height
;
849 fse
->max_height
= fse
->min_height
;
854 static int og01a1b_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
856 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
858 mutex_lock(&og01a1b
->mutex
);
859 og01a1b_update_pad_format(&supported_modes
[0],
860 v4l2_subdev_state_get_format(fh
->state
, 0));
861 mutex_unlock(&og01a1b
->mutex
);
866 static const struct v4l2_subdev_video_ops og01a1b_video_ops
= {
867 .s_stream
= og01a1b_set_stream
,
870 static const struct v4l2_subdev_pad_ops og01a1b_pad_ops
= {
871 .set_fmt
= og01a1b_set_format
,
872 .get_fmt
= og01a1b_get_format
,
873 .enum_mbus_code
= og01a1b_enum_mbus_code
,
874 .enum_frame_size
= og01a1b_enum_frame_size
,
877 static const struct v4l2_subdev_ops og01a1b_subdev_ops
= {
878 .video
= &og01a1b_video_ops
,
879 .pad
= &og01a1b_pad_ops
,
882 static const struct media_entity_operations og01a1b_subdev_entity_ops
= {
883 .link_validate
= v4l2_subdev_link_validate
,
886 static const struct v4l2_subdev_internal_ops og01a1b_internal_ops
= {
887 .open
= og01a1b_open
,
890 static int og01a1b_identify_module(struct og01a1b
*og01a1b
)
892 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
896 ret
= og01a1b_read_reg(og01a1b
, OG01A1B_REG_CHIP_ID
,
897 OG01A1B_REG_VALUE_24BIT
, &val
);
901 if (val
!= OG01A1B_CHIP_ID
) {
902 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
903 OG01A1B_CHIP_ID
, val
);
910 static int og01a1b_check_hwcfg(struct og01a1b
*og01a1b
)
912 struct i2c_client
*client
= v4l2_get_subdevdata(&og01a1b
->sd
);
913 struct device
*dev
= &client
->dev
;
914 struct fwnode_handle
*ep
;
915 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
916 struct v4l2_fwnode_endpoint bus_cfg
= {
917 .bus_type
= V4L2_MBUS_CSI2_DPHY
926 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &mclk
);
928 if (!og01a1b
->xvclk
) {
929 dev_err(dev
, "can't get clock frequency");
933 mclk
= clk_get_rate(og01a1b
->xvclk
);
936 if (mclk
!= OG01A1B_MCLK
) {
937 dev_err(dev
, "external clock %d is not supported", mclk
);
941 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
945 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
946 fwnode_handle_put(ep
);
950 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OG01A1B_DATA_LANES
) {
951 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
952 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
954 goto check_hwcfg_error
;
957 if (!bus_cfg
.nr_of_link_frequencies
) {
958 dev_err(dev
, "no link frequencies defined");
960 goto check_hwcfg_error
;
963 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
964 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
965 if (link_freq_menu_items
[i
] ==
966 bus_cfg
.link_frequencies
[j
])
970 if (j
== bus_cfg
.nr_of_link_frequencies
) {
971 dev_err(dev
, "no link frequency %lld supported",
972 link_freq_menu_items
[i
]);
974 goto check_hwcfg_error
;
979 v4l2_fwnode_endpoint_free(&bus_cfg
);
984 /* Power/clock management functions */
985 static int og01a1b_power_on(struct device
*dev
)
987 unsigned long delay
= DIV_ROUND_UP(8192UL * USEC_PER_SEC
, OG01A1B_MCLK
);
988 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
989 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
993 ret
= regulator_enable(og01a1b
->avdd
);
998 if (og01a1b
->dovdd
) {
999 ret
= regulator_enable(og01a1b
->dovdd
);
1004 if (og01a1b
->dvdd
) {
1005 ret
= regulator_enable(og01a1b
->dvdd
);
1010 ret
= clk_prepare_enable(og01a1b
->xvclk
);
1014 gpiod_set_value_cansleep(og01a1b
->reset_gpio
, 0);
1016 if (og01a1b
->reset_gpio
)
1017 usleep_range(5 * USEC_PER_MSEC
, 6 * USEC_PER_MSEC
);
1018 else if (og01a1b
->xvclk
)
1019 usleep_range(delay
, 2 * delay
);
1025 regulator_disable(og01a1b
->dvdd
);
1028 regulator_disable(og01a1b
->dovdd
);
1031 regulator_disable(og01a1b
->avdd
);
1036 static int og01a1b_power_off(struct device
*dev
)
1038 unsigned long delay
= DIV_ROUND_UP(512 * USEC_PER_SEC
, OG01A1B_MCLK
);
1039 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1040 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
1043 usleep_range(delay
, 2 * delay
);
1045 clk_disable_unprepare(og01a1b
->xvclk
);
1047 gpiod_set_value_cansleep(og01a1b
->reset_gpio
, 1);
1050 regulator_disable(og01a1b
->dvdd
);
1053 regulator_disable(og01a1b
->dovdd
);
1056 regulator_disable(og01a1b
->avdd
);
1061 static void og01a1b_remove(struct i2c_client
*client
)
1063 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1064 struct og01a1b
*og01a1b
= to_og01a1b(sd
);
1066 v4l2_async_unregister_subdev(sd
);
1067 media_entity_cleanup(&sd
->entity
);
1068 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1069 pm_runtime_disable(&client
->dev
);
1070 mutex_destroy(&og01a1b
->mutex
);
1073 static int og01a1b_probe(struct i2c_client
*client
)
1075 struct og01a1b
*og01a1b
;
1078 og01a1b
= devm_kzalloc(&client
->dev
, sizeof(*og01a1b
), GFP_KERNEL
);
1082 v4l2_i2c_subdev_init(&og01a1b
->sd
, client
, &og01a1b_subdev_ops
);
1084 og01a1b
->xvclk
= devm_clk_get_optional(&client
->dev
, NULL
);
1085 if (IS_ERR(og01a1b
->xvclk
)) {
1086 ret
= PTR_ERR(og01a1b
->xvclk
);
1087 dev_err(&client
->dev
, "failed to get xvclk clock: %d\n", ret
);
1091 ret
= og01a1b_check_hwcfg(og01a1b
);
1093 dev_err(&client
->dev
, "failed to check HW configuration: %d",
1098 og01a1b
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
1100 if (IS_ERR(og01a1b
->reset_gpio
)) {
1101 dev_err(&client
->dev
, "cannot get reset GPIO\n");
1102 return PTR_ERR(og01a1b
->reset_gpio
);
1105 og01a1b
->avdd
= devm_regulator_get_optional(&client
->dev
, "avdd");
1106 if (IS_ERR(og01a1b
->avdd
)) {
1107 ret
= PTR_ERR(og01a1b
->avdd
);
1108 if (ret
!= -ENODEV
) {
1109 dev_err_probe(&client
->dev
, ret
,
1110 "Failed to get 'avdd' regulator\n");
1114 og01a1b
->avdd
= NULL
;
1117 og01a1b
->dovdd
= devm_regulator_get_optional(&client
->dev
, "dovdd");
1118 if (IS_ERR(og01a1b
->dovdd
)) {
1119 ret
= PTR_ERR(og01a1b
->dovdd
);
1120 if (ret
!= -ENODEV
) {
1121 dev_err_probe(&client
->dev
, ret
,
1122 "Failed to get 'dovdd' regulator\n");
1126 og01a1b
->dovdd
= NULL
;
1129 og01a1b
->dvdd
= devm_regulator_get_optional(&client
->dev
, "dvdd");
1130 if (IS_ERR(og01a1b
->dvdd
)) {
1131 ret
= PTR_ERR(og01a1b
->dvdd
);
1132 if (ret
!= -ENODEV
) {
1133 dev_err_probe(&client
->dev
, ret
,
1134 "Failed to get 'dvdd' regulator\n");
1138 og01a1b
->dvdd
= NULL
;
1141 /* The sensor must be powered on to read the CHIP_ID register */
1142 ret
= og01a1b_power_on(&client
->dev
);
1146 ret
= og01a1b_identify_module(og01a1b
);
1148 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1152 mutex_init(&og01a1b
->mutex
);
1153 og01a1b
->cur_mode
= &supported_modes
[0];
1154 ret
= og01a1b_init_controls(og01a1b
);
1156 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1157 goto probe_error_v4l2_ctrl_handler_free
;
1160 og01a1b
->sd
.internal_ops
= &og01a1b_internal_ops
;
1161 og01a1b
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1162 og01a1b
->sd
.entity
.ops
= &og01a1b_subdev_entity_ops
;
1163 og01a1b
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1164 og01a1b
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1165 ret
= media_entity_pads_init(&og01a1b
->sd
.entity
, 1, &og01a1b
->pad
);
1167 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1168 goto probe_error_v4l2_ctrl_handler_free
;
1171 ret
= v4l2_async_register_subdev_sensor(&og01a1b
->sd
);
1173 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1175 goto probe_error_media_entity_cleanup
;
1178 /* Enable runtime PM and turn off the device */
1179 pm_runtime_set_active(&client
->dev
);
1180 pm_runtime_enable(&client
->dev
);
1181 pm_runtime_idle(&client
->dev
);
1185 probe_error_media_entity_cleanup
:
1186 media_entity_cleanup(&og01a1b
->sd
.entity
);
1188 probe_error_v4l2_ctrl_handler_free
:
1189 v4l2_ctrl_handler_free(og01a1b
->sd
.ctrl_handler
);
1190 mutex_destroy(&og01a1b
->mutex
);
1193 og01a1b_power_off(&client
->dev
);
1198 static const struct dev_pm_ops og01a1b_pm_ops
= {
1199 SET_RUNTIME_PM_OPS(og01a1b_power_off
, og01a1b_power_on
, NULL
)
1203 static const struct acpi_device_id og01a1b_acpi_ids
[] = {
1208 MODULE_DEVICE_TABLE(acpi
, og01a1b_acpi_ids
);
1211 static const struct of_device_id og01a1b_of_match
[] = {
1212 { .compatible
= "ovti,og01a1b" },
1215 MODULE_DEVICE_TABLE(of
, og01a1b_of_match
);
1217 static struct i2c_driver og01a1b_i2c_driver
= {
1220 .pm
= &og01a1b_pm_ops
,
1221 .acpi_match_table
= ACPI_PTR(og01a1b_acpi_ids
),
1222 .of_match_table
= og01a1b_of_match
,
1224 .probe
= og01a1b_probe
,
1225 .remove
= og01a1b_remove
,
1228 module_i2c_driver(og01a1b_i2c_driver
);
1230 MODULE_AUTHOR("Shawn Tu");
1231 MODULE_DESCRIPTION("OmniVision OG01A1B sensor driver");
1232 MODULE_LICENSE("GPL v2");