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>
22 #include <linux/platform_device.h>
25 #include <linux/fsl_devices.h>
26 #include <linux/i2c.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
30 #define MPC_I2C_ADDR 0x00
31 #define MPC_I2C_FDR 0x04
32 #define MPC_I2C_CR 0x08
33 #define MPC_I2C_SR 0x0c
34 #define MPC_I2C_DR 0x10
35 #define MPC_I2C_DFSRR 0x14
36 #define MPC_I2C_REGION 0x20
56 wait_queue_head_t queue;
57 struct i2c_adapter adap;
62 static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
64 writeb(x, i2c->base + MPC_I2C_CR);
67 static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
69 struct mpc_i2c *i2c = dev_id;
70 if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
71 /* Read again to allow register to stabilise */
72 i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
73 writeb(0, i2c->base + MPC_I2C_SR);
74 wake_up_interruptible(&i2c->queue);
79 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
81 unsigned long orig_jiffies = jiffies;
87 while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
89 if (time_after(jiffies, orig_jiffies + timeout)) {
90 pr_debug("I2C: timeout\n");
95 x = readb(i2c->base + MPC_I2C_SR);
96 writeb(0, i2c->base + MPC_I2C_SR);
99 result = wait_event_interruptible_timeout(i2c->queue,
100 (i2c->interrupt & CSR_MIF), timeout * HZ);
102 if (unlikely(result < 0))
103 pr_debug("I2C: wait interrupted\n");
104 else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
105 pr_debug("I2C: wait timeout\n");
116 if (!(x & CSR_MCF)) {
117 pr_debug("I2C: unfinished\n");
122 pr_debug("I2C: MAL\n");
126 if (writing && (x & CSR_RXAK)) {
127 pr_debug("I2C: No RXAK\n");
129 writeccr(i2c, CCR_MEN);
135 static void mpc_i2c_setclock(struct mpc_i2c *i2c)
137 /* Set clock and filters */
138 if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
139 writeb(0x31, i2c->base + MPC_I2C_FDR);
140 writeb(0x10, i2c->base + MPC_I2C_DFSRR);
141 } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
142 writeb(0x3f, i2c->base + MPC_I2C_FDR);
144 writel(0x1031, i2c->base + MPC_I2C_FDR);
147 static void mpc_i2c_start(struct mpc_i2c *i2c)
149 /* Clear arbitration */
150 writeb(0, i2c->base + MPC_I2C_SR);
152 writeccr(i2c, CCR_MEN);
155 static void mpc_i2c_stop(struct mpc_i2c *i2c)
157 writeccr(i2c, CCR_MEN);
160 static int mpc_write(struct mpc_i2c *i2c, int target,
161 const u8 * data, int length, int restart)
164 unsigned timeout = i2c->adap.timeout;
165 u32 flags = restart ? CCR_RSTA : 0;
169 writeccr(i2c, CCR_MEN);
170 /* Start as master */
171 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
172 /* Write target byte */
173 writeb((target << 1), i2c->base + MPC_I2C_DR);
175 if (i2c_wait(i2c, timeout, 1) < 0)
178 for (i = 0; i < length; i++) {
179 /* Write data byte */
180 writeb(data[i], i2c->base + MPC_I2C_DR);
182 if (i2c_wait(i2c, timeout, 1) < 0)
189 static int mpc_read(struct mpc_i2c *i2c, int target,
190 u8 * data, int length, int restart)
192 unsigned timeout = i2c->adap.timeout;
194 u32 flags = restart ? CCR_RSTA : 0;
198 writeccr(i2c, CCR_MEN);
199 /* Switch to read - restart */
200 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
201 /* Write target address byte - this time with the read flag set */
202 writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
204 if (i2c_wait(i2c, timeout, 1) < 0)
209 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
211 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
213 readb(i2c->base + MPC_I2C_DR);
216 for (i = 0; i < length; i++) {
217 if (i2c_wait(i2c, timeout, 0) < 0)
220 /* Generate txack on next to last byte */
222 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
223 /* Generate stop on last byte */
225 writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
226 data[i] = readb(i2c->base + MPC_I2C_DR);
232 static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
234 struct i2c_msg *pmsg;
237 unsigned long orig_jiffies = jiffies;
238 struct mpc_i2c *i2c = i2c_get_adapdata(adap);
242 /* Allow bus up to 1s to become not busy */
243 while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
244 if (signal_pending(current)) {
245 pr_debug("I2C: Interrupted\n");
248 if (time_after(jiffies, orig_jiffies + HZ)) {
249 pr_debug("I2C: timeout\n");
255 for (i = 0; ret >= 0 && i < num; i++) {
257 pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
258 pmsg->flags & I2C_M_RD ? "read" : "write",
259 pmsg->len, pmsg->addr, i + 1, num);
260 if (pmsg->flags & I2C_M_RD)
262 mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
265 mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
268 return (ret < 0) ? ret : num;
271 static u32 mpc_functionality(struct i2c_adapter *adap)
273 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
276 static struct i2c_algorithm mpc_algo = {
277 .master_xfer = mpc_xfer,
278 .functionality = mpc_functionality,
281 static struct i2c_adapter mpc_ops = {
282 .owner = THIS_MODULE,
283 .name = "MPC adapter",
286 .class = I2C_CLASS_HWMON,
291 static int fsl_i2c_probe(struct device *device)
295 struct platform_device *pdev = to_platform_device(device);
296 struct fsl_i2c_platform_data *pdata;
297 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
299 pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
301 if (!(i2c = kzalloc(sizeof(*i2c), GFP_KERNEL))) {
305 i2c->irq = platform_get_irq(pdev, 0);
306 i2c->flags = pdata->device_flags;
307 init_waitqueue_head(&i2c->queue);
309 i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
312 printk(KERN_ERR "i2c-mpc - failed to map controller\n");
318 if ((result = request_irq(i2c->irq, mpc_i2c_isr,
319 SA_SHIRQ, "i2c-mpc", i2c)) < 0) {
321 "i2c-mpc - failed to attach interrupt\n");
325 mpc_i2c_setclock(i2c);
326 dev_set_drvdata(device, i2c);
329 i2c_set_adapdata(&i2c->adap, i2c);
330 i2c->adap.dev.parent = &pdev->dev;
331 if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
332 printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
340 free_irq(i2c->irq, NULL);
348 static int fsl_i2c_remove(struct device *device)
350 struct mpc_i2c *i2c = dev_get_drvdata(device);
352 i2c_del_adapter(&i2c->adap);
353 dev_set_drvdata(device, NULL);
356 free_irq(i2c->irq, i2c);
363 /* Structure for a device driver */
364 static struct device_driver fsl_i2c_driver = {
365 .owner = THIS_MODULE,
367 .bus = &platform_bus_type,
368 .probe = fsl_i2c_probe,
369 .remove = fsl_i2c_remove,
372 static int __init fsl_i2c_init(void)
374 return driver_register(&fsl_i2c_driver);
377 static void __exit fsl_i2c_exit(void)
379 driver_unregister(&fsl_i2c_driver);
382 module_init(fsl_i2c_init);
383 module_exit(fsl_i2c_exit);
385 MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
387 ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
388 MODULE_LICENSE("GPL");