i2c: ls2x: Add support for the Loongson-2K/LS7A I2C
Subsystems (6)
Binbin Zhou
maintainerv2 cover i2c: ls2x: Add support for the Loongson-2K/LS7A I2C
Hi all:
This patch series adds support for the I2C module found on various Loongson systems with the Loongson-2K SoC or the Loongson LS7A bridge chip.
For now, the I2C driver is suitable for DT-based or ACPI-based systems.
I have only tested the Loongson-3A5000+LS7A1000/LS7A2000 under LoongArch architecture.
Thanks.
Changes since V1:
1. Remove the function of getting the static i2c bus number from ACPI "_UID";
2. Fix build warning from kernel test robot.
Binbin Zhou (4):
i2c: gpio: Add support on ACPI-based system
dt-bindings: i2c: add bindings for Loongson LS2X I2C
i2c: Add driver for Loongson-2K/LS7A I2C controller
LoongArch: Enable LS2X I2C in loongson3_defconfig
.../bindings/i2c/loongson,ls2x-i2c.yaml | 48 +++ arch/loongarch/configs/loongson3_defconfig | 1 + drivers/i2c/busses/Kconfig | 7 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-gpio.c | 30 ++ drivers/i2c/busses/i2c-ls2x.c | 365 ++++++++++++++++++ 6 files changed, 452 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml create mode 100644 drivers/i2c/busses/i2c-ls2x.c
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v2 1/4 i2c: gpio: Add support on ACPI-based system
Add support for the ACPI-based device registration so that the driver can be also enabled through ACPI table.
--- drivers/i2c/busses/i2c-gpio.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
drivers/i2c/busses/i2c-gpio.c +30 -0
@@ -13,6 +13,7 @@#include <linux/init.h> #include <linux/interrupt.h> #include <linux/module.h> +#include <linux/acpi.h> #include <linux/of.h> #include <linux/platform_data/i2c-gpio.h> #include <linux/platform_device.h>
@@ -318,6 +319,24 @@ static void of_i2c_gpio_get_props(struct device_node *np,of_property_read_bool (np,"i2c-gpio,scl-output-only" ); } +static void acpi_i2c_gpio_get_props (struct device *dev, +struct i2c_gpio_platform_data *pdata) +{ +u32 reg; + +device_property_read_u32 (dev,"delay-us" , &pdata->udelay); + +if (!device_property_read_u32 (dev,"timeout-ms" , ®)) + pdata->timeout =msecs_to_jiffies (reg); + + pdata->sda_is_open_drain = +device_property_read_bool (dev,"sda-open-drain" ); + pdata->scl_is_open_drain = +device_property_read_bool (dev,"scl-open-drain" ); + pdata->scl_is_output_only = +device_property_read_bool (dev,"scl-output-only" ); +} + static struct gpio_desc *i2c_gpio_get_desc(struct device *dev, const char *con_id, unsigned int index,
@@ -375,6 +394,8 @@ static int i2c_gpio_probe(struct platform_device *pdev)if (np) {of_i2c_gpio_get_props (np, pdata); + }else if (ACPI_COMPANION (dev)) { +acpi_i2c_gpio_get_props (dev, pdata); }else {/* * If all platform data settings are zero it is OK
@@ -491,10 +512,19 @@ static const struct of_device_id i2c_gpio_dt_ids[] = {MODULE_DEVICE_TABLE (of, i2c_gpio_dt_ids);#endif +#ifdef CONFIG_ACPI +static const struct acpi_device_id i2c_gpio_acpi_match[] = { + {"LOON0005" },/*LoongArch*/ + {} +}; +MODULE_DEVICE_TABLE (acpi, i2c_gpio_acpi_match); +#endif +static struct platform_driver i2c_gpio_driver = { .driver = { .name ="i2c-gpio" , .of_match_table =of_match_ptr (i2c_gpio_dt_ids), + .acpi_match_table =ACPI_PTR (i2c_gpio_acpi_match), }, .probe = i2c_gpio_probe, .remove = i2c_gpio_remove,
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v2 2/4 dt-bindings: i2c: add bindings for Loongson LS2X I2C
In reply to Binbin ZhouAdd device tree bindings for the i2c controller on the Loongson-2K Soc or Loongosn LS7A bridge.
--- .../bindings/i2c/loongson,ls2x-i2c.yaml | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml
Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml new file +48 -0
@@ -0,0 +1,48 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: "http://devicetree.org/schemas/i2c/loongson,ls2x-i2c.yaml#" +$schema: "http://devicetree.org/meta-schemas/core.yaml#" + +title: Loongson LS2X I2C Controller + +maintainers: + - Binbin Zhou <[email protected]> + +allOf: + - $ref: /schemas/i2c/i2c-controller.yaml# + +properties: + compatible: + enum: + - loongson,ls2k-i2c # Loongson-2K SoCs + - loongson,ls7a-i2c # Loongson LS7A Bridge + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + +required: + - compatible + - reg + - interrupts + +unevaluatedProperties: false + +examples: + - | + i2c@1fe21000 { + compatible = "loongson,ls2k-i2c"; + reg = <0 0x1fe21000 0 0x8>; + interrupts = <22>; + #address-cells = <1>; + #size-cells = <0>; + + eeprom@57{ + compatible = "atmel,24c16"; + reg = <0x57>; + pagesize = <16>; + }; + };
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v2 3/4 i2c: Add driver for Loongson-2K/LS7A I2C controller
In reply to Binbin ZhouThis I2C module is integrated into the Loongson-2K SoC and the Loongson LS7A bridge chip.
Initialize the i2c controller early. This is required in order to ensure that core system devices such as the display controller(DC) attached via I2C are available early in boot.
--- drivers/i2c/busses/Kconfig | 7 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-ls2x.c | 365 ++++++++++++++++++++++++++++++++++ 3 files changed, 373 insertions(+) create mode 100644 drivers/i2c/busses/i2c-ls2x.c
drivers/i2c/busses/Kconfig +7 -0
@@ -887,6 +887,13 @@ config I2C_OWL Say Y here if you want to use the I2C bus controller on the Actions Semiconductor Owl SoC's. +config I2C_LS2X + tristate "Loongson LS2X I2C adapter" + depends on MACH_LOONGSON64 || COMPILE_TEST + help + If you say yes to this option, support will be included for the + I2C interface on the Loongson's LS2K/LS7A Platform-Bridge. + config I2C_PASEMI tristate "PA Semi SMBus interface" depends on PPC_PASEMI && PCI
drivers/i2c/busses/Makefile +1 -0
@@ -86,6 +86,7 @@ obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o obj-$(CONFIG_I2C_MXS) += i2c-mxs.o obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o obj-$(CONFIG_I2C_NPCM) += i2c-npcm7xx.o +obj-$(CONFIG_I2C_LS2X) += i2c-ls2x.o obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o obj-$(CONFIG_I2C_OMAP) += i2c-omap.o obj-$(CONFIG_I2C_OWL) += i2c-owl.o
drivers/i2c/busses/i2c-ls2x.c new file +365 -0
365 lines not on this page · raw message
Signature · 1 line
2.31.1
Binbin Zhou
maintainerPATCH v2 4/4 LoongArch: Enable LS2X I2C in loongson3_defconfig
In reply to Binbin ZhouThis is now supported, enable for Loongson-3 systems. Other systems are affected.
--- arch/loongarch/configs/loongson3_defconfig | 1 + 1 file changed, 1 insertion(+)
arch/loongarch/configs/loongson3_defconfig +1 -0
@@ -558,6 +558,7 @@ CONFIG_HW_RANDOM_VIRTIO=m CONFIG_I2C_CHARDEV=y CONFIG_I2C_PIIX4=y CONFIG_I2C_GPIO=y +CONFIG_I2C_LS2X=y CONFIG_SPI=y CONFIG_GPIO_SYSFS=y CONFIG_GPIO_LOONGSON=y
Signature · 1 line
2.31.1
Re: [PATCH V2 1/4] i2c: gpio: Add support on ACPI-based system
In reply to Binbin Zhou[+Rafael and Andy]
Hi,
Quoted 32 lines · Binbin Zhou
Add support for the ACPI-based device registration so that the driver can be also enabled through ACPI table.
Signed-off-by Huacai Chen <[email protected]>Signed-off-by Binbin Zhou <[email protected]>--- drivers/i2c/busses/i2c-gpio.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)drivers/i2c/busses/i2c-gpio.c +8
-0@@ -13,6 +13,7 @@#include <linux/init.h> #include <linux/interrupt.h> #include <linux/module.h> +#include <linux/acpi.h> #include <linux/of.h> #include <linux/platform_data/i2c-gpio.h> #include <linux/platform_device.h> @@ -318,6 +319,24 @@ static void of_i2c_gpio_get_props(struct device_node *np,of_property_read_bool (np,"i2c-gpio,scl-output-only" ); } +static void acpi_i2c_gpio_get_props (struct device *dev, +struct i2c_gpio_platform_data *pdata) +{ +u32 reg; + +device_property_read_u32 (dev,"delay-us" , &pdata->udelay); +
drivers/i2c/busses/i2c-gpio.c static void of_i2c_gpio_get_props(struct device_node *np, · line 338
Quoted 10 lines
drivers/i2c/busses/i2c-gpio.c +10
-0+if (!device_property_read_u32 (dev,"timeout-ms" , ®)) + pdata->timeout =msecs_to_jiffies (reg); + + pdata->sda_is_open_drain = +device_property_read_bool (dev,"sda-open-drain" ); + pdata->scl_is_open_drain = +device_property_read_bool (dev,"scl-open-drain" ); + pdata->scl_is_output_only = +device_property_read_bool (dev,"scl-output-only" ); +}
Otherwise this patch looks good but I'm concerned because we have two kinds of bindings now. The DT one above uses "i2c-gpio,..." and this ACPI one uses just "..." so the question is where did these come from? Is there already some existing system out there with these bindings or they are documented somewhere?
Ideally we would be able to just do:
pdata->sda_is_open_drain = device_property_read_bool(dev, "i2c-gpio,sda-open-drain");
for any firmware description.
Quoted 35 lines
drivers/i2c/busses/i2c-gpio.c +12
-0+ static struct gpio_desc *i2c_gpio_get_desc(struct device *dev, const char *con_id, unsigned int index,@@ -375,6 +394,8 @@ static int i2c_gpio_probe(struct platform_device *pdev)if (np) {of_i2c_gpio_get_props (np, pdata); + }else if (ACPI_COMPANION (dev)) { +acpi_i2c_gpio_get_props (dev, pdata); }else {/* * If all platform data settings are zero it is OK @@ -491,10 +512,19 @@ static const struct of_device_id i2c_gpio_dt_ids[] = {MODULE_DEVICE_TABLE (of, i2c_gpio_dt_ids);#endif +#ifdef CONFIG_ACPI +static const struct acpi_device_id i2c_gpio_acpi_match[] = { + {"LOON0005" },/*LoongArch*/ + {} +}; +MODULE_DEVICE_TABLE (acpi, i2c_gpio_acpi_match); +#endif +static struct platform_driver i2c_gpio_driver = { .driver = { .name ="i2c-gpio" , .of_match_table =of_match_ptr (i2c_gpio_dt_ids), + .acpi_match_table =ACPI_PTR (i2c_gpio_acpi_match), }, .probe = i2c_gpio_probe, .remove = i2c_gpio_remove,Signature · 1 line
2.31.1
Re: [PATCH V2 1/4] i2c: gpio: Add support on ACPI-based system
In reply to Binbin ZhouQuoted 4 lines · Binbin Zhou
Add support for the ACPI-based device registration so that the driver can be also enabled through ACPI table.
Signed-off-by Huacai Chen <[email protected]>
Who is this and why this SoB in the chain?
Quoted 1 line
Signed-off-by Binbin Zhou <[email protected]>
...
Quoted 7 lines
#include <
linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
+#include <linux/acpi.h>
#include <linux/of.h>
#include <linux/platform_data/i2c-gpio.h>
#include <linux/platform_device.h>
Seems you misinterpret ordering.
Besides that I don't see the needs of acpi.h. The header missed the mod_devicetable.h (even without this patch), and your code needs property.h.
...
Quoted 7 lines
drivers/i2c/busses/i2c-gpio.c +7
-0+static void acpi_i2c_gpio_get_props (struct device *dev, +struct i2c_gpio_platform_data *pdata) +{ +u32 reg; + +device_property_read_u32 (dev,"delay-us" , &pdata->udelay); +
drivers/i2c/busses/i2c-gpio.c static void of_i2c_gpio_get_props(struct device_node *np, · line 338
Quoted 10 lines
drivers/i2c/busses/i2c-gpio.c +10
-0+if (!device_property_read_u32 (dev,"timeout-ms" , ®)) + pdata->timeout =msecs_to_jiffies (reg); + + pdata->sda_is_open_drain = +device_property_read_bool (dev,"sda-open-drain" ); + pdata->scl_is_open_drain = +device_property_read_bool (dev,"scl-open-drain" ); + pdata->scl_is_output_only = +device_property_read_bool (dev,"scl-output-only" ); +}
+1 to Mika's objection. Instead, make the common bindings and convert the driver from OF to be agnostic.
...
Quoted 4 lines
MODULE_DEVICE_TABLE(of, i2c_gpio_dt_ids);
#endif+#ifdef
CONFIG_ACPI
Please, drop these ifdefferies (including OF one), it's more harmful than useful.
Quoted 1 line
+#endif
...
drivers/i2c/busses/i2c-gpio.c static const struct of_device_id i2c_gpio_dt_ids[] = { · line 527
Quoted 2 lines
drivers/i2c/busses/i2c-gpio.c +1
-0.of_match_table = of_match_ptr(i2c_gpio_dt_ids), + .acpi_match_table = ACPI_PTR(i2c_gpio_acpi_match),
No ACPI_PTR(), and accordingly no of_match_ptr(). See above.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V2 3/4] i2c: Add driver for Loongson-2K/LS7A I2C controller
In reply to Binbin ZhouQuoted 6 lines · Binbin Zhou
This I2C module is integrated into the Loongson-2K SoC and the Loongson LS7A bridge chip.
Initialize the i2c controller early. This is required in order to ensure that core system devices such as the display controller(DC) attached via I2C are available early in boot.
...
drivers/i2c/busses/Kconfig config I2C_OWL · line 895
Quoted 3 lines
drivers/i2c/busses/Kconfig +3
-0+ help + If you say yes to this option, support will be included for the + I2C interface on the Loongson's LS2K/LS7A Platform-Bridge.
What will be module name?
...
Quoted 2 lines
+ * Copyright (C) 2013 Loongson Technology Corporation Limited
+ * Copyright (C) 2014-2017 Lemote, Inc.
It's 2022 out of the window, are you sure this code wasn't changed for 5 years?!
...
drivers/i2c/busses/i2c-ls2x.c line 18
Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c new file +8
-0+#include <linux/io.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/acpi.h> +#include <linux/delay.h> +#include <linux/module.h> +#include <linux/completion.h> +#include <linux/platform_device.h>
Keep it sorted.
Also check the headers, the rule of thumb is to include headers you are direct user of, excluding the ones that are guaranteed to be included by already mentioned.
...
Quoted 1 line
+#define
I2C_MAX_RETRIES5
No namespace?
...
Quoted 1 line
+#define
I2C_CLK_RATE_50M(50 * 1000000)
HZ_PER_MHZ
...
drivers/i2c/busses/i2c-ls2x.c line 47
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2
-0+#define i2c_readb (addr) readb(dev->base + addr) +#define i2c_writeb (val, addr) writeb(val, dev->base + addr)
No namespace? What is the usefulness of these macros taking into consideration that:
- they are macros and not inliners
- they missed the used parameter
...
drivers/i2c/busses/i2c-ls2x.c line 56
Quoted 8 lines
drivers/i2c/busses/i2c-ls2x.c new file +8
-0+struct ls2x_i2c_dev { +unsigned int suspended:1 ; +struct device *dev; +void __iomem *base; +int irq; +u32 bus_clk_rate; +struct completion cmd_complete; +struct i2c_adapter adapter;
You may save a few bytes of code if you put the first member the one that is used a lot in the pointer arithmetics or performance-wise. You may check the result with bloat-o-meter.
drivers/i2c/busses/i2c-ls2x.c line 69
Quoted 13 lines
drivers/i2c/busses/i2c-ls2x.c new file +12
-0+}; +static void i2c_stop (struct ls2x_i2c_dev *dev) +{ +again: +i2c_writeb (LS2X_I2C_CMD_STOP ,LS2X_I2C_CR_REG ); +wait_for_completion (&dev->cmd_complete); + +i2c_readb (LS2X_I2C_SR_REG ); + +while (i2c_readb (LS2X_I2C_SR_REG ) &LS2X_I2C_SR_BUSY ) + goto again; +}
Can't you refactor to avoid label?
...
drivers/i2c/busses/i2c-ls2x.c line 77
Quoted 7 lines
drivers/i2c/busses/i2c-ls2x.c new file +6
-0+static int ls2x_i2c_start (struct ls2x_i2c_dev *dev, +int dev_addr,int flags) +{ +int retry =I2C_MAX_RETRIES ; +unsigned char addr = (dev_addr &0x7f ) <<1 ; + addr |= (flags &I2C_M_RD ) ?1 :0 ;
NIH: i2c_8bit_addr_from_msg() ?
drivers/i2c/busses/i2c-ls2x.c line 82
Quoted 5 lines
drivers/i2c/busses/i2c-ls2x.c new file +5
-0+start: +mdelay (1 ); +i2c_writeb (addr,LS2X_I2C_TXR_REG ); +dev_dbg (dev->dev,"%s <line%d>: i2c device address: 0x%x\n" , + __func__, __LINE__, addr);
No need to have __func__, __LINE__, etc. First of all, these are available via Dynamic Debug. Second, using those mean the lack of uniqueness of the message test, make it more unique instead.
drivers/i2c/busses/i2c-ls2x.c line 91
Quoted 10 lines
drivers/i2c/busses/i2c-ls2x.c new file +9
-0+ +i2c_writeb ((LS2X_I2C_CMD_START |LS2X_I2C_CMD_WRITE ), +LS2X_I2C_CR_REG ); +wait_for_completion (&dev->cmd_complete); + +if (i2c_readb (LS2X_I2C_SR_REG ) &LS2X_I2C_SR_NOACK ) { +i2c_stop (dev); +while (retry--) + goto start;
Try to refactor your code to avoid using too many labels here and there.
Quoted 6 lines
drivers/i2c/busses/i2c-ls2x.c new file +6
-0+dev_info (dev->dev,"There is no i2c device ack\n" ); +return 0 ; + } + +return 1 ; +}
...
drivers/i2c/busses/i2c-ls2x.c line 101
Quoted 1 line
drivers/i2c/busses/i2c-ls2x.c new file +1
-0+u16 val =0x12c ;
Magic!
...
drivers/i2c/busses/i2c-ls2x.c line 108
Quoted 2 lines
drivers/i2c/busses/i2c-ls2x.c new file +2
-0+i2c_writeb (val &0xff ,LS2X_I2C_PRER_LO_REG ); +i2c_writeb ((val &0xff00 ) >>8 ,LS2X_I2C_PRER_HI_REG );
Redundant '& 0xff...' parts. Besides that, is there any HW limitation of using 16-bit writes?
...
drivers/i2c/busses/i2c-ls2x.c line 109
Quoted 1 line
drivers/i2c/busses/i2c-ls2x.c new file +1
-0+i2c_writeb (0xc0 ,LS2X_I2C_CTR_REG );
Magic!
It's enough for now, this code needs much more work, please take your time.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH V2 1/4] i2c: gpio: Add support on ACPI-based system
In reply to Mika WesterbergQuoted 15 lines · Mika Westerberg
Quoted 15 lines
drivers/i2c/busses/i2c-gpio.c +15
-0+static void acpi_i2c_gpio_get_props (struct device *dev, +struct i2c_gpio_platform_data *pdata) +{ +u32 reg; + +device_property_read_u32 (dev,"delay-us" , &pdata->udelay); + +if (!device_property_read_u32 (dev,"timeout-ms" , ®)) + pdata->timeout =msecs_to_jiffies (reg); + + pdata->sda_is_open_drain = +device_property_read_bool (dev,"sda-open-drain" ); + pdata->scl_is_open_drain = +device_property_read_bool (dev,"scl-open-drain" ); + pdata->scl_is_output_only =
Quoted 8 lines
drivers/i2c/busses/i2c-gpio.c static void of_i2c_gpio_get_props(struct device_node *np, · line 338
Quoted 2 lines
drivers/i2c/busses/i2c-gpio.c +2
-0+device_property_read_bool (dev,"scl-output-only" ); +}Otherwise this patch looks good but I'm concerned because we have two kinds of bindings now. The DT one above uses "i2c-gpio,..." and this ACPI one uses just "..." so the question is where did these come from? Is there already some existing system out there with these bindings or they are documented somewhere?
I'm fairly sure it's just a mistake and it should use the regular binding. As far as I understand, there are still other incompatible changes being made to the firmware on these machines, so it's just a matter of updating this part as well.
Arnd