i2c: core: Provide generic definitions for bus frequencies
Subsystems (7)
PATCH v4 1/6 i2c: core: Provide generic definitions for bus frequencies
There are few maximum bus frequencies being used in the I²C core code. Provide generic definitions for bus frequencies and use them in the core.
The drivers may use predefined constants where it is appropriate. Some of them are already using these under slightly different names. We will convert them later to use newly introduced defines.
Note, the name of modes are chosen to follow well established naming scheme [1].
These definitions will also help to avoid typos in the numbers that may lead to subtle errors.
[1]: https://en.wikipedia.org/wiki/I%C2%B2C#Differences_between_modes
---
v4: no changes
drivers/i2c/i2c-core-acpi.c | 2 +- drivers/i2c/i2c-core-base.c | 8 ++++---- include/linux/i2c.h | 8 ++++++++ 3 files changed, 13 insertions(+), 5 deletions(-)
drivers/i2c/i2c-core-acpi.c +1 -1
@@ -318,7 +318,7 @@ static acpi_status i2c_acpi_lookup_speed(acpi_handle handle, u32 level, lookup->min_speed = lookup->speed;if (acpi_match_device_ids (adev, i2c_acpi_force_400khz_device_ids) ==0 ) - lookup->force_speed =400000 ; + lookup->force_speed =I2C_MAX_FAST_MODE_FREQ ;return AE_OK ; }
drivers/i2c/i2c-core-base.c +4 -4
@@ -1612,13 +1612,13 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de ret =device_property_read_u32 (dev,"clock-frequency" , &t->bus_freq_hz);if (ret && use_defaults) - t->bus_freq_hz =100000 ; + t->bus_freq_hz =I2C_MAX_STANDARD_MODE_FREQ ; ret =device_property_read_u32 (dev,"i2c-scl-rising-time-ns" , &t->scl_rise_ns);if (ret && use_defaults) { -if (t->bus_freq_hz <=100000 ) +if (t->bus_freq_hz <=I2C_MAX_STANDARD_MODE_FREQ ) t->scl_rise_ns =1000 ; -else if (t->bus_freq_hz <=400000 ) +else if (t->bus_freq_hz <=I2C_MAX_FAST_MODE_FREQ ) t->scl_rise_ns =300 ;else t->scl_rise_ns =120 ;
@@ -1626,7 +1626,7 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de ret =device_property_read_u32 (dev,"i2c-scl-falling-time-ns" , &t->scl_fall_ns);if (ret && use_defaults) { -if (t->bus_freq_hz <=400000 ) +if (t->bus_freq_hz <=I2C_MAX_FAST_MODE_FREQ ) t->scl_fall_ns =300 ;else t->scl_fall_ns =120 ;
include/linux/i2c.h +8 -0
@@ -39,6 +39,14 @@ enum i2c_slave_event;typedef int (*i2c_slave_cb_t )(struct i2c_client *client,enum i2c_slave_event event,u8 *val); +/* I2C Frequency Modes */ +#define I2C_MAX_STANDARD_MODE_FREQ 100000 +#define I2C_MAX_FAST_MODE_FREQ 400000 +#define I2C_MAX_FAST_MODE_PLUS_FREQ 1000000 +#define I2C_MAX_TURBO_MODE_FREQ 1400000 +#define I2C_MAX_HIGH_SPEED_MODE_FREQ 3400000 +#define I2C_MAX_ULTRA_FAST_MODE_FREQ 5000000 +struct module ;struct property_entry ;
Signature · 1 line
2.25.1
PATCH v4 2/6 i2c: core: Allow override timing properties with 0
Some drivers may allow to override properties with 0 value when defaults are not in use, thus, replace memset() with corresponding per property update.
---
v4: replace {0} with { 0 } (Wolfram)
drivers/i2c/busses/i2c-rcar.c | 2 +- drivers/i2c/i2c-core-base.c | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-rcar.c +1 -1
@@ -920,7 +920,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)struct rcar_i2c_priv *priv;struct i2c_adapter *adap;struct device *dev = &pdev->dev; -struct i2c_timings i2c_t; +struct i2c_timings i2c_t = {0 };int ret;/* Otherwise logic will break because some bytes must always use PIO */
drivers/i2c/i2c-core-base.c +17 -13
@@ -1593,23 +1593,21 @@ EXPORT_SYMBOL(i2c_del_adapter);* @dev: The device to scan for I2C timing properties * @t: the i2c_timings struct to be filled with values * @use_defaults: bool to use sane defaults derived from the I2C specification -* when properties are not found, otherwise use 0 +* when properties are not found, otherwise don't update * * Scan the device for the generic I2C properties describing timing parameters * for the signal and fill the given struct with the results. If a property was * not found and use_defaults was true, then maximum timings are assumed which * are derived from the I2C specification. If use_defaults is not used, the -* results will be 0, so drivers can apply their own defaults later. The latter -* is mainly intended for avoiding regressions of existing drivers which want -* to switch to this function. New drivers almost always should use the defaults. +* results will be as before, so drivers can apply their own defaults before +* calling this helper. The latter is mainly intended for avoiding regressions +* of existing drivers which want to switch to this function. New drivers +* almost always should use the defaults. */ -void i2c_parse_fw_timings (struct device *dev,struct i2c_timings *t,bool use_defaults) {int ret; -memset (t,0 ,sizeof (*t)); - ret =device_property_read_u32 (dev,"clock-frequency" , &t->bus_freq_hz);if (ret && use_defaults) t->bus_freq_hz =I2C_MAX_STANDARD_MODE_FREQ ;
@@ -1632,19 +1630,25 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de t->scl_fall_ns =120 ; } -device_property_read_u32 (dev,"i2c-scl-internal-delay-ns" , &t->scl_int_delay_ns); + ret =device_property_read_u32 (dev,"i2c-scl-internal-delay-ns" , &t->scl_int_delay_ns); +if (ret && use_defaults) + t->scl_int_delay_ns =0 ; ret =device_property_read_u32 (dev,"i2c-sda-falling-time-ns" , &t->sda_fall_ns);if (ret && use_defaults) t->sda_fall_ns = t->scl_fall_ns; -device_property_read_u32 (dev,"i2c-sda-hold-time-ns" , &t->sda_hold_ns); + ret =device_property_read_u32 (dev,"i2c-sda-hold-time-ns" , &t->sda_hold_ns); +if (ret && use_defaults) + t->sda_hold_ns =0 ; -device_property_read_u32 (dev,"i2c-digital-filter-width-ns" , - &t->digital_filter_width_ns); + ret =device_property_read_u32 (dev,"i2c-digital-filter-width-ns" , &t->digital_filter_width_ns); +if (ret && use_defaults) + t->digital_filter_width_ns =0 ; -device_property_read_u32 (dev,"i2c-analog-filter-cutoff-frequency" , - &t->analog_filter_cutoff_freq_hz); + ret =device_property_read_u32 (dev,"i2c-analog-filter-cutoff-frequency" , &t->analog_filter_cutoff_freq_hz); +if (ret && use_defaults) + t->analog_filter_cutoff_freq_hz =0 ; }EXPORT_SYMBOL_GPL (i2c_parse_fw_timings);
Signature · 1 line
2.25.1
PATCH v4 3/6 i2c: rcar: Consolidate timings calls in rcar_i2c_clock_calculate()
In reply to Andy ShevchenkoMove i2c_parse_fw_timings() to rcar_i2c_clock_calculate() to consolidate timings calls in one place.
While here, replace hard coded values with standard bus frequency definitions.
---
v4: no change
drivers/i2c/busses/i2c-rcar.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/i2c/busses/i2c-rcar.c +9 -9
@@ -235,17 +235,20 @@ static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)return i2c_recover_bus (&priv->adap); } -static int rcar_i2c_clock_calculate (struct rcar_i2c_priv *priv,struct i2c_timings *t) +static int rcar_i2c_clock_calculate (struct rcar_i2c_priv *priv) {u32 scgd, cdf, round, ick, sum, scl, cdf_width;unsigned long rate;struct device *dev =rcar_i2c_priv_to_dev (priv); +struct i2c_timings i2c_t, *t = &i2c_t;/* Fall back to previously used values if not supplied */ - t->bus_freq_hz = t->bus_freq_hz ?:100000 ; - t->scl_fall_ns = t->scl_fall_ns ?:35 ; - t->scl_rise_ns = t->scl_rise_ns ?:200 ; - t->scl_int_delay_ns = t->scl_int_delay_ns ?:50 ; + t->bus_freq_hz =I2C_MAX_STANDARD_MODE_FREQ ; + t->scl_fall_ns =35 ; + t->scl_rise_ns =200 ; + t->scl_int_delay_ns =50 ; + +i2c_parse_fw_timings (dev, &i2c_t, false);switch (priv->devtype) {case I2C_RCAR_GEN1 :
@@ -920,7 +923,6 @@ static int rcar_i2c_probe(struct platform_device *pdev)struct rcar_i2c_priv *priv;struct i2c_adapter *adap;struct device *dev = &pdev->dev; -struct i2c_timings i2c_t = {0 };int ret;/* Otherwise logic will break because some bytes must always use PIO */
@@ -957,8 +959,6 @@ static int rcar_i2c_probe(struct platform_device *pdev)i2c_set_adapdata (adap, priv);strlcpy (adap->name, pdev->name,sizeof (adap->name)); -i2c_parse_fw_timings (dev, &i2c_t, false); -/* Init DMA */ sg_init_table (&priv->sg,1 ); priv->dma_direction =DMA_NONE ;
@@ -967,7 +967,7 @@ static int rcar_i2c_probe(struct platform_device *pdev)/* Activate device for clock calculation */ pm_runtime_enable (dev);pm_runtime_get_sync (dev); - ret =rcar_i2c_clock_calculate (priv, &i2c_t); + ret =rcar_i2c_clock_calculate (priv);if (ret <0 ) goto out_pm_put;
Signature · 1 line
2.25.1
PATCH v4 4/6 i2c: stm32f7: switch to I²C generic property parsing
In reply to Andy ShevchenkoSwitch to the new generic functions: i2c_parse_fw_timings().
While here, replace hard coded values with standard bus frequency definitions.
---
v4: fixed typo in parameter (Alain)
drivers/i2c/busses/i2c-stm32f7.c | 57 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 31 deletions(-)
drivers/i2c/busses/i2c-stm32f7.c +26 -31
@@ -344,9 +344,9 @@ struct stm32f7_i2c_dev {*/ static struct stm32f7_i2c_spec i2c_specs[] = { [STM32_I2C_SPEED_STANDARD ] = { - .rate =100000 , - .rate_min =80000 , - .rate_max =100000 , + .rate =I2C_MAX_STANDARD_MODE_FREQ , + .rate_min =I2C_MAX_STANDARD_MODE_FREQ *8 /10 ,/* 80% */ + .rate_max =I2C_MAX_STANDARD_MODE_FREQ , .fall_max =300 , .rise_max =1000 , .hddat_min =0 ,
@@ -356,9 +356,9 @@ static struct stm32f7_i2c_spec i2c_specs[] = { .h_min = 4000, }, [STM32_I2C_SPEED_FAST] = { - .rate = 400000, - .rate_min = 320000, - .rate_max = 400000, + .rate = I2C_MAX_FAST_MODE_FREQ, + .rate_min = I2C_MAX_FAST_MODE_FREQ * 8 / 10, /* 80% */ + .rate_max = I2C_MAX_FAST_MODE_FREQ, .fall_max = 300, .rise_max = 300, .hddat_min = 0,
@@ -368,9 +368,9 @@ static struct stm32f7_i2c_spec i2c_specs[] = { .h_min = 600, }, [STM32_I2C_SPEED_FAST_PLUS] = { - .rate = 1000000, - .rate_min = 800000, - .rate_max = 1000000, + .rate = I2C_MAX_FAST_MODE_PLUS_FREQ, + .rate_min = I2C_MAX_FAST_MODE_PLUS_FREQ * 8 / 10, /* 80% */ + .rate_max = I2C_MAX_FAST_MODE_PLUS_FREQ, .fall_max = 100, .rise_max = 120, .hddat_min = 0,
@@ -610,8 +610,25 @@ static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev,static int stm32f7_i2c_setup_timing (struct stm32f7_i2c_dev *i2c_dev,struct stm32f7_i2c_setup *setup) { +struct i2c_timings timings, *t = &timings;int ret =0 ; + t->bus_freq_hz =I2C_MAX_STANDARD_MODE_FREQ ; + t->scl_rise_ns = i2c_dev->setup.rise_time; + t->scl_fall_ns = i2c_dev->setup.fall_time; + +i2c_parse_fw_timings (i2c_dev->dev, t, false); + +if (t->bus_freq_hz >=I2C_MAX_FAST_MODE_PLUS_FREQ ) + i2c_dev->speed =STM32_I2C_SPEED_FAST_PLUS ; +else if (t->bus_freq_hz >=I2C_MAX_FAST_MODE_FREQ ) + i2c_dev->speed =STM32_I2C_SPEED_FAST ; +else + i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; + + i2c_dev->setup.rise_time = t->scl_rise_ns; + i2c_dev->setup.fall_time = t->scl_fall_ns; + setup->speed = i2c_dev->speed; setup->speed_freq = i2c_specs[setup->speed].rate; setup->clock_src =clk_get_rate (i2c_dev->clk);
@@ -1914,7 +1931,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)struct stm32f7_i2c_dev *i2c_dev;const struct stm32f7_i2c_setup *setup;struct resource *res; -u32 clk_rate, rise_time, fall_time;struct i2c_adapter *adap;struct reset_control *rst;dma_addr_t phy_addr;
@@ -1961,17 +1977,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)return ret; } - i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; - ret =device_property_read_u32 (&pdev->dev,"clock-frequency" , - &clk_rate); -if (!ret && clk_rate >=1000000 ) { - i2c_dev->speed =STM32_I2C_SPEED_FAST_PLUS ; - }else if (!ret && clk_rate >=400000 ) { - i2c_dev->speed =STM32_I2C_SPEED_FAST ; - }else if (!ret && clk_rate >=100000 ) { - i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; - } - rst =devm_reset_control_get (&pdev->dev,NULL );if (IS_ERR (rst)) {dev_err (&pdev->dev,"Error: Missing controller reset\n" );
@@ -2011,16 +2016,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev) } i2c_dev->setup = *setup; - ret =device_property_read_u32 (i2c_dev->dev,"i2c-scl-rising-time-ns" , - &rise_time); -if (!ret) - i2c_dev->setup.rise_time = rise_time; - - ret =device_property_read_u32 (i2c_dev->dev,"i2c-scl-falling-time-ns" , - &fall_time); -if (!ret) - i2c_dev->setup.fall_time = fall_time; - ret =stm32f7_i2c_setup_timing (i2c_dev, &i2c_dev->setup);if (ret) goto clk_free;
Signature · 1 line
2.25.1
Re: [PATCH v4 4/6] i2c: stm32f7: switch to I²C generic property parsing
Hi Andy,
Looks good for me,
Regards
Quoted 129 lines · Andy Shevchenko
Switch to the new generic functions:
i2c_parse_fw_timings().While here, replace hard coded values with standard bus frequency definitions.
Cc Pierre-Yves MORDRET <[email protected]>Cc Maxime Coquelin <[email protected]>Cc Alexandre Torgue <[email protected]>Cc Alain Volmat <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>---
v4: fixed typo in parameter (Alain)drivers/i2c/busses/i2c-stm32f7.c | 57 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 31 deletions(-)drivers/i2c/busses/i2c-stm32f7.c +26
-31@@ -344,9 +344,9 @@ struct stm32f7_i2c_dev {*/ static struct stm32f7_i2c_spec i2c_specs[] = { [STM32_I2C_SPEED_STANDARD ] = { - .rate =100000 , - .rate_min =80000 , - .rate_max =100000 , + .rate =I2C_MAX_STANDARD_MODE_FREQ , + .rate_min =I2C_MAX_STANDARD_MODE_FREQ *8 /10 ,/* 80% */ + .rate_max =I2C_MAX_STANDARD_MODE_FREQ , .fall_max =300 , .rise_max =1000 , .hddat_min =0 ,@@ -356,9 +356,9 @@ static struct stm32f7_i2c_spec i2c_specs[] = { .h_min = 4000, }, [STM32_I2C_SPEED_FAST] = { - .rate = 400000, - .rate_min = 320000, - .rate_max = 400000, + .rate = I2C_MAX_FAST_MODE_FREQ, + .rate_min = I2C_MAX_FAST_MODE_FREQ * 8 / 10, /* 80% */ + .rate_max = I2C_MAX_FAST_MODE_FREQ, .fall_max = 300, .rise_max = 300, .hddat_min = 0,@@ -368,9 +368,9 @@ static struct stm32f7_i2c_spec i2c_specs[] = { .h_min = 600, }, [STM32_I2C_SPEED_FAST_PLUS] = { - .rate = 1000000, - .rate_min = 800000, - .rate_max = 1000000, + .rate = I2C_MAX_FAST_MODE_PLUS_FREQ, + .rate_min = I2C_MAX_FAST_MODE_PLUS_FREQ * 8 / 10, /* 80% */ + .rate_max = I2C_MAX_FAST_MODE_PLUS_FREQ, .fall_max = 100, .rise_max = 120, .hddat_min = 0,@@ -610,8 +610,25 @@ static int stm32f7_i2c_compute_timing(struct stm32f7_i2c_dev *i2c_dev,static int stm32f7_i2c_setup_timing (struct stm32f7_i2c_dev *i2c_dev,struct stm32f7_i2c_setup *setup) { +struct i2c_timings timings, *t = &timings;int ret =0 ; + t->bus_freq_hz =I2C_MAX_STANDARD_MODE_FREQ ; + t->scl_rise_ns = i2c_dev->setup.rise_time; + t->scl_fall_ns = i2c_dev->setup.fall_time; + +i2c_parse_fw_timings (i2c_dev->dev, t, false); + +if (t->bus_freq_hz >=I2C_MAX_FAST_MODE_PLUS_FREQ ) + i2c_dev->speed =STM32_I2C_SPEED_FAST_PLUS ; +else if (t->bus_freq_hz >=I2C_MAX_FAST_MODE_FREQ ) + i2c_dev->speed =STM32_I2C_SPEED_FAST ; +else + i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; + + i2c_dev->setup.rise_time = t->scl_rise_ns; + i2c_dev->setup.fall_time = t->scl_fall_ns; + setup->speed = i2c_dev->speed; setup->speed_freq = i2c_specs[setup->speed].rate; setup->clock_src =clk_get_rate (i2c_dev->clk);@@ -1914,7 +1931,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)struct stm32f7_i2c_dev *i2c_dev;const struct stm32f7_i2c_setup *setup;struct resource *res; -u32 clk_rate, rise_time, fall_time;struct i2c_adapter *adap;struct reset_control *rst;dma_addr_t phy_addr;@@ -1961,17 +1977,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)return ret; } - i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; - ret =device_property_read_u32 (&pdev->dev,"clock-frequency" , - &clk_rate); -if (!ret && clk_rate >=1000000 ) { - i2c_dev->speed =STM32_I2C_SPEED_FAST_PLUS ; - }else if (!ret && clk_rate >=400000 ) { - i2c_dev->speed =STM32_I2C_SPEED_FAST ; - }else if (!ret && clk_rate >=100000 ) { - i2c_dev->speed =STM32_I2C_SPEED_STANDARD ; - } - rst =devm_reset_control_get (&pdev->dev,NULL );if (IS_ERR (rst)) {dev_err (&pdev->dev,"Error: Missing controller reset\n" );@@ -2011,16 +2016,6 @@ static int stm32f7_i2c_probe(struct platform_device *pdev) } i2c_dev->setup = *setup; - ret =device_property_read_u32 (i2c_dev->dev,"i2c-scl-rising-time-ns" , - &rise_time); -if (!ret) - i2c_dev->setup.rise_time = rise_time; - - ret =device_property_read_u32 (i2c_dev->dev,"i2c-scl-falling-time-ns" , - &fall_time); -if (!ret) - i2c_dev->setup.fall_time = fall_time; - ret =stm32f7_i2c_setup_timing (i2c_dev, &i2c_dev->setup);if (ret) goto clk_free;Signature · 1 line
2.25.1
PATCH v4 5/6 i2c: algo: Use generic definitions for bus frequencies
In reply to Andy ShevchenkoSince we have generic definitions for bus frequencies, let's use them.
---
v4: no changes
drivers/i2c/algos/i2c-algo-pca.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/i2c/algos/i2c-algo-pca.c +3 -3
@@ -459,17 +459,17 @@ static int pca_init(struct i2c_adapter *adap)/* To avoid integer overflow, use clock/100 for calculations */ clock =pca_clock (pca_data) /100 ; -if (pca_data->i2c_clock >1000000 ) { +if (pca_data->i2c_clock >I2C_MAX_FAST_MODE_PLUS_FREQ ) { mode =I2C_PCA_MODE_TURBO ; min_tlow =14 ; min_thi =5 ; raise_fall_time =22 ;/* Raise 11e-8s, Fall 11e-8s */ - }else if (pca_data->i2c_clock >400000 ) { + }else if (pca_data->i2c_clock >I2C_MAX_FAST_MODE_FREQ ) { mode =I2C_PCA_MODE_FASTP ; min_tlow =17 ; min_thi =9 ; raise_fall_time =22 ;/* Raise 11e-8s, Fall 11e-8s */ - }else if (pca_data->i2c_clock >100000 ) { + }else if (pca_data->i2c_clock >I2C_MAX_STANDARD_MODE_FREQ ) { mode =I2C_PCA_MODE_FAST ; min_tlow =44 ; min_thi =20 ;
Signature · 1 line
2.25.1