Re: [PATCH 2/3] i2c: Add Congatec CGEB I2C driver

From: Christian Gmeiner
Date: Tue Feb 07 2012 - 03:38:44 EST


Christian Gmeiner, MSc



2012/2/1 Sascha Hauer <s.hauer@xxxxxxxxxxxxxx>:
> This driver provides a I2C bus driver for the CGEB interface
> found on some Congatec x86 modules. No devices are registered
> on the bus, the user has to do this via the i2c device /sys
> interface.
>
> Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx>
> ---
> Âdrivers/i2c/busses/Kconfig       |  Â7 +
> Âdrivers/i2c/busses/Makefile      Â|  Â1 +
> Âdrivers/i2c/busses/i2c-congatec-cgeb.c | Â206 ++++++++++++++++++++++++++++++++
> Â3 files changed, 214 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/i2c/busses/i2c-congatec-cgeb.c
>
> diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
> index a3afac4..cc8eb22 100644
> --- a/drivers/i2c/busses/Kconfig
> +++ b/drivers/i2c/busses/Kconfig
> @@ -696,6 +696,13 @@ config I2C_EG20T
> Â Â Â Â ÂML7213/ML7223 is companion chip for Intel Atom E6xx series.
> Â Â Â Â ÂML7213/ML7223 is completely compatible for Intel EG20T PCH.
>
> +config I2C_CONGATEC_CGEB
> + Â Â Â tristate "Congatec CGEB I2C driver"
> + Â Â Â depends on MFD_CONGATEC_CGEB
> + Â Â Â help
> + Â Â Â Â This driver provides support for the I2C busses accssable via
> + Â Â Â Â the Congatec CGEB interface found on Congatec boards.
> +
> Âcomment "External I2C/SMBus adapter drivers"
>
> Âconfig I2C_DIOLAN_U2C
> diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
> index fba6da6..4aab659 100644
> --- a/drivers/i2c/busses/Makefile
> +++ b/drivers/i2c/busses/Makefile
> @@ -69,6 +69,7 @@ obj-$(CONFIG_I2C_VERSATILE) Â += i2c-versatile.o
> Âobj-$(CONFIG_I2C_OCTEON) Â Â Â += i2c-octeon.o
> Âobj-$(CONFIG_I2C_XILINX) Â Â Â += i2c-xiic.o
> Âobj-$(CONFIG_I2C_EG20T) Â Â Â Â += i2c-eg20t.o
> +obj-$(CONFIG_I2C_CONGATEC_CGEB) Â Â Â Â+= i2c-congatec-cgeb.o
>
> Â# External I2C/SMBus adapter drivers
> Âobj-$(CONFIG_I2C_DIOLAN_U2C) Â += i2c-diolan-u2c.o
> diff --git a/drivers/i2c/busses/i2c-congatec-cgeb.c b/drivers/i2c/busses/i2c-congatec-cgeb.c
> new file mode 100644
> index 0000000..a387662
> --- /dev/null
> +++ b/drivers/i2c/busses/i2c-congatec-cgeb.c
> @@ -0,0 +1,206 @@
> +/*
> + * CGEB i2c driver
> + *
> + * (c) 2011 Sascha Hauer, Pengutronix
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; version 2 of the License.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ÂSee the
> + * GNU General Public License for more details.
> + */
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +#include <linux/i2c.h>
> +#include <linux/mfd/congatec-cgeb.h>
> +
> +#define CG_I2C_FLAG_START Â 0x00080 Â Â/* send START condition */
> +#define CG_I2C_FLAG_STOP Â Â0x00040 Â Â/* send STOP condition */
> +#define CG_I2C_FLAG_ALL_ACK 0x08000 Â Â/* send ACK on all read bytes */
> +#define CG_I2C_FLAG_ALL_NAK 0x04000 Â Â/* send NAK on all read bytes */
> +
> +struct cgeb_i2c_priv {
> +    struct cgeb_board_data     Â*board;
> +    struct i2c_adapter   Âadapter;
> + Â Â Â int unit;
> +};
> +
> +static u32 cgeb_i2c_func(struct i2c_adapter *adapter)
> +{
> + Â Â Â return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
> +}
> +
> +static int cgeb_i2c_set_speed(struct cgeb_i2c_priv *priv, int speed)
> +{
> + Â Â Â struct cgeb_function_parameters fps;
> +
> + Â Â Â memset(&fps, 0, sizeof(fps));
> +
> + Â Â Â fps.unit = priv->unit;
> + Â Â Â fps.pars[0] = speed;
> +
> + Â Â Â return cgeb_call(priv->board, &fps, CgebI2CSetFrequency);
> +}
> +
> +static int cgeb_i2c_xfer(struct i2c_adapter *adapter,
> + Â Â Â Â Â Â Â struct i2c_msg *msgs, int num)
> +{
> + Â Â Â struct cgeb_function_parameters fps;
> + Â Â Â int i, ret;
> + Â Â Â unsigned long flags = CG_I2C_FLAG_START;
> + Â Â Â struct cgeb_i2c_priv *priv = i2c_get_adapdata(adapter);
> + Â Â Â unsigned long rdlen, wrlen;
> + Â Â Â unsigned char *rdbuf, *wrbuf, *raw_wrbuf;
> + Â Â Â unsigned short lmax = 0;
> +
> + Â Â Â /*
> + Â Â Â Â* With cgeb the I2C address is part of the write data
> + Â Â Â Â* buffer, so allocate a buffer with the length of the
> + Â Â Â Â* longest write buffer + 1
> + Â Â Â Â*/
> + Â Â Â for (i = 0; i < num; i++)
> + Â Â Â Â Â Â Â if (!(msgs[i].flags & I2C_M_RD))
> + Â Â Â Â Â Â Â Â Â Â Â lmax = max(lmax, msgs[i].len);
> +
> + Â Â Â raw_wrbuf = kmalloc(lmax + 1, GFP_KERNEL);
> + Â Â Â if (!raw_wrbuf)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â for (i = 0; i < num; i++) {
> +
> + Â Â Â Â Â Â Â if (msgs[i].flags & I2C_M_RD) {
> + Â Â Â Â Â Â Â Â Â Â Â rdbuf = msgs[i].buf;
> + Â Â Â Â Â Â Â Â Â Â Â rdlen = msgs[i].len;
> + Â Â Â Â Â Â Â Â Â Â Â wrbuf = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â wrlen = 0;
> + Â Â Â Â Â Â Â } else {
> + Â Â Â Â Â Â Â Â Â Â Â rdbuf = NULL;
> + Â Â Â Â Â Â Â Â Â Â Â rdlen = 0;
> + Â Â Â Â Â Â Â Â Â Â Â wrbuf = msgs[i].buf;
> + Â Â Â Â Â Â Â Â Â Â Â wrlen = msgs[i].len;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â raw_wrbuf[0] = msgs[i].addr << 1;
> + Â Â Â Â Â Â Â if (wrlen)
> + Â Â Â Â Â Â Â Â Â Â Â memcpy(&raw_wrbuf[1], wrbuf, wrlen);
> +
> + Â Â Â Â Â Â Â if (msgs[i].flags & I2C_M_RD)
> + Â Â Â Â Â Â Â Â Â Â Â raw_wrbuf[0] |= 1;
> +
> + Â Â Â Â Â Â Â if (i == num - 1)
> + Â Â Â Â Â Â Â Â Â Â Â flags |= CG_I2C_FLAG_STOP;
> +
> + Â Â Â Â Â Â Â dev_dbg(&adapter->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "%s: rd: %p/%ld wr: %p/%ld flags: 0x%08lx %s\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __func__, rdbuf, rdlen, raw_wrbuf, wrlen + 1,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â flags,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â msgs[i].flags & I2C_M_RD ? "READ" : "WRITE");
> +
> + Â Â Â Â Â Â Â memset(&fps, 0, sizeof(fps));
> +
> + Â Â Â Â Â Â Â fps.unit = priv->unit;
> + Â Â Â Â Â Â Â fps.pars[0] = wrlen + 1;
> + Â Â Â Â Â Â Â fps.pars[1] = rdlen;
> + Â Â Â Â Â Â Â fps.pars[2] = flags;
> + Â Â Â Â Â Â Â fps.iptr = raw_wrbuf;
> + Â Â Â Â Â Â Â fps.optr = rdbuf;
> +
> + Â Â Â Â Â Â Â ret = cgeb_call(priv->board, &fps, CgebI2CTransfer);
> + Â Â Â Â Â Â Â if (ret) {
> + Â Â Â Â Â Â Â Â Â Â Â ret = -EREMOTEIO;
> + Â Â Â Â Â Â Â Â Â Â Â goto out;
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +
> + Â Â Â ret = num;
> +
> +out:
> + Â Â Â kfree(raw_wrbuf);
> +
> + Â Â Â return ret;
> +
> +}
> +
> +static struct i2c_algorithm cgeb_i2c_algo = {
> +    .master_xfer  Â= cgeb_i2c_xfer,
> + Â Â Â .functionality Â= cgeb_i2c_func,
> +};
> +
> +static int __devinit cgeb_i2c_probe(struct platform_device *pdev)
> +{
> + Â Â Â struct cgeb_i2c_priv *priv;
> + Â Â Â struct cgeb_pdata *pdata = pdev->dev.platform_data;
> + Â Â Â int ret;
> +
> + Â Â Â priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> + Â Â Â if (!priv)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â strcpy(priv->adapter.name, pdev->name);
> +    priv->adapter.owner       = THIS_MODULE;
> +    priv->adapter.algo       Â= &cgeb_i2c_algo;
> +    priv->adapter.dev.parent    Â= &pdev->dev;
> + Â Â Â priv->unit = pdata->unit;
> + Â Â Â priv->board = pdata->board;
> + Â Â Â i2c_set_adapdata(&priv->adapter, priv);
> +
> + Â Â Â platform_set_drvdata(pdev, priv);
> +
> + Â Â Â ret = cgeb_i2c_set_speed(priv, 400000);
> + Â Â Â if (ret)
> + Â Â Â Â Â Â Â /*
> + Â Â Â Â Â Â Â Â* not a critical error, we can continue with the default speed.
> + Â Â Â Â Â Â Â Â*/
> + Â Â Â Â Â Â Â dev_warn(&pdev->dev, "Could not set speed to 400KHz\n");
> +
> + Â Â Â ret = i2c_add_adapter(&priv->adapter);
> + Â Â Â if (ret < 0) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "registration failed\n");
> + Â Â Â Â Â Â Â return ret;
> + Â Â Â }
> +
> + Â Â Â dev_info(&pdev->dev, "registered\n");
> +
> + Â Â Â return 0;
> +};
> +
> +static int __devexit cgeb_i2c_remove(struct platform_device *pdev)
> +{
> + Â Â Â struct cgeb_i2c_priv *priv = platform_get_drvdata(pdev);
> +
> + Â Â Â i2c_del_adapter(&priv->adapter);
> +
> + Â Â Â platform_set_drvdata(pdev, NULL);
> +
> + Â Â Â return 0;
> +}
> +
> +static struct platform_driver cgeb_i2c_driver = {
> +    .probe     Â= cgeb_i2c_probe,
> +    .remove     = __exit_p(cgeb_i2c_remove),
> + Â Â Â .driver = {
> +        .name  = "cgeb-i2c",
> + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
> + Â Â Â },
> +};
> +
> +static int __init cgeb_i2c_driver_init(void)
> +{
> + Â Â Â return platform_driver_register(&cgeb_i2c_driver);
> +}
> +
> +static void __exit cgeb_i2c_driver_exit(void)
> +{
> + Â Â Â platform_driver_unregister(&cgeb_i2c_driver);
> +}
> +
> +module_init(cgeb_i2c_driver_init);
> +module_exit(cgeb_i2c_driver_exit);
> +
> +MODULE_AUTHOR("Sascha Hauer <s.hauer@xxxxxxxxxxxxxx>");
> +MODULE_DESCRIPTION("cgeb i2c driver");
> +MODULE_LICENSE("GPL");
> --
> 1.7.2.5
>

Patch looks good and runs nicely with some i2c devices (eeprom, io
expander, temp sensors,...)

0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: UU UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
50: UU UU -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Tested-by: Christian Gmeiner <christian.gmeiner@xxxxxxxxx>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/