Lines Matching +full:lane +full:- +full:polarities
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>
151 * to set the MIPI CSI-2 virtual channel.
156 "MIPI CSI-2 virtual channel (0..3), default 0");
269 return &container_of(ctrl->handler, struct ov5640_dev, in ctrl_to_sd()
270 ctrls.handler)->sd; in ctrl_to_sd()
275 * entries that set the register to their power-on default values,
566 /* power-on sensor init reg table */
630 struct i2c_client *client = sensor->i2c_client; in ov5640_init_slave_id()
635 if (client->addr == OV5640_DEFAULT_SLAVE_ID) in ov5640_init_slave_id()
640 buf[2] = client->addr << 1; in ov5640_init_slave_id()
647 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_init_slave_id()
649 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); in ov5640_init_slave_id()
658 struct i2c_client *client = sensor->i2c_client; in ov5640_write_reg()
667 msg.addr = client->addr; in ov5640_write_reg()
668 msg.flags = client->flags; in ov5640_write_reg()
672 ret = i2c_transfer(client->adapter, &msg, 1); in ov5640_write_reg()
674 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", in ov5640_write_reg()
684 struct i2c_client *client = sensor->i2c_client; in ov5640_read_reg()
692 msg[0].addr = client->addr; in ov5640_read_reg()
693 msg[0].flags = client->flags; in ov5640_read_reg()
697 msg[1].addr = client->addr; in ov5640_read_reg()
698 msg[1].flags = client->flags | I2C_M_RD; in ov5640_read_reg()
702 ret = i2c_transfer(client->adapter, msg, 2); in ov5640_read_reg()
704 dev_err(&client->dev, "%s: error: reg=%x\n", in ov5640_read_reg()
762 * +--------------+
764 * +-+------------+
765 * | +----------+
766 * +->| PLL1 | - reg 0x3036, for the multiplier
767 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
768 * | +--------------+
769 * +->| System Clock | - reg 0x3035, bits 4-7
770 * +-+------------+
771 * | +--------------+
772 * +->| MIPI Divider | - reg 0x3035, bits 0-3
773 * | +-+------------+
774 * | +----------------> MIPI SCLK
775 * | + +-----+
776 * | +->| / 2 |-------> MIPI BIT CLK
777 * | +-----+
778 * | +--------------+
779 * +->| PLL Root Div | - reg 0x3037, bit 4
780 * +-+------------+
781 * | +---------+
782 * +->| Bit Div | - reg 0x3034, bits 0-3
783 * +-+-------+
784 * | +-------------+
785 * +->| SCLK Div | - reg 0x3108, bits 0-1
786 * | +-+-----------+
787 * | +---------------> SCLK
788 * | +-------------+
789 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
790 * | +-+-----------+
791 * | +---------------> SCLK 2X
792 * | +-------------+
793 * +->| PCLK Div | - reg 0x3108, bits 4-5
794 * ++------------+
795 * + +-----------+
796 * +->| P_DIV | - reg 0x3035, bits 0-3
797 * +-----+-----+
798 * +------------> PCLK
805 * - the PLL pre-divider output rate should be in the 4-27MHz range
806 * - the PLL multiplier output rate should be in the 500-1000MHz range
807 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
833 * Hardcode these values for scaler and non-scaler modes.
834 * FIXME: to be re-calcualted for 1 data lanes setups
847 * We only supports 8-bit formats at the moment
875 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; in ov5640_compute_sys_clk()
926 if (abs(rate - _rate) < abs(rate - best)) { in ov5640_calc_sys_clk()
946 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
947 * for the MIPI CSI-2 output.
949 * @rate: The requested bandwidth per lane in bytes per second.
950 * 'Bandwidth Per Lane' is calculated as:
954 * - sample_rate = bpl / (bpp / num_lanes);
957 * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
968 * - mipi_sclk = bpl / MIPI_DIV / 2;
974 * - sample_rate = bpl / (bpp / num_lanes);
977 * - MIPI_DIV = bpp / (4 * num_lanes);
980 * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
981 * above formula for setups with 1 lane or image formats with different bpp.
989 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mipi_pclk()
999 if (mode->dn_mode == SCALING || in ov5640_set_mipi_pclk()
1000 (mode->id == OV5640_MODE_720P_1280_720)) in ov5640_set_mipi_pclk()
1076 0x1f, prediv | ((pll_rdiv - 1) << 4)); in ov5640_set_dvp_pclk()
1101 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact); in ov5640_set_jpeg_timings()
1105 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact); in ov5640_set_jpeg_timings()
1114 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { in ov5640_set_timings()
1120 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); in ov5640_set_timings()
1124 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); in ov5640_set_timings()
1128 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); in ov5640_set_timings()
1132 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); in ov5640_set_timings()
1138 const struct reg_value *regs = mode->reg_data; in ov5640_load_regs()
1145 for (i = 0; i < mode->reg_data_size; ++i, ++regs) { in ov5640_load_regs()
1146 delay_ms = regs->delay_ms; in ov5640_load_regs()
1147 reg_addr = regs->reg_addr; in ov5640_load_regs()
1148 val = regs->val; in ov5640_load_regs()
1149 mask = regs->mask; in ov5640_load_regs()
1152 if (regs->reg_addr == OV5640_REG_SYS_CTRL0 && in ov5640_load_regs()
1154 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY) in ov5640_load_regs()
1262 * [7:5] = 000 : 1 data lane mode in ov5640_set_stream_mipi()
1285 u32 xvclk = sensor->xclk_freq / 10000; in ov5640_get_sysclk()
1324 return -EINVAL; in ov5640_get_sysclk()
1425 return -EINVAL; in ov5640_set_bandingfilter()
1426 sensor->prev_sysclk = ret; in ov5640_set_bandingfilter()
1432 return -EINVAL; in ov5640_set_bandingfilter()
1433 sensor->prev_hts = ret; in ov5640_set_bandingfilter()
1443 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; in ov5640_set_bandingfilter()
1448 return -EINVAL; in ov5640_set_bandingfilter()
1449 max_band60 = (int)((prev_vts - 4) / band_step60); in ov5640_set_bandingfilter()
1455 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; in ov5640_set_bandingfilter()
1460 return -EINVAL; in ov5640_set_bandingfilter()
1461 max_band50 = (int)((prev_vts - 4) / band_step50); in ov5640_set_bandingfilter()
1471 sensor->ae_low = target * 23 / 25; /* 0.92 */ in ov5640_set_ae_target()
1472 sensor->ae_high = target * 27 / 25; /* 1.08 */ in ov5640_set_ae_target()
1474 fast_high = sensor->ae_high << 1; in ov5640_set_ae_target()
1478 fast_low = sensor->ae_low >> 1; in ov5640_set_ae_target()
1480 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); in ov5640_set_ae_target()
1483 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); in ov5640_set_ae_target()
1486 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); in ov5640_set_ae_target()
1489 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); in ov5640_set_ae_target()
1516 * - [0]: Horizontal binning enable in ov5640_set_binning()
1524 * - [0]: Undocumented, but hardcoded init sequences in ov5640_set_binning()
1533 struct i2c_client *client = sensor->i2c_client; in ov5640_set_virtual_channel()
1538 dev_err(&client->dev, in ov5640_set_virtual_channel()
1541 return -EINVAL; in ov5640_set_virtual_channel()
1564 (!nearest && (mode->hact != width || mode->vact != height))) in ov5640_find_mode()
1568 if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) in ov5640_find_mode()
1578 rate = sensor->current_mode->vtot * sensor->current_mode->htot; in ov5640_calc_pixel_rate()
1579 rate *= ov5640_framerates[sensor->current_fr]; in ov5640_calc_pixel_rate()
1599 if (!mode->reg_data) in ov5640_set_mode_exposure_calc()
1600 return -EINVAL; in ov5640_set_mode_exposure_calc()
1610 if (ret && mode->id != OV5640_MODE_720P_1280_720 && in ov5640_set_mode_exposure_calc()
1611 mode->id != OV5640_MODE_1080P_1920_1080) in ov5640_set_mode_exposure_calc()
1644 return -EINVAL; in ov5640_set_mode_exposure_calc()
1651 return -EINVAL; in ov5640_set_mode_exposure_calc()
1668 if (!sensor->prev_sysclk) { in ov5640_set_mode_exposure_calc()
1673 return -EINVAL; in ov5640_set_mode_exposure_calc()
1674 sensor->prev_sysclk = ret; in ov5640_set_mode_exposure_calc()
1678 return -EINVAL; in ov5640_set_mode_exposure_calc()
1680 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); in ov5640_set_mode_exposure_calc()
1683 if (average > sensor->ae_low && average < sensor->ae_high) { in ov5640_set_mode_exposure_calc()
1687 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1688 sensor->prev_hts / cap_hts * in ov5640_set_mode_exposure_calc()
1689 sensor->ae_target / average; in ov5640_set_mode_exposure_calc()
1693 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1694 sensor->prev_hts / cap_hts; in ov5640_set_mode_exposure_calc()
1712 return -EINVAL; in ov5640_set_mode_exposure_calc()
1721 return -EINVAL; in ov5640_set_mode_exposure_calc()
1733 if (cap_shutter > (cap_vts - 4)) { in ov5640_set_mode_exposure_calc()
1751 if (!mode->reg_data) in ov5640_set_mode_direct()
1752 return -EINVAL; in ov5640_set_mode_direct()
1760 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mode()
1761 const struct ov5640_mode_info *orig_mode = sensor->last_mode; in ov5640_set_mode()
1763 bool auto_gain = sensor->ctrls.auto_gain->val == 1; in ov5640_set_mode()
1764 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; in ov5640_set_mode()
1768 dn_mode = mode->dn_mode; in ov5640_set_mode()
1769 orig_dn_mode = orig_mode->dn_mode; in ov5640_set_mode()
1789 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) { in ov5640_set_mode()
1790 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; in ov5640_set_mode()
1793 rate = rate / sensor->ep.bus.parallel.bus_width; in ov5640_set_mode()
1826 ret = ov5640_set_ae_target(sensor, sensor->ae_target); in ov5640_set_mode()
1839 sensor->pending_mode_change = false; in ov5640_set_mode()
1840 sensor->last_mode = mode; in ov5640_set_mode()
1857 /* restore the last set video mode after chip power-on */
1866 sensor->last_mode = &ov5640_mode_init_data; in ov5640_restore_mode()
1879 return ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_restore_mode()
1884 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); in ov5640_power()
1889 if (!sensor->reset_gpio) in ov5640_reset()
1892 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1900 gpiod_set_value_cansleep(sensor->reset_gpio, 1); in ov5640_reset()
1903 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1909 struct i2c_client *client = sensor->i2c_client; in ov5640_set_power_on()
1912 ret = clk_prepare_enable(sensor->xclk); in ov5640_set_power_on()
1914 dev_err(&client->dev, "%s: failed to enable clock\n", in ov5640_set_power_on()
1920 sensor->supplies); in ov5640_set_power_on()
1922 dev_err(&client->dev, "%s: failed to enable regulators\n", in ov5640_set_power_on()
1938 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_on()
1940 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_on()
1947 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_off()
1948 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_off()
1992 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping' in ov5640_set_power_mipi()
1993 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping' in ov5640_set_power_mipi()
1994 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping' in ov5640_set_power_mipi()
2008 unsigned int flags = sensor->ep.bus.parallel.flags; in ov5640_set_power_dvp()
2009 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; in ov5640_set_power_dvp()
2010 u8 polarities = 0; in ov5640_set_power_dvp() local
2036 * - VSYNC: active high in ov5640_set_power_dvp()
2037 * - HREF: active low in ov5640_set_power_dvp()
2038 * - PCLK: active low in ov5640_set_power_dvp()
2047 * - [7]: SYNC code selection (0: auto generate sync code, in ov5640_set_power_dvp()
2048 * 1: sync code from regs 0x4732-0x4735) in ov5640_set_power_dvp()
2049 * - [6]: f value in CCIR656 SYNC code when fixed f value in ov5640_set_power_dvp()
2050 * - [5]: Fixed f value in ov5640_set_power_dvp()
2051 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, in ov5640_set_power_dvp()
2052 * 01: data from regs 0x4736-0x4738, 10: always keep 0) in ov5640_set_power_dvp()
2053 * - [1]: Clip data disable in ov5640_set_power_dvp()
2054 * - [0]: CCIR656 mode enable in ov5640_set_power_dvp()
2058 * - CCIR656 mode enable in ov5640_set_power_dvp()
2059 * - auto generation of sync codes in ov5640_set_power_dvp()
2060 * - blank toggle data 1'h040/1'h200 in ov5640_set_power_dvp()
2061 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) in ov5640_set_power_dvp()
2072 * - [5]: PCLK polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2073 * - [1]: HREF polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2074 * - [0]: VSYNC polarity (mismatch here between in ov5640_set_power_dvp()
2080 polarities |= BIT(1); in ov5640_set_power_dvp()
2082 polarities |= BIT(0); in ov5640_set_power_dvp()
2085 polarities |= BIT(5); in ov5640_set_power_dvp()
2087 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities); in ov5640_set_power_dvp()
2108 * - 6: VSYNC output enable in ov5640_set_power_dvp()
2109 * - 5: HREF output enable in ov5640_set_power_dvp()
2110 * - 4: PCLK output enable in ov5640_set_power_dvp()
2111 * - [3:0]: D[9:6] output enable in ov5640_set_power_dvp()
2122 * - [7:2]: D[5:0] output enable in ov5640_set_power_dvp()
2141 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_set_power()
2158 /* --------------- Subdev Operations --------------- */
2165 mutex_lock(&sensor->lock); in ov5640_s_power()
2171 if (sensor->power_count == !on) { in ov5640_s_power()
2178 sensor->power_count += on ? 1 : -1; in ov5640_s_power()
2179 WARN_ON(sensor->power_count < 0); in ov5640_s_power()
2181 mutex_unlock(&sensor->lock); in ov5640_s_power()
2183 if (on && !ret && sensor->power_count == 1) { in ov5640_s_power()
2185 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); in ov5640_s_power()
2203 if (fi->numerator == 0) { in ov5640_try_frame_interval()
2204 fi->denominator = maxfps; in ov5640_try_frame_interval()
2205 fi->numerator = 1; in ov5640_try_frame_interval()
2210 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), in ov5640_try_frame_interval()
2217 if (abs(curr_fps - fps) < abs(best_fps - fps)) { in ov5640_try_frame_interval()
2223 fi->numerator = 1; in ov5640_try_frame_interval()
2224 fi->denominator = best_fps; in ov5640_try_frame_interval()
2228 return mode ? rate : -EINVAL; in ov5640_try_frame_interval()
2238 if (format->pad != 0) in ov5640_get_fmt()
2239 return -EINVAL; in ov5640_get_fmt()
2241 mutex_lock(&sensor->lock); in ov5640_get_fmt()
2243 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_get_fmt()
2244 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state, in ov5640_get_fmt()
2245 format->pad); in ov5640_get_fmt()
2247 fmt = &sensor->fmt; in ov5640_get_fmt()
2249 format->format = *fmt; in ov5640_get_fmt()
2251 mutex_unlock(&sensor->lock); in ov5640_get_fmt()
2265 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); in ov5640_try_fmt_internal()
2267 return -EINVAL; in ov5640_try_fmt_internal()
2268 fmt->width = mode->hact; in ov5640_try_fmt_internal()
2269 fmt->height = mode->vact; in ov5640_try_fmt_internal()
2275 if (ov5640_formats[i].code == fmt->code) in ov5640_try_fmt_internal()
2280 fmt->code = ov5640_formats[i].code; in ov5640_try_fmt_internal()
2281 fmt->colorspace = ov5640_formats[i].colorspace; in ov5640_try_fmt_internal()
2282 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2283 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_try_fmt_internal()
2284 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2295 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; in ov5640_set_fmt()
2299 if (format->pad != 0) in ov5640_set_fmt()
2300 return -EINVAL; in ov5640_set_fmt()
2302 mutex_lock(&sensor->lock); in ov5640_set_fmt()
2304 if (sensor->streaming) { in ov5640_set_fmt()
2305 ret = -EBUSY; in ov5640_set_fmt()
2310 sensor->current_fr, &new_mode); in ov5640_set_fmt()
2314 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_set_fmt()
2317 fmt = &sensor->fmt; in ov5640_set_fmt()
2321 if (new_mode != sensor->current_mode) { in ov5640_set_fmt()
2322 sensor->current_mode = new_mode; in ov5640_set_fmt()
2323 sensor->pending_mode_change = true; in ov5640_set_fmt()
2325 if (mbus_fmt->code != sensor->fmt.code) in ov5640_set_fmt()
2326 sensor->pending_fmt_change = true; in ov5640_set_fmt()
2328 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_set_fmt()
2331 mutex_unlock(&sensor->lock); in ov5640_set_fmt()
2342 switch (format->code) { in ov5640_set_framefmt()
2392 return -EINVAL; in ov5640_set_framefmt()
2407 * - [5]: JPEG enable in ov5640_set_framefmt()
2416 * - [4]: Reset JFIFO in ov5640_set_framefmt()
2417 * - [3]: Reset SFIFO in ov5640_set_framefmt()
2418 * - [2]: Reset JPEG in ov5640_set_framefmt()
2428 * - [5]: Enable JPEG 2x clock in ov5640_set_framefmt()
2429 * - [3]: Enable JPEG clock in ov5640_set_framefmt()
2507 u16 red = (u16)sensor->ctrls.red_balance->val; in ov5640_set_ctrl_white_balance()
2508 u16 blue = (u16)sensor->ctrls.blue_balance->val; in ov5640_set_ctrl_white_balance()
2522 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_exposure()
2526 if (ctrls->auto_exp->is_new) { in ov5640_set_ctrl_exposure()
2532 if (!auto_exp && ctrls->exposure->is_new) { in ov5640_set_ctrl_exposure()
2545 if (ctrls->exposure->val < max_exp) in ov5640_set_ctrl_exposure()
2546 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); in ov5640_set_ctrl_exposure()
2554 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_gain()
2557 if (ctrls->auto_gain->is_new) { in ov5640_set_ctrl_gain()
2563 if (!auto_gain && ctrls->gain->is_new) in ov5640_set_ctrl_gain()
2564 ret = ov5640_set_gain(sensor, ctrls->gain->val); in ov5640_set_ctrl_gain()
2634 * - [2]: ISP mirror in ov5640_set_ctrl_hflip()
2635 * - [1]: Sensor mirror in ov5640_set_ctrl_hflip()
2639 (!(value ^ sensor->upside_down)) ? in ov5640_set_ctrl_hflip()
2649 * - [2]: ISP vflip in ov5640_set_ctrl_vflip()
2650 * - [1]: Sensor vflip in ov5640_set_ctrl_vflip()
2654 (value ^ sensor->upside_down) ? in ov5640_set_ctrl_vflip()
2666 switch (ctrl->id) { in ov5640_g_volatile_ctrl()
2671 sensor->ctrls.gain->val = val; in ov5640_g_volatile_ctrl()
2677 sensor->ctrls.exposure->val = val; in ov5640_g_volatile_ctrl()
2695 * the controls will be restored right after power-up. in ov5640_s_ctrl()
2697 if (sensor->power_count == 0) in ov5640_s_ctrl()
2700 switch (ctrl->id) { in ov5640_s_ctrl()
2702 ret = ov5640_set_ctrl_gain(sensor, ctrl->val); in ov5640_s_ctrl()
2705 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); in ov5640_s_ctrl()
2708 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); in ov5640_s_ctrl()
2711 ret = ov5640_set_ctrl_hue(sensor, ctrl->val); in ov5640_s_ctrl()
2714 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); in ov5640_s_ctrl()
2717 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); in ov5640_s_ctrl()
2720 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); in ov5640_s_ctrl()
2723 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); in ov5640_s_ctrl()
2726 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); in ov5640_s_ctrl()
2729 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); in ov5640_s_ctrl()
2732 ret = -EINVAL; in ov5640_s_ctrl()
2747 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_init_controls()
2748 struct v4l2_ctrl_handler *hdl = &ctrls->handler; in ov5640_init_controls()
2754 hdl->lock = &sensor->lock; in ov5640_init_controls()
2757 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, in ov5640_init_controls()
2762 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, in ov5640_init_controls()
2765 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, in ov5640_init_controls()
2767 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, in ov5640_init_controls()
2770 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, in ov5640_init_controls()
2774 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, in ov5640_init_controls()
2777 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, in ov5640_init_controls()
2779 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, in ov5640_init_controls()
2782 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, in ov5640_init_controls()
2784 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, in ov5640_init_controls()
2786 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, in ov5640_init_controls()
2788 ctrls->test_pattern = in ov5640_init_controls()
2790 ARRAY_SIZE(test_pattern_menu) - 1, in ov5640_init_controls()
2792 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, in ov5640_init_controls()
2794 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, in ov5640_init_controls()
2797 ctrls->light_freq = in ov5640_init_controls()
2803 if (hdl->error) { in ov5640_init_controls()
2804 ret = hdl->error; in ov5640_init_controls()
2808 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
2809 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2810 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2812 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); in ov5640_init_controls()
2813 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); in ov5640_init_controls()
2814 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); in ov5640_init_controls()
2816 sensor->sd.ctrl_handler = hdl; in ov5640_init_controls()
2828 if (fse->pad != 0) in ov5640_enum_frame_size()
2829 return -EINVAL; in ov5640_enum_frame_size()
2830 if (fse->index >= OV5640_NUM_MODES) in ov5640_enum_frame_size()
2831 return -EINVAL; in ov5640_enum_frame_size()
2833 fse->min_width = in ov5640_enum_frame_size()
2834 ov5640_mode_data[fse->index].hact; in ov5640_enum_frame_size()
2835 fse->max_width = fse->min_width; in ov5640_enum_frame_size()
2836 fse->min_height = in ov5640_enum_frame_size()
2837 ov5640_mode_data[fse->index].vact; in ov5640_enum_frame_size()
2838 fse->max_height = fse->min_height; in ov5640_enum_frame_size()
2852 if (fie->pad != 0) in ov5640_enum_frame_interval()
2853 return -EINVAL; in ov5640_enum_frame_interval()
2854 if (fie->index >= OV5640_NUM_FRAMERATES) in ov5640_enum_frame_interval()
2855 return -EINVAL; in ov5640_enum_frame_interval()
2858 tpf.denominator = ov5640_framerates[fie->index]; in ov5640_enum_frame_interval()
2861 fie->width, fie->height); in ov5640_enum_frame_interval()
2863 return -EINVAL; in ov5640_enum_frame_interval()
2865 fie->interval = tpf; in ov5640_enum_frame_interval()
2874 mutex_lock(&sensor->lock); in ov5640_g_frame_interval()
2875 fi->interval = sensor->frame_interval; in ov5640_g_frame_interval()
2876 mutex_unlock(&sensor->lock); in ov5640_g_frame_interval()
2888 if (fi->pad != 0) in ov5640_s_frame_interval()
2889 return -EINVAL; in ov5640_s_frame_interval()
2891 mutex_lock(&sensor->lock); in ov5640_s_frame_interval()
2893 if (sensor->streaming) { in ov5640_s_frame_interval()
2894 ret = -EBUSY; in ov5640_s_frame_interval()
2898 mode = sensor->current_mode; in ov5640_s_frame_interval()
2900 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, in ov5640_s_frame_interval()
2901 mode->hact, mode->vact); in ov5640_s_frame_interval()
2904 fi->interval = sensor->frame_interval; in ov5640_s_frame_interval()
2908 mode = ov5640_find_mode(sensor, frame_rate, mode->hact, in ov5640_s_frame_interval()
2909 mode->vact, true); in ov5640_s_frame_interval()
2911 ret = -EINVAL; in ov5640_s_frame_interval()
2915 if (mode != sensor->current_mode || in ov5640_s_frame_interval()
2916 frame_rate != sensor->current_fr) { in ov5640_s_frame_interval()
2917 sensor->current_fr = frame_rate; in ov5640_s_frame_interval()
2918 sensor->frame_interval = fi->interval; in ov5640_s_frame_interval()
2919 sensor->current_mode = mode; in ov5640_s_frame_interval()
2920 sensor->pending_mode_change = true; in ov5640_s_frame_interval()
2922 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_s_frame_interval()
2926 mutex_unlock(&sensor->lock); in ov5640_s_frame_interval()
2934 if (code->pad != 0) in ov5640_enum_mbus_code()
2935 return -EINVAL; in ov5640_enum_mbus_code()
2936 if (code->index >= ARRAY_SIZE(ov5640_formats)) in ov5640_enum_mbus_code()
2937 return -EINVAL; in ov5640_enum_mbus_code()
2939 code->code = ov5640_formats[code->index].code; in ov5640_enum_mbus_code()
2948 mutex_lock(&sensor->lock); in ov5640_s_stream()
2950 if (sensor->streaming == !enable) { in ov5640_s_stream()
2951 if (enable && sensor->pending_mode_change) { in ov5640_s_stream()
2957 if (enable && sensor->pending_fmt_change) { in ov5640_s_stream()
2958 ret = ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_s_stream()
2961 sensor->pending_fmt_change = false; in ov5640_s_stream()
2964 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_s_stream()
2970 sensor->streaming = enable; in ov5640_s_stream()
2973 mutex_unlock(&sensor->lock); in ov5640_s_stream()
3009 sensor->supplies[i].supply = ov5640_supply_name[i]; in ov5640_get_regulators()
3011 return devm_regulator_bulk_get(&sensor->i2c_client->dev, in ov5640_get_regulators()
3013 sensor->supplies); in ov5640_get_regulators()
3018 struct i2c_client *client = sensor->i2c_client; in ov5640_check_chip_id()
3028 dev_err(&client->dev, "%s: failed to read chip identifier\n", in ov5640_check_chip_id()
3034 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", in ov5640_check_chip_id()
3036 ret = -ENXIO; in ov5640_check_chip_id()
3046 struct device *dev = &client->dev; in ov5640_probe()
3055 return -ENOMEM; in ov5640_probe()
3057 sensor->i2c_client = client; in ov5640_probe()
3063 fmt = &sensor->fmt; in ov5640_probe()
3064 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; in ov5640_probe()
3065 fmt->colorspace = V4L2_COLORSPACE_SRGB; in ov5640_probe()
3066 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_probe()
3067 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_probe()
3068 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_probe()
3069 fmt->width = 640; in ov5640_probe()
3070 fmt->height = 480; in ov5640_probe()
3071 fmt->field = V4L2_FIELD_NONE; in ov5640_probe()
3072 sensor->frame_interval.numerator = 1; in ov5640_probe()
3073 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; in ov5640_probe()
3074 sensor->current_fr = OV5640_30_FPS; in ov5640_probe()
3075 sensor->current_mode = in ov5640_probe()
3077 sensor->last_mode = sensor->current_mode; in ov5640_probe()
3079 sensor->ae_target = 52; in ov5640_probe()
3082 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", in ov5640_probe()
3087 sensor->upside_down = true; in ov5640_probe()
3097 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), in ov5640_probe()
3101 return -EINVAL; in ov5640_probe()
3104 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); in ov5640_probe()
3111 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && in ov5640_probe()
3112 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && in ov5640_probe()
3113 sensor->ep.bus_type != V4L2_MBUS_BT656) { in ov5640_probe()
3114 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); in ov5640_probe()
3115 return -EINVAL; in ov5640_probe()
3119 sensor->xclk = devm_clk_get(dev, "xclk"); in ov5640_probe()
3120 if (IS_ERR(sensor->xclk)) { in ov5640_probe()
3122 return PTR_ERR(sensor->xclk); in ov5640_probe()
3125 sensor->xclk_freq = clk_get_rate(sensor->xclk); in ov5640_probe()
3126 if (sensor->xclk_freq < OV5640_XCLK_MIN || in ov5640_probe()
3127 sensor->xclk_freq > OV5640_XCLK_MAX) { in ov5640_probe()
3129 sensor->xclk_freq); in ov5640_probe()
3130 return -EINVAL; in ov5640_probe()
3134 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", in ov5640_probe()
3136 if (IS_ERR(sensor->pwdn_gpio)) in ov5640_probe()
3137 return PTR_ERR(sensor->pwdn_gpio); in ov5640_probe()
3140 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", in ov5640_probe()
3142 if (IS_ERR(sensor->reset_gpio)) in ov5640_probe()
3143 return PTR_ERR(sensor->reset_gpio); in ov5640_probe()
3145 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); in ov5640_probe()
3147 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in ov5640_probe()
3149 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ov5640_probe()
3150 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov5640_probe()
3151 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ov5640_probe()
3159 mutex_init(&sensor->lock); in ov5640_probe()
3169 ret = v4l2_async_register_subdev_sensor(&sensor->sd); in ov5640_probe()
3176 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_probe()
3178 media_entity_cleanup(&sensor->sd.entity); in ov5640_probe()
3179 mutex_destroy(&sensor->lock); in ov5640_probe()
3188 v4l2_async_unregister_subdev(&sensor->sd); in ov5640_remove()
3189 media_entity_cleanup(&sensor->sd.entity); in ov5640_remove()
3190 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_remove()
3191 mutex_destroy(&sensor->lock); in ov5640_remove()