blob: ab59b7e492eda3b36de291b53fcb4ac2eb3fc110 [file] [log] [blame]
Russell Kingb652b432005-06-15 12:38:14 +01001/*
2 * i2c_adap_pxa.c
3 *
4 * I2C adapter for the PXA I2C bus access.
5 *
6 * Copyright (C) 2002 Intrinsyc Software Inc.
7 * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * History:
14 * Apr 2002: Initial version [CS]
Daniel Mack3ad2f3f2010-02-03 08:01:28 +080015 * Jun 2002: Properly separated algo/adap [FB]
Russell Kingb652b432005-06-15 12:38:14 +010016 * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
17 * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
18 * Sep 2004: Major rework to ensure efficient bus handling [RMK]
19 * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
20 * Feb 2005: Rework slave mode handling [RMK]
21 */
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/i2c.h>
Russell Kingb652b432005-06-15 12:38:14 +010025#include <linux/init.h>
26#include <linux/time.h>
27#include <linux/sched.h>
28#include <linux/delay.h>
29#include <linux/errno.h>
30#include <linux/interrupt.h>
31#include <linux/i2c-pxa.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
Russell Kingc3cef3f2007-08-20 10:19:10 +010033#include <linux/err.h>
34#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
H Hartley Sweeten21782182010-05-21 18:41:01 +020036#include <linux/io.h>
Sebastian Andrzej Siewiorb4593962011-02-23 12:38:16 +010037#include <linux/i2c/pxa-i2c.h>
Russell Kingb652b432005-06-15 12:38:14 +010038
Russell Kingb652b432005-06-15 12:38:14 +010039#include <asm/irq.h>
Eric Miao283afa02008-09-08 14:15:08 +080040
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010041#ifndef CONFIG_HAVE_CLK
42#define clk_get(dev, id) NULL
43#define clk_put(clk) do { } while (0)
44#define clk_disable(clk) do { } while (0)
45#define clk_enable(clk) do { } while (0)
46#endif
47
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010048struct pxa_reg_layout {
49 u32 ibmr;
50 u32 idbr;
51 u32 icr;
52 u32 isr;
53 u32 isar;
54};
55
56enum pxa_i2c_types {
57 REGS_PXA2XX,
58 REGS_PXA3XX,
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010059 REGS_CE4100,
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010060};
61
Eric Miao283afa02008-09-08 14:15:08 +080062/*
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010063 * I2C registers definitions
Eric Miaof23d4912009-04-13 14:43:25 +080064 */
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010065static struct pxa_reg_layout pxa_reg_layout[] = {
66 [REGS_PXA2XX] = {
67 .ibmr = 0x00,
68 .idbr = 0x10,
69 .icr = 0x20,
70 .isr = 0x30,
71 .isar = 0x40,
72 },
73 [REGS_PXA3XX] = {
74 .ibmr = 0x00,
75 .idbr = 0x08,
76 .icr = 0x10,
77 .isr = 0x18,
78 .isar = 0x20,
79 },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010080 [REGS_CE4100] = {
81 .ibmr = 0x14,
82 .idbr = 0x0c,
83 .icr = 0x00,
84 .isr = 0x04,
85 /* no isar register */
86 },
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010087};
Eric Miaof23d4912009-04-13 14:43:25 +080088
89static const struct platform_device_id i2c_pxa_id_table[] = {
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010090 { "pxa2xx-i2c", REGS_PXA2XX },
91 { "pxa3xx-pwri2c", REGS_PXA3XX },
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +010092 { "ce4100-i2c", REGS_CE4100 },
Eric Miaof23d4912009-04-13 14:43:25 +080093 { },
94};
95MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);
96
97/*
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +010098 * I2C bit definitions
Eric Miao283afa02008-09-08 14:15:08 +080099 */
Eric Miao283afa02008-09-08 14:15:08 +0800100
101#define ICR_START (1 << 0) /* start bit */
102#define ICR_STOP (1 << 1) /* stop bit */
103#define ICR_ACKNAK (1 << 2) /* send ACK(0) or NAK(1) */
104#define ICR_TB (1 << 3) /* transfer byte bit */
105#define ICR_MA (1 << 4) /* master abort */
106#define ICR_SCLE (1 << 5) /* master clock enable */
107#define ICR_IUE (1 << 6) /* unit enable */
108#define ICR_GCD (1 << 7) /* general call disable */
109#define ICR_ITEIE (1 << 8) /* enable tx interrupts */
110#define ICR_IRFIE (1 << 9) /* enable rx interrupts */
111#define ICR_BEIE (1 << 10) /* enable bus error ints */
112#define ICR_SSDIE (1 << 11) /* slave STOP detected int enable */
113#define ICR_ALDIE (1 << 12) /* enable arbitration interrupt */
114#define ICR_SADIE (1 << 13) /* slave address detected int enable */
115#define ICR_UR (1 << 14) /* unit reset */
116#define ICR_FM (1 << 15) /* fast mode */
117
118#define ISR_RWM (1 << 0) /* read/write mode */
119#define ISR_ACKNAK (1 << 1) /* ack/nak status */
120#define ISR_UB (1 << 2) /* unit busy */
121#define ISR_IBB (1 << 3) /* bus busy */
122#define ISR_SSD (1 << 4) /* slave stop detected */
123#define ISR_ALD (1 << 5) /* arbitration loss detected */
124#define ISR_ITE (1 << 6) /* tx buffer empty */
125#define ISR_IRF (1 << 7) /* rx buffer full */
126#define ISR_GCAD (1 << 8) /* general call address detected */
127#define ISR_SAD (1 << 9) /* slave address detected */
128#define ISR_BED (1 << 10) /* bus error no ACK/NAK */
Russell Kingb652b432005-06-15 12:38:14 +0100129
130struct pxa_i2c {
131 spinlock_t lock;
132 wait_queue_head_t wait;
133 struct i2c_msg *msg;
134 unsigned int msg_num;
135 unsigned int msg_idx;
136 unsigned int msg_ptr;
137 unsigned int slave_addr;
138
139 struct i2c_adapter adap;
Russell Kingc3cef3f2007-08-20 10:19:10 +0100140 struct clk *clk;
Russell Kingb652b432005-06-15 12:38:14 +0100141#ifdef CONFIG_I2C_PXA_SLAVE
142 struct i2c_slave_client *slave;
143#endif
144
145 unsigned int irqlogidx;
146 u32 isrlog[32];
147 u32 icrlog[32];
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100148
149 void __iomem *reg_base;
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100150 void __iomem *reg_ibmr;
151 void __iomem *reg_idbr;
152 void __iomem *reg_icr;
153 void __iomem *reg_isr;
154 void __iomem *reg_isar;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100155
156 unsigned long iobase;
157 unsigned long iosize;
158
159 int irq;
Jonathan Cameronc46c9482008-10-03 15:07:36 +0100160 unsigned int use_pio :1;
161 unsigned int fast_mode :1;
Russell Kingb652b432005-06-15 12:38:14 +0100162};
163
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +0100164#define _IBMR(i2c) ((i2c)->reg_ibmr)
165#define _IDBR(i2c) ((i2c)->reg_idbr)
166#define _ICR(i2c) ((i2c)->reg_icr)
167#define _ISR(i2c) ((i2c)->reg_isr)
168#define _ISAR(i2c) ((i2c)->reg_isar)
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100169
Russell Kingb652b432005-06-15 12:38:14 +0100170/*
171 * I2C Slave mode address
172 */
173#define I2C_PXA_SLAVE_ADDR 0x1
174
Russell Kingb652b432005-06-15 12:38:14 +0100175#ifdef DEBUG
176
177struct bits {
178 u32 mask;
179 const char *set;
180 const char *unset;
181};
Jiri Slabyed113992007-10-18 23:40:28 -0700182#define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
Russell Kingb652b432005-06-15 12:38:14 +0100183
184static inline void
185decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
186{
187 printk("%s %08x: ", prefix, val);
188 while (num--) {
189 const char *str = val & bits->mask ? bits->set : bits->unset;
190 if (str)
191 printk("%s ", str);
192 bits++;
193 }
194}
195
196static const struct bits isr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700197 PXA_BIT(ISR_RWM, "RX", "TX"),
198 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
199 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
200 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
201 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
202 PXA_BIT(ISR_ALD, "ALD", NULL),
203 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
204 PXA_BIT(ISR_IRF, "RxFull", NULL),
205 PXA_BIT(ISR_GCAD, "GenCall", NULL),
206 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
207 PXA_BIT(ISR_BED, "BusErr", NULL),
Russell Kingb652b432005-06-15 12:38:14 +0100208};
209
210static void decode_ISR(unsigned int val)
211{
Russell King6fd60fa2005-09-08 21:04:58 +0100212 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100213 printk("\n");
214}
215
216static const struct bits icr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700217 PXA_BIT(ICR_START, "START", NULL),
218 PXA_BIT(ICR_STOP, "STOP", NULL),
219 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
220 PXA_BIT(ICR_TB, "TB", NULL),
221 PXA_BIT(ICR_MA, "MA", NULL),
222 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
223 PXA_BIT(ICR_IUE, "IUE", "iue"),
224 PXA_BIT(ICR_GCD, "GCD", NULL),
225 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
226 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
227 PXA_BIT(ICR_BEIE, "BEIE", NULL),
228 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
229 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
230 PXA_BIT(ICR_SADIE, "SADIE", NULL),
231 PXA_BIT(ICR_UR, "UR", "ur"),
Russell Kingb652b432005-06-15 12:38:14 +0100232};
233
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100234#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +0100235static void decode_ICR(unsigned int val)
236{
Russell King6fd60fa2005-09-08 21:04:58 +0100237 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100238 printk("\n");
239}
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100240#endif
Russell Kingb652b432005-06-15 12:38:14 +0100241
242static unsigned int i2c_debug = DEBUG;
243
244static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
245{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100246 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
247 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100248}
249
Harvey Harrison08882d22008-04-22 22:16:47 +0200250#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
Russell Kingb652b432005-06-15 12:38:14 +0100251
252static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
253{
254 unsigned int i;
Frank Seidel154d22b2009-03-28 21:34:42 +0100255 printk(KERN_ERR "i2c: error: %s\n", why);
256 printk(KERN_ERR "i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
Russell Kingb652b432005-06-15 12:38:14 +0100257 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
Frank Seidel154d22b2009-03-28 21:34:42 +0100258 printk(KERN_ERR "i2c: ICR: %08x ISR: %08x\n",
259 readl(_ICR(i2c)), readl(_ISR(i2c)));
260 printk(KERN_DEBUG "i2c: log: ");
Russell Kingb652b432005-06-15 12:38:14 +0100261 for (i = 0; i < i2c->irqlogidx; i++)
262 printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
263 printk("\n");
264}
265
Wolfram Sang0d813d92009-11-03 12:53:41 +0100266#else /* ifdef DEBUG */
267
268#define i2c_debug 0
269
270#define show_state(i2c) do { } while (0)
271#define decode_ISR(val) do { } while (0)
272#define decode_ICR(val) do { } while (0)
273#define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)
274
275#endif /* ifdef DEBUG / else */
276
277static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
278static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
279
Russell Kingb652b432005-06-15 12:38:14 +0100280static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
281{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100282 return !(readl(_ICR(i2c)) & ICR_SCLE);
Russell Kingb652b432005-06-15 12:38:14 +0100283}
284
285static void i2c_pxa_abort(struct pxa_i2c *i2c)
286{
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100287 int i = 250;
Russell Kingb652b432005-06-15 12:38:14 +0100288
289 if (i2c_pxa_is_slavemode(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100290 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100291 return;
292 }
293
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100294 while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100295 unsigned long icr = readl(_ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100296
297 icr &= ~ICR_START;
298 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
299
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100300 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100301
302 show_state(i2c);
303
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100304 mdelay(1);
305 i --;
Russell Kingb652b432005-06-15 12:38:14 +0100306 }
307
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100308 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
309 _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100310}
311
312static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
313{
314 int timeout = DEF_TIMEOUT;
315
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100316 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
317 if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
Russell Kingb652b432005-06-15 12:38:14 +0100318 timeout += 4;
319
320 msleep(2);
321 show_state(i2c);
322 }
323
Roel Kluind10db3a2009-04-23 16:27:39 +0200324 if (timeout < 0)
Russell Kingb652b432005-06-15 12:38:14 +0100325 show_state(i2c);
326
Roel Kluind10db3a2009-04-23 16:27:39 +0200327 return timeout < 0 ? I2C_RETRY : 0;
Russell Kingb652b432005-06-15 12:38:14 +0100328}
329
330static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
331{
332 unsigned long timeout = jiffies + HZ*4;
333
334 while (time_before(jiffies, timeout)) {
335 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100336 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100337 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100338
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100339 if (readl(_ISR(i2c)) & ISR_SAD) {
Russell Kingb652b432005-06-15 12:38:14 +0100340 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100341 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100342 goto out;
343 }
344
345 /* wait for unit and bus being not busy, and we also do a
346 * quick check of the i2c lines themselves to ensure they've
347 * gone high...
348 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100349 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
Russell Kingb652b432005-06-15 12:38:14 +0100350 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100351 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100352 return 1;
353 }
354
355 msleep(1);
356 }
357
358 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100359 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100360 out:
361 return 0;
362}
363
364static int i2c_pxa_set_master(struct pxa_i2c *i2c)
365{
366 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100367 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
Russell Kingb652b432005-06-15 12:38:14 +0100368
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100369 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100370 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100371 if (!i2c_pxa_wait_master(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100372 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100373 return I2C_RETRY;
374 }
375 }
376
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100377 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100378 return 0;
379}
380
381#ifdef CONFIG_I2C_PXA_SLAVE
382static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
383{
384 unsigned long timeout = jiffies + HZ*1;
385
386 /* wait for stop */
387
388 show_state(i2c);
389
390 while (time_before(jiffies, timeout)) {
391 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100392 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100393 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100394
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100395 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
396 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
397 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
Russell Kingb652b432005-06-15 12:38:14 +0100398 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100399 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100400 return 1;
401 }
402
403 msleep(1);
404 }
405
406 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100407 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100408 return 0;
409}
410
411/*
412 * clear the hold on the bus, and take of anything else
413 * that has been configured
414 */
415static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
416{
417 show_state(i2c);
418
419 if (errcode < 0) {
420 udelay(100); /* simple delay */
421 } else {
422 /* we need to wait for the stop condition to end */
423
424 /* if we where in stop, then clear... */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100425 if (readl(_ICR(i2c)) & ICR_STOP) {
Russell Kingb652b432005-06-15 12:38:14 +0100426 udelay(100);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100427 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100428 }
429
430 if (!i2c_pxa_wait_slave(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100431 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
432 __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100433 return;
434 }
435 }
436
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100437 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
438 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100439
440 if (i2c_debug) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100441 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
442 decode_ICR(readl(_ICR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100443 }
444}
445#else
446#define i2c_pxa_set_slave(i2c, err) do { } while (0)
447#endif
448
449static void i2c_pxa_reset(struct pxa_i2c *i2c)
450{
451 pr_debug("Resetting I2C Controller Unit\n");
452
453 /* abort any transfer currently under way */
454 i2c_pxa_abort(i2c);
455
456 /* reset according to 9.8 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100457 writel(ICR_UR, _ICR(i2c));
458 writel(I2C_ISR_INIT, _ISR(i2c));
459 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100460
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +0100461 if (i2c->reg_isar)
462 writel(i2c->slave_addr, _ISAR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100463
464 /* set control register values */
Jonathan Cameronc46c9482008-10-03 15:07:36 +0100465 writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100466
467#ifdef CONFIG_I2C_PXA_SLAVE
Russell King6fd60fa2005-09-08 21:04:58 +0100468 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100469 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100470#endif
471
472 i2c_pxa_set_slave(i2c, 0);
473
474 /* enable unit */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100475 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100476 udelay(100);
477}
478
479
480#ifdef CONFIG_I2C_PXA_SLAVE
481/*
Russell Kingb652b432005-06-15 12:38:14 +0100482 * PXA I2C Slave mode
483 */
484
485static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
486{
487 if (isr & ISR_BED) {
488 /* what should we do here? */
489 } else {
Russell King84b5abe2006-10-28 22:30:17 +0100490 int ret = 0;
491
492 if (i2c->slave != NULL)
493 ret = i2c->slave->read(i2c->slave->data);
Russell Kingb652b432005-06-15 12:38:14 +0100494
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100495 writel(ret, _IDBR(i2c));
496 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
Russell Kingb652b432005-06-15 12:38:14 +0100497 }
498}
499
500static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
501{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100502 unsigned int byte = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100503
504 if (i2c->slave != NULL)
505 i2c->slave->write(i2c->slave->data, byte);
506
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100507 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100508}
509
510static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
511{
512 int timeout;
513
514 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100515 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
Russell Kingb652b432005-06-15 12:38:14 +0100516 (isr & ISR_RWM) ? 'r' : 't');
517
518 if (i2c->slave != NULL)
519 i2c->slave->event(i2c->slave->data,
520 (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
521
522 /*
523 * slave could interrupt in the middle of us generating a
524 * start condition... if this happens, we'd better back off
525 * and stop holding the poor thing up
526 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100527 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
528 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100529
530 timeout = 0x10000;
531
532 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100533 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100534 break;
535
536 timeout--;
537
538 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100539 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100540 break;
541 }
542 }
543
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100544 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100545}
546
547static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
548{
549 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100550 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
Russell Kingb652b432005-06-15 12:38:14 +0100551
552 if (i2c->slave != NULL)
553 i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
554
555 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100556 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
Russell Kingb652b432005-06-15 12:38:14 +0100557
558 /*
559 * If we have a master-mode message waiting,
560 * kick it off now that the slave has completed.
561 */
562 if (i2c->msg)
563 i2c_pxa_master_complete(i2c, I2C_RETRY);
564}
565#else
566static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
567{
568 if (isr & ISR_BED) {
569 /* what should we do here? */
570 } else {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100571 writel(0, _IDBR(i2c));
572 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100573 }
574}
575
576static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
577{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100578 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100579}
580
581static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
582{
583 int timeout;
584
585 /*
586 * slave could interrupt in the middle of us generating a
587 * start condition... if this happens, we'd better back off
588 * and stop holding the poor thing up
589 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100590 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
591 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100592
593 timeout = 0x10000;
594
595 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100596 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100597 break;
598
599 timeout--;
600
601 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100602 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100603 break;
604 }
605 }
606
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100607 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100608}
609
610static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
611{
612 if (i2c->msg)
613 i2c_pxa_master_complete(i2c, I2C_RETRY);
614}
615#endif
616
617/*
618 * PXA I2C Master mode
619 */
620
621static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
622{
623 unsigned int addr = (msg->addr & 0x7f) << 1;
624
625 if (msg->flags & I2C_M_RD)
626 addr |= 1;
627
628 return addr;
629}
630
631static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
632{
633 u32 icr;
634
635 /*
636 * Step 1: target slave address into IDBR
637 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100638 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100639
640 /*
641 * Step 2: initiate the write.
642 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100643 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
644 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100645}
646
Jean Delvare7d054812007-05-01 23:26:33 +0200647static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
648{
649 u32 icr;
650
651 /*
652 * Clear the STOP and ACK flags
653 */
654 icr = readl(_ICR(i2c));
655 icr &= ~(ICR_STOP | ICR_ACKNAK);
Russell King0cfe61e2007-05-10 03:15:32 -0700656 writel(icr, _ICR(i2c));
Jean Delvare7d054812007-05-01 23:26:33 +0200657}
658
Mike Rapoportb7a36702008-01-27 18:14:50 +0100659static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
660{
661 /* make timeout the same as for interrupt based functions */
662 long timeout = 2 * DEF_TIMEOUT;
663
664 /*
665 * Wait for the bus to become free.
666 */
667 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
668 udelay(1000);
669 show_state(i2c);
670 }
671
Roel Kluind10db3a2009-04-23 16:27:39 +0200672 if (timeout < 0) {
Mike Rapoportb7a36702008-01-27 18:14:50 +0100673 show_state(i2c);
674 dev_err(&i2c->adap.dev,
675 "i2c_pxa: timeout waiting for bus free\n");
676 return I2C_RETRY;
677 }
678
679 /*
680 * Set master mode.
681 */
682 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
683
684 return 0;
685}
686
687static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
688 struct i2c_msg *msg, int num)
689{
690 unsigned long timeout = 500000; /* 5 seconds */
691 int ret = 0;
692
693 ret = i2c_pxa_pio_set_master(i2c);
694 if (ret)
695 goto out;
696
697 i2c->msg = msg;
698 i2c->msg_num = num;
699 i2c->msg_idx = 0;
700 i2c->msg_ptr = 0;
701 i2c->irqlogidx = 0;
702
703 i2c_pxa_start_message(i2c);
704
Roel Kluina746b572009-02-24 19:19:48 +0100705 while (i2c->msg_num > 0 && --timeout) {
Mike Rapoportb7a36702008-01-27 18:14:50 +0100706 i2c_pxa_handler(0, i2c);
707 udelay(10);
708 }
709
710 i2c_pxa_stop_message(i2c);
711
712 /*
713 * We place the return code in i2c->msg_idx.
714 */
715 ret = i2c->msg_idx;
716
717out:
718 if (timeout == 0)
719 i2c_pxa_scream_blue_murder(i2c, "timeout");
720
721 return ret;
722}
723
Russell Kingb652b432005-06-15 12:38:14 +0100724/*
Jean Delvare3fb9a652006-01-18 23:17:01 +0100725 * We are protected by the adapter bus mutex.
Russell Kingb652b432005-06-15 12:38:14 +0100726 */
727static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
728{
729 long timeout;
730 int ret;
731
732 /*
733 * Wait for the bus to become free.
734 */
735 ret = i2c_pxa_wait_bus_not_busy(i2c);
736 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100737 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
Russell Kingb652b432005-06-15 12:38:14 +0100738 goto out;
739 }
740
741 /*
742 * Set master mode.
743 */
744 ret = i2c_pxa_set_master(i2c);
745 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100746 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
Russell Kingb652b432005-06-15 12:38:14 +0100747 goto out;
748 }
749
750 spin_lock_irq(&i2c->lock);
751
752 i2c->msg = msg;
753 i2c->msg_num = num;
754 i2c->msg_idx = 0;
755 i2c->msg_ptr = 0;
756 i2c->irqlogidx = 0;
757
758 i2c_pxa_start_message(i2c);
759
760 spin_unlock_irq(&i2c->lock);
761
762 /*
763 * The rest of the processing occurs in the interrupt handler.
764 */
765 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
Jean Delvare7d054812007-05-01 23:26:33 +0200766 i2c_pxa_stop_message(i2c);
Russell Kingb652b432005-06-15 12:38:14 +0100767
768 /*
769 * We place the return code in i2c->msg_idx.
770 */
771 ret = i2c->msg_idx;
772
773 if (timeout == 0)
774 i2c_pxa_scream_blue_murder(i2c, "timeout");
775
776 out:
777 return ret;
778}
779
Mike Rapoportb7a36702008-01-27 18:14:50 +0100780static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
781 struct i2c_msg msgs[], int num)
782{
783 struct pxa_i2c *i2c = adap->algo_data;
784 int ret, i;
785
786 /* If the I2C controller is disabled we need to reset it
787 (probably due to a suspend/resume destroying state). We do
788 this here as we can then avoid worrying about resuming the
789 controller before its users. */
790 if (!(readl(_ICR(i2c)) & ICR_IUE))
791 i2c_pxa_reset(i2c);
792
793 for (i = adap->retries; i >= 0; i--) {
794 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
795 if (ret != I2C_RETRY)
796 goto out;
797
798 if (i2c_debug)
799 dev_dbg(&adap->dev, "Retrying transmission\n");
800 udelay(100);
801 }
802 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
803 ret = -EREMOTEIO;
804 out:
805 i2c_pxa_set_slave(i2c, ret);
806 return ret;
807}
808
Russell Kingb652b432005-06-15 12:38:14 +0100809/*
810 * i2c_pxa_master_complete - complete the message and wake up.
811 */
812static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
813{
814 i2c->msg_ptr = 0;
815 i2c->msg = NULL;
816 i2c->msg_idx ++;
817 i2c->msg_num = 0;
818 if (ret)
819 i2c->msg_idx = ret;
Mike Rapoportb7a36702008-01-27 18:14:50 +0100820 if (!i2c->use_pio)
821 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +0100822}
823
824static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
825{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100826 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100827
828 again:
829 /*
830 * If ISR_ALD is set, we lost arbitration.
831 */
832 if (isr & ISR_ALD) {
833 /*
834 * Do we need to do anything here? The PXA docs
835 * are vague about what happens.
836 */
837 i2c_pxa_scream_blue_murder(i2c, "ALD set");
838
839 /*
840 * We ignore this error. We seem to see spurious ALDs
841 * for seemingly no reason. If we handle them as I think
842 * they should, we end up causing an I2C error, which
843 * is painful for some systems.
844 */
845 return; /* ignore */
846 }
847
848 if (isr & ISR_BED) {
849 int ret = BUS_ERROR;
850
851 /*
852 * I2C bus error - either the device NAK'd us, or
853 * something more serious happened. If we were NAK'd
854 * on the initial address phase, we can retry.
855 */
856 if (isr & ISR_ACKNAK) {
857 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
858 ret = I2C_RETRY;
859 else
860 ret = XFER_NAKED;
861 }
862 i2c_pxa_master_complete(i2c, ret);
863 } else if (isr & ISR_RWM) {
864 /*
865 * Read mode. We have just sent the address byte, and
866 * now we must initiate the transfer.
867 */
868 if (i2c->msg_ptr == i2c->msg->len - 1 &&
869 i2c->msg_idx == i2c->msg_num - 1)
870 icr |= ICR_STOP | ICR_ACKNAK;
871
872 icr |= ICR_ALDIE | ICR_TB;
873 } else if (i2c->msg_ptr < i2c->msg->len) {
874 /*
875 * Write mode. Write the next data byte.
876 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100877 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100878
879 icr |= ICR_ALDIE | ICR_TB;
880
881 /*
882 * If this is the last byte of the last message, send
883 * a STOP.
884 */
885 if (i2c->msg_ptr == i2c->msg->len &&
886 i2c->msg_idx == i2c->msg_num - 1)
887 icr |= ICR_STOP;
888 } else if (i2c->msg_idx < i2c->msg_num - 1) {
889 /*
890 * Next segment of the message.
891 */
892 i2c->msg_ptr = 0;
893 i2c->msg_idx ++;
894 i2c->msg++;
895
896 /*
897 * If we aren't doing a repeated start and address,
898 * go back and try to send the next byte. Note that
899 * we do not support switching the R/W direction here.
900 */
901 if (i2c->msg->flags & I2C_M_NOSTART)
902 goto again;
903
904 /*
905 * Write the next address.
906 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100907 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100908
909 /*
910 * And trigger a repeated start, and send the byte.
911 */
912 icr &= ~ICR_ALDIE;
913 icr |= ICR_START | ICR_TB;
914 } else {
915 if (i2c->msg->len == 0) {
916 /*
917 * Device probes have a message length of zero
918 * and need the bus to be reset before it can
919 * be used again.
920 */
921 i2c_pxa_reset(i2c);
922 }
923 i2c_pxa_master_complete(i2c, 0);
924 }
925
926 i2c->icrlog[i2c->irqlogidx-1] = icr;
927
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100928 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100929 show_state(i2c);
930}
931
932static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
933{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100934 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100935
936 /*
937 * Read the byte.
938 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100939 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100940
941 if (i2c->msg_ptr < i2c->msg->len) {
942 /*
943 * If this is the last byte of the last
944 * message, send a STOP.
945 */
946 if (i2c->msg_ptr == i2c->msg->len - 1)
947 icr |= ICR_STOP | ICR_ACKNAK;
948
949 icr |= ICR_ALDIE | ICR_TB;
950 } else {
951 i2c_pxa_master_complete(i2c, 0);
952 }
953
954 i2c->icrlog[i2c->irqlogidx-1] = icr;
955
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100956 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100957}
958
David Howells7d12e782006-10-05 14:55:46 +0100959static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
Russell Kingb652b432005-06-15 12:38:14 +0100960{
961 struct pxa_i2c *i2c = dev_id;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100962 u32 isr = readl(_ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100963
964 if (i2c_debug > 2 && 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100965 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100966 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100967 decode_ISR(isr);
968 }
969
Tobias Klauser7e3d7db2006-01-09 23:19:51 +0100970 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
Russell Kingb652b432005-06-15 12:38:14 +0100971 i2c->isrlog[i2c->irqlogidx++] = isr;
972
973 show_state(i2c);
974
975 /*
976 * Always clear all pending IRQs.
977 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100978 writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100979
980 if (isr & ISR_SAD)
981 i2c_pxa_slave_start(i2c, isr);
982 if (isr & ISR_SSD)
983 i2c_pxa_slave_stop(i2c);
984
985 if (i2c_pxa_is_slavemode(i2c)) {
986 if (isr & ISR_ITE)
987 i2c_pxa_slave_txempty(i2c, isr);
988 if (isr & ISR_IRF)
989 i2c_pxa_slave_rxfull(i2c, isr);
990 } else if (i2c->msg) {
991 if (isr & ISR_ITE)
992 i2c_pxa_irq_txempty(i2c, isr);
993 if (isr & ISR_IRF)
994 i2c_pxa_irq_rxfull(i2c, isr);
995 } else {
996 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
997 }
998
999 return IRQ_HANDLED;
1000}
1001
1002
1003static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
1004{
1005 struct pxa_i2c *i2c = adap->algo_data;
1006 int ret, i;
1007
1008 for (i = adap->retries; i >= 0; i--) {
1009 ret = i2c_pxa_do_xfer(i2c, msgs, num);
1010 if (ret != I2C_RETRY)
1011 goto out;
1012
1013 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +01001014 dev_dbg(&adap->dev, "Retrying transmission\n");
Russell Kingb652b432005-06-15 12:38:14 +01001015 udelay(100);
1016 }
1017 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
1018 ret = -EREMOTEIO;
1019 out:
1020 i2c_pxa_set_slave(i2c, ret);
1021 return ret;
1022}
1023
Russell Kingda16e322005-09-14 22:54:45 +01001024static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
1025{
1026 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1027}
1028
Jean Delvare8f9082c2006-09-03 22:39:46 +02001029static const struct i2c_algorithm i2c_pxa_algorithm = {
Russell Kingb652b432005-06-15 12:38:14 +01001030 .master_xfer = i2c_pxa_xfer,
Russell Kingda16e322005-09-14 22:54:45 +01001031 .functionality = i2c_pxa_functionality,
Russell Kingb652b432005-06-15 12:38:14 +01001032};
1033
Mike Rapoportb7a36702008-01-27 18:14:50 +01001034static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
1035 .master_xfer = i2c_pxa_pio_xfer,
1036 .functionality = i2c_pxa_functionality,
1037};
1038
Russell King3ae5eae2005-11-09 22:32:44 +00001039static int i2c_pxa_probe(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001040{
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001041 struct pxa_i2c *i2c;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001042 struct resource *res;
Russell King3ae5eae2005-11-09 22:32:44 +00001043 struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
Marc Kleine-Budde21e2ecf2010-06-15 10:56:45 +02001044 const struct platform_device_id *id = platform_get_device_id(dev);
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +01001045 enum pxa_i2c_types i2c_type = id->driver_data;
Russell Kingb652b432005-06-15 12:38:14 +01001046 int ret;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001047 int irq;
Russell Kingb652b432005-06-15 12:38:14 +01001048
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001049 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1050 irq = platform_get_irq(dev, 0);
1051 if (res == NULL || irq < 0)
1052 return -ENODEV;
1053
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001054 if (!request_mem_region(res->start, resource_size(res), res->name))
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001055 return -ENOMEM;
1056
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001057 i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001058 if (!i2c) {
1059 ret = -ENOMEM;
1060 goto emalloc;
1061 }
1062
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001063 i2c->adap.owner = THIS_MODULE;
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001064 i2c->adap.retries = 5;
1065
1066 spin_lock_init(&i2c->lock);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001067 init_waitqueue_head(&i2c->wait);
Enrico Scholz6776f3d2007-05-21 12:29:40 +01001068
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001069 /*
1070 * If "dev->id" is negative we consider it as zero.
1071 * The reason to do so is to avoid sysfs names that only make
1072 * sense when there are multiple adapters.
1073 */
1074 i2c->adap.nr = dev->id != -1 ? dev->id : 0;
1075 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
1076 i2c->adap.nr);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001077
Russell Kinge0d8b132008-11-11 17:52:32 +00001078 i2c->clk = clk_get(&dev->dev, NULL);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001079 if (IS_ERR(i2c->clk)) {
1080 ret = PTR_ERR(i2c->clk);
1081 goto eclk;
1082 }
1083
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001084 i2c->reg_base = ioremap(res->start, resource_size(res));
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001085 if (!i2c->reg_base) {
1086 ret = -EIO;
1087 goto eremap;
1088 }
Sebastian Andrzej Siewiord6668c72011-02-23 12:38:15 +01001089
1090 i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
1091 i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
1092 i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
1093 i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +01001094 if (i2c_type != REGS_CE4100)
1095 i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001096
1097 i2c->iobase = res->start;
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001098 i2c->iosize = resource_size(res);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001099
1100 i2c->irq = irq;
Russell Kingb652b432005-06-15 12:38:14 +01001101
1102 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
1103
1104#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +01001105 if (plat) {
1106 i2c->slave_addr = plat->slave_addr;
Russell Kingbeea4942006-11-07 21:03:20 +00001107 i2c->slave = plat->slave;
Russell Kingb652b432005-06-15 12:38:14 +01001108 }
1109#endif
1110
Russell Kingc3cef3f2007-08-20 10:19:10 +01001111 clk_enable(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001112
Mike Rapoportb7a36702008-01-27 18:14:50 +01001113 if (plat) {
1114 i2c->adap.class = plat->class;
1115 i2c->use_pio = plat->use_pio;
Jonathan Cameronc46c9482008-10-03 15:07:36 +01001116 i2c->fast_mode = plat->fast_mode;
Mike Rapoportb7a36702008-01-27 18:14:50 +01001117 }
1118
1119 if (i2c->use_pio) {
1120 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1121 } else {
1122 i2c->adap.algo = &i2c_pxa_algorithm;
1123 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
1124 i2c->adap.name, i2c);
1125 if (ret)
1126 goto ereqirq;
1127 }
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001128
Russell Kingb652b432005-06-15 12:38:14 +01001129 i2c_pxa_reset(i2c);
1130
1131 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001132 i2c->adap.dev.parent = &dev->dev;
Russell Kingb652b432005-06-15 12:38:14 +01001133
Sebastian Andrzej Siewior7e94dd12011-03-02 11:26:53 +01001134 if (i2c_type == REGS_CE4100)
1135 ret = i2c_add_adapter(&i2c->adap);
1136 else
1137 ret = i2c_add_numbered_adapter(&i2c->adap);
Russell Kingb652b432005-06-15 12:38:14 +01001138 if (ret < 0) {
1139 printk(KERN_INFO "I2C: Failed to add bus\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001140 goto eadapt;
Russell Kingb652b432005-06-15 12:38:14 +01001141 }
1142
Russell King3ae5eae2005-11-09 22:32:44 +00001143 platform_set_drvdata(dev, i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001144
1145#ifdef CONFIG_I2C_PXA_SLAVE
1146 printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
Jean Delvare22e965c2009-01-07 14:29:16 +01001147 dev_name(&i2c->adap.dev), i2c->slave_addr);
Russell Kingb652b432005-06-15 12:38:14 +01001148#else
1149 printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
Jean Delvare22e965c2009-01-07 14:29:16 +01001150 dev_name(&i2c->adap.dev));
Russell Kingb652b432005-06-15 12:38:14 +01001151#endif
1152 return 0;
1153
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001154eadapt:
Mike Rapoportb7a36702008-01-27 18:14:50 +01001155 if (!i2c->use_pio)
1156 free_irq(irq, i2c);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001157ereqirq:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001158 clk_disable(i2c->clk);
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001159 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001160eremap:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001161 clk_put(i2c->clk);
1162eclk:
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001163 kfree(i2c);
1164emalloc:
Linus Walleijc6ffdde2009-06-14 00:20:36 +02001165 release_mem_region(res->start, resource_size(res));
Russell Kingb652b432005-06-15 12:38:14 +01001166 return ret;
1167}
1168
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001169static int __exit i2c_pxa_remove(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001170{
Russell King3ae5eae2005-11-09 22:32:44 +00001171 struct pxa_i2c *i2c = platform_get_drvdata(dev);
Russell Kingb652b432005-06-15 12:38:14 +01001172
Russell King3ae5eae2005-11-09 22:32:44 +00001173 platform_set_drvdata(dev, NULL);
Russell Kingb652b432005-06-15 12:38:14 +01001174
1175 i2c_del_adapter(&i2c->adap);
Mike Rapoportb7a36702008-01-27 18:14:50 +01001176 if (!i2c->use_pio)
1177 free_irq(i2c->irq, i2c);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001178
1179 clk_disable(i2c->clk);
1180 clk_put(i2c->clk);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001181
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001182 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001183 release_mem_region(i2c->iobase, i2c->iosize);
1184 kfree(i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001185
1186 return 0;
1187}
1188
Russell Kinge7d48fa2008-08-26 10:40:50 +01001189#ifdef CONFIG_PM
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001190static int i2c_pxa_suspend_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001191{
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001192 struct platform_device *pdev = to_platform_device(dev);
1193 struct pxa_i2c *i2c = platform_get_drvdata(pdev);
1194
Russell Kinge7d48fa2008-08-26 10:40:50 +01001195 clk_disable(i2c->clk);
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001196
Russell Kinge7d48fa2008-08-26 10:40:50 +01001197 return 0;
1198}
1199
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001200static int i2c_pxa_resume_noirq(struct device *dev)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001201{
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001202 struct platform_device *pdev = to_platform_device(dev);
1203 struct pxa_i2c *i2c = platform_get_drvdata(pdev);
Russell Kinge7d48fa2008-08-26 10:40:50 +01001204
1205 clk_enable(i2c->clk);
1206 i2c_pxa_reset(i2c);
1207
1208 return 0;
1209}
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001210
Alexey Dobriyan47145212009-12-14 18:00:08 -08001211static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001212 .suspend_noirq = i2c_pxa_suspend_noirq,
1213 .resume_noirq = i2c_pxa_resume_noirq,
1214};
1215
1216#define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
Russell Kinge7d48fa2008-08-26 10:40:50 +01001217#else
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001218#define I2C_PXA_DEV_PM_OPS NULL
Russell Kinge7d48fa2008-08-26 10:40:50 +01001219#endif
1220
Russell King3ae5eae2005-11-09 22:32:44 +00001221static struct platform_driver i2c_pxa_driver = {
Russell Kingb652b432005-06-15 12:38:14 +01001222 .probe = i2c_pxa_probe,
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001223 .remove = __exit_p(i2c_pxa_remove),
Russell King3ae5eae2005-11-09 22:32:44 +00001224 .driver = {
1225 .name = "pxa2xx-i2c",
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001226 .owner = THIS_MODULE,
Magnus Damm57f4d4f2009-07-08 13:22:39 +02001227 .pm = I2C_PXA_DEV_PM_OPS,
Russell King3ae5eae2005-11-09 22:32:44 +00001228 },
Eric Miaof23d4912009-04-13 14:43:25 +08001229 .id_table = i2c_pxa_id_table,
Russell Kingb652b432005-06-15 12:38:14 +01001230};
1231
1232static int __init i2c_adap_pxa_init(void)
1233{
Russell King3ae5eae2005-11-09 22:32:44 +00001234 return platform_driver_register(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001235}
1236
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001237static void __exit i2c_adap_pxa_exit(void)
Russell Kingb652b432005-06-15 12:38:14 +01001238{
Holger Schurigd6a7b5f2008-02-11 16:51:41 +01001239 platform_driver_unregister(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001240}
1241
Richard Purdieece5f7b2006-01-12 16:30:23 +00001242MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +02001243MODULE_ALIAS("platform:pxa2xx-i2c");
Richard Purdieece5f7b2006-01-12 16:30:23 +00001244
Uli Luckas47a9b132008-07-14 22:38:30 +02001245subsys_initcall(i2c_adap_pxa_init);
Russell Kingb652b432005-06-15 12:38:14 +01001246module_exit(i2c_adap_pxa_exit);