Lines Matching full:sensor
545 /* power-on sensor init reg table */
602 static int ov5640_init_slave_id(struct ov5640_dev *sensor) in ov5640_init_slave_id() argument
604 struct i2c_client *client = sensor->i2c_client; in ov5640_init_slave_id()
630 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val) in ov5640_write_reg() argument
632 struct i2c_client *client = sensor->i2c_client; in ov5640_write_reg()
656 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val) in ov5640_read_reg() argument
658 struct i2c_client *client = sensor->i2c_client; in ov5640_read_reg()
687 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val) in ov5640_read_reg16() argument
692 ret = ov5640_read_reg(sensor, reg, &hi); in ov5640_read_reg16()
695 ret = ov5640_read_reg(sensor, reg + 1, &lo); in ov5640_read_reg16()
703 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val) in ov5640_write_reg16() argument
707 ret = ov5640_write_reg(sensor, reg, val >> 8); in ov5640_write_reg16()
711 return ov5640_write_reg(sensor, reg + 1, val & 0xff); in ov5640_write_reg16()
714 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg, in ov5640_mod_reg() argument
720 ret = ov5640_read_reg(sensor, reg, &readval); in ov5640_mod_reg()
728 return ov5640_write_reg(sensor, reg, val); in ov5640_mod_reg()
845 static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor, in ov5640_compute_sys_clk() argument
849 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult; in ov5640_compute_sys_clk()
858 static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor, in ov5640_calc_sys_clk() argument
882 _rate = ov5640_compute_sys_clk(sensor, in ov5640_calc_sys_clk()
957 * FIXME: this deviates from the sensor manual documentation which is quite
960 static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor, in ov5640_set_mipi_pclk() argument
963 const struct ov5640_mode_info *mode = sensor->current_mode; in ov5640_set_mipi_pclk()
970 * but according to the sensor manual it goes through the in ov5640_set_mipi_pclk()
979 ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv); in ov5640_set_mipi_pclk()
981 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, in ov5640_set_mipi_pclk()
984 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, in ov5640_set_mipi_pclk()
989 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult); in ov5640_set_mipi_pclk()
993 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, in ov5640_set_mipi_pclk()
998 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, in ov5640_set_mipi_pclk()
1002 static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor, in ov5640_calc_pclk() argument
1010 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult, in ov5640_calc_pclk()
1019 static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate) in ov5640_set_dvp_pclk() argument
1024 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv, in ov5640_set_dvp_pclk()
1030 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0, in ov5640_set_dvp_pclk()
1039 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, in ov5640_set_dvp_pclk()
1044 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, in ov5640_set_dvp_pclk()
1049 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3, in ov5640_set_dvp_pclk()
1054 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30, in ov5640_set_dvp_pclk()
1059 static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor, in ov5640_set_jpeg_timings() argument
1071 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3); in ov5640_set_jpeg_timings()
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()
1082 /* download ov5640 settings to sensor through i2c */
1083 static int ov5640_set_timings(struct ov5640_dev *sensor, in ov5640_set_timings() argument
1088 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) { in ov5640_set_timings()
1089 ret = ov5640_set_jpeg_timings(sensor, mode); 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()
1109 static int ov5640_load_regs(struct ov5640_dev *sensor, in ov5640_load_regs() argument
1128 sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY) in ov5640_load_regs()
1132 ret = ov5640_mod_reg(sensor, reg_addr, mask, val); in ov5640_load_regs()
1134 ret = ov5640_write_reg(sensor, reg_addr, val); in ov5640_load_regs()
1142 return ov5640_set_timings(sensor, mode); in ov5640_load_regs()
1145 static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on) in ov5640_set_autoexposure() argument
1147 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, in ov5640_set_autoexposure()
1152 static int ov5640_get_exposure(struct ov5640_dev *sensor) in ov5640_get_exposure() argument
1157 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp); in ov5640_get_exposure()
1161 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp); in ov5640_get_exposure()
1165 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp); in ov5640_get_exposure()
1174 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure) in ov5640_set_exposure() argument
1180 ret = ov5640_write_reg(sensor, in ov5640_set_exposure()
1185 ret = ov5640_write_reg(sensor, in ov5640_set_exposure()
1190 return ov5640_write_reg(sensor, in ov5640_set_exposure()
1195 static int ov5640_get_gain(struct ov5640_dev *sensor) in ov5640_get_gain() argument
1200 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain); in ov5640_get_gain()
1207 static int ov5640_set_gain(struct ov5640_dev *sensor, int gain) in ov5640_set_gain() argument
1209 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, in ov5640_set_gain()
1213 static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on) in ov5640_set_autogain() argument
1215 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, in ov5640_set_autogain()
1219 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) in ov5640_set_stream_dvp() argument
1221 return ov5640_write_reg(sensor, OV5640_REG_SYS_CTRL0, on ? in ov5640_set_stream_dvp()
1226 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on) in ov5640_set_stream_mipi() argument
1235 * FIXME: the sensor manual (version 2.03) reports in ov5640_set_stream_mipi()
1247 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, in ov5640_set_stream_mipi()
1252 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01, in ov5640_set_stream_mipi()
1256 static int ov5640_get_sysclk(struct ov5640_dev *sensor) in ov5640_get_sysclk() argument
1259 u32 xvclk = sensor->xclk_freq / 10000; in ov5640_get_sysclk()
1266 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1); in ov5640_get_sysclk()
1273 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1); in ov5640_get_sysclk()
1280 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1); in ov5640_get_sysclk()
1285 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1); in ov5640_get_sysclk()
1291 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1); in ov5640_get_sysclk()
1307 static int ov5640_set_night_mode(struct ov5640_dev *sensor) in ov5640_set_night_mode() argument
1313 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode); in ov5640_set_night_mode()
1317 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode); in ov5640_set_night_mode()
1320 static int ov5640_get_hts(struct ov5640_dev *sensor) in ov5640_get_hts() argument
1326 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts); in ov5640_get_hts()
1332 static int ov5640_get_vts(struct ov5640_dev *sensor) in ov5640_get_vts() argument
1337 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts); in ov5640_get_vts()
1343 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts) in ov5640_set_vts() argument
1345 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts); in ov5640_set_vts()
1348 static int ov5640_get_light_freq(struct ov5640_dev *sensor) in ov5640_get_light_freq() argument
1354 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp); in ov5640_get_light_freq()
1360 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00, in ov5640_get_light_freq()
1373 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C, in ov5640_get_light_freq()
1389 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor) in ov5640_set_bandingfilter() argument
1395 ret = ov5640_get_sysclk(sensor); in ov5640_set_bandingfilter()
1400 sensor->prev_sysclk = ret; in ov5640_set_bandingfilter()
1402 ret = ov5640_get_hts(sensor); in ov5640_set_bandingfilter()
1407 sensor->prev_hts = ret; in ov5640_set_bandingfilter()
1410 ret = ov5640_get_vts(sensor); in ov5640_set_bandingfilter()
1417 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; in ov5640_set_bandingfilter()
1418 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60); in ov5640_set_bandingfilter()
1424 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60); in ov5640_set_bandingfilter()
1429 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; in ov5640_set_bandingfilter()
1430 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50); in ov5640_set_bandingfilter()
1436 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50); in ov5640_set_bandingfilter()
1439 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target) in ov5640_set_ae_target() argument
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()
1466 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high); in ov5640_set_ae_target()
1469 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low); in ov5640_set_ae_target()
1472 static int ov5640_get_binning(struct ov5640_dev *sensor) in ov5640_get_binning() argument
1477 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp); in ov5640_get_binning()
1484 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable) in ov5640_set_binning() argument
1492 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, in ov5640_set_binning()
1501 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, in ov5640_set_binning()
1505 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor) in ov5640_set_virtual_channel() argument
1507 struct i2c_client *client = sensor->i2c_client; in ov5640_set_virtual_channel()
1518 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp); in ov5640_set_virtual_channel()
1523 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp); in ov5640_set_virtual_channel()
1527 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr, in ov5640_find_mode() argument
1548 static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor) in ov5640_calc_pixel_rate() argument
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()
1559 * sensor changes between scaling and subsampling, go through
1562 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, in ov5640_set_mode_exposure_calc() argument
1577 ret = ov5640_get_exposure(sensor); in ov5640_set_mode_exposure_calc()
1581 ret = ov5640_get_binning(sensor); in ov5640_set_mode_exposure_calc()
1589 ret = ov5640_get_gain(sensor); in ov5640_set_mode_exposure_calc()
1595 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average); in ov5640_set_mode_exposure_calc()
1600 ret = ov5640_set_night_mode(sensor); in ov5640_set_mode_exposure_calc()
1605 ret = ov5640_load_regs(sensor, mode); in ov5640_set_mode_exposure_calc()
1610 ret = ov5640_get_vts(sensor); in ov5640_set_mode_exposure_calc()
1614 ret = ov5640_get_hts(sensor); in ov5640_set_mode_exposure_calc()
1621 ret = ov5640_get_sysclk(sensor); in ov5640_set_mode_exposure_calc()
1629 ret = ov5640_get_light_freq(sensor); in ov5640_set_mode_exposure_calc()
1642 if (!sensor->prev_sysclk) { in ov5640_set_mode_exposure_calc()
1643 ret = ov5640_get_sysclk(sensor); in ov5640_set_mode_exposure_calc()
1648 sensor->prev_sysclk = ret; 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()
1702 ret = ov5640_set_gain(sensor, cap_gain16); in ov5640_set_mode_exposure_calc()
1709 ret = ov5640_set_vts(sensor, cap_vts); in ov5640_set_mode_exposure_calc()
1715 return ov5640_set_exposure(sensor, cap_shutter); in ov5640_set_mode_exposure_calc()
1719 * if sensor changes inside scaling or subsampling
1722 static int ov5640_set_mode_direct(struct ov5640_dev *sensor, in ov5640_set_mode_direct() argument
1729 return ov5640_load_regs(sensor, mode); in ov5640_set_mode_direct()
1732 static int ov5640_set_mode(struct ov5640_dev *sensor) in ov5640_set_mode() argument
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()
1747 ret = ov5640_set_autogain(sensor, false); in ov5640_set_mode()
1753 ret = ov5640_set_autoexposure(sensor, false); in ov5640_set_mode()
1762 rate = ov5640_calc_pixel_rate(sensor) * 16; 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()
1765 ret = ov5640_set_mipi_pclk(sensor, rate); in ov5640_set_mode()
1767 rate = rate / sensor->ep.bus.parallel.bus_width; in ov5640_set_mode()
1768 ret = ov5640_set_dvp_pclk(sensor, rate); in ov5640_set_mode()
1780 ret = ov5640_set_mode_exposure_calc(sensor, mode); in ov5640_set_mode()
1786 ret = ov5640_set_mode_direct(sensor, mode); in ov5640_set_mode()
1793 ov5640_set_autogain(sensor, true); in ov5640_set_mode()
1795 ov5640_set_autoexposure(sensor, true); in ov5640_set_mode()
1797 ret = ov5640_set_binning(sensor, dn_mode != SCALING); in ov5640_set_mode()
1800 ret = ov5640_set_ae_target(sensor, sensor->ae_target); in ov5640_set_mode()
1803 ret = ov5640_get_light_freq(sensor); in ov5640_set_mode()
1806 ret = ov5640_set_bandingfilter(sensor); in ov5640_set_mode()
1809 ret = ov5640_set_virtual_channel(sensor); in ov5640_set_mode()
1813 sensor->pending_mode_change = false; in ov5640_set_mode()
1814 sensor->last_mode = mode; in ov5640_set_mode()
1820 ov5640_set_autoexposure(sensor, true); in ov5640_set_mode()
1823 ov5640_set_autogain(sensor, true); in ov5640_set_mode()
1828 static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1832 static int ov5640_restore_mode(struct ov5640_dev *sensor) in ov5640_restore_mode() argument
1837 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data); in ov5640_restore_mode()
1840 sensor->last_mode = &ov5640_mode_init_data; in ov5640_restore_mode()
1842 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f, in ov5640_restore_mode()
1849 ret = ov5640_set_mode(sensor); in ov5640_restore_mode()
1853 return ov5640_set_framefmt(sensor, &sensor->fmt); in ov5640_restore_mode()
1856 static void ov5640_power(struct ov5640_dev *sensor, bool enable) in ov5640_power() argument
1858 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); in ov5640_power()
1861 static void ov5640_reset(struct ov5640_dev *sensor) in ov5640_reset() argument
1863 if (!sensor->reset_gpio) in ov5640_reset()
1866 gpiod_set_value_cansleep(sensor->reset_gpio, 0); in ov5640_reset()
1869 ov5640_power(sensor, false); in ov5640_reset()
1871 ov5640_power(sensor, true); 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()
1881 static int ov5640_set_power_on(struct ov5640_dev *sensor) in ov5640_set_power_on() argument
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()
1894 sensor->supplies); in ov5640_set_power_on()
1901 ov5640_reset(sensor); in ov5640_set_power_on()
1902 ov5640_power(sensor, true); in ov5640_set_power_on()
1904 ret = ov5640_init_slave_id(sensor); in ov5640_set_power_on()
1911 ov5640_power(sensor, false); 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()
1918 static void ov5640_set_power_off(struct ov5640_dev *sensor) in ov5640_set_power_off() argument
1920 ov5640_power(sensor, false); in ov5640_set_power_off()
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()
1925 static int ov5640_set_power_mipi(struct ov5640_dev *sensor, bool on) in ov5640_set_power_mipi() argument
1931 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); in ov5640_set_power_mipi()
1932 ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x04); in ov5640_set_power_mipi()
1933 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x00); in ov5640_set_power_mipi()
1947 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x40); in ov5640_set_power_mipi()
1958 ret = ov5640_write_reg(sensor, OV5640_REG_MIPI_CTRL00, 0x24); in ov5640_set_power_mipi()
1970 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 0x70); in ov5640_set_power_mipi()
1980 static int ov5640_set_power_dvp(struct ov5640_dev *sensor, bool on) in ov5640_set_power_dvp() argument
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()
1989 ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, 0x00); in ov5640_set_power_dvp()
1990 ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x58); in ov5640_set_power_dvp()
1991 ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, 0x20); in ov5640_set_power_dvp()
1992 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, 0x00); in ov5640_set_power_dvp()
1993 ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0x00); in ov5640_set_power_dvp()
2037 ret = ov5640_write_reg(sensor, OV5640_REG_CCIR656_CTRL00, in ov5640_set_power_dvp()
2061 ret = ov5640_write_reg(sensor, OV5640_REG_POLARITY_CTRL00, polarities); in ov5640_set_power_dvp()
2073 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00, 0x18); in ov5640_set_power_dvp()
2087 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE01, in ov5640_set_power_dvp()
2098 return ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT_ENABLE02, 0xfc); in ov5640_set_power_dvp()
2101 static int ov5640_set_power(struct ov5640_dev *sensor, bool on) in ov5640_set_power() argument
2106 ret = ov5640_set_power_on(sensor); in ov5640_set_power()
2110 ret = ov5640_restore_mode(sensor); in ov5640_set_power()
2115 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) in ov5640_set_power()
2116 ret = ov5640_set_power_mipi(sensor, on); in ov5640_set_power()
2118 ret = ov5640_set_power_dvp(sensor, on); in ov5640_set_power()
2123 ov5640_set_power_off(sensor); in ov5640_set_power()
2128 ov5640_set_power_off(sensor); in ov5640_set_power()
2136 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_s_power() local
2139 mutex_lock(&sensor->lock); in ov5640_s_power()
2145 if (sensor->power_count == !on) { in ov5640_s_power()
2146 ret = ov5640_set_power(sensor, !!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()
2165 static int ov5640_try_frame_interval(struct ov5640_dev *sensor, in ov5640_try_frame_interval() argument
2201 mode = ov5640_find_mode(sensor, rate, width, height, false); in ov5640_try_frame_interval()
2209 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_get_fmt() local
2215 mutex_lock(&sensor->lock); in ov5640_get_fmt()
2218 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, in ov5640_get_fmt()
2221 fmt = &sensor->fmt; in ov5640_get_fmt()
2225 mutex_unlock(&sensor->lock); in ov5640_get_fmt()
2235 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_try_fmt_internal() local
2239 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); in ov5640_try_fmt_internal()
2267 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_set_fmt() local
2276 mutex_lock(&sensor->lock); in ov5640_set_fmt()
2278 if (sensor->streaming) { in ov5640_set_fmt()
2284 sensor->current_fr, &new_mode); 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()
2303 ov5640_calc_pixel_rate(sensor)); in ov5640_set_fmt()
2305 mutex_unlock(&sensor->lock); in ov5640_set_fmt()
2309 static int ov5640_set_framefmt(struct ov5640_dev *sensor, in ov5640_set_framefmt() argument
2368 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt); in ov5640_set_framefmt()
2373 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux); in ov5640_set_framefmt()
2381 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, in ov5640_set_framefmt()
2392 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02, in ov5640_set_framefmt()
2403 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02, in ov5640_set_framefmt()
2409 * Sensor Controls.
2412 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_hue() argument
2417 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, in ov5640_set_ctrl_hue()
2421 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value); in ov5640_set_ctrl_hue()
2423 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0); in ov5640_set_ctrl_hue()
2429 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_contrast() argument
2434 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, in ov5640_set_ctrl_contrast()
2438 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5, in ov5640_set_ctrl_contrast()
2441 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0); in ov5640_set_ctrl_contrast()
2447 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_saturation() argument
2452 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, in ov5640_set_ctrl_saturation()
2456 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3, in ov5640_set_ctrl_saturation()
2460 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4, in ov5640_set_ctrl_saturation()
2463 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0); in ov5640_set_ctrl_saturation()
2469 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) in ov5640_set_ctrl_white_balance() argument
2473 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL, in ov5640_set_ctrl_white_balance()
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()
2482 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red); in ov5640_set_ctrl_white_balance()
2485 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue); in ov5640_set_ctrl_white_balance()
2491 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, in ov5640_set_ctrl_exposure() argument
2494 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_exposure()
2499 ret = ov5640_set_autoexposure(sensor, auto_exp); in ov5640_set_ctrl_exposure()
2507 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, in ov5640_set_ctrl_exposure()
2511 ret = ov5640_get_vts(sensor); in ov5640_set_ctrl_exposure()
2518 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); in ov5640_set_ctrl_exposure()
2524 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain) in ov5640_set_ctrl_gain() argument
2526 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_set_ctrl_gain()
2530 ret = ov5640_set_autogain(sensor, auto_gain); in ov5640_set_ctrl_gain()
2536 ret = ov5640_set_gain(sensor, ctrls->gain->val); in ov5640_set_ctrl_gain()
2572 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_test_pattern() argument
2574 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1, in ov5640_set_ctrl_test_pattern()
2578 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_light_freq() argument
2582 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7), in ov5640_set_ctrl_light_freq()
2588 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2), in ov5640_set_ctrl_light_freq()
2593 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_hflip() argument
2596 * If sensor is mounted upside down, mirror logic is inversed. in ov5640_set_ctrl_hflip()
2598 * Sensor is a BSI (Back Side Illuminated) one, in ov5640_set_ctrl_hflip()
2607 * - [1]: Sensor mirror in ov5640_set_ctrl_hflip()
2609 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, in ov5640_set_ctrl_hflip()
2611 (!(value ^ sensor->upside_down)) ? in ov5640_set_ctrl_hflip()
2615 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value) in ov5640_set_ctrl_vflip() argument
2617 /* If sensor is mounted upside down, flip logic is inversed */ in ov5640_set_ctrl_vflip()
2622 * - [1]: Sensor vflip in ov5640_set_ctrl_vflip()
2624 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, in ov5640_set_ctrl_vflip()
2626 (value ^ sensor->upside_down) ? in ov5640_set_ctrl_vflip()
2633 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_g_volatile_ctrl() local
2640 val = ov5640_get_gain(sensor); in ov5640_g_volatile_ctrl()
2643 sensor->ctrls.gain->val = val; in ov5640_g_volatile_ctrl()
2646 val = ov5640_get_exposure(sensor); in ov5640_g_volatile_ctrl()
2649 sensor->ctrls.exposure->val = val; in ov5640_g_volatile_ctrl()
2659 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_s_ctrl() local
2669 if (sensor->power_count == 0) 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()
2716 static int ov5640_init_controls(struct ov5640_dev *sensor) in ov5640_init_controls() argument
2719 struct ov5640_ctrls *ctrls = &sensor->ctrls; in ov5640_init_controls()
2726 hdl->lock = &sensor->lock; in ov5640_init_controls()
2731 ov5640_calc_pixel_rate(sensor)); in ov5640_init_controls()
2788 sensor->sd.ctrl_handler = hdl; in ov5640_init_controls()
2820 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_enum_frame_interval() local
2832 ret = ov5640_try_frame_interval(sensor, &tpf, in ov5640_enum_frame_interval()
2844 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_g_frame_interval() local
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()
2856 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_s_frame_interval() local
2863 mutex_lock(&sensor->lock); in ov5640_s_frame_interval()
2865 if (sensor->streaming) { 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()
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()
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()
2895 ov5640_calc_pixel_rate(sensor)); in ov5640_s_frame_interval()
2898 mutex_unlock(&sensor->lock); in ov5640_s_frame_interval()
2917 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_s_stream() local
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()
2924 ret = ov5640_set_mode(sensor); 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()
2937 ret = ov5640_set_stream_mipi(sensor, enable); in ov5640_s_stream()
2939 ret = ov5640_set_stream_dvp(sensor, enable); in ov5640_s_stream()
2942 sensor->streaming = enable; in ov5640_s_stream()
2945 mutex_unlock(&sensor->lock); in ov5640_s_stream()
2976 static int ov5640_get_regulators(struct ov5640_dev *sensor) in ov5640_get_regulators() argument
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()
2988 static int ov5640_check_chip_id(struct ov5640_dev *sensor) in ov5640_check_chip_id() argument
2990 struct i2c_client *client = sensor->i2c_client; in ov5640_check_chip_id()
2994 ret = ov5640_set_power_on(sensor); in ov5640_check_chip_id()
2998 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id); in ov5640_check_chip_id()
3012 ov5640_set_power_off(sensor); in ov5640_check_chip_id()
3020 struct ov5640_dev *sensor; in ov5640_probe() local
3025 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); in ov5640_probe()
3026 if (!sensor) in ov5640_probe()
3029 sensor->i2c_client = client; in ov5640_probe()
3032 * default init sequence initialize sensor to in ov5640_probe()
3035 fmt = &sensor->fmt; 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()
3053 /* optional indication of physical rotation of sensor */ in ov5640_probe()
3059 sensor->upside_down = true; 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()
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()
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()
3127 ret = ov5640_get_regulators(sensor); in ov5640_probe()
3131 mutex_init(&sensor->lock); in ov5640_probe()
3133 ret = ov5640_check_chip_id(sensor); in ov5640_probe()
3137 ret = ov5640_init_controls(sensor); 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()
3158 struct ov5640_dev *sensor = to_ov5640_dev(sd); in ov5640_remove() local
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()