forked from Minki/linux
52caa59ed3
Pull i2c updates from Wolfram Sang: "Highlights: - new drivers for Intel ismt & Broadcom bcm2835 - a number of drivers got support for more variants and mostly got cleaned up on the way (sis630, i801, at91, tegra, designware) - i2c got rid of all *_set_drvdata(..., NULL) on remove/probe failure - removed the i2c_smbus_process_call from the core since there are no users - mxs can now switch between PIO and DMA depending on the message size and the bus speed can now be arbitrary In addition, there is the usual bunch of fixes, cleanups, devm_* conversions, etc" Fixed conflict (and buggy devm_* conversion) in i2c-s3c2410.c * 'i2c/for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: (39 commits) i2c: Remove unneeded xxx_set_drvdata(..., NULL) calls i2c: pxa: remove incorrect __exit annotations i2c: ocores: Fix pointer to integer cast warning i2c: tegra: remove warning dump if timeout happen in transfer i2c: fix i2c-ismt.c printk format warning i2c: i801: Add Device IDs for Intel Wellsburg PCH i2c: add bcm2835 driver i2c: ismt: Add Seth and Myself as maintainers i2c: sis630: checkpatch cleanup i2c: sis630: display unsigned hex i2c: sis630: use hex to constants for SMBus commands i2c: sis630: fix behavior after collision i2c: sis630: clear sticky bits i2c: sis630: Add SIS964 support i2c: isch: Add module parameter for backbone clock rate if divider is unset i2c: at91: fix unsed variable warning when building with !CONFIG_OF i2c: Adding support for Intel iSMT SMBus 2.0 host controller i2c: sh_mobile: don't send a stop condition by default inside transfers i2c: sh_mobile: eliminate an open-coded "goto" loop i2c: sh_mobile: fix timeout error handling ...
277 lines
6.8 KiB
C
277 lines
6.8 KiB
C
/*
|
|
* Copyright 2011, Netlogic Microsystems Inc.
|
|
* Copyright 2004, Matt Porter <mporter@kernel.crashing.org>
|
|
*
|
|
* This file is licensed under the terms of the GNU General Public
|
|
* License version 2. This program is licensed "as is" without any
|
|
* warranty of any kind, whether express or implied.
|
|
*/
|
|
|
|
#include <linux/err.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/init.h>
|
|
#include <linux/ioport.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/io.h>
|
|
#include <linux/platform_device.h>
|
|
|
|
/* XLR I2C REGISTERS */
|
|
#define XLR_I2C_CFG 0x00
|
|
#define XLR_I2C_CLKDIV 0x01
|
|
#define XLR_I2C_DEVADDR 0x02
|
|
#define XLR_I2C_ADDR 0x03
|
|
#define XLR_I2C_DATAOUT 0x04
|
|
#define XLR_I2C_DATAIN 0x05
|
|
#define XLR_I2C_STATUS 0x06
|
|
#define XLR_I2C_STARTXFR 0x07
|
|
#define XLR_I2C_BYTECNT 0x08
|
|
#define XLR_I2C_HDSTATIM 0x09
|
|
|
|
/* XLR I2C REGISTERS FLAGS */
|
|
#define XLR_I2C_BUS_BUSY 0x01
|
|
#define XLR_I2C_SDOEMPTY 0x02
|
|
#define XLR_I2C_RXRDY 0x04
|
|
#define XLR_I2C_ACK_ERR 0x08
|
|
#define XLR_I2C_ARB_STARTERR 0x30
|
|
|
|
/* Register Values */
|
|
#define XLR_I2C_CFG_ADDR 0xF8
|
|
#define XLR_I2C_CFG_NOADDR 0xFA
|
|
#define XLR_I2C_STARTXFR_ND 0x02 /* No Data */
|
|
#define XLR_I2C_STARTXFR_RD 0x01 /* Read */
|
|
#define XLR_I2C_STARTXFR_WR 0x00 /* Write */
|
|
|
|
#define XLR_I2C_TIMEOUT 10 /* timeout per byte in msec */
|
|
|
|
/*
|
|
* On XLR/XLS, we need to use __raw_ IO to read the I2C registers
|
|
* because they are in the big-endian MMIO area on the SoC.
|
|
*
|
|
* The readl/writel implementation on XLR/XLS byteswaps, because
|
|
* those are for its little-endian PCI space (see arch/mips/Kconfig).
|
|
*/
|
|
static inline void xlr_i2c_wreg(u32 __iomem *base, unsigned int reg, u32 val)
|
|
{
|
|
__raw_writel(val, base + reg);
|
|
}
|
|
|
|
static inline u32 xlr_i2c_rdreg(u32 __iomem *base, unsigned int reg)
|
|
{
|
|
return __raw_readl(base + reg);
|
|
}
|
|
|
|
struct xlr_i2c_private {
|
|
struct i2c_adapter adap;
|
|
u32 __iomem *iobase;
|
|
};
|
|
|
|
static int xlr_i2c_tx(struct xlr_i2c_private *priv, u16 len,
|
|
u8 *buf, u16 addr)
|
|
{
|
|
struct i2c_adapter *adap = &priv->adap;
|
|
unsigned long timeout, stoptime, checktime;
|
|
u32 i2c_status;
|
|
int pos, timedout;
|
|
u8 offset, byte;
|
|
|
|
offset = buf[0];
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_ADDR, offset);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, XLR_I2C_CFG_ADDR);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len - 1);
|
|
|
|
timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
|
|
stoptime = jiffies + timeout;
|
|
timedout = 0;
|
|
pos = 1;
|
|
retry:
|
|
if (len == 1) {
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR,
|
|
XLR_I2C_STARTXFR_ND);
|
|
} else {
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, buf[pos]);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR,
|
|
XLR_I2C_STARTXFR_WR);
|
|
}
|
|
|
|
while (!timedout) {
|
|
checktime = jiffies;
|
|
i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
|
|
|
|
if (i2c_status & XLR_I2C_SDOEMPTY) {
|
|
pos++;
|
|
/* need to do a empty dataout after the last byte */
|
|
byte = (pos < len) ? buf[pos] : 0;
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_DATAOUT, byte);
|
|
|
|
/* reset timeout on successful xmit */
|
|
stoptime = jiffies + timeout;
|
|
}
|
|
timedout = time_after(checktime, stoptime);
|
|
|
|
if (i2c_status & XLR_I2C_ARB_STARTERR) {
|
|
if (timedout)
|
|
break;
|
|
goto retry;
|
|
}
|
|
|
|
if (i2c_status & XLR_I2C_ACK_ERR)
|
|
return -EIO;
|
|
|
|
if ((i2c_status & XLR_I2C_BUS_BUSY) == 0 && pos >= len)
|
|
return 0;
|
|
}
|
|
dev_err(&adap->dev, "I2C transmit timeout\n");
|
|
return -ETIMEDOUT;
|
|
}
|
|
|
|
static int xlr_i2c_rx(struct xlr_i2c_private *priv, u16 len, u8 *buf, u16 addr)
|
|
{
|
|
struct i2c_adapter *adap = &priv->adap;
|
|
u32 i2c_status;
|
|
unsigned long timeout, stoptime, checktime;
|
|
int nbytes, timedout;
|
|
u8 byte;
|
|
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_CFG, XLR_I2C_CFG_NOADDR);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_BYTECNT, len);
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_DEVADDR, addr);
|
|
|
|
timeout = msecs_to_jiffies(XLR_I2C_TIMEOUT);
|
|
stoptime = jiffies + timeout;
|
|
timedout = 0;
|
|
nbytes = 0;
|
|
retry:
|
|
xlr_i2c_wreg(priv->iobase, XLR_I2C_STARTXFR, XLR_I2C_STARTXFR_RD);
|
|
|
|
while (!timedout) {
|
|
checktime = jiffies;
|
|
i2c_status = xlr_i2c_rdreg(priv->iobase, XLR_I2C_STATUS);
|
|
if (i2c_status & XLR_I2C_RXRDY) {
|
|
if (nbytes > len)
|
|
return -EIO; /* should not happen */
|
|
|
|
/* we need to do a dummy datain when nbytes == len */
|
|
byte = xlr_i2c_rdreg(priv->iobase, XLR_I2C_DATAIN);
|
|
if (nbytes < len)
|
|
buf[nbytes] = byte;
|
|
nbytes++;
|
|
|
|
/* reset timeout on successful read */
|
|
stoptime = jiffies + timeout;
|
|
}
|
|
|
|
timedout = time_after(checktime, stoptime);
|
|
if (i2c_status & XLR_I2C_ARB_STARTERR) {
|
|
if (timedout)
|
|
break;
|
|
goto retry;
|
|
}
|
|
|
|
if (i2c_status & XLR_I2C_ACK_ERR)
|
|
return -EIO;
|
|
|
|
if ((i2c_status & XLR_I2C_BUS_BUSY) == 0)
|
|
return 0;
|
|
}
|
|
|
|
dev_err(&adap->dev, "I2C receive timeout\n");
|
|
return -ETIMEDOUT;
|
|
}
|
|
|
|
static int xlr_i2c_xfer(struct i2c_adapter *adap,
|
|
struct i2c_msg *msgs, int num)
|
|
{
|
|
struct i2c_msg *msg;
|
|
int i;
|
|
int ret = 0;
|
|
struct xlr_i2c_private *priv = i2c_get_adapdata(adap);
|
|
|
|
for (i = 0; ret == 0 && i < num; i++) {
|
|
msg = &msgs[i];
|
|
if (msg->flags & I2C_M_RD)
|
|
ret = xlr_i2c_rx(priv, msg->len, &msg->buf[0],
|
|
msg->addr);
|
|
else
|
|
ret = xlr_i2c_tx(priv, msg->len, &msg->buf[0],
|
|
msg->addr);
|
|
}
|
|
|
|
return (ret != 0) ? ret : num;
|
|
}
|
|
|
|
static u32 xlr_func(struct i2c_adapter *adap)
|
|
{
|
|
/* Emulate SMBUS over I2C */
|
|
return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
|
|
}
|
|
|
|
static struct i2c_algorithm xlr_i2c_algo = {
|
|
.master_xfer = xlr_i2c_xfer,
|
|
.functionality = xlr_func,
|
|
};
|
|
|
|
static int xlr_i2c_probe(struct platform_device *pdev)
|
|
{
|
|
struct xlr_i2c_private *priv;
|
|
struct resource *res;
|
|
int ret;
|
|
|
|
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
|
|
if (!priv)
|
|
return -ENOMEM;
|
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
priv->iobase = devm_ioremap_resource(&pdev->dev, res);
|
|
if (IS_ERR(priv->iobase))
|
|
return PTR_ERR(priv->iobase);
|
|
|
|
priv->adap.dev.parent = &pdev->dev;
|
|
priv->adap.owner = THIS_MODULE;
|
|
priv->adap.algo_data = priv;
|
|
priv->adap.algo = &xlr_i2c_algo;
|
|
priv->adap.nr = pdev->id;
|
|
priv->adap.class = I2C_CLASS_HWMON;
|
|
snprintf(priv->adap.name, sizeof(priv->adap.name), "xlr-i2c");
|
|
|
|
i2c_set_adapdata(&priv->adap, priv);
|
|
ret = i2c_add_numbered_adapter(&priv->adap);
|
|
if (ret < 0) {
|
|
dev_err(&priv->adap.dev, "Failed to add i2c bus.\n");
|
|
return ret;
|
|
}
|
|
|
|
platform_set_drvdata(pdev, priv);
|
|
dev_info(&priv->adap.dev, "Added I2C Bus.\n");
|
|
return 0;
|
|
}
|
|
|
|
static int xlr_i2c_remove(struct platform_device *pdev)
|
|
{
|
|
struct xlr_i2c_private *priv;
|
|
|
|
priv = platform_get_drvdata(pdev);
|
|
i2c_del_adapter(&priv->adap);
|
|
return 0;
|
|
}
|
|
|
|
static struct platform_driver xlr_i2c_driver = {
|
|
.probe = xlr_i2c_probe,
|
|
.remove = xlr_i2c_remove,
|
|
.driver = {
|
|
.name = "xlr-i2cbus",
|
|
.owner = THIS_MODULE,
|
|
},
|
|
};
|
|
|
|
module_platform_driver(xlr_i2c_driver);
|
|
|
|
MODULE_AUTHOR("Ganesan Ramalingam <ganesanr@netlogicmicro.com>");
|
|
MODULE_DESCRIPTION("XLR/XLS SoC I2C Controller driver");
|
|
MODULE_LICENSE("GPL v2");
|
|
MODULE_ALIAS("platform:xlr-i2cbus");
|