2 +++ b/drivers/i2c/busses/i2c-bcm2708.c
5 + * Driver for Broadcom BCM2708 BSC Controllers
7 + * Copyright (C) 2012 Chris Boot & Frank Buss
9 + * This driver is inspired by:
10 + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
12 + * This program is free software; you can redistribute it and/or modify
13 + * it under the terms of the GNU General Public License as published by
14 + * the Free Software Foundation; either version 2 of the License, or
15 + * (at your option) any later version.
17 + * This program is distributed in the hope that it will be useful,
18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 + * GNU General Public License for more details.
22 + * You should have received a copy of the GNU General Public License
23 + * along with this program; if not, write to the Free Software
24 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 +#include <linux/kernel.h>
28 +#include <linux/module.h>
29 +#include <linux/spinlock.h>
30 +#include <linux/clk.h>
31 +#include <linux/err.h>
32 +#include <linux/platform_device.h>
33 +#include <linux/io.h>
34 +#include <linux/slab.h>
35 +#include <linux/i2c.h>
36 +#include <linux/interrupt.h>
37 +#include <linux/sched.h>
38 +#include <linux/wait.h>
40 +/* BSC register offsets */
43 +#define BSC_DLEN 0x08
45 +#define BSC_FIFO 0x10
48 +#define BSC_CLKT 0x1c
50 +/* Bitfields in BSC_C */
51 +#define BSC_C_I2CEN 0x00008000
52 +#define BSC_C_INTR 0x00000400
53 +#define BSC_C_INTT 0x00000200
54 +#define BSC_C_INTD 0x00000100
55 +#define BSC_C_ST 0x00000080
56 +#define BSC_C_CLEAR_1 0x00000020
57 +#define BSC_C_CLEAR_2 0x00000010
58 +#define BSC_C_READ 0x00000001
60 +/* Bitfields in BSC_S */
61 +#define BSC_S_CLKT 0x00000200
62 +#define BSC_S_ERR 0x00000100
63 +#define BSC_S_RXF 0x00000080
64 +#define BSC_S_TXE 0x00000040
65 +#define BSC_S_RXD 0x00000020
66 +#define BSC_S_TXD 0x00000010
67 +#define BSC_S_RXR 0x00000008
68 +#define BSC_S_TXW 0x00000004
69 +#define BSC_S_DONE 0x00000002
70 +#define BSC_S_TA 0x00000001
72 +#define I2C_TIMEOUT_MS 150
74 +#define DRV_NAME "bcm2708_i2c"
76 +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
77 +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
78 +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
82 + struct i2c_adapter adapter;
89 + struct completion done;
91 + struct i2c_msg *msg;
98 + * This function sets the ALT mode on the I2C pins so that we can use them with
101 + * FIXME: This is a hack. Use pinmux / pinctrl.
103 +static void bcm2708_i2c_init_pinmode(int id)
105 +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
106 +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
109 + u32 *gpio = ioremap(0x20200000, SZ_16K);
111 + BUG_ON(id != 0 && id != 1);
112 + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
113 + for (pin = id*2+0; pin <= id*2+1; pin++) {
114 +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
115 + INP_GPIO(pin); /* set mode to GPIO input first */
116 + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
125 +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
127 + return readl(bi->base + reg);
130 +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
132 + writel(val, bi->base + reg);
135 +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
137 + bcm2708_wr(bi, BSC_C, 0);
138 + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
141 +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
143 + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
144 + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
147 +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
149 + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
150 + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
153 +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
155 + unsigned long bus_hz;
157 + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
159 + bus_hz = clk_get_rate(bi->clk);
160 + cdiv = bus_hz / baudrate;
162 + if (bi->msg->flags & I2C_M_RD)
163 + c |= BSC_C_INTR | BSC_C_READ;
167 + bcm2708_wr(bi, BSC_DIV, cdiv);
168 + bcm2708_wr(bi, BSC_A, bi->msg->addr);
169 + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
170 + bcm2708_wr(bi, BSC_C, c);
173 +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
175 + struct bcm2708_i2c *bi = dev_id;
176 + bool handled = true;
179 + spin_lock(&bi->lock);
181 + /* we may see camera interrupts on the "other" I2C channel
182 + Just return if we've not sent anything */
183 + if (!bi->nmsgs || !bi->msg )
186 + s = bcm2708_rd(bi, BSC_S);
188 + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
189 + bcm2708_bsc_reset(bi);
192 + /* wake up our bh */
193 + complete(&bi->done);
194 + } else if (s & BSC_S_DONE) {
197 + if (bi->msg->flags & I2C_M_RD)
198 + bcm2708_bsc_fifo_drain(bi);
200 + bcm2708_bsc_reset(bi);
203 + /* advance to next message */
206 + bcm2708_bsc_setup(bi);
208 + /* wake up our bh */
209 + complete(&bi->done);
211 + } else if (s & BSC_S_TXW) {
212 + bcm2708_bsc_fifo_fill(bi);
213 + } else if (s & BSC_S_RXR) {
214 + bcm2708_bsc_fifo_drain(bi);
220 + spin_unlock(&bi->lock);
222 + return handled ? IRQ_HANDLED : IRQ_NONE;
225 +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
226 + struct i2c_msg *msgs, int num)
228 + struct bcm2708_i2c *bi = adap->algo_data;
229 + unsigned long flags;
232 + spin_lock_irqsave(&bi->lock, flags);
234 + INIT_COMPLETION(bi->done);
240 + spin_unlock_irqrestore(&bi->lock, flags);
242 + bcm2708_bsc_setup(bi);
244 + ret = wait_for_completion_timeout(&bi->done,
245 + msecs_to_jiffies(I2C_TIMEOUT_MS));
247 + dev_err(&adap->dev, "transfer timed out\n");
248 + spin_lock_irqsave(&bi->lock, flags);
249 + bcm2708_bsc_reset(bi);
250 + spin_unlock_irqrestore(&bi->lock, flags);
254 + return bi->error ? -EIO : num;
257 +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
259 + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
262 +static struct i2c_algorithm bcm2708_i2c_algorithm = {
263 + .master_xfer = bcm2708_i2c_master_xfer,
264 + .functionality = bcm2708_i2c_functionality,
267 +static int bcm2708_i2c_probe(struct platform_device *pdev)
269 + struct resource *regs;
270 + int irq, err = -ENOMEM;
272 + struct bcm2708_i2c *bi;
273 + struct i2c_adapter *adap;
275 + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
277 + dev_err(&pdev->dev, "could not get IO memory\n");
281 + irq = platform_get_irq(pdev, 0);
283 + dev_err(&pdev->dev, "could not get IRQ\n");
287 + clk = clk_get(&pdev->dev, NULL);
289 + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
290 + return PTR_ERR(clk);
293 + bcm2708_i2c_init_pinmode(pdev->id);
295 + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
299 + platform_set_drvdata(pdev, bi);
301 + adap = &bi->adapter;
302 + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
303 + adap->algo = &bcm2708_i2c_algorithm;
304 + adap->algo_data = bi;
305 + adap->dev.parent = &pdev->dev;
306 + adap->nr = pdev->id;
307 + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
309 + switch (pdev->id) {
311 + adap->class = I2C_CLASS_HWMON;
314 + adap->class = I2C_CLASS_DDC;
317 + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
322 + spin_lock_init(&bi->lock);
323 + init_completion(&bi->done);
325 + bi->base = ioremap(regs->start, resource_size(regs));
327 + dev_err(&pdev->dev, "could not remap memory\n");
334 + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
335 + dev_name(&pdev->dev), bi);
337 + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
341 + bcm2708_bsc_reset(bi);
343 + err = i2c_add_numbered_adapter(adap);
345 + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
349 + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %dk)\n",
350 + pdev->id, (unsigned long)regs->start, irq, baudrate/1000);
355 + free_irq(bi->irq, bi);
365 +static int bcm2708_i2c_remove(struct platform_device *pdev)
367 + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
369 + platform_set_drvdata(pdev, NULL);
371 + i2c_del_adapter(&bi->adapter);
372 + free_irq(bi->irq, bi);
374 + clk_disable(bi->clk);
381 +static struct platform_driver bcm2708_i2c_driver = {
384 + .owner = THIS_MODULE,
386 + .probe = bcm2708_i2c_probe,
387 + .remove = bcm2708_i2c_remove,
390 +// module_platform_driver(bcm2708_i2c_driver);
393 +static int __init bcm2708_i2c_init(void)
395 + return platform_driver_register(&bcm2708_i2c_driver);
398 +static void __exit bcm2708_i2c_exit(void)
400 + platform_driver_unregister(&bcm2708_i2c_driver);
403 +module_init(bcm2708_i2c_init);
404 +module_exit(bcm2708_i2c_exit);
408 +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
409 +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
410 +MODULE_LICENSE("GPL v2");
411 +MODULE_ALIAS("platform:" DRV_NAME);
412 --- a/drivers/i2c/busses/Kconfig
413 +++ b/drivers/i2c/busses/Kconfig
414 @@ -345,6 +345,25 @@ config I2C_BCM2835
415 This support is also available as a module. If so, the module
416 will be called i2c-bcm2835.
419 + tristate "BCM2708 BSC"
420 + depends on MACH_BCM2708
422 + Enabling this option will add BSC (Broadcom Serial Controller)
423 + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
424 + with I2C/TWI/SMBus.
426 +config I2C_BCM2708_BAUDRATE
427 + prompt "BCM2708 I2C baudrate"
428 + depends on I2C_BCM2708
432 + Set the I2C baudrate. This will alter the default value. A
433 + different baudrate can be set by using a module parameter as well. If
434 + no parameter is provided when loading, this is the value that will be
437 config I2C_BLACKFIN_TWI
438 tristate "Blackfin TWI I2C support"
440 --- a/drivers/i2c/busses/Makefile
441 +++ b/drivers/i2c/busses/Makefile
442 @@ -32,6 +32,7 @@ obj-$(CONFIG_I2C_POWERMAC) += i2c-powerm
443 obj-$(CONFIG_I2C_AT91) += i2c-at91.o
444 obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
445 obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
446 +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
447 obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
448 obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
449 obj-$(CONFIG_I2C_CPM) += i2c-cpm.o