2 * (C) Copyright 2003-2004
3 * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
5 * This is a combined i2c adapter and algorithm driver for the
6 * MPC107/Tsi107 PowerPC northbridge and processors that include
7 * the same I2C unit (8240, 8245, 85xx).
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/pci.h>
25 #define FSL_I2C_DEV_SEPARATE_DFSRR FS_I2C_SEPARATE_DFSRR
26 #define FSL_I2C_DEV_CLOCK_5200 FS_I2C_CLOCK_5200
28 #include <linux/fsl_devices.h>
30 #include <linux/i2c.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
34 #define MPC_I2C_ADDR 0x00
35 #define MPC_I2C_FDR 0x04
36 #define MPC_I2C_CR 0x08
37 #define MPC_I2C_SR 0x0c
38 #define MPC_I2C_DR 0x10
39 #define MPC_I2C_DFSRR 0x14
40 #define MPC_I2C_REGION 0x20
60 wait_queue_head_t queue;
61 struct i2c_adapter adap;
66 static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
68 writeb(x, i2c->base + MPC_I2C_CR);
71 static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
73 struct mpc_i2c *i2c = dev_id;
74 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
75 /* Read again to allow register to stabilise */
76 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
77 writeb(0, i2c->base + MPC_I2C_SR);
78 wake_up_interruptible(&i2c->queue);
83 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
85 unsigned long orig_jiffies = jiffies;
91 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
93 if (time_after(jiffies, orig_jiffies + timeout)) {
94 pr_debug("I2C: timeout\n");
99 x = readb(i2c->base + MPC_I2C_SR);
100 writeb(0, i2c->base + MPC_I2C_SR);
103 result = wait_event_interruptible_timeout(i2c->queue,
104 (i2c->interrupt & CSR_MIF), timeout * HZ);
106 if (unlikely(result < 0))
107 pr_debug("I2C: wait interrupted\n");
108 else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
109 pr_debug("I2C: wait timeout\n");
120 if (!(x & CSR_MCF)) {
121 pr_debug("I2C: unfinished\n");
126 pr_debug("I2C: MAL\n");
130 if (writing && (x & CSR_RXAK)) {
131 pr_debug("I2C: No RXAK\n");
133 writeccr(i2c, CCR_MEN);
139 static void mpc_i2c_setclock(struct mpc_i2c *i2c)
141 /* Set clock and filters */
142 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
143 writeb(0x31, i2c->base + MPC_I2C_FDR);
144 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
145 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
146 writeb(0x3f, i2c->base + MPC_I2C_FDR);
148 writel(0x1031, i2c->base + MPC_I2C_FDR);
151 static void mpc_i2c_start(struct mpc_i2c *i2c)
153 /* Clear arbitration */
154 writeb(0, i2c->base + MPC_I2C_SR);
156 writeccr(i2c, CCR_MEN);
159 static void mpc_i2c_stop(struct mpc_i2c *i2c)
161 writeccr(i2c, CCR_MEN);
164 static int mpc_write(struct mpc_i2c *i2c, int target,
165 const u8 * data, int length, int restart)
168 unsigned timeout = i2c->adap.timeout;
169 u32 flags = restart ? CCR_RSTA : 0;
173 writeccr(i2c, CCR_MEN);
174 /* Start as master */
175 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
176 /* Write target byte */
177 writeb((target << 1), i2c->base + MPC_I2C_DR);
179 if (i2c_wait(i2c, timeout, 1) < 0)
182 for (i = 0; i < length; i++) {
183 /* Write data byte */
184 writeb(data[i], i2c->base + MPC_I2C_DR);
186 if (i2c_wait(i2c, timeout, 1) < 0)
193 static int mpc_read(struct mpc_i2c *i2c, int target,
194 u8 * data, int length, int restart)
196 unsigned timeout = i2c->adap.timeout;
198 u32 flags = restart ? CCR_RSTA : 0;
202 writeccr(i2c, CCR_MEN);
203 /* Switch to read - restart */
204 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
205 /* Write target address byte - this time with the read flag set */
206 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
208 if (i2c_wait(i2c, timeout, 1) < 0)
213 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
215 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
217 readb(i2c->base + MPC_I2C_DR);
220 for (i = 0; i < length; i++) {
221 if (i2c_wait(i2c, timeout, 0) < 0)
224 /* Generate txack on next to last byte */
226 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
227 /* Generate stop on last byte */
229 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
230 data[i] = readb(i2c->base + MPC_I2C_DR);
236 static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
238 struct i2c_msg *pmsg;
241 unsigned long orig_jiffies = jiffies;
242 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
246 /* Allow bus up to 1s to become not busy */
247 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
248 if (signal_pending(current)) {
249 pr_debug("I2C: Interrupted\n");
252 if (time_after(jiffies, orig_jiffies + HZ)) {
253 pr_debug("I2C: timeout\n");
259 for (i = 0; ret >= 0 && i < num; i++) {
261 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
262 pmsg->flags & I2C_M_RD ? "read" : "write",
263 pmsg->len, pmsg->addr, i + 1, num);
264 if (pmsg->flags & I2C_M_RD)
266 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
269 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
272 return (ret < 0) ? ret : num;
275 static u32 mpc_functionality(struct i2c_adapter *adap)
277 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
280 static struct i2c_algorithm mpc_algo = {
281 .name = "MPC algorithm",
282 .id = I2C_ALGO_MPC107,
283 .master_xfer = mpc_xfer,
284 .functionality = mpc_functionality,
287 static struct i2c_adapter mpc_ops = {
288 .owner = THIS_MODULE,
289 .name = "MPC adapter",
290 .id = I2C_ALGO_MPC107 | I2C_HW_MPC107,
292 .class = I2C_CLASS_HWMON,
297 #ifdef CONFIG_FSL_OCP
298 static int __devinit mpc_i2c_probe(struct ocp_device *ocp)
303 if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
306 memset(i2c, 0, sizeof(*i2c));
308 i2c->irq = ocp->def->irq;
309 i2c->flags = ((struct ocp_fs_i2c_data *)ocp->def->additions)->flags;
310 init_waitqueue_head(&i2c->queue);
312 if (!request_mem_region(ocp->def->paddr, MPC_I2C_REGION, "i2c-mpc")) {
313 printk(KERN_ERR "i2c-mpc - resource unavailable\n");
317 i2c->base = ioremap(ocp->def->paddr, MPC_I2C_REGION);
320 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
325 if (i2c->irq != OCP_IRQ_NA)
327 if ((result = request_irq(ocp->def->irq, mpc_i2c_isr,
328 0, "i2c-mpc", i2c)) < 0) {
330 "i2c-mpc - failed to attach interrupt\n");
337 i2c_set_adapdata(&i2c->adap, i2c);
339 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
340 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
344 mpc_i2c_setclock(i2c);
345 ocp_set_drvdata(ocp, i2c);
349 if (ocp->def->irq != OCP_IRQ_NA)
350 free_irq(ocp->def->irq, 0);
354 release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
358 static void __devexit mpc_i2c_remove(struct ocp_device *ocp)
360 struct mpc_i2c *i2c = ocp_get_drvdata(ocp);
361 ocp_set_drvdata(ocp, NULL);
362 i2c_del_adapter(&i2c->adap);
364 if (ocp->def->irq != OCP_IRQ_NA)
365 free_irq(i2c->irq, i2c);
367 release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
371 static struct ocp_device_id mpc_iic_ids[] __devinitdata = {
372 {.vendor = OCP_VENDOR_FREESCALE,.function = OCP_FUNC_IIC},
373 {.vendor = OCP_VENDOR_INVALID}
376 MODULE_DEVICE_TABLE(ocp, mpc_iic_ids);
378 static struct ocp_driver mpc_iic_driver = {
380 .id_table = mpc_iic_ids,
381 .probe = mpc_i2c_probe,
382 .remove = __devexit_p(mpc_i2c_remove)
385 static int __init iic_init(void)
387 return ocp_register_driver(&mpc_iic_driver);
390 static void __exit iic_exit(void)
392 ocp_unregister_driver(&mpc_iic_driver);
395 module_init(iic_init);
396 module_exit(iic_exit);
398 static int fsl_i2c_probe(struct device *device)
402 struct platform_device *pdev = to_platform_device(device);
403 struct fsl_i2c_platform_data *pdata;
404 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
406 pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
408 if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
411 memset(i2c, 0, sizeof(*i2c));
413 i2c->irq = platform_get_irq(pdev, 0);
414 i2c->flags = pdata->device_flags;
415 init_waitqueue_head(&i2c->queue);
417 i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
420 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
426 if ((result = request_irq(i2c->irq, mpc_i2c_isr,
427 0, "fsl-i2c", i2c)) < 0) {
429 "i2c-mpc - failed to attach interrupt\n");
434 i2c_set_adapdata(&i2c->adap, i2c);
435 i2c->adap.dev.parent = &pdev->dev;
436 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
437 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
441 mpc_i2c_setclock(i2c);
442 dev_set_drvdata(device, i2c);
447 free_irq(i2c->irq, NULL);
455 static int fsl_i2c_remove(struct device *device)
457 struct mpc_i2c *i2c = dev_get_drvdata(device);
459 dev_set_drvdata(device, NULL);
460 i2c_del_adapter(&i2c->adap);
463 free_irq(i2c->irq, i2c);
470 /* Structure for a device driver */
471 static struct device_driver fsl_i2c_driver = {
473 .bus = &platform_bus_type,
474 .probe = fsl_i2c_probe,
475 .remove = fsl_i2c_remove,
478 static int __init fsl_i2c_init(void)
480 return driver_register(&fsl_i2c_driver);
483 static void __exit fsl_i2c_exit(void)
485 driver_unregister(&fsl_i2c_driver);
488 module_init(fsl_i2c_init);
489 module_exit(fsl_i2c_exit);
491 #endif /* CONFIG_FSL_OCP */
493 MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
495 ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
496 MODULE_LICENSE("GPL");