Lines Matching +full:clock +full:- +full:noncontinuous

1 // SPDX-License-Identifier: GPL-2.0-only
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
68 /* Input clock rate */
104 * struct ov9282_reg - ov9282 sensor register
114 * struct ov9282_reg_list - ov9282 sensor register list
124 * struct ov9282_mode - ov9282 sensor mode structure
127 * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and
128 * continuous[1] clock modes
133 * @crop: on-sensor cropping for this mode
149 * struct ov9282 - ov9282 sensor device structure
151 * @sd: V4L2 sub-device
154 * @inclk: Sensor input clock
164 * @noncontinuous_clock: Selection of CSI2 noncontinuous clock mode
437 * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
438 * @subdev: pointer to ov9282 V4L2 sub-device
448 * ov9282_read_reg() - Read registers.
458 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd); in ov9282_read_reg()
465 return -EINVAL; in ov9282_read_reg()
470 msgs[0].addr = client->addr; in ov9282_read_reg()
476 msgs[1].addr = client->addr; in ov9282_read_reg()
479 msgs[1].buf = &data_buf[4 - len]; in ov9282_read_reg()
481 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); in ov9282_read_reg()
483 return -EIO; in ov9282_read_reg()
491 * ov9282_write_reg() - Write register
501 struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd); in ov9282_write_reg()
505 return -EINVAL; in ov9282_write_reg()
508 put_unaligned_be32(val << (8 * (4 - len)), buf + 2); in ov9282_write_reg()
510 return -EIO; in ov9282_write_reg()
516 * ov9282_write_regs() - Write a list of registers
539 * ov9282_update_controls() - Update control ranges based on streaming mode
554 ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx); in ov9282_update_controls()
558 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ? in ov9282_update_controls()
560 ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate, in ov9282_update_controls()
565 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1]; in ov9282_update_controls()
566 ret = __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min, in ov9282_update_controls()
567 OV9282_TIMING_HTS_MAX - mode->width, 1, in ov9282_update_controls()
572 return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min, in ov9282_update_controls()
573 mode->vblank_max, 1, mode->vblank); in ov9282_update_controls()
577 * ov9282_update_exp_gain() - Set updated exposure and gain
588 dev_dbg(ov9282->dev, "Set exp %u, analog gain %u", in ov9282_update_exp_gain()
642 * ov9282_set_ctrl() - Set subdevice control
646 * - V4L2_CID_VBLANK
647 * - cluster controls:
648 * - V4L2_CID_ANALOGUE_GAIN
649 * - V4L2_CID_EXPOSURE
656 container_of(ctrl->handler, struct ov9282, ctrl_handler); in ov9282_set_ctrl()
662 switch (ctrl->id) { in ov9282_set_ctrl()
664 ov9282->vblank = ov9282->vblank_ctrl->val; in ov9282_set_ctrl()
666 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u", in ov9282_set_ctrl()
667 ov9282->vblank, in ov9282_set_ctrl()
668 ov9282->vblank + ov9282->cur_mode->height); in ov9282_set_ctrl()
670 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl, in ov9282_set_ctrl()
672 ov9282->vblank + in ov9282_set_ctrl()
673 ov9282->cur_mode->height - in ov9282_set_ctrl()
680 if (!pm_runtime_get_if_in_use(ov9282->dev)) in ov9282_set_ctrl()
683 switch (ctrl->id) { in ov9282_set_ctrl()
685 exposure = ctrl->val; in ov9282_set_ctrl()
686 analog_gain = ov9282->again_ctrl->val; in ov9282_set_ctrl()
688 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u", in ov9282_set_ctrl()
694 lpfr = ov9282->vblank + ov9282->cur_mode->height; in ov9282_set_ctrl()
698 ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val); in ov9282_set_ctrl()
701 ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val); in ov9282_set_ctrl()
705 (ctrl->val + ov9282->cur_mode->width) >> 1); in ov9282_set_ctrl()
708 dev_err(ov9282->dev, "Invalid control %d", ctrl->id); in ov9282_set_ctrl()
709 ret = -EINVAL; in ov9282_set_ctrl()
712 pm_runtime_put(ov9282->dev); in ov9282_set_ctrl()
723 * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
724 * @sd: pointer to ov9282 V4L2 sub-device structure
725 * @sd_state: V4L2 sub-device configuration
726 * @code: V4L2 sub-device code enumeration need to be filled
734 switch (code->index) { in ov9282_enum_mbus_code()
736 code->code = MEDIA_BUS_FMT_Y10_1X10; in ov9282_enum_mbus_code()
739 code->code = MEDIA_BUS_FMT_Y8_1X8; in ov9282_enum_mbus_code()
742 return -EINVAL; in ov9282_enum_mbus_code()
749 * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
750 * @sd: pointer to ov9282 V4L2 sub-device structure
751 * @sd_state: V4L2 sub-device configuration
752 * @fsize: V4L2 sub-device size enumeration need to be filled
760 if (fsize->index >= ARRAY_SIZE(supported_modes)) in ov9282_enum_frame_size()
761 return -EINVAL; in ov9282_enum_frame_size()
763 if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 && in ov9282_enum_frame_size()
764 fsize->code != MEDIA_BUS_FMT_Y8_1X8) in ov9282_enum_frame_size()
765 return -EINVAL; in ov9282_enum_frame_size()
767 fsize->min_width = supported_modes[fsize->index].width; in ov9282_enum_frame_size()
768 fsize->max_width = fsize->min_width; in ov9282_enum_frame_size()
769 fsize->min_height = supported_modes[fsize->index].height; in ov9282_enum_frame_size()
770 fsize->max_height = fsize->min_height; in ov9282_enum_frame_size()
776 * ov9282_fill_pad_format() - Fill subdevice pad format
781 * @fmt: V4L2 sub-device format need to be filled
788 fmt->format.width = mode->width; in ov9282_fill_pad_format()
789 fmt->format.height = mode->height; in ov9282_fill_pad_format()
790 fmt->format.code = code; in ov9282_fill_pad_format()
791 fmt->format.field = V4L2_FIELD_NONE; in ov9282_fill_pad_format()
792 fmt->format.colorspace = V4L2_COLORSPACE_RAW; in ov9282_fill_pad_format()
793 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; in ov9282_fill_pad_format()
794 fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT; in ov9282_fill_pad_format()
795 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; in ov9282_fill_pad_format()
799 * ov9282_get_pad_format() - Get subdevice pad format
800 * @sd: pointer to ov9282 V4L2 sub-device structure
801 * @sd_state: V4L2 sub-device configuration
802 * @fmt: V4L2 sub-device format need to be set
812 mutex_lock(&ov9282->mutex); in ov9282_get_pad_format()
814 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in ov9282_get_pad_format()
817 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in ov9282_get_pad_format()
818 fmt->format = *framefmt; in ov9282_get_pad_format()
820 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code, in ov9282_get_pad_format()
824 mutex_unlock(&ov9282->mutex); in ov9282_get_pad_format()
830 * ov9282_set_pad_format() - Set subdevice pad format
831 * @sd: pointer to ov9282 V4L2 sub-device structure
832 * @sd_state: V4L2 sub-device configuration
833 * @fmt: V4L2 sub-device format need to be set
846 mutex_lock(&ov9282->mutex); in ov9282_set_pad_format()
851 fmt->format.width, in ov9282_set_pad_format()
852 fmt->format.height); in ov9282_set_pad_format()
853 if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8) in ov9282_set_pad_format()
860 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) { in ov9282_set_pad_format()
863 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad); in ov9282_set_pad_format()
864 *framefmt = fmt->format; in ov9282_set_pad_format()
868 ov9282->cur_mode = mode; in ov9282_set_pad_format()
869 ov9282->code = code; in ov9282_set_pad_format()
873 mutex_unlock(&ov9282->mutex); in ov9282_set_pad_format()
879 * ov9282_init_state() - Initialize sub-device state
880 * @sd: pointer to ov9282 V4L2 sub-device structure
881 * @sd_state: V4L2 sub-device configuration
893 ov9282->code, &fmt); in ov9282_init_state()
907 return &ov9282->cur_mode->crop; in __ov9282_get_pad_crop()
917 switch (sel->target) { in ov9282_get_selection()
921 mutex_lock(&ov9282->mutex); in ov9282_get_selection()
922 sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad, in ov9282_get_selection()
923 sel->which); in ov9282_get_selection()
924 mutex_unlock(&ov9282->mutex); in ov9282_get_selection()
930 sel->r.top = 0; in ov9282_get_selection()
931 sel->r.left = 0; in ov9282_get_selection()
932 sel->r.width = OV9282_NATIVE_WIDTH; in ov9282_get_selection()
933 sel->r.height = OV9282_NATIVE_HEIGHT; in ov9282_get_selection()
939 sel->r.top = OV9282_PIXEL_ARRAY_TOP; in ov9282_get_selection()
940 sel->r.left = OV9282_PIXEL_ARRAY_LEFT; in ov9282_get_selection()
941 sel->r.width = OV9282_PIXEL_ARRAY_WIDTH; in ov9282_get_selection()
942 sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT; in ov9282_get_selection()
947 return -EINVAL; in ov9282_get_selection()
951 * ov9282_start_streaming() - Start sensor stream
975 dev_err(ov9282->dev, "fail to write common registers"); in ov9282_start_streaming()
979 bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1; in ov9282_start_streaming()
982 dev_err(ov9282->dev, "fail to write bitdepth regs"); in ov9282_start_streaming()
987 reg_list = &ov9282->cur_mode->reg_list; in ov9282_start_streaming()
988 ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs); in ov9282_start_streaming()
990 dev_err(ov9282->dev, "fail to write initial registers"); in ov9282_start_streaming()
995 ret = __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler); in ov9282_start_streaming()
997 dev_err(ov9282->dev, "fail to setup handler"); in ov9282_start_streaming()
1005 dev_err(ov9282->dev, "fail to start streaming"); in ov9282_start_streaming()
1013 * ov9282_stop_streaming() - Stop sensor stream
1025 * ov9282_set_stream() - Enable sensor streaming
1036 mutex_lock(&ov9282->mutex); in ov9282_set_stream()
1039 ret = pm_runtime_resume_and_get(ov9282->dev); in ov9282_set_stream()
1048 pm_runtime_put(ov9282->dev); in ov9282_set_stream()
1051 mutex_unlock(&ov9282->mutex); in ov9282_set_stream()
1056 pm_runtime_put(ov9282->dev); in ov9282_set_stream()
1058 mutex_unlock(&ov9282->mutex); in ov9282_set_stream()
1064 * ov9282_detect() - Detect ov9282 sensor
1067 * Return: 0 if successful, -EIO if sensor id does not match
1079 dev_err(ov9282->dev, "chip id mismatch: %x!=%x", in ov9282_detect()
1081 return -ENXIO; in ov9282_detect()
1092 ov9282->supplies[i].supply = ov9282_supply_names[i]; in ov9282_configure_regulators()
1094 return devm_regulator_bulk_get(ov9282->dev, in ov9282_configure_regulators()
1096 ov9282->supplies); in ov9282_configure_regulators()
1100 * ov9282_parse_hw_config() - Parse HW configuration and check if supported
1107 struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev); in ov9282_parse_hw_config()
1117 return -ENXIO; in ov9282_parse_hw_config()
1120 ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset", in ov9282_parse_hw_config()
1122 if (IS_ERR(ov9282->reset_gpio)) { in ov9282_parse_hw_config()
1123 dev_err(ov9282->dev, "failed to get reset gpio %ld", in ov9282_parse_hw_config()
1124 PTR_ERR(ov9282->reset_gpio)); in ov9282_parse_hw_config()
1125 return PTR_ERR(ov9282->reset_gpio); in ov9282_parse_hw_config()
1128 /* Get sensor input clock */ in ov9282_parse_hw_config()
1129 ov9282->inclk = devm_clk_get(ov9282->dev, NULL); in ov9282_parse_hw_config()
1130 if (IS_ERR(ov9282->inclk)) { in ov9282_parse_hw_config()
1131 dev_err(ov9282->dev, "could not get inclk"); in ov9282_parse_hw_config()
1132 return PTR_ERR(ov9282->inclk); in ov9282_parse_hw_config()
1137 return dev_err_probe(ov9282->dev, ret, in ov9282_parse_hw_config()
1140 rate = clk_get_rate(ov9282->inclk); in ov9282_parse_hw_config()
1142 dev_err(ov9282->dev, "inclk frequency mismatch"); in ov9282_parse_hw_config()
1143 return -EINVAL; in ov9282_parse_hw_config()
1148 return -ENXIO; in ov9282_parse_hw_config()
1155 ov9282->noncontinuous_clock = in ov9282_parse_hw_config()
1159 dev_err(ov9282->dev, in ov9282_parse_hw_config()
1162 ret = -EINVAL; in ov9282_parse_hw_config()
1167 dev_err(ov9282->dev, "no link frequencies defined"); in ov9282_parse_hw_config()
1168 ret = -EINVAL; in ov9282_parse_hw_config()
1176 ret = -EINVAL; in ov9282_parse_hw_config()
1213 * ov9282_power_on() - Sensor power on sequence
1224 ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies); in ov9282_power_on()
1232 gpiod_set_value_cansleep(ov9282->reset_gpio, 1); in ov9282_power_on()
1234 ret = clk_prepare_enable(ov9282->inclk); in ov9282_power_on()
1236 dev_err(ov9282->dev, "fail to enable inclk"); in ov9282_power_on()
1243 ov9282->noncontinuous_clock ? in ov9282_power_on()
1246 dev_err(ov9282->dev, "fail to write MIPI_CTRL00"); in ov9282_power_on()
1253 clk_disable_unprepare(ov9282->inclk); in ov9282_power_on()
1255 gpiod_set_value_cansleep(ov9282->reset_gpio, 0); in ov9282_power_on()
1257 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies); in ov9282_power_on()
1263 * ov9282_power_off() - Sensor power off sequence
1273 gpiod_set_value_cansleep(ov9282->reset_gpio, 0); in ov9282_power_off()
1275 clk_disable_unprepare(ov9282->inclk); in ov9282_power_off()
1277 regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies); in ov9282_power_off()
1283 * ov9282_init_controls() - Initialize sensor subdevice controls
1290 struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler; in ov9282_init_controls()
1291 const struct ov9282_mode *mode = ov9282->cur_mode; in ov9282_init_controls()
1302 ctrl_hdlr->lock = &ov9282->mutex; in ov9282_init_controls()
1305 lpfr = mode->vblank + mode->height; in ov9282_init_controls()
1306 ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in ov9282_init_controls()
1310 lpfr - OV9282_EXPOSURE_OFFSET, in ov9282_init_controls()
1314 ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in ov9282_init_controls()
1322 v4l2_ctrl_cluster(2, &ov9282->exp_ctrl); in ov9282_init_controls()
1324 ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in ov9282_init_controls()
1327 mode->vblank_min, in ov9282_init_controls()
1328 mode->vblank_max, in ov9282_init_controls()
1329 1, mode->vblank); in ov9282_init_controls()
1338 ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, in ov9282_init_controls()
1344 ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr, in ov9282_init_controls()
1347 ARRAY_SIZE(link_freq) - in ov9282_init_controls()
1349 mode->link_freq_idx, in ov9282_init_controls()
1351 if (ov9282->link_freq_ctrl) in ov9282_init_controls()
1352 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov9282_init_controls()
1354 hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1]; in ov9282_init_controls()
1355 ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr, in ov9282_init_controls()
1359 OV9282_TIMING_HTS_MAX - mode->width, in ov9282_init_controls()
1362 ret = v4l2_fwnode_device_parse(ov9282->dev, &props); in ov9282_init_controls()
1364 /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */ in ov9282_init_controls()
1369 if (ctrl_hdlr->error || ret) { in ov9282_init_controls()
1370 dev_err(ov9282->dev, "control init failed: %d", in ov9282_init_controls()
1371 ctrl_hdlr->error); in ov9282_init_controls()
1373 return ctrl_hdlr->error; in ov9282_init_controls()
1376 ov9282->sd.ctrl_handler = ctrl_hdlr; in ov9282_init_controls()
1382 * ov9282_probe() - I2C client device binding
1392 ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL); in ov9282_probe()
1394 return -ENOMEM; in ov9282_probe()
1396 ov9282->dev = &client->dev; in ov9282_probe()
1399 v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops); in ov9282_probe()
1400 ov9282->sd.internal_ops = &ov9282_internal_ops; in ov9282_probe()
1401 v4l2_i2c_subdev_set_name(&ov9282->sd, client, in ov9282_probe()
1402 device_get_match_data(ov9282->dev), NULL); in ov9282_probe()
1406 dev_err(ov9282->dev, "HW configuration is not supported"); in ov9282_probe()
1410 mutex_init(&ov9282->mutex); in ov9282_probe()
1412 ret = ov9282_power_on(ov9282->dev); in ov9282_probe()
1414 dev_err(ov9282->dev, "failed to power-on the sensor"); in ov9282_probe()
1421 dev_err(ov9282->dev, "failed to find sensor: %d", ret); in ov9282_probe()
1426 ov9282->cur_mode = &supported_modes[DEFAULT_MODE]; in ov9282_probe()
1427 ov9282->code = MEDIA_BUS_FMT_Y10_1X10; in ov9282_probe()
1428 ov9282->vblank = ov9282->cur_mode->vblank; in ov9282_probe()
1432 dev_err(ov9282->dev, "failed to init controls: %d", ret); in ov9282_probe()
1437 ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in ov9282_probe()
1439 ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov9282_probe()
1442 ov9282->pad.flags = MEDIA_PAD_FL_SOURCE; in ov9282_probe()
1443 ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad); in ov9282_probe()
1445 dev_err(ov9282->dev, "failed to init entity pads: %d", ret); in ov9282_probe()
1449 ret = v4l2_async_register_subdev_sensor(&ov9282->sd); in ov9282_probe()
1451 dev_err(ov9282->dev, in ov9282_probe()
1456 pm_runtime_set_active(ov9282->dev); in ov9282_probe()
1457 pm_runtime_enable(ov9282->dev); in ov9282_probe()
1458 pm_runtime_idle(ov9282->dev); in ov9282_probe()
1463 media_entity_cleanup(&ov9282->sd.entity); in ov9282_probe()
1465 v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler); in ov9282_probe()
1467 ov9282_power_off(ov9282->dev); in ov9282_probe()
1469 mutex_destroy(&ov9282->mutex); in ov9282_probe()
1475 * ov9282_remove() - I2C client device unbinding
1486 media_entity_cleanup(&sd->entity); in ov9282_remove()
1487 v4l2_ctrl_handler_free(sd->ctrl_handler); in ov9282_remove()
1489 pm_runtime_disable(&client->dev); in ov9282_remove()
1490 if (!pm_runtime_status_suspended(&client->dev)) in ov9282_remove()
1491 ov9282_power_off(&client->dev); in ov9282_remove()
1492 pm_runtime_set_suspended(&client->dev); in ov9282_remove()
1494 mutex_destroy(&ov9282->mutex); in ov9282_remove()