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/kernel.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
24 #include <linux/fsl_devices.h>
25 #include <linux/i2c.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
29 #define MPC_I2C_ADDR 0x00
30 #define MPC_I2C_FDR 0x04
31 #define MPC_I2C_CR 0x08
32 #define MPC_I2C_SR 0x0c
33 #define MPC_I2C_DR 0x10
34 #define MPC_I2C_DFSRR 0x14
35 #define MPC_I2C_REGION 0x20
55 wait_queue_head_t queue;
56 struct i2c_adapter adap;
61 static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
63 writeb(x, i2c->base + MPC_I2C_CR);
66 static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
68 struct mpc_i2c *i2c = dev_id;
69 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
70 /* Read again to allow register to stabilise */
71 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
72 writeb(0, i2c->base + MPC_I2C_SR);
73 wake_up_interruptible(&i2c->queue);
78 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
80 unsigned long orig_jiffies = jiffies;
86 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
88 if (time_after(jiffies, orig_jiffies + timeout)) {
89 pr_debug("I2C: timeout\n");
94 x = readb(i2c->base + MPC_I2C_SR);
95 writeb(0, i2c->base + MPC_I2C_SR);
98 result = wait_event_interruptible_timeout(i2c->queue,
99 (i2c->interrupt & CSR_MIF), timeout * HZ);
101 if (unlikely(result < 0))
102 pr_debug("I2C: wait interrupted\n");
103 else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
104 pr_debug("I2C: wait timeout\n");
115 if (!(x & CSR_MCF)) {
116 pr_debug("I2C: unfinished\n");
121 pr_debug("I2C: MAL\n");
125 if (writing && (x & CSR_RXAK)) {
126 pr_debug("I2C: No RXAK\n");
128 writeccr(i2c, CCR_MEN);
134 static void mpc_i2c_setclock(struct mpc_i2c *i2c)
136 /* Set clock and filters */
137 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
138 writeb(0x31, i2c->base + MPC_I2C_FDR);
139 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
140 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
141 writeb(0x3f, i2c->base + MPC_I2C_FDR);
143 writel(0x1031, i2c->base + MPC_I2C_FDR);
146 static void mpc_i2c_start(struct mpc_i2c *i2c)
148 /* Clear arbitration */
149 writeb(0, i2c->base + MPC_I2C_SR);
151 writeccr(i2c, CCR_MEN);
154 static void mpc_i2c_stop(struct mpc_i2c *i2c)
156 writeccr(i2c, CCR_MEN);
159 static int mpc_write(struct mpc_i2c *i2c, int target,
160 const u8 * data, int length, int restart)
163 unsigned timeout = i2c->adap.timeout;
164 u32 flags = restart ? CCR_RSTA : 0;
168 writeccr(i2c, CCR_MEN);
169 /* Start as master */
170 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
171 /* Write target byte */
172 writeb((target << 1), i2c->base + MPC_I2C_DR);
174 if (i2c_wait(i2c, timeout, 1) < 0)
177 for (i = 0; i < length; i++) {
178 /* Write data byte */
179 writeb(data[i], i2c->base + MPC_I2C_DR);
181 if (i2c_wait(i2c, timeout, 1) < 0)
188 static int mpc_read(struct mpc_i2c *i2c, int target,
189 u8 * data, int length, int restart)
191 unsigned timeout = i2c->adap.timeout;
193 u32 flags = restart ? CCR_RSTA : 0;
197 writeccr(i2c, CCR_MEN);
198 /* Switch to read - restart */
199 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
200 /* Write target address byte - this time with the read flag set */
201 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
203 if (i2c_wait(i2c, timeout, 1) < 0)
208 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
210 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
212 readb(i2c->base + MPC_I2C_DR);
215 for (i = 0; i < length; i++) {
216 if (i2c_wait(i2c, timeout, 0) < 0)
219 /* Generate txack on next to last byte */
221 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
222 /* Generate stop on last byte */
224 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
225 data[i] = readb(i2c->base + MPC_I2C_DR);
231 static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
233 struct i2c_msg *pmsg;
236 unsigned long orig_jiffies = jiffies;
237 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
241 /* Allow bus up to 1s to become not busy */
242 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
243 if (signal_pending(current)) {
244 pr_debug("I2C: Interrupted\n");
247 if (time_after(jiffies, orig_jiffies + HZ)) {
248 pr_debug("I2C: timeout\n");
254 for (i = 0; ret >= 0 && i < num; i++) {
256 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
257 pmsg->flags & I2C_M_RD ? "read" : "write",
258 pmsg->len, pmsg->addr, i + 1, num);
259 if (pmsg->flags & I2C_M_RD)
261 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
264 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
267 return (ret < 0) ? ret : num;
270 static u32 mpc_functionality(struct i2c_adapter *adap)
272 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
275 static const struct i2c_algorithm mpc_algo = {
276 .master_xfer = mpc_xfer,
277 .functionality = mpc_functionality,
280 static struct i2c_adapter mpc_ops = {
281 .owner = THIS_MODULE,
282 .name = "MPC adapter",
285 .class = I2C_CLASS_HWMON,
290 static int fsl_i2c_probe(struct platform_device *pdev)
294 struct fsl_i2c_platform_data *pdata;
295 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
297 pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
299 if (!(i2c = kzalloc(sizeof(*i2c), GFP_KERNEL))) {
303 i2c->irq = platform_get_irq(pdev, 0);
308 i2c->flags = pdata->device_flags;
309 init_waitqueue_head(&i2c->queue);
311 i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
314 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
320 if ((result = request_irq(i2c->irq, mpc_i2c_isr,
321 IRQF_SHARED, "i2c-mpc", i2c)) < 0) {
323 "i2c-mpc - failed to attach interrupt\n");
327 mpc_i2c_setclock(i2c);
328 platform_set_drvdata(pdev, i2c);
331 i2c_set_adapdata(&i2c->adap, i2c);
332 i2c->adap.dev.parent = &pdev->dev;
333 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
334 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
342 free_irq(i2c->irq, NULL);
351 static int fsl_i2c_remove(struct platform_device *pdev)
353 struct mpc_i2c *i2c = platform_get_drvdata(pdev);
355 i2c_del_adapter(&i2c->adap);
356 platform_set_drvdata(pdev, NULL);
359 free_irq(i2c->irq, i2c);
366 /* Structure for a device driver */
367 static struct platform_driver fsl_i2c_driver = {
368 .probe = fsl_i2c_probe,
369 .remove = fsl_i2c_remove,
371 .owner = THIS_MODULE,
376 static int __init fsl_i2c_init(void)
378 return platform_driver_register(&fsl_i2c_driver);
381 static void __exit fsl_i2c_exit(void)
383 platform_driver_unregister(&fsl_i2c_driver);
386 module_init(fsl_i2c_init);
387 module_exit(fsl_i2c_exit);
389 MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
391 ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
392 MODULE_LICENSE("GPL");