Lines Matching +full:hi +full:- +full:fi
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Copyright (C) 2014-2017 Mentor Graphics Inc.
8 #include <linux/clk-provider.h>
22 #include <media/v4l2-async.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
166 * MIPI CSI-2 link frequencies.
338 * to set the MIPI CSI-2 virtual channel.
343 "MIPI CSI-2 virtual channel (0..3), default 0");
379 /* Visibile crop: from analog crop top-left corner. */
476 return &container_of(ctrl->handler, struct ov5640_dev, in ctrl_to_sd()
477 ctrls.handler)->sd; in ctrl_to_sd()
482 return sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY; in ov5640_is_csi2()
511 return format->bpp; in ov5640_code_to_bpp()
516 * entries that set the register to their power-on default values,
1134 return &mode->csi2_timings; in ov5640_timings()
1136 return &mode->dvp_timings; in ov5640_timings()
1141 struct i2c_client *client = sensor->i2c_client; in ov5640_init_slave_id()
1146 if (client->addr == OV5640_DEFAULT_SLAVE_ID) in ov5640_init_slave_id()
1151 buf[2] = client->addr << 1; in ov5640_init_slave_id()
1158 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_init_slave_id()
1160 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); in ov5640_init_slave_id()
1169 struct i2c_client *client = sensor->i2c_client; in ov5640_write_reg()
1178 msg.addr = client->addr; in ov5640_write_reg()
1179 msg.flags = client->flags; in ov5640_write_reg()
1183 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_write_reg()
1185 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", in ov5640_write_reg()
1195 struct i2c_client *client = sensor->i2c_client; in ov5640_read_reg()
1203 msg[0].addr = client->addr; in ov5640_read_reg()
1204 msg[0].flags = client->flags; in ov5640_read_reg()
1208 msg[1].addr = client->addr; in ov5640_read_reg()
1209 msg[1].flags = client->flags | I2C_M_RD; in ov5640_read_reg()
1213 ret = i2c_transfer(client->adapter, msg, 2); in ov5640_read_reg()
1215 dev_err(&client->dev, "%s: error: reg=%x\n", in ov5640_read_reg()
1226 u8 hi, lo; in ov5640_read_reg16() local
1229 ret = ov5640_read_reg(sensor, reg, &hi); in ov5640_read_reg16()
1236 *val = ((u16)hi << 8) | (u16)lo; in ov5640_read_reg16()
1273 * +--------------+
1275 * +-+------------+
1276 * | +----------+
1277 * +->| PLL1 | - reg 0x3036, for the multiplier
1278 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
1279 * | +--------------+
1280 * +->| System Clock | - reg 0x3035, bits 4-7
1281 * +-+------------+
1282 * | +--------------+
1283 * +->| MIPI Divider | - reg 0x3035, bits 0-3
1284 * | +-+------------+
1285 * | +----------------> MIPI SCLK
1286 * | + +-----+
1287 * | +->| / 2 |-------> MIPI BIT CLK
1288 * | +-----+
1289 * | +--------------+
1290 * +->| PLL Root Div | - reg 0x3037, bit 4
1291 * +-+------------+
1292 * | +---------+
1293 * +->| Bit Div | - reg 0x3034, bits 0-3
1294 * +-+-------+
1295 * | +-------------+
1296 * +->| SCLK Div | - reg 0x3108, bits 0-1
1297 * | +-+-----------+
1298 * | +---------------> SCLK
1299 * | +-------------+
1300 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
1301 * | +-+-----------+
1302 * | +---------------> SCLK 2X
1303 * | +-------------+
1304 * +->| PCLK Div | - reg 0x3108, bits 4-5
1305 * ++------------+
1306 * + +-----------+
1307 * +->| P_DIV | - reg 0x3035, bits 0-3
1308 * +-----+-----+
1309 * +------------> PCLK
1312 * - the PLL pre-divider output rate should be in the 4-27MHz range
1313 * - the PLL multiplier output rate should be in the 500-1000MHz range
1314 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
1341 * We only supports 8-bit formats at the moment
1369 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; in ov5640_compute_sys_clk()
1420 if (abs(rate - _rate) < abs(rate - best)) { in ov5640_calc_sys_clk()
1440 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1441 * for the MIPI CSI-2 output.
1455 link_freq = sensor->current_link_freq; in ov5640_set_mipi_pclk()
1458 * - mipi_div - Additional divider for the MIPI lane clock. in ov5640_set_mipi_pclk()
1472 * Adjust PLL parameters to maintain the MIPI_SCLK-to-PCLK ratio. in ov5640_set_mipi_pclk()
1474 * - root_div = 2 (fixed) in ov5640_set_mipi_pclk()
1475 * - bit_div : MIPI 8-bit = 2; MIPI 10-bit = 2.5 in ov5640_set_mipi_pclk()
1476 * - pclk_div = 1 (fixed) in ov5640_set_mipi_pclk()
1477 * - p_div = (2 lanes ? mipi_div : 2 * mipi_div) in ov5640_set_mipi_pclk()
1482 * - 2 lanes: MIPI_SCLK = (4 or 5) * PCLK in ov5640_set_mipi_pclk()
1483 * - 1 lanes: MIPI_SCLK = (8 or 10) * PCLK in ov5640_set_mipi_pclk()
1491 * - YUV: PCLK >= 2 * SCLK in ov5640_set_mipi_pclk()
1492 * - RAW or JPEG: PCLK >= SCLK in ov5640_set_mipi_pclk()
1493 * - sclk2x_div = sclk_div / 2 in ov5640_set_mipi_pclk()
1499 * Set the pixel clock period expressed in ns with 1-bit decimal in ov5640_set_mipi_pclk()
1504 * actual definition is 2 * 8-bit sample period. in ov5640_set_mipi_pclk()
1508 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes; in ov5640_set_mipi_pclk()
1541 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_calc_pixel_rate()
1542 const struct ov5640_timings *timings = &mode->dvp_timings; in ov5640_calc_pixel_rate()
1545 rate = timings->htot * (timings->crop.height + timings->vblank_def); in ov5640_calc_pixel_rate()
1546 rate *= ov5640_framerates[sensor->current_fr]; in ov5640_calc_pixel_rate()
1575 rate *= ov5640_code_to_bpp(sensor, sensor->fmt.code); in ov5640_set_dvp_pclk()
1576 rate /= sensor->ep.bus.parallel.bus_width; in ov5640_set_dvp_pclk()
1604 0x1f, prediv | ((pll_rdiv - 1) << 4)); in ov5640_set_dvp_pclk()
1629 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->width); in ov5640_set_jpeg_timings()
1633 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->height); in ov5640_set_jpeg_timings()
1645 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { in ov5640_set_timings()
1652 analog_crop = &timings->analog_crop; in ov5640_set_timings()
1653 crop = &timings->crop; in ov5640_set_timings()
1656 analog_crop->left); in ov5640_set_timings()
1661 analog_crop->top); in ov5640_set_timings()
1666 analog_crop->left + analog_crop->width - 1); in ov5640_set_timings()
1671 analog_crop->top + analog_crop->height - 1); in ov5640_set_timings()
1675 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HOFFS, crop->left); in ov5640_set_timings()
1679 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VOFFS, crop->top); in ov5640_set_timings()
1683 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->width); in ov5640_set_timings()
1687 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->height); in ov5640_set_timings()
1691 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, timings->htot); in ov5640_set_timings()
1696 mode->height + timings->vblank_def); in ov5640_set_timings()
1713 delay_ms = regs->delay_ms; in ov5640_load_regs()
1714 reg_addr = regs->reg_addr; in ov5640_load_regs()
1715 val = regs->val; in ov5640_load_regs()
1716 mask = regs->mask; in ov5640_load_regs()
1719 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && in ov5640_load_regs()
1850 u32 xvclk = sensor->xclk_freq / 10000; in ov5640_get_sysclk()
1889 return -EINVAL; in ov5640_get_sysclk()
1990 return -EINVAL; in ov5640_set_bandingfilter()
1991 sensor->prev_sysclk = ret; in ov5640_set_bandingfilter()
1997 return -EINVAL; in ov5640_set_bandingfilter()
1998 sensor->prev_hts = ret; in ov5640_set_bandingfilter()
2008 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; in ov5640_set_bandingfilter()
2013 return -EINVAL; in ov5640_set_bandingfilter()
2014 max_band60 = (int)((prev_vts - 4) / band_step60); in ov5640_set_bandingfilter()
2020 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; in ov5640_set_bandingfilter()
2025 return -EINVAL; in ov5640_set_bandingfilter()
2026 max_band50 = (int)((prev_vts - 4) / band_step50); in ov5640_set_bandingfilter()
2036 sensor->ae_low = target * 23 / 25; /* 0.92 */ in ov5640_set_ae_target()
2037 sensor->ae_high = target * 27 / 25; /* 1.08 */ in ov5640_set_ae_target()
2039 fast_high = sensor->ae_high << 1; in ov5640_set_ae_target()
2043 fast_low = sensor->ae_low >> 1; in ov5640_set_ae_target()
2045 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); in ov5640_set_ae_target()
2048 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); in ov5640_set_ae_target()
2051 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); in ov5640_set_ae_target()
2054 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); in ov5640_set_ae_target()
2081 * - [0]: Horizontal binning enable in ov5640_set_binning()
2089 * - [0]: Undocumented, but hardcoded init sequences in ov5640_set_binning()
2098 struct i2c_client *client = sensor->i2c_client; in ov5640_set_virtual_channel()
2103 dev_err(&client->dev, in ov5640_set_virtual_channel()
2106 return -EINVAL; in ov5640_set_virtual_channel()
2128 (mode->width != width || mode->height != height))) in ov5640_find_mode()
2149 if (!mode->reg_data) in ov5640_set_mode_exposure_calc()
2150 return -EINVAL; in ov5640_set_mode_exposure_calc()
2160 if (ret && mode->id != OV5640_MODE_720P_1280_720 && in ov5640_set_mode_exposure_calc()
2161 mode->id != OV5640_MODE_1080P_1920_1080) in ov5640_set_mode_exposure_calc()
2181 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size); in ov5640_set_mode_exposure_calc()
2195 return -EINVAL; in ov5640_set_mode_exposure_calc()
2202 return -EINVAL; in ov5640_set_mode_exposure_calc()
2219 if (!sensor->prev_sysclk) { in ov5640_set_mode_exposure_calc()
2224 return -EINVAL; in ov5640_set_mode_exposure_calc()
2225 sensor->prev_sysclk = ret; in ov5640_set_mode_exposure_calc()
2229 return -EINVAL; in ov5640_set_mode_exposure_calc()
2231 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); in ov5640_set_mode_exposure_calc()
2234 if (average > sensor->ae_low && average < sensor->ae_high) { in ov5640_set_mode_exposure_calc()
2238 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
2239 sensor->prev_hts / cap_hts * in ov5640_set_mode_exposure_calc()
2240 sensor->ae_target / average; in ov5640_set_mode_exposure_calc()
2244 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
2245 sensor->prev_hts / cap_hts; in ov5640_set_mode_exposure_calc()
2263 return -EINVAL; in ov5640_set_mode_exposure_calc()
2272 return -EINVAL; in ov5640_set_mode_exposure_calc()
2284 if (cap_shutter > (cap_vts - 4)) { in ov5640_set_mode_exposure_calc()
2302 if (!mode->reg_data) in ov5640_set_mode_direct()
2303 return -EINVAL; in ov5640_set_mode_direct()
2306 ov5640_load_regs(sensor, mode->reg_data, mode->reg_data_size); in ov5640_set_mode_direct()
2312 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mode()
2313 const struct ov5640_mode_info *orig_mode = sensor->last_mode; in ov5640_set_mode()
2315 bool auto_gain = sensor->ctrls.auto_gain->val == 1; in ov5640_set_mode()
2316 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; in ov5640_set_mode()
2319 dn_mode = mode->dn_mode; in ov5640_set_mode()
2320 orig_dn_mode = orig_mode->dn_mode; in ov5640_set_mode()
2368 ret = ov5640_set_ae_target(sensor, sensor->ae_target); in ov5640_set_mode()
2381 sensor->pending_mode_change = false; in ov5640_set_mode()
2382 sensor->last_mode = mode; in ov5640_set_mode()
2399 /* restore the last set video mode after chip power-on */
2419 return ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_restore_mode()
2424 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); in ov5640_power()
2429 if (!sensor->reset_gpio) in ov5640_reset()
2432 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
2440 gpiod_set_value_cansleep(sensor->reset_gpio, 1); in ov5640_reset()
2443 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
2449 struct i2c_client *client = sensor->i2c_client; in ov5640_set_power_on()
2452 ret = clk_prepare_enable(sensor->xclk); in ov5640_set_power_on()
2454 dev_err(&client->dev, "%s: failed to enable clock\n", in ov5640_set_power_on()
2460 sensor->supplies); in ov5640_set_power_on()
2462 dev_err(&client->dev, "%s: failed to enable regulators\n", in ov5640_set_power_on()
2478 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_on()
2480 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_on()
2487 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_off()
2488 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_off()
2548 unsigned int flags = sensor->ep.bus.parallel.flags; in ov5640_set_power_dvp()
2549 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; in ov5640_set_power_dvp()
2576 * - VSYNC: active high in ov5640_set_power_dvp()
2577 * - HREF: active low in ov5640_set_power_dvp()
2578 * - PCLK: active low in ov5640_set_power_dvp()
2587 * - [7]: SYNC code selection (0: auto generate sync code, in ov5640_set_power_dvp()
2588 * 1: sync code from regs 0x4732-0x4735) in ov5640_set_power_dvp()
2589 * - [6]: f value in CCIR656 SYNC code when fixed f value in ov5640_set_power_dvp()
2590 * - [5]: Fixed f value in ov5640_set_power_dvp()
2591 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, in ov5640_set_power_dvp()
2592 * 01: data from regs 0x4736-0x4738, 10: always keep 0) in ov5640_set_power_dvp()
2593 * - [1]: Clip data disable in ov5640_set_power_dvp()
2594 * - [0]: CCIR656 mode enable in ov5640_set_power_dvp()
2598 * - CCIR656 mode enable in ov5640_set_power_dvp()
2599 * - auto generation of sync codes in ov5640_set_power_dvp()
2600 * - blank toggle data 1'h040/1'h200 in ov5640_set_power_dvp()
2601 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) in ov5640_set_power_dvp()
2612 * - [5]: PCLK polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2613 * - [1]: HREF polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2614 * - [0]: VSYNC polarity (mismatch here between in ov5640_set_power_dvp()
2648 * - 6: VSYNC output enable in ov5640_set_power_dvp()
2649 * - 5: HREF output enable in ov5640_set_power_dvp()
2650 * - 4: PCLK output enable in ov5640_set_power_dvp()
2651 * - [3:0]: D[9:6] output enable in ov5640_set_power_dvp()
2662 * - [7:2]: D[5:0] output enable in ov5640_set_power_dvp()
2681 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_set_power()
2714 /* --------------- Subdev Operations --------------- */
2717 struct v4l2_fract *fi, in ov5640_try_frame_interval() argument
2728 if (fi->numerator == 0) { in ov5640_try_frame_interval()
2729 fi->denominator = maxfps; in ov5640_try_frame_interval()
2730 fi->numerator = 1; in ov5640_try_frame_interval()
2735 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), in ov5640_try_frame_interval()
2742 if (abs(curr_fps - fps) < abs(best_fps - fps)) { in ov5640_try_frame_interval()
2748 fi->numerator = 1; in ov5640_try_frame_interval()
2749 fi->denominator = best_fps; in ov5640_try_frame_interval()
2753 return mode ? rate : -EINVAL; in ov5640_try_frame_interval()
2763 if (format->pad != 0) in ov5640_get_fmt()
2764 return -EINVAL; in ov5640_get_fmt()
2766 mutex_lock(&sensor->lock); in ov5640_get_fmt()
2768 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_get_fmt()
2769 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, in ov5640_get_fmt()
2770 format->pad); in ov5640_get_fmt()
2772 fmt = &sensor->fmt; in ov5640_get_fmt()
2774 format->format = *fmt; in ov5640_get_fmt()
2776 mutex_unlock(&sensor->lock); in ov5640_get_fmt()
2791 mode = ov5640_find_mode(sensor, fmt->width, fmt->height, true); in ov5640_try_fmt_internal()
2793 return -EINVAL; in ov5640_try_fmt_internal()
2795 pixfmt = ov5640_code_to_pixfmt(sensor, fmt->code); in ov5640_try_fmt_internal()
2796 bpp = pixfmt->bpp; in ov5640_try_fmt_internal()
2800 * - 8bpp modes work for resolution >= 1280x720 in ov5640_try_fmt_internal()
2801 * - 24bpp modes work resolution < 1280x720 in ov5640_try_fmt_internal()
2803 if (bpp == 8 && mode->width < 1280) in ov5640_try_fmt_internal()
2805 else if (bpp == 24 && mode->width > 1024) in ov5640_try_fmt_internal()
2808 fmt->width = mode->width; in ov5640_try_fmt_internal()
2809 fmt->height = mode->height; in ov5640_try_fmt_internal()
2814 fmt->code = pixfmt->code; in ov5640_try_fmt_internal()
2815 fmt->colorspace = pixfmt->colorspace; in ov5640_try_fmt_internal()
2816 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2817 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_try_fmt_internal()
2818 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2825 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_update_pixel_rate()
2826 enum ov5640_pixel_rate_id pixel_rate_id = mode->pixel_rate; in ov5640_update_pixel_rate()
2827 struct v4l2_mbus_framefmt *fmt = &sensor->fmt; in ov5640_update_pixel_rate()
2844 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_update_pixel_rate()
2851 * The MIPI CSI-2 link frequency should comply with the CSI-2 in ov5640_update_pixel_rate()
2857 num_lanes = sensor->ep.bus.mipi_csi2.num_data_lanes; in ov5640_update_pixel_rate()
2858 bpp = ov5640_code_to_bpp(sensor, fmt->code); in ov5640_update_pixel_rate()
2865 sensor->current_link_freq = link_freq; in ov5640_update_pixel_rate()
2886 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, pixel_rate); in ov5640_update_pixel_rate()
2887 __v4l2_ctrl_s_ctrl(sensor->ctrls.link_freq, i); in ov5640_update_pixel_rate()
2890 hblank = timings->htot - mode->width; in ov5640_update_pixel_rate()
2891 __v4l2_ctrl_modify_range(sensor->ctrls.hblank, in ov5640_update_pixel_rate()
2894 vblank = timings->vblank_def; in ov5640_update_pixel_rate()
2896 if (sensor->current_fr != mode->def_fps) { in ov5640_update_pixel_rate()
2901 int fie_num = sensor->frame_interval.numerator; in ov5640_update_pixel_rate()
2902 int fie_denom = sensor->frame_interval.denominator; in ov5640_update_pixel_rate()
2904 vblank = ((fie_num * pixel_rate / fie_denom) / timings->htot) - in ov5640_update_pixel_rate()
2905 mode->height; in ov5640_update_pixel_rate()
2908 __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV5640_MIN_VBLANK, in ov5640_update_pixel_rate()
2909 OV5640_MAX_VTS - mode->height, 1, vblank); in ov5640_update_pixel_rate()
2910 __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, vblank); in ov5640_update_pixel_rate()
2912 exposure_max = timings->crop.height + vblank - 4; in ov5640_update_pixel_rate()
2913 exposure_val = clamp_t(s32, sensor->ctrls.exposure->val, in ov5640_update_pixel_rate()
2914 sensor->ctrls.exposure->minimum, in ov5640_update_pixel_rate()
2917 __v4l2_ctrl_modify_range(sensor->ctrls.exposure, in ov5640_update_pixel_rate()
2918 sensor->ctrls.exposure->minimum, in ov5640_update_pixel_rate()
2930 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; in ov5640_set_fmt()
2933 if (format->pad != 0) in ov5640_set_fmt()
2934 return -EINVAL; in ov5640_set_fmt()
2936 mutex_lock(&sensor->lock); in ov5640_set_fmt()
2938 if (sensor->streaming) { in ov5640_set_fmt()
2939 ret = -EBUSY; in ov5640_set_fmt()
2944 sensor->current_fr, &new_mode); in ov5640_set_fmt()
2948 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { in ov5640_set_fmt()
2953 if (new_mode != sensor->current_mode) { in ov5640_set_fmt()
2954 sensor->current_fr = new_mode->def_fps; in ov5640_set_fmt()
2955 sensor->current_mode = new_mode; in ov5640_set_fmt()
2956 sensor->pending_mode_change = true; in ov5640_set_fmt()
2958 if (mbus_fmt->code != sensor->fmt.code) in ov5640_set_fmt()
2959 sensor->pending_fmt_change = true; in ov5640_set_fmt()
2962 sensor->fmt = *mbus_fmt; in ov5640_set_fmt()
2967 mutex_unlock(&sensor->lock); in ov5640_set_fmt()
2976 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_get_selection()
2979 switch (sel->target) { in ov5640_get_selection()
2981 mutex_lock(&sensor->lock); in ov5640_get_selection()
2983 sel->r = timings->analog_crop; in ov5640_get_selection()
2984 mutex_unlock(&sensor->lock); in ov5640_get_selection()
2991 sel->r.top = 0; in ov5640_get_selection()
2992 sel->r.left = 0; in ov5640_get_selection()
2993 sel->r.width = OV5640_NATIVE_WIDTH; in ov5640_get_selection()
2994 sel->r.height = OV5640_NATIVE_HEIGHT; in ov5640_get_selection()
2999 sel->r.top = OV5640_PIXEL_ARRAY_TOP; in ov5640_get_selection()
3000 sel->r.left = OV5640_PIXEL_ARRAY_LEFT; in ov5640_get_selection()
3001 sel->r.width = OV5640_PIXEL_ARRAY_WIDTH; in ov5640_get_selection()
3002 sel->r.height = OV5640_PIXEL_ARRAY_HEIGHT; in ov5640_get_selection()
3007 return -EINVAL; in ov5640_get_selection()
3013 bool is_jpeg = format->code == MEDIA_BUS_FMT_JPEG_1X8; in ov5640_set_framefmt()
3017 pixfmt = ov5640_code_to_pixfmt(sensor, format->code); in ov5640_set_framefmt()
3021 pixfmt->ctrl00); in ov5640_set_framefmt()
3027 pixfmt->mux); in ov5640_set_framefmt()
3033 * - [5]: JPEG enable in ov5640_set_framefmt()
3042 * - [4]: Reset JFIFO in ov5640_set_framefmt()
3043 * - [3]: Reset SFIFO in ov5640_set_framefmt()
3044 * - [2]: Reset JPEG in ov5640_set_framefmt()
3054 * - [5]: Enable JPEG 2x clock in ov5640_set_framefmt()
3055 * - [3]: Enable JPEG clock in ov5640_set_framefmt()
3133 u16 red = (u16)sensor->ctrls.red_balance->val; in ov5640_set_ctrl_white_balance()
3134 u16 blue = (u16)sensor->ctrls.blue_balance->val; in ov5640_set_ctrl_white_balance()
3148 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_exposure()
3152 if (ctrls->auto_exp->is_new) { in ov5640_set_ctrl_exposure()
3158 if (!auto_exp && ctrls->exposure->is_new) { in ov5640_set_ctrl_exposure()
3171 if (ctrls->exposure->val < max_exp) in ov5640_set_ctrl_exposure()
3172 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); in ov5640_set_ctrl_exposure()
3180 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_gain()
3183 if (ctrls->auto_gain->is_new) { in ov5640_set_ctrl_gain()
3189 if (!auto_gain && ctrls->gain->is_new) in ov5640_set_ctrl_gain()
3190 ret = ov5640_set_gain(sensor, ctrls->gain->val); in ov5640_set_ctrl_gain()
3260 * - [2]: ISP mirror in ov5640_set_ctrl_hflip()
3261 * - [1]: Sensor mirror in ov5640_set_ctrl_hflip()
3265 (!(value ^ sensor->upside_down)) ? in ov5640_set_ctrl_hflip()
3275 * - [2]: ISP vflip in ov5640_set_ctrl_vflip()
3276 * - [1]: Sensor vflip in ov5640_set_ctrl_vflip()
3280 (value ^ sensor->upside_down) ? in ov5640_set_ctrl_vflip()
3286 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_ctrl_vblank()
3290 mode->height + value); in ov5640_set_ctrl_vblank()
3301 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev)) in ov5640_g_volatile_ctrl()
3304 switch (ctrl->id) { in ov5640_g_volatile_ctrl()
3309 sensor->ctrls.gain->val = val; in ov5640_g_volatile_ctrl()
3315 sensor->ctrls.exposure->val = val; in ov5640_g_volatile_ctrl()
3319 pm_runtime_put_autosuspend(&sensor->i2c_client->dev); in ov5640_g_volatile_ctrl()
3328 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_s_ctrl()
3335 switch (ctrl->id) { in ov5640_s_ctrl()
3339 exp_max = mode->height + ctrl->val - 4; in ov5640_s_ctrl()
3340 __v4l2_ctrl_modify_range(sensor->ctrls.exposure, in ov5640_s_ctrl()
3341 sensor->ctrls.exposure->minimum, in ov5640_s_ctrl()
3342 exp_max, sensor->ctrls.exposure->step, in ov5640_s_ctrl()
3343 timings->vblank_def); in ov5640_s_ctrl()
3352 if (!pm_runtime_get_if_in_use(&sensor->i2c_client->dev)) in ov5640_s_ctrl()
3355 switch (ctrl->id) { in ov5640_s_ctrl()
3357 ret = ov5640_set_ctrl_gain(sensor, ctrl->val); in ov5640_s_ctrl()
3360 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); in ov5640_s_ctrl()
3363 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); in ov5640_s_ctrl()
3366 ret = ov5640_set_ctrl_hue(sensor, ctrl->val); in ov5640_s_ctrl()
3369 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); in ov5640_s_ctrl()
3372 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); in ov5640_s_ctrl()
3375 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); in ov5640_s_ctrl()
3378 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); in ov5640_s_ctrl()
3381 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); in ov5640_s_ctrl()
3384 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); in ov5640_s_ctrl()
3387 ret = ov5640_set_ctrl_vblank(sensor, ctrl->val); in ov5640_s_ctrl()
3390 ret = -EINVAL; in ov5640_s_ctrl()
3394 pm_runtime_put_autosuspend(&sensor->i2c_client->dev); in ov5640_s_ctrl()
3406 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_init_controls()
3408 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_init_controls()
3409 struct v4l2_ctrl_handler *hdl = &ctrls->handler; in ov5640_init_controls()
3419 hdl->lock = &sensor->lock; in ov5640_init_controls()
3422 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, in ov5640_init_controls()
3423 ov5640_pixel_rates[OV5640_NUM_PIXEL_RATES - 1], in ov5640_init_controls()
3425 ov5640_pixel_rates[mode->pixel_rate]); in ov5640_init_controls()
3427 ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, ops, in ov5640_init_controls()
3429 ARRAY_SIZE(ov5640_csi2_link_freqs) - 1, in ov5640_init_controls()
3434 hblank = timings->htot - mode->width; in ov5640_init_controls()
3435 ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK, hblank, in ov5640_init_controls()
3438 max_vblank = OV5640_MAX_VTS - mode->height; in ov5640_init_controls()
3439 ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK, in ov5640_init_controls()
3441 1, timings->vblank_def); in ov5640_init_controls()
3444 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, in ov5640_init_controls()
3447 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, in ov5640_init_controls()
3449 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, in ov5640_init_controls()
3452 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, in ov5640_init_controls()
3456 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, in ov5640_init_controls()
3459 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, in ov5640_init_controls()
3461 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, in ov5640_init_controls()
3464 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, in ov5640_init_controls()
3466 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, in ov5640_init_controls()
3468 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, in ov5640_init_controls()
3470 ctrls->test_pattern = in ov5640_init_controls()
3472 ARRAY_SIZE(test_pattern_menu) - 1, in ov5640_init_controls()
3474 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, in ov5640_init_controls()
3476 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, in ov5640_init_controls()
3479 ctrls->light_freq = in ov5640_init_controls()
3485 if (hdl->error) { in ov5640_init_controls()
3486 ret = hdl->error; in ov5640_init_controls()
3490 ret = v4l2_fwnode_device_parse(&sensor->i2c_client->dev, &props); in ov5640_init_controls()
3495 sensor->upside_down = true; in ov5640_init_controls()
3501 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
3502 ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
3503 ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
3504 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
3505 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
3507 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); in ov5640_init_controls()
3508 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); in ov5640_init_controls()
3509 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); in ov5640_init_controls()
3511 sensor->sd.ctrl_handler = hdl; in ov5640_init_controls()
3524 u32 bpp = ov5640_code_to_bpp(sensor, fse->code); in ov5640_enum_frame_size()
3525 unsigned int index = fse->index; in ov5640_enum_frame_size()
3527 if (fse->pad != 0) in ov5640_enum_frame_size()
3528 return -EINVAL; in ov5640_enum_frame_size()
3530 return -EINVAL; in ov5640_enum_frame_size()
3532 /* Only low-resolution modes are supported for 24bpp formats. */ in ov5640_enum_frame_size()
3534 return -EINVAL; in ov5640_enum_frame_size()
3541 return -EINVAL; in ov5640_enum_frame_size()
3543 fse->min_width = ov5640_mode_data[index].width; in ov5640_enum_frame_size()
3544 fse->max_width = fse->min_width; in ov5640_enum_frame_size()
3545 fse->min_height = ov5640_mode_data[index].height; in ov5640_enum_frame_size()
3546 fse->max_height = fse->min_height; in ov5640_enum_frame_size()
3560 if (fie->pad != 0) in ov5640_enum_frame_interval()
3561 return -EINVAL; in ov5640_enum_frame_interval()
3562 if (fie->index >= OV5640_NUM_FRAMERATES) in ov5640_enum_frame_interval()
3563 return -EINVAL; in ov5640_enum_frame_interval()
3566 tpf.denominator = ov5640_framerates[fie->index]; in ov5640_enum_frame_interval()
3569 fie->width, fie->height); in ov5640_enum_frame_interval()
3571 return -EINVAL; in ov5640_enum_frame_interval()
3573 fie->interval = tpf; in ov5640_enum_frame_interval()
3578 struct v4l2_subdev_frame_interval *fi) in ov5640_g_frame_interval() argument
3582 mutex_lock(&sensor->lock); in ov5640_g_frame_interval()
3583 fi->interval = sensor->frame_interval; in ov5640_g_frame_interval()
3584 mutex_unlock(&sensor->lock); in ov5640_g_frame_interval()
3590 struct v4l2_subdev_frame_interval *fi) in ov5640_s_frame_interval() argument
3596 if (fi->pad != 0) in ov5640_s_frame_interval()
3597 return -EINVAL; in ov5640_s_frame_interval()
3599 mutex_lock(&sensor->lock); in ov5640_s_frame_interval()
3601 if (sensor->streaming) { in ov5640_s_frame_interval()
3602 ret = -EBUSY; in ov5640_s_frame_interval()
3606 mode = sensor->current_mode; in ov5640_s_frame_interval()
3608 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, in ov5640_s_frame_interval()
3609 mode->width, in ov5640_s_frame_interval()
3610 mode->height); in ov5640_s_frame_interval()
3613 fi->interval = sensor->frame_interval; in ov5640_s_frame_interval()
3617 mode = ov5640_find_mode(sensor, mode->width, mode->height, true); in ov5640_s_frame_interval()
3619 ret = -EINVAL; in ov5640_s_frame_interval()
3623 if (ov5640_framerates[frame_rate] > ov5640_framerates[mode->max_fps]) { in ov5640_s_frame_interval()
3624 ret = -EINVAL; in ov5640_s_frame_interval()
3628 if (mode != sensor->current_mode || in ov5640_s_frame_interval()
3629 frame_rate != sensor->current_fr) { in ov5640_s_frame_interval()
3630 sensor->current_fr = frame_rate; in ov5640_s_frame_interval()
3631 sensor->frame_interval = fi->interval; in ov5640_s_frame_interval()
3632 sensor->current_mode = mode; in ov5640_s_frame_interval()
3633 sensor->pending_mode_change = true; in ov5640_s_frame_interval()
3638 mutex_unlock(&sensor->lock); in ov5640_s_frame_interval()
3652 num_formats = ARRAY_SIZE(ov5640_csi2_formats) - 1; in ov5640_enum_mbus_code()
3655 num_formats = ARRAY_SIZE(ov5640_dvp_formats) - 1; in ov5640_enum_mbus_code()
3658 if (code->index >= num_formats) in ov5640_enum_mbus_code()
3659 return -EINVAL; in ov5640_enum_mbus_code()
3661 code->code = formats[code->index].code; in ov5640_enum_mbus_code()
3672 ret = pm_runtime_resume_and_get(&sensor->i2c_client->dev); in ov5640_s_stream()
3676 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); in ov5640_s_stream()
3678 pm_runtime_put(&sensor->i2c_client->dev); in ov5640_s_stream()
3683 mutex_lock(&sensor->lock); in ov5640_s_stream()
3685 if (sensor->streaming == !enable) { in ov5640_s_stream()
3686 if (enable && sensor->pending_mode_change) { in ov5640_s_stream()
3692 if (enable && sensor->pending_fmt_change) { in ov5640_s_stream()
3693 ret = ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_s_stream()
3696 sensor->pending_fmt_change = false; in ov5640_s_stream()
3705 sensor->streaming = enable; in ov5640_s_stream()
3709 mutex_unlock(&sensor->lock); in ov5640_s_stream()
3712 pm_runtime_put_autosuspend(&sensor->i2c_client->dev); in ov5640_s_stream()
3726 crop->left = OV5640_PIXEL_ARRAY_LEFT; in ov5640_init_cfg()
3727 crop->top = OV5640_PIXEL_ARRAY_TOP; in ov5640_init_cfg()
3728 crop->width = OV5640_PIXEL_ARRAY_WIDTH; in ov5640_init_cfg()
3729 crop->height = OV5640_PIXEL_ARRAY_HEIGHT; in ov5640_init_cfg()
3767 sensor->supplies[i].supply = ov5640_supply_name[i]; in ov5640_get_regulators()
3769 return devm_regulator_bulk_get(&sensor->i2c_client->dev, in ov5640_get_regulators()
3771 sensor->supplies); in ov5640_get_regulators()
3776 struct i2c_client *client = sensor->i2c_client; in ov5640_check_chip_id()
3782 dev_err(&client->dev, "%s: failed to read chip identifier\n", in ov5640_check_chip_id()
3788 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", in ov5640_check_chip_id()
3790 return -ENXIO; in ov5640_check_chip_id()
3798 struct device *dev = &client->dev; in ov5640_probe()
3805 return -ENOMEM; in ov5640_probe()
3807 sensor->i2c_client = client; in ov5640_probe()
3813 sensor->fmt = ov5640_default_fmt; in ov5640_probe()
3814 sensor->frame_interval.numerator = 1; in ov5640_probe()
3815 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; in ov5640_probe()
3816 sensor->current_fr = OV5640_30_FPS; in ov5640_probe()
3817 sensor->current_mode = in ov5640_probe()
3819 sensor->last_mode = sensor->current_mode; in ov5640_probe()
3820 sensor->current_link_freq = OV5640_DEFAULT_LINK_FREQ; in ov5640_probe()
3822 sensor->ae_target = 52; in ov5640_probe()
3824 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), in ov5640_probe()
3828 return -EINVAL; in ov5640_probe()
3831 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); in ov5640_probe()
3838 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && in ov5640_probe()
3839 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && in ov5640_probe()
3840 sensor->ep.bus_type != V4L2_MBUS_BT656) { in ov5640_probe()
3841 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); in ov5640_probe()
3842 return -EINVAL; in ov5640_probe()
3846 sensor->xclk = devm_clk_get(dev, "xclk"); in ov5640_probe()
3847 if (IS_ERR(sensor->xclk)) { in ov5640_probe()
3849 return PTR_ERR(sensor->xclk); in ov5640_probe()
3852 sensor->xclk_freq = clk_get_rate(sensor->xclk); in ov5640_probe()
3853 if (sensor->xclk_freq < OV5640_XCLK_MIN || in ov5640_probe()
3854 sensor->xclk_freq > OV5640_XCLK_MAX) { in ov5640_probe()
3856 sensor->xclk_freq); in ov5640_probe()
3857 return -EINVAL; in ov5640_probe()
3861 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", in ov5640_probe()
3863 if (IS_ERR(sensor->pwdn_gpio)) in ov5640_probe()
3864 return PTR_ERR(sensor->pwdn_gpio); in ov5640_probe()
3867 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", in ov5640_probe()
3869 if (IS_ERR(sensor->reset_gpio)) in ov5640_probe()
3870 return PTR_ERR(sensor->reset_gpio); in ov5640_probe()
3872 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); in ov5640_probe()
3874 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in ov5640_probe()
3876 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ov5640_probe()
3877 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov5640_probe()
3878 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ov5640_probe()
3886 mutex_init(&sensor->lock); in ov5640_probe()
3906 ret = v4l2_async_register_subdev_sensor(&sensor->sd); in ov5640_probe()
3919 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_probe()
3922 media_entity_cleanup(&sensor->sd.entity); in ov5640_probe()
3923 mutex_destroy(&sensor->lock); in ov5640_probe()
3931 struct device *dev = &client->dev; in ov5640_remove()
3938 v4l2_async_unregister_subdev(&sensor->sd); in ov5640_remove()
3939 media_entity_cleanup(&sensor->sd.entity); in ov5640_remove()
3940 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_remove()
3941 mutex_destroy(&sensor->lock); in ov5640_remove()