Lines Matching +full:clk +full:- +full:output +full:- +full:sel
1 // SPDX-License-Identifier: GPL-2.0-only
12 #include <linux/clk.h>
23 #include <linux/v4l2-mediabus.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-fwnode.h>
30 #include <media/v4l2-subdev.h>
203 struct clk *clk; member
228 struct regmap *map = mt9v032->regmap; in mt9v032_update_aec_agc()
229 u16 value = mt9v032->aec_agc; in mt9v032_update_aec_agc()
241 mt9v032->aec_agc = value; in mt9v032_update_aec_agc()
248 struct v4l2_rect *crop = &mt9v032->crop; in mt9v032_update_hblank()
249 unsigned int min_hblank = mt9v032->model->data->min_hblank; in mt9v032_update_hblank()
252 if (mt9v032->version->version == MT9V034_CHIP_ID_REV1) in mt9v032_update_hblank()
253 min_hblank += (mt9v032->hratio - 1) * 10; in mt9v032_update_hblank()
254 min_hblank = max_t(int, mt9v032->model->data->min_row_time - crop->width, in mt9v032_update_hblank()
256 hblank = max_t(unsigned int, mt9v032->hblank, min_hblank); in mt9v032_update_hblank()
258 return regmap_write(mt9v032->regmap, MT9V032_HORIZONTAL_BLANKING, in mt9v032_update_hblank()
264 struct regmap *map = mt9v032->regmap; in mt9v032_power_on()
267 gpiod_set_value_cansleep(mt9v032->reset_gpio, 1); in mt9v032_power_on()
269 ret = clk_set_rate(mt9v032->clk, mt9v032->sysclk); in mt9v032_power_on()
274 ret = clk_prepare_enable(mt9v032->clk); in mt9v032_power_on()
280 if (mt9v032->reset_gpio) { in mt9v032_power_on()
281 gpiod_set_value_cansleep(mt9v032->reset_gpio, 0); in mt9v032_power_on()
308 clk_disable_unprepare(mt9v032->clk); in mt9v032_power_on()
314 clk_disable_unprepare(mt9v032->clk); in mt9v032_power_off()
319 struct regmap *map = mt9v032->regmap; in __mt9v032_set_power()
332 if (mt9v032->pdata && mt9v032->pdata->clk_pol) { in __mt9v032_set_power()
333 ret = regmap_write(map, mt9v032->model->data->pclk_reg, in __mt9v032_set_power()
344 return v4l2_ctrl_handler_setup(&mt9v032->ctrls); in __mt9v032_set_power()
347 /* -----------------------------------------------------------------------------
358 return v4l2_subdev_get_try_format(&mt9v032->subdev, sd_state, in __mt9v032_get_pad_format()
361 return &mt9v032->format; in __mt9v032_get_pad_format()
374 return v4l2_subdev_get_try_crop(&mt9v032->subdev, sd_state, in __mt9v032_get_pad_crop()
377 return &mt9v032->crop; in __mt9v032_get_pad_crop()
388 struct v4l2_rect *crop = &mt9v032->crop; in mt9v032_s_stream()
389 struct regmap *map = mt9v032->regmap; in mt9v032_s_stream()
398 hbin = fls(mt9v032->hratio) - 1; in mt9v032_s_stream()
399 vbin = fls(mt9v032->vratio) - 1; in mt9v032_s_stream()
407 ret = regmap_write(map, MT9V032_COLUMN_START, crop->left); in mt9v032_s_stream()
411 ret = regmap_write(map, MT9V032_ROW_START, crop->top); in mt9v032_s_stream()
415 ret = regmap_write(map, MT9V032_WINDOW_WIDTH, crop->width); in mt9v032_s_stream()
419 ret = regmap_write(map, MT9V032_WINDOW_HEIGHT, crop->height); in mt9v032_s_stream()
437 if (code->index > 0) in mt9v032_enum_mbus_code()
438 return -EINVAL; in mt9v032_enum_mbus_code()
440 code->code = mt9v032->format.code; in mt9v032_enum_mbus_code()
450 if (fse->index >= 3) in mt9v032_enum_frame_size()
451 return -EINVAL; in mt9v032_enum_frame_size()
452 if (mt9v032->format.code != fse->code) in mt9v032_enum_frame_size()
453 return -EINVAL; in mt9v032_enum_frame_size()
455 fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index); in mt9v032_enum_frame_size()
456 fse->max_width = fse->min_width; in mt9v032_enum_frame_size()
457 fse->min_height = MT9V032_WINDOW_HEIGHT_DEF / (1 << fse->index); in mt9v032_enum_frame_size()
458 fse->max_height = fse->min_height; in mt9v032_enum_frame_size()
469 format->format = *__mt9v032_get_pad_format(mt9v032, sd_state, in mt9v032_get_format()
470 format->pad, in mt9v032_get_format()
471 format->which); in mt9v032_get_format()
477 struct i2c_client *client = v4l2_get_subdevdata(&mt9v032->subdev); in mt9v032_configure_pixel_rate()
480 ret = v4l2_ctrl_s_ctrl_int64(mt9v032->pixel_rate, in mt9v032_configure_pixel_rate()
481 mt9v032->sysclk / mt9v032->hratio); in mt9v032_configure_pixel_rate()
483 dev_warn(&client->dev, "failed to set pixel rate (%d)\n", ret); in mt9v032_configure_pixel_rate()
486 static unsigned int mt9v032_calc_ratio(unsigned int input, unsigned int output) in mt9v032_calc_ratio() argument
488 /* Compute the power-of-two binning factor closest to the input size to in mt9v032_calc_ratio()
489 * output size ratio. Given that the output size is bounded by input/4 in mt9v032_calc_ratio()
492 if (output * 3 > input * 2) in mt9v032_calc_ratio()
494 if (output * 3 > input) in mt9v032_calc_ratio()
511 __crop = __mt9v032_get_pad_crop(mt9v032, sd_state, format->pad, in mt9v032_set_format()
512 format->which); in mt9v032_set_format()
515 width = clamp(ALIGN(format->format.width, 2), in mt9v032_set_format()
516 max_t(unsigned int, __crop->width / 4, in mt9v032_set_format()
518 __crop->width); in mt9v032_set_format()
519 height = clamp(ALIGN(format->format.height, 2), in mt9v032_set_format()
520 max_t(unsigned int, __crop->height / 4, in mt9v032_set_format()
522 __crop->height); in mt9v032_set_format()
524 hratio = mt9v032_calc_ratio(__crop->width, width); in mt9v032_set_format()
525 vratio = mt9v032_calc_ratio(__crop->height, height); in mt9v032_set_format()
527 __format = __mt9v032_get_pad_format(mt9v032, sd_state, format->pad, in mt9v032_set_format()
528 format->which); in mt9v032_set_format()
529 __format->width = __crop->width / hratio; in mt9v032_set_format()
530 __format->height = __crop->height / vratio; in mt9v032_set_format()
532 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v032_set_format()
533 mt9v032->hratio = hratio; in mt9v032_set_format()
534 mt9v032->vratio = vratio; in mt9v032_set_format()
538 format->format = *__format; in mt9v032_set_format()
545 struct v4l2_subdev_selection *sel) in mt9v032_get_selection() argument
549 if (sel->target != V4L2_SEL_TGT_CROP) in mt9v032_get_selection()
550 return -EINVAL; in mt9v032_get_selection()
552 sel->r = *__mt9v032_get_pad_crop(mt9v032, sd_state, sel->pad, in mt9v032_get_selection()
553 sel->which); in mt9v032_get_selection()
559 struct v4l2_subdev_selection *sel) in mt9v032_set_selection() argument
566 if (sel->target != V4L2_SEL_TGT_CROP) in mt9v032_set_selection()
567 return -EINVAL; in mt9v032_set_selection()
572 rect.left = clamp(ALIGN(sel->r.left + 1, 2) - 1, in mt9v032_set_selection()
575 rect.top = clamp(ALIGN(sel->r.top + 1, 2) - 1, in mt9v032_set_selection()
578 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), in mt9v032_set_selection()
581 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), in mt9v032_set_selection()
586 rect.width, MT9V032_PIXEL_ARRAY_WIDTH - rect.left); in mt9v032_set_selection()
588 rect.height, MT9V032_PIXEL_ARRAY_HEIGHT - rect.top); in mt9v032_set_selection()
590 __crop = __mt9v032_get_pad_crop(mt9v032, sd_state, sel->pad, in mt9v032_set_selection()
591 sel->which); in mt9v032_set_selection()
593 if (rect.width != __crop->width || rect.height != __crop->height) { in mt9v032_set_selection()
594 /* Reset the output image size if the crop rectangle size has in mt9v032_set_selection()
598 sel->pad, in mt9v032_set_selection()
599 sel->which); in mt9v032_set_selection()
600 __format->width = rect.width; in mt9v032_set_selection()
601 __format->height = rect.height; in mt9v032_set_selection()
602 if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) { in mt9v032_set_selection()
603 mt9v032->hratio = 1; in mt9v032_set_selection()
604 mt9v032->vratio = 1; in mt9v032_set_selection()
610 sel->r = rect; in mt9v032_set_selection()
615 /* -----------------------------------------------------------------------------
629 * Possible values are 0-2. 0 means no LPF. For 1 and 2 this equation is used:
631 * if |(calculated new exp - current exp)| > (current exp / 4)
634 * next exp = current exp + ((calculated new exp - current exp) / 2^LPF)
652 container_of(ctrl->handler, struct mt9v032, ctrls); in mt9v032_s_ctrl()
653 struct regmap *map = mt9v032->regmap; in mt9v032_s_ctrl()
657 switch (ctrl->id) { in mt9v032_s_ctrl()
660 ctrl->val); in mt9v032_s_ctrl()
663 return regmap_write(map, MT9V032_ANALOG_GAIN, ctrl->val); in mt9v032_s_ctrl()
667 !ctrl->val); in mt9v032_s_ctrl()
671 ctrl->val); in mt9v032_s_ctrl()
674 mt9v032->hblank = ctrl->val; in mt9v032_s_ctrl()
679 ctrl->val); in mt9v032_s_ctrl()
683 if (mt9v032->link_freq == NULL) in mt9v032_s_ctrl()
686 freq = mt9v032->pdata->link_freqs[mt9v032->link_freq->val]; in mt9v032_s_ctrl()
687 *mt9v032->pixel_rate->p_new.p_s64 = freq; in mt9v032_s_ctrl()
688 mt9v032->sysclk = freq; in mt9v032_s_ctrl()
692 switch (mt9v032->test_pattern->val) { in mt9v032_s_ctrl()
709 data = (mt9v032->test_pattern_color->val << in mt9v032_s_ctrl()
719 return regmap_write(map, MT9V032_AEGC_DESIRED_BIN, ctrl->val); in mt9v032_s_ctrl()
722 return regmap_write(map, MT9V032_AEC_LPF, ctrl->val); in mt9v032_s_ctrl()
725 return regmap_write(map, MT9V032_AGC_LPF, ctrl->val); in mt9v032_s_ctrl()
729 ctrl->val); in mt9v032_s_ctrl()
733 ctrl->val); in mt9v032_s_ctrl()
737 mt9v032->model->data->aec_max_shutter_reg, in mt9v032_s_ctrl()
738 ctrl->val); in mt9v032_s_ctrl()
846 /* -----------------------------------------------------------------------------
855 mutex_lock(&mt9v032->power_lock); in mt9v032_set_power()
860 if (mt9v032->power_count == !on) { in mt9v032_set_power()
867 mt9v032->power_count += on ? 1 : -1; in mt9v032_set_power()
868 WARN_ON(mt9v032->power_count < 0); in mt9v032_set_power()
871 mutex_unlock(&mt9v032->power_lock); in mt9v032_set_power()
875 /* -----------------------------------------------------------------------------
887 dev_info(&client->dev, "Probing MT9V032 at address 0x%02x\n", in mt9v032_registered()
888 client->addr); in mt9v032_registered()
892 dev_err(&client->dev, "MT9V032 power up failed\n"); in mt9v032_registered()
897 ret = regmap_read(mt9v032->regmap, MT9V032_CHIP_VERSION, &version); in mt9v032_registered()
902 dev_err(&client->dev, "Failed reading chip version\n"); in mt9v032_registered()
908 mt9v032->version = &mt9v032_versions[i]; in mt9v032_registered()
913 if (mt9v032->version == NULL) { in mt9v032_registered()
914 dev_err(&client->dev, "Unsupported chip version 0x%04x\n", in mt9v032_registered()
916 return -ENODEV; in mt9v032_registered()
919 dev_info(&client->dev, "%s detected at address 0x%02x\n", in mt9v032_registered()
920 mt9v032->version->name, client->addr); in mt9v032_registered()
933 crop = v4l2_subdev_get_try_crop(subdev, fh->state, 0); in mt9v032_open()
934 crop->left = MT9V032_COLUMN_START_DEF; in mt9v032_open()
935 crop->top = MT9V032_ROW_START_DEF; in mt9v032_open()
936 crop->width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_open()
937 crop->height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_open()
939 format = v4l2_subdev_get_try_format(subdev, fh->state, 0); in mt9v032_open()
941 if (mt9v032->model->color) in mt9v032_open()
942 format->code = MEDIA_BUS_FMT_SGRBG10_1X10; in mt9v032_open()
944 format->code = MEDIA_BUS_FMT_Y10_1X10; in mt9v032_open()
946 format->width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_open()
947 format->height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_open()
948 format->field = V4L2_FIELD_NONE; in mt9v032_open()
949 format->colorspace = V4L2_COLORSPACE_SRGB; in mt9v032_open()
995 /* -----------------------------------------------------------------------------
1007 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node) in mt9v032_get_pdata()
1008 return client->dev.platform_data; in mt9v032_get_pdata()
1010 np = of_graph_get_next_endpoint(client->dev.of_node, NULL); in mt9v032_get_pdata()
1017 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); in mt9v032_get_pdata()
1021 prop = of_find_property(np, "link-frequencies", NULL); in mt9v032_get_pdata()
1024 size_t size = prop->length / sizeof(*link_freqs); in mt9v032_get_pdata()
1026 link_freqs = devm_kcalloc(&client->dev, size, in mt9v032_get_pdata()
1031 if (of_property_read_u64_array(np, "link-frequencies", in mt9v032_get_pdata()
1035 pdata->link_freqs = link_freqs; in mt9v032_get_pdata()
1036 pdata->link_def_freq = link_freqs[0]; in mt9v032_get_pdata()
1039 pdata->clk_pol = !!(endpoint.bus.parallel.flags & in mt9v032_get_pdata()
1055 mt9v032 = devm_kzalloc(&client->dev, sizeof(*mt9v032), GFP_KERNEL); in mt9v032_probe()
1057 return -ENOMEM; in mt9v032_probe()
1059 mt9v032->regmap = devm_regmap_init_i2c(client, &mt9v032_regmap_config); in mt9v032_probe()
1060 if (IS_ERR(mt9v032->regmap)) in mt9v032_probe()
1061 return PTR_ERR(mt9v032->regmap); in mt9v032_probe()
1063 mt9v032->clk = devm_clk_get(&client->dev, NULL); in mt9v032_probe()
1064 if (IS_ERR(mt9v032->clk)) in mt9v032_probe()
1065 return PTR_ERR(mt9v032->clk); in mt9v032_probe()
1067 mt9v032->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", in mt9v032_probe()
1069 if (IS_ERR(mt9v032->reset_gpio)) in mt9v032_probe()
1070 return PTR_ERR(mt9v032->reset_gpio); in mt9v032_probe()
1072 mt9v032->standby_gpio = devm_gpiod_get_optional(&client->dev, "standby", in mt9v032_probe()
1074 if (IS_ERR(mt9v032->standby_gpio)) in mt9v032_probe()
1075 return PTR_ERR(mt9v032->standby_gpio); in mt9v032_probe()
1077 mutex_init(&mt9v032->power_lock); in mt9v032_probe()
1078 mt9v032->pdata = pdata; in mt9v032_probe()
1079 mt9v032->model = (const void *)did->driver_data; in mt9v032_probe()
1081 v4l2_ctrl_handler_init(&mt9v032->ctrls, 11 + in mt9v032_probe()
1084 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1086 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1089 v4l2_ctrl_new_std_menu(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1092 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1093 V4L2_CID_EXPOSURE, mt9v032->model->data->min_shutter, in mt9v032_probe()
1094 mt9v032->model->data->max_shutter, 1, in mt9v032_probe()
1096 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1097 V4L2_CID_HBLANK, mt9v032->model->data->min_hblank, in mt9v032_probe()
1100 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1101 V4L2_CID_VBLANK, mt9v032->model->data->min_vblank, in mt9v032_probe()
1102 mt9v032->model->data->max_vblank, 1, in mt9v032_probe()
1104 mt9v032->test_pattern = v4l2_ctrl_new_std_menu_items(&mt9v032->ctrls, in mt9v032_probe()
1106 ARRAY_SIZE(mt9v032_test_pattern_menu) - 1, 0, 0, in mt9v032_probe()
1108 mt9v032->test_pattern_color = v4l2_ctrl_new_custom(&mt9v032->ctrls, in mt9v032_probe()
1111 v4l2_ctrl_new_custom(&mt9v032->ctrls, in mt9v032_probe()
1112 mt9v032->model->data->aec_max_shutter_v4l2_ctrl, in mt9v032_probe()
1115 v4l2_ctrl_new_custom(&mt9v032->ctrls, &mt9v032_aegc_controls[i], in mt9v032_probe()
1118 v4l2_ctrl_cluster(2, &mt9v032->test_pattern); in mt9v032_probe()
1120 mt9v032->pixel_rate = in mt9v032_probe()
1121 v4l2_ctrl_new_std(&mt9v032->ctrls, &mt9v032_ctrl_ops, in mt9v032_probe()
1124 if (pdata && pdata->link_freqs) { in mt9v032_probe()
1127 for (i = 0; pdata->link_freqs[i]; ++i) { in mt9v032_probe()
1128 if (pdata->link_freqs[i] == pdata->link_def_freq) in mt9v032_probe()
1132 mt9v032->link_freq = in mt9v032_probe()
1133 v4l2_ctrl_new_int_menu(&mt9v032->ctrls, in mt9v032_probe()
1135 V4L2_CID_LINK_FREQ, i - 1, def, in mt9v032_probe()
1136 pdata->link_freqs); in mt9v032_probe()
1137 v4l2_ctrl_cluster(2, &mt9v032->link_freq); in mt9v032_probe()
1141 mt9v032->subdev.ctrl_handler = &mt9v032->ctrls; in mt9v032_probe()
1143 if (mt9v032->ctrls.error) { in mt9v032_probe()
1144 dev_err(&client->dev, "control initialization error %d\n", in mt9v032_probe()
1145 mt9v032->ctrls.error); in mt9v032_probe()
1146 ret = mt9v032->ctrls.error; in mt9v032_probe()
1150 mt9v032->crop.left = MT9V032_COLUMN_START_DEF; in mt9v032_probe()
1151 mt9v032->crop.top = MT9V032_ROW_START_DEF; in mt9v032_probe()
1152 mt9v032->crop.width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_probe()
1153 mt9v032->crop.height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_probe()
1155 if (mt9v032->model->color) in mt9v032_probe()
1156 mt9v032->format.code = MEDIA_BUS_FMT_SGRBG10_1X10; in mt9v032_probe()
1158 mt9v032->format.code = MEDIA_BUS_FMT_Y10_1X10; in mt9v032_probe()
1160 mt9v032->format.width = MT9V032_WINDOW_WIDTH_DEF; in mt9v032_probe()
1161 mt9v032->format.height = MT9V032_WINDOW_HEIGHT_DEF; in mt9v032_probe()
1162 mt9v032->format.field = V4L2_FIELD_NONE; in mt9v032_probe()
1163 mt9v032->format.colorspace = V4L2_COLORSPACE_SRGB; in mt9v032_probe()
1165 mt9v032->hratio = 1; in mt9v032_probe()
1166 mt9v032->vratio = 1; in mt9v032_probe()
1168 mt9v032->aec_agc = MT9V032_AEC_ENABLE | MT9V032_AGC_ENABLE; in mt9v032_probe()
1169 mt9v032->hblank = MT9V032_HORIZONTAL_BLANKING_DEF; in mt9v032_probe()
1170 mt9v032->sysclk = MT9V032_SYSCLK_FREQ_DEF; in mt9v032_probe()
1172 v4l2_i2c_subdev_init(&mt9v032->subdev, client, &mt9v032_subdev_ops); in mt9v032_probe()
1173 mt9v032->subdev.internal_ops = &mt9v032_subdev_internal_ops; in mt9v032_probe()
1174 mt9v032->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mt9v032_probe()
1176 mt9v032->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9v032_probe()
1177 mt9v032->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9v032_probe()
1178 ret = media_entity_pads_init(&mt9v032->subdev.entity, 1, &mt9v032->pad); in mt9v032_probe()
1182 mt9v032->subdev.dev = &client->dev; in mt9v032_probe()
1183 ret = v4l2_async_register_subdev(&mt9v032->subdev); in mt9v032_probe()
1190 media_entity_cleanup(&mt9v032->subdev.entity); in mt9v032_probe()
1191 v4l2_ctrl_handler_free(&mt9v032->ctrls); in mt9v032_probe()
1201 v4l2_ctrl_handler_free(&mt9v032->ctrls); in mt9v032_remove()
1202 media_entity_cleanup(&subdev->entity); in mt9v032_remove()