Lines Matching +full:gain +full:- +full:scaling +full:- +full:n
1 // SPDX-License-Identifier: GPL-2.0-only
15 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
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)
187 /* gain/autogain cluster */
189 struct v4l2_ctrl *gain; 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()
477 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_get_selection()
478 /* pre-select try crop rectangle */ 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()
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()
538 /* detect current pad config scaling factor */ in ov6650_set_selection()
539 bool half_scale = !is_unscaled_ok(mf->width, mf->height, crop); in ov6650_set_selection()
542 *crop = sel->r; in ov6650_set_selection()
545 mf->width = crop->width >> half_scale; in ov6650_set_selection()
546 mf->height = crop->height >> half_scale; in ov6650_set_selection()
554 ret = ov6650_reg_write(client, REG_HSTRT, sel->r.left >> 1); in ov6650_set_selection()
556 priv->rect.width += priv->rect.left - sel->r.left; in ov6650_set_selection()
557 priv->rect.left = sel->r.left; in ov6650_set_selection()
559 (sel->r.left + sel->r.width) >> 1); in ov6650_set_selection()
562 priv->rect.width = sel->r.width; in ov6650_set_selection()
563 ret = ov6650_reg_write(client, REG_VSTRT, sel->r.top >> 1); in ov6650_set_selection()
566 priv->rect.height += priv->rect.top - sel->r.top; in ov6650_set_selection()
567 priv->rect.top = sel->r.top; in ov6650_set_selection()
569 (sel->r.top + sel->r.height) >> 1); in ov6650_set_selection()
572 priv->rect.height = sel->r.height; in ov6650_set_selection()
581 struct v4l2_mbus_framefmt *mf = &format->format; in ov6650_get_fmt()
585 if (format->pad) in ov6650_get_fmt()
586 return -EINVAL; in ov6650_get_fmt()
592 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_get_fmt()
596 mf->width = try_fmt->width; in ov6650_get_fmt()
597 mf->height = try_fmt->height; in ov6650_get_fmt()
598 mf->code = try_fmt->code; in ov6650_get_fmt()
601 mf->width = priv->rect.width >> priv->half_scale; in ov6650_get_fmt()
602 mf->height = priv->rect.height >> priv->half_scale; in ov6650_get_fmt()
603 mf->code = priv->code; in ov6650_get_fmt()
608 #define to_clkrc(div) ((div) - 1)
621 dev_dbg(&client->dev, "pixel format GREY8_1X8\n"); in ov6650_s_fmt()
626 dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n"); in ov6650_s_fmt()
631 dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n"); in ov6650_s_fmt()
636 dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n"); in ov6650_s_fmt()
646 dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n"); in ov6650_s_fmt()
656 dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n"); in ov6650_s_fmt()
661 dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code); in ov6650_s_fmt()
662 return -EINVAL; in ov6650_s_fmt()
675 dev_dbg(&client->dev, "max resolution: QCIF\n"); in ov6650_s_fmt()
678 dev_dbg(&client->dev, "max resolution: CIF\n"); in ov6650_s_fmt()
684 priv->half_scale = half_scale; in ov6650_s_fmt()
689 priv->code = code; in ov6650_s_fmt()
698 struct v4l2_mbus_framefmt *mf = &format->format; in ov6650_set_fmt()
704 if (format->pad) in ov6650_set_fmt()
705 return -EINVAL; in ov6650_set_fmt()
707 switch (mf->code) { in ov6650_set_fmt()
709 mf->code = MEDIA_BUS_FMT_Y8_1X8; in ov6650_set_fmt()
718 mf->code = MEDIA_BUS_FMT_SBGGR8_1X8; in ov6650_set_fmt()
724 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov6650_set_fmt()
727 crop = &priv->rect; in ov6650_set_fmt()
729 half_scale = !is_unscaled_ok(mf->width, mf->height, crop); in ov6650_set_fmt()
731 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { in ov6650_set_fmt()
736 try_fmt->width = crop->width >> half_scale; in ov6650_set_fmt()
737 try_fmt->height = crop->height >> half_scale; in ov6650_set_fmt()
738 try_fmt->code = mf->code; in ov6650_set_fmt()
742 mf->width = try_fmt->width; in ov6650_set_fmt()
743 mf->height = try_fmt->height; in ov6650_set_fmt()
744 mf->code = try_fmt->code; in ov6650_set_fmt()
749 /* apply new media bus frame format and scaling if changed */ in ov6650_set_fmt()
750 if (mf->code != priv->code || half_scale != priv->half_scale) in ov6650_set_fmt()
751 ret = ov6650_s_fmt(sd, mf->code, half_scale); in ov6650_set_fmt()
757 mf->width = priv->rect.width >> priv->half_scale; in ov6650_set_fmt()
758 mf->height = priv->rect.height >> priv->half_scale; in ov6650_set_fmt()
759 mf->code = priv->code; in ov6650_set_fmt()
768 if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes)) in ov6650_enum_mbus_code()
769 return -EINVAL; in ov6650_enum_mbus_code()
771 code->code = ov6650_codes[code->index]; in ov6650_enum_mbus_code()
782 if (fie->index > CLKRC_DIV_MASK || in ov6650_enum_frame_interval()
783 GET_CLKRC_DIV(fie->index) > FRAME_RATE_MAX) in ov6650_enum_frame_interval()
784 return -EINVAL; in ov6650_enum_frame_interval()
787 if (fie->code == ov6650_codes[i]) in ov6650_enum_frame_interval()
790 return -EINVAL; in ov6650_enum_frame_interval()
792 if (!fie->width || fie->width > W_CIF || in ov6650_enum_frame_interval()
793 !fie->height || fie->height > H_CIF) in ov6650_enum_frame_interval()
794 return -EINVAL; in ov6650_enum_frame_interval()
796 fie->interval.numerator = GET_CLKRC_DIV(fie->index); in ov6650_enum_frame_interval()
797 fie->interval.denominator = FRAME_RATE_MAX; in ov6650_enum_frame_interval()
813 if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE) in ov6650_get_frame_interval()
814 return -EINVAL; in ov6650_get_frame_interval()
816 ival->interval = priv->tpf; in ov6650_get_frame_interval()
818 dev_dbg(&client->dev, "Frame interval: %u/%u s\n", in ov6650_get_frame_interval()
819 ival->interval.numerator, ival->interval.denominator); in ov6650_get_frame_interval()
830 struct v4l2_fract *tpf = &ival->interval; in ov6650_set_frame_interval()
837 if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE) in ov6650_set_frame_interval()
838 return -EINVAL; in ov6650_set_frame_interval()
840 if (tpf->numerator == 0 || tpf->denominator == 0) in ov6650_set_frame_interval()
843 div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator; in ov6650_set_frame_interval()
852 priv->tpf.numerator = div; in ov6650_set_frame_interval()
853 priv->tpf.denominator = FRAME_RATE_MAX; in ov6650_set_frame_interval()
855 *tpf = priv->tpf; in ov6650_set_frame_interval()
866 dev_dbg(&client->dev, "reset\n"); in ov6650_reset()
870 dev_err(&client->dev, in ov6650_reset()
871 "An error occurred while entering soft reset!\n"); in ov6650_reset()
881 dev_dbg(&client->dev, "initializing\n"); in ov6650_prog_dflt()
901 priv->clk = devm_clk_get(&client->dev, NULL); in ov6650_video_probe()
902 if (IS_ERR(priv->clk)) { in ov6650_video_probe()
903 ret = PTR_ERR(priv->clk); in ov6650_video_probe()
904 dev_err(&client->dev, "clk request err: %d\n", ret); in ov6650_video_probe()
908 rate = clk_get_rate(priv->clk); in ov6650_video_probe()
914 dev_info(&client->dev, "using host default clock rate %lukHz\n", in ov6650_video_probe()
919 ret = clk_set_rate(priv->clk, ov6650_xclk[i].rate); in ov6650_video_probe()
920 if (ret || clk_get_rate(priv->clk) != ov6650_xclk[i].rate) in ov6650_video_probe()
924 dev_info(&client->dev, "using negotiated clock rate %lukHz\n", in ov6650_video_probe()
925 xclk->rate / 1000); in ov6650_video_probe()
929 dev_err(&client->dev, "unable to get supported clock rate\n"); in ov6650_video_probe()
931 ret = -EINVAL; in ov6650_video_probe()
956 dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n", in ov6650_video_probe()
958 ret = -ENODEV; in ov6650_video_probe()
962 dev_info(&client->dev, in ov6650_video_probe()
963 "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n", in ov6650_video_probe()
968 ret = ov6650_prog_dflt(client, xclk->clkrc); in ov6650_video_probe()
970 /* driver default frame format, no scaling */ in ov6650_video_probe()
974 ret = v4l2_ctrl_handler_setup(&priv->hdl); in ov6650_video_probe()
1011 cfg->type = V4L2_MBUS_PARALLEL; in ov6650_get_mbus_config()
1013 cfg->bus.parallel.flags = V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH in ov6650_get_mbus_config()
1057 priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); in ov6650_probe()
1059 return -ENOMEM; in ov6650_probe()
1061 v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops); in ov6650_probe()
1062 v4l2_ctrl_handler_init(&priv->hdl, 13); in ov6650_probe()
1063 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1065 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1067 priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1069 priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1071 priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1073 priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1075 priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1077 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1079 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1081 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1083 priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl, in ov6650_probe()
1086 priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1088 v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops, in ov6650_probe()
1091 priv->subdev.ctrl_handler = &priv->hdl; in ov6650_probe()
1092 if (priv->hdl.error) { in ov6650_probe()
1093 ret = priv->hdl.error; in ov6650_probe()
1097 v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true); in ov6650_probe()
1098 v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true); in ov6650_probe()
1099 v4l2_ctrl_auto_cluster(2, &priv->autoexposure, in ov6650_probe()
1102 priv->rect.left = DEF_HSTRT << 1; in ov6650_probe()
1103 priv->rect.top = DEF_VSTRT << 1; in ov6650_probe()
1104 priv->rect.width = W_CIF; in ov6650_probe()
1105 priv->rect.height = H_CIF; in ov6650_probe()
1108 priv->tpf.numerator = GET_CLKRC_DIV(DEF_CLKRC); in ov6650_probe()
1109 priv->tpf.denominator = FRAME_RATE_MAX; in ov6650_probe()
1111 priv->subdev.internal_ops = &ov6650_internal_ops; in ov6650_probe()
1113 ret = v4l2_async_register_subdev(&priv->subdev); in ov6650_probe()
1117 v4l2_ctrl_handler_free(&priv->hdl); in ov6650_probe()
1126 v4l2_async_unregister_subdev(&priv->subdev); in ov6650_remove()
1127 v4l2_ctrl_handler_free(&priv->hdl); in ov6650_remove()