i2c: vt8500: Add support for I2C bus on Wondermedia SoCs
Subsystems (2)
PATCH 1/1 i2c: vt8500: Add support for I2C bus on Wondermedia SoCs
This patch adds support for the I2C bus controllers found on Wondermedia 8xxx-series SoCs. Only master-mode is supported.
--- .../devicetree/bindings/i2c/i2c-vt8500.txt | 24 + MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-wmt.c | 488 ++++++++++++++++++++ 5 files changed, 524 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/i2c-vt8500.txt create mode 100644 drivers/i2c/busses/i2c-wmt.c
Documentation/devicetree/bindings/i2c/i2c-vt8500.txt new file +24 -0
@@ -0,0 +1,24 @@ +* Wondermedia I2C Controller + +Required properties : + + - compatible : should be "wm,wm8505-i2c" + - reg : Offset and length of the register set for the device + - interrupts : <IRQ> where IRQ is the interrupt number + - clocks : phandle to the I2C clock source + +Optional properties : + + - clock-frequency : desired I2C bus clock frequency in Hz. + Valid values are 100000 and 400000. + Default to 100000 if not specified, or invalid value. + +Example : + + i2c_0: i2c at d8280000 { + compatible = "wm,wm8505-i2c"; + reg = <0xd8280000 0x1000>; + interrupts = <19>; + clocks = <&clki2c0>; + clock-frequency = <400000>; + };
MAINTAINERS +1 -0
@@ -1285,6 +1285,7 @@ S: Maintained F: arch/arm/mach-vt8500/ F: drivers/clocksource/vt8500_timer.c F: drivers/gpio/gpio-vt8500.c +F: drivers/i2c/busses/i2c-wmt.c F: drivers/mmc/host/wmt-sdmmc.c F: drivers/pwm/pwm-vt8500.c F: drivers/rtc/rtc-vt8500.c
drivers/i2c/busses/Kconfig +10 -0
@@ -724,6 +724,16 @@ config I2C_VERSATILE This driver can also be built as a module. If so, the module will be called i2c-versatile. +config I2C_WMT + tristate "Wondermedia WM8xxx SoC I2C bus support" + depends on ARCH_VT8500 + help + Say yes if you want to support the I2C bus on Wondermedia 8xxx-series + SoCs. + + This driver can also be built as a module. If so, the module will be + called i2c-wmt. + config I2C_OCTEON tristate "Cavium OCTEON I2C bus support" depends on CPU_CAVIUM_OCTEON
drivers/i2c/busses/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_I2C_SIRF) += i2c-sirf.o obj-$(CONFIG_I2C_STU300) += i2c-stu300.o obj-$(CONFIG_I2C_TEGRA) += i2c-tegra.o obj-$(CONFIG_I2C_VERSATILE) += i2c-versatile.o +obj-$(CONFIG_I2C_WMT) += i2c-wmt.o obj-$(CONFIG_I2C_OCTEON) += i2c-octeon.o obj-$(CONFIG_I2C_XILINX) += i2c-xiic.o obj-$(CONFIG_I2C_XLR) += i2c-xlr.o
drivers/i2c/busses/i2c-wmt.c new file +488 -0
488 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Quoted 11 lines · Tony Prisk
This patch adds support for the I2C bus controllers found on Wondermedia 8xxx-series SoCs. Only master-mode is supported.
Signed-off-by Tony Prisk <[email protected]>--- .../devicetree/bindings/i2c/i2c-vt8500.txt | 24 + MAINTAINERS | 1 + drivers/i2c/busses/Kconfig | 10 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-wmt.c | 488 ++++++++++++++++++++ 5 files changed, 524 insertions(+)
Quoted 4 lines
create mode 100644
Documentation/devicetree/bindings/i2c/i2c-vt8500.txtcreate mode 100644drivers/i2c/busses/i2c-wmt.c
This patch seems to have stalled. Any news?
Regards
Tony Prisk
Hi Tony,
Quoted 6 lines
drivers/i2c/busses/i2c-wmt.c new file +1
-0+static int wmt_i2c_wait_bus_not_busy (struct wmt_i2c_dev *i2c_dev)drivers/i2c/busses/i2c-wmt.c new file +0
-0
...
Quoted 4 lines
drivers/i2c/busses/i2c-wmt.c new file +4
-0+{ +u16 val; +int i; +int ret =0 ;
drivers/i2c/busses/i2c-wmt.c line 106
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ +for (i =0 ; i <100000 ; i++) { + val =readw (i2c_dev->base +REG_CSR ); +if (val &CSR_READY_MASK ) +break ; + +udelay (1 ); + } +if (i >=9999999 ) + ret = -EBUSY ;
What about using time_after and usleep_range?
Quoted 29 lines
drivers/i2c/busses/i2c-wmt.c new file +29
-0+ +return ret; +} +static int wmt_i2c_write (struct i2c_adapter *adap,struct i2c_msg *pmsg, +int restart,int last) +{ +struct wmt_i2c_dev *i2c_dev =i2c_get_adapdata (adap); +u16 val, tcr_val; +int ret, wait_result; +int xfer_len =0 ; + +if (!restart) { + ret =wmt_i2c_wait_bus_not_busy (i2c_dev); +if (ret <0 ) +return ret; + } + +if (pmsg->len ==0 ) { +/* +* We still need to run through the while (..) once, so +* start at -1 and break out early from the loop +*/ + xfer_len =-1 ; +writew (0 , i2c_dev->base +REG_CDR ); + }else { +writew (pmsg->buf[0 ] &0xFF , i2c_dev->base +REG_CDR ); + } + +if (!restart) {
drivers/i2c/busses/i2c-wmt.c line 159
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ val =readw (i2c_dev->base +REG_CR ); + val &= ~CR_TX_END ; +writew (val, i2c_dev->base +REG_CR ); + + val =readw (i2c_dev->base +REG_CR ); + val |=CR_CPU_RDY ; +writew (val, i2c_dev->base +REG_CR ); + } + +init_completion (&i2c_dev->complete);
INIT_COMPLETION (and init_completion in probe)
Quoted 25 lines
drivers/i2c/busses/i2c-wmt.c new file +25
-0+static int wmt_i2c_read (struct i2c_adapter *adap,struct i2c_msg *pmsg, +int restart,int last) +{ +struct wmt_i2c_dev *i2c_dev =i2c_get_adapdata (adap); +u16 val, tcr_val; +int ret, wait_result; +u32 xfer_len =0 ; + +if (!restart) { + ret =wmt_i2c_wait_bus_not_busy (i2c_dev); +if (ret <0 ) +return ret; + } + + val =readw (i2c_dev->base +REG_CR ); + val &= ~CR_TX_END ; +writew (val, i2c_dev->base +REG_CR ); + + val =readw (i2c_dev->base +REG_CR ); + val &= ~CR_TX_NEXT_NO_ACK ; +writew (val, i2c_dev->base +REG_CR ); + +if (!restart) { + val =readw (i2c_dev->base +REG_CR ); + val |=CR_CPU_RDY ;
drivers/i2c/busses/i2c-wmt.c line 248
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+writew (val, i2c_dev->base +REG_CR ); + } + +if (pmsg->len ==1 ) { + val =readw (i2c_dev->base +REG_CR ); + val |=CR_TX_NEXT_NO_ACK ; +writew (val, i2c_dev->base +REG_CR ); + } + +init_completion (&i2c_dev->complete);
ditto
Quoted 5 lines
drivers/i2c/busses/i2c-wmt.c new file +5
-0+static int wmt_i2c_xfer (struct i2c_adapter *adap, +struct i2c_msg msgs[], +int num) +{ +struct i2c_msg *pmsg;
drivers/i2c/busses/i2c-wmt.c line 307
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+int i, is_last, restart; +int ret =0 ; + +for (i =0 ; ret >=0 && i < num; i++) { + is_last = ((i +1 ) == num); + restart = (i !=0 ); + + pmsg = &msgs[i]; +if (pmsg->flags &I2C_M_NOSTART ) + restart =1 ;
NOSTART is not restart!? Either your variable is misnamed or check Documentation/i2c/i2c-protocol what it is about.
Quoted 17 lines
drivers/i2c/busses/i2c-wmt.c new file +17
-0+if (pmsg->flags &I2C_M_RD ) + ret =wmt_i2c_read (adap, pmsg, restart, is_last); +else + ret =wmt_i2c_write (adap, pmsg, restart, is_last); + } + +return (ret <0 ) ? ret : i; +} + +static int wmt_i2c_reset_hardware (struct wmt_i2c_dev *i2c_dev) +{ +int err; + + err =clk_prepare_enable (i2c_dev->clk); +if (err) { +dev_err (i2c_dev->dev,"failed to enable clock\n" ); +return err;
drivers/i2c/busses/i2c-wmt.c line 357
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ } + + err =clk_set_rate (i2c_dev->clk,20000000 ); +if (err) { +dev_err (i2c_dev->dev,"failed to set clock = 20Mhz\n" ); +return err; + } + +writew (0 , i2c_dev->base +REG_CR ); +writew (12 , i2c_dev->base +REG_MCR );
What are those magic values?
Quoted 17 lines
drivers/i2c/busses/i2c-wmt.c new file +17
-0+writew (ISR_WRITE_ALL , i2c_dev->base +REG_ISR ); +writew (IMR_ENABLE_ALL , i2c_dev->base +REG_IMR ); +writew (CR_ENABLE , i2c_dev->base +REG_CR ); +readw (i2c_dev->base +REG_CSR );/* read clear */ +writew (ISR_WRITE_ALL , i2c_dev->base +REG_ISR ); + +if (i2c_dev->mode ==I2C_MODE_STANDARD ) +writew (SCL_TIMEOUT (128 ) |TR_STD , i2c_dev->base +REG_TR ); +else +writew (SCL_TIMEOUT (128 ) |TR_HS , i2c_dev->base +REG_TR ); + +return 0 ; +} + +static int wmt_i2c_probe (struct platform_device *pdev) +{ +struct device_node *np = pdev->dev.of_node;
drivers/i2c/busses/i2c-wmt.c line 384
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+struct wmt_i2c_dev *i2c_dev; +struct i2c_adapter *adap; +struct resource *res; +int err; +u32 clk_rate; + +if (!np) { +dev_err (&pdev->dev,"device node not found\n" ); +return -ENODEV ; + }
This can't happen since we were matched by the driver core.
Quoted 33 lines
drivers/i2c/busses/i2c-wmt.c new file +33
-0+ + i2c_dev =devm_kzalloc (&pdev->dev,sizeof (*i2c_dev),GFP_KERNEL ); +if (!i2c_dev) { +dev_err (&pdev->dev,"device memory allocation failed\n" ); +return -ENOMEM ; + } + + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + i2c_dev->base =devm_ioremap_resource (&pdev->dev, res); +if (IS_ERR (i2c_dev->base)) +return PTR_ERR (i2c_dev->base); + + i2c_dev->irq =irq_of_parse_and_map (np,0 ); +if (!i2c_dev->irq) { +dev_err (&pdev->dev,"irq missing or invalid\n" ); +return -EINVAL ; + } + + i2c_dev->clk =of_clk_get (np,0 ); +if (IS_ERR (i2c_dev->clk)) { +dev_err (&pdev->dev,"unable to request clock\n" ); +return PTR_ERR (i2c_dev->clk); + } + + i2c_dev->mode =I2C_MODE_STANDARD ; + err =of_property_read_u32 (np,"clock-frequency" , &clk_rate); +if ((!err) && (clk_rate ==400000 )) + i2c_dev->mode =I2C_MODE_FAST ; + + i2c_dev->dev = &pdev->dev; + + err =devm_request_irq (&pdev->dev, i2c_dev->irq, wmt_i2c_isr,0 , +"i2c" , i2c_dev);
drivers/i2c/busses/i2c-wmt.c line 427
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+if (err) { +dev_err (&pdev->dev,"failed to request irq %i\n" , i2c_dev->irq); +return err; + } + + adap = &i2c_dev->adapter; +i2c_set_adapdata (adap, i2c_dev); +strlcpy (adap->name,"WMT I2C adapter" ,sizeof (adap->name)); + adap->owner =THIS_MODULE ; + adap->class =I2C_CLASS_HWMON ;
Don't use .class unless you have an explicit reason to do so. It may cost boot-time.
Quoted 5 lines
drivers/i2c/busses/i2c-wmt.c new file +5
-0+ adap->algo = &wmt_i2c_algo; + adap->dev.parent = &pdev->dev; + adap->dev.of_node = pdev->dev.of_node; + adap->nr =of_alias_get_id (pdev->dev.of_node,"i2c" ); +
drivers/i2c/busses/i2c-wmt.c line 442
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ err =wmt_i2c_reset_hardware (i2c_dev); +if (err) { +dev_err (&pdev->dev,"error initializing hardware\n" ); +return err; + } + +if (adap->nr <0 ) + err =i2c_add_adapter (adap); +else + err =i2c_add_numbered_adapter (adap);
add_numbered_adapter does handle this case internally.
Quoted 35 lines
drivers/i2c/busses/i2c-wmt.c new file +35
-0+ +if (err) { +dev_err (&pdev->dev,"failed to add adapter\n" ); +return err; + } + +platform_set_drvdata (pdev, i2c_dev); + +of_i2c_register_devices (adap); + +return 0 ; +} + +static int wmt_i2c_remove (struct platform_device *pdev) +{ +struct wmt_i2c_dev *i2c_dev =platform_get_drvdata (pdev); + +/* Disable interrupts, clock and delete adapter */ +writew (0 , i2c_dev->base +REG_IMR ); +clk_disable_unprepare (i2c_dev->clk); +i2c_del_adapter (&i2c_dev->adapter); + +return 0 ; +} + +static struct of_device_id wmt_i2c_dt_ids[] = { + { .compatible ="wm,wm8505-i2c" }, + {/* Sentinel */ }, +}; + +static struct platform_driver wmt_i2c_driver = { + .probe = wmt_i2c_probe, + .remove = wmt_i2c_remove, + .driver = { + .name ="wmt-i2c" ,
drivers/i2c/busses/i2c-wmt.c line 487
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ .owner = THIS_MODULE, + .of_match_table = wmt_i2c_dt_ids, + }, +}; + +module_platform_driver (wmt_i2c_driver); + +MODULE_DESCRIPTION ("Wondermedia I2C master-mode bus adapter" ); +MODULE_AUTHOR ("Tony Prisk <[email protected]>" ); +MODULE_LICENSE ("GPL v2" );
Header says GPLv2+
Quoted 1 line
+MODULE_DEVICE_TABLE(of,
wmt_i2c_dt_ids);
Thanks,
Wolfram
Quoted 11 lines · Wolfram Sang
Hi Tony,
Quoted 6 lines
drivers/i2c/busses/i2c-wmt.c new file +1
-0+static int wmt_i2c_wait_bus_not_busy (struct wmt_i2c_dev *i2c_dev)drivers/i2c/busses/i2c-wmt.c new file +0
-0...
Quoted 16 lines
Quoted 4 lines
drivers/i2c/busses/i2c-wmt.c new file +4
-0+{ +u16 val; +int i; +int ret =0 ;drivers/i2c/busses/i2c-wmt.c line 106
Quoted 10 lines
drivers/i2c/busses/i2c-wmt.c new file +10
-0+ +for (i =0 ; i <100000 ; i++) { + val =readw (i2c_dev->base +REG_CSR ); +if (val &CSR_READY_MASK ) +break ; + +udelay (1 ); + } +if (i >=9999999 ) + ret = -EBUSY ;What about using
time_afterandusleep_range?
And in any case this is not the correct way to check for success or failure.
Failure is defined by readw(i2c_dev->base + REG_CSR) & CSR_READY_MASK being false. The above does not check it after the final 1us delay. You might as well not wait for that final 1us because it's literally just wasting time the way you've coded it.
Or fix it to re-check for success after the loop completes.