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>
21 #include <media/v4l2-async.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-fwnode.h>
26 #include <media/v4l2-subdev.h>
148 * to set the MIPI CSI-2 virtual channel.
153 "MIPI CSI-2 virtual channel (0..3), default 0");
266 return &container_of(ctrl->handler, struct ov5640_dev, in ctrl_to_sd()
267 ctrls.handler)->sd; in ctrl_to_sd()
272 * entries that set the register to their power-on default values,
545 /* power-on sensor init reg table */
604 struct i2c_client *client = sensor->i2c_client; in ov5640_init_slave_id()
609 if (client->addr == OV5640_DEFAULT_SLAVE_ID) in ov5640_init_slave_id()
614 buf[2] = client->addr << 1; in ov5640_init_slave_id()
621 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_init_slave_id()
623 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); in ov5640_init_slave_id()
632 struct i2c_client *client = sensor->i2c_client; in ov5640_write_reg()
641 msg.addr = client->addr; in ov5640_write_reg()
642 msg.flags = client->flags; in ov5640_write_reg()
646 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_write_reg()
648 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", in ov5640_write_reg()
658 struct i2c_client *client = sensor->i2c_client; in ov5640_read_reg()
666 msg[0].addr = client->addr; in ov5640_read_reg()
667 msg[0].flags = client->flags; in ov5640_read_reg()
671 msg[1].addr = client->addr; in ov5640_read_reg()
672 msg[1].flags = client->flags | I2C_M_RD; in ov5640_read_reg()
676 ret = i2c_transfer(client->adapter, msg, 2); in ov5640_read_reg()
678 dev_err(&client->dev, "%s: error: reg=%x\n", in ov5640_read_reg()
689 u8 hi, lo; in ov5640_read_reg16() local
692 ret = ov5640_read_reg(sensor, reg, &hi); in ov5640_read_reg16()
699 *val = ((u16)hi << 8) | (u16)lo; in ov5640_read_reg16()
736 * +--------------+
738 * +-+------------+
739 * | +----------+
740 * +->| PLL1 | - reg 0x3036, for the multiplier
741 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
742 * | +--------------+
743 * +->| System Clock | - reg 0x3035, bits 4-7
744 * +-+------------+
745 * | +--------------+
746 * +->| MIPI Divider | - reg 0x3035, bits 0-3
747 * | +-+------------+
748 * | +----------------> MIPI SCLK
749 * | + +-----+
750 * | +->| / 2 |-------> MIPI BIT CLK
751 * | +-----+
752 * | +--------------+
753 * +->| PLL Root Div | - reg 0x3037, bit 4
754 * +-+------------+
755 * | +---------+
756 * +->| Bit Div | - reg 0x3034, bits 0-3
757 * +-+-------+
758 * | +-------------+
759 * +->| SCLK Div | - reg 0x3108, bits 0-1
760 * | +-+-----------+
761 * | +---------------> SCLK
762 * | +-------------+
763 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
764 * | +-+-----------+
765 * | +---------------> SCLK 2X
766 * | +-------------+
767 * +->| PCLK Div | - reg 0x3108, bits 4-5
768 * ++------------+
769 * + +-----------+
770 * +->| P_DIV | - reg 0x3035, bits 0-3
771 * +-----+-----+
772 * +------------> PCLK
779 * - the PLL pre-divider output rate should be in the 4-27MHz range
780 * - the PLL multiplier output rate should be in the 500-1000MHz range
781 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
807 * Hardcode these values for scaler and non-scaler modes.
808 * FIXME: to be re-calcualted for 1 data lanes setups
821 * We only supports 8-bit formats at the moment
849 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; in ov5640_compute_sys_clk()
900 if (abs(rate - _rate) < abs(rate - best)) { in ov5640_calc_sys_clk()
920 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
921 * for the MIPI CSI-2 output.
928 * - sample_rate = bpl / (bpp / num_lanes);
931 * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
942 * - mipi_sclk = bpl / MIPI_DIV / 2;
948 * - sample_rate = bpl / (bpp / num_lanes);
951 * - MIPI_DIV = bpp / (4 * num_lanes);
954 * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
963 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mipi_pclk()
973 if (mode->dn_mode == SCALING || in ov5640_set_mipi_pclk()
974 (mode->id == OV5640_MODE_720P_1280_720)) in ov5640_set_mipi_pclk()
1050 0x1f, prediv | ((pll_rdiv - 1) << 4)); in ov5640_set_dvp_pclk()
1075 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact); in ov5640_set_jpeg_timings()
1079 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact); in ov5640_set_jpeg_timings()
1088 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { in ov5640_set_timings()
1094 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); in ov5640_set_timings()
1098 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); in ov5640_set_timings()
1102 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); in ov5640_set_timings()
1106 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); in ov5640_set_timings()
1112 const struct reg_value *regs = mode->reg_data; in ov5640_load_regs()
1119 for (i = 0; i < mode->reg_data_size; ++i, ++regs) { in ov5640_load_regs()
1120 delay_ms = regs->delay_ms; in ov5640_load_regs()
1121 reg_addr = regs->reg_addr; in ov5640_load_regs()
1122 val = regs->val; in ov5640_load_regs()
1123 mask = regs->mask; in ov5640_load_regs()
1126 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && in ov5640_load_regs()
1128 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY) in ov5640_load_regs()
1273 u32 xvclk = sensor->xclk_freq / 10000; in ov5640_get_sysclk()
1312 return -EINVAL; in ov5640_get_sysclk()
1413 return -EINVAL; in ov5640_set_bandingfilter()
1414 sensor->prev_sysclk = ret; in ov5640_set_bandingfilter()
1420 return -EINVAL; in ov5640_set_bandingfilter()
1421 sensor->prev_hts = ret; in ov5640_set_bandingfilter()
1431 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; in ov5640_set_bandingfilter()
1436 return -EINVAL; in ov5640_set_bandingfilter()
1437 max_band60 = (int)((prev_vts - 4) / band_step60); in ov5640_set_bandingfilter()
1443 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; in ov5640_set_bandingfilter()
1448 return -EINVAL; in ov5640_set_bandingfilter()
1449 max_band50 = (int)((prev_vts - 4) / band_step50); in ov5640_set_bandingfilter()
1459 sensor->ae_low = target * 23 / 25; /* 0.92 */ in ov5640_set_ae_target()
1460 sensor->ae_high = target * 27 / 25; /* 1.08 */ in ov5640_set_ae_target()
1462 fast_high = sensor->ae_high << 1; in ov5640_set_ae_target()
1466 fast_low = sensor->ae_low >> 1; in ov5640_set_ae_target()
1468 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); in ov5640_set_ae_target()
1471 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); in ov5640_set_ae_target()
1474 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); in ov5640_set_ae_target()
1477 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); in ov5640_set_ae_target()
1504 * - [0]: Horizontal binning enable in ov5640_set_binning()
1512 * - [0]: Undocumented, but hardcoded init sequences in ov5640_set_binning()
1521 struct i2c_client *client = sensor->i2c_client; in ov5640_set_virtual_channel()
1526 dev_err(&client->dev, in ov5640_set_virtual_channel()
1529 return -EINVAL; in ov5640_set_virtual_channel()
1552 (!nearest && (mode->hact != width || mode->vact != height))) in ov5640_find_mode()
1556 if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) in ov5640_find_mode()
1566 rate = sensor->current_mode->vtot * sensor->current_mode->htot; in ov5640_calc_pixel_rate()
1567 rate *= ov5640_framerates[sensor->current_fr]; in ov5640_calc_pixel_rate()
1587 if (!mode->reg_data) in ov5640_set_mode_exposure_calc()
1588 return -EINVAL; in ov5640_set_mode_exposure_calc()
1598 if (ret && mode->id != OV5640_MODE_720P_1280_720 && in ov5640_set_mode_exposure_calc()
1599 mode->id != OV5640_MODE_1080P_1920_1080) in ov5640_set_mode_exposure_calc()
1632 return -EINVAL; in ov5640_set_mode_exposure_calc()
1639 return -EINVAL; in ov5640_set_mode_exposure_calc()
1656 if (!sensor->prev_sysclk) { in ov5640_set_mode_exposure_calc()
1661 return -EINVAL; in ov5640_set_mode_exposure_calc()
1662 sensor->prev_sysclk = ret; in ov5640_set_mode_exposure_calc()
1666 return -EINVAL; in ov5640_set_mode_exposure_calc()
1668 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); in ov5640_set_mode_exposure_calc()
1671 if (average > sensor->ae_low && average < sensor->ae_high) { in ov5640_set_mode_exposure_calc()
1675 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1676 sensor->prev_hts / cap_hts * in ov5640_set_mode_exposure_calc()
1677 sensor->ae_target / average; in ov5640_set_mode_exposure_calc()
1681 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1682 sensor->prev_hts / cap_hts; in ov5640_set_mode_exposure_calc()
1700 return -EINVAL; in ov5640_set_mode_exposure_calc()
1709 return -EINVAL; in ov5640_set_mode_exposure_calc()
1721 if (cap_shutter > (cap_vts - 4)) { in ov5640_set_mode_exposure_calc()
1739 if (!mode->reg_data) in ov5640_set_mode_direct()
1740 return -EINVAL; in ov5640_set_mode_direct()
1748 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mode()
1749 const struct ov5640_mode_info *orig_mode = sensor->last_mode; in ov5640_set_mode()
1751 bool auto_gain = sensor->ctrls.auto_gain->val == 1; in ov5640_set_mode()
1752 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; in ov5640_set_mode()
1756 dn_mode = mode->dn_mode; in ov5640_set_mode()
1757 orig_dn_mode = orig_mode->dn_mode; in ov5640_set_mode()
1777 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) { in ov5640_set_mode()
1778 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; in ov5640_set_mode()
1781 rate = rate / sensor->ep.bus.parallel.bus_width; in ov5640_set_mode()
1814 ret = ov5640_set_ae_target(sensor, sensor->ae_target); in ov5640_set_mode()
1827 sensor->pending_mode_change = false; in ov5640_set_mode()
1828 sensor->last_mode = mode; in ov5640_set_mode()
1845 /* restore the last set video mode after chip power-on */
1854 sensor->last_mode = &ov5640_mode_init_data; in ov5640_restore_mode()
1867 return ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_restore_mode()
1872 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); in ov5640_power()
1877 if (!sensor->reset_gpio) in ov5640_reset()
1880 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1888 gpiod_set_value_cansleep(sensor->reset_gpio, 1); in ov5640_reset()
1891 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1897 struct i2c_client *client = sensor->i2c_client; in ov5640_set_power_on()
1900 ret = clk_prepare_enable(sensor->xclk); in ov5640_set_power_on()
1902 dev_err(&client->dev, "%s: failed to enable clock\n", in ov5640_set_power_on()
1908 sensor->supplies); in ov5640_set_power_on()
1910 dev_err(&client->dev, "%s: failed to enable regulators\n", in ov5640_set_power_on()
1926 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_on()
1928 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_on()
1935 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_off()
1936 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_off()
1996 unsigned int flags = sensor->ep.bus.parallel.flags; in ov5640_set_power_dvp()
2024 * - VSYNC: active high in ov5640_set_power_dvp()
2025 * - HREF: active low in ov5640_set_power_dvp()
2026 * - PCLK: active low in ov5640_set_power_dvp()
2032 * - [5]: PCLK polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2033 * - [1]: HREF polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2034 * - [0]: VSYNC polarity (mismatch here between in ov5640_set_power_dvp()
2038 if (sensor->ep.bus_type == V4L2_MBUS_PARALLEL) { in ov5640_set_power_dvp()
2071 * - 6: VSYNC output enable in ov5640_set_power_dvp()
2072 * - 5: HREF output enable in ov5640_set_power_dvp()
2073 * - 4: PCLK output enable in ov5640_set_power_dvp()
2074 * - [3:0]: D[9:6] output enable in ov5640_set_power_dvp()
2077 sensor->ep.bus_type == V4L2_MBUS_PARALLEL ? in ov5640_set_power_dvp()
2086 * - [7:2]: D[5:0] output enable in ov5640_set_power_dvp()
2105 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_set_power()
2122 /* --------------- Subdev Operations --------------- */
2129 mutex_lock(&sensor->lock); in ov5640_s_power()
2135 if (sensor->power_count == !on) { in ov5640_s_power()
2142 sensor->power_count += on ? 1 : -1; in ov5640_s_power()
2143 WARN_ON(sensor->power_count < 0); in ov5640_s_power()
2145 mutex_unlock(&sensor->lock); in ov5640_s_power()
2147 if (on && !ret && sensor->power_count == 1) { in ov5640_s_power()
2149 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); in ov5640_s_power()
2156 struct v4l2_fract *fi, in ov5640_try_frame_interval() argument
2167 if (fi->numerator == 0) { in ov5640_try_frame_interval()
2168 fi->denominator = maxfps; in ov5640_try_frame_interval()
2169 fi->numerator = 1; in ov5640_try_frame_interval()
2174 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), in ov5640_try_frame_interval()
2181 if (abs(curr_fps - fps) < abs(best_fps - fps)) { in ov5640_try_frame_interval()
2187 fi->numerator = 1; in ov5640_try_frame_interval()
2188 fi->denominator = best_fps; in ov5640_try_frame_interval()
2192 return mode ? rate : -EINVAL; in ov5640_try_frame_interval()
2202 if (format->pad != 0) in ov5640_get_fmt()
2203 return -EINVAL; in ov5640_get_fmt()
2205 mutex_lock(&sensor->lock); in ov5640_get_fmt()
2207 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_get_fmt()
2208 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, in ov5640_get_fmt()
2209 format->pad); in ov5640_get_fmt()
2211 fmt = &sensor->fmt; in ov5640_get_fmt()
2213 format->format = *fmt; in ov5640_get_fmt()
2215 mutex_unlock(&sensor->lock); in ov5640_get_fmt()
2229 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); in ov5640_try_fmt_internal()
2231 return -EINVAL; in ov5640_try_fmt_internal()
2232 fmt->width = mode->hact; in ov5640_try_fmt_internal()
2233 fmt->height = mode->vact; in ov5640_try_fmt_internal()
2239 if (ov5640_formats[i].code == fmt->code) in ov5640_try_fmt_internal()
2244 fmt->code = ov5640_formats[i].code; in ov5640_try_fmt_internal()
2245 fmt->colorspace = ov5640_formats[i].colorspace; in ov5640_try_fmt_internal()
2246 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2247 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_try_fmt_internal()
2248 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2259 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; in ov5640_set_fmt()
2263 if (format->pad != 0) in ov5640_set_fmt()
2264 return -EINVAL; in ov5640_set_fmt()
2266 mutex_lock(&sensor->lock); in ov5640_set_fmt()
2268 if (sensor->streaming) { in ov5640_set_fmt()
2269 ret = -EBUSY; in ov5640_set_fmt()
2274 sensor->current_fr, &new_mode); in ov5640_set_fmt()
2278 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_set_fmt()
2281 fmt = &sensor->fmt; in ov5640_set_fmt()
2285 if (new_mode != sensor->current_mode) { in ov5640_set_fmt()
2286 sensor->current_mode = new_mode; in ov5640_set_fmt()
2287 sensor->pending_mode_change = true; in ov5640_set_fmt()
2289 if (mbus_fmt->code != sensor->fmt.code) in ov5640_set_fmt()
2290 sensor->pending_fmt_change = true; in ov5640_set_fmt()
2292 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_set_fmt()
2295 mutex_unlock(&sensor->lock); in ov5640_set_fmt()
2306 switch (format->code) { in ov5640_set_framefmt()
2354 return -EINVAL; in ov5640_set_framefmt()
2369 * - [5]: JPEG enable in ov5640_set_framefmt()
2378 * - [4]: Reset JFIFO in ov5640_set_framefmt()
2379 * - [3]: Reset SFIFO in ov5640_set_framefmt()
2380 * - [2]: Reset JPEG in ov5640_set_framefmt()
2390 * - [5]: Enable JPEG 2x clock in ov5640_set_framefmt()
2391 * - [3]: Enable JPEG clock in ov5640_set_framefmt()
2469 u16 red = (u16)sensor->ctrls.red_balance->val; in ov5640_set_ctrl_white_balance()
2470 u16 blue = (u16)sensor->ctrls.blue_balance->val; in ov5640_set_ctrl_white_balance()
2484 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_exposure()
2488 if (ctrls->auto_exp->is_new) { in ov5640_set_ctrl_exposure()
2494 if (!auto_exp && ctrls->exposure->is_new) { in ov5640_set_ctrl_exposure()
2507 if (ctrls->exposure->val < max_exp) in ov5640_set_ctrl_exposure()
2508 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); in ov5640_set_ctrl_exposure()
2516 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_gain()
2519 if (ctrls->auto_gain->is_new) { in ov5640_set_ctrl_gain()
2525 if (!auto_gain && ctrls->gain->is_new) in ov5640_set_ctrl_gain()
2526 ret = ov5640_set_gain(sensor, ctrls->gain->val); in ov5640_set_ctrl_gain()
2596 * - [2]: ISP mirror in ov5640_set_ctrl_hflip()
2597 * - [1]: Sensor mirror in ov5640_set_ctrl_hflip()
2601 (!(value ^ sensor->upside_down)) ? in ov5640_set_ctrl_hflip()
2611 * - [2]: ISP vflip in ov5640_set_ctrl_vflip()
2612 * - [1]: Sensor vflip in ov5640_set_ctrl_vflip()
2616 (value ^ sensor->upside_down) ? in ov5640_set_ctrl_vflip()
2628 switch (ctrl->id) { in ov5640_g_volatile_ctrl()
2633 sensor->ctrls.gain->val = val; in ov5640_g_volatile_ctrl()
2639 sensor->ctrls.exposure->val = val; in ov5640_g_volatile_ctrl()
2657 * the controls will be restored right after power-up. in ov5640_s_ctrl()
2659 if (sensor->power_count == 0) in ov5640_s_ctrl()
2662 switch (ctrl->id) { in ov5640_s_ctrl()
2664 ret = ov5640_set_ctrl_gain(sensor, ctrl->val); in ov5640_s_ctrl()
2667 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); in ov5640_s_ctrl()
2670 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); in ov5640_s_ctrl()
2673 ret = ov5640_set_ctrl_hue(sensor, ctrl->val); in ov5640_s_ctrl()
2676 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); in ov5640_s_ctrl()
2679 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); in ov5640_s_ctrl()
2682 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); in ov5640_s_ctrl()
2685 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); in ov5640_s_ctrl()
2688 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); in ov5640_s_ctrl()
2691 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); in ov5640_s_ctrl()
2694 ret = -EINVAL; in ov5640_s_ctrl()
2709 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_init_controls()
2710 struct v4l2_ctrl_handler *hdl = &ctrls->handler; in ov5640_init_controls()
2716 hdl->lock = &sensor->lock; in ov5640_init_controls()
2719 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, in ov5640_init_controls()
2724 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, in ov5640_init_controls()
2727 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, in ov5640_init_controls()
2729 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, in ov5640_init_controls()
2732 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, in ov5640_init_controls()
2736 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, in ov5640_init_controls()
2739 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, in ov5640_init_controls()
2741 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, in ov5640_init_controls()
2744 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, in ov5640_init_controls()
2746 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, in ov5640_init_controls()
2748 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, in ov5640_init_controls()
2750 ctrls->test_pattern = in ov5640_init_controls()
2752 ARRAY_SIZE(test_pattern_menu) - 1, in ov5640_init_controls()
2754 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, in ov5640_init_controls()
2756 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, in ov5640_init_controls()
2759 ctrls->light_freq = in ov5640_init_controls()
2765 if (hdl->error) { in ov5640_init_controls()
2766 ret = hdl->error; in ov5640_init_controls()
2770 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
2771 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2772 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2774 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); in ov5640_init_controls()
2775 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); in ov5640_init_controls()
2776 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); in ov5640_init_controls()
2778 sensor->sd.ctrl_handler = hdl; in ov5640_init_controls()
2790 if (fse->pad != 0) in ov5640_enum_frame_size()
2791 return -EINVAL; in ov5640_enum_frame_size()
2792 if (fse->index >= OV5640_NUM_MODES) in ov5640_enum_frame_size()
2793 return -EINVAL; in ov5640_enum_frame_size()
2795 fse->min_width = in ov5640_enum_frame_size()
2796 ov5640_mode_data[fse->index].hact; in ov5640_enum_frame_size()
2797 fse->max_width = fse->min_width; in ov5640_enum_frame_size()
2798 fse->min_height = in ov5640_enum_frame_size()
2799 ov5640_mode_data[fse->index].vact; in ov5640_enum_frame_size()
2800 fse->max_height = fse->min_height; in ov5640_enum_frame_size()
2814 if (fie->pad != 0) in ov5640_enum_frame_interval()
2815 return -EINVAL; in ov5640_enum_frame_interval()
2816 if (fie->index >= OV5640_NUM_FRAMERATES) in ov5640_enum_frame_interval()
2817 return -EINVAL; in ov5640_enum_frame_interval()
2820 tpf.denominator = ov5640_framerates[fie->index]; in ov5640_enum_frame_interval()
2823 fie->width, fie->height); in ov5640_enum_frame_interval()
2825 return -EINVAL; in ov5640_enum_frame_interval()
2827 fie->interval = tpf; in ov5640_enum_frame_interval()
2832 struct v4l2_subdev_frame_interval *fi) in ov5640_g_frame_interval() argument
2836 mutex_lock(&sensor->lock); in ov5640_g_frame_interval()
2837 fi->interval = sensor->frame_interval; in ov5640_g_frame_interval()
2838 mutex_unlock(&sensor->lock); in ov5640_g_frame_interval()
2844 struct v4l2_subdev_frame_interval *fi) in ov5640_s_frame_interval() argument
2850 if (fi->pad != 0) in ov5640_s_frame_interval()
2851 return -EINVAL; in ov5640_s_frame_interval()
2853 mutex_lock(&sensor->lock); in ov5640_s_frame_interval()
2855 if (sensor->streaming) { in ov5640_s_frame_interval()
2856 ret = -EBUSY; in ov5640_s_frame_interval()
2860 mode = sensor->current_mode; in ov5640_s_frame_interval()
2862 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, in ov5640_s_frame_interval()
2863 mode->hact, mode->vact); in ov5640_s_frame_interval()
2866 fi->interval = sensor->frame_interval; in ov5640_s_frame_interval()
2870 mode = ov5640_find_mode(sensor, frame_rate, mode->hact, in ov5640_s_frame_interval()
2871 mode->vact, true); in ov5640_s_frame_interval()
2873 ret = -EINVAL; in ov5640_s_frame_interval()
2877 if (mode != sensor->current_mode || in ov5640_s_frame_interval()
2878 frame_rate != sensor->current_fr) { in ov5640_s_frame_interval()
2879 sensor->current_fr = frame_rate; in ov5640_s_frame_interval()
2880 sensor->frame_interval = fi->interval; in ov5640_s_frame_interval()
2881 sensor->current_mode = mode; in ov5640_s_frame_interval()
2882 sensor->pending_mode_change = true; in ov5640_s_frame_interval()
2884 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_s_frame_interval()
2888 mutex_unlock(&sensor->lock); in ov5640_s_frame_interval()
2896 if (code->pad != 0) in ov5640_enum_mbus_code()
2897 return -EINVAL; in ov5640_enum_mbus_code()
2898 if (code->index >= ARRAY_SIZE(ov5640_formats)) in ov5640_enum_mbus_code()
2899 return -EINVAL; in ov5640_enum_mbus_code()
2901 code->code = ov5640_formats[code->index].code; in ov5640_enum_mbus_code()
2910 mutex_lock(&sensor->lock); in ov5640_s_stream()
2912 if (sensor->streaming == !enable) { in ov5640_s_stream()
2913 if (enable && sensor->pending_mode_change) { in ov5640_s_stream()
2919 if (enable && sensor->pending_fmt_change) { in ov5640_s_stream()
2920 ret = ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_s_stream()
2923 sensor->pending_fmt_change = false; in ov5640_s_stream()
2926 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_s_stream()
2928 else if (sensor->ep.bus_type == V4L2_MBUS_BT656) in ov5640_s_stream()
2934 sensor->streaming = enable; in ov5640_s_stream()
2937 mutex_unlock(&sensor->lock); in ov5640_s_stream()
2973 sensor->supplies[i].supply = ov5640_supply_name[i]; in ov5640_get_regulators()
2975 return devm_regulator_bulk_get(&sensor->i2c_client->dev, in ov5640_get_regulators()
2977 sensor->supplies); in ov5640_get_regulators()
2982 struct i2c_client *client = sensor->i2c_client; in ov5640_check_chip_id()
2992 dev_err(&client->dev, "%s: failed to read chip identifier\n", in ov5640_check_chip_id()
2998 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", in ov5640_check_chip_id()
3000 ret = -ENXIO; in ov5640_check_chip_id()
3010 struct device *dev = &client->dev; in ov5640_probe()
3019 return -ENOMEM; in ov5640_probe()
3021 sensor->i2c_client = client; in ov5640_probe()
3027 fmt = &sensor->fmt; in ov5640_probe()
3028 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; in ov5640_probe()
3029 fmt->colorspace = V4L2_COLORSPACE_SRGB; in ov5640_probe()
3030 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_probe()
3031 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_probe()
3032 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_probe()
3033 fmt->width = 640; in ov5640_probe()
3034 fmt->height = 480; in ov5640_probe()
3035 fmt->field = V4L2_FIELD_NONE; in ov5640_probe()
3036 sensor->frame_interval.numerator = 1; in ov5640_probe()
3037 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; in ov5640_probe()
3038 sensor->current_fr = OV5640_30_FPS; in ov5640_probe()
3039 sensor->current_mode = in ov5640_probe()
3041 sensor->last_mode = sensor->current_mode; in ov5640_probe()
3043 sensor->ae_target = 52; in ov5640_probe()
3046 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", in ov5640_probe()
3051 sensor->upside_down = true; in ov5640_probe()
3061 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), in ov5640_probe()
3065 return -EINVAL; in ov5640_probe()
3068 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); in ov5640_probe()
3075 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && in ov5640_probe()
3076 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && in ov5640_probe()
3077 sensor->ep.bus_type != V4L2_MBUS_BT656) { in ov5640_probe()
3078 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); in ov5640_probe()
3079 return -EINVAL; in ov5640_probe()
3083 sensor->xclk = devm_clk_get(dev, "xclk"); in ov5640_probe()
3084 if (IS_ERR(sensor->xclk)) { in ov5640_probe()
3086 return PTR_ERR(sensor->xclk); in ov5640_probe()
3089 sensor->xclk_freq = clk_get_rate(sensor->xclk); in ov5640_probe()
3090 if (sensor->xclk_freq < OV5640_XCLK_MIN || in ov5640_probe()
3091 sensor->xclk_freq > OV5640_XCLK_MAX) { in ov5640_probe()
3093 sensor->xclk_freq); in ov5640_probe()
3094 return -EINVAL; in ov5640_probe()
3098 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", in ov5640_probe()
3100 if (IS_ERR(sensor->pwdn_gpio)) in ov5640_probe()
3101 return PTR_ERR(sensor->pwdn_gpio); in ov5640_probe()
3104 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", in ov5640_probe()
3106 if (IS_ERR(sensor->reset_gpio)) in ov5640_probe()
3107 return PTR_ERR(sensor->reset_gpio); in ov5640_probe()
3109 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); in ov5640_probe()
3111 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in ov5640_probe()
3113 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ov5640_probe()
3114 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov5640_probe()
3115 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ov5640_probe()
3123 mutex_init(&sensor->lock); in ov5640_probe()
3133 ret = v4l2_async_register_subdev_sensor_common(&sensor->sd); in ov5640_probe()
3140 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_probe()
3142 media_entity_cleanup(&sensor->sd.entity); in ov5640_probe()
3143 mutex_destroy(&sensor->lock); in ov5640_probe()
3152 v4l2_async_unregister_subdev(&sensor->sd); in ov5640_remove()
3153 media_entity_cleanup(&sensor->sd.entity); in ov5640_remove()
3154 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_remove()
3155 mutex_destroy(&sensor->lock); in ov5640_remove()