i2c: Add Loongson-2K0300 I2C controller support
Binbin Zhou
maintainerv4 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.
======
V4:
- Rebase on linux-i2c/i2c-next;
Patch (2/2):
- The parent_rate parameter type should be unsigned long;
- Drop fallthrough and add missing break;
- device_set_node()->i2c_adapter_set_node();
- Use i2c_parse_fw_timings();
- Use i2c_t.bus_freq_hz instead of priv->speed;
- Sperate loongson2_i2c_handle_read() into loongson2_i2c_handle_read() and loongson2_i2c_handle_rx_done().
Link to V3:
https://lore.kernel.org/all/[email protected]/
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 | 545 ++++++++++++++++++ 5 files changed, 561 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x-v2.c
base-commit: 6117e1ba1db78a52a4161208ea403d3769ad73c6
Signature · 1 line
2.52.0
Binbin Zhou
maintainerPATCH v4 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 v4 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 | 545 +++++++++++++++++++++++++++++++ 4 files changed, 557 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x-v2.c
MAINTAINERS +1 -0
@@ -14959,6 +14959,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 +545 -0
545 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH v4 2/2] i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
Quoted 6 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 18
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +11
-0+#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/clk.h> +#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/property.h> +#include <linux/regmap.h>
+ time.h // for USEC_PER_* et alia.
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+#include <linux/types.h> +#include <linux/units.h>
...
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +5
-0+/** +* struct loongson2_i2c_priv - private data of the controller +* @adapter: I2C adapter for this controller +* @complete: completion of I2C message +* @clk: hw i2c clock
drivers/i2c/busses/i2c-ls2x-v2.c line 114
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+* @regmap: regmap of the I2C device +* @parent_rate: I2C clock parent rate in MHz +* @msg: I2C transfer information +*/ +struct loongson2_i2c_priv { +struct i2c_adapter adapter; +struct completion complete; +struct clk *clk; +struct regmap *regmap; +unsigned long parent_rate;
it's better to keep units in the variable name
unsigned long parent_rate_MHz;
// yes, it's fine to spell the unit suffix as it's in physics.
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+struct loongson2_i2c_msg msg; +};
...
drivers/i2c/busses/i2c-ls2x-v2.c line 265
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+static void loongson2_i2c_handle_rx_done (struct loongson2_i2c_priv *priv) +{ +struct loongson2_i2c_msg *msg = &priv->msg; +u32 i;
Not needed to be here (see below how).
drivers/i2c/busses/i2c-ls2x-v2.c line 277
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+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);
First of all, in another case the type of iterator is unsigned int. Second, this iterator is not used outside of the for-loop, third, the upper limit is already known, no need to use magic, hence
for (unsigned int i = 0; i < msg->count; i++)
Quoted 17 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +17
-0+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 12 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +12
-0+static int loongson2_i2c_xfer (struct i2c_adapter *i2c_adap,struct i2c_msg msgs[],int num) +{ +struct loongson2_i2c_priv *priv =i2c_get_adapdata (i2c_adap); +struct device *dev = priv->adapter.dev.parent; +unsigned int i, status; +int ret; + +/* Wait I2C bus free */ + ret =regmap_read_poll_timeout (priv->regmap,LOONGSON2_I2C_SR2 , status, + !(status &LOONGSON2_I2C_SR2_BUSY ), +LOONGSON2_I2C_FREE_SLEEP_US , +LOONGSON2_I2C_FREE_TIMEOUT_US );
drivers/i2c/busses/i2c-ls2x-v2.c line 411
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +9
-0+if (ret) { +dev_dbg (dev,"The I2C bus is busy now.\n" ); +return ret; + } + +/* Start generation */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_START , +LOONGSON2_I2C_CR1_START ); +for (i =0 ; i < num; i++) {
for (unsigned int i = 0; i < num; i++) {Quoted 7 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +7
-0+ ret =loongson2_i2c_xfer_msg (priv, &msgs[i], i == num -1 ); +if (ret <0 ) +return ret; + } + +return num; +}
...
drivers/i2c/busses/i2c-ls2x-v2.c line 434
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) +{ +struct device *dev = priv->adapter.dev.parent; +struct i2c_timings i2c_t; +u32 val, freq_mhz, ccr;
freq_MHz
(or at least make the parent_rate and this one consistent with the suffixes in use)
Quoted 4 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+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 ) {
drivers/i2c/busses/i2c-ls2x-v2.c line 448
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +9
-0+/* 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);
hz --> Hz (in the message)
Review
Quoted 2 lines
(quoted diff) +2
-0+ return -EINVAL; + }
It seems part of the probe phase, so why not
return dev_err_probe(...);
?
Quoted 16 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +16
-0+FIELD_MODIFY (LOONGSON2_I2C_CCR_CCR , &ccr, val); +regmap_write (priv->regmap,LOONGSON2_I2C_CCR , ccr); + + 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)); + +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 ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 2/2] i2c: ls2x-v2: Add driver for Loongson-2K0300 I2C controller
Hi Andy:
Thanks for your continued review of the patch.
Quoted 74 lines · Andy Shevchenko
Quoted 6 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 18
Quoted 11 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +11
-0+#include <linux/bitfield.h> +#include <linux/bits.h> +#include <linux/clk.h> +#include <linux/io.h> +#include <linux/iopoll.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/property.h> +#include <linux/regmap.h> +
time.h// for USEC_PER_* et alia.Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+#include <linux/types.h> +#include <linux/units.h> ...
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +5
-0+/** +* struct loongson2_i2c_priv - private data of the controller +* @adapter: I2C adapter for this controller +* @complete: completion of I2C message +* @clk: hw i2c clock drivers/i2c/busses/i2c-ls2x-v2.c line 114
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+* @regmap: regmap of the I2C device +* @parent_rate: I2C clock parent rate in MHz +* @msg: I2C transfer information +*/ +struct loongson2_i2c_priv { +struct i2c_adapter adapter; +struct completion complete; +struct clk *clk; +struct regmap *regmap; +unsigned long parent_rate;it's better to keep units in the variable name
unsigned long parent_rate_MHz;// yes, it's fine to spell the unit suffix as it's in physics.
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +2
-0+struct loongson2_i2c_msg msg; +};...
drivers/i2c/busses/i2c-ls2x-v2.c line 265
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+static void loongson2_i2c_handle_rx_done (struct loongson2_i2c_priv *priv) +{ +struct loongson2_i2c_msg *msg = &priv->msg; +u32 i;Not needed to be here (see below how).
Quoted 8 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +8
-0+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 );
Quoted 8 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ for (i = 2; i > 0; i--) + loongson2_i2c_read_msg(priv);First of all, in another case the type of iterator is unsigned int. Second, this iterator is not used outside of the for-loop, third, the upper limit is already known, no need to use magic, hence
for (unsigned int i = 0; i < msg->count; i++)
Here, we read the last two bytes. In loongson2_i2c_read_msg(), msg->count is decremented, so the loop condition here should be:
for (unsigned int i = msg->count; i > 0; i--)
Also, I will address all other comments in the next version.
Quoted 112 lines
Quoted 18 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +17
-0+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 12 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +12
-0+static int loongson2_i2c_xfer (struct i2c_adapter *i2c_adap,struct i2c_msg msgs[],int num) +{ +struct loongson2_i2c_priv *priv =i2c_get_adapdata (i2c_adap); +struct device *dev = priv->adapter.dev.parent; +unsigned int i, status; +int ret; + +/* Wait I2C bus free */ + ret =regmap_read_poll_timeout (priv->regmap,LOONGSON2_I2C_SR2 , status, + !(status &LOONGSON2_I2C_SR2_BUSY ), +LOONGSON2_I2C_FREE_SLEEP_US , +LOONGSON2_I2C_FREE_TIMEOUT_US );drivers/i2c/busses/i2c-ls2x-v2.c line 411
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +9
-0+if (ret) { +dev_dbg (dev,"The I2C bus is busy now.\n" ); +return ret; + } + +/* Start generation */ +regmap_update_bits (priv->regmap,LOONGSON2_I2C_CR1 ,LOONGSON2_I2C_CR1_START , +LOONGSON2_I2C_CR1_START ); +for (i =0 ; i < num; i++) {for (unsigned int i = 0; i < num; i++) {Quoted 7 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +7
-0+ ret =loongson2_i2c_xfer_msg (priv, &msgs[i], i == num -1 ); +if (ret <0 ) +return ret; + } + +return num; +}...
drivers/i2c/busses/i2c-ls2x-v2.c line 434
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) +{ +struct device *dev = priv->adapter.dev.parent; +struct i2c_timings i2c_t; +u32 val, freq_mhz, ccr;
freq_MHz
(or at least make theparent_rateand this one consistent with the suffixes in use)Quoted 4 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +4
-0+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 ) {drivers/i2c/busses/i2c-ls2x-v2.c line 448
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +9
-0+/* 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);hz --> Hz (in the message)
Review
Quoted 2 lines
(quoted diff) +2
-0+ return -EINVAL; + }It seems part of the probe phase, so why not
return dev_err_probe(...);?
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +6
-0+FIELD_MODIFY (LOONGSON2_I2C_CCR_CCR , &ccr, val); +regmap_write (priv->regmap,LOONGSON2_I2C_CCR , ccr); + + 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));drivers/i2c/busses/i2c-ls2x-v2.c line 467
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x-v2.c new file +10
-0+ +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 ; +}--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
Thanks. Binbin