1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 MediaTek Inc.
5 #include <linux/delay.h>
6 #include <linux/device.h>
7 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/regulator/consumer.h>
12 #include <media/media-entity.h>
13 #include <media/v4l2-async.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-fwnode.h>
16 #include <media/v4l2-subdev.h>
18 #define OV02A10_ID 0x2509
19 #define OV02A10_ID_MASK GENMASK(15, 0)
21 #define OV02A10_REG_CHIP_ID 0x02
23 /* Bit[1] vertical upside down */
24 /* Bit[0] horizontal mirror */
25 #define REG_MIRROR_FLIP_CONTROL 0x3f
28 #define REG_MIRROR_FLIP_ENABLE 0x03
30 /* Bit[2:0] MIPI transmission speed select */
31 #define TX_SPEED_AREA_SEL 0xa1
32 #define OV02A10_MIPI_TX_SPEED_DEFAULT 0x04
34 #define REG_PAGE_SWITCH 0xfd
35 #define REG_GLOBAL_EFFECTIVE 0x01
36 #define REG_ENABLE BIT(0)
38 #define REG_SC_CTRL_MODE 0xac
39 #define SC_CTRL_MODE_STANDBY 0x00
40 #define SC_CTRL_MODE_STREAMING 0x01
42 /* Exposure control */
43 #define OV02A10_EXP_SHIFT 8
44 #define OV02A10_REG_EXPOSURE_H 0x03
45 #define OV02A10_REG_EXPOSURE_L 0x04
46 #define OV02A10_EXPOSURE_MIN 4
47 #define OV02A10_EXPOSURE_MAX_MARGIN 4
48 #define OV02A10_EXPOSURE_STEP 1
50 /* Vblanking control */
51 #define OV02A10_VTS_SHIFT 8
52 #define OV02A10_REG_VTS_H 0x05
53 #define OV02A10_REG_VTS_L 0x06
54 #define OV02A10_VTS_MAX 0x209f
55 #define OV02A10_BASE_LINES 1224
57 /* Analog gain control */
58 #define OV02A10_REG_GAIN 0x24
59 #define OV02A10_GAIN_MIN 0x10
60 #define OV02A10_GAIN_MAX 0xf8
61 #define OV02A10_GAIN_STEP 0x01
62 #define OV02A10_GAIN_DEFAULT 0x40
64 /* Test pattern control */
65 #define OV02A10_REG_TEST_PATTERN 0xb6
67 #define HZ_PER_MHZ 1000000L
68 #define OV02A10_LINK_FREQ_390MHZ (390 * HZ_PER_MHZ)
69 #define OV02A10_ECLK_FREQ (24 * HZ_PER_MHZ)
71 /* Number of lanes supported by this driver */
72 #define OV02A10_DATA_LANES 1
74 /* Bits per sample of sensor output */
75 #define OV02A10_BITS_PER_SAMPLE 10
77 static const char * const ov02a10_supply_names
[] = {
78 "dovdd", /* Digital I/O power */
79 "avdd", /* Analog power */
80 "dvdd", /* Digital core power */
88 struct ov02a10_reg_list
{
90 const struct ov02a10_reg
*regs
;
99 const struct ov02a10_reg_list reg_list
;
104 /* Indication of MIPI transmission speed select */
105 u32 mipi_clock_voltage
;
108 struct gpio_desc
*pd_gpio
;
109 struct gpio_desc
*rst_gpio
;
110 struct regulator_bulk_data supplies
[ARRAY_SIZE(ov02a10_supply_names
)];
116 * Serialize control access, get/set format, get selection
117 * and start streaming.
120 struct v4l2_subdev subdev
;
121 struct media_pad pad
;
122 struct v4l2_mbus_framefmt fmt
;
123 struct v4l2_ctrl_handler ctrl_handler
;
124 struct v4l2_ctrl
*exposure
;
126 const struct ov02a10_mode
*cur_mode
;
129 static inline struct ov02a10
*to_ov02a10(struct v4l2_subdev
*sd
)
131 return container_of(sd
, struct ov02a10
, subdev
);
137 * linelength 934(0x3a6)
138 * framelength 1390(0x56E)
139 * grabwindow_width 1600
140 * grabwindow_height 1200
141 * max_framerate 30fps
142 * mipi_datarate per lane 780Mbps
144 static const struct ov02a10_reg ov02a10_1600x1200_regs
[] = {
240 static const char * const ov02a10_test_pattern_menu
[] = {
242 "Eight Vertical Colour Bars",
245 static const s64 link_freq_menu_items
[] = {
246 OV02A10_LINK_FREQ_390MHZ
,
249 static u64
to_pixel_rate(u32 f_index
)
251 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OV02A10_DATA_LANES
;
253 do_div(pixel_rate
, OV02A10_BITS_PER_SAMPLE
);
258 static const struct ov02a10_mode supported_modes
[] = {
266 .num_of_regs
= ARRAY_SIZE(ov02a10_1600x1200_regs
),
267 .regs
= ov02a10_1600x1200_regs
,
272 static int ov02a10_write_array(struct ov02a10
*ov02a10
,
273 const struct ov02a10_reg_list
*r_list
)
275 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
279 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
280 ret
= i2c_smbus_write_byte_data(client
, r_list
->regs
[i
].addr
,
281 r_list
->regs
[i
].val
);
289 static void ov02a10_fill_fmt(const struct ov02a10_mode
*mode
,
290 struct v4l2_mbus_framefmt
*fmt
)
292 fmt
->width
= mode
->width
;
293 fmt
->height
= mode
->height
;
294 fmt
->field
= V4L2_FIELD_NONE
;
297 static int ov02a10_set_fmt(struct v4l2_subdev
*sd
,
298 struct v4l2_subdev_pad_config
*cfg
,
299 struct v4l2_subdev_format
*fmt
)
301 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
302 struct v4l2_mbus_framefmt
*mbus_fmt
= &fmt
->format
;
303 struct v4l2_mbus_framefmt
*frame_fmt
;
306 mutex_lock(&ov02a10
->mutex
);
308 if (ov02a10
->streaming
&& fmt
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
313 /* Only one sensor mode supported */
314 mbus_fmt
->code
= ov02a10
->fmt
.code
;
315 ov02a10_fill_fmt(ov02a10
->cur_mode
, mbus_fmt
);
317 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
318 frame_fmt
= v4l2_subdev_get_try_format(sd
, cfg
, 0);
320 frame_fmt
= &ov02a10
->fmt
;
322 *frame_fmt
= *mbus_fmt
;
325 mutex_unlock(&ov02a10
->mutex
);
329 static int ov02a10_get_fmt(struct v4l2_subdev
*sd
,
330 struct v4l2_subdev_pad_config
*cfg
,
331 struct v4l2_subdev_format
*fmt
)
333 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
334 struct v4l2_mbus_framefmt
*mbus_fmt
= &fmt
->format
;
336 mutex_lock(&ov02a10
->mutex
);
338 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
339 fmt
->format
= *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
);
341 fmt
->format
= ov02a10
->fmt
;
342 mbus_fmt
->code
= ov02a10
->fmt
.code
;
343 ov02a10_fill_fmt(ov02a10
->cur_mode
, mbus_fmt
);
346 mutex_unlock(&ov02a10
->mutex
);
351 static int ov02a10_enum_mbus_code(struct v4l2_subdev
*sd
,
352 struct v4l2_subdev_pad_config
*cfg
,
353 struct v4l2_subdev_mbus_code_enum
*code
)
355 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
357 if (code
->index
!= 0)
360 code
->code
= ov02a10
->fmt
.code
;
365 static int ov02a10_enum_frame_sizes(struct v4l2_subdev
*sd
,
366 struct v4l2_subdev_pad_config
*cfg
,
367 struct v4l2_subdev_frame_size_enum
*fse
)
369 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
372 fse
->min_width
= supported_modes
[fse
->index
].width
;
373 fse
->max_width
= supported_modes
[fse
->index
].width
;
374 fse
->max_height
= supported_modes
[fse
->index
].height
;
375 fse
->min_height
= supported_modes
[fse
->index
].height
;
380 static int ov02a10_check_sensor_id(struct ov02a10
*ov02a10
)
382 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
386 /* Validate the chip ID */
387 ret
= i2c_smbus_read_word_swapped(client
, OV02A10_REG_CHIP_ID
);
391 chip_id
= le16_to_cpu(ret
);
393 if ((chip_id
& OV02A10_ID_MASK
) != OV02A10_ID
) {
394 dev_err(&client
->dev
, "unexpected sensor id(0x%04x)\n", chip_id
);
401 static int ov02a10_power_on(struct device
*dev
)
403 struct i2c_client
*client
= to_i2c_client(dev
);
404 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
405 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
408 gpiod_set_value_cansleep(ov02a10
->rst_gpio
, 1);
409 gpiod_set_value_cansleep(ov02a10
->pd_gpio
, 1);
411 ret
= clk_prepare_enable(ov02a10
->eclk
);
413 dev_err(dev
, "failed to enable eclk\n");
417 ret
= regulator_bulk_enable(ARRAY_SIZE(ov02a10_supply_names
),
420 dev_err(dev
, "failed to enable regulators\n");
423 usleep_range(5000, 6000);
425 gpiod_set_value_cansleep(ov02a10
->pd_gpio
, 0);
426 usleep_range(5000, 6000);
428 gpiod_set_value_cansleep(ov02a10
->rst_gpio
, 0);
429 usleep_range(5000, 6000);
431 ret
= ov02a10_check_sensor_id(ov02a10
);
433 goto disable_regulator
;
438 regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names
),
441 clk_disable_unprepare(ov02a10
->eclk
);
446 static int ov02a10_power_off(struct device
*dev
)
448 struct i2c_client
*client
= to_i2c_client(dev
);
449 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
450 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
452 gpiod_set_value_cansleep(ov02a10
->rst_gpio
, 1);
453 clk_disable_unprepare(ov02a10
->eclk
);
454 gpiod_set_value_cansleep(ov02a10
->pd_gpio
, 1);
455 regulator_bulk_disable(ARRAY_SIZE(ov02a10_supply_names
),
461 static int __ov02a10_start_stream(struct ov02a10
*ov02a10
)
463 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
464 const struct ov02a10_reg_list
*reg_list
;
467 /* Apply default values of current mode */
468 reg_list
= &ov02a10
->cur_mode
->reg_list
;
469 ret
= ov02a10_write_array(ov02a10
, reg_list
);
473 /* Apply customized values from user */
474 ret
= __v4l2_ctrl_handler_setup(ov02a10
->subdev
.ctrl_handler
);
478 /* Set orientation to 180 degree */
479 if (ov02a10
->upside_down
) {
480 ret
= i2c_smbus_write_byte_data(client
, REG_MIRROR_FLIP_CONTROL
,
481 REG_MIRROR_FLIP_ENABLE
);
483 dev_err(&client
->dev
, "failed to set orientation\n");
486 ret
= i2c_smbus_write_byte_data(client
, REG_GLOBAL_EFFECTIVE
,
492 /* Set MIPI TX speed according to DT property */
493 if (ov02a10
->mipi_clock_voltage
!= OV02A10_MIPI_TX_SPEED_DEFAULT
) {
494 ret
= i2c_smbus_write_byte_data(client
, TX_SPEED_AREA_SEL
,
495 ov02a10
->mipi_clock_voltage
);
500 /* Set stream on register */
501 return i2c_smbus_write_byte_data(client
, REG_SC_CTRL_MODE
,
502 SC_CTRL_MODE_STREAMING
);
505 static int __ov02a10_stop_stream(struct ov02a10
*ov02a10
)
507 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
509 return i2c_smbus_write_byte_data(client
, REG_SC_CTRL_MODE
,
510 SC_CTRL_MODE_STANDBY
);
513 static int ov02a10_entity_init_cfg(struct v4l2_subdev
*sd
,
514 struct v4l2_subdev_pad_config
*cfg
)
516 struct v4l2_subdev_format fmt
= {
517 .which
= V4L2_SUBDEV_FORMAT_TRY
,
524 ov02a10_set_fmt(sd
, cfg
, &fmt
);
529 static int ov02a10_s_stream(struct v4l2_subdev
*sd
, int on
)
531 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
532 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
535 mutex_lock(&ov02a10
->mutex
);
537 if (ov02a10
->streaming
== on
) {
539 goto unlock_and_return
;
543 ret
= pm_runtime_get_sync(&client
->dev
);
545 pm_runtime_put_noidle(&client
->dev
);
546 goto unlock_and_return
;
549 ret
= __ov02a10_start_stream(ov02a10
);
551 __ov02a10_stop_stream(ov02a10
);
552 ov02a10
->streaming
= !on
;
556 __ov02a10_stop_stream(ov02a10
);
557 pm_runtime_put(&client
->dev
);
560 ov02a10
->streaming
= on
;
561 mutex_unlock(&ov02a10
->mutex
);
566 pm_runtime_put(&client
->dev
);
568 mutex_unlock(&ov02a10
->mutex
);
573 static const struct dev_pm_ops ov02a10_pm_ops
= {
574 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
575 pm_runtime_force_resume
)
576 SET_RUNTIME_PM_OPS(ov02a10_power_off
, ov02a10_power_on
, NULL
)
579 static int ov02a10_set_exposure(struct ov02a10
*ov02a10
, int val
)
581 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
584 ret
= i2c_smbus_write_byte_data(client
, REG_PAGE_SWITCH
, REG_ENABLE
);
588 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_EXPOSURE_H
,
589 val
>> OV02A10_EXP_SHIFT
);
593 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_EXPOSURE_L
, val
);
597 return i2c_smbus_write_byte_data(client
, REG_GLOBAL_EFFECTIVE
,
601 static int ov02a10_set_gain(struct ov02a10
*ov02a10
, int val
)
603 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
606 ret
= i2c_smbus_write_byte_data(client
, REG_PAGE_SWITCH
, REG_ENABLE
);
610 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_GAIN
, val
);
614 return i2c_smbus_write_byte_data(client
, REG_GLOBAL_EFFECTIVE
,
618 static int ov02a10_set_vblank(struct ov02a10
*ov02a10
, int val
)
620 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
621 u32 vts
= val
+ ov02a10
->cur_mode
->height
- OV02A10_BASE_LINES
;
624 ret
= i2c_smbus_write_byte_data(client
, REG_PAGE_SWITCH
, REG_ENABLE
);
628 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_VTS_H
,
629 vts
>> OV02A10_VTS_SHIFT
);
633 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_VTS_L
, vts
);
637 return i2c_smbus_write_byte_data(client
, REG_GLOBAL_EFFECTIVE
,
641 static int ov02a10_set_test_pattern(struct ov02a10
*ov02a10
, int pattern
)
643 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
646 ret
= i2c_smbus_write_byte_data(client
, REG_PAGE_SWITCH
, REG_ENABLE
);
650 ret
= i2c_smbus_write_byte_data(client
, OV02A10_REG_TEST_PATTERN
,
655 ret
= i2c_smbus_write_byte_data(client
, REG_GLOBAL_EFFECTIVE
,
660 return i2c_smbus_write_byte_data(client
, REG_SC_CTRL_MODE
,
661 SC_CTRL_MODE_STREAMING
);
664 static int ov02a10_set_ctrl(struct v4l2_ctrl
*ctrl
)
666 struct ov02a10
*ov02a10
= container_of(ctrl
->handler
,
667 struct ov02a10
, ctrl_handler
);
668 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
672 /* Propagate change of current control to all related controls */
673 if (ctrl
->id
== V4L2_CID_VBLANK
) {
674 /* Update max exposure while meeting expected vblanking */
675 max_expo
= ov02a10
->cur_mode
->height
+ ctrl
->val
-
676 OV02A10_EXPOSURE_MAX_MARGIN
;
677 __v4l2_ctrl_modify_range(ov02a10
->exposure
,
678 ov02a10
->exposure
->minimum
, max_expo
,
679 ov02a10
->exposure
->step
,
680 ov02a10
->exposure
->default_value
);
683 /* V4L2 controls values will be applied only when power is already up */
684 if (!pm_runtime_get_if_in_use(&client
->dev
))
688 case V4L2_CID_EXPOSURE
:
689 ret
= ov02a10_set_exposure(ov02a10
, ctrl
->val
);
691 case V4L2_CID_ANALOGUE_GAIN
:
692 ret
= ov02a10_set_gain(ov02a10
, ctrl
->val
);
694 case V4L2_CID_VBLANK
:
695 ret
= ov02a10_set_vblank(ov02a10
, ctrl
->val
);
697 case V4L2_CID_TEST_PATTERN
:
698 ret
= ov02a10_set_test_pattern(ov02a10
, ctrl
->val
);
705 pm_runtime_put(&client
->dev
);
710 static const struct v4l2_subdev_video_ops ov02a10_video_ops
= {
711 .s_stream
= ov02a10_s_stream
,
714 static const struct v4l2_subdev_pad_ops ov02a10_pad_ops
= {
715 .init_cfg
= ov02a10_entity_init_cfg
,
716 .enum_mbus_code
= ov02a10_enum_mbus_code
,
717 .enum_frame_size
= ov02a10_enum_frame_sizes
,
718 .get_fmt
= ov02a10_get_fmt
,
719 .set_fmt
= ov02a10_set_fmt
,
722 static const struct v4l2_subdev_ops ov02a10_subdev_ops
= {
723 .video
= &ov02a10_video_ops
,
724 .pad
= &ov02a10_pad_ops
,
727 static const struct media_entity_operations ov02a10_subdev_entity_ops
= {
728 .link_validate
= v4l2_subdev_link_validate
,
731 static const struct v4l2_ctrl_ops ov02a10_ctrl_ops
= {
732 .s_ctrl
= ov02a10_set_ctrl
,
735 static int ov02a10_initialize_controls(struct ov02a10
*ov02a10
)
737 struct i2c_client
*client
= v4l2_get_subdevdata(&ov02a10
->subdev
);
738 const struct ov02a10_mode
*mode
;
739 struct v4l2_ctrl_handler
*handler
;
740 struct v4l2_ctrl
*ctrl
;
747 handler
= &ov02a10
->ctrl_handler
;
748 mode
= ov02a10
->cur_mode
;
749 ret
= v4l2_ctrl_handler_init(handler
, 7);
753 handler
->lock
= &ov02a10
->mutex
;
755 ctrl
= v4l2_ctrl_new_int_menu(handler
, NULL
, V4L2_CID_LINK_FREQ
, 0, 0,
756 link_freq_menu_items
);
758 ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
760 pixel_rate
= to_pixel_rate(0);
761 v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_PIXEL_RATE
, 0, pixel_rate
, 1,
764 h_blank
= mode
->hts_def
- mode
->width
;
765 v4l2_ctrl_new_std(handler
, NULL
, V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
768 vblank_def
= mode
->vts_def
- mode
->height
;
769 v4l2_ctrl_new_std(handler
, &ov02a10_ctrl_ops
, V4L2_CID_VBLANK
,
770 vblank_def
, OV02A10_VTS_MAX
- mode
->height
, 1,
773 exposure_max
= mode
->vts_def
- 4;
774 ov02a10
->exposure
= v4l2_ctrl_new_std(handler
, &ov02a10_ctrl_ops
,
776 OV02A10_EXPOSURE_MIN
,
778 OV02A10_EXPOSURE_STEP
,
781 v4l2_ctrl_new_std(handler
, &ov02a10_ctrl_ops
,
782 V4L2_CID_ANALOGUE_GAIN
, OV02A10_GAIN_MIN
,
783 OV02A10_GAIN_MAX
, OV02A10_GAIN_STEP
,
784 OV02A10_GAIN_DEFAULT
);
786 v4l2_ctrl_new_std_menu_items(handler
, &ov02a10_ctrl_ops
,
787 V4L2_CID_TEST_PATTERN
,
788 ARRAY_SIZE(ov02a10_test_pattern_menu
) - 1,
789 0, 0, ov02a10_test_pattern_menu
);
791 if (handler
->error
) {
792 ret
= handler
->error
;
793 dev_err(&client
->dev
, "failed to init controls(%d)\n", ret
);
794 goto err_free_handler
;
797 ov02a10
->subdev
.ctrl_handler
= handler
;
802 v4l2_ctrl_handler_free(handler
);
807 static int ov02a10_check_hwcfg(struct device
*dev
, struct ov02a10
*ov02a10
)
809 struct fwnode_handle
*ep
;
810 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
811 struct v4l2_fwnode_endpoint bus_cfg
= {
812 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
821 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
825 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
826 fwnode_handle_put(ep
);
830 /* Optional indication of MIPI clock voltage unit */
831 ret
= fwnode_property_read_u32(ep
, "ovti,mipi-clock-voltage",
835 ov02a10
->mipi_clock_voltage
= clk_volt
;
837 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
838 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
839 if (link_freq_menu_items
[i
] ==
840 bus_cfg
.link_frequencies
[j
])
844 if (j
== bus_cfg
.nr_of_link_frequencies
) {
845 dev_err(dev
, "no link frequency %lld supported\n",
846 link_freq_menu_items
[i
]);
852 v4l2_fwnode_endpoint_free(&bus_cfg
);
857 static int ov02a10_probe(struct i2c_client
*client
)
859 struct device
*dev
= &client
->dev
;
860 struct ov02a10
*ov02a10
;
862 unsigned int rotation
;
865 ov02a10
= devm_kzalloc(dev
, sizeof(*ov02a10
), GFP_KERNEL
);
869 ret
= ov02a10_check_hwcfg(dev
, ov02a10
);
871 return dev_err_probe(dev
, ret
,
872 "failed to check HW configuration\n");
874 v4l2_i2c_subdev_init(&ov02a10
->subdev
, client
, &ov02a10_subdev_ops
);
876 ov02a10
->mipi_clock_voltage
= OV02A10_MIPI_TX_SPEED_DEFAULT
;
877 ov02a10
->fmt
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
879 /* Optional indication of physical rotation of sensor */
881 device_property_read_u32(dev
, "rotation", &rotation
);
882 if (rotation
== 180) {
883 ov02a10
->upside_down
= true;
884 ov02a10
->fmt
.code
= MEDIA_BUS_FMT_SRGGB10_1X10
;
887 ov02a10
->eclk
= devm_clk_get(dev
, "eclk");
888 if (IS_ERR(ov02a10
->eclk
))
889 return dev_err_probe(dev
, PTR_ERR(ov02a10
->eclk
),
890 "failed to get eclk\n");
892 ret
= device_property_read_u32(dev
, "clock-frequency",
893 &ov02a10
->eclk_freq
);
895 return dev_err_probe(dev
, ret
,
896 "failed to get eclk frequency\n");
898 ret
= clk_set_rate(ov02a10
->eclk
, ov02a10
->eclk_freq
);
900 return dev_err_probe(dev
, ret
,
901 "failed to set eclk frequency (24MHz)\n");
903 if (clk_get_rate(ov02a10
->eclk
) != OV02A10_ECLK_FREQ
)
904 dev_warn(dev
, "eclk mismatched, mode is based on 24MHz\n");
906 ov02a10
->pd_gpio
= devm_gpiod_get(dev
, "powerdown", GPIOD_OUT_HIGH
);
907 if (IS_ERR(ov02a10
->pd_gpio
))
908 return dev_err_probe(dev
, PTR_ERR(ov02a10
->pd_gpio
),
909 "failed to get powerdown-gpios\n");
911 ov02a10
->rst_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
912 if (IS_ERR(ov02a10
->rst_gpio
))
913 return dev_err_probe(dev
, PTR_ERR(ov02a10
->rst_gpio
),
914 "failed to get reset-gpios\n");
916 for (i
= 0; i
< ARRAY_SIZE(ov02a10_supply_names
); i
++)
917 ov02a10
->supplies
[i
].supply
= ov02a10_supply_names
[i
];
919 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(ov02a10_supply_names
),
922 return dev_err_probe(dev
, ret
, "failed to get regulators\n");
924 mutex_init(&ov02a10
->mutex
);
926 /* Set default mode */
927 ov02a10
->cur_mode
= &supported_modes
[0];
929 ret
= ov02a10_initialize_controls(ov02a10
);
931 dev_err_probe(dev
, ret
, "failed to initialize controls\n");
932 goto err_destroy_mutex
;
935 /* Initialize subdev */
936 ov02a10
->subdev
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
937 ov02a10
->subdev
.entity
.ops
= &ov02a10_subdev_entity_ops
;
938 ov02a10
->subdev
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
939 ov02a10
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
941 ret
= media_entity_pads_init(&ov02a10
->subdev
.entity
, 1, &ov02a10
->pad
);
943 dev_err_probe(dev
, ret
, "failed to initialize entity pads\n");
944 goto err_free_handler
;
947 pm_runtime_enable(dev
);
948 if (!pm_runtime_enabled(dev
)) {
949 ret
= ov02a10_power_on(dev
);
951 dev_err_probe(dev
, ret
, "failed to power on\n");
952 goto err_clean_entity
;
956 ret
= v4l2_async_register_subdev(&ov02a10
->subdev
);
958 dev_err_probe(dev
, ret
, "failed to register V4L2 subdev\n");
965 if (pm_runtime_enabled(dev
))
966 pm_runtime_disable(dev
);
968 ov02a10_power_off(dev
);
970 media_entity_cleanup(&ov02a10
->subdev
.entity
);
972 v4l2_ctrl_handler_free(ov02a10
->subdev
.ctrl_handler
);
974 mutex_destroy(&ov02a10
->mutex
);
979 static int ov02a10_remove(struct i2c_client
*client
)
981 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
982 struct ov02a10
*ov02a10
= to_ov02a10(sd
);
984 v4l2_async_unregister_subdev(sd
);
985 media_entity_cleanup(&sd
->entity
);
986 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
987 pm_runtime_disable(&client
->dev
);
988 if (!pm_runtime_status_suspended(&client
->dev
))
989 ov02a10_power_off(&client
->dev
);
990 pm_runtime_set_suspended(&client
->dev
);
991 mutex_destroy(&ov02a10
->mutex
);
996 static const struct of_device_id ov02a10_of_match
[] = {
997 { .compatible
= "ovti,ov02a10" },
1000 MODULE_DEVICE_TABLE(of
, ov02a10_of_match
);
1002 static struct i2c_driver ov02a10_i2c_driver
= {
1005 .pm
= &ov02a10_pm_ops
,
1006 .of_match_table
= ov02a10_of_match
,
1008 .probe_new
= &ov02a10_probe
,
1009 .remove
= &ov02a10_remove
,
1011 module_i2c_driver(ov02a10_i2c_driver
);
1013 MODULE_AUTHOR("Dongchun Zhu <dongchun.zhu@mediatek.com>");
1014 MODULE_DESCRIPTION("OmniVision OV02A10 sensor driver");
1015 MODULE_LICENSE("GPL v2");