2 * TI DAVINCI I2C adapter driver.
4 * Copyright (C) 2006 Texas Instruments.
5 * Copyright (C) 2007 MontaVista Software Inc.
7 * Updated by Vinod & Sudhakar Feb 2005
9 * ----------------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * ----------------------------------------------------------------------------
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/clk.h>
32 #include <linux/errno.h>
33 #include <linux/sched.h>
34 #include <linux/err.h>
35 #include <linux/interrupt.h>
36 #include <linux/platform_device.h>
39 #include <asm/hardware.h>
40 #include <asm/mach-types.h>
42 #include <asm/arch/i2c.h>
44 /* ----- global defines ----------------------------------------------- */
46 #define DAVINCI_I2C_TIMEOUT (1*HZ)
47 #define I2C_DAVINCI_INTR_ALL (DAVINCI_I2C_IMR_AAS | \
48 DAVINCI_I2C_IMR_SCD | \
49 DAVINCI_I2C_IMR_ARDY | \
50 DAVINCI_I2C_IMR_NACK | \
53 #define DAVINCI_I2C_OAR_REG 0x00
54 #define DAVINCI_I2C_IMR_REG 0x04
55 #define DAVINCI_I2C_STR_REG 0x08
56 #define DAVINCI_I2C_CLKL_REG 0x0c
57 #define DAVINCI_I2C_CLKH_REG 0x10
58 #define DAVINCI_I2C_CNT_REG 0x14
59 #define DAVINCI_I2C_DRR_REG 0x18
60 #define DAVINCI_I2C_SAR_REG 0x1c
61 #define DAVINCI_I2C_DXR_REG 0x20
62 #define DAVINCI_I2C_MDR_REG 0x24
63 #define DAVINCI_I2C_IVR_REG 0x28
64 #define DAVINCI_I2C_EMDR_REG 0x2c
65 #define DAVINCI_I2C_PSC_REG 0x30
67 #define DAVINCI_I2C_IVR_AAS 0x07
68 #define DAVINCI_I2C_IVR_SCD 0x06
69 #define DAVINCI_I2C_IVR_XRDY 0x05
70 #define DAVINCI_I2C_IVR_RDR 0x04
71 #define DAVINCI_I2C_IVR_ARDY 0x03
72 #define DAVINCI_I2C_IVR_NACK 0x02
73 #define DAVINCI_I2C_IVR_AL 0x01
75 #define DAVINCI_I2C_STR_BB (1 << 12)
76 #define DAVINCI_I2C_STR_RSFULL (1 << 11)
77 #define DAVINCI_I2C_STR_SCD (1 << 5)
78 #define DAVINCI_I2C_STR_ARDY (1 << 2)
79 #define DAVINCI_I2C_STR_NACK (1 << 1)
80 #define DAVINCI_I2C_STR_AL (1 << 0)
82 #define DAVINCI_I2C_MDR_NACK (1 << 15)
83 #define DAVINCI_I2C_MDR_STT (1 << 13)
84 #define DAVINCI_I2C_MDR_STP (1 << 11)
85 #define DAVINCI_I2C_MDR_MST (1 << 10)
86 #define DAVINCI_I2C_MDR_TRX (1 << 9)
87 #define DAVINCI_I2C_MDR_XA (1 << 8)
88 #define DAVINCI_I2C_MDR_RM (1 << 7)
89 #define DAVINCI_I2C_MDR_IRS (1 << 5)
91 #define DAVINCI_I2C_IMR_AAS (1 << 6)
92 #define DAVINCI_I2C_IMR_SCD (1 << 5)
93 #define DAVINCI_I2C_IMR_XRDY (1 << 4)
94 #define DAVINCI_I2C_IMR_RRDY (1 << 3)
95 #define DAVINCI_I2C_IMR_ARDY (1 << 2)
96 #define DAVINCI_I2C_IMR_NACK (1 << 1)
97 #define DAVINCI_I2C_IMR_AL (1 << 0)
99 #define MOD_REG_BIT(val, mask, set) do { \
107 struct davinci_i2c_dev {
110 struct completion cmd_complete;
117 struct i2c_adapter adapter;
120 /* default platform data to use if not supplied in the platform_device */
121 static struct davinci_i2c_platform_data davinci_i2c_platform_data_default = {
126 static inline void davinci_i2c_write_reg(struct davinci_i2c_dev *i2c_dev,
129 __raw_writew(val, i2c_dev->base + reg);
132 static inline u16 davinci_i2c_read_reg(struct davinci_i2c_dev *i2c_dev, int reg)
134 return __raw_readw(i2c_dev->base + reg);
138 * This functions configures I2C and brings I2C out of reset.
139 * This function is called during I2C init function. This function
140 * also gets called if I2C encounters any errors.
142 static int i2c_davinci_init(struct davinci_i2c_dev *dev)
144 struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
150 u32 input_clock = clk_get_rate(dev->clk);
154 pdata = &davinci_i2c_platform_data_default;
156 /* put I2C into reset */
157 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
158 MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 0);
159 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
161 /* NOTE: I2C Clock divider programming info
162 * As per I2C specs the following formulas provide prescaler
163 * and low/high divider values
164 * input clk --> PSC Div -----------> ICCL/H Div --> output clock
167 * output clk = module clk / (PSC + 1) [ (ICCL + d) + (ICCH + d) ]
170 * (ICCL + ICCH) = clk = (input clk / ((psc +1) * output clk)) - 2d;
172 * where if PSC == 0, d = 7,
177 /* get minimum of 7 MHz clock, but max of 12 MHz */
178 psc = (input_clock / 7000000) - 1;
179 if ((input_clock / (psc + 1)) > 12000000)
180 psc++; /* better to run under spec than over */
181 d = (psc >= 2) ? 5 : 7 - psc;
183 clk = ((input_clock / (psc + 1)) / (pdata->bus_freq * 1000)) - (d << 1);
187 davinci_i2c_write_reg(dev, DAVINCI_I2C_PSC_REG, psc);
188 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKH_REG, clkh);
189 davinci_i2c_write_reg(dev, DAVINCI_I2C_CLKL_REG, clkl);
191 dev_dbg(dev->dev, "input_clock = %d, CLK = %d\n", input_clock, clk);
192 dev_dbg(dev->dev, "PSC = %d\n",
193 davinci_i2c_read_reg(dev, DAVINCI_I2C_PSC_REG));
194 dev_dbg(dev->dev, "CLKL = %d\n",
195 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKL_REG));
196 dev_dbg(dev->dev, "CLKH = %d\n",
197 davinci_i2c_read_reg(dev, DAVINCI_I2C_CLKH_REG));
198 dev_dbg(dev->dev, "bus_freq = %dkHz, bus_delay = %d\n",
199 pdata->bus_freq, pdata->bus_delay);
201 /* Take the I2C module out of reset: */
202 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
203 MOD_REG_BIT(w, DAVINCI_I2C_MDR_IRS, 1);
204 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
206 /* Enable interrupts */
207 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, I2C_DAVINCI_INTR_ALL);
213 * Waiting for bus not busy
215 static int i2c_davinci_wait_bus_not_busy(struct davinci_i2c_dev *dev,
218 unsigned long timeout;
220 timeout = jiffies + DAVINCI_I2C_TIMEOUT;
221 while (davinci_i2c_read_reg(dev, DAVINCI_I2C_STR_REG)
222 & DAVINCI_I2C_STR_BB) {
223 if (time_after(jiffies, timeout)) {
225 "timeout waiting for bus ready\n");
236 * Low level master read/write transaction. This function is called
237 * from i2c_davinci_xfer.
240 i2c_davinci_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg, int stop)
242 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
243 struct davinci_i2c_platform_data *pdata = dev->dev->platform_data;
252 pdata = &davinci_i2c_platform_data_default;
253 /* Introduce a delay, required for some boards (e.g Davinci EVM) */
254 if (pdata->bus_delay)
255 udelay(pdata->bus_delay);
257 /* set the slave address */
258 davinci_i2c_write_reg(dev, DAVINCI_I2C_SAR_REG, msg->addr);
261 dev->buf_len = msg->len;
263 davinci_i2c_write_reg(dev, DAVINCI_I2C_CNT_REG, dev->buf_len);
265 INIT_COMPLETION(dev->cmd_complete);
268 /* Take I2C out of reset, configure it as master and set the
270 flag = DAVINCI_I2C_MDR_IRS | DAVINCI_I2C_MDR_MST | DAVINCI_I2C_MDR_STT;
272 /* if the slave address is ten bit address, enable XA bit */
273 if (msg->flags & I2C_M_TEN)
274 flag |= DAVINCI_I2C_MDR_XA;
275 if (!(msg->flags & I2C_M_RD))
276 flag |= DAVINCI_I2C_MDR_TRX;
278 flag |= DAVINCI_I2C_MDR_STP;
280 /* Enable receive or transmit interrupts */
281 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_IMR_REG);
282 if (msg->flags & I2C_M_RD)
283 MOD_REG_BIT(w, DAVINCI_I2C_IMR_RRDY, 1);
285 MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 1);
286 davinci_i2c_write_reg(dev, DAVINCI_I2C_IMR_REG, w);
289 /* write the data into mode register */
290 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, flag);
292 r = wait_for_completion_interruptible_timeout(&dev->cmd_complete,
293 DAVINCI_I2C_TIMEOUT);
295 dev_err(dev->dev, "controller timed out\n");
296 i2c_davinci_init(dev);
301 /* This should be 0 if all bytes were transferred
302 * or dev->cmd_err denotes an error.
303 * A signal may have aborted the transfer.
306 dev_err(dev->dev, "abnormal termination buf_len=%i\n",
318 if (likely(!dev->cmd_err))
321 /* We have an error */
322 if (dev->cmd_err & DAVINCI_I2C_STR_AL) {
323 i2c_davinci_init(dev);
327 if (dev->cmd_err & DAVINCI_I2C_STR_NACK) {
328 if (msg->flags & I2C_M_IGNORE_NAK)
331 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
332 MOD_REG_BIT(w, DAVINCI_I2C_MDR_STP, 1);
333 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
341 * Prepare controller for a transaction and call i2c_davinci_xfer_msg
344 i2c_davinci_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
346 struct davinci_i2c_dev *dev = i2c_get_adapdata(adap);
350 dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
352 ret = i2c_davinci_wait_bus_not_busy(dev, 1);
354 dev_warn(dev->dev, "timeout waiting for bus ready\n");
358 for (i = 0; i < num; i++) {
359 ret = i2c_davinci_xfer_msg(adap, &msgs[i], (i == (num - 1)));
360 dev_dbg(dev->dev, "%s [%d/%d] ret: %d\n", __func__, i + 1, num,
368 static u32 i2c_davinci_func(struct i2c_adapter *adap)
370 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
373 static void terminate_read(struct davinci_i2c_dev *dev)
375 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
376 w |= DAVINCI_I2C_MDR_NACK;
377 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
379 /* Throw away data */
380 davinci_i2c_read_reg(dev, DAVINCI_I2C_DRR_REG);
382 dev_err(dev->dev, "RDR IRQ while no data requested\n");
384 static void terminate_write(struct davinci_i2c_dev *dev)
386 u16 w = davinci_i2c_read_reg(dev, DAVINCI_I2C_MDR_REG);
387 w |= DAVINCI_I2C_MDR_RM | DAVINCI_I2C_MDR_STP;
388 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, w);
391 dev_err(dev->dev, "TDR IRQ while no data to send\n");
395 * Interrupt service routine. This gets called whenever an I2C interrupt
398 static irqreturn_t i2c_davinci_isr(int this_irq, void *dev_id)
400 struct davinci_i2c_dev *dev = dev_id;
405 while ((stat = davinci_i2c_read_reg(dev, DAVINCI_I2C_IVR_REG))) {
406 dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
407 if (count++ == 100) {
408 dev_warn(dev->dev, "Too much work in one IRQ\n");
413 case DAVINCI_I2C_IVR_AL:
414 /* Arbitration lost, must retry */
415 dev->cmd_err |= DAVINCI_I2C_STR_AL;
417 complete(&dev->cmd_complete);
420 case DAVINCI_I2C_IVR_NACK:
421 dev->cmd_err |= DAVINCI_I2C_STR_NACK;
423 complete(&dev->cmd_complete);
426 case DAVINCI_I2C_IVR_ARDY:
427 davinci_i2c_write_reg(dev,
428 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_ARDY);
429 complete(&dev->cmd_complete);
432 case DAVINCI_I2C_IVR_RDR:
435 davinci_i2c_read_reg(dev,
436 DAVINCI_I2C_DRR_REG);
441 davinci_i2c_write_reg(dev,
443 DAVINCI_I2C_IMR_RRDY);
445 /* signal can terminate transfer */
450 case DAVINCI_I2C_IVR_XRDY:
452 davinci_i2c_write_reg(dev, DAVINCI_I2C_DXR_REG,
458 w = davinci_i2c_read_reg(dev,
459 DAVINCI_I2C_IMR_REG);
460 MOD_REG_BIT(w, DAVINCI_I2C_IMR_XRDY, 0);
461 davinci_i2c_write_reg(dev,
465 /* signal can terminate transfer */
466 terminate_write(dev);
470 case DAVINCI_I2C_IVR_SCD:
471 davinci_i2c_write_reg(dev,
472 DAVINCI_I2C_STR_REG, DAVINCI_I2C_STR_SCD);
473 complete(&dev->cmd_complete);
476 case DAVINCI_I2C_IVR_AAS:
477 dev_warn(dev->dev, "Address as slave interrupt\n");
481 return count ? IRQ_HANDLED : IRQ_NONE;
484 static struct i2c_algorithm i2c_davinci_algo = {
485 .master_xfer = i2c_davinci_xfer,
486 .functionality = i2c_davinci_func,
489 static int davinci_i2c_probe(struct platform_device *pdev)
491 struct davinci_i2c_dev *dev;
492 struct i2c_adapter *adap;
493 struct resource *mem, *irq, *ioarea;
496 /* NOTE: driver uses the static register mapping */
497 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
499 dev_err(&pdev->dev, "no mem resource?\n");
503 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
505 dev_err(&pdev->dev, "no irq resource?\n");
509 ioarea = request_mem_region(mem->start, (mem->end - mem->start) + 1,
512 dev_err(&pdev->dev, "I2C region already claimed\n");
516 dev = kzalloc(sizeof(struct davinci_i2c_dev), GFP_KERNEL);
519 goto err_release_region;
522 init_completion(&dev->cmd_complete);
523 dev->dev = get_device(&pdev->dev);
524 dev->irq = irq->start;
525 platform_set_drvdata(pdev, dev);
527 dev->clk = clk_get(&pdev->dev, "I2CCLK");
528 if (IS_ERR(dev->clk)) {
532 clk_enable(dev->clk);
534 dev->base = (void __iomem *)IO_ADDRESS(mem->start);
535 i2c_davinci_init(dev);
537 r = request_irq(dev->irq, i2c_davinci_isr, 0, pdev->name, dev);
539 dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
540 goto err_unuse_clocks;
543 adap = &dev->adapter;
544 i2c_set_adapdata(adap, dev);
545 adap->owner = THIS_MODULE;
546 adap->class = I2C_CLASS_HWMON;
547 strlcpy(adap->name, "DaVinci I2C adapter", sizeof(adap->name));
548 adap->algo = &i2c_davinci_algo;
549 adap->dev.parent = &pdev->dev;
555 r = i2c_add_numbered_adapter(adap);
557 dev_err(&pdev->dev, "failure adding adapter\n");
564 free_irq(dev->irq, dev);
566 clk_disable(dev->clk);
570 platform_set_drvdata(pdev, NULL);
571 put_device(&pdev->dev);
574 release_mem_region(mem->start, (mem->end - mem->start) + 1);
579 static int davinci_i2c_remove(struct platform_device *pdev)
581 struct davinci_i2c_dev *dev = platform_get_drvdata(pdev);
582 struct resource *mem;
584 platform_set_drvdata(pdev, NULL);
585 i2c_del_adapter(&dev->adapter);
586 put_device(&pdev->dev);
588 clk_disable(dev->clk);
592 davinci_i2c_write_reg(dev, DAVINCI_I2C_MDR_REG, 0);
593 free_irq(IRQ_I2C, dev);
596 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
597 release_mem_region(mem->start, (mem->end - mem->start) + 1);
601 /* work with hotplug and coldplug */
602 MODULE_ALIAS("platform:i2c_davinci");
604 static struct platform_driver davinci_i2c_driver = {
605 .probe = davinci_i2c_probe,
606 .remove = davinci_i2c_remove,
608 .name = "i2c_davinci",
609 .owner = THIS_MODULE,
613 /* I2C may be needed to bring up other drivers */
614 static int __init davinci_i2c_init_driver(void)
616 return platform_driver_register(&davinci_i2c_driver);
618 subsys_initcall(davinci_i2c_init_driver);
620 static void __exit davinci_i2c_exit_driver(void)
622 platform_driver_unregister(&davinci_i2c_driver);
624 module_exit(davinci_i2c_exit_driver);
626 MODULE_AUTHOR("Texas Instruments India");
627 MODULE_DESCRIPTION("TI DaVinci I2C bus adapter");
628 MODULE_LICENSE("GPL");