Lines Matching +full:clk +full:- +full:output +full:- +full:sel

1 // SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/clk.h>
24 #include <linux/v4l2-mediabus.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-fwnode.h>
31 #include <media/v4l2-subdev.h>
204 struct clk *clk; member
229 struct regmap *map = mt9v032->regmap; in mt9v032_update_aec_agc()
230 u16 value = mt9v032->aec_agc; in mt9v032_update_aec_agc()
242 mt9v032->aec_agc = value; in mt9v032_update_aec_agc()
249 struct v4l2_rect *crop = &mt9v032->crop; in mt9v032_update_hblank()
250 unsigned int min_hblank = mt9v032->model->data->min_hblank; in mt9v032_update_hblank()
253 if (mt9v032->version->version == MT9V034_CHIP_ID_REV1) in mt9v032_update_hblank()
254 min_hblank += (mt9v032->hratio - 1) * 10; in mt9v032_update_hblank()
255 min_hblank = max_t(int, mt9v032->model->data->min_row_time - crop->width, in mt9v032_update_hblank()
257 hblank = max_t(unsigned int, mt9v032->hblank, min_hblank); in mt9v032_update_hblank()
259 return regmap_write(mt9v032->regmap, MT9V032_HORIZONTAL_BLANKING, in mt9v032_update_hblank()
265 struct regmap *map = mt9v032->regmap; in mt9v032_power_on()
268 gpiod_set_value_cansleep(mt9v032->reset_gpio, 1); in mt9v032_power_on()
270 ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk); in mt9v032_power_on()
275 ret = clk_prepare_enable(mt9v032->clk); in mt9v032_power_on()
281 if (mt9v032->reset_gpio) { in mt9v032_power_on()
282 gpiod_set_value_cansleep(mt9v032->reset_gpio, 0); in mt9v032_power_on()
309 clk_disable_unprepare(mt9v032->clk); in mt9v032_power_on()
315 clk_disable_unprepare(mt9v032->clk); in mt9v032_power_off()
320 struct regmap *map = mt9v032->regmap; in __mt9v032_set_power()
333 if (mt9v032->pdata && mt9v032->pdata->clk_pol) { in __mt9v032_set_power()
334 ret = regmap_write(map, mt9v032->model->data->pclk_reg, in __mt9v032_set_power()
345 return v4l2_ctrl_handler_setup(&mt9v032->ctrls); in __mt9v032_set_power()
348 /* -----------------------------------------------------------------------------
361 return &mt9v032->format; in __mt9v032_get_pad_format()
376 return &mt9v032->crop; in __mt9v032_get_pad_crop()
387 struct v4l2_rect *crop = &mt9v032->crop; in mt9v032_s_stream()
388 struct regmap *map = mt9v032->regmap; in mt9v032_s_stream()
397 hbin = fls(mt9v032->hratio) - 1; in mt9v032_s_stream()
398 vbin = fls(mt9v032->vratio) - 1; in mt9v032_s_stream()
406 ret = regmap_write(map, MT9V032_COLUMN_START, crop->left); in mt9v032_s_stream()
410 ret = regmap_write(map, MT9V032_ROW_START, crop->top); in mt9v032_s_stream()
414 ret = regmap_write(map, MT9V032_WINDOW_WIDTH, crop->width); in mt9v032_s_stream()
418 ret = regmap_write(map, MT9V032_WINDOW_HEIGHT, crop->height); in mt9v032_s_stream()
436 if (code->index > 0) in mt9v032_enum_mbus_code()
437 return -EINVAL; in mt9v032_enum_mbus_code()
439 code->code = mt9v032->format.code; in mt9v032_enum_mbus_code()
449 if (fse->index >= 3) in mt9v032_enum_frame_size()
450 return -EINVAL; in mt9v032_enum_frame_size()
451 if (mt9v032->format.code != fse->code) in mt9v032_enum_frame_size()
452 return -EINVAL; in mt9v032_enum_frame_size()
454 fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index); in mt9v032_enum_frame_size()
455 fse->max_width = fse->min_width; in mt9v032_enum_frame_size()
456 fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index); in mt9v032_enum_frame_size()
457 fse->max_height = fse->min_height; in mt9v032_enum_frame_size()
468 format->format = *__mt9v032_get_pad_format(mt9v032, sd_state, in mt9v032_get_format()
469 format->pad, in mt9v032_get_format()
470 format->which); in mt9v032_get_format()
476 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev); in mt9v032_configure_pixel_rate()
479 ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate, in mt9v032_configure_pixel_rate()
480 mt9v032->sysclk / mt9v032->hratio); in mt9v032_configure_pixel_rate()
482 dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret); in mt9v032_configure_pixel_rate()
485 static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output) in mt9v032_calc_ratio() argument
487 /* Compute the power-of-two binning factor closest to the input size to in mt9v032_calc_ratio()
488 * output size ratio. Given that the output size is bounded by input/4 in mt9v032_calc_ratio()
491 if (output * 3 > input * 2) in mt9v032_calc_ratio()
493 if (output * 3 > input) in mt9v032_calc_ratio()
510 __crop = __mt9v032_get_pad_crop(mt9v032, sd_state, format->pad, in mt9v032_set_format()
511 format->which); in mt9v032_set_format()
514 width = clamp(ALIGN(format->format.width, 2), in mt9v032_set_format()
515 max_t(unsigned int, __crop->width / 4, in mt9v032_set_format()
517 __crop->width); in mt9v032_set_format()
518 height = clamp(ALIGN(format->format.height, 2), in mt9v032_set_format()
519 max_t(unsigned int, __crop->height / 4, in mt9v032_set_format()
521 __crop->height); in mt9v032_set_format()
523 hratio = mt9v032_calc_ratio(__crop->width, width); in mt9v032_set_format()
524 vratio = mt9v032_calc_ratio(__crop->height, height); in mt9v032_set_format()
526 __format = __mt9v032_get_pad_format(mt9v032, sd_state, format->pad, in mt9v032_set_format()
527 format->which); in mt9v032_set_format()
528 __format->width = __crop->width / hratio; in mt9v032_set_format()
529 __format->height = __crop->height / vratio; in mt9v032_set_format()
531 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v032_set_format()
532 mt9v032->hratio = hratio; in mt9v032_set_format()
533 mt9v032->vratio = vratio; in mt9v032_set_format()
537 format->format = *__format; in mt9v032_set_format()
544 struct v4l2_subdev_selection *sel) in mt9v032_get_selection() argument
548 if (sel->target != V4L2_SEL_TGT_CROP) in mt9v032_get_selection()
549 return -EINVAL; in mt9v032_get_selection()
551 sel->r = *__mt9v032_get_pad_crop(mt9v032, sd_state, sel->pad, in mt9v032_get_selection()
552 sel->which); in mt9v032_get_selection()
558 struct v4l2_subdev_selection *sel) in mt9v032_set_selection() argument
565 if (sel->target != V4L2_SEL_TGT_CROP) in mt9v032_set_selection()
566 return -EINVAL; in mt9v032_set_selection()
571 rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1, in mt9v032_set_selection()
574 rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1, in mt9v032_set_selection()
577 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), in mt9v032_set_selection()
580 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), in mt9v032_set_selection()
585 rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left); in mt9v032_set_selection()
587 rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top); in mt9v032_set_selection()
589 __crop = __mt9v032_get_pad_crop(mt9v032, sd_state, sel->pad, in mt9v032_set_selection()
590 sel->which); in mt9v032_set_selection()
592 if (rect.width != __crop->width || rect.height != __crop->height) { in mt9v032_set_selection()
593 /* Reset the output image size if the crop rectangle size has in mt9v032_set_selection()
597 sel->pad, in mt9v032_set_selection()
598 sel->which); in mt9v032_set_selection()
599 __format->width = rect.width; in mt9v032_set_selection()
600 __format->height = rect.height; in mt9v032_set_selection()
601 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v032_set_selection()
602 mt9v032->hratio = 1; in mt9v032_set_selection()
603 mt9v032->vratio = 1; in mt9v032_set_selection()
609 sel->r = rect; in mt9v032_set_selection()
614 /* -----------------------------------------------------------------------------
628 * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
630 * if |(calculated new exp - current exp)| > (current exp / 4)
633 * next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
651 container_of(ctrl->handler, struct mt9v032, ctrls); in mt9v032_s_ctrl()
652 struct regmap *map = mt9v032->regmap; in mt9v032_s_ctrl()
656 switch (ctrl->id) { in mt9v032_s_ctrl()
659 ctrl->val); in mt9v032_s_ctrl()
662 return regmap_write(map, MT9V032_ANALOG_GAIN, ctrl->val); in mt9v032_s_ctrl()
666 !ctrl->val); in mt9v032_s_ctrl()
670 ctrl->val); in mt9v032_s_ctrl()
673 mt9v032->hblank = ctrl->val; in mt9v032_s_ctrl()
678 ctrl->val); in mt9v032_s_ctrl()
682 if (mt9v032->link_freq == NULL) in mt9v032_s_ctrl()
685 freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val]; in mt9v032_s_ctrl()
686 *mt9v032->pixel_rate->p_new.p_s64 = freq; in mt9v032_s_ctrl()
687 mt9v032->sysclk = freq; in mt9v032_s_ctrl()
691 switch (mt9v032->test_pattern->val) { in mt9v032_s_ctrl()
708 data = (mt9v032->test_pattern_color->val << in mt9v032_s_ctrl()
718 return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val); in mt9v032_s_ctrl()
721 return regmap_write(map, MT9V032_AEC_LPF, ctrl->val); in mt9v032_s_ctrl()
724 return regmap_write(map, MT9V032_AGC_LPF, ctrl->val); in mt9v032_s_ctrl()
728 ctrl->val); in mt9v032_s_ctrl()
732 ctrl->val); in mt9v032_s_ctrl()
736 mt9v032->model->data->aec_max_shutter_reg, in mt9v032_s_ctrl()
737 ctrl->val); in mt9v032_s_ctrl()
845 /* -----------------------------------------------------------------------------
854 mutex_lock(&mt9v032->power_lock); in mt9v032_set_power()
859 if (mt9v032->power_count == !on) { in mt9v032_set_power()
866 mt9v032->power_count += on ? 1 : -1; in mt9v032_set_power()
867 WARN_ON(mt9v032->power_count < 0); in mt9v032_set_power()
870 mutex_unlock(&mt9v032->power_lock); in mt9v032_set_power()
874 /* -----------------------------------------------------------------------------
886 dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n", in mt9v032_registered()
887 client->addr); in mt9v032_registered()
891 dev_err(&client->dev, "MT9V032 power up failed\n"); in mt9v032_registered()
896 ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version); in mt9v032_registered()
901 dev_err(&client->dev, "Failed reading chip version\n"); in mt9v032_registered()
907 mt9v032->version = &mt9v032_versions[i]; in mt9v032_registered()
912 if (mt9v032->version == NULL) { in mt9v032_registered()
913 dev_err(&client->dev, "Unsupported chip version 0x%04x\n", in mt9v032_registered()
915 return -ENODEV; in mt9v032_registered()
918 dev_info(&client->dev, "%s detected at address 0x%02x\n", in mt9v032_registered()
919 mt9v032->version->name, client->addr); in mt9v032_registered()
932 crop = v4l2_subdev_state_get_crop(fh->state, 0); in mt9v032_open()
933 crop->left = MT9V032_COLUMN_START_DEF; in mt9v032_open()
934 crop->top = MT9V032_ROW_START_DEF; in mt9v032_open()
935 crop->width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_open()
936 crop->height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_open()
938 format = v4l2_subdev_state_get_format(fh->state, 0); in mt9v032_open()
940 if (mt9v032->model->color) in mt9v032_open()
941 format->code = MEDIA_BUS_FMT_SGRBG10_1X10; in mt9v032_open()
943 format->code = MEDIA_BUS_FMT_Y10_1X10; in mt9v032_open()
945 format->width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_open()
946 format->height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_open()
947 format->field = V4L2_FIELD_NONE; in mt9v032_open()
948 format->colorspace = V4L2_COLORSPACE_SRGB; in mt9v032_open()
994 /* -----------------------------------------------------------------------------
1006 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node) in mt9v032_get_pdata()
1007 return client->dev.platform_data; in mt9v032_get_pdata()
1009 np = of_graph_get_endpoint_by_regs(client->dev.of_node, 0, -1); in mt9v032_get_pdata()
1016 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); in mt9v032_get_pdata()
1020 prop = of_find_property(np, "link-frequencies", NULL); in mt9v032_get_pdata()
1023 size_t size = prop->length / sizeof(*link_freqs); in mt9v032_get_pdata()
1025 link_freqs = devm_kcalloc(&client->dev, size, in mt9v032_get_pdata()
1030 if (of_property_read_u64_array(np, "link-frequencies", in mt9v032_get_pdata()
1034 pdata->link_freqs = link_freqs; in mt9v032_get_pdata()
1035 pdata->link_def_freq = link_freqs[0]; in mt9v032_get_pdata()
1038 pdata->clk_pol = !!(endpoint.bus.parallel.flags & in mt9v032_get_pdata()
1053 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL); in mt9v032_probe()
1055 return -ENOMEM; in mt9v032_probe()
1057 mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config); in mt9v032_probe()
1058 if (IS_ERR(mt9v032->regmap)) in mt9v032_probe()
1059 return PTR_ERR(mt9v032->regmap); in mt9v032_probe()
1061 mt9v032->clk = devm_clk_get(&client->dev, NULL); in mt9v032_probe()
1062 if (IS_ERR(mt9v032->clk)) in mt9v032_probe()
1063 return PTR_ERR(mt9v032->clk); in mt9v032_probe()
1065 mt9v032->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", in mt9v032_probe()
1067 if (IS_ERR(mt9v032->reset_gpio)) in mt9v032_probe()
1068 return PTR_ERR(mt9v032->reset_gpio); in mt9v032_probe()
1070 mt9v032->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby", in mt9v032_probe()
1072 if (IS_ERR(mt9v032->standby_gpio)) in mt9v032_probe()
1073 return PTR_ERR(mt9v032->standby_gpio); in mt9v032_probe()
1075 mutex_init(&mt9v032->power_lock); in mt9v032_probe()
1076 mt9v032->pdata = pdata; in mt9v032_probe()
1077 mt9v032->model = i2c_get_match_data(client); in mt9v032_probe()
1079 v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 + in mt9v032_probe()
1082 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1084 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1087 v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1090 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1091 V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter, in mt9v032_probe()
1092 mt9v032->model->data->max_shutter, 1, in mt9v032_probe()
1094 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1095 V4L2_CID_HBLANK, mt9v032->model->data->min_hblank, in mt9v032_probe()
1098 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1099 V4L2_CID_VBLANK, mt9v032->model->data->min_vblank, in mt9v032_probe()
1100 mt9v032->model->data->max_vblank, 1, in mt9v032_probe()
1102 mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls, in mt9v032_probe()
1104 ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0, in mt9v032_probe()
1106 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls, in mt9v032_probe()
1109 v4l2_ctrl_new_custom(&mt9v032->ctrls, in mt9v032_probe()
1110 mt9v032->model->data->aec_max_shutter_v4l2_ctrl, in mt9v032_probe()
1113 v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i], in mt9v032_probe()
1116 v4l2_ctrl_cluster(2, &mt9v032->test_pattern); in mt9v032_probe()
1118 mt9v032->pixel_rate = in mt9v032_probe()
1119 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1122 if (pdata && pdata->link_freqs) { in mt9v032_probe()
1125 for (i = 0; pdata->link_freqs[i]; ++i) { in mt9v032_probe()
1126 if (pdata->link_freqs[i] == pdata->link_def_freq) in mt9v032_probe()
1130 mt9v032->link_freq = in mt9v032_probe()
1131 v4l2_ctrl_new_int_menu(&mt9v032->ctrls, in mt9v032_probe()
1133 V4L2_CID_LINK_FREQ, i - 1, def, in mt9v032_probe()
1134 pdata->link_freqs); in mt9v032_probe()
1135 v4l2_ctrl_cluster(2, &mt9v032->link_freq); in mt9v032_probe()
1139 mt9v032->subdev.ctrl_handler = &mt9v032->ctrls; in mt9v032_probe()
1141 if (mt9v032->ctrls.error) { in mt9v032_probe()
1142 dev_err(&client->dev, "control initialization error %d\n", in mt9v032_probe()
1143 mt9v032->ctrls.error); in mt9v032_probe()
1144 ret = mt9v032->ctrls.error; in mt9v032_probe()
1148 mt9v032->crop.left = MT9V032_COLUMN_START_DEF; in mt9v032_probe()
1149 mt9v032->crop.top = MT9V032_ROW_START_DEF; in mt9v032_probe()
1150 mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_probe()
1151 mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_probe()
1153 if (mt9v032->model->color) in mt9v032_probe()
1154 mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10; in mt9v032_probe()
1156 mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10; in mt9v032_probe()
1158 mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_probe()
1159 mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_probe()
1160 mt9v032->format.field = V4L2_FIELD_NONE; in mt9v032_probe()
1161 mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB; in mt9v032_probe()
1163 mt9v032->hratio = 1; in mt9v032_probe()
1164 mt9v032->vratio = 1; in mt9v032_probe()
1166 mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE; in mt9v032_probe()
1167 mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF; in mt9v032_probe()
1168 mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF; in mt9v032_probe()
1170 v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops); in mt9v032_probe()
1171 mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops; in mt9v032_probe()
1172 mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mt9v032_probe()
1174 mt9v032->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9v032_probe()
1175 mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9v032_probe()
1176 ret = media_entity_pads_init(&mt9v032->subdev.entity, 1, &mt9v032->pad); in mt9v032_probe()
1180 mt9v032->subdev.dev = &client->dev; in mt9v032_probe()
1181 ret = v4l2_async_register_subdev(&mt9v032->subdev); in mt9v032_probe()
1188 media_entity_cleanup(&mt9v032->subdev.entity); in mt9v032_probe()
1189 v4l2_ctrl_handler_free(&mt9v032->ctrls); in mt9v032_probe()
1199 v4l2_ctrl_handler_free(&mt9v032->ctrls); in mt9v032_remove()
1200 media_entity_cleanup(&subdev->entity); in mt9v032_remove()