i2c: mt7621: add Airoha AN7589 support
Subsystems (4)
v2 cover i2c: mt7621: add Airoha AN7589 support
The Airoha AN7589 carries the MT7621 I2C controller with a second control register that holds a 16 bit clock divider. It runs without SCL stretching.
Patch 1 resets the controller with reset_control_assert() and reset_control_deassert() instead of device_reset(), as the EN7581 and AN7589 reset controller has no .reset operation. Patch 2 adds the compatible, with mediatek,mt7621-i2c as the fallback. Patch 3 describes the variants as match data and uses the wider divider for bus rates down to about 3 kHz.
The series applies to next-20261105 and has no dependencies.
Changes in v2:
- Add patch 1. It replaces "clk: en7523: add a reset operation" from the clk series, as Philipp suggested.
- Patch 2: add mediatek,mt7621-i2c as the fallback compatible (Conor).
- Patch 3: the extended divider supports bus rates down to about 3 kHz. Set it up before the controller is enabled.
- Link to v1: https://lore.kernel.org/r/[email protected]
John Crispin (3):
i2c: mt7621: assert and deassert the reset line
dt-bindings: i2c: mt7621: add Airoha AN7589
i2c: mt7621: add support for Airoha AN7589
.../bindings/i2c/mediatek,mt7621-i2c.yaml | 10 ++- drivers/i2c/busses/i2c-mt7621.c | 69 ++++++++++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-)
base-commit: ddfe51b0a0f5dc9e415cc2c8ad8db17532dd426b
Signature · 1 line
2.34.1
PATCH v2 1/3 i2c: mt7621: assert and deassert the reset line
The driver resets the controller with device_reset(), which needs a .reset operation in the reset controller. The EN7581 reset controller implements assert and deassert only, so there every call fails, logs "I2C reset failed!" and leaves the controller as it is, also after a transfer timeout.
Get the optional reset line at probe and assert and deassert it. The MT7621 and MT7628 reset controller does the same in its .reset operation, with no delay in between, so nothing changes there. A board without a reset line no longer logs an error, and a reset controller that is not ready yet defers the probe.
---
Changes in v2:
- New patch. It replaces "clk: en7523: add a reset operation" from
the clk series (Philipp).
- Link to the discussion: https://lore.kernel.org/r/[email protected]
drivers/i2c/busses/i2c-mt7621.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-mt7621.c +9 -1
@@ -65,6 +65,7 @@ struct mtk_i2c {u32 clk_div;u32 flags;struct clk *clk; +struct reset_control *rst; };static int mtk_i2c_wait_idle (struct mtk_i2c *i2c)
@@ -85,7 +86,9 @@ static void mtk_i2c_reset(struct mtk_i2c *i2c) {int ret; - ret =device_reset (i2c->adap.dev.parent); + ret =reset_control_assert (i2c->rst); +if (!ret) + ret =reset_control_deassert (i2c->rst);if (ret)dev_err (i2c->dev,"I2C reset failed!\n" );
@@ -288,6 +291,11 @@ static int mtk_i2c_probe(struct platform_device *pdev)return PTR_ERR (i2c->clk); } + i2c->rst =devm_reset_control_get_optional_exclusive (&pdev->dev,NULL ); +if (IS_ERR (i2c->rst)) +return dev_err_probe (&pdev->dev,PTR_ERR (i2c->rst), +"Failed to get reset\n" ); + i2c->dev = &pdev->dev; if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
Signature · 1 line
2.34.1
Re: [PATCH v2 1/3] i2c: mt7621: assert and deassert the reset line
Quoted 6 lines · John Crispin
The driver resets the controller with
device_reset(), which needs a .reset operation in the reset controller. The EN7581 reset controller implements assert and deassert only, so there every call fails, logs "I2C reset failed!" and leaves the controller as it is, also after a transfer timeout.
Quoted 8 lines
Get the optional reset line at probe and assert and deassert it. The MT7621 and MT7628 reset controller does the same in its .reset operation, with no delay in between, so nothing changes there. A board without a reset line no longer logs an error, and a reset controller that is not ready yet defers the probe.
Suggested-by Philipp Zabel <[email protected]>Signed-off-by John Crispin <[email protected]>
regards
Philipp
PATCH v2 2/3 dt-bindings: i2c: mt7621: add Airoha AN7589
In reply to John CrispinThe Airoha AN7589 carries the MT7621 I2C controller with a second control register holding a wider clock divider. Out of reset the controller uses the MT7621 divider, so the MT7621 programming works and mediatek,mt7621-i2c serves as the fallback. Give the AN7589 a compatible of its own so the driver can use the wider divider.
---
Changes in v2:
- Add mediatek,mt7621-i2c as the fallback compatible (Conor).
.../devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/i2c/mediatek,mt7621-i2c.yaml +9 -1
@@ -9,12 +9,20 @@ maintainers: title: Mediatek MT7621/MT7628 I2C master controller +description: + The same controller is used on Airoha SoCs. The AN7589 variant adds a + second control register with a wider clock divider. + allOf: - $ref: /schemas/i2c/i2c-controller.yaml# properties: compatible: - const: mediatek,mt7621-i2c + oneOf: + - const: mediatek,mt7621-i2c + - items: + - const: airoha,an7589-i2c + - const: mediatek,mt7621-i2c reg: maxItems: 1
Signature · 1 line
2.34.1
Re: [PATCH v2 2/3] dt-bindings: i2c: mt7621: add Airoha AN7589
pw-bot: not-applicable
PATCH v2 3/3 i2c: mt7621: add support for Airoha AN7589
In reply to John CrispinThe Airoha AN7589 carries this controller with a second control register at 0x68. It holds a 16 bit clock divider selected by SM0CTL2_SCL_DIV_SEL, wider than the 11 bit field in SM0CTL0, and SDA wait control. The AN7589 also runs without SCL stretching.
Describe the variants as match data: whether to set SCL_STRETCH, and whether to program the extended divider. The extended divider is set up before the controller is enabled, and the SM0CTL0 divider stays zero. With the extended divider and the 200 MHz clock the bus runs from fast mode plus down to about 3 kHz, where the SM0CTL0 divider stops at 97.7 kHz.
---
Changes in v2:
- The extended divider supports bus rates down to about 3 kHz with the 200 MHz clock. The SM0CTL0 divider stops at 97.7 kHz.
- Set up the extended divider before the controller is enabled. The SM0CTL0 divider stays zero.
drivers/i2c/busses/i2c-mt7621.c | 59 ++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-)
drivers/i2c/busses/i2c-mt7621.c +51 -8
@@ -10,6 +10,7 @@* (C) 2014 Sittisak <[email protected]> */ +#include <linux/bitfield.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/i2c.h>
@@ -18,6 +19,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> +#include <linux/property.h> #include <linux/reset.h> #define REG_SM0CFG2_REG 0x28
@@ -28,6 +30,7 @@#define REG_PINTEN_REG 0x5c#define REG_PINTST_REG 0x60#define REG_PINTCL_REG 0x64 +#define REG_SM0CTL2_REG 0x68/* REG_SM0CFG2_REG */ #define SM0CFG2_IS_AUTOMODE BIT(0)
@@ -54,13 +57,25 @@#define SM0CTL1_MODE_MASK (0x7 << 4)#define SM0CTL1_TRI BIT(0) +/* REG_SM0CTL2_REG */ +#define SM0CTL2_DATA_WAIT_SDA BIT(18) +#define SM0CTL2_INIT_WAIT_SDA BIT(17) +#define SM0CTL2_SCL_DIV_SEL BIT(16) +#define SM0CTL2_CLK_DIV_EXT GENMASK(15, 0) +/* timeout waiting for I2C devices to respond */ #define TIMEOUT_MS 1000 +struct mtk_i2c_soc_data { +bool scl_stretch; +bool ext_clk_div; +}; +struct mtk_i2c {void __iomem *base;struct device *dev;struct i2c_adapter adap; +const struct mtk_i2c_soc_data *soc;u32 bus_freq;u32 clk_div;u32 flags;
@@ -84,6 +99,7 @@ static int mtk_i2c_wait_idle(struct mtk_i2c *i2c)static void mtk_i2c_reset (struct mtk_i2c *i2c) { +u32 ctl0;int ret; ret =reset_control_assert (i2c->rst);
@@ -96,8 +112,17 @@ static void mtk_i2c_reset(struct mtk_i2c *i2c)* Don't set SM0CTL0_ODRAIN as its bit meaning is inverted. To * configure open-drain mode, this bit needs to be cleared. */ -iowrite32 (((i2c->clk_div <<16 ) &SM0CTL0_CLK_DIV_MASK ) |SM0CTL0_EN | -SM0CTL0_SCL_STRETCH , i2c->base +REG_SM0CTL0_REG ); + ctl0 =SM0CTL0_EN ; +if (i2c->soc->ext_clk_div) +iowrite32 (SM0CTL2_SCL_DIV_SEL |SM0CTL2_DATA_WAIT_SDA | +FIELD_PREP (SM0CTL2_CLK_DIV_EXT , i2c->clk_div), + i2c->base +REG_SM0CTL2_REG ); +else + ctl0 |= (i2c->clk_div <<16 ) &SM0CTL0_CLK_DIV_MASK ; +if (i2c->soc->scl_stretch) + ctl0 |=SM0CTL0_SCL_STRETCH ; +iowrite32 (ctl0, i2c->base +REG_SM0CTL0_REG ); +iowrite32 (0 , i2c->base +REG_SM0CFG2_REG ); }
@@ -253,8 +278,17 @@ static const struct i2c_algorithm mtk_i2c_algo = { .functionality = mtk_i2c_func, }; +static const struct mtk_i2c_soc_data mt7621_i2c_soc_data = { + .scl_stretch = true, +}; + +static const struct mtk_i2c_soc_data an7589_i2c_soc_data = { + .ext_clk_div = true, +}; +static const struct of_device_id i2c_mtk_dt_ids[] = { - { .compatible ="mediatek,mt7621-i2c" }, + { .compatible ="mediatek,mt7621-i2c" , .data = &mt7621_i2c_soc_data }, + { .compatible ="airoha,an7589-i2c" , .data = &an7589_i2c_soc_data }, {/* sentinel */ } };
@@ -262,11 +296,16 @@ MODULE_DEVICE_TABLE(of, i2c_mtk_dt_ids);static void mtk_i2c_init (struct mtk_i2c *i2c) { - i2c->clk_div =clk_get_rate (i2c->clk) / i2c->bus_freq -1 ; -if (i2c->clk_div <99 ) - i2c->clk_div =99 ; -if (i2c->clk_div >SM0CTL0_CLK_DIV_MAX ) - i2c->clk_div =SM0CTL0_CLK_DIV_MAX ; +unsigned long rate =clk_get_rate (i2c->clk); +unsigned long div_min =99 ; +unsigned long div_max =SM0CTL0_CLK_DIV_MAX ; + +if (i2c->soc->ext_clk_div) { + div_min = rate /I2C_MAX_FAST_MODE_PLUS_FREQ -1 ; + div_max =FIELD_MAX (SM0CTL2_CLK_DIV_EXT ); + } + + i2c->clk_div =clamp (rate / i2c->bus_freq -1 , div_min, div_max);mtk_i2c_reset (i2c); }
@@ -281,6 +320,10 @@ static int mtk_i2c_probe(struct platform_device *pdev)if (!i2c)return -ENOMEM ; + i2c->soc =device_get_match_data (&pdev->dev); +if (!i2c->soc) +return -EINVAL ; + i2c->base =devm_platform_get_and_ioremap_resource (pdev,0 ,NULL );if (IS_ERR (i2c->base))return PTR_ERR (i2c->base);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v2 3/3] i2c: mt7621: add support for Airoha AN7589
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] i2c: mt7621: unsigned underflow in mtk_i2c_init() divider clamp() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] i2c: mt7621: unsigned underflow in mtk_i2c_init() divider clamp()
--
commit 36029923d9708d51405f55c0dcd224ddc5b82105
Author: John Crispin <[email protected]>
i2c: mt7621: add support for Airoha AN7589
This commit adds match data to i2c-mt7621 to support the Airoha AN7589 variant, which uses an extended 16-bit clock divider in SM0CTL2 and disables SCL stretching.
Quoted 4 lines
drivers/i2c/busses/i2c-mt7621.c +0
-0
[ ... ]
Quoted 9 lines
drivers/i2c/busses/i2c-mt7621.c +0
-5@@ -262,11 +296,16 @@ MODULE_DEVICE_TABLE(of, i2c_mtk_dt_ids);static void mtk_i2c_init (struct mtk_i2c *i2c) { - i2c->clk_div =clk_get_rate (i2c->clk) / i2c->bus_freq -1 ; -if (i2c->clk_div <99 ) - i2c->clk_div =99 ; -if (i2c->clk_div >SM0CTL0_CLK_DIV_MAX ) - i2c->clk_div =SM0CTL0_CLK_DIV_MAX ;
drivers/i2c/busses/i2c-mt7621.c MODULE_DEVICE_TABLE(of, i2c_mtk_dt_ids); · line 308
Quoted 10 lines
drivers/i2c/busses/i2c-mt7621.c +10
-0+unsigned long rate =clk_get_rate (i2c->clk); +unsigned long div_min =99 ; +unsigned long div_max =SM0CTL0_CLK_DIV_MAX ; + +if (i2c->soc->ext_clk_div) { + div_min = rate /I2C_MAX_FAST_MODE_PLUS_FREQ -1 ; + div_max =FIELD_MAX (SM0CTL2_CLK_DIV_EXT ); + } + + i2c->clk_div =clamp (rate / i2c->bus_freq -1 , div_min, div_max);
[Severity: Medium]
Can div_min and rate / i2c->bus_freq - 1 underflow unsigned long here?
When CONFIG_HAVE_CLK is disabled (which is reachable via COMPILE_TEST), clk_get_rate() returns 0, so div_min folds to ULONG_MAX while div_max is 0xffff. That trips the BUILD_BUG_ON_MSG(statically_true(ulo > uhi)) check inside clamp().
At runtime, if rate is smaller than I2C_MAX_FAST_MODE_PLUS_FREQ or i2c->bus_freq, the unsigned subtraction also wraps to ULONG_MAX and clamps i2c->clk_div to div_max instead of the minimum divider.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
kernel test robot
build botRe: [PATCH v2 3/3] i2c: mt7621: add support for Airoha AN7589
In reply to John Crispinkernel test robot noticed the following build errors:
Hi John,
kernel test robot noticed the following build errors:
[auto build test ERROR on robh/for-next]
[also build test ERROR on andi-shyti/i2c/i2c-host linus/master v7.4-rc2 next-20261112] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/John-Crispin/dt-bindings-i2c-mt7621-add-Airoha-AN7589/20261112-195819 base: https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next patch link: https://lore.kernel.org/r/20261112185820.1315718-4-john%40phrozen.org patch subject: [PATCH v2 3/3] i2c: mt7621: add support for Airoha AN7589 config: sparc-randconfig-1000-20261113 (https://download.01.org/0day-ci/archive/20261114/[email protected]/config) compiler: sparc-linux-gcc (GCC) 8.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261114/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from <command-line>:
In function 'mtk_i2c_init',
inlined from 'mtk_i2c_probe' at drivers/i2c/busses/i2c-mt7621.c:364:2:
>> include/linux/compiler_types.h:702:38: error: call to '__compiletime_assert_437' declared with attribute error: clamp() low limit div_min greater than high limit div_max
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
^
include/linux/compiler_types.h:683:4: note: in definition of macro '__compiletime_assert'
prefix ## suffix(); \
^~~~~~
include/linux/compiler_types.h:702:2: note: in expansion of macro '_compiletime_assert'
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:40:37: note: in expansion of macro 'compiletime_assert'
#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
^~~~~~~~~~~~~~~~~~
include/linux/minmax.h:188:2: note: in expansion of macro 'BUILD_BUG_ON_MSG'
BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \
^~~~~~~~~~~~~~~~
include/linux/minmax.h:195:2: note: in expansion of macro '__clamp_once'
__clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
^~~~~~~~~~~~
include/linux/minmax.h:206:28: note: in expansion of macro '__careful_clamp'
#define clamp(val, lo, hi) __careful_clamp(auto, val, lo, hi)
^~~~~~~~~~~~~~~
drivers/i2c/busses/i2c-mt7621.c:308:17: note: in expansion of macro 'clamp'
i2c->clk_div = clamp(rate / i2c->bus_freq - 1, div_min, div_max);
^~~~~vim +/__compiletime_assert_437 +702 include/linux/compiler_types.h
eb5c2d4b45e3d2d Will Deacon 2020-07-21 688 eb5c2d4b45e3d2d Will Deacon 2020-07-21 689 #define _compiletime_assert(condition, msg, prefix, suffix) \ eb5c2d4b45e3d2d Will Deacon 2020-07-21 690 __compiletime_assert(condition, msg, prefix, suffix) eb5c2d4b45e3d2d Will Deacon 2020-07-21 691 eb5c2d4b45e3d2d Will Deacon 2020-07-21 692 /** eb5c2d4b45e3d2d Will Deacon 2020-07-21 693 * compiletime_assert - break build and emit msg if condition is false eb5c2d4b45e3d2d Will Deacon 2020-07-21 694 * @condition: a compile-time constant condition to check eb5c2d4b45e3d2d Will Deacon 2020-07-21 695 * @msg: a message to emit if condition is false eb5c2d4b45e3d2d Will Deacon 2020-07-21 696 * eb5c2d4b45e3d2d Will Deacon 2020-07-21 697 * In tradition of POSIX assert, this macro will break the build if the eb5c2d4b45e3d2d Will Deacon 2020-07-21 698 * supplied condition is *false*, emitting the supplied error message if the eb5c2d4b45e3d2d Will Deacon 2020-07-21 699 * compiler has support to do so. eb5c2d4b45e3d2d Will Deacon 2020-07-21 700 */ eb5c2d4b45e3d2d Will Deacon 2020-07-21 701 #define compiletime_assert(condition, msg) \
eb5c2d4b45e3d2d Will Deacon 2020-07-21 @702 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)eb5c2d4b45e3d2d Will Deacon 2020-07-21 703
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki