Lines Matching +full:sel +full:- +full:clk
1 // SPDX-License-Identifier: GPL-2.0-only
15 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
25 #include <linux/clk.h>
29 #include <linux/v4l2-mediabus.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-device.h>
36 #define REG_GAIN 0x00 /* range 00 - 3F */
51 /* [5:0]: Internal Clock Pre-Scaler */
165 #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
167 #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
197 struct clk *clk; member
254 .addr = client->addr, in ov6650_reg_read()
260 ret = i2c_transfer(client->adapter, &msg, 1); in ov6650_reg_read()
265 ret = i2c_transfer(client->adapter, &msg, 1); in ov6650_reg_read()
273 dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg); in ov6650_reg_read()
283 .addr = client->addr, in ov6650_reg_write()
289 ret = i2c_transfer(client->adapter, &msg, 1); in ov6650_reg_write()
293 dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg); in ov6650_reg_write()
308 dev_err(&client->dev, in ov6650_reg_rmw()
309 "[Read]-Modify-Write of register 0x%02x failed!\n", in ov6650_reg_rmw()
319 dev_err(&client->dev, in ov6650_reg_rmw()
320 "Read-Modify-[Write] of register 0x%02x failed!\n", in ov6650_reg_rmw()
340 struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl); in ov6550_g_volatile_ctrl()
341 struct v4l2_subdev *sd = &priv->subdev; in ov6550_g_volatile_ctrl()
346 switch (ctrl->id) { in ov6550_g_volatile_ctrl()
350 priv->gain->val = reg; in ov6550_g_volatile_ctrl()
357 priv->blue->val = reg; in ov6550_g_volatile_ctrl()
358 priv->red->val = reg2; in ov6550_g_volatile_ctrl()
364 priv->exposure->val = reg; in ov6550_g_volatile_ctrl()
367 return -EINVAL; in ov6550_g_volatile_ctrl()
373 struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl); in ov6550_s_ctrl()
374 struct v4l2_subdev *sd = &priv->subdev; in ov6550_s_ctrl()
378 switch (ctrl->id) { in ov6550_s_ctrl()
381 ctrl->val ? COMB_AGC : 0, COMB_AGC); in ov6550_s_ctrl()
382 if (!ret && !ctrl->val) in ov6550_s_ctrl()
383 ret = ov6650_reg_write(client, REG_GAIN, priv->gain->val); in ov6550_s_ctrl()
387 ctrl->val ? COMB_AWB : 0, COMB_AWB); in ov6550_s_ctrl()
388 if (!ret && !ctrl->val) { in ov6550_s_ctrl()
389 ret = ov6650_reg_write(client, REG_BLUE, priv->blue->val); in ov6550_s_ctrl()
392 priv->red->val); in ov6550_s_ctrl()
396 return ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->val), in ov6550_s_ctrl()
399 return ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->val), in ov6550_s_ctrl()
402 return ov6650_reg_write(client, REG_BRT, ctrl->val); in ov6550_s_ctrl()
404 ret = ov6650_reg_rmw(client, REG_COMB, ctrl->val == in ov6550_s_ctrl()
406 if (!ret && ctrl->val == V4L2_EXPOSURE_MANUAL) in ov6550_s_ctrl()
408 priv->exposure->val); in ov6550_s_ctrl()
411 return ov6650_reg_write(client, REG_GAM1, ctrl->val); in ov6550_s_ctrl()
414 ctrl->val ? COMB_FLIP_V : 0, COMB_FLIP_V); in ov6550_s_ctrl()
417 ctrl->val ? COMB_FLIP_H : 0, COMB_FLIP_H); in ov6550_s_ctrl()
420 return -EINVAL; in ov6550_s_ctrl()
431 if (reg->reg & ~0xff) in ov6650_get_register()
432 return -EINVAL; in ov6650_get_register()
434 reg->size = 1; in ov6650_get_register()
436 ret = ov6650_reg_read(client, reg->reg, &val); in ov6650_get_register()
438 reg->val = (__u64)val; in ov6650_get_register()
448 if (reg->reg & ~0xff || reg->val & ~0xff) in ov6650_set_register()
449 return -EINVAL; in ov6650_set_register()
451 return ov6650_reg_write(client, reg->reg, reg->val); in ov6650_set_register()
462 ret = clk_prepare_enable(priv->clk); in ov6650_s_power()
464 clk_disable_unprepare(priv->clk); in ov6650_s_power()
471 struct v4l2_subdev_selection *sel) in ov6650_get_selection() argument
477 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_get_selection()
478 /* pre-select try crop rectangle */ in ov6650_get_selection()
479 rect = &sd_state->pads->try_crop; in ov6650_get_selection()
482 /* pre-select active crop rectangle */ in ov6650_get_selection()
483 rect = &priv->rect; in ov6650_get_selection()
486 switch (sel->target) { in ov6650_get_selection()
488 sel->r.left = DEF_HSTRT << 1; in ov6650_get_selection()
489 sel->r.top = DEF_VSTRT << 1; in ov6650_get_selection()
490 sel->r.width = W_CIF; in ov6650_get_selection()
491 sel->r.height = H_CIF; in ov6650_get_selection()
496 sel->r = *rect; in ov6650_get_selection()
500 return -EINVAL; in ov6650_get_selection()
506 return width > rect->width >> 1 || height > rect->height >> 1; in is_unscaled_ok()
511 v4l_bound_align_image(&rect->width, 2, W_CIF, 1, in ov6650_bind_align_crop_rectangle()
512 &rect->height, 2, H_CIF, 1, 0); in ov6650_bind_align_crop_rectangle()
513 v4l_bound_align_image(&rect->left, DEF_HSTRT << 1, in ov6650_bind_align_crop_rectangle()
514 (DEF_HSTRT << 1) + W_CIF - (__s32)rect->width, 1, in ov6650_bind_align_crop_rectangle()
515 &rect->top, DEF_VSTRT << 1, in ov6650_bind_align_crop_rectangle()
516 (DEF_VSTRT << 1) + H_CIF - (__s32)rect->height, in ov6650_bind_align_crop_rectangle()
522 struct v4l2_subdev_selection *sel) in ov6650_set_selection() argument
528 if (sel->target != V4L2_SEL_TGT_CROP) in ov6650_set_selection()
529 return -EINVAL; in ov6650_set_selection()
531 ov6650_bind_align_crop_rectangle(&sel->r); in ov6650_set_selection()
533 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_set_selection()
534 struct v4l2_rect *crop = &sd_state->pads->try_crop; in ov6650_set_selection()
535 struct v4l2_mbus_framefmt *mf = &sd_state->pads->try_fmt; in ov6650_set_selection()
537 bool half_scale = !is_unscaled_ok(mf->width, mf->height, crop); in ov6650_set_selection()
540 *crop = sel->r; in ov6650_set_selection()
543 mf->width = crop->width >> half_scale; in ov6650_set_selection()
544 mf->height = crop->height >> half_scale; in ov6650_set_selection()
552 ret = ov6650_reg_write(client, REG_HSTRT, sel->r.left >> 1); in ov6650_set_selection()
554 priv->rect.width += priv->rect.left - sel->r.left; in ov6650_set_selection()
555 priv->rect.left = sel->r.left; in ov6650_set_selection()
557 (sel->r.left + sel->r.width) >> 1); in ov6650_set_selection()
560 priv->rect.width = sel->r.width; in ov6650_set_selection()
561 ret = ov6650_reg_write(client, REG_VSTRT, sel->r.top >> 1); in ov6650_set_selection()
564 priv->rect.height += priv->rect.top - sel->r.top; in ov6650_set_selection()
565 priv->rect.top = sel->r.top; in ov6650_set_selection()
567 (sel->r.top + sel->r.height) >> 1); in ov6650_set_selection()
570 priv->rect.height = sel->r.height; in ov6650_set_selection()
579 struct v4l2_mbus_framefmt *mf = &format->format; in ov6650_get_fmt()
583 if (format->pad) in ov6650_get_fmt()
584 return -EINVAL; in ov6650_get_fmt()
590 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_get_fmt()
591 mf->width = sd_state->pads->try_fmt.width; in ov6650_get_fmt()
592 mf->height = sd_state->pads->try_fmt.height; in ov6650_get_fmt()
593 mf->code = sd_state->pads->try_fmt.code; in ov6650_get_fmt()
596 mf->width = priv->rect.width >> priv->half_scale; in ov6650_get_fmt()
597 mf->height = priv->rect.height >> priv->half_scale; in ov6650_get_fmt()
598 mf->code = priv->code; in ov6650_get_fmt()
603 #define to_clkrc(div) ((div) - 1)
616 dev_dbg(&client->dev, "pixel format GREY8_1X8\n"); in ov6650_s_fmt()
621 dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n"); in ov6650_s_fmt()
626 dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n"); in ov6650_s_fmt()
631 dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n"); in ov6650_s_fmt()
641 dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n"); in ov6650_s_fmt()
651 dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n"); in ov6650_s_fmt()
656 dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code); in ov6650_s_fmt()
657 return -EINVAL; in ov6650_s_fmt()
670 dev_dbg(&client->dev, "max resolution: QCIF\n"); in ov6650_s_fmt()
673 dev_dbg(&client->dev, "max resolution: CIF\n"); in ov6650_s_fmt()
679 priv->half_scale = half_scale; in ov6650_s_fmt()
684 priv->code = code; in ov6650_s_fmt()
693 struct v4l2_mbus_framefmt *mf = &format->format; in ov6650_set_fmt()
699 if (format->pad) in ov6650_set_fmt()
700 return -EINVAL; in ov6650_set_fmt()
702 switch (mf->code) { in ov6650_set_fmt()
704 mf->code = MEDIA_BUS_FMT_Y8_1X8; in ov6650_set_fmt()
713 mf->code = MEDIA_BUS_FMT_SBGGR8_1X8; in ov6650_set_fmt()
719 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov6650_set_fmt()
720 crop = &sd_state->pads->try_crop; in ov6650_set_fmt()
722 crop = &priv->rect; in ov6650_set_fmt()
724 half_scale = !is_unscaled_ok(mf->width, mf->height, crop); in ov6650_set_fmt()
726 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_set_fmt()
728 sd_state->pads->try_fmt.width = crop->width >> half_scale; in ov6650_set_fmt()
729 sd_state->pads->try_fmt.height = crop->height >> half_scale; in ov6650_set_fmt()
730 sd_state->pads->try_fmt.code = mf->code; in ov6650_set_fmt()
734 mf->width = sd_state->pads->try_fmt.width; in ov6650_set_fmt()
735 mf->height = sd_state->pads->try_fmt.height; in ov6650_set_fmt()
736 mf->code = sd_state->pads->try_fmt.code; in ov6650_set_fmt()
742 if (mf->code != priv->code || half_scale != priv->half_scale) in ov6650_set_fmt()
743 ret = ov6650_s_fmt(sd, mf->code, half_scale); in ov6650_set_fmt()
749 mf->width = priv->rect.width >> priv->half_scale; in ov6650_set_fmt()
750 mf->height = priv->rect.height >> priv->half_scale; in ov6650_set_fmt()
751 mf->code = priv->code; in ov6650_set_fmt()
760 if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes)) in ov6650_enum_mbus_code()
761 return -EINVAL; in ov6650_enum_mbus_code()
763 code->code = ov6650_codes[code->index]; in ov6650_enum_mbus_code()
774 if (fie->index > CLKRC_DIV_MASK || in ov6650_enum_frame_interval()
775 GET_CLKRC_DIV(fie->index) > FRAME_RATE_MAX) in ov6650_enum_frame_interval()
776 return -EINVAL; in ov6650_enum_frame_interval()
779 if (fie->code == ov6650_codes[i]) in ov6650_enum_frame_interval()
782 return -EINVAL; in ov6650_enum_frame_interval()
784 if (!fie->width || fie->width > W_CIF || in ov6650_enum_frame_interval()
785 !fie->height || fie->height > H_CIF) in ov6650_enum_frame_interval()
786 return -EINVAL; in ov6650_enum_frame_interval()
788 fie->interval.numerator = GET_CLKRC_DIV(fie->index); in ov6650_enum_frame_interval()
789 fie->interval.denominator = FRAME_RATE_MAX; in ov6650_enum_frame_interval()
800 ival->interval = priv->tpf; in ov6650_g_frame_interval()
802 dev_dbg(&client->dev, "Frame interval: %u/%u s\n", in ov6650_g_frame_interval()
803 ival->interval.numerator, ival->interval.denominator); in ov6650_g_frame_interval()
813 struct v4l2_fract *tpf = &ival->interval; in ov6650_s_frame_interval()
816 if (tpf->numerator == 0 || tpf->denominator == 0) in ov6650_s_frame_interval()
819 div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator; in ov6650_s_frame_interval()
828 priv->tpf.numerator = div; in ov6650_s_frame_interval()
829 priv->tpf.denominator = FRAME_RATE_MAX; in ov6650_s_frame_interval()
831 *tpf = priv->tpf; in ov6650_s_frame_interval()
842 dev_dbg(&client->dev, "reset\n"); in ov6650_reset()
846 dev_err(&client->dev, in ov6650_reset()
857 dev_dbg(&client->dev, "initializing\n"); in ov6650_prog_dflt()
877 priv->clk = devm_clk_get(&client->dev, NULL); in ov6650_video_probe()
878 if (IS_ERR(priv->clk)) { in ov6650_video_probe()
879 ret = PTR_ERR(priv->clk); in ov6650_video_probe()
880 dev_err(&client->dev, "clk request err: %d\n", ret); in ov6650_video_probe()
884 rate = clk_get_rate(priv->clk); in ov6650_video_probe()
890 dev_info(&client->dev, "using host default clock rate %lukHz\n", in ov6650_video_probe()
895 ret = clk_set_rate(priv->clk, ov6650_xclk[i].rate); in ov6650_video_probe()
896 if (ret || clk_get_rate(priv->clk) != ov6650_xclk[i].rate) in ov6650_video_probe()
900 dev_info(&client->dev, "using negotiated clock rate %lukHz\n", in ov6650_video_probe()
901 xclk->rate / 1000); in ov6650_video_probe()
905 dev_err(&client->dev, "unable to get supported clock rate\n"); in ov6650_video_probe()
907 ret = -EINVAL; in ov6650_video_probe()
932 dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n", in ov6650_video_probe()
934 ret = -ENODEV; in ov6650_video_probe()
938 dev_info(&client->dev, in ov6650_video_probe()
944 ret = ov6650_prog_dflt(client, xclk->clkrc); in ov6650_video_probe()
950 ret = v4l2_ctrl_handler_setup(&priv->hdl); in ov6650_video_probe()
987 cfg->type = V4L2_MBUS_PARALLEL; in ov6650_get_mbus_config()
989 cfg->bus.parallel.flags = V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH in ov6650_get_mbus_config()
1033 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); in ov6650_probe()
1035 return -ENOMEM; in ov6650_probe()
1037 v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops); in ov6650_probe()
1038 v4l2_ctrl_handler_init(&priv->hdl, 13); in ov6650_probe()
1039 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1041 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1043 priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1045 priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1047 priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1049 priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1051 priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1053 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1055 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1057 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1059 priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl, in ov6650_probe()
1062 priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1064 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1067 priv->subdev.ctrl_handler = &priv->hdl; in ov6650_probe()
1068 if (priv->hdl.error) { in ov6650_probe()
1069 ret = priv->hdl.error; in ov6650_probe()
1073 v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true); in ov6650_probe()
1074 v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true); in ov6650_probe()
1075 v4l2_ctrl_auto_cluster(2, &priv->autoexposure, in ov6650_probe()
1078 priv->rect.left = DEF_HSTRT << 1; in ov6650_probe()
1079 priv->rect.top = DEF_VSTRT << 1; in ov6650_probe()
1080 priv->rect.width = W_CIF; in ov6650_probe()
1081 priv->rect.height = H_CIF; in ov6650_probe()
1084 priv->tpf.numerator = GET_CLKRC_DIV(DEF_CLKRC); in ov6650_probe()
1085 priv->tpf.denominator = FRAME_RATE_MAX; in ov6650_probe()
1087 priv->subdev.internal_ops = &ov6650_internal_ops; in ov6650_probe()
1089 ret = v4l2_async_register_subdev(&priv->subdev); in ov6650_probe()
1093 v4l2_ctrl_handler_free(&priv->hdl); in ov6650_probe()
1102 v4l2_async_unregister_subdev(&priv->subdev); in ov6650_remove()
1103 v4l2_ctrl_handler_free(&priv->hdl); in ov6650_remove()