media: i2c: ov5675: actual PLL settings and 24MHz input
cover media: i2c: ov5675: actual PLL settings and 24MHz input
Some of our boards cannot supply the correct 19.2MHz currently expected by the driver, just 19.1xxMHz and we carried a hack in our downstream tree seemingly forever.
After finding a datasheet for the camera that includes the clock description and the actual expected values for a 24MHz input clock, this can be finally fixed.
After testing the values and receiving correct images both with our previous clock-rate hack and the 24MHz input, I choose not to make it worse by adding another hard-coded init sequence but instead describing the PLL settings similar to how the ov8865 driver does it.
Heiko Stuebner (5):
media: i2c: ov5675: Fix type of xvclk_rate
media: i2c: ov5675: Move camera init out of the link_freq sequence
media: i2c: ov5675: Make room for checking for multiple input
frequencies
media: i2c: ov5675: Handle PLLs using actual register descriptions
media: i2c: ov5675: Add PLL settings for a 24MHz input clock
drivers/media/i2c/ov5675.c | 364 ++++++++++++++++++++++++++++++++++--- 1 file changed, 341 insertions(+), 23 deletions(-)
Signature · 1 line
2.47.3
PATCH 1/5 media: i2c: ov5675: Fix type of xvclk_rate
From: Heiko Stuebner <[email protected]>
clk_get_rate returns an unsigned long, so don't be creative by using a different type in the driver.
--- drivers/media/i2c/ov5675.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov5675.c +2 -2
@@ -1173,7 +1173,7 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675)struct v4l2_fwnode_endpoint bus_cfg = { .bus_type =V4L2_MBUS_CSI2_DPHY }; -u32 xvclk_rate; +unsigned long xvclk_rate;int ret;unsigned int i, j;
@@ -1196,7 +1196,7 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675) xvclk_rate =clk_get_rate (ov5675->xvclk);if (xvclk_rate !=OV5675_XVCLK_19_2 ) { -dev_err (dev,"external clock rate %u is unsupported" , +dev_err (dev,"external clock rate %lu is unsupported" , xvclk_rate); ret = -EINVAL ; goto check_hwcfg_error;
Signature · 1 line
2.47.3
Re: [PATCH 1/5] media: i2c: ov5675: Fix type of xvclk_rate
Hi Heiko,
Quoted 5 lines · Heiko Stuebner
From: Heiko Stuebner <[email protected]>
clk_get_ratereturns an unsigned long, so don't be creative by using a different type in the driver.
Thanks!
Quentin
PATCH 2/5 media: i2c: ov5675: Move camera init out of the link_freq sequence
In reply to Heiko StuebnerFrom: Heiko Stuebner <[email protected]>
The register sequence for the PLL configuration also contains basic setup like the software-reset and making sure the module is in stanbdy mode.
Instead of having the byte sequences, move those to a dedicated function.
--- drivers/media/i2c/ov5675.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/media/i2c/ov5675.c +24 -2
@@ -32,6 +32,9 @@#define OV5675_MODE_STANDBY 0x00#define OV5675_MODE_STREAMING 0x01 +#define OV5675_REG_SOFTWARE_RST 0x0103 +#define OV5675_SOFTWARE_RST BIT(0) +/* vertical-timings from sensor */ #define OV5675_REG_VTS 0x380e#define OV5675_VTS_30FPS 0x07e4
@@ -130,8 +133,6 @@ struct ov5675_mode { };static const struct ov5675_reg mipi_data_rate_900mbps[] = { - {0x0103 ,0x01 }, - {0x0100 ,0x00 }, {0x0300 ,0x04 }, {0x0302 ,0x8d }, {0x0303 ,0x00 },
@@ -890,6 +891,23 @@ static int ov5675_identify_module(struct ov5675 *ov5675)return 0 ; } +static int ov5675_init_module (struct ov5675 *ov5675) +{ +int ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_SOFTWARE_RST , +OV5675_REG_VALUE_08BIT ,OV5675_SOFTWARE_RST ); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_MODE_SELECT , +OV5675_REG_VALUE_08BIT ,OV5675_MODE_STANDBY ); +if (ret) +return ret; + +return 0 ; +} +static int ov5675_start_streaming (struct ov5675 *ov5675) {const struct ov5675_reg_list *reg_list;
@@ -899,6 +917,10 @@ static int ov5675_start_streaming(struct ov5675 *ov5675)if (ret)return ret; + ret =ov5675_init_module (ov5675); +if (ret) +return ret; + link_freq_index = ov5675->cur_mode->link_freq_index; reg_list = &link_freq_configs[link_freq_index].reg_list; ret =ov5675_write_reg_list (ov5675, reg_list);
Signature · 1 line
2.47.3
Re: [PATCH 2/5] media: i2c: ov5675: Move camera init out of the link_freq sequence
Hi Heiko,
Quoted 8 lines · Heiko Stuebner
From: Heiko Stuebner <[email protected]>
The register sequence for the PLL configuration also contains basic setup like the software-reset and making sure the module is in stanbdy mode.
Instead of having the byte sequences, move those to a dedicated function.
Nitpick below.
Quoted 37 lines
Signed-off-by Heiko Stuebner <[email protected]>--- drivers/media/i2c/ov5675.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)drivers/media/i2c/ov5675.c +8
-2@@ -32,6 +32,9 @@#define OV5675_MODE_STANDBY 0x00#define OV5675_MODE_STREAMING 0x01 +#define OV5675_REG_SOFTWARE_RST 0x0103 +#define OV5675_SOFTWARE_RST BIT(0) +/* vertical-timings from sensor */ #define OV5675_REG_VTS 0x380e#define OV5675_VTS_30FPS 0x07e4@@ -130,8 +133,6 @@ struct ov5675_mode { };static const struct ov5675_reg mipi_data_rate_900mbps[] = { - {0x0103 ,0x01 }, - {0x0100 ,0x00 }, {0x0300 ,0x04 }, {0x0302 ,0x8d }, {0x0303 ,0x00 },@@ -890,6 +891,23 @@ static int ov5675_identify_module(struct ov5675 *ov5675)return 0 ; } +static int ov5675_init_module (struct ov5675 *ov5675) +{ +int ret; + + ret = ov5675_write_reg(ov5675, OV5675_REG_SOFTWARE_RST,
drivers/media/i2c/ov5675.c static int ov5675_identify_module(struct ov5675 *ov5675) · line 908
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+ OV5675_REG_VALUE_08BIT, OV5675_SOFTWARE_RST); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_MODE_SELECT , +OV5675_REG_VALUE_08BIT ,OV5675_MODE_STANDBY ); +if (ret) +return ret; + +return 0 ;
Last 6 lines can be simplified to:
return ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
OV5675_REG_VALUE_08BIT, OV5675_MODE_STANDBY);
Cheers,
Quentin
PATCH 3/5 media: i2c: ov5675: Make room for checking for multiple input frequencies
In reply to Heiko StuebnerFrom: Heiko Stuebner <[email protected]>
Right now the driver only allows the single 19.2MHz input clock, but we want to support multiple ones in the future. So as a first step, adapt the frequency check to handle a list of supported frequencies.
--- drivers/media/i2c/ov5675.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
drivers/media/i2c/ov5675.c +21 -5
@@ -21,7 +21,6 @@#define OV5675_LINK_FREQ_450MHZ 450000000ULL#define OV5675_SCLK 90000000LL -#define OV5675_XVCLK_19_2 19200000#define OV5675_DATA_LANES 2#define OV5675_RGB_DEPTH 10
@@ -132,6 +131,17 @@ struct ov5675_mode {const struct ov5675_reg_list reg_list; }; +/* Clock rate */ + +enum xvclk_rate { +OV5675_XVCLK_19_2 , +OV5675_NUM_SUPPORTED_RATES +}; + +static const unsigned long supported_xvclk_rates[] = { + [OV5675_XVCLK_19_2 ] =19200000 , +}; +static const struct ov5675_reg mipi_data_rate_900mbps[] = { {0x0300 ,0x04 }, {0x0302 ,0x8d },
@@ -500,6 +510,7 @@ struct ov5675 {struct media_pad pad;struct v4l2_ctrl_handler ctrl_handler;struct clk *xvclk; +unsigned long xvclk_rate;struct gpio_desc *reset_gpio;struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES ];
@@ -1195,7 +1206,6 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675)struct v4l2_fwnode_endpoint bus_cfg = { .bus_type =V4L2_MBUS_CSI2_DPHY }; -unsigned long xvclk_rate;int ret;unsigned int i, j;
@@ -1216,10 +1226,16 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675) goto check_hwcfg_error; } - xvclk_rate =clk_get_rate (ov5675->xvclk); -if (xvclk_rate !=OV5675_XVCLK_19_2 ) { + ov5675->xvclk_rate =clk_get_rate (ov5675->xvclk); + +for (i =0 ; i <ARRAY_SIZE (supported_xvclk_rates); i++) { +if (ov5675->xvclk_rate == supported_xvclk_rates[i]) +break ; + } + +if (i ==ARRAY_SIZE (supported_xvclk_rates)) {dev_err (dev,"external clock rate %lu is unsupported" , - xvclk_rate); + ov5675->xvclk_rate); ret = -EINVAL ; goto check_hwcfg_error; }
Signature · 1 line
2.47.3
Re: [PATCH 3/5] media: i2c: ov5675: Make room for checking for multiple input frequencies
Hi Heiko,
Quoted 42 lines · Heiko Stuebner
From: Heiko Stuebner <[email protected]>
Right now the driver only allows the single 19.2MHz input clock, but we want to support multiple ones in the future. So as a first step, adapt the frequency check to handle a list of supported frequencies.
Signed-off-by Heiko Stuebner <[email protected]>--- drivers/media/i2c/ov5675.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)drivers/media/i2c/ov5675.c +11
-1@@ -21,7 +21,6 @@#define OV5675_LINK_FREQ_450MHZ 450000000ULL#define OV5675_SCLK 90000000LL -#define OV5675_XVCLK_19_2 19200000#define OV5675_DATA_LANES 2#define OV5675_RGB_DEPTH 10@@ -132,6 +131,17 @@ struct ov5675_mode {const struct ov5675_reg_list reg_list; }; +/* Clock rate */ + +enum xvclk_rate { +OV5675_XVCLK_19_2 , +OV5675_NUM_SUPPORTED_RATES +}; + +static const unsigned long supported_xvclk_rates[] = { + [OV5675_XVCLK_19_2 ] =19200000 , +}; +static const struct ov5675_reg mipi_data_rate_900mbps[] = { {0x0300 ,0x04 }, {0x0302 ,0x8d },
drivers/media/i2c/ov5675.c struct ov5675 { · line 513
Quoted 4 lines
drivers/media/i2c/ov5675.c +1
-0@@ -500,6 +510,7 @@ struct ov5675 {struct media_pad pad;struct v4l2_ctrl_handler ctrl_handler;struct clk *xvclk; +unsigned long xvclk_rate;
Why this new member in the struct? You don't seem to be using it, in this patch or later.
I think you meant to declare pll_config_index instead
Quoted 12 lines
drivers/media/i2c/ov5675.c +0
-1struct gpio_desc *reset_gpio;struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES ];@@ -1195,7 +1206,6 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675)struct v4l2_fwnode_endpoint bus_cfg = { .bus_type =V4L2_MBUS_CSI2_DPHY }; -unsigned long xvclk_rate;int ret;unsigned int i, j;
drivers/media/i2c/ov5675.c static int ov5675_get_hwcfg(struct ov5675 *ov5675) · line 1232
Quoted 9 lines
drivers/media/i2c/ov5675.c +4
-2@@ -1216,10 +1226,16 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675) goto check_hwcfg_error; } - xvclk_rate =clk_get_rate (ov5675->xvclk); -if (xvclk_rate !=OV5675_XVCLK_19_2 ) { + ov5675->xvclk_rate =clk_get_rate (ov5675->xvclk); + +for (i =0 ; i <ARRAY_SIZE (supported_xvclk_rates); i++) { +if (ov5675->xvclk_rate == supported_xvclk_rates[i])
and set it here, like in patch 4?
Cheers,
Quentin
PATCH 4/5 media: i2c: ov5675: Handle PLLs using actual register descriptions
In reply to Heiko StuebnerFrom: Heiko Stuebner <[email protected]>
The datasheet actually describes the clock tree, so we can set PLL rates using actual code and not via an opaque register write list.
This allows the easily add additional rates for different input clocks in the future.
With the old hard values + the default values in the other registers, I was able to verify that the previous settings result in the correct 900MHz MIPI-PHY rate, 112MHz MIPI PCLK and 90MHz SCLK when following the clock diagram in the manual and re-calculating the rates from that. (Exact values are 902.4MHz, 112.8MHz, 91.2MHz, coming from the 19.2MHz)
A caveat is that the datasheet is not fully consistent between the graphical clock diagram and the register description - with the diagram showing another selector between both PLLs, not mentioned in the register description.
--- drivers/media/i2c/ov5675.c | 287 ++++++++++++++++++++++++++++++++++--- 1 file changed, 271 insertions(+), 16 deletions(-)
drivers/media/i2c/ov5675.c +271 -16
@@ -34,6 +34,42 @@#define OV5675_REG_SOFTWARE_RST 0x0103#define OV5675_SOFTWARE_RST BIT(0) +#define OV5675_REG_PLL_CTRL0 0x300 +#define OV5675_PLL1_PREDIV (v) ((v) & GENMASK(2, 0)) +#define OV5675_REG_PLL_CTRL1 0x301 +#define OV5675_PLL1_MUL_H (v) (((v) & GENMASK(9, 8)) >> 8) +#define OV5675_REG_PLL_CTRL2 0x302 +#define OV5675_PLL1_MUL_L (v) ((v) & GENMASK(7, 0)) + +/* datasheet calls this sometimes MIPI_DIV[1:0], but mostly DIVPX[3:0] */ +#define OV5675_REG_PLL_CTRL3 0x303 +#define OV5675_PLL1_DIVPX (v) (((v) - 1) & GENMASK(1, 0)) + +/* datasheet uses both [0] and [1:0] as range */ +#define OV5675_REG_PLL_CTRL4 0x304 +#define OV5675_PLL1_PIX_BITDIV (v) ((v - 4) & BIT(0)) +#define OV5675_REG_PLL_CTRL5 0x305 +#define OV5675_PLL1_SYS_BITDIV (v) ((v - 4) & BIT(0)) + +#define OV5675_REG_PLL_CTRL6 0x306 +#define OV5675_PLL1_PREDIVP (v) ((v - 1) & BIT(0)) + +#define OV5675_REG_PLL_CTRLB 0x30b +#define OV5675_PLL2_PREDIV (v) ((v) & GENMASK(2, 0)) +#define OV5675_REG_PLL_CTRLC 0x30c +#define OV5675_PLL2_MUL_H (v) (((v) & GENMASK(9, 8)) >> 8) +#define OV5675_REG_PLL_CTRLD 0x30d +#define OV5675_PLL2_MUL_L (v) ((v) & GENMASK(7, 0)) +#define OV5675_REG_PLL_CTRLF 0x30f +#define OV5675_PLL2_SA1_DIV (v) ((v - 5) & GENMASK(1, 0)) +#define OV5675_REG_PLL_CTRL12 0x312 +#define OV5675_PLL2_PREDIVP_MASK BIT(4) +#define OV5675_PLL2_PREDIVP (v) ((((v) - 1) << 4) & BIT(4)) + +#define OV5675_REG_CLOCK_SEL 0x3020 +#define OV5675_PCLK_SEL_MASK BIT(3) +#define OV5675_PCLK_SEL (v) ((((v) - 1) << 3) & BIT(3)) +/* vertical-timings from sensor */ #define OV5675_REG_VTS 0x380e#define OV5675_VTS_30FPS 0x07e4
@@ -82,6 +118,74 @@#define to_ov5675 (_sd) container_of(_sd, struct ov5675, sd) +/* +* PLL1 Clock Tree: +* +* +-< EXTCLK +* | +* +-+ pll_predivp (0x307 [0]), partly labelled prediv0 +* | +* +-+ pll_prediv (0x300 [2:0], special values: +* | 0: 1, 1: 1.5, 2: 2, 3: 2.5, 4: 3, 5: 4, 6: 6, 7: 8) +* +-+ pll_multiplier (0x301 [1:0], 0x302 [7:0]) +* | +* +-+ pll_divpx (0x303 [3:0]), partly labelled mipi_div +* | | +* | +-> PHY_SCLK +* | | +* | +-+ pll_pix_bitdiv (0x304 [0], special values: 0: 4, 1: 5) +* | | +* | +-+ pclk_div (0x3020 [3]) +* | | +* | +-> PCLK +* | +* +-+ pll_sys_bitdiv (0x305 [0], special values: 0: 4, 1: 5) +*/ + +struct ov5675_pll1_config { +unsigned int pll_predivp; +unsigned int pll_prediv; +unsigned int pll_multiplier; +unsigned int pll_divpx; +unsigned int pll_pix_bitdiv; +unsigned int pll_sys_bitdiv; +unsigned int pclk_div; +}; + +/* +* PLL2 Clock Tree: +* +* +-< EXTCLK +* | +* +-+ pll_predivp (0x312 [4]), partly labelled prediv0 +* | +* +-+ pll_prediv (0x30b [2:0], special values: +* | 0: 1, 1: 1.5, 2: 2, 3: 2.5, 4: 3, 5: 4, 6: 6, 7: 8) +* +-+ pll_mul (0x30c [1:0], 0x30d [7:0]) +* | +* +-+ dac_div (0x312 [3:0]) +* | | +* | +-> DAC_CLK +* | +* +-+ pll_sa1_div (0x30f [3:0]) +* | +* + (pll graph specifies selector, not in register description) +* | +* +-> SCLK +*/ + +struct ov5675_pll2_config { +unsigned int pll_predivp; +unsigned int pll_prediv; +unsigned int pll_multiplier; +unsigned int pll_sa1_div; +}; + +struct ov5675_pll_configs { +const struct ov5675_pll1_config *pll1_config; +const struct ov5675_pll2_config *pll2_config; +}; +static const char *const ov5675_supply_names[] = {"avdd" ,/* Analog power */ "dovdd" ,/* Digital I/O power */
@@ -105,7 +209,7 @@ struct ov5675_reg_list { };struct ov5675_link_freq_config { -const struct ov5675_reg_list reg_list; +const struct ov5675_pll_configs **pll_configs; };struct ov5675_mode {
@@ -142,11 +246,31 @@ static const unsigned long supported_xvclk_rates[] = { [OV5675_XVCLK_19_2] = 19200000, }; -static const struct ov5675_reg mipi_data_rate_900mbps[] = { - {0x0300 ,0x04 }, - {0x0302 ,0x8d }, - {0x0303 ,0x00 }, - {0x030d ,0x26 }, +/* PLL Targets: PHY_SCLK = 900 MHz, PCLK = 112.5 MHz, SCLK = 90 MHz */ +static const struct ov5675_pll1_config ov5675_pll1_config_900mbps_19_2mhz = { + .pll_predivp =1 , + .pll_prediv =4 , + .pll_multiplier =141 , + .pll_divpx =1 , + .pll_pix_bitdiv =4 , + .pll_sys_bitdiv =5 , + .pclk_div =2 , +}; + +static const struct ov5675_pll2_config ov5675_pll2_config_900mbps_19_2mhz = { + .pll_predivp =1 , + .pll_prediv =0 , + .pll_multiplier =38 , + .pll_sa1_div =8 , +}; + +static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_19_2mhz = { + .pll1_config = &ov5675_pll1_config_900mbps_19_2mhz, + .pll2_config = &ov5675_pll2_config_900mbps_19_2mhz, +}; + +static const struct ov5675_pll_configs *ov5675_pll_configs_900mbps[] = { + [OV5675_XVCLK_19_2 ] = &ov5675_pll_configs_900mbps_19_2mhz, };static const struct ov5675_reg mode_2592x1944_regs[] = {
@@ -469,10 +593,7 @@ static const s64 link_freq_menu_items[] = {static const struct ov5675_link_freq_config link_freq_configs[] = { [OV5675_LINK_FREQ_900MBPS ] = { - .reg_list = { - .num_of_regs =ARRAY_SIZE (mipi_data_rate_900mbps), - .regs = mipi_data_rate_900mbps, - } + .pll_configs = ov5675_pll_configs_900mbps, } };
@@ -513,6 +634,7 @@ struct ov5675 {unsigned long xvclk_rate;struct gpio_desc *reset_gpio;struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES ]; +int pll_config_index;/* V4L2 Controls */ struct v4l2_ctrl *link_freq;
@@ -595,6 +717,22 @@ static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)return 0 ; } +static int ov5675_update_reg_bits (struct ov5675 *ov5675,u16 reg,u16 len, +u8 mask,u8 bits) +{ +u32 val =0 ; +int ret; + + ret =ov5675_read_reg (ov5675, reg, len, &val); +if (ret) +return ret; + + val &= ~mask; + val |= bits; + +return ov5675_write_reg (ov5675, reg, len, val); +} +static int ov5675_write_reg_list (struct ov5675 *ov5675,const struct ov5675_reg_list *r_list) {
@@ -919,9 +1057,125 @@ static int ov5675_init_module(struct ov5675 *ov5675)return 0 ; } +static int ov5675_set_pll1_rate (struct ov5675 *ov5675, +const struct ov5675_pll1_config *config) +{ +int ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL0 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PREDIV (config->pll_prediv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL1 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_MUL_H (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL2 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_MUL_L (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL3 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_DIVPX (config->pll_divpx)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL4 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PIX_BITDIV (config->pll_pix_bitdiv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL5 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_SYS_BITDIV (config->pll_sys_bitdiv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL6 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PREDIVP (config->pll_predivp)); +if (ret) +return ret; + + ret =ov5675_update_reg_bits (ov5675,OV5675_REG_CLOCK_SEL , +OV5675_REG_VALUE_08BIT , +OV5675_PCLK_SEL_MASK , +OV5675_PCLK_SEL (config->pclk_div)); +if (ret) +return ret; + +return 0 ; +} + + +static int ov5675_set_pll2_rate (struct ov5675 *ov5675, +const struct ov5675_pll2_config *config) +{ +int ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLB , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_PREDIV (config->pll_prediv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLC , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_MUL_H (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLD , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_MUL_L (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLF , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_SA1_DIV (config->pll_sa1_div)); +if (ret) +return ret; + + ret =ov5675_update_reg_bits (ov5675,OV5675_REG_PLL_CTRL12 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_PREDIVP_MASK , +OV5675_PLL2_PREDIVP (config->pll_predivp)); + +return 0 ; +} + +static int ov5675_set_pll_rates (struct ov5675 *ov5675, +const struct ov5675_pll_configs *configs) +{ +int ret; + + ret =ov5675_set_pll1_rate (ov5675, configs->pll1_config); +if (ret) { +dev_err (ov5675->dev,"failed to set PLL1 config\n" ); +return ret; + } + + ret =ov5675_set_pll2_rate (ov5675, configs->pll2_config); +if (ret) { +dev_err (ov5675->dev,"failed to set PLL2 config\n" ); +return ret; + } + +return 0 ; +} +static int ov5675_start_streaming (struct ov5675 *ov5675) {const struct ov5675_reg_list *reg_list; +const struct ov5675_pll_configs *pll_configs;int link_freq_index, ret; ret =ov5675_identify_module (ov5675);
@@ -933,12 +1187,11 @@ static int ov5675_start_streaming(struct ov5675 *ov5675)return ret; link_freq_index = ov5675->cur_mode->link_freq_index; - reg_list = &link_freq_configs[link_freq_index].reg_list; - ret =ov5675_write_reg_list (ov5675, reg_list); -if (ret) { -dev_err (ov5675->dev,"failed to set plls" ); + + pll_configs = link_freq_configs[link_freq_index].pll_configs[ov5675->pll_config_index]; + ret =ov5675_set_pll_rates (ov5675, pll_configs); +if (ret)return ret; - } reg_list = &ov5675->cur_mode->reg_list; ret =ov5675_write_reg_list (ov5675, reg_list);
@@ -1229,8 +1482,10 @@ static int ov5675_get_hwcfg(struct ov5675 *ov5675) ov5675->xvclk_rate =clk_get_rate (ov5675->xvclk);for (i =0 ; i <ARRAY_SIZE (supported_xvclk_rates); i++) { -if (ov5675->xvclk_rate == supported_xvclk_rates[i]) +if (ov5675->xvclk_rate == supported_xvclk_rates[i]) { + ov5675->pll_config_index = i;break ; + } }if (i ==ARRAY_SIZE (supported_xvclk_rates)) {
Signature · 1 line
2.47.3
Re: [PATCH 4/5] media: i2c: ov5675: Handle PLLs using actual register descriptions
Hi Heiko,
Quoted 35 lines · Heiko Stuebner
From: Heiko Stuebner <[email protected]>
The datasheet actually describes the clock tree, so we can set PLL rates using actual code and not via an opaque register write list.
This allows the easily add additional rates for different input clocks in the future.
With the old hard values + the default values in the other registers, I was able to verify that the previous settings result in the correct 900MHz MIPI-PHY rate, 112MHz MIPI PCLK and 90MHz SCLK when following the clock diagram in the manual and re-calculating the rates from that. (Exact values are 902.4MHz, 112.8MHz, 91.2MHz, coming from the 19.2MHz)
A caveat is that the datasheet is not fully consistent between the graphical clock diagram and the register description - with the diagram showing another selector between both PLLs, not mentioned in the register description.
Signed-off-by Heiko Stuebner <[email protected]>--- drivers/media/i2c/ov5675.c | 287 ++++++++++++++++++++++++++++++++++--- 1 file changed, 271 insertions(+), 16 deletions(-)drivers/media/i2c/ov5675.c +3
-0@@ -34,6 +34,42 @@#define OV5675_REG_SOFTWARE_RST 0x0103#define OV5675_SOFTWARE_RST BIT(0) +#define OV5675_REG_PLL_CTRL0 0x300 +#define OV5675_PLL1_PREDIV (v) ((v) & GENMASK(2, 0)) +#define OV5675_REG_PLL_CTRL1 0x301
drivers/media/i2c/ov5675.c line 49
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+#define OV5675_PLL1_MUL_H (v) (((v) & GENMASK(9, 8)) >> 8) +#define OV5675_REG_PLL_CTRL2 0x302 +#define OV5675_PLL1_MUL_L (v) ((v) & GENMASK(7, 0)) + +/* datasheet calls this sometimes MIPI_DIV[1:0], but mostly DIVPX[3:0] */ +#define OV5675_REG_PLL_CTRL3 0x303 +#define OV5675_PLL1_DIVPX (v) (((v) - 1) & GENMASK(1, 0)) +> +/* datasheet uses both [0] and [1:0] as range */ +#define OV5675_REG_PLL_CTRL4 0x304 +#define OV5675_PLL1_PIX_BITDIV (v) ((v - 4) & BIT(0))
issue: Missing parentheses around v.
drivers/media/i2c/ov5675.c line 51
Quoted 2 lines
drivers/media/i2c/ov5675.c +2
-0+#define OV5675_REG_PLL_CTRL5 0x305 +#define OV5675_PLL1_SYS_BITDIV (v) ((v - 4) & BIT(0))
issue: Missing parentheses around v.
issue: wrong mask. datasheet says it's bit[1:0]. (and you don't explain if that's on purpose as with OV5675_PLL1_PIX_BITDIV or OV5675_PLL1_DIVPX).
drivers/media/i2c/ov5675.c line 53
Quoted 2 lines
drivers/media/i2c/ov5675.c +2
-0+ +#define OV5675_REG_PLL_CTRL6 0x306
issue: there's not CTRL6, it's CTRL7 and it's at offset 0x307.
drivers/media/i2c/ov5675.c line 54
Quoted 1 line
drivers/media/i2c/ov5675.c +1
-0+#define OV5675_PLL1_PREDIVP (v) ((v - 1) & BIT(0))
issue: Missing parentheses around v.
drivers/media/i2c/ov5675.c line 57
Quoted 3 lines
drivers/media/i2c/ov5675.c +3
-0+ +#define OV5675_REG_PLL_CTRLB 0x30b +#define OV5675_PLL2_PREDIV (v) ((v) & GENMASK(2, 0))
issue: wrong mask. datasheet says it's bit[0].
drivers/media/i2c/ov5675.c line 63
Quoted 6 lines
drivers/media/i2c/ov5675.c +6
-0+#define OV5675_REG_PLL_CTRLC 0x30c +#define OV5675_PLL2_MUL_H (v) (((v) & GENMASK(9, 8)) >> 8) +#define OV5675_REG_PLL_CTRLD 0x30d +#define OV5675_PLL2_MUL_L (v) ((v) & GENMASK(7, 0)) +#define OV5675_REG_PLL_CTRLF 0x30f +#define OV5675_PLL2_SA1_DIV (v) ((v - 5) & GENMASK(1, 0))
issue: Missing parentheses around v.
issue: wrong mask. datasheet says it's bit[3:0].
drivers/media/i2c/ov5675.c line 71
Quoted 8 lines
drivers/media/i2c/ov5675.c +8
-0+#define OV5675_REG_PLL_CTRL12 0x312 +#define OV5675_PLL2_PREDIVP_MASK BIT(4) +#define OV5675_PLL2_PREDIVP (v) ((((v) - 1) << 4) & BIT(4)) + +#define OV5675_REG_CLOCK_SEL 0x3020 +#define OV5675_PCLK_SEL_MASK BIT(3) +#define OV5675_PCLK_SEL (v) ((((v) - 1) << 3) & BIT(3)) +
suggestion: Any chance I can convince you to use field_get and field_prep for all the above?
That should give us:
#define OV5675_PLL2_MUL_H(v) field_get(GENMASK(9, 8), (v))
#define OV5675_PCLK_SEL(v) field_prep(OV5675_PCLK_SEL_MASK, (v) - 1)
I believe? That should avoid forgetting to mask or shift stuff.
drivers/media/i2c/ov5675.c line 126
Quoted 13 lines
drivers/media/i2c/ov5675.c +6
-0/* vertical-timings from sensor */ #define OV5675_REG_VTS 0x380e#define OV5675_VTS_30FPS 0x07e4@@ -82,6 +118,74 @@#define to_ov5675 (_sd) container_of(_sd, struct ov5675, sd) +/* +* PLL1 Clock Tree: +* +* +-< EXTCLK +* | +* +-+ pll_predivp (0x307 [0]), partly labelled prediv0
"special" value here too with 0=1 and 1=2.
drivers/media/i2c/ov5675.c line 132
Quoted 6 lines
drivers/media/i2c/ov5675.c +6
-0+* | +* +-+ pll_prediv (0x300 [2:0], special values: +* | 0: 1, 1: 1.5, 2: 2, 3: 2.5, 4: 3, 5: 4, 6: 6, 7: 8) +* +-+ pll_multiplier (0x301 [1:0], 0x302 [7:0]) +* | +* +-+ pll_divpx (0x303 [3:0]), partly labelled mipi_div
special value here too because the divider is 1+the value stored in the register.
drivers/media/i2c/ov5675.c line 138
Quoted 6 lines
drivers/media/i2c/ov5675.c +6
-0+* | | +* | +-> PHY_SCLK +* | | +* | +-+ pll_pix_bitdiv (0x304 [0], special values: 0: 4, 1: 5) +* | | +* | +-+ pclk_div (0x3020 [3])
special value here too with 0=1, 1=2.
Quoted 24 lines
drivers/media/i2c/ov5675.c +24
-0+* | | +* | +-> PCLK +* | +* +-+ pll_sys_bitdiv (0x305 [0], special values: 0: 4, 1: 5) +*/ + +struct ov5675_pll1_config { +unsigned int pll_predivp; +unsigned int pll_prediv; +unsigned int pll_multiplier; +unsigned int pll_divpx; +unsigned int pll_pix_bitdiv; +unsigned int pll_sys_bitdiv; +unsigned int pclk_div; +}; + +/* +* PLL2 Clock Tree: +* +* +-< EXTCLK +* | +* +-+ pll_predivp (0x312 [4]), partly labelled prediv0 +* | +* +-+ pll_prediv (0x30b [2:0], special values:
drivers/media/i2c/ov5675.c line 172
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+* | 0: 1, 1: 1.5, 2: 2, 3: 2.5, 4: 3, 5: 4, 6: 6, 7: 8) +* +-+ pll_mul (0x30c [1:0], 0x30d [7:0]) +* | +* +-+ dac_div (0x312 [3:0]) +* | | +* | +-> DAC_CLK +* | +* +-+ pll_sa1_div (0x30f [3:0]) +* | +* + (pll graph specifies selector, not in register description)
sugesstion: Still mention it in any case, there's a mux between pll_sys_bitdiv and pll_sa1_div in register 0x3660 bit[4].
question: I'm curious now, what's the value of the mux? Wondering if we use PLL2 at all (the init sequence doesn't set it, and since it's not defined in the register description, we don't know its reset value either).
question: there's an additional divider after the mux before it becomes SCLK, in register 0x3106[3:2] (the register description specifies that as "sclk_sel" but the tree clock implies it's divider 1/2/4 so no clue what to make of it (the reset value according to the datasheet is 0b01). What about it?
Quoted 45 lines
drivers/media/i2c/ov5675.c +25
-6+* | +* +-> SCLK +*/ + +struct ov5675_pll2_config { +unsigned int pll_predivp; +unsigned int pll_prediv; +unsigned int pll_multiplier; +unsigned int pll_sa1_div; +}; + +struct ov5675_pll_configs { +const struct ov5675_pll1_config *pll1_config; +const struct ov5675_pll2_config *pll2_config; +}; +static const char *const ov5675_supply_names[] = {"avdd" ,/* Analog power */ "dovdd" ,/* Digital I/O power */ @@ -105,7 +209,7 @@ struct ov5675_reg_list { };struct ov5675_link_freq_config { -const struct ov5675_reg_list reg_list; +const struct ov5675_pll_configs **pll_configs; };struct ov5675_mode {@@ -142,11 +246,31 @@ static const unsigned long supported_xvclk_rates[] = { [OV5675_XVCLK_19_2] = 19200000, }; -static const struct ov5675_reg mipi_data_rate_900mbps[] = { - {0x0300 ,0x04 }, - {0x0302 ,0x8d }, - {0x0303 ,0x00 }, - {0x030d ,0x26 }, +/* PLL Targets: PHY_SCLK = 900 MHz, PCLK = 112.5 MHz, SCLK = 90 MHz */ +static const struct ov5675_pll1_config ov5675_pll1_config_900mbps_19_2mhz = { + .pll_predivp =1 , + .pll_prediv =4 , + .pll_multiplier =141 , + .pll_divpx =1 , + .pll_pix_bitdiv =4 , + .pll_sys_bitdiv =5 ,
drivers/media/i2c/ov5675.c static const unsigned long supported_xvclk_rates[] = { · line 266
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+ .pclk_div = 2, +}; + +static const struct ov5675_pll2_config ov5675_pll2_config_900mbps_19_2mhz = { + .pll_predivp =1 , + .pll_prediv =0 , + .pll_multiplier =38 , + .pll_sa1_div =8 , +}; +
praise: I very much like that the order of the declaration of members matches the order of the clock dividers and multipliers (top to bottom is left to right in the schematics), that makes things easier to follow, thanks for doing that!
drivers/media/i2c/ov5675.c static const unsigned long supported_xvclk_rates[] = { · line 275
Quoted 9 lines
drivers/media/i2c/ov5675.c +7
-0+static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_19_2mhz = { + .pll1_config = &ov5675_pll1_config_900mbps_19_2mhz, + .pll2_config = &ov5675_pll2_config_900mbps_19_2mhz, +}; + +static const struct ov5675_pll_configs *ov5675_pll_configs_900mbps[] = { + [OV5675_XVCLK_19_2 ] = &ov5675_pll_configs_900mbps_19_2mhz, };
suggestion: I find it tiring to have to go through so many levels of indirection to get to the actual value. How about having them declared directly in ov5675_pll_configs_900mbps? They are anyway specific to the link frequency (requesting 900MHz PHY SCLK, I'm assuming this is linked to the requested link frequency?).
Something like:
drivers/media/i2c/ov5675.c +41 -53
@@ -182,8 +182,8 @@ struct ov5675_pll2_config { };struct ov5675_pll_configs { -const struct ov5675_pll1_config *pll1_config; -const struct ov5675_pll2_config *pll2_config; +const struct ov5675_pll1_config pll1_config; +const struct ov5675_pll2_config pll2_config; };static const char *const ov5675_supply_names[] = {
@@ -209,7 +209,7 @@ struct ov5675_reg_list { };struct ov5675_link_freq_config { -const struct ov5675_pll_configs **pll_configs; +const struct ov5675_pll_configs *pll_configs; };struct ov5675_mode {
@@ -249,53 +249,41 @@ static const unsigned long supported_xvclk_rates[] = { };/* PLL Targets: PHY_SCLK = 900 MHz, PCLK = 112.5 MHz, SCLK = 90 MHz */ -static const struct ov5675_pll1_config ov5675_pll1_config_900mbps_19_2mhz = { - .pll_predivp =1 , - .pll_prediv =4 , - .pll_multiplier =141 , - .pll_divpx =1 , - .pll_pix_bitdiv =4 , - .pll_sys_bitdiv =5 , - .pclk_div =2 , -}; - -static const struct ov5675_pll2_config ov5675_pll2_config_900mbps_19_2mhz = { - .pll_predivp =1 , - .pll_prediv =0 , - .pll_multiplier =38 , - .pll_sa1_div =8 , -}; - -static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_19_2mhz = { - .pll1_config = &ov5675_pll1_config_900mbps_19_2mhz, - .pll2_config = &ov5675_pll2_config_900mbps_19_2mhz, -}; - -static const struct ov5675_pll1_config ov5675_pll1_config_900mbps_24_0mhz = { - .pll_predivp =1 , - .pll_prediv =5 , - .pll_multiplier =150 , - .pll_divpx =1 , - .pll_pix_bitdiv =4 , - .pll_sys_bitdiv =5 , - .pclk_div =2 , -}; - -static const struct ov5675_pll2_config ov5675_pll2_config_900mbps_24_0mhz = { - .pll_predivp =1 , - .pll_prediv =0 , - .pll_multiplier =30 , - .pll_sa1_div =8 , -}; - -static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_24_0mhz = { - .pll1_config = &ov5675_pll1_config_900mbps_24_0mhz, - .pll2_config = &ov5675_pll2_config_900mbps_24_0mhz, -}; - -static const struct ov5675_pll_configs *ov5675_pll_configs_900mbps[] = { - [OV5675_XVCLK_19_2 ] = &ov5675_pll_configs_900mbps_19_2mhz, - [OV5675_XVCLK_24_0 ] = &ov5675_pll_configs_900mbps_24_0mhz, +static const struct ov5675_pll_configs ov5675_pll_configs_900mbps[] = { + [OV5675_XVCLK_19_2 ] = { + .pll1_config = { + .pll_predivp =1 , + .pll_prediv =4 , + .pll_multiplier =141 , + .pll_divpx =1 , + .pll_pix_bitdiv =4 , + .pll_sys_bitdiv =5 , + .pclk_div =2 , + }, + .pll2_config = { + .pll_predivp =1 , + .pll_prediv =0 , + .pll_multiplier =38 , + .pll_sa1_div =8 , + }, + }, + [OV5675_XVCLK_24_0 ] = { + .pll1_config = { + .pll_predivp =1 , + .pll_prediv =5 , + .pll_multiplier =150 , + .pll_divpx =1 , + .pll_pix_bitdiv =4 , + .pll_sys_bitdiv =5 , + .pclk_div =2 , + }, + .pll2_config = { + .pll_predivp =1 , + .pll_prediv =0 , + .pll_multiplier =30 , + .pll_sa1_div =8 , + }, + }, };static const struct ov5675_reg mode_2592x1944_regs[] = {
@@ -1182,13 +1170,13 @@ static int ov5675_set_pll_rates(struct ov5675 *ov5675, {int ret; - ret =ov5675_set_pll1_rate (ov5675, configs->pll1_config); + ret =ov5675_set_pll1_rate (ov5675, &configs->pll1_config);if (ret) {dev_err (ov5675->dev,"failed to set PLL1 config\n" );return ret; } - ret =ov5675_set_pll2_rate (ov5675, configs->pll2_config); + ret =ov5675_set_pll2_rate (ov5675, &configs->pll2_config);if (ret) {dev_err (ov5675->dev,"failed to set PLL2 config\n" );return ret;
@@ -1213,7 +1201,7 @@ static int ov5675_start_streaming(struct ov5675 *ov5675) link_freq_index = ov5675->cur_mode->link_freq_index; - pll_configs = link_freq_configs[link_freq_index].pll_configs[ov5675->pll_config_index]; + pll_configs = &link_freq_configs[link_freq_index].pll_configs[ov5675->pll_config_index]; ret =ov5675_set_pll_rates (ov5675, pll_configs);if (ret)return ret;
Maybe? (only build tested).
Quoted 22 lines
drivers/media/i2c/ov5675.c +2
-4static const struct ov5675_reg mode_2592x1944_regs[] = {@@ -469,10 +593,7 @@ static const s64 link_freq_menu_items[] = {static const struct ov5675_link_freq_config link_freq_configs[] = { [OV5675_LINK_FREQ_900MBPS ] = { - .reg_list = { - .num_of_regs =ARRAY_SIZE (mipi_data_rate_900mbps), - .regs = mipi_data_rate_900mbps, - } + .pll_configs = ov5675_pll_configs_900mbps, } };@@ -513,6 +634,7 @@ struct ov5675 {unsigned long xvclk_rate;struct gpio_desc *reset_gpio;struct regulator_bulk_data supplies[OV5675_NUM_SUPPLIES ]; +int pll_config_index;/* V4L2 Controls */ struct v4l2_ctrl *link_freq;
drivers/media/i2c/ov5675.c static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val) · line 721
Quoted 5 lines
drivers/media/i2c/ov5675.c +2
-0@@ -595,6 +717,22 @@ static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)return 0 ; } +static int ov5675_update_reg_bits (struct ov5675 *ov5675,u16 reg,u16 len, +u8 mask,u8 bits)
question: Why u8 when the read and write functions expect a u32?
drivers/media/i2c/ov5675.c static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val) · line 731
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+{ +u32 val =0 ; +int ret; + + ret =ov5675_read_reg (ov5675, reg, len, &val); +if (ret) +return ret; + + val &= ~mask; + val |= bits;
issue: Please mask bits as well otherwise we may overflow (you can take the cci_update_bits implementation as a reference).
drivers/media/i2c/ov5675.c static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val) · line 735
Quoted 4 lines
drivers/media/i2c/ov5675.c +4
-0+ +return ov5675_write_reg (ov5675, reg, len, val); +} +
suggestion: FYI, there's cci_update_bits() now that is available for v4l2 device, c.f. 273f52f3cbec ("media: i2c: ov9282: Convert to CCI register access helpers") which should help you not have to reimplement yet another update_bits function. It's unrelated to adding support for a new clock frequency, so feel free to ignore this. Using that API, you could also simplify the handling of multi-register values (e.g. for OV5675_PLL[12]_MUL_[HL]) into a single cci_write() call.
Quoted 5 lines
drivers/media/i2c/ov5675.c +0
-0static int ov5675_write_reg_list (struct ov5675 *ov5675,const struct ov5675_reg_list *r_list) {@@ -919,9 +1057,125 @@ static int ov5675_init_module(struct ov5675 *ov5675)return 0 ;
drivers/media/i2c/ov5675.c static int ov5675_init_module(struct ov5675 *ov5675) · line 1067
Quoted 10 lines
drivers/media/i2c/ov5675.c +8
-0} +static int ov5675_set_pll1_rate (struct ov5675 *ov5675, +const struct ov5675_pll1_config *config) +{ +int ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL0 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PREDIV (config->pll_prediv));
issue: incorrect logic. pll_prediv is an unsigned long member, and the macro simply removes one (1) from it, this definitely does not match what the datasheet says about the register values meaning 1/1.5/2/2.5/3/4/6/8. Just define a constant for each value and set pll_prediv to that value? Please document this in the struct as well, so we know the value of that particular member shouldn't be used verbatim in the clock frequency computation.
Quoted 37 lines
drivers/media/i2c/ov5675.c +37
-0+if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL1 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_MUL_H (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL2 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_MUL_L (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL3 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_DIVPX (config->pll_divpx)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL4 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PIX_BITDIV (config->pll_pix_bitdiv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL5 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_SYS_BITDIV (config->pll_sys_bitdiv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRL6 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL1_PREDIVP (config->pll_predivp)); +if (ret)
drivers/media/i2c/ov5675.c static int ov5675_init_module(struct ov5675 *ov5675) · line 1114
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+return ret; + + ret =ov5675_update_reg_bits (ov5675,OV5675_REG_CLOCK_SEL , +OV5675_REG_VALUE_08BIT , +OV5675_PCLK_SEL_MASK , +OV5675_PCLK_SEL (config->pclk_div)); +if (ret) +return ret; + +return 0 ;
nitpick: Last function call can be used directly as return to avoid the last four lines (applicable to other functions in this patch but won't repeat it).
Quoted 27 lines
drivers/media/i2c/ov5675.c +27
-0+} + + +static int ov5675_set_pll2_rate (struct ov5675 *ov5675, +const struct ov5675_pll2_config *config) +{ +int ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLB , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_PREDIV (config->pll_prediv)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLC , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_MUL_H (config->pll_multiplier)); +if (ret) +return ret; + + ret =ov5675_write_reg (ov5675,OV5675_REG_PLL_CTRLD , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_MUL_L (config->pll_multiplier)); +if (ret) +return ret; + + ret = ov5675_write_reg(ov5675, OV5675_REG_PLL_CTRLF,
drivers/media/i2c/ov5675.c static int ov5675_init_module(struct ov5675 *ov5675) · line 1151
Quoted 10 lines
drivers/media/i2c/ov5675.c +10
-0+OV5675_REG_VALUE_08BIT , + OV5675_PLL2_SA1_DIV(config->pll_sa1_div)); +if (ret) +return ret; + + ret =ov5675_update_reg_bits (ov5675,OV5675_REG_PLL_CTRL12 , +OV5675_REG_VALUE_08BIT , +OV5675_PLL2_PREDIVP_MASK , +OV5675_PLL2_PREDIVP (config->pll_predivp)); +
issue: incorrect logic. pll_prediv is an unsigned long member, and the macro simply removes one (1) from it, this definitely does not match what the datasheet says about the register values meaning 1/1.5/2/2.5/3/4/6/8. Just define a constant for each value and set pll_prediv to that value? Considering that 19.2MHz, 24MHz and 6MHz all set that value to 0, another option is to hardcode it for now without implementing the actual logic? Please document this in the struct as well, so we know the value of that particular member shouldn't be used verbatim in the clock frequency computation.
drivers/media/i2c/ov5675.c static int ov5675_init_module(struct ov5675 *ov5675) · line 1152
Quoted 1 line
drivers/media/i2c/ov5675.c +1
-0+return 0 ;
issue: Why are you ignoring the ret value here suddenly?
Quoted 16 lines
drivers/media/i2c/ov5675.c +16
-0+} + +static int ov5675_set_pll_rates (struct ov5675 *ov5675, +const struct ov5675_pll_configs *configs) +{ +int ret; + + ret =ov5675_set_pll1_rate (ov5675, configs->pll1_config); +if (ret) { +dev_err (ov5675->dev,"failed to set PLL1 config\n" ); +return ret; + } + + ret =ov5675_set_pll2_rate (ov5675, configs->pll2_config); +if (ret) { +dev_err (ov5675->dev,"failed to set PLL2 config\n" );
drivers/media/i2c/ov5675.c static int ov5675_init_module(struct ov5675 *ov5675) · line 1178
Quoted 10 lines
drivers/media/i2c/ov5675.c +7
-0+return ret; + } + +return 0 ; +} +static int ov5675_start_streaming (struct ov5675 *ov5675) {const struct ov5675_reg_list *reg_list; +const struct ov5675_pll_configs *pll_configs;
nitpick: Reorder so longest line is first.
Cheers,
Quentin
PATCH 5/5 media: i2c: ov5675: Add PLL settings for a 24MHz input clock
In reply to Heiko StuebnerFrom: Heiko Stuebner <[email protected]>
The ov5675 datasheet actually only specifies settings for 6MHz and 24MHz input clocks. As some boards cannot supply the correct 19.2MHz currently required, add a second set of PLL configs for said 24MHz input clock with the values specified in the datasheet.
Using the same calculation over the clock-diagram results in the correct expected clocks for MIPI_PHYCLK of 900MHz, MIPI_PCLK of 112MHz and SCLK of 90MHz.
--- drivers/media/i2c/ov5675.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
drivers/media/i2c/ov5675.c +25 -0
@@ -239,11 +239,13 @@ struct ov5675_mode {enum xvclk_rate {OV5675_XVCLK_19_2 , +OV5675_XVCLK_24_0 ,OV5675_NUM_SUPPORTED_RATES };static const unsigned long supported_xvclk_rates[] = { [OV5675_XVCLK_19_2 ] =19200000 , + [OV5675_XVCLK_24_0 ] =24000000 , };/* PLL Targets: PHY_SCLK = 900 MHz, PCLK = 112.5 MHz, SCLK = 90 MHz */
@@ -269,8 +271,31 @@ static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_19_2mhz = { .pll2_config = &ov5675_pll2_config_900mbps_19_2mhz, }; +static const struct ov5675_pll1_config ov5675_pll1_config_900mbps_24_0mhz = { + .pll_predivp =1 , + .pll_prediv =5 , + .pll_multiplier =150 , + .pll_divpx =1 , + .pll_pix_bitdiv =4 , + .pll_sys_bitdiv =5 , + .pclk_div =2 , +}; + +static const struct ov5675_pll2_config ov5675_pll2_config_900mbps_24_0mhz = { + .pll_predivp =1 , + .pll_prediv =0 , + .pll_multiplier =30 , + .pll_sa1_div =8 , +}; + +static const struct ov5675_pll_configs ov5675_pll_configs_900mbps_24_0mhz = { + .pll1_config = &ov5675_pll1_config_900mbps_24_0mhz, + .pll2_config = &ov5675_pll2_config_900mbps_24_0mhz, +}; +static const struct ov5675_pll_configs *ov5675_pll_configs_900mbps[] = { [OV5675_XVCLK_19_2 ] = &ov5675_pll_configs_900mbps_19_2mhz, + [OV5675_XVCLK_24_0 ] = &ov5675_pll_configs_900mbps_24_0mhz, };static const struct ov5675_reg mode_2592x1944_regs[] = {
Signature · 1 line
2.47.3
Re: [PATCH 5/5] media: i2c: ov5675: Add PLL settings for a 24MHz input clock
Hi Heiko,
Quoted 11 lines · Heiko Stuebner
From: Heiko Stuebner <[email protected]>
The ov5675 datasheet actually only specifies settings for 6MHz and 24MHz input clocks. As some boards cannot supply the correct 19.2MHz currently required, add a second set of PLL configs for said 24MHz input clock with the values specified in the datasheet.
Using the same calculation over the clock-diagram results in the correct expected clocks for
MIPI_PHYCLKof 900MHz,MIPI_PCLKof 112MHz and SCLK of 90MHz.
I'm assuming we'll have eventually different values for pll_prediv member due to review provided on patch 4/5, so I'm not giving my Rb yet, but the content here is fine by me.
Thanks!
Quentin