i2c: add support for microchip fpga i2c controllers
Subsystems (2)
PATCH 1/1 i2c: add support for microchip fpga i2c controllers
Add Microchip CoreI2C i2c controller support. This driver supports the "hard" i2c controller on the Microchip PolarFire SoC & the basic feature set for "soft" i2c controller implemtations in the FPGA fabric.
--- drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-microchip-core.c | 487 ++++++++++++++++++++++++ 3 files changed, 499 insertions(+) create mode 100644 drivers/i2c/busses/i2c-microchip-core.c
drivers/i2c/busses/Kconfig +11 -0
@@ -781,6 +781,17 @@ config I2C_MESON If you say yes to this option, support will be included for the I2C interface on the Amlogic Meson family of SoCs. +config I2C_MICROCHIP_CORE + tristate "Microchip FPGA I2C controller" + depends on SOC_MICROCHIP_POLARFIRE || COMPILE_TEST + depends on OF + help + If you say yes to this option, support will be included for the + I2C interface on Microchip FPGAs. + + This driver can also be built as a module. If so, the module will be + called i2c-microchip-core. + config I2C_MPC tristate "MPC107/824x/85xx/512x/52xx/83xx/86xx" depends on PPC
drivers/i2c/busses/Makefile +1 -0
@@ -78,6 +78,7 @@ obj-$(CONFIG_I2C_JZ4780) += i2c-jz4780.o obj-$(CONFIG_I2C_KEMPLD) += i2c-kempld.o obj-$(CONFIG_I2C_LPC2K) += i2c-lpc2k.o obj-$(CONFIG_I2C_MESON) += i2c-meson.o +obj-$(CONFIG_I2C_MICROCHIP_CORE) += i2c-microchip-core.o obj-$(CONFIG_I2C_MPC) += i2c-mpc.o obj-$(CONFIG_I2C_MT65XX) += i2c-mt65xx.o obj-$(CONFIG_I2C_MT7621) += i2c-mt7621.o
drivers/i2c/busses/i2c-microchip-core.c new file +487 -0
487 lines not on this page · raw message
Signature · 1 line
2.35.1
Quoted 22 lines · Conor Dooley
Add Microchip CoreI2C i2c controller support. This driver supports the "hard" i2c controller on the Microchip PolarFire SoC & the basic feature set for "soft" i2c controller implemtations in the FPGA fabric.
Co-developed-by Daire McNamara <[email protected]>Signed-off-by Daire McNamara <[email protected]>Signed-off-by Conor Dooley <[email protected]>--- drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-microchip-core.c | 487 ++++++++++++++++++++++++ 3 files changed, 499 insertions(+)create mode 100644
drivers/i2c/busses/i2c-microchip-core.cdrivers/i2c/busses/Kconfig +0
-0@@ -781,6 +781,17 @@ config I2C_MESON If you say yes to this option, support will be included for the I2C interface on the Amlogic Meson family of SoCs.
snip
Quoted 6 lines
drivers/i2c/busses/Kconfig +6
-0+ +static void mchp_corei2c_core_disable(struct mchp_corei2c_dev *idev) +{ + u8 ctrl = readl(idev->base + CORE_I2C_CTRL); + + ctrl &= ~CTRL_ENS1;
drivers/i2c/busses/Kconfig
Quoted 10 lines
drivers/i2c/busses/Kconfig +10
-0+ writel(ctrl, idev->base + CORE_I2C_CTRL); +} + +static void mchp_corei2c_core_enable(struct mchp_corei2c_dev *idev) +{ + u8 ctrl = readl(idev->base + CORE_I2C_CTRL); + + ctrl |= CTRL_ENS1; + writel(ctrl, idev->base + CORE_I2C_CTRL); +}
Not sure why you would use readl/writel with an u8, surely an readb/writeb be better?
drivers/i2c/busses/Kconfig
Quoted 9 lines
drivers/i2c/busses/Kconfig +9
-0+static irqreturn_t mchp_corei2c_handle_isr(struct mchp_corei2c_dev *idev) +{ + u32 status = idev->isr_status; + u8 ctrl; + + if (!idev->buf) { + dev_warn(idev->dev, "unexpected interrupt\n"); + return IRQ_HANDLED; + }
is IRQ_HANDLED correct here?
Quoted 47 lines
drivers/i2c/busses/Kconfig +47
-0+ +static int mchp_corei2c_probe(struct platform_device *pdev) +{ + struct mchp_corei2c_dev *idev = NULL; + struct resource *res; + int irq, ret; + u32 val; + + idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); + if (!idev) + return -ENOMEM; + + idev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); + if (IS_ERR(idev->base)) + return PTR_ERR(idev->base); + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return dev_err_probe(&pdev->dev, irq, + "missing interrupt resource\n"); + + idev->i2c_clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(idev->i2c_clk)) + return dev_err_probe(&pdev->dev, PTR_ERR(idev->i2c_clk), + "missing clock\n"); + + idev->dev = &pdev->dev; + init_completion(&idev->msg_complete); + spin_lock_init(&idev->lock); + + val = device_property_read_u32(idev->dev, "clock-frequency", + &idev->bus_clk_rate); + if (val) { + dev_info(&pdev->dev, "default to 100kHz\n"); + idev->bus_clk_rate = 100000; + } + + if (idev->bus_clk_rate > 400000) + return dev_err_probe(&pdev->dev, -EINVAL, + "clock-frequency too high: %d\n", + idev->bus_clk_rate); + + ret = devm_request_irq(&pdev->dev, irq, mchp_corei2c_isr, IRQF_SHARED, + pdev->name, idev); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to claim irq %d\n", irq);
drivers/i2c/busses/Kconfig
Quoted 10 lines
drivers/i2c/busses/Kconfig +10
-0+ + ret = clk_prepare_enable(idev->i2c_clk); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to enable clock\n"); + + ret = mchp_corei2c_init(idev); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to program clock divider\n");
going to leak a prepared clock from here on?
Quoted 7 lines
drivers/i2c/busses/Kconfig +7
-0+ i2c_set_adapdata(&idev->adapter, idev); + snprintf(idev->adapter.name, sizeof(idev->adapter.name), + "Microchip I2C hw bus"); + idev->adapter.owner = THIS_MODULE; + idev->adapter.algo = &mchp_corei2c_algo; + idev->adapter.dev.parent = &pdev->dev; + idev->adapter.dev.of_node = pdev->dev.of_node;
drivers/i2c/busses/Kconfig
Quoted 10 lines
drivers/i2c/busses/Kconfig +10
-0+ + platform_set_drvdata(pdev, idev); + + ret = i2c_add_adapter(&idev->adapter); + if (ret) { + clk_disable_unprepare(idev->i2c_clk); + return ret; + } + + dev_info(&pdev->dev, "Microchip I2C Probe Complete\n");
not sure if necessary, doesn't the i2c core also announce?
Quoted 35 lines
drivers/i2c/busses/Kconfig +35
-0+ return 0; +} + +static int mchp_corei2c_remove(struct platform_device *pdev) +{ + struct mchp_corei2c_dev *idev = platform_get_drvdata(pdev); + + clk_disable_unprepare(idev->i2c_clk); + i2c_del_adapter(&idev->adapter); + + return 0; +} + +static const struct of_device_id mchp_corei2c_of_match[] = { + { .compatible = "microchip,mpfs-i2c" }, + { .compatible = "microchip,corei2c-rtl-v7" }, + {}, +}; +MODULE_DEVICE_TABLE(of, mchp_corei2c_of_match); + +static struct platform_driver mchp_corei2c_driver = { + .probe = mchp_corei2c_probe, + .remove = mchp_corei2c_remove, + .driver = { + .name = "microchip-corei2c", + .of_match_table = mchp_corei2c_of_match, + }, +}; + +module_platform_driver(mchp_corei2c_driver); + +MODULE_DESCRIPTION("Microchip CoreI2C bus driver"); +MODULE_AUTHOR("Daire McNamara <[email protected]>"); +MODULE_AUTHOR("Conor Dooley <[email protected]>"); +MODULE_LICENSE("GPL v2");
Signature · 4 lines
Ben Dooks http://www.codethink.co.uk/ Senior Engineer Codethink - Providing Genius https://www.codethink.co.uk/privacy.html
Quoted 36 lines · Ben Dooks
Quoted 21 lines · Conor Dooley
Add Microchip CoreI2C i2c controller support. This driver supports the "hard" i2c controller on the Microchip PolarFire SoC & the basic feature set for "soft" i2c controller implemtations in the FPGA fabric.
Co-developed-by Daire McNamara <[email protected]>Signed-off-by Daire McNamara <[email protected]>Signed-off-by Conor Dooley <[email protected]>---
drivers/i2c/busses/Kconfig | 11 + drivers/i2c/busses/Makefile | 1 +drivers/i2c/busses/i2c-microchip-core.c| 487 ++++++++++++++++++++++++3 files changed, 499 insertions(+)create mode 100644
drivers/i2c/busses/i2c-microchip-core.cdrivers/i2c/busses/Kconfig +0
-0@@ -781,6 +781,17 @@ config I2C_MESONIf you say yes to this option, support will be included for the I2C interface on the Amlogic Meson family of SoCs.
snip
Quoted 11 lines
drivers/i2c/busses/Kconfig +11
-0+ +static void mchp_corei2c_core_disable(struct mchp_corei2c_dev *idev) +{ + u8 ctrl = readl(idev->base + CORE_I2C_CTRL); + + ctrl &= ~CTRL_ENS1; + writel(ctrl, idev->base + CORE_I2C_CTRL); +} + +static void mchp_corei2c_core_enable(struct mchp_corei2c_dev *idev) +{
Quoted 8 lines
drivers/i2c/busses/Kconfig
Quoted 5 lines
+ u8 ctrl =
readl(idev->base + CORE_I2C_CTRL);drivers/i2c/busses/Kconfig +4
-0+ + ctrl |= CTRL_ENS1; + writel(ctrl, idev->base + CORE_I2C_CTRL); +}Not sure why you would use readl/writel with an u8, surely an readb/writeb be better?
Sure, can try drop it to b.
Quoted 14 lines
drivers/i2c/busses/Kconfig
Quoted 12 lines
+static
irqreturn_tmchp_corei2c_handle_isr(struct mchp_corei2c_dev *idev)drivers/i2c/busses/Kconfig +8
-0+{ + u32 status = idev->isr_status; + u8 ctrl; + + if (!idev->buf) { + dev_warn(idev->dev, "unexpected interrupt\n"); + return IRQ_HANDLED; + }is
IRQ_HANDLEDcorrect here?
Hmm, the dev_warn is probably incorrect too. For the mpfs, the interrupt line isn't shared, but the generic fabric core could be. Ill change this.
Quoted 53 lines
Quoted 53 lines
drivers/i2c/busses/Kconfig +43
-0+ +static int mchp_corei2c_probe(struct platform_device *pdev) +{ + struct mchp_corei2c_dev *idev = NULL; + struct resource *res; + int irq, ret; + u32 val; + + idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); + if (!idev) + return -ENOMEM; + + idev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); + if (IS_ERR(idev->base)) + return PTR_ERR(idev->base); + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return dev_err_probe(&pdev->dev, irq, + "missing interrupt resource\n"); + + idev->i2c_clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(idev->i2c_clk)) + return dev_err_probe(&pdev->dev, PTR_ERR(idev->i2c_clk), + "missing clock\n"); + + idev->dev = &pdev->dev; + init_completion(&idev->msg_complete); + spin_lock_init(&idev->lock); + + val = device_property_read_u32(idev->dev, "clock-frequency", + &idev->bus_clk_rate); + if (val) { + dev_info(&pdev->dev, "default to 100kHz\n"); + idev->bus_clk_rate = 100000; + } + + if (idev->bus_clk_rate > 400000) + return dev_err_probe(&pdev->dev, -EINVAL, + "clock-frequency too high: %d\n", + idev->bus_clk_rate); + + ret = devm_request_irq(&pdev->dev, irq, mchp_corei2c_isr,
IRQF_SHARED,
+pdev->name, idev);
+ if (ret)
+ returndev_err_probe(&pdev->dev, ret, + "failed to claim irq %d\n", irq); +
+ ret =clk_prepare_enable(idev->i2c_clk); + if (ret)
+ return dev_err_probe(&pdev->dev, ret,
Quoted 8 lines
Quoted 6 lines
+ "failed to enable clock\n"); +
+ ret =mchp_corei2c_init(idev);
+ if (ret)
+ return dev_err_probe(&pdev->dev, ret,
+ "failed to program clock divider\n");going to leak a prepared clock from here on?
Aye, good spot. Thanks.
Quoted 11 lines
Quoted 11 lines
+
i2c_set_adapdata(&idev->adapter, idev);
+snprintf(idev->adapter.name, sizeof(idev->adapter.name), + "Microchip I2C hw bus");
+idev->adapter.owner=THIS_MODULE;
+idev->adapter.algo=&mchp_corei2c_algo;
+idev->adapter.dev.parent=&pdev->dev;
+idev->adapter.dev.of_node=pdev->dev.of_node;drivers/i2c/busses/Kconfig +4
-0+ + platform_set_drvdata(pdev, idev); + + ret = i2c_add_adapter(&idev->adapter);
Quoted 8 lines
Quoted 6 lines
+ if (ret) {
+clk_disable_unprepare(idev->i2c_clk);
+ return ret;
+ }
+
+dev_info(&pdev->dev, "Microchip I2C Probe Complete\n");not sure if necessary, doesn't the i2c core also announce?
The only thing I see on boot is this, but I'll take a look and see if I can do this via the core.
Quoted 38 lines
Quoted 36 lines
+ return 0;
drivers/i2c/busses/Kconfig +34
-0+} + +static int mchp_corei2c_remove(struct platform_device *pdev) +{ + struct mchp_corei2c_dev *idev = platform_get_drvdata(pdev); + + clk_disable_unprepare(idev->i2c_clk); + i2c_del_adapter(&idev->adapter); + + return 0; +} + +static const struct of_device_id mchp_corei2c_of_match[] = { + { .compatible = "microchip,mpfs-i2c" }, + { .compatible = "microchip,corei2c-rtl-v7" }, + {}, +}; +MODULE_DEVICE_TABLE(of, mchp_corei2c_of_match); + +static struct platform_driver mchp_corei2c_driver = { + .probe = mchp_corei2c_probe, + .remove = mchp_corei2c_remove, + .driver = { + .name = "microchip-corei2c", + .of_match_table = mchp_corei2c_of_match, + }, +}; + +module_platform_driver(mchp_corei2c_driver); + +MODULE_DESCRIPTION("Microchip CoreI2C bus driver"); +MODULE_AUTHOR("Daire McNamara <[email protected]>"); +MODULE_AUTHOR("Conor Dooley <[email protected]>"); +MODULE_LICENSE("GPL v2");