1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/nvmem-provider.h>
11 #include <linux/regmap.h>
12 #include <media/v4l2-ctrls.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-fwnode.h>
16 #define OV2740_LINK_FREQ_360MHZ 360000000ULL
17 #define OV2740_SCLK 72000000LL
18 #define OV2740_MCLK 19200000
19 #define OV2740_DATA_LANES 2
20 #define OV2740_RGB_DEPTH 10
22 #define OV2740_REG_CHIP_ID 0x300a
23 #define OV2740_CHIP_ID 0x2740
25 #define OV2740_REG_MODE_SELECT 0x0100
26 #define OV2740_MODE_STANDBY 0x00
27 #define OV2740_MODE_STREAMING 0x01
29 /* vertical-timings from sensor */
30 #define OV2740_REG_VTS 0x380e
31 #define OV2740_VTS_DEF 0x088a
32 #define OV2740_VTS_MIN 0x0460
33 #define OV2740_VTS_MAX 0x7fff
35 /* horizontal-timings from sensor */
36 #define OV2740_REG_HTS 0x380c
38 /* Exposure controls from sensor */
39 #define OV2740_REG_EXPOSURE 0x3500
40 #define OV2740_EXPOSURE_MIN 4
41 #define OV2740_EXPOSURE_MAX_MARGIN 8
42 #define OV2740_EXPOSURE_STEP 1
44 /* Analog gain controls from sensor */
45 #define OV2740_REG_ANALOG_GAIN 0x3508
46 #define OV2740_ANAL_GAIN_MIN 128
47 #define OV2740_ANAL_GAIN_MAX 1983
48 #define OV2740_ANAL_GAIN_STEP 1
50 /* Digital gain controls from sensor */
51 #define OV2740_REG_MWB_R_GAIN 0x500a
52 #define OV2740_REG_MWB_G_GAIN 0x500c
53 #define OV2740_REG_MWB_B_GAIN 0x500e
54 #define OV2740_DGTL_GAIN_MIN 0
55 #define OV2740_DGTL_GAIN_MAX 4095
56 #define OV2740_DGTL_GAIN_STEP 1
57 #define OV2740_DGTL_GAIN_DEFAULT 1024
59 /* Test Pattern Control */
60 #define OV2740_REG_TEST_PATTERN 0x5040
61 #define OV2740_TEST_PATTERN_ENABLE BIT(7)
62 #define OV2740_TEST_PATTERN_BAR_SHIFT 2
65 #define OV2740_REG_ISP_CTRL00 0x5000
67 #define OV2740_REG_ISP_CTRL01 0x5001
68 /* Customer Addresses: 0x7010 - 0x710F */
69 #define CUSTOMER_USE_OTP_SIZE 0x100
70 /* OTP registers from sensor */
71 #define OV2740_REG_OTP_CUSTOMER 0x7010
74 struct i2c_client
*client
;
75 struct nvmem_device
*nvmem
;
76 struct regmap
*regmap
;
81 OV2740_LINK_FREQ_360MHZ_INDEX
,
89 struct ov2740_reg_list
{
91 const struct ov2740_reg
*regs
;
94 struct ov2740_link_freq_config
{
95 const struct ov2740_reg_list reg_list
;
99 /* Frame width in pixels */
102 /* Frame height in pixels */
105 /* Horizontal timining size */
108 /* Default vertical timining size */
111 /* Min vertical timining size */
114 /* Link frequency needed for this resolution */
117 /* Sensor register settings for this resolution */
118 const struct ov2740_reg_list reg_list
;
121 static const struct ov2740_reg mipi_data_rate_720mbps
[] = {
130 static const struct ov2740_reg mode_1932x1092_regs
[] = {
283 static const char * const ov2740_test_pattern_menu
[] = {
286 "Top-Bottom Darker Color Bar",
287 "Right-Left Darker Color Bar",
288 "Bottom-Top Darker Color Bar",
291 static const s64 link_freq_menu_items
[] = {
292 OV2740_LINK_FREQ_360MHZ
,
295 static const struct ov2740_link_freq_config link_freq_configs
[] = {
296 [OV2740_LINK_FREQ_360MHZ_INDEX
] = {
298 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_720mbps
),
299 .regs
= mipi_data_rate_720mbps
,
304 static const struct ov2740_mode supported_modes
[] = {
309 .vts_def
= OV2740_VTS_DEF
,
310 .vts_min
= OV2740_VTS_MIN
,
312 .num_of_regs
= ARRAY_SIZE(mode_1932x1092_regs
),
313 .regs
= mode_1932x1092_regs
,
315 .link_freq_index
= OV2740_LINK_FREQ_360MHZ_INDEX
,
320 struct v4l2_subdev sd
;
321 struct media_pad pad
;
322 struct v4l2_ctrl_handler ctrl_handler
;
325 struct v4l2_ctrl
*link_freq
;
326 struct v4l2_ctrl
*pixel_rate
;
327 struct v4l2_ctrl
*vblank
;
328 struct v4l2_ctrl
*hblank
;
329 struct v4l2_ctrl
*exposure
;
332 const struct ov2740_mode
*cur_mode
;
334 /* To serialize asynchronus callbacks */
337 /* Streaming on/off */
340 /* NVM data inforamtion */
341 struct nvm_data
*nvm
;
344 static inline struct ov2740
*to_ov2740(struct v4l2_subdev
*subdev
)
346 return container_of(subdev
, struct ov2740
, sd
);
349 static u64
to_pixel_rate(u32 f_index
)
351 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OV2740_DATA_LANES
;
353 do_div(pixel_rate
, OV2740_RGB_DEPTH
);
358 static u64
to_pixels_per_line(u32 hts
, u32 f_index
)
360 u64 ppl
= hts
* to_pixel_rate(f_index
);
362 do_div(ppl
, OV2740_SCLK
);
367 static int ov2740_read_reg(struct ov2740
*ov2740
, u16 reg
, u16 len
, u32
*val
)
369 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
370 struct i2c_msg msgs
[2];
372 u8 data_buf
[4] = {0};
375 if (len
> sizeof(data_buf
))
378 put_unaligned_be16(reg
, addr_buf
);
379 msgs
[0].addr
= client
->addr
;
381 msgs
[0].len
= sizeof(addr_buf
);
382 msgs
[0].buf
= addr_buf
;
383 msgs
[1].addr
= client
->addr
;
384 msgs
[1].flags
= I2C_M_RD
;
386 msgs
[1].buf
= &data_buf
[sizeof(data_buf
) - len
];
388 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
389 if (ret
!= ARRAY_SIZE(msgs
))
390 return ret
< 0 ? ret
: -EIO
;
392 *val
= get_unaligned_be32(data_buf
);
397 static int ov2740_write_reg(struct ov2740
*ov2740
, u16 reg
, u16 len
, u32 val
)
399 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
406 put_unaligned_be16(reg
, buf
);
407 put_unaligned_be32(val
<< 8 * (4 - len
), buf
+ 2);
409 ret
= i2c_master_send(client
, buf
, len
+ 2);
411 return ret
< 0 ? ret
: -EIO
;
416 static int ov2740_write_reg_list(struct ov2740
*ov2740
,
417 const struct ov2740_reg_list
*r_list
)
419 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
423 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
424 ret
= ov2740_write_reg(ov2740
, r_list
->regs
[i
].address
, 1,
425 r_list
->regs
[i
].val
);
427 dev_err_ratelimited(&client
->dev
,
428 "write reg 0x%4.4x return err = %d",
429 r_list
->regs
[i
].address
, ret
);
437 static int ov2740_update_digital_gain(struct ov2740
*ov2740
, u32 d_gain
)
441 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MWB_R_GAIN
, 2, d_gain
);
445 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MWB_G_GAIN
, 2, d_gain
);
449 return ov2740_write_reg(ov2740
, OV2740_REG_MWB_B_GAIN
, 2, d_gain
);
452 static int ov2740_test_pattern(struct ov2740
*ov2740
, u32 pattern
)
455 pattern
= (pattern
- 1) << OV2740_TEST_PATTERN_BAR_SHIFT
|
456 OV2740_TEST_PATTERN_ENABLE
;
458 return ov2740_write_reg(ov2740
, OV2740_REG_TEST_PATTERN
, 1, pattern
);
461 static int ov2740_set_ctrl(struct v4l2_ctrl
*ctrl
)
463 struct ov2740
*ov2740
= container_of(ctrl
->handler
,
464 struct ov2740
, ctrl_handler
);
465 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
469 /* Propagate change of current control to all related controls */
470 if (ctrl
->id
== V4L2_CID_VBLANK
) {
471 /* Update max exposure while meeting expected vblanking */
472 exposure_max
= ov2740
->cur_mode
->height
+ ctrl
->val
-
473 OV2740_EXPOSURE_MAX_MARGIN
;
474 __v4l2_ctrl_modify_range(ov2740
->exposure
,
475 ov2740
->exposure
->minimum
,
476 exposure_max
, ov2740
->exposure
->step
,
480 /* V4L2 controls values will be applied only when power is already up */
481 if (!pm_runtime_get_if_in_use(&client
->dev
))
485 case V4L2_CID_ANALOGUE_GAIN
:
486 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ANALOG_GAIN
, 2,
490 case V4L2_CID_DIGITAL_GAIN
:
491 ret
= ov2740_update_digital_gain(ov2740
, ctrl
->val
);
494 case V4L2_CID_EXPOSURE
:
495 /* 4 least significant bits of expsoure are fractional part */
496 ret
= ov2740_write_reg(ov2740
, OV2740_REG_EXPOSURE
, 3,
500 case V4L2_CID_VBLANK
:
501 ret
= ov2740_write_reg(ov2740
, OV2740_REG_VTS
, 2,
502 ov2740
->cur_mode
->height
+ ctrl
->val
);
505 case V4L2_CID_TEST_PATTERN
:
506 ret
= ov2740_test_pattern(ov2740
, ctrl
->val
);
514 pm_runtime_put(&client
->dev
);
519 static const struct v4l2_ctrl_ops ov2740_ctrl_ops
= {
520 .s_ctrl
= ov2740_set_ctrl
,
523 static int ov2740_init_controls(struct ov2740
*ov2740
)
525 struct v4l2_ctrl_handler
*ctrl_hdlr
;
526 const struct ov2740_mode
*cur_mode
;
527 s64 exposure_max
, h_blank
, pixel_rate
;
528 u32 vblank_min
, vblank_max
, vblank_default
;
532 ctrl_hdlr
= &ov2740
->ctrl_handler
;
533 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
537 ctrl_hdlr
->lock
= &ov2740
->mutex
;
538 cur_mode
= ov2740
->cur_mode
;
539 size
= ARRAY_SIZE(link_freq_menu_items
);
541 ov2740
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &ov2740_ctrl_ops
,
544 link_freq_menu_items
);
545 if (ov2740
->link_freq
)
546 ov2740
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
548 pixel_rate
= to_pixel_rate(OV2740_LINK_FREQ_360MHZ_INDEX
);
549 ov2740
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
550 V4L2_CID_PIXEL_RATE
, 0,
551 pixel_rate
, 1, pixel_rate
);
553 vblank_min
= cur_mode
->vts_min
- cur_mode
->height
;
554 vblank_max
= OV2740_VTS_MAX
- cur_mode
->height
;
555 vblank_default
= cur_mode
->vts_def
- cur_mode
->height
;
556 ov2740
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
557 V4L2_CID_VBLANK
, vblank_min
,
558 vblank_max
, 1, vblank_default
);
560 h_blank
= to_pixels_per_line(cur_mode
->hts
, cur_mode
->link_freq_index
);
561 h_blank
-= cur_mode
->width
;
562 ov2740
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
563 V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
566 ov2740
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
568 v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
569 OV2740_ANAL_GAIN_MIN
, OV2740_ANAL_GAIN_MAX
,
570 OV2740_ANAL_GAIN_STEP
, OV2740_ANAL_GAIN_MIN
);
571 v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
572 OV2740_DGTL_GAIN_MIN
, OV2740_DGTL_GAIN_MAX
,
573 OV2740_DGTL_GAIN_STEP
, OV2740_DGTL_GAIN_DEFAULT
);
574 exposure_max
= cur_mode
->vts_def
- OV2740_EXPOSURE_MAX_MARGIN
;
575 ov2740
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
577 OV2740_EXPOSURE_MIN
, exposure_max
,
578 OV2740_EXPOSURE_STEP
,
580 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov2740_ctrl_ops
,
581 V4L2_CID_TEST_PATTERN
,
582 ARRAY_SIZE(ov2740_test_pattern_menu
) - 1,
583 0, 0, ov2740_test_pattern_menu
);
584 if (ctrl_hdlr
->error
)
585 return ctrl_hdlr
->error
;
587 ov2740
->sd
.ctrl_handler
= ctrl_hdlr
;
592 static void ov2740_update_pad_format(const struct ov2740_mode
*mode
,
593 struct v4l2_mbus_framefmt
*fmt
)
595 fmt
->width
= mode
->width
;
596 fmt
->height
= mode
->height
;
597 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
598 fmt
->field
= V4L2_FIELD_NONE
;
601 static int ov2740_load_otp_data(struct nvm_data
*nvm
)
603 struct i2c_client
*client
;
604 struct ov2740
*ov2740
;
615 client
= nvm
->client
;
616 ov2740
= to_ov2740(i2c_get_clientdata(client
));
618 nvm
->nvm_buffer
= kzalloc(CUSTOMER_USE_OTP_SIZE
, GFP_KERNEL
);
619 if (!nvm
->nvm_buffer
)
622 ret
= ov2740_read_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1, &isp_ctrl00
);
624 dev_err(&client
->dev
, "failed to read ISP CTRL00\n");
628 ret
= ov2740_read_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1, &isp_ctrl01
);
630 dev_err(&client
->dev
, "failed to read ISP CTRL01\n");
634 /* Clear bit 5 of ISP CTRL00 */
635 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1,
636 isp_ctrl00
& ~BIT(5));
638 dev_err(&client
->dev
, "failed to set ISP CTRL00\n");
642 /* Clear bit 7 of ISP CTRL01 */
643 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1,
644 isp_ctrl01
& ~BIT(7));
646 dev_err(&client
->dev
, "failed to set ISP CTRL01\n");
650 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
651 OV2740_MODE_STREAMING
);
653 dev_err(&client
->dev
, "failed to set streaming mode\n");
658 * Users are not allowed to access OTP-related registers and memory
659 * during the 20 ms period after streaming starts (0x100 = 0x01).
663 ret
= regmap_bulk_read(nvm
->regmap
, OV2740_REG_OTP_CUSTOMER
,
664 nvm
->nvm_buffer
, CUSTOMER_USE_OTP_SIZE
);
666 dev_err(&client
->dev
, "failed to read OTP data, ret %d\n", ret
);
670 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
671 OV2740_MODE_STANDBY
);
673 dev_err(&client
->dev
, "failed to set streaming mode\n");
677 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1, isp_ctrl01
);
679 dev_err(&client
->dev
, "failed to set ISP CTRL01\n");
683 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1, isp_ctrl00
);
685 dev_err(&client
->dev
, "failed to set ISP CTRL00\n");
691 kfree(nvm
->nvm_buffer
);
692 nvm
->nvm_buffer
= NULL
;
697 static int ov2740_start_streaming(struct ov2740
*ov2740
)
699 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
700 struct nvm_data
*nvm
= ov2740
->nvm
;
701 const struct ov2740_reg_list
*reg_list
;
705 ov2740_load_otp_data(nvm
);
707 link_freq_index
= ov2740
->cur_mode
->link_freq_index
;
708 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
709 ret
= ov2740_write_reg_list(ov2740
, reg_list
);
711 dev_err(&client
->dev
, "failed to set plls");
715 reg_list
= &ov2740
->cur_mode
->reg_list
;
716 ret
= ov2740_write_reg_list(ov2740
, reg_list
);
718 dev_err(&client
->dev
, "failed to set mode");
722 ret
= __v4l2_ctrl_handler_setup(ov2740
->sd
.ctrl_handler
);
726 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
727 OV2740_MODE_STREAMING
);
729 dev_err(&client
->dev
, "failed to start streaming");
734 static void ov2740_stop_streaming(struct ov2740
*ov2740
)
736 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
738 if (ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
739 OV2740_MODE_STANDBY
))
740 dev_err(&client
->dev
, "failed to stop streaming");
743 static int ov2740_set_stream(struct v4l2_subdev
*sd
, int enable
)
745 struct ov2740
*ov2740
= to_ov2740(sd
);
746 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
749 if (ov2740
->streaming
== enable
)
752 mutex_lock(&ov2740
->mutex
);
754 ret
= pm_runtime_get_sync(&client
->dev
);
756 pm_runtime_put_noidle(&client
->dev
);
757 mutex_unlock(&ov2740
->mutex
);
761 ret
= ov2740_start_streaming(ov2740
);
764 ov2740_stop_streaming(ov2740
);
765 pm_runtime_put(&client
->dev
);
768 ov2740_stop_streaming(ov2740
);
769 pm_runtime_put(&client
->dev
);
772 ov2740
->streaming
= enable
;
773 mutex_unlock(&ov2740
->mutex
);
778 static int __maybe_unused
ov2740_suspend(struct device
*dev
)
780 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
781 struct ov2740
*ov2740
= to_ov2740(sd
);
783 mutex_lock(&ov2740
->mutex
);
784 if (ov2740
->streaming
)
785 ov2740_stop_streaming(ov2740
);
787 mutex_unlock(&ov2740
->mutex
);
792 static int __maybe_unused
ov2740_resume(struct device
*dev
)
794 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
795 struct ov2740
*ov2740
= to_ov2740(sd
);
798 mutex_lock(&ov2740
->mutex
);
799 if (!ov2740
->streaming
)
802 ret
= ov2740_start_streaming(ov2740
);
804 ov2740
->streaming
= false;
805 ov2740_stop_streaming(ov2740
);
809 mutex_unlock(&ov2740
->mutex
);
813 static int ov2740_set_format(struct v4l2_subdev
*sd
,
814 struct v4l2_subdev_pad_config
*cfg
,
815 struct v4l2_subdev_format
*fmt
)
817 struct ov2740
*ov2740
= to_ov2740(sd
);
818 const struct ov2740_mode
*mode
;
819 s32 vblank_def
, h_blank
;
821 mode
= v4l2_find_nearest_size(supported_modes
,
822 ARRAY_SIZE(supported_modes
), width
,
823 height
, fmt
->format
.width
,
826 mutex_lock(&ov2740
->mutex
);
827 ov2740_update_pad_format(mode
, &fmt
->format
);
828 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
829 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
831 ov2740
->cur_mode
= mode
;
832 __v4l2_ctrl_s_ctrl(ov2740
->link_freq
, mode
->link_freq_index
);
833 __v4l2_ctrl_s_ctrl_int64(ov2740
->pixel_rate
,
834 to_pixel_rate(mode
->link_freq_index
));
836 /* Update limits and set FPS to default */
837 vblank_def
= mode
->vts_def
- mode
->height
;
838 __v4l2_ctrl_modify_range(ov2740
->vblank
,
839 mode
->vts_min
- mode
->height
,
840 OV2740_VTS_MAX
- mode
->height
, 1,
842 __v4l2_ctrl_s_ctrl(ov2740
->vblank
, vblank_def
);
843 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
845 __v4l2_ctrl_modify_range(ov2740
->hblank
, h_blank
, h_blank
, 1,
848 mutex_unlock(&ov2740
->mutex
);
853 static int ov2740_get_format(struct v4l2_subdev
*sd
,
854 struct v4l2_subdev_pad_config
*cfg
,
855 struct v4l2_subdev_format
*fmt
)
857 struct ov2740
*ov2740
= to_ov2740(sd
);
859 mutex_lock(&ov2740
->mutex
);
860 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
861 fmt
->format
= *v4l2_subdev_get_try_format(&ov2740
->sd
, cfg
,
864 ov2740_update_pad_format(ov2740
->cur_mode
, &fmt
->format
);
866 mutex_unlock(&ov2740
->mutex
);
871 static int ov2740_enum_mbus_code(struct v4l2_subdev
*sd
,
872 struct v4l2_subdev_pad_config
*cfg
,
873 struct v4l2_subdev_mbus_code_enum
*code
)
878 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
883 static int ov2740_enum_frame_size(struct v4l2_subdev
*sd
,
884 struct v4l2_subdev_pad_config
*cfg
,
885 struct v4l2_subdev_frame_size_enum
*fse
)
887 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
890 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
893 fse
->min_width
= supported_modes
[fse
->index
].width
;
894 fse
->max_width
= fse
->min_width
;
895 fse
->min_height
= supported_modes
[fse
->index
].height
;
896 fse
->max_height
= fse
->min_height
;
901 static int ov2740_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
903 struct ov2740
*ov2740
= to_ov2740(sd
);
905 mutex_lock(&ov2740
->mutex
);
906 ov2740_update_pad_format(&supported_modes
[0],
907 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0));
908 mutex_unlock(&ov2740
->mutex
);
913 static const struct v4l2_subdev_video_ops ov2740_video_ops
= {
914 .s_stream
= ov2740_set_stream
,
917 static const struct v4l2_subdev_pad_ops ov2740_pad_ops
= {
918 .set_fmt
= ov2740_set_format
,
919 .get_fmt
= ov2740_get_format
,
920 .enum_mbus_code
= ov2740_enum_mbus_code
,
921 .enum_frame_size
= ov2740_enum_frame_size
,
924 static const struct v4l2_subdev_ops ov2740_subdev_ops
= {
925 .video
= &ov2740_video_ops
,
926 .pad
= &ov2740_pad_ops
,
929 static const struct media_entity_operations ov2740_subdev_entity_ops
= {
930 .link_validate
= v4l2_subdev_link_validate
,
933 static const struct v4l2_subdev_internal_ops ov2740_internal_ops
= {
937 static int ov2740_identify_module(struct ov2740
*ov2740
)
939 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
943 ret
= ov2740_read_reg(ov2740
, OV2740_REG_CHIP_ID
, 3, &val
);
947 if (val
!= OV2740_CHIP_ID
) {
948 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
949 OV2740_CHIP_ID
, val
);
956 static int ov2740_check_hwcfg(struct device
*dev
)
958 struct fwnode_handle
*ep
;
959 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
960 struct v4l2_fwnode_endpoint bus_cfg
= {
961 .bus_type
= V4L2_MBUS_CSI2_DPHY
970 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &mclk
);
974 if (mclk
!= OV2740_MCLK
) {
975 dev_err(dev
, "external clock %d is not supported", mclk
);
979 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
983 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
984 fwnode_handle_put(ep
);
988 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV2740_DATA_LANES
) {
989 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
990 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
992 goto check_hwcfg_error
;
995 if (!bus_cfg
.nr_of_link_frequencies
) {
996 dev_err(dev
, "no link frequencies defined");
998 goto check_hwcfg_error
;
1001 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1002 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1003 if (link_freq_menu_items
[i
] ==
1004 bus_cfg
.link_frequencies
[j
])
1008 if (j
== bus_cfg
.nr_of_link_frequencies
) {
1009 dev_err(dev
, "no link frequency %lld supported",
1010 link_freq_menu_items
[i
]);
1012 goto check_hwcfg_error
;
1017 v4l2_fwnode_endpoint_free(&bus_cfg
);
1022 static int ov2740_remove(struct i2c_client
*client
)
1024 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1025 struct ov2740
*ov2740
= to_ov2740(sd
);
1027 v4l2_async_unregister_subdev(sd
);
1028 media_entity_cleanup(&sd
->entity
);
1029 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1030 pm_runtime_disable(&client
->dev
);
1031 mutex_destroy(&ov2740
->mutex
);
1036 static int ov2740_nvmem_read(void *priv
, unsigned int off
, void *val
,
1039 struct nvm_data
*nvm
= priv
;
1040 struct v4l2_subdev
*sd
= i2c_get_clientdata(nvm
->client
);
1041 struct device
*dev
= &nvm
->client
->dev
;
1042 struct ov2740
*ov2740
= to_ov2740(sd
);
1045 mutex_lock(&ov2740
->mutex
);
1047 if (nvm
->nvm_buffer
) {
1048 memcpy(val
, nvm
->nvm_buffer
+ off
, count
);
1052 ret
= pm_runtime_get_sync(dev
);
1054 pm_runtime_put_noidle(dev
);
1058 ret
= ov2740_load_otp_data(nvm
);
1060 memcpy(val
, nvm
->nvm_buffer
+ off
, count
);
1062 pm_runtime_put(dev
);
1064 mutex_unlock(&ov2740
->mutex
);
1068 static int ov2740_register_nvmem(struct i2c_client
*client
,
1069 struct ov2740
*ov2740
)
1071 struct nvm_data
*nvm
;
1072 struct regmap_config regmap_config
= { };
1073 struct nvmem_config nvmem_config
= { };
1074 struct regmap
*regmap
;
1075 struct device
*dev
= &client
->dev
;
1078 nvm
= devm_kzalloc(dev
, sizeof(*nvm
), GFP_KERNEL
);
1082 regmap_config
.val_bits
= 8;
1083 regmap_config
.reg_bits
= 16;
1084 regmap_config
.disable_locking
= true;
1085 regmap
= devm_regmap_init_i2c(client
, ®map_config
);
1087 return PTR_ERR(regmap
);
1089 nvm
->regmap
= regmap
;
1090 nvm
->client
= client
;
1092 nvmem_config
.name
= dev_name(dev
);
1093 nvmem_config
.dev
= dev
;
1094 nvmem_config
.read_only
= true;
1095 nvmem_config
.root_only
= true;
1096 nvmem_config
.owner
= THIS_MODULE
;
1097 nvmem_config
.compat
= true;
1098 nvmem_config
.base_dev
= dev
;
1099 nvmem_config
.reg_read
= ov2740_nvmem_read
;
1100 nvmem_config
.reg_write
= NULL
;
1101 nvmem_config
.priv
= nvm
;
1102 nvmem_config
.stride
= 1;
1103 nvmem_config
.word_size
= 1;
1104 nvmem_config
.size
= CUSTOMER_USE_OTP_SIZE
;
1106 nvm
->nvmem
= devm_nvmem_register(dev
, &nvmem_config
);
1108 ret
= PTR_ERR_OR_ZERO(nvm
->nvmem
);
1115 static int ov2740_probe(struct i2c_client
*client
)
1117 struct ov2740
*ov2740
;
1120 ret
= ov2740_check_hwcfg(&client
->dev
);
1122 dev_err(&client
->dev
, "failed to check HW configuration: %d",
1127 ov2740
= devm_kzalloc(&client
->dev
, sizeof(*ov2740
), GFP_KERNEL
);
1131 v4l2_i2c_subdev_init(&ov2740
->sd
, client
, &ov2740_subdev_ops
);
1132 ret
= ov2740_identify_module(ov2740
);
1134 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1138 mutex_init(&ov2740
->mutex
);
1139 ov2740
->cur_mode
= &supported_modes
[0];
1140 ret
= ov2740_init_controls(ov2740
);
1142 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1143 goto probe_error_v4l2_ctrl_handler_free
;
1146 ov2740
->sd
.internal_ops
= &ov2740_internal_ops
;
1147 ov2740
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1148 ov2740
->sd
.entity
.ops
= &ov2740_subdev_entity_ops
;
1149 ov2740
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1150 ov2740
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1151 ret
= media_entity_pads_init(&ov2740
->sd
.entity
, 1, &ov2740
->pad
);
1153 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1154 goto probe_error_v4l2_ctrl_handler_free
;
1157 ret
= v4l2_async_register_subdev_sensor_common(&ov2740
->sd
);
1159 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1161 goto probe_error_media_entity_cleanup
;
1164 ret
= ov2740_register_nvmem(client
, ov2740
);
1166 dev_warn(&client
->dev
, "register nvmem failed, ret %d\n", ret
);
1169 * Device is already turned on by i2c-core with ACPI domain PM.
1170 * Enable runtime PM and turn off the device.
1172 pm_runtime_set_active(&client
->dev
);
1173 pm_runtime_enable(&client
->dev
);
1174 pm_runtime_idle(&client
->dev
);
1178 probe_error_media_entity_cleanup
:
1179 media_entity_cleanup(&ov2740
->sd
.entity
);
1181 probe_error_v4l2_ctrl_handler_free
:
1182 v4l2_ctrl_handler_free(ov2740
->sd
.ctrl_handler
);
1183 mutex_destroy(&ov2740
->mutex
);
1188 static const struct dev_pm_ops ov2740_pm_ops
= {
1189 SET_SYSTEM_SLEEP_PM_OPS(ov2740_suspend
, ov2740_resume
)
1192 static const struct acpi_device_id ov2740_acpi_ids
[] = {
1197 MODULE_DEVICE_TABLE(acpi
, ov2740_acpi_ids
);
1199 static struct i2c_driver ov2740_i2c_driver
= {
1202 .pm
= &ov2740_pm_ops
,
1203 .acpi_match_table
= ov2740_acpi_ids
,
1205 .probe_new
= ov2740_probe
,
1206 .remove
= ov2740_remove
,
1209 module_i2c_driver(ov2740_i2c_driver
);
1211 MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>");
1212 MODULE_AUTHOR("Shawn Tu <shawnx.tu@intel.com>");
1213 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
1214 MODULE_DESCRIPTION("OmniVision OV2740 sensor driver");
1215 MODULE_LICENSE("GPL v2");