Lines Matching +full:vaa +full:- +full:supply

1 // SPDX-License-Identifier: GPL-2.0-only
6 * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
27 #include <media/v4l2-async.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>
33 #include "aptina-pll.h"
165 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_output_control()
166 u16 value = (mt9p031->output_control & ~clear) | set; in mt9p031_set_output_control()
173 mt9p031->output_control = value; in mt9p031_set_output_control()
179 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_mode2()
180 u16 value = (mt9p031->mode2 & ~clear) | set; in mt9p031_set_mode2()
187 mt9p031->mode2 = value; in mt9p031_set_mode2()
193 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_reset()
205 MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div)); in mt9p031_reset()
231 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_clk_setup()
232 struct mt9p031_platform_data *pdata = mt9p031->pdata; in mt9p031_clk_setup()
236 mt9p031->clk = devm_clk_get(&client->dev, NULL); in mt9p031_clk_setup()
237 if (IS_ERR(mt9p031->clk)) in mt9p031_clk_setup()
238 return PTR_ERR(mt9p031->clk); in mt9p031_clk_setup()
240 ret = clk_set_rate(mt9p031->clk, pdata->ext_freq); in mt9p031_clk_setup()
244 ext_freq = clk_get_rate(mt9p031->clk); in mt9p031_clk_setup()
252 div = DIV_ROUND_UP(ext_freq, pdata->target_freq); in mt9p031_clk_setup()
255 mt9p031->clk_div = min_t(unsigned int, div, 64); in mt9p031_clk_setup()
256 mt9p031->use_pll = false; in mt9p031_clk_setup()
261 mt9p031->pll.ext_clock = ext_freq; in mt9p031_clk_setup()
262 mt9p031->pll.pix_clock = pdata->target_freq; in mt9p031_clk_setup()
263 mt9p031->use_pll = true; in mt9p031_clk_setup()
265 return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll); in mt9p031_clk_setup()
270 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_enable()
273 if (!mt9p031->use_pll) in mt9p031_pll_enable()
282 (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1)); in mt9p031_pll_enable()
286 ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1); in mt9p031_pll_enable()
299 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_pll_disable()
301 if (!mt9p031->use_pll) in mt9p031_pll_disable()
314 if (mt9p031->reset) { in mt9p031_power_on()
315 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_on()
320 ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
321 mt9p031->regulators); in mt9p031_power_on()
326 if (mt9p031->clk) { in mt9p031_power_on()
327 ret = clk_prepare_enable(mt9p031->clk); in mt9p031_power_on()
329 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_on()
330 mt9p031->regulators); in mt9p031_power_on()
336 if (mt9p031->reset) { in mt9p031_power_on()
337 gpiod_set_value(mt9p031->reset, 0); in mt9p031_power_on()
338 /* Wait 850000 EXTCLK cycles before de-asserting reset. */ in mt9p031_power_on()
339 rate = clk_get_rate(mt9p031->clk); in mt9p031_power_on()
351 if (mt9p031->reset) { in mt9p031_power_off()
352 gpiod_set_value(mt9p031->reset, 1); in mt9p031_power_off()
356 regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), in mt9p031_power_off()
357 mt9p031->regulators); in mt9p031_power_off()
359 clk_disable_unprepare(mt9p031->clk); in mt9p031_power_off()
364 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in __mt9p031_set_power()
378 dev_err(&client->dev, "Failed to reset the camera\n"); in __mt9p031_set_power()
383 if (mt9p031->pdata && mt9p031->pdata->pixclk_pol) { in __mt9p031_set_power()
390 return v4l2_ctrl_handler_setup(&mt9p031->ctrls); in __mt9p031_set_power()
393 /* -----------------------------------------------------------------------------
399 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_set_params()
400 struct v4l2_mbus_framefmt *format = &mt9p031->format; in mt9p031_set_params()
401 const struct v4l2_rect *crop = &mt9p031->crop; in mt9p031_set_params()
416 ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left); in mt9p031_set_params()
419 ret = mt9p031_write(client, MT9P031_ROW_START, crop->top); in mt9p031_set_params()
422 ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1); in mt9p031_set_params()
425 ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1); in mt9p031_set_params()
432 xskip = DIV_ROUND_CLOSEST(crop->width, format->width); in mt9p031_set_params()
433 yskip = DIV_ROUND_CLOSEST(crop->height, format->height); in mt9p031_set_params()
434 xbin = 1 << (ffs(xskip) - 1); in mt9p031_set_params()
435 ybin = 1 << (ffs(yskip) - 1); in mt9p031_set_params()
438 ((xbin - 1) << 4) | (xskip - 1)); in mt9p031_set_params()
442 ((ybin - 1) << 4) | (yskip - 1)); in mt9p031_set_params()
446 /* Blanking - use minimum value for horizontal blanking and default in mt9p031_set_params()
452 ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1); in mt9p031_set_params()
455 ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1); in mt9p031_set_params()
502 * - clear pause restart in mt9p031_s_stream()
503 * - don't clear restart as clearing restart manually can cause in mt9p031_s_stream()
520 if (code->pad || code->index) in mt9p031_enum_mbus_code()
521 return -EINVAL; in mt9p031_enum_mbus_code()
523 code->code = mt9p031->format.code; in mt9p031_enum_mbus_code()
533 if (fse->index >= 8 || fse->code != mt9p031->format.code) in mt9p031_enum_frame_size()
534 return -EINVAL; in mt9p031_enum_frame_size()
536 fse->min_width = MT9P031_WINDOW_WIDTH_DEF in mt9p031_enum_frame_size()
537 / min_t(unsigned int, 7, fse->index + 1); in mt9p031_enum_frame_size()
538 fse->max_width = fse->min_width; in mt9p031_enum_frame_size()
539 fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1); in mt9p031_enum_frame_size()
540 fse->max_height = fse->min_height; in mt9p031_enum_frame_size()
552 return v4l2_subdev_get_try_format(&mt9p031->subdev, sd_state, in __mt9p031_get_pad_format()
555 return &mt9p031->format; in __mt9p031_get_pad_format()
568 return v4l2_subdev_get_try_crop(&mt9p031->subdev, sd_state, in __mt9p031_get_pad_crop()
571 return &mt9p031->crop; in __mt9p031_get_pad_crop()
583 fmt->format = *__mt9p031_get_pad_format(mt9p031, sd_state, fmt->pad, in mt9p031_get_format()
584 fmt->which); in mt9p031_get_format()
600 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, format->pad, in mt9p031_set_format()
601 format->which); in mt9p031_set_format()
604 width = clamp_t(unsigned int, ALIGN(format->format.width, 2), in mt9p031_set_format()
605 max_t(unsigned int, __crop->width / 7, in mt9p031_set_format()
607 __crop->width); in mt9p031_set_format()
608 height = clamp_t(unsigned int, ALIGN(format->format.height, 2), in mt9p031_set_format()
609 max_t(unsigned int, __crop->height / 8, in mt9p031_set_format()
611 __crop->height); in mt9p031_set_format()
613 hratio = DIV_ROUND_CLOSEST(__crop->width, width); in mt9p031_set_format()
614 vratio = DIV_ROUND_CLOSEST(__crop->height, height); in mt9p031_set_format()
616 __format = __mt9p031_get_pad_format(mt9p031, sd_state, format->pad, in mt9p031_set_format()
617 format->which); in mt9p031_set_format()
618 __format->width = __crop->width / hratio; in mt9p031_set_format()
619 __format->height = __crop->height / vratio; in mt9p031_set_format()
621 format->format = *__format; in mt9p031_set_format()
632 switch (sel->target) { in mt9p031_get_selection()
634 sel->r.left = MT9P031_COLUMN_START_MIN; in mt9p031_get_selection()
635 sel->r.top = MT9P031_ROW_START_MIN; in mt9p031_get_selection()
636 sel->r.width = MT9P031_WINDOW_WIDTH_MAX; in mt9p031_get_selection()
637 sel->r.height = MT9P031_WINDOW_HEIGHT_MAX; in mt9p031_get_selection()
641 sel->r = *__mt9p031_get_pad_crop(mt9p031, sd_state, in mt9p031_get_selection()
642 sel->pad, sel->which); in mt9p031_get_selection()
646 return -EINVAL; in mt9p031_get_selection()
659 if (sel->target != V4L2_SEL_TGT_CROP) in mt9p031_set_selection()
660 return -EINVAL; in mt9p031_set_selection()
665 rect.left = clamp(ALIGN(sel->r.left, 2), MT9P031_COLUMN_START_MIN, in mt9p031_set_selection()
667 rect.top = clamp(ALIGN(sel->r.top, 2), MT9P031_ROW_START_MIN, in mt9p031_set_selection()
669 rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), in mt9p031_set_selection()
672 rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), in mt9p031_set_selection()
677 MT9P031_PIXEL_ARRAY_WIDTH - rect.left); in mt9p031_set_selection()
679 MT9P031_PIXEL_ARRAY_HEIGHT - rect.top); in mt9p031_set_selection()
681 __crop = __mt9p031_get_pad_crop(mt9p031, sd_state, sel->pad, in mt9p031_set_selection()
682 sel->which); in mt9p031_set_selection()
684 if (rect.width != __crop->width || rect.height != __crop->height) { in mt9p031_set_selection()
689 sel->pad, in mt9p031_set_selection()
690 sel->which); in mt9p031_set_selection()
691 __format->width = rect.width; in mt9p031_set_selection()
692 __format->height = rect.height; in mt9p031_set_selection()
696 sel->r = rect; in mt9p031_set_selection()
711 crop->left = MT9P031_COLUMN_START_DEF; in mt9p031_init_cfg()
712 crop->top = MT9P031_ROW_START_DEF; in mt9p031_init_cfg()
713 crop->width = MT9P031_WINDOW_WIDTH_DEF; in mt9p031_init_cfg()
714 crop->height = MT9P031_WINDOW_HEIGHT_DEF; in mt9p031_init_cfg()
718 if (mt9p031->model == MT9P031_MODEL_MONOCHROME) in mt9p031_init_cfg()
719 format->code = MEDIA_BUS_FMT_Y12_1X12; in mt9p031_init_cfg()
721 format->code = MEDIA_BUS_FMT_SGRBG12_1X12; in mt9p031_init_cfg()
723 format->width = MT9P031_WINDOW_WIDTH_DEF; in mt9p031_init_cfg()
724 format->height = MT9P031_WINDOW_HEIGHT_DEF; in mt9p031_init_cfg()
725 format->field = V4L2_FIELD_NONE; in mt9p031_init_cfg()
726 format->colorspace = V4L2_COLORSPACE_SRGB; in mt9p031_init_cfg()
731 /* -----------------------------------------------------------------------------
742 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_restore_blc()
745 if (mt9p031->blc_auto->cur.val != 0) { in mt9p031_restore_blc()
752 if (mt9p031->blc_offset->cur.val != 0) { in mt9p031_restore_blc()
754 mt9p031->blc_offset->cur.val); in mt9p031_restore_blc()
765 container_of(ctrl->handler, struct mt9p031, ctrls); in mt9p031_s_ctrl()
766 struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); in mt9p031_s_ctrl()
770 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) in mt9p031_s_ctrl()
773 switch (ctrl->id) { in mt9p031_s_ctrl()
776 (ctrl->val >> 16) & 0xffff); in mt9p031_s_ctrl()
781 ctrl->val & 0xffff); in mt9p031_s_ctrl()
788 * ------------------------------------------ in mt9p031_s_ctrl()
798 if (ctrl->val <= 32) { in mt9p031_s_ctrl()
799 data = ctrl->val; in mt9p031_s_ctrl()
800 } else if (ctrl->val <= 64) { in mt9p031_s_ctrl()
801 ctrl->val &= ~1; in mt9p031_s_ctrl()
802 data = (1 << 6) | (ctrl->val >> 1); in mt9p031_s_ctrl()
804 ctrl->val &= ~7; in mt9p031_s_ctrl()
805 data = ((ctrl->val - 64) << 5) | (1 << 6) | 32; in mt9p031_s_ctrl()
811 if (ctrl->val) in mt9p031_s_ctrl()
819 if (ctrl->val) in mt9p031_s_ctrl()
829 * in the image. Activate (deactivate) the BLC-related controls in mt9p031_s_ctrl()
832 v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0); in mt9p031_s_ctrl()
833 v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0); in mt9p031_s_ctrl()
835 if (!ctrl->val) { in mt9p031_s_ctrl()
865 ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT) in mt9p031_s_ctrl()
870 ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC, in mt9p031_s_ctrl()
871 ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0); in mt9p031_s_ctrl()
876 ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC); in mt9p031_s_ctrl()
880 ctrl->val); in mt9p031_s_ctrl()
883 data = ctrl->val & ((1 << 9) - 1); in mt9p031_s_ctrl()
898 ctrl->val & ((1 << 12) - 1)); in mt9p031_s_ctrl()
947 .min = -255,
957 .min = -2048,
965 /* -----------------------------------------------------------------------------
974 mutex_lock(&mt9p031->power_lock); in mt9p031_set_power()
979 if (mt9p031->power_count == !on) { in mt9p031_set_power()
986 mt9p031->power_count += on ? 1 : -1; in mt9p031_set_power()
987 WARN_ON(mt9p031->power_count < 0); in mt9p031_set_power()
990 mutex_unlock(&mt9p031->power_lock); in mt9p031_set_power()
994 /* -----------------------------------------------------------------------------
1007 dev_err(&client->dev, "MT9P031 power up failed\n"); in mt9p031_registered()
1016 dev_err(&client->dev, "MT9P031 not detected, wrong version " in mt9p031_registered()
1018 return -ENODEV; in mt9p031_registered()
1021 dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n", in mt9p031_registered()
1022 client->addr); in mt9p031_registered()
1067 /* -----------------------------------------------------------------------------
1080 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node) in mt9p031_get_pdata()
1081 return client->dev.platform_data; in mt9p031_get_pdata()
1083 np = of_graph_get_next_endpoint(client->dev.of_node, NULL); in mt9p031_get_pdata()
1090 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); in mt9p031_get_pdata()
1094 of_property_read_u32(np, "input-clock-frequency", &pdata->ext_freq); in mt9p031_get_pdata()
1095 of_property_read_u32(np, "pixel-clock-frequency", &pdata->target_freq); in mt9p031_get_pdata()
1097 pdata->pixclk_pol = !!(endpoint.bus.parallel.flags & in mt9p031_get_pdata()
1109 struct i2c_adapter *adapter = client->adapter; in mt9p031_probe()
1115 dev_err(&client->dev, "No platform data\n"); in mt9p031_probe()
1116 return -EINVAL; in mt9p031_probe()
1120 dev_warn(&client->dev, in mt9p031_probe()
1121 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); in mt9p031_probe()
1122 return -EIO; in mt9p031_probe()
1125 mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL); in mt9p031_probe()
1127 return -ENOMEM; in mt9p031_probe()
1129 mt9p031->pdata = pdata; in mt9p031_probe()
1130 mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF; in mt9p031_probe()
1131 mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC; in mt9p031_probe()
1132 mt9p031->model = did->driver_data; in mt9p031_probe()
1134 mt9p031->regulators[0].supply = "vdd"; in mt9p031_probe()
1135 mt9p031->regulators[1].supply = "vdd_io"; in mt9p031_probe()
1136 mt9p031->regulators[2].supply = "vaa"; in mt9p031_probe()
1138 ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators); in mt9p031_probe()
1140 dev_err(&client->dev, "Unable to get regulators\n"); in mt9p031_probe()
1144 mutex_init(&mt9p031->power_lock); in mt9p031_probe()
1146 v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6); in mt9p031_probe()
1148 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1152 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1155 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1157 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1159 v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1160 V4L2_CID_PIXEL_RATE, pdata->target_freq, in mt9p031_probe()
1161 pdata->target_freq, 1, pdata->target_freq); in mt9p031_probe()
1162 v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops, in mt9p031_probe()
1164 ARRAY_SIZE(mt9p031_test_pattern_menu) - 1, 0, in mt9p031_probe()
1168 v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL); in mt9p031_probe()
1170 mt9p031->subdev.ctrl_handler = &mt9p031->ctrls; in mt9p031_probe()
1172 if (mt9p031->ctrls.error) { in mt9p031_probe()
1174 __func__, mt9p031->ctrls.error); in mt9p031_probe()
1175 ret = mt9p031->ctrls.error; in mt9p031_probe()
1179 mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO); in mt9p031_probe()
1180 mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls, in mt9p031_probe()
1183 v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops); in mt9p031_probe()
1184 mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops; in mt9p031_probe()
1186 mt9p031->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; in mt9p031_probe()
1187 mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE; in mt9p031_probe()
1188 ret = media_entity_pads_init(&mt9p031->subdev.entity, 1, &mt9p031->pad); in mt9p031_probe()
1192 mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in mt9p031_probe()
1194 ret = mt9p031_init_cfg(&mt9p031->subdev, NULL); in mt9p031_probe()
1198 mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset", in mt9p031_probe()
1205 ret = v4l2_async_register_subdev(&mt9p031->subdev); in mt9p031_probe()
1209 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_probe()
1210 media_entity_cleanup(&mt9p031->subdev.entity); in mt9p031_probe()
1211 mutex_destroy(&mt9p031->power_lock); in mt9p031_probe()
1222 v4l2_ctrl_handler_free(&mt9p031->ctrls); in mt9p031_remove()
1224 media_entity_cleanup(&subdev->entity); in mt9p031_remove()
1225 mutex_destroy(&mt9p031->power_lock); in mt9p031_remove()