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/platform_device.h>
23 #include <linux/fsl_devices.h>
24 #include <linux/i2c.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
28 #define MPC_I2C_ADDR 0x00
29 #define MPC_I2C_FDR 0x04
30 #define MPC_I2C_CR 0x08
31 #define MPC_I2C_SR 0x0c
32 #define MPC_I2C_DR 0x10
33 #define MPC_I2C_DFSRR 0x14
34 #define MPC_I2C_REGION 0x20
54 wait_queue_head_t queue;
55 struct i2c_adapter adap;
60 static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
62 writeb(x, i2c->base + MPC_I2C_CR);
65 static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
67 struct mpc_i2c *i2c = dev_id;
68 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
69 /* Read again to allow register to stabilise */
70 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
71 writeb(0, i2c->base + MPC_I2C_SR);
72 wake_up_interruptible(&i2c->queue);
77 /* Sometimes 9th clock pulse isn't generated, and slave doesn't release
78 * the bus, because it wants to send ACK.
79 * Following sequence of enabling/disabling and sending start/stop generates
80 * the pulse, so it's all OK.
82 static void mpc_i2c_fixup(struct mpc_i2c *i2c)
86 writeccr(i2c, CCR_MEN);
88 writeccr(i2c, CCR_MSTA | CCR_MTX);
90 writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
92 writeccr(i2c, CCR_MEN);
96 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
98 unsigned long orig_jiffies = jiffies;
104 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
106 if (time_after(jiffies, orig_jiffies + timeout)) {
107 pr_debug("I2C: timeout\n");
113 x = readb(i2c->base + MPC_I2C_SR);
114 writeb(0, i2c->base + MPC_I2C_SR);
117 result = wait_event_interruptible_timeout(i2c->queue,
118 (i2c->interrupt & CSR_MIF), timeout * HZ);
120 if (unlikely(result < 0)) {
121 pr_debug("I2C: wait interrupted\n");
123 } else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
124 pr_debug("I2C: wait timeout\n");
136 if (!(x & CSR_MCF)) {
137 pr_debug("I2C: unfinished\n");
142 pr_debug("I2C: MAL\n");
146 if (writing && (x & CSR_RXAK)) {
147 pr_debug("I2C: No RXAK\n");
149 writeccr(i2c, CCR_MEN);
155 static void mpc_i2c_setclock(struct mpc_i2c *i2c)
157 /* Set clock and filters */
158 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
159 writeb(0x31, i2c->base + MPC_I2C_FDR);
160 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
161 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
162 writeb(0x3f, i2c->base + MPC_I2C_FDR);
164 writel(0x1031, i2c->base + MPC_I2C_FDR);
167 static void mpc_i2c_start(struct mpc_i2c *i2c)
169 /* Clear arbitration */
170 writeb(0, i2c->base + MPC_I2C_SR);
172 writeccr(i2c, CCR_MEN);
175 static void mpc_i2c_stop(struct mpc_i2c *i2c)
177 writeccr(i2c, CCR_MEN);
180 static int mpc_write(struct mpc_i2c *i2c, int target,
181 const u8 * data, int length, int restart)
184 unsigned timeout = i2c->adap.timeout;
185 u32 flags = restart ? CCR_RSTA : 0;
189 writeccr(i2c, CCR_MEN);
190 /* Start as master */
191 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
192 /* Write target byte */
193 writeb((target << 1), i2c->base + MPC_I2C_DR);
195 if (i2c_wait(i2c, timeout, 1) < 0)
198 for (i = 0; i < length; i++) {
199 /* Write data byte */
200 writeb(data[i], i2c->base + MPC_I2C_DR);
202 if (i2c_wait(i2c, timeout, 1) < 0)
209 static int mpc_read(struct mpc_i2c *i2c, int target,
210 u8 * data, int length, int restart)
212 unsigned timeout = i2c->adap.timeout;
214 u32 flags = restart ? CCR_RSTA : 0;
218 writeccr(i2c, CCR_MEN);
219 /* Switch to read - restart */
220 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
221 /* Write target address byte - this time with the read flag set */
222 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
224 if (i2c_wait(i2c, timeout, 1) < 0)
229 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
231 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
233 readb(i2c->base + MPC_I2C_DR);
236 for (i = 0; i < length; i++) {
237 if (i2c_wait(i2c, timeout, 0) < 0)
240 /* Generate txack on next to last byte */
242 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
243 /* Generate stop on last byte */
245 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
246 data[i] = readb(i2c->base + MPC_I2C_DR);
252 static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
254 struct i2c_msg *pmsg;
257 unsigned long orig_jiffies = jiffies;
258 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
262 /* Allow bus up to 1s to become not busy */
263 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
264 if (signal_pending(current)) {
265 pr_debug("I2C: Interrupted\n");
269 if (time_after(jiffies, orig_jiffies + HZ)) {
270 pr_debug("I2C: timeout\n");
271 if (readb(i2c->base + MPC_I2C_SR) ==
272 (CSR_MCF | CSR_MBB | CSR_RXAK))
279 for (i = 0; ret >= 0 && i < num; i++) {
281 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
282 pmsg->flags & I2C_M_RD ? "read" : "write",
283 pmsg->len, pmsg->addr, i + 1, num);
284 if (pmsg->flags & I2C_M_RD)
286 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
289 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
292 return (ret < 0) ? ret : num;
295 static u32 mpc_functionality(struct i2c_adapter *adap)
297 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
300 static const struct i2c_algorithm mpc_algo = {
301 .master_xfer = mpc_xfer,
302 .functionality = mpc_functionality,
305 static struct i2c_adapter mpc_ops = {
306 .owner = THIS_MODULE,
307 .name = "MPC adapter",
310 .class = I2C_CLASS_HWMON,
315 static int fsl_i2c_probe(struct platform_device *pdev)
319 struct fsl_i2c_platform_data *pdata;
320 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
322 pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
324 if (!(i2c = kzalloc(sizeof(*i2c), GFP_KERNEL))) {
328 i2c->irq = platform_get_irq(pdev, 0);
333 i2c->flags = pdata->device_flags;
334 init_waitqueue_head(&i2c->queue);
336 i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
339 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
345 if ((result = request_irq(i2c->irq, mpc_i2c_isr,
346 IRQF_SHARED, "i2c-mpc", i2c)) < 0) {
348 "i2c-mpc - failed to attach interrupt\n");
352 mpc_i2c_setclock(i2c);
353 platform_set_drvdata(pdev, i2c);
356 i2c->adap.nr = pdev->id;
357 i2c_set_adapdata(&i2c->adap, i2c);
358 i2c->adap.dev.parent = &pdev->dev;
359 if ((result = i2c_add_numbered_adapter(&i2c->adap)) < 0) {
360 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
368 free_irq(i2c->irq, i2c);
377 static int fsl_i2c_remove(struct platform_device *pdev)
379 struct mpc_i2c *i2c = platform_get_drvdata(pdev);
381 i2c_del_adapter(&i2c->adap);
382 platform_set_drvdata(pdev, NULL);
385 free_irq(i2c->irq, i2c);
392 /* Structure for a device driver */
393 static struct platform_driver fsl_i2c_driver = {
394 .probe = fsl_i2c_probe,
395 .remove = fsl_i2c_remove,
397 .owner = THIS_MODULE,
402 static int __init fsl_i2c_init(void)
404 return platform_driver_register(&fsl_i2c_driver);
407 static void __exit fsl_i2c_exit(void)
409 platform_driver_unregister(&fsl_i2c_driver);
412 module_init(fsl_i2c_init);
413 module_exit(fsl_i2c_exit);
415 MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
417 ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
418 MODULE_LICENSE("GPL");