i2c: Add Loongson-2K0300 I2C controller support
Binbin Zhou
maintainerv3 cover i2c: Add Loongson-2K0300 I2C controller support
Hi all:
This patch set describes the I2C controller integrated the Loongson-2K0300 chip.
It has a significantly different design from the previous I2C controller(i2c-ls2x), such as support for master-slave transfer mode, and DMA transfers (implementation in progress), etc. Therefore, we try to name it i2c-ls2x-v2.
Thanks.
======
V3:
- Rebase on linux-i2c/i2c-next;
Patch (2/2):
- Reorder header file follow IWYU principle;
- Better indentation and coding style;
- Use generic macro definitions;
- Amend *all* struct data types;
- Correct unreasonable variable type definitions;
- Refact loongson2_i2c_isr_error();
- of_property_read_u32()->device_property_read_u32();
- Remove meaningless blank lines and output.
Link to V2:
https://lore.kernel.org/all/[email protected]/
V2:
Patch (1/2):
- Add Acked-by tag from Conor, thanks.
Patch (2/2):
- Reorder the definitions of read() and write();
- Adjust the calculation method for bus speed.
Link to V1:
https://lore.kernel.org/all/[email protected]/
Binbin Zhou (2):
dt-bindings: i2c: loongson,ls2x: Add ls2k0300-i2c compatible
i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
.../bindings/i2c/loongson,ls2x-i2c.yaml | 4 + MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ls2x-v2.c | 510 ++++++++++++++++++ 5 files changed, 526 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x-v2.c
base-commit: 6de23f81a5e08be8fbf5e8d7e9febc72a5b5f27f
Signature · 1 line
2.52.0
Binbin Zhou
maintainerPATCH v3 1/2 dt-bindings: i2c: loongson,ls2x: Add ls2k0300-i2c compatible
Add "loongson,ls2k0300-i2c" dedicated compatible for representing I2C of Loongson-2K0300 chip, because its HW integration is quiet different from others.
--- Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml +4 -0
@@ -16,6 +16,7 @@ properties: compatible: enum: - loongson,ls2k-i2c + - loongson,ls2k0300-i2c - loongson,ls7a-i2c reg:
@@ -24,6 +25,9 @@ properties: interrupts: maxItems: 1 + clocks: + maxItems: 1 + required: - compatible - reg
Signature · 1 line
2.52.0
Binbin Zhou
maintainerPATCH v3 2/2 i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
In reply to Binbin ZhouThis I2C module is integrated into the Loongson-2K0300 SoCs.
It provides multi-master functionality and controls all I2C bus-specific timing, protocols, arbitration, and timing. It supports both standard and fast modes.
--- MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ls2x-v2.c | 510 +++++++++++++++++++++++++++++++ 4 files changed, 522 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x-v2.c
MAINTAINERS +1 -0
@@ -14960,6 +14960,7 @@ M: Binbin Zhou <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml +F: drivers/i2c/busses/i2c-ls2x-v2.c F: drivers/i2c/busses/i2c-ls2x.c LOONGSON PWM DRIVER
drivers/i2c/busses/Kconfig +10 -0
@@ -850,6 +850,16 @@ config I2C_LS2X This driver can also be built as a module. If so, the module will be called i2c-ls2x. +config I2C_LS2X_V2 + tristate "Loongson-2 Fast Speed I2C adapter" + depends on LOONGARCH || COMPILE_TEST + help + If you say yes to this option, support will be included for the + I2C interface on the Loongson-2K0300 SoCs. + + This driver can also be built as a module. If so, the module + will be called i2c-ls2x-v2. + config I2C_MLXBF tristate "Mellanox BlueField I2C controller" depends on (MELLANOX_PLATFORM && ARM64) || COMPILE_TEST
drivers/i2c/busses/Makefile +1 -0
@@ -80,6 +80,7 @@ obj-$(CONFIG_I2C_KEBA) += i2c-keba.o obj-$(CONFIG_I2C_KEMPLD) += i2c-kempld.o obj-$(CONFIG_I2C_LPC2K) += i2c-lpc2k.o obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o +obj-$(CONFIG_I2C_LS2X_V2) += i2c-ls2x-v2.o obj-$(CONFIG_I2C_MESON) += i2c-meson.o obj-$(CONFIG_I2C_MICROCHIP_CORE) += i2c-microchip-corei2c.o obj-$(CONFIG_I2C_MPC) += i2c-mpc.o
drivers/i2c/busses/i2c-ls2x-v2.c new file +510 -0
510 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v3 2/2] i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
Quoted 5 lines · Binbin Zhou
This I2C module is integrated into the Loongson-2K0300 SoCs.
It provides multi-master functionality and controls all I2C bus-specific timing, protocols, arbitration, and timing. It supports both standard and fast modes.
...
drivers/i2c/busses/i2c-ls2x-v2.c line 113
Quoted 8 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +7
-0+struct loongson2_i2c_priv { +struct i2c_adapter adapter; +struct completion complete; +struct clk *clk; +struct regmap *regmap; +int speed; +int parent_rate;
May any of these two be negative? The kernel doc says nothing about that.
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+struct loongson2_i2c_msg msg; +};
...
Quoted 28 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +28
-0+static void loongson2_i2c_handle_read (struct loongson2_i2c_priv *priv,int flag) +{ +struct loongson2_i2c_msg *msg = &priv->msg; +unsigned int i; +bool changed; + +switch (msg->count) { +case 1 : +/* only transmit 1 bytes condition */ +loongson2_i2c_disable_irq (priv); +loongson2_i2c_read_msg (priv); +complete (&priv->complete); +break ; +case 2 : +if (flag !=1 ) { +/* ensure only transmit 2 bytes condition */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 , +LOONGSON2_I2C_CR2_ITBUFEN ,0 ); +break ; + } +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_OP_MASK , + msg->stop ?LOONGSON2_I2C_CR1_STOP :LOONGSON2_I2C_CR1_START ); + +loongson2_i2c_disable_irq (priv); + +for (i = msg->count; i >0 ; i--) +loongson2_i2c_read_msg (priv); +
drivers/i2c/busses/i2c-ls2x-v2.c line 184
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_POS ,0 ); +complete (&priv->complete); +break ; +case 3 : +regmap_update_bits_check (priv->regmap,LOONGSON2_I2C_CR2 ,LOONGSON2_I2C_CR2_ITBUFEN , +0 , &changed); +if (changed) +break ; +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_ACK ,0 ); + fallthrough;
Instead better to spell it explicitly:
loongson2_i2c_read_msg(priv); break;
drivers/i2c/busses/i2c-ls2x-v2.c line 186
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+default : +loongson2_i2c_read_msg (priv);
Missing break.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +}
This also make me think for the possible optimisations. if regmap_updates_bit() in case 2 can be run after the completion, the case 1 with a parameter may be split to a helper.
Also would be nice to have a comment why in case 3 we only read a single message.
...
drivers/i2c/busses/i2c-ls2x-v2.c line 197
Quoted 9 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +8
-0+static void loongson2_i2c_handle_write (struct loongson2_i2c_priv *priv) +{ +struct loongson2_i2c_msg *msg = &priv->msg; + +if (msg->count) { +loongson2_i2c_write_msg (priv, *msg->buf++); + msg->count--; +if (!msg->count)
Can be
if (!--msg->count)
but someone may find it difficult to correctly parse.
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +6
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 , +LOONGSON2_I2C_CR2_ITBUFEN ,0 ); + }else { +loongson2_i2c_terminate_xfer (priv); + } +}
...
drivers/i2c/busses/i2c-ls2x-v2.c line 401
Quoted 7 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +6
-0+static int loongson2_i2c_adjust_bus_speed (struct loongson2_i2c_priv *priv) +{ +u32 val, freq_mhz, ccr; + + priv->parent_rate =clk_get_rate (priv->clk); + freq_mhz =DIV_ROUND_UP (priv->parent_rate,HZ_PER_MHZ );
Usually the rule of thumb to put assignments close to its first user...
drivers/i2c/busses/i2c-ls2x-v2.c line 414
Quoted 12 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +12
-0+if (priv->speed ==I2C_MAX_STANDARD_MODE_FREQ ) { +/* Select Standard mode */ + ccr =0 ; + val =DIV_ROUND_UP (priv->parent_rate,I2C_MAX_STANDARD_MODE_FREQ *2 ); + }else { +/* Select Fast mode */ + ccr =LOONGSON2_I2C_CCR_FS ; + val =DIV_ROUND_UP (priv->parent_rate,I2C_MAX_FAST_MODE_FREQ *3 ); + } + +FIELD_MODIFY (LOONGSON2_I2C_CCR_CCR , &ccr, val); +regmap_write (priv->regmap,LOONGSON2_I2C_CCR , ccr);
...somewhere here.
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +11
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 ,LOONGSON2_I2C_CR2_FREQ , +FIELD_GET (LOONGSON2_I2C_CR2_FREQ , freq_mhz)); +regmap_update_bits (priv->regmap,LOONGSON2_I2C_TRISE ,LOONGSON2_I2C_TRISE_SCL , +LOONGSON2_I2C_TRISE_SCL ); + +/* Enable I2C */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_PE , +LOONGSON2_I2C_CR1_PE ); + +return 0 ; +}
...
drivers/i2c/busses/i2c-ls2x-v2.c line 480
Quoted 4 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+ priv->speed =I2C_MAX_STANDARD_MODE_FREQ ; + ret =device_property_read_u32 (dev,"clock-frequency" , &clk_rate); +if (!ret && clk_rate >=I2C_MAX_FAST_MODE_FREQ ) + priv->speed =I2C_MAX_FAST_MODE_FREQ ;
Please, use i2c_parse_fw_timings() instead of custom approach.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
Hi Andy:
Sorry for the late reply.
Quoted 16 lines · Andy Shevchenko
Quoted 5 lines · Binbin Zhou
This I2C module is integrated into the Loongson-2K0300 SoCs.
It provides multi-master functionality and controls all I2C bus-specific timing, protocols, arbitration, and timing. It supports both standard and fast modes.
...
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +5
-0+struct loongson2_i2c_priv { +struct i2c_adapter adapter; +struct completion complete; +struct clk *clk; +struct regmap *regmap;
Quoted 4 lines
drivers/i2c/busses/i2c-ls2x-v2.c line 113
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+int speed; +int parent_rate;May any of these two be negative? The kernel doc says nothing about that.
It should be:
u32 speed;
unsigned long parent_rate;
Quoted 58 lines
Quoted 3 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+struct loongson2_i2c_msg msg; +};...
Quoted 28 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +28
-0+static void loongson2_i2c_handle_read (struct loongson2_i2c_priv *priv,int flag) +{ +struct loongson2_i2c_msg *msg = &priv->msg; +unsigned int i; +bool changed; + +switch (msg->count) { +case 1 : +/* only transmit 1 bytes condition */ +loongson2_i2c_disable_irq (priv); +loongson2_i2c_read_msg (priv); +complete (&priv->complete); +break ; +case 2 : +if (flag !=1 ) { +/* ensure only transmit 2 bytes condition */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 , +LOONGSON2_I2C_CR2_ITBUFEN ,0 ); +break ; + } +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_OP_MASK , + msg->stop ?LOONGSON2_I2C_CR1_STOP :LOONGSON2_I2C_CR1_START ); + +loongson2_i2c_disable_irq (priv); + +for (i = msg->count; i >0 ; i--) +loongson2_i2c_read_msg (priv); +drivers/i2c/busses/i2c-ls2x-v2.c line 184
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_POS ,0 ); +complete (&priv->complete); +break ; +case 3 : +regmap_update_bits_check (priv->regmap,LOONGSON2_I2C_CR2 ,LOONGSON2_I2C_CR2_ITBUFEN , +0 , &changed); +if (changed) +break ; +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_ACK ,0 ); + fallthrough;Instead better to spell it explicitly:
loongson2_i2c_read_msg(priv); break;drivers/i2c/busses/i2c-ls2x-v2.c line 186
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+default : +loongson2_i2c_read_msg (priv);Missing break.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } +}
Quoted 6 lines
This also make me think for the possible optimisations. if
regmap_updates_bit()in case 2 can be run after the completion, the case 1 with a parameter may be split to a helper.Also would be nice to have a comment why in case 3 we only read a single message.
This part might be a bit confusing. I'll try to restructure it, which might make it clearer.
In loongson2_i2c_isr_event(), the read/write operations are divided into three parts.
/* TX empty */
if ((event & LOONGSON2_I2C_SR1_TXE) && !(msg->addr & I2C_M_RD))
loongson2_i2c_handle_write(priv);
/* RX not empty */
if ((event & LOONGSON2_I2C_SR1_RXNE) && (msg->addr & I2C_M_RD))
loongson2_i2c_handle_read(priv);//Handle FIFO empty interrupt in case of read, it is called when a new data is received in data register
/*
* The BTF (Byte Transfer finished) event occurs
*/
if (event & LOONGSON2_I2C_SR1_BTF) {
if (msg->addr & I2C_M_RD)
loongson2_i2c_handle_rx_done(priv);
//Handle byte transfer finished interrupt in case of read
else
loongson2_i2c_handle_write(priv);
}static void loongson2_i2c_handle_read(struct loongson2_i2c_priv *priv)
{
struct loongson2_i2c_msg *msg = &priv->msg;
switch (msg->count) {
case 1:
loongson2_i2c_disable_irq(priv);
loongson2_i2c_read_msg(priv);
complete(&priv->complete);
break;
case 2:
case 3:
/*
* For 2-byte/3-byte reception and for N-bytereception with N > 3, we have to
* wait for byte transferred finished event before reading data.
* Just disable buffer interrupt in order to avoid
another system preemption due
* to RX not empty event.
*/
regmap_update_bits(priv->regmap, LOONGSON2_I2C_CR2,
LOONGSON2_I2C_CR2_ITBUFEN, 0);
break;
default:
/*
* For N byte reception with N > 3 we directly read
data register
* until N-2 data.
*/
loongson2_i2c_read_msg(priv);
break;
}
}static void loongson2_i2c_handle_rx_done(struct loongson2_i2c_priv *priv)
{
struct loongson2_i2c_msg *msg = &priv->msg;
u32 i;
switch (msg->count) {
case 2:
/*
* The STOP/START bit has to be set before reading the
last two bytes.
* After that, we could read the last two bytes.
*/
regmap_update_bits(priv->regmap, LOONGSON2_I2C_CR1,
LOONGSON2_I2C_CR1_OP_MASK,
msg->stop ? LOONGSON2_I2C_CR1_STOP: LOONGSON2_I2C_CR1_START);
for (i = 2; i > 0; i--)
loongson2_i2c_read_msg(priv);
loongson2_i2c_disable_irq(priv);
complete(&priv->complete);
break;
case 3:
/*
* In order to generate the NACK after the last
received data byte, enable NACK
* before reading N-2 data
*/
regmap_update_bits(priv->regmap, LOONGSON2_I2C_CR1,
LOONGSON2_I2C_CR1_ACK, 0);
loongson2_i2c_read_msg(priv);
break;
default:
loongson2_i2c_read_msg(priv);
break;
}
}Quoted 34 lines
...
drivers/i2c/busses/i2c-ls2x-v2.c line 197
Quoted 9 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +8
-0+static void loongson2_i2c_handle_write (struct loongson2_i2c_priv *priv) +{ +struct loongson2_i2c_msg *msg = &priv->msg; + +if (msg->count) { +loongson2_i2c_write_msg (priv, *msg->buf++); + msg->count--; +if (!msg->count)Can be
if (!--msg->count)but someone may find it difficult to correctly parse.
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +6
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 , +LOONGSON2_I2C_CR2_ITBUFEN ,0 ); + }else { +loongson2_i2c_terminate_xfer (priv); + } +}...
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +5
-0+static int loongson2_i2c_adjust_bus_speed (struct loongson2_i2c_priv *priv) +{ +u32 val, freq_mhz, ccr; + + priv->parent_rate =clk_get_rate (priv->clk);
Quoted 3 lines
Quoted 1 line
+
freq_mhz=DIV_ROUND_UP(priv->parent_rate, HZ_PER_MHZ);Usually the rule of thumb to put assignments close to its first user...
See blow.
Quoted 30 lines
drivers/i2c/busses/i2c-ls2x-v2.c line 414
Quoted 13 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +12
-0+if (priv->speed ==I2C_MAX_STANDARD_MODE_FREQ ) { +/* Select Standard mode */ + ccr =0 ; + val =DIV_ROUND_UP (priv->parent_rate,I2C_MAX_STANDARD_MODE_FREQ *2 ); + }else { +/* Select Fast mode */ + ccr =LOONGSON2_I2C_CCR_FS ; + val =DIV_ROUND_UP (priv->parent_rate,I2C_MAX_FAST_MODE_FREQ *3 ); + } + +FIELD_MODIFY (LOONGSON2_I2C_CCR_CCR , &ccr, val); +regmap_write (priv->regmap,LOONGSON2_I2C_CCR , ccr);...somewhere here.
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +11
-0+regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR2 ,LOONGSON2_I2C_CR2_FREQ , +FIELD_GET (LOONGSON2_I2C_CR2_FREQ , freq_mhz)); +regmap_update_bits (priv->regmap,LOONGSON2_I2C_TRISE ,LOONGSON2_I2C_TRISE_SCL , +LOONGSON2_I2C_TRISE_SCL ); + +/* Enable I2C */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_PE , +LOONGSON2_I2C_CR1_PE ); + +return 0 ; +}...
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x-v2.c line 480
Quoted 4 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+ priv->speed =I2C_MAX_STANDARD_MODE_FREQ ; + ret =device_property_read_u32 (dev,"clock-frequency" , &clk_rate); +if (!ret && clk_rate >=I2C_MAX_FAST_MODE_FREQ ) + priv->speed =I2C_MAX_FAST_MODE_FREQ ;Please, use
i2c_parse_fw_timings()instead of custom approach.
This section will be merged into loongson2_i2c_adjust_bus_speed(), and bus_freq_hz will replace priv->speed as follows:
static int loongson2_i2c_adjust_bus_speed(struct loongson2_i2c_priv *priv)
{
struct i2c_timings i2c_t;
....
i2c_parse_fw_timings(dev, &i2c_t, true);
priv->parent_rate = clk_get_rate(priv->clk);
if (i2c_t.bus_freq_hz == I2C_MAX_STANDARD_MODE_FREQ) {
/* Select Standard mode */
ccr = 0;
val = DIV_ROUND_UP(priv->parent_rate, i2c_t.bus_freq_hz * 2);
} else if (i2c_t.bus_freq_hz == I2C_MAX_FAST_MODE_FREQ) {
/* Select Fast mode */
ccr = LOONGSON2_I2C_CCR_FS;
val = DIV_ROUND_UP(priv->parent_rate, i2c_t.bus_freq_hz * 3);
} else {
dev_err(dev, "Unsupported speed (%uhz)\n", i2c_t.bus_freq_hz);
return -EINVAL;
}......
freq_mhz = DIV_ROUND_UP(priv->parent_rate, HZ_PER_MHZ);
regmap_update_bits(priv->regmap, LOONGSON2_I2C_CR2,
LOONGSON2_I2C_CR2_FREQ,
FIELD_GET(LOONGSON2_I2C_CR2_FREQ, freq_mhz));.............
return 0; }
Quoted 6 lines
--
With Best Regards,
Andy Shevchenko
--
Thanks.
Binbin