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()
1259 u32 xvclk = sensor->xclk_freq / 10000; in ov5640_get_sysclk()
1298 return -EINVAL; in ov5640_get_sysclk()
1399 return -EINVAL; in ov5640_set_bandingfilter()
1400 sensor->prev_sysclk = ret; in ov5640_set_bandingfilter()
1406 return -EINVAL; in ov5640_set_bandingfilter()
1407 sensor->prev_hts = ret; in ov5640_set_bandingfilter()
1417 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; in ov5640_set_bandingfilter()
1422 return -EINVAL; in ov5640_set_bandingfilter()
1423 max_band60 = (int)((prev_vts - 4) / band_step60); in ov5640_set_bandingfilter()
1429 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; in ov5640_set_bandingfilter()
1434 return -EINVAL; in ov5640_set_bandingfilter()
1435 max_band50 = (int)((prev_vts - 4) / band_step50); in ov5640_set_bandingfilter()
1445 sensor->ae_low = target * 23 / 25; /* 0.92 */ in ov5640_set_ae_target()
1446 sensor->ae_high = target * 27 / 25; /* 1.08 */ in ov5640_set_ae_target()
1448 fast_high = sensor->ae_high << 1; in ov5640_set_ae_target()
1452 fast_low = sensor->ae_low >> 1; in ov5640_set_ae_target()
1454 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); in ov5640_set_ae_target()
1457 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); in ov5640_set_ae_target()
1460 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); in ov5640_set_ae_target()
1463 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); in ov5640_set_ae_target()
1490 * - [0]: Horizontal binning enable in ov5640_set_binning()
1498 * - [0]: Undocumented, but hardcoded init sequences in ov5640_set_binning()
1507 struct i2c_client *client = sensor->i2c_client; in ov5640_set_virtual_channel()
1512 dev_err(&client->dev, in ov5640_set_virtual_channel()
1515 return -EINVAL; in ov5640_set_virtual_channel()
1538 (!nearest && (mode->hact != width || mode->vact != height))) in ov5640_find_mode()
1542 if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps]) in ov5640_find_mode()
1552 rate = sensor->current_mode->vtot * sensor->current_mode->htot; in ov5640_calc_pixel_rate()
1553 rate *= ov5640_framerates[sensor->current_fr]; in ov5640_calc_pixel_rate()
1573 if (!mode->reg_data) in ov5640_set_mode_exposure_calc()
1574 return -EINVAL; in ov5640_set_mode_exposure_calc()
1584 if (ret && mode->id != OV5640_MODE_720P_1280_720 && in ov5640_set_mode_exposure_calc()
1585 mode->id != OV5640_MODE_1080P_1920_1080) in ov5640_set_mode_exposure_calc()
1618 return -EINVAL; in ov5640_set_mode_exposure_calc()
1625 return -EINVAL; in ov5640_set_mode_exposure_calc()
1642 if (!sensor->prev_sysclk) { in ov5640_set_mode_exposure_calc()
1647 return -EINVAL; in ov5640_set_mode_exposure_calc()
1648 sensor->prev_sysclk = ret; in ov5640_set_mode_exposure_calc()
1652 return -EINVAL; in ov5640_set_mode_exposure_calc()
1654 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); in ov5640_set_mode_exposure_calc()
1657 if (average > sensor->ae_low && average < sensor->ae_high) { in ov5640_set_mode_exposure_calc()
1661 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1662 sensor->prev_hts / cap_hts * in ov5640_set_mode_exposure_calc()
1663 sensor->ae_target / average; in ov5640_set_mode_exposure_calc()
1667 cap_sysclk / sensor->prev_sysclk * in ov5640_set_mode_exposure_calc()
1668 sensor->prev_hts / cap_hts; in ov5640_set_mode_exposure_calc()
1686 return -EINVAL; in ov5640_set_mode_exposure_calc()
1695 return -EINVAL; in ov5640_set_mode_exposure_calc()
1707 if (cap_shutter > (cap_vts - 4)) { in ov5640_set_mode_exposure_calc()
1725 if (!mode->reg_data) in ov5640_set_mode_direct()
1726 return -EINVAL; in ov5640_set_mode_direct()
1734 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mode()
1735 const struct ov5640_mode_info *orig_mode = sensor->last_mode; in ov5640_set_mode()
1737 bool auto_gain = sensor->ctrls.auto_gain->val == 1; in ov5640_set_mode()
1738 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO; in ov5640_set_mode()
1742 dn_mode = mode->dn_mode; in ov5640_set_mode()
1743 orig_dn_mode = orig_mode->dn_mode; in ov5640_set_mode()
1763 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) { in ov5640_set_mode()
1764 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes; in ov5640_set_mode()
1767 rate = rate / sensor->ep.bus.parallel.bus_width; in ov5640_set_mode()
1800 ret = ov5640_set_ae_target(sensor, sensor->ae_target); in ov5640_set_mode()
1813 sensor->pending_mode_change = false; in ov5640_set_mode()
1814 sensor->last_mode = mode; in ov5640_set_mode()
1831 /* restore the last set video mode after chip power-on */
1840 sensor->last_mode = &ov5640_mode_init_data; in ov5640_restore_mode()
1853 return ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_restore_mode()
1858 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); in ov5640_power()
1863 if (!sensor->reset_gpio) in ov5640_reset()
1866 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1874 gpiod_set_value_cansleep(sensor->reset_gpio, 1); in ov5640_reset()
1877 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1883 struct i2c_client *client = sensor->i2c_client; in ov5640_set_power_on()
1886 ret = clk_prepare_enable(sensor->xclk); in ov5640_set_power_on()
1888 dev_err(&client->dev, "%s: failed to enable clock\n", in ov5640_set_power_on()
1894 sensor->supplies); in ov5640_set_power_on()
1896 dev_err(&client->dev, "%s: failed to enable regulators\n", in ov5640_set_power_on()
1912 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_on()
1914 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_on()
1921 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); in ov5640_set_power_off()
1922 clk_disable_unprepare(sensor->xclk); in ov5640_set_power_off()
1982 unsigned int flags = sensor->ep.bus.parallel.flags; in ov5640_set_power_dvp()
1983 bool bt656 = sensor->ep.bus_type == V4L2_MBUS_BT656; in ov5640_set_power_dvp()
2010 * - VSYNC: active high in ov5640_set_power_dvp()
2011 * - HREF: active low in ov5640_set_power_dvp()
2012 * - PCLK: active low in ov5640_set_power_dvp()
2021 * - [7]: SYNC code selection (0: auto generate sync code, in ov5640_set_power_dvp()
2022 * 1: sync code from regs 0x4732-0x4735) in ov5640_set_power_dvp()
2023 * - [6]: f value in CCIR656 SYNC code when fixed f value in ov5640_set_power_dvp()
2024 * - [5]: Fixed f value in ov5640_set_power_dvp()
2025 * - [4:3]: Blank toggle data options (00: data=1'h040/1'h200, in ov5640_set_power_dvp()
2026 * 01: data from regs 0x4736-0x4738, 10: always keep 0) in ov5640_set_power_dvp()
2027 * - [1]: Clip data disable in ov5640_set_power_dvp()
2028 * - [0]: CCIR656 mode enable in ov5640_set_power_dvp()
2032 * - CCIR656 mode enable in ov5640_set_power_dvp()
2033 * - auto generation of sync codes in ov5640_set_power_dvp()
2034 * - blank toggle data 1'h040/1'h200 in ov5640_set_power_dvp()
2035 * - clip reserved data (0x00 & 0xff changed to 0x01 & 0xfe) in ov5640_set_power_dvp()
2046 * - [5]: PCLK polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2047 * - [1]: HREF polarity (0: active low, 1: active high) in ov5640_set_power_dvp()
2048 * - [0]: VSYNC polarity (mismatch here between in ov5640_set_power_dvp()
2082 * - 6: VSYNC output enable in ov5640_set_power_dvp()
2083 * - 5: HREF output enable in ov5640_set_power_dvp()
2084 * - 4: PCLK output enable in ov5640_set_power_dvp()
2085 * - [3:0]: D[9:6] output enable in ov5640_set_power_dvp()
2096 * - [7:2]: D[5:0] output enable in ov5640_set_power_dvp()
2115 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_set_power()
2132 /* --------------- Subdev Operations --------------- */
2139 mutex_lock(&sensor->lock); in ov5640_s_power()
2145 if (sensor->power_count == !on) { in ov5640_s_power()
2152 sensor->power_count += on ? 1 : -1; in ov5640_s_power()
2153 WARN_ON(sensor->power_count < 0); in ov5640_s_power()
2155 mutex_unlock(&sensor->lock); in ov5640_s_power()
2157 if (on && !ret && sensor->power_count == 1) { in ov5640_s_power()
2159 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); in ov5640_s_power()
2166 struct v4l2_fract *fi, in ov5640_try_frame_interval() argument
2177 if (fi->numerator == 0) { in ov5640_try_frame_interval()
2178 fi->denominator = maxfps; in ov5640_try_frame_interval()
2179 fi->numerator = 1; in ov5640_try_frame_interval()
2184 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator), in ov5640_try_frame_interval()
2191 if (abs(curr_fps - fps) < abs(best_fps - fps)) { in ov5640_try_frame_interval()
2197 fi->numerator = 1; in ov5640_try_frame_interval()
2198 fi->denominator = best_fps; in ov5640_try_frame_interval()
2202 return mode ? rate : -EINVAL; in ov5640_try_frame_interval()
2212 if (format->pad != 0) in ov5640_get_fmt()
2213 return -EINVAL; in ov5640_get_fmt()
2215 mutex_lock(&sensor->lock); in ov5640_get_fmt()
2217 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_get_fmt()
2218 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, in ov5640_get_fmt()
2219 format->pad); in ov5640_get_fmt()
2221 fmt = &sensor->fmt; in ov5640_get_fmt()
2223 format->format = *fmt; in ov5640_get_fmt()
2225 mutex_unlock(&sensor->lock); in ov5640_get_fmt()
2239 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); in ov5640_try_fmt_internal()
2241 return -EINVAL; in ov5640_try_fmt_internal()
2242 fmt->width = mode->hact; in ov5640_try_fmt_internal()
2243 fmt->height = mode->vact; in ov5640_try_fmt_internal()
2249 if (ov5640_formats[i].code == fmt->code) in ov5640_try_fmt_internal()
2254 fmt->code = ov5640_formats[i].code; in ov5640_try_fmt_internal()
2255 fmt->colorspace = ov5640_formats[i].colorspace; in ov5640_try_fmt_internal()
2256 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2257 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_try_fmt_internal()
2258 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_try_fmt_internal()
2269 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; in ov5640_set_fmt()
2273 if (format->pad != 0) in ov5640_set_fmt()
2274 return -EINVAL; in ov5640_set_fmt()
2276 mutex_lock(&sensor->lock); in ov5640_set_fmt()
2278 if (sensor->streaming) { in ov5640_set_fmt()
2279 ret = -EBUSY; in ov5640_set_fmt()
2284 sensor->current_fr, &new_mode); in ov5640_set_fmt()
2288 if (format->which == V4L2_SUBDEV_FORMAT_TRY) in ov5640_set_fmt()
2291 fmt = &sensor->fmt; in ov5640_set_fmt()
2295 if (new_mode != sensor->current_mode) { in ov5640_set_fmt()
2296 sensor->current_mode = new_mode; in ov5640_set_fmt()
2297 sensor->pending_mode_change = true; in ov5640_set_fmt()
2299 if (mbus_fmt->code != sensor->fmt.code) in ov5640_set_fmt()
2300 sensor->pending_fmt_change = true; in ov5640_set_fmt()
2302 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_set_fmt()
2305 mutex_unlock(&sensor->lock); in ov5640_set_fmt()
2316 switch (format->code) { in ov5640_set_framefmt()
2364 return -EINVAL; in ov5640_set_framefmt()
2379 * - [5]: JPEG enable in ov5640_set_framefmt()
2388 * - [4]: Reset JFIFO in ov5640_set_framefmt()
2389 * - [3]: Reset SFIFO in ov5640_set_framefmt()
2390 * - [2]: Reset JPEG in ov5640_set_framefmt()
2400 * - [5]: Enable JPEG 2x clock in ov5640_set_framefmt()
2401 * - [3]: Enable JPEG clock in ov5640_set_framefmt()
2479 u16 red = (u16)sensor->ctrls.red_balance->val; in ov5640_set_ctrl_white_balance()
2480 u16 blue = (u16)sensor->ctrls.blue_balance->val; in ov5640_set_ctrl_white_balance()
2494 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_exposure()
2498 if (ctrls->auto_exp->is_new) { in ov5640_set_ctrl_exposure()
2504 if (!auto_exp && ctrls->exposure->is_new) { in ov5640_set_ctrl_exposure()
2517 if (ctrls->exposure->val < max_exp) in ov5640_set_ctrl_exposure()
2518 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); in ov5640_set_ctrl_exposure()
2526 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_gain()
2529 if (ctrls->auto_gain->is_new) { in ov5640_set_ctrl_gain()
2535 if (!auto_gain && ctrls->gain->is_new) in ov5640_set_ctrl_gain()
2536 ret = ov5640_set_gain(sensor, ctrls->gain->val); in ov5640_set_ctrl_gain()
2606 * - [2]: ISP mirror in ov5640_set_ctrl_hflip()
2607 * - [1]: Sensor mirror in ov5640_set_ctrl_hflip()
2611 (!(value ^ sensor->upside_down)) ? in ov5640_set_ctrl_hflip()
2621 * - [2]: ISP vflip in ov5640_set_ctrl_vflip()
2622 * - [1]: Sensor vflip in ov5640_set_ctrl_vflip()
2626 (value ^ sensor->upside_down) ? in ov5640_set_ctrl_vflip()
2638 switch (ctrl->id) { in ov5640_g_volatile_ctrl()
2643 sensor->ctrls.gain->val = val; in ov5640_g_volatile_ctrl()
2649 sensor->ctrls.exposure->val = val; in ov5640_g_volatile_ctrl()
2667 * the controls will be restored right after power-up. in ov5640_s_ctrl()
2669 if (sensor->power_count == 0) in ov5640_s_ctrl()
2672 switch (ctrl->id) { in ov5640_s_ctrl()
2674 ret = ov5640_set_ctrl_gain(sensor, ctrl->val); in ov5640_s_ctrl()
2677 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); in ov5640_s_ctrl()
2680 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); in ov5640_s_ctrl()
2683 ret = ov5640_set_ctrl_hue(sensor, ctrl->val); in ov5640_s_ctrl()
2686 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); in ov5640_s_ctrl()
2689 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); in ov5640_s_ctrl()
2692 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); in ov5640_s_ctrl()
2695 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); in ov5640_s_ctrl()
2698 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); in ov5640_s_ctrl()
2701 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); in ov5640_s_ctrl()
2704 ret = -EINVAL; in ov5640_s_ctrl()
2719 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_init_controls()
2720 struct v4l2_ctrl_handler *hdl = &ctrls->handler; in ov5640_init_controls()
2726 hdl->lock = &sensor->lock; in ov5640_init_controls()
2729 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE, in ov5640_init_controls()
2734 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, in ov5640_init_controls()
2737 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, in ov5640_init_controls()
2739 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, in ov5640_init_controls()
2742 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, in ov5640_init_controls()
2746 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, in ov5640_init_controls()
2749 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, in ov5640_init_controls()
2751 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, in ov5640_init_controls()
2754 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, in ov5640_init_controls()
2756 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, in ov5640_init_controls()
2758 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, in ov5640_init_controls()
2760 ctrls->test_pattern = in ov5640_init_controls()
2762 ARRAY_SIZE(test_pattern_menu) - 1, in ov5640_init_controls()
2764 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, in ov5640_init_controls()
2766 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, in ov5640_init_controls()
2769 ctrls->light_freq = in ov5640_init_controls()
2775 if (hdl->error) { in ov5640_init_controls()
2776 ret = hdl->error; in ov5640_init_controls()
2780 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY; in ov5640_init_controls()
2781 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2782 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; in ov5640_init_controls()
2784 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); in ov5640_init_controls()
2785 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); in ov5640_init_controls()
2786 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); in ov5640_init_controls()
2788 sensor->sd.ctrl_handler = hdl; in ov5640_init_controls()
2800 if (fse->pad != 0) in ov5640_enum_frame_size()
2801 return -EINVAL; in ov5640_enum_frame_size()
2802 if (fse->index >= OV5640_NUM_MODES) in ov5640_enum_frame_size()
2803 return -EINVAL; in ov5640_enum_frame_size()
2805 fse->min_width = in ov5640_enum_frame_size()
2806 ov5640_mode_data[fse->index].hact; in ov5640_enum_frame_size()
2807 fse->max_width = fse->min_width; in ov5640_enum_frame_size()
2808 fse->min_height = in ov5640_enum_frame_size()
2809 ov5640_mode_data[fse->index].vact; in ov5640_enum_frame_size()
2810 fse->max_height = fse->min_height; in ov5640_enum_frame_size()
2824 if (fie->pad != 0) in ov5640_enum_frame_interval()
2825 return -EINVAL; in ov5640_enum_frame_interval()
2826 if (fie->index >= OV5640_NUM_FRAMERATES) in ov5640_enum_frame_interval()
2827 return -EINVAL; in ov5640_enum_frame_interval()
2830 tpf.denominator = ov5640_framerates[fie->index]; in ov5640_enum_frame_interval()
2833 fie->width, fie->height); in ov5640_enum_frame_interval()
2835 return -EINVAL; in ov5640_enum_frame_interval()
2837 fie->interval = tpf; in ov5640_enum_frame_interval()
2842 struct v4l2_subdev_frame_interval *fi) in ov5640_g_frame_interval() argument
2846 mutex_lock(&sensor->lock); in ov5640_g_frame_interval()
2847 fi->interval = sensor->frame_interval; in ov5640_g_frame_interval()
2848 mutex_unlock(&sensor->lock); in ov5640_g_frame_interval()
2854 struct v4l2_subdev_frame_interval *fi) in ov5640_s_frame_interval() argument
2860 if (fi->pad != 0) in ov5640_s_frame_interval()
2861 return -EINVAL; in ov5640_s_frame_interval()
2863 mutex_lock(&sensor->lock); in ov5640_s_frame_interval()
2865 if (sensor->streaming) { in ov5640_s_frame_interval()
2866 ret = -EBUSY; in ov5640_s_frame_interval()
2870 mode = sensor->current_mode; in ov5640_s_frame_interval()
2872 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, in ov5640_s_frame_interval()
2873 mode->hact, mode->vact); in ov5640_s_frame_interval()
2876 fi->interval = sensor->frame_interval; in ov5640_s_frame_interval()
2880 mode = ov5640_find_mode(sensor, frame_rate, mode->hact, in ov5640_s_frame_interval()
2881 mode->vact, true); in ov5640_s_frame_interval()
2883 ret = -EINVAL; in ov5640_s_frame_interval()
2887 if (mode != sensor->current_mode || in ov5640_s_frame_interval()
2888 frame_rate != sensor->current_fr) { in ov5640_s_frame_interval()
2889 sensor->current_fr = frame_rate; in ov5640_s_frame_interval()
2890 sensor->frame_interval = fi->interval; in ov5640_s_frame_interval()
2891 sensor->current_mode = mode; in ov5640_s_frame_interval()
2892 sensor->pending_mode_change = true; in ov5640_s_frame_interval()
2894 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate, in ov5640_s_frame_interval()
2898 mutex_unlock(&sensor->lock); in ov5640_s_frame_interval()
2906 if (code->pad != 0) in ov5640_enum_mbus_code()
2907 return -EINVAL; in ov5640_enum_mbus_code()
2908 if (code->index >= ARRAY_SIZE(ov5640_formats)) in ov5640_enum_mbus_code()
2909 return -EINVAL; in ov5640_enum_mbus_code()
2911 code->code = ov5640_formats[code->index].code; in ov5640_enum_mbus_code()
2920 mutex_lock(&sensor->lock); in ov5640_s_stream()
2922 if (sensor->streaming == !enable) { in ov5640_s_stream()
2923 if (enable && sensor->pending_mode_change) { in ov5640_s_stream()
2929 if (enable && sensor->pending_fmt_change) { in ov5640_s_stream()
2930 ret = ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_s_stream()
2933 sensor->pending_fmt_change = false; in ov5640_s_stream()
2936 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_s_stream()
2942 sensor->streaming = enable; in ov5640_s_stream()
2945 mutex_unlock(&sensor->lock); in ov5640_s_stream()
2981 sensor->supplies[i].supply = ov5640_supply_name[i]; in ov5640_get_regulators()
2983 return devm_regulator_bulk_get(&sensor->i2c_client->dev, in ov5640_get_regulators()
2985 sensor->supplies); in ov5640_get_regulators()
2990 struct i2c_client *client = sensor->i2c_client; in ov5640_check_chip_id()
3000 dev_err(&client->dev, "%s: failed to read chip identifier\n", in ov5640_check_chip_id()
3006 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", in ov5640_check_chip_id()
3008 ret = -ENXIO; in ov5640_check_chip_id()
3018 struct device *dev = &client->dev; in ov5640_probe()
3027 return -ENOMEM; in ov5640_probe()
3029 sensor->i2c_client = client; in ov5640_probe()
3035 fmt = &sensor->fmt; in ov5640_probe()
3036 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; in ov5640_probe()
3037 fmt->colorspace = V4L2_COLORSPACE_SRGB; in ov5640_probe()
3038 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); in ov5640_probe()
3039 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; in ov5640_probe()
3040 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); in ov5640_probe()
3041 fmt->width = 640; in ov5640_probe()
3042 fmt->height = 480; in ov5640_probe()
3043 fmt->field = V4L2_FIELD_NONE; in ov5640_probe()
3044 sensor->frame_interval.numerator = 1; in ov5640_probe()
3045 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; in ov5640_probe()
3046 sensor->current_fr = OV5640_30_FPS; in ov5640_probe()
3047 sensor->current_mode = in ov5640_probe()
3049 sensor->last_mode = sensor->current_mode; in ov5640_probe()
3051 sensor->ae_target = 52; in ov5640_probe()
3054 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", in ov5640_probe()
3059 sensor->upside_down = true; in ov5640_probe()
3069 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), in ov5640_probe()
3073 return -EINVAL; in ov5640_probe()
3076 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); in ov5640_probe()
3083 if (sensor->ep.bus_type != V4L2_MBUS_PARALLEL && in ov5640_probe()
3084 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY && in ov5640_probe()
3085 sensor->ep.bus_type != V4L2_MBUS_BT656) { in ov5640_probe()
3086 dev_err(dev, "Unsupported bus type %d\n", sensor->ep.bus_type); in ov5640_probe()
3087 return -EINVAL; in ov5640_probe()
3091 sensor->xclk = devm_clk_get(dev, "xclk"); in ov5640_probe()
3092 if (IS_ERR(sensor->xclk)) { in ov5640_probe()
3094 return PTR_ERR(sensor->xclk); in ov5640_probe()
3097 sensor->xclk_freq = clk_get_rate(sensor->xclk); in ov5640_probe()
3098 if (sensor->xclk_freq < OV5640_XCLK_MIN || in ov5640_probe()
3099 sensor->xclk_freq > OV5640_XCLK_MAX) { in ov5640_probe()
3101 sensor->xclk_freq); in ov5640_probe()
3102 return -EINVAL; in ov5640_probe()
3106 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", in ov5640_probe()
3108 if (IS_ERR(sensor->pwdn_gpio)) in ov5640_probe()
3109 return PTR_ERR(sensor->pwdn_gpio); in ov5640_probe()
3112 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", in ov5640_probe()
3114 if (IS_ERR(sensor->reset_gpio)) in ov5640_probe()
3115 return PTR_ERR(sensor->reset_gpio); in ov5640_probe()
3117 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); in ov5640_probe()
3119 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | in ov5640_probe()
3121 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; in ov5640_probe()
3122 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; in ov5640_probe()
3123 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); in ov5640_probe()
3131 mutex_init(&sensor->lock); in ov5640_probe()
3141 ret = v4l2_async_register_subdev_sensor_common(&sensor->sd); in ov5640_probe()
3148 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_probe()
3150 media_entity_cleanup(&sensor->sd.entity); in ov5640_probe()
3151 mutex_destroy(&sensor->lock); in ov5640_probe()
3160 v4l2_async_unregister_subdev(&sensor->sd); in ov5640_remove()
3161 media_entity_cleanup(&sensor->sd.entity); in ov5640_remove()
3162 v4l2_ctrl_handler_free(&sensor->ctrls.handler); in ov5640_remove()
3163 mutex_destroy(&sensor->lock); in ov5640_remove()