diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c index fa02a1aa9f4e79..3162bfc0a9f8b0 100644 --- a/arch/arm/mach-bcm2708/bcm2708.c +++ b/arch/arm/mach-bcm2708/bcm2708.c @@ -103,6 +103,11 @@ static struct map_desc bcm2708_io_desc[] __initdata = { .length = SZ_4K, .type = MT_DEVICE}, #endif + { + .virtual = IO_ADDRESS(I2C0_BASE), + .pfn = __phys_to_pfn(I2C0_BASE), + .length = SZ_4K, + .type = MT_DEVICE}, { .virtual = IO_ADDRESS(DMA_BASE), .pfn = __phys_to_pfn(DMA_BASE), @@ -304,6 +309,26 @@ static struct platform_device bcm2708_uart1_device = { }, }; +static struct resource bcm2708_i2c0_resources[] = { + { + .start = I2C0_BASE, + .end = I2C0_BASE + SZ_4K - 1, + .flags = IORESOURCE_MEM, + }, + { + .start = IRQ_I2C, + .end = IRQ_I2C, + .flags = IORESOURCE_IRQ, + } +}; + +static struct platform_device bcm2708_i2c0_device = { + .name = "i2c-bcm2708", + .id = 0, + .resource = bcm2708_i2c0_resources, + .num_resources = ARRAY_SIZE(bcm2708_i2c0_resources), +}; + static struct resource bcm2708_usb_resources[] = { [0] = { .start = USB_BASE, @@ -491,6 +516,7 @@ void __init bcm2708_init(void) bcm_register_device(&bcm2708_fb_device); bcm_register_device(&bcm2708_usb_device); bcm_register_device(&bcm2708_uart1_device); + bcm_register_device(&bcm2708_i2c0_device); bcm_register_device(&bcm2708_powerman_device); #ifdef CONFIG_MMC_SDHCI_BCM2708 bcm_register_device(&bcm2708_emmc_device); diff --git a/arch/arm/mach-bcm2708/bcm2708_gpio.c b/arch/arm/mach-bcm2708/bcm2708_gpio.c index 59df912fc84d70..0bead8c85352ba 100644 --- a/arch/arm/mach-bcm2708/bcm2708_gpio.c +++ b/arch/arm/mach-bcm2708/bcm2708_gpio.c @@ -273,6 +273,10 @@ static int bcm2708_gpio_probe(struct platform_device *dev) ucb->gc.set = bcm2708_gpio_set; ucb->gc.can_sleep = 0; + /* Hack: Set GPIOs 0/1 to ALT0 (I2C0) */ + bcm2708_set_function(&ucb->gc, 0, GPIO_FSEL_ALT0); + bcm2708_set_function(&ucb->gc, 1, GPIO_FSEL_ALT0); + err = gpiochip_add(&ucb->gc); if (err) goto err; diff --git a/arch/arm/mach-bcm2708/include/mach/platform.h b/arch/arm/mach-bcm2708/include/mach/platform.h index 5cb1caa8a17227..2e1fcf962f4f32 100644 --- a/arch/arm/mach-bcm2708/include/mach/platform.h +++ b/arch/arm/mach-bcm2708/include/mach/platform.h @@ -63,6 +63,7 @@ #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */ #define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */ #define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */ +#define I2C0_BASE (BCM2708_PERI_BASE + 0x205000) #define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */ #define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */ #define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */ diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig index 646068e5100bac..9783ac21f2625e 100644 --- a/drivers/i2c/busses/Kconfig +++ b/drivers/i2c/busses/Kconfig @@ -687,6 +687,18 @@ config I2C_EG20T ML7213/ML7223 is companion chip for Intel Atom E6xx series. ML7213/ML7223 is completely compatible for Intel EG20T PCH. +config I2C_BCM2708 + tristate "Broadcom BCM2708 I2C controller" + depends on ARCH_BCM2708 + help + If you say yes to this option, support will be included for the + BCM2708 I2C controller. + + If you don't know what to do here, say N. + + This support is also available as a module. If so, the module + will be called i2c-bcm2708. + comment "External I2C/SMBus adapter drivers" config I2C_DIOLAN_U2C diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile index e6cf294d37293b..92b1f5b0d2c463 100644 --- a/drivers/i2c/busses/Makefile +++ b/drivers/i2c/busses/Makefile @@ -66,6 +66,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_BCM2708) += i2c-bcm2708.o # External I2C/SMBus adapter drivers obj-$(CONFIG_I2C_DIOLAN_U2C) += i2c-diolan-u2c.o diff --git a/drivers/i2c/busses/i2c-bcm2708.c b/drivers/i2c/busses/i2c-bcm2708.c new file mode 100644 index 00000000000000..c62354d3e0ace8 --- /dev/null +++ b/drivers/i2c/busses/i2c-bcm2708.c @@ -0,0 +1,327 @@ +/* + * BCM2708 master mode driver + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#define BCM2708_I2C_C 0x0 +#define BCM2708_I2C_S 0x4 +#define BCM2708_I2C_DLEN 0x8 +#define BCM2708_I2C_A 0xc +#define BCM2708_I2C_FIFO 0x10 +#define BCM2708_I2C_DIV 0x14 +#define BCM2708_I2C_DEL 0x18 +#define BCM2708_I2C_CLKT 0x1c + +#define BCM2708_I2C_C_READ BIT(0) +#define BCM2708_I2C_C_CLEAR BIT(4) /* bits 4 and 5 both clear */ +#define BCM2708_I2C_C_ST BIT(7) +#define BCM2708_I2C_C_INTD BIT(8) +#define BCM2708_I2C_C_INTT BIT(9) +#define BCM2708_I2C_C_INTR BIT(10) +#define BCM2708_I2C_C_I2CEN BIT(15) + +#define BCM2708_I2C_S_TA BIT(0) +#define BCM2708_I2C_S_DONE BIT(1) +#define BCM2708_I2C_S_TXW BIT(2) +#define BCM2708_I2C_S_RXR BIT(3) +#define BCM2708_I2C_S_TXD BIT(4) +#define BCM2708_I2C_S_RXD BIT(5) +#define BCM2708_I2C_S_TXE BIT(6) +#define BCM2708_I2C_S_RXF BIT(7) +#define BCM2708_I2C_S_ERR BIT(8) +#define BCM2708_I2C_S_CLKT BIT(9) +#define BCM2708_I2C_S_LEN BIT(10) /* Fake bit for SW error reporting */ + +#define BCM2708_I2C_TIMEOUT (msecs_to_jiffies(1000)) + +struct bcm2708_i2c_dev { + struct device *dev; + void __iomem *regs; + struct i2c_adapter adapter; + struct completion completion; + u32 msg_err; + u8 *msg_buf; + size_t msg_buf_remaining; +}; + +static inline void bcm2708_i2c_writel(struct bcm2708_i2c_dev *i2c_dev, + u32 reg, u32 val) +{ + writel(val, i2c_dev->regs + reg); +} + +static inline u32 bcm2708_i2c_readl(struct bcm2708_i2c_dev *i2c_dev, u32 reg) +{ + return __raw_readw(i2c_dev->regs + reg); +} + +static void bcm2708_fill_txfifo(struct bcm2708_i2c_dev *i2c_dev) +{ + u32 val; + + for (;;) { + if (!i2c_dev->msg_buf_remaining) + return; + val = bcm2708_i2c_readl(i2c_dev, BCM2708_I2C_S); + if (!(val & BCM2708_I2C_S_TXD)) + break; + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_FIFO, + *i2c_dev->msg_buf); + i2c_dev->msg_buf++; + i2c_dev->msg_buf_remaining--; + } +} + +static void bcm2708_drain_rxfifo(struct bcm2708_i2c_dev *i2c_dev) +{ + u32 val; + + for (;;) { + if (!i2c_dev->msg_buf_remaining) + return; + val = bcm2708_i2c_readl(i2c_dev, BCM2708_I2C_S); + if (!(val & BCM2708_I2C_S_RXD)) + break; + *i2c_dev->msg_buf = bcm2708_i2c_readl(i2c_dev, + BCM2708_I2C_FIFO); + i2c_dev->msg_buf++; + i2c_dev->msg_buf_remaining--; + } +} + +static irqreturn_t bcm2708_i2c_isr(int this_irq, void *data) +{ + struct bcm2708_i2c_dev *i2c_dev = data; + u32 val, err; + + val = bcm2708_i2c_readl(i2c_dev, BCM2708_I2C_S); + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_S, val); + + err = val & (BCM2708_I2C_S_CLKT | BCM2708_I2C_S_ERR); + if (err) { + i2c_dev->msg_err = err; + complete(&i2c_dev->completion); + return IRQ_HANDLED; + } + + if (val & BCM2708_I2C_S_RXD) { + bcm2708_drain_rxfifo(i2c_dev); + if (!(val & BCM2708_I2C_S_DONE)) + return IRQ_HANDLED; + } + + if (val & BCM2708_I2C_S_DONE) { + if (i2c_dev->msg_buf_remaining) + i2c_dev->msg_err = BCM2708_I2C_S_LEN; + else + i2c_dev->msg_err = 0; + complete(&i2c_dev->completion); + return IRQ_HANDLED; + } + + if (val & BCM2708_I2C_S_TXD) { + bcm2708_fill_txfifo(i2c_dev); + return IRQ_NONE; + } + + return IRQ_NONE; +} + +static int bcm2708_i2c_xfer_msg(struct bcm2708_i2c_dev *i2c_dev, + struct i2c_msg *msg) +{ + u32 c; + int ret; + + if (msg->len == 0) + return -EINVAL; + + i2c_dev->msg_buf = msg->buf; + i2c_dev->msg_buf_remaining = msg->len; + INIT_COMPLETION(i2c_dev->completion); + + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_C, BCM2708_I2C_C_CLEAR); + + if (msg->flags & I2C_M_RD) { + c = BCM2708_I2C_C_READ | BCM2708_I2C_C_INTR; + } else { + c = BCM2708_I2C_C_INTT; + bcm2708_fill_txfifo(i2c_dev); + } + c |= BCM2708_I2C_C_ST | BCM2708_I2C_C_INTD | BCM2708_I2C_C_I2CEN; + + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_A, msg->addr); + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_DLEN, msg->len); + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_C, c); + + ret = wait_for_completion_timeout(&i2c_dev->completion, + BCM2708_I2C_TIMEOUT); + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_C, BCM2708_I2C_C_CLEAR); + if (WARN_ON(ret == 0)) { + dev_err(i2c_dev->dev, "i2c transfer timed out\n"); + return -ETIMEDOUT; + } + + if (likely(!i2c_dev->msg_err)) + return 0; + + if ((i2c_dev->msg_err & BCM2708_I2C_S_ERR) && + (msg->flags & I2C_M_IGNORE_NAK)) + return 0; + + dev_err(i2c_dev->dev, "i2c transfer failed: %x\n", i2c_dev->msg_err); + + if (i2c_dev->msg_err & BCM2708_I2C_S_ERR) + return -EREMOTEIO; + else + return -EIO; +} + +static int bcm2708_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], + int num) +{ + struct bcm2708_i2c_dev *i2c_dev = i2c_get_adapdata(adap); + int i; + int ret = 0; + + for (i = 0; i < num; i++) { + ret = bcm2708_i2c_xfer_msg(i2c_dev, &msgs[i]); + if (ret) + break; + } + + return ret ?: i; +} + +static u32 bcm2708_i2c_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; +} + +static const struct i2c_algorithm bcm2708_i2c_algo = { + .master_xfer = bcm2708_i2c_xfer, + .functionality = bcm2708_i2c_func, +}; + +static int __devinit bcm2708_i2c_probe(struct platform_device *pdev) +{ + struct bcm2708_i2c_dev *i2c_dev; + struct resource *mem, *requested, *irq; + int ret; + struct i2c_adapter *adap; + + i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); + if (!i2c_dev) { + dev_err(&pdev->dev, "Cannot allocate i2c_dev\n"); + return -ENOMEM; + } + platform_set_drvdata(pdev, i2c_dev); + i2c_dev->dev = &pdev->dev; + init_completion(&i2c_dev->completion); + + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (!mem) { + dev_err(&pdev->dev, "No mem resource\n"); + return -ENODEV; + } + + requested = devm_request_mem_region(&pdev->dev, mem->start, + resource_size(mem), + dev_name(&pdev->dev)); + if (!requested) { + dev_err(&pdev->dev, "Could not claim register region\n"); + return -EBUSY; + } + + i2c_dev->regs = devm_ioremap(&pdev->dev, mem->start, + resource_size(mem)); + if (!i2c_dev->regs) { + dev_err(&pdev->dev, "Could not map registers\n"); + return -ENOMEM; + } + + irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); + if (!irq) { + dev_err(&pdev->dev, "No IRQ resource\n"); + return -ENODEV; + } + + ret = devm_request_irq(&pdev->dev, irq->start, bcm2708_i2c_isr, + IRQF_SHARED, dev_name(&pdev->dev), i2c_dev); + if (ret) { + dev_err(&pdev->dev, "Could not request IRQ\n"); + return -ENODEV; + } + + adap = &i2c_dev->adapter; + i2c_set_adapdata(adap, i2c_dev); + adap->owner = THIS_MODULE; + adap->class = I2C_CLASS_HWMON; + strlcpy(adap->name, "bcm2708 I2C adapter", sizeof(adap->name)); + adap->algo = &bcm2708_i2c_algo; + adap->dev.parent = &pdev->dev; + adap->nr = -1; + + bcm2708_i2c_writel(i2c_dev, BCM2708_I2C_C, 0); + + ret = i2c_add_numbered_adapter(adap); + if (ret) { + dev_err(&pdev->dev, "Could not add adapter\n"); + return ret; + } + + return 0; +} + +static int bcm2708_i2c_remove(struct platform_device *pdev) +{ + struct bcm2708_i2c_dev *i2c_dev = platform_get_drvdata(pdev); + + i2c_del_adapter(&i2c_dev->adapter); + + return 0; +} + +static struct platform_driver bcm2708_i2c_driver = { + .probe = bcm2708_i2c_probe, + .remove = bcm2708_i2c_remove, + .driver = { + .name = "i2c-bcm2708", + .owner = THIS_MODULE, + }, +}; + +static int __init bcm2708_i2c_init_driver(void) +{ + return platform_driver_register(&bcm2708_i2c_driver); +} +module_init(bcm2708_i2c_init_driver); + +static void __exit bcm2708_i2c_exit_driver(void) +{ + platform_driver_unregister(&bcm2708_i2c_driver); +} +module_exit(bcm2708_i2c_exit_driver); + +MODULE_AUTHOR("Stephen Warren "); +MODULE_DESCRIPTION("BCM2708 I2C bus adapter"); +MODULE_LICENSE("GPL v2"); +MODULE_ALIAS("platform:i2c-bcm2708");