blob: 8b38ed0379d5aa9fa301f33e7cf0f70151eaf85b [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]
15 * Jun 2002: Properly seperated algo/adap [FB]
16 * 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>
25#include <linux/i2c-id.h>
26#include <linux/init.h>
27#include <linux/time.h>
28#include <linux/sched.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/interrupt.h>
32#include <linux/i2c-pxa.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Russell Kingc3cef3f2007-08-20 10:19:10 +010034#include <linux/err.h>
35#include <linux/clk.h>
Russell Kingb652b432005-06-15 12:38:14 +010036
Russell Kinga09e64f2008-08-05 16:14:15 +010037#include <mach/hardware.h>
Russell Kingb652b432005-06-15 12:38:14 +010038#include <asm/irq.h>
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +010039#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/i2c.h>
41#include <mach/pxa-regs.h>
Russell Kingb652b432005-06-15 12:38:14 +010042
43struct pxa_i2c {
44 spinlock_t lock;
45 wait_queue_head_t wait;
46 struct i2c_msg *msg;
47 unsigned int msg_num;
48 unsigned int msg_idx;
49 unsigned int msg_ptr;
50 unsigned int slave_addr;
51
52 struct i2c_adapter adap;
Russell Kingc3cef3f2007-08-20 10:19:10 +010053 struct clk *clk;
Russell Kingb652b432005-06-15 12:38:14 +010054#ifdef CONFIG_I2C_PXA_SLAVE
55 struct i2c_slave_client *slave;
56#endif
57
58 unsigned int irqlogidx;
59 u32 isrlog[32];
60 u32 icrlog[32];
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +010061
62 void __iomem *reg_base;
Mike Rapoport9ba63c42008-08-17 06:23:05 +010063 unsigned int reg_shift;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +010064
65 unsigned long iobase;
66 unsigned long iosize;
67
68 int irq;
Jonathan Cameronc46c9482008-10-03 15:07:36 +010069 unsigned int use_pio :1;
70 unsigned int fast_mode :1;
Russell Kingb652b432005-06-15 12:38:14 +010071};
72
Mike Rapoport9ba63c42008-08-17 06:23:05 +010073#define _IBMR(i2c) ((i2c)->reg_base + (0x0 << (i2c)->reg_shift))
74#define _IDBR(i2c) ((i2c)->reg_base + (0x4 << (i2c)->reg_shift))
75#define _ICR(i2c) ((i2c)->reg_base + (0x8 << (i2c)->reg_shift))
76#define _ISR(i2c) ((i2c)->reg_base + (0xc << (i2c)->reg_shift))
77#define _ISAR(i2c) ((i2c)->reg_base + (0x10 << (i2c)->reg_shift))
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +010078
Russell Kingb652b432005-06-15 12:38:14 +010079/*
80 * I2C Slave mode address
81 */
82#define I2C_PXA_SLAVE_ADDR 0x1
83
Russell Kingb652b432005-06-15 12:38:14 +010084#ifdef DEBUG
85
86struct bits {
87 u32 mask;
88 const char *set;
89 const char *unset;
90};
Jiri Slabyed113992007-10-18 23:40:28 -070091#define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
Russell Kingb652b432005-06-15 12:38:14 +010092
93static inline void
94decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
95{
96 printk("%s %08x: ", prefix, val);
97 while (num--) {
98 const char *str = val & bits->mask ? bits->set : bits->unset;
99 if (str)
100 printk("%s ", str);
101 bits++;
102 }
103}
104
105static const struct bits isr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700106 PXA_BIT(ISR_RWM, "RX", "TX"),
107 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
108 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
109 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
110 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
111 PXA_BIT(ISR_ALD, "ALD", NULL),
112 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
113 PXA_BIT(ISR_IRF, "RxFull", NULL),
114 PXA_BIT(ISR_GCAD, "GenCall", NULL),
115 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
116 PXA_BIT(ISR_BED, "BusErr", NULL),
Russell Kingb652b432005-06-15 12:38:14 +0100117};
118
119static void decode_ISR(unsigned int val)
120{
Russell King6fd60fa2005-09-08 21:04:58 +0100121 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100122 printk("\n");
123}
124
125static const struct bits icr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700126 PXA_BIT(ICR_START, "START", NULL),
127 PXA_BIT(ICR_STOP, "STOP", NULL),
128 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
129 PXA_BIT(ICR_TB, "TB", NULL),
130 PXA_BIT(ICR_MA, "MA", NULL),
131 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
132 PXA_BIT(ICR_IUE, "IUE", "iue"),
133 PXA_BIT(ICR_GCD, "GCD", NULL),
134 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
135 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
136 PXA_BIT(ICR_BEIE, "BEIE", NULL),
137 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
138 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
139 PXA_BIT(ICR_SADIE, "SADIE", NULL),
140 PXA_BIT(ICR_UR, "UR", "ur"),
Russell Kingb652b432005-06-15 12:38:14 +0100141};
142
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100143#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +0100144static void decode_ICR(unsigned int val)
145{
Russell King6fd60fa2005-09-08 21:04:58 +0100146 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100147 printk("\n");
148}
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100149#endif
Russell Kingb652b432005-06-15 12:38:14 +0100150
151static unsigned int i2c_debug = DEBUG;
152
153static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
154{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100155 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
156 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100157}
158
Harvey Harrison08882d22008-04-22 22:16:47 +0200159#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
Russell Kingb652b432005-06-15 12:38:14 +0100160#else
161#define i2c_debug 0
162
163#define show_state(i2c) do { } while (0)
164#define decode_ISR(val) do { } while (0)
165#define decode_ICR(val) do { } while (0)
166#endif
167
Russell King6fd60fa2005-09-08 21:04:58 +0100168#define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
Russell Kingb652b432005-06-15 12:38:14 +0100169
170static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
Mike Rapoportb7a36702008-01-27 18:14:50 +0100171static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
Russell Kingb652b432005-06-15 12:38:14 +0100172
173static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
174{
175 unsigned int i;
176 printk("i2c: error: %s\n", why);
177 printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
178 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
Russell King6fd60fa2005-09-08 21:04:58 +0100179 printk("i2c: ICR: %08x ISR: %08x\n"
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100180 "i2c: log: ", readl(_ICR(i2c)), readl(_ISR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100181 for (i = 0; i < i2c->irqlogidx; i++)
182 printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
183 printk("\n");
184}
185
186static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
187{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100188 return !(readl(_ICR(i2c)) & ICR_SCLE);
Russell Kingb652b432005-06-15 12:38:14 +0100189}
190
191static void i2c_pxa_abort(struct pxa_i2c *i2c)
192{
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100193 int i = 250;
Russell Kingb652b432005-06-15 12:38:14 +0100194
195 if (i2c_pxa_is_slavemode(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100196 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100197 return;
198 }
199
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100200 while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100201 unsigned long icr = readl(_ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100202
203 icr &= ~ICR_START;
204 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
205
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100206 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100207
208 show_state(i2c);
209
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100210 mdelay(1);
211 i --;
Russell Kingb652b432005-06-15 12:38:14 +0100212 }
213
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100214 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
215 _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100216}
217
218static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
219{
220 int timeout = DEF_TIMEOUT;
221
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100222 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
223 if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
Russell Kingb652b432005-06-15 12:38:14 +0100224 timeout += 4;
225
226 msleep(2);
227 show_state(i2c);
228 }
229
230 if (timeout <= 0)
231 show_state(i2c);
232
233 return timeout <= 0 ? I2C_RETRY : 0;
234}
235
236static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
237{
238 unsigned long timeout = jiffies + HZ*4;
239
240 while (time_before(jiffies, timeout)) {
241 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100242 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100243 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100244
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100245 if (readl(_ISR(i2c)) & ISR_SAD) {
Russell Kingb652b432005-06-15 12:38:14 +0100246 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100247 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100248 goto out;
249 }
250
251 /* wait for unit and bus being not busy, and we also do a
252 * quick check of the i2c lines themselves to ensure they've
253 * gone high...
254 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100255 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
Russell Kingb652b432005-06-15 12:38:14 +0100256 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100257 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100258 return 1;
259 }
260
261 msleep(1);
262 }
263
264 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100265 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100266 out:
267 return 0;
268}
269
270static int i2c_pxa_set_master(struct pxa_i2c *i2c)
271{
272 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100273 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
Russell Kingb652b432005-06-15 12:38:14 +0100274
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100275 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100276 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100277 if (!i2c_pxa_wait_master(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100278 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100279 return I2C_RETRY;
280 }
281 }
282
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100283 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100284 return 0;
285}
286
287#ifdef CONFIG_I2C_PXA_SLAVE
288static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
289{
290 unsigned long timeout = jiffies + HZ*1;
291
292 /* wait for stop */
293
294 show_state(i2c);
295
296 while (time_before(jiffies, timeout)) {
297 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100298 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100299 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100300
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100301 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
302 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
303 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
Russell Kingb652b432005-06-15 12:38:14 +0100304 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100305 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100306 return 1;
307 }
308
309 msleep(1);
310 }
311
312 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100313 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100314 return 0;
315}
316
317/*
318 * clear the hold on the bus, and take of anything else
319 * that has been configured
320 */
321static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
322{
323 show_state(i2c);
324
325 if (errcode < 0) {
326 udelay(100); /* simple delay */
327 } else {
328 /* we need to wait for the stop condition to end */
329
330 /* if we where in stop, then clear... */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100331 if (readl(_ICR(i2c)) & ICR_STOP) {
Russell Kingb652b432005-06-15 12:38:14 +0100332 udelay(100);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100333 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100334 }
335
336 if (!i2c_pxa_wait_slave(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100337 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
338 __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100339 return;
340 }
341 }
342
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100343 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
344 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100345
346 if (i2c_debug) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100347 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
348 decode_ICR(readl(_ICR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100349 }
350}
351#else
352#define i2c_pxa_set_slave(i2c, err) do { } while (0)
353#endif
354
355static void i2c_pxa_reset(struct pxa_i2c *i2c)
356{
357 pr_debug("Resetting I2C Controller Unit\n");
358
359 /* abort any transfer currently under way */
360 i2c_pxa_abort(i2c);
361
362 /* reset according to 9.8 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100363 writel(ICR_UR, _ICR(i2c));
364 writel(I2C_ISR_INIT, _ISR(i2c));
365 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100366
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100367 writel(i2c->slave_addr, _ISAR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100368
369 /* set control register values */
Jonathan Cameronc46c9482008-10-03 15:07:36 +0100370 writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100371
372#ifdef CONFIG_I2C_PXA_SLAVE
Russell King6fd60fa2005-09-08 21:04:58 +0100373 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100374 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100375#endif
376
377 i2c_pxa_set_slave(i2c, 0);
378
379 /* enable unit */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100380 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100381 udelay(100);
382}
383
384
385#ifdef CONFIG_I2C_PXA_SLAVE
386/*
Russell Kingb652b432005-06-15 12:38:14 +0100387 * PXA I2C Slave mode
388 */
389
390static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
391{
392 if (isr & ISR_BED) {
393 /* what should we do here? */
394 } else {
Russell King84b5abe2006-10-28 22:30:17 +0100395 int ret = 0;
396
397 if (i2c->slave != NULL)
398 ret = i2c->slave->read(i2c->slave->data);
Russell Kingb652b432005-06-15 12:38:14 +0100399
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100400 writel(ret, _IDBR(i2c));
401 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
Russell Kingb652b432005-06-15 12:38:14 +0100402 }
403}
404
405static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
406{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100407 unsigned int byte = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100408
409 if (i2c->slave != NULL)
410 i2c->slave->write(i2c->slave->data, byte);
411
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100412 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100413}
414
415static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
416{
417 int timeout;
418
419 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100420 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
Russell Kingb652b432005-06-15 12:38:14 +0100421 (isr & ISR_RWM) ? 'r' : 't');
422
423 if (i2c->slave != NULL)
424 i2c->slave->event(i2c->slave->data,
425 (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
426
427 /*
428 * slave could interrupt in the middle of us generating a
429 * start condition... if this happens, we'd better back off
430 * and stop holding the poor thing up
431 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100432 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
433 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100434
435 timeout = 0x10000;
436
437 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100438 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100439 break;
440
441 timeout--;
442
443 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100444 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100445 break;
446 }
447 }
448
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100449 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100450}
451
452static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
453{
454 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100455 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
Russell Kingb652b432005-06-15 12:38:14 +0100456
457 if (i2c->slave != NULL)
458 i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
459
460 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100461 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
Russell Kingb652b432005-06-15 12:38:14 +0100462
463 /*
464 * If we have a master-mode message waiting,
465 * kick it off now that the slave has completed.
466 */
467 if (i2c->msg)
468 i2c_pxa_master_complete(i2c, I2C_RETRY);
469}
470#else
471static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
472{
473 if (isr & ISR_BED) {
474 /* what should we do here? */
475 } else {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100476 writel(0, _IDBR(i2c));
477 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100478 }
479}
480
481static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
482{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100483 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100484}
485
486static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
487{
488 int timeout;
489
490 /*
491 * slave could interrupt in the middle of us generating a
492 * start condition... if this happens, we'd better back off
493 * and stop holding the poor thing up
494 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100495 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
496 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100497
498 timeout = 0x10000;
499
500 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100501 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100502 break;
503
504 timeout--;
505
506 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100507 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100508 break;
509 }
510 }
511
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100512 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100513}
514
515static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
516{
517 if (i2c->msg)
518 i2c_pxa_master_complete(i2c, I2C_RETRY);
519}
520#endif
521
522/*
523 * PXA I2C Master mode
524 */
525
526static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
527{
528 unsigned int addr = (msg->addr & 0x7f) << 1;
529
530 if (msg->flags & I2C_M_RD)
531 addr |= 1;
532
533 return addr;
534}
535
536static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
537{
538 u32 icr;
539
540 /*
541 * Step 1: target slave address into IDBR
542 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100543 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100544
545 /*
546 * Step 2: initiate the write.
547 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100548 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
549 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100550}
551
Jean Delvare7d054812007-05-01 23:26:33 +0200552static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
553{
554 u32 icr;
555
556 /*
557 * Clear the STOP and ACK flags
558 */
559 icr = readl(_ICR(i2c));
560 icr &= ~(ICR_STOP | ICR_ACKNAK);
Russell King0cfe61e2007-05-10 03:15:32 -0700561 writel(icr, _ICR(i2c));
Jean Delvare7d054812007-05-01 23:26:33 +0200562}
563
Mike Rapoportb7a36702008-01-27 18:14:50 +0100564static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
565{
566 /* make timeout the same as for interrupt based functions */
567 long timeout = 2 * DEF_TIMEOUT;
568
569 /*
570 * Wait for the bus to become free.
571 */
572 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
573 udelay(1000);
574 show_state(i2c);
575 }
576
577 if (timeout <= 0) {
578 show_state(i2c);
579 dev_err(&i2c->adap.dev,
580 "i2c_pxa: timeout waiting for bus free\n");
581 return I2C_RETRY;
582 }
583
584 /*
585 * Set master mode.
586 */
587 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
588
589 return 0;
590}
591
592static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
593 struct i2c_msg *msg, int num)
594{
595 unsigned long timeout = 500000; /* 5 seconds */
596 int ret = 0;
597
598 ret = i2c_pxa_pio_set_master(i2c);
599 if (ret)
600 goto out;
601
602 i2c->msg = msg;
603 i2c->msg_num = num;
604 i2c->msg_idx = 0;
605 i2c->msg_ptr = 0;
606 i2c->irqlogidx = 0;
607
608 i2c_pxa_start_message(i2c);
609
610 while (timeout-- && i2c->msg_num > 0) {
611 i2c_pxa_handler(0, i2c);
612 udelay(10);
613 }
614
615 i2c_pxa_stop_message(i2c);
616
617 /*
618 * We place the return code in i2c->msg_idx.
619 */
620 ret = i2c->msg_idx;
621
622out:
623 if (timeout == 0)
624 i2c_pxa_scream_blue_murder(i2c, "timeout");
625
626 return ret;
627}
628
Russell Kingb652b432005-06-15 12:38:14 +0100629/*
Jean Delvare3fb9a652006-01-18 23:17:01 +0100630 * We are protected by the adapter bus mutex.
Russell Kingb652b432005-06-15 12:38:14 +0100631 */
632static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
633{
634 long timeout;
635 int ret;
636
637 /*
638 * Wait for the bus to become free.
639 */
640 ret = i2c_pxa_wait_bus_not_busy(i2c);
641 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100642 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
Russell Kingb652b432005-06-15 12:38:14 +0100643 goto out;
644 }
645
646 /*
647 * Set master mode.
648 */
649 ret = i2c_pxa_set_master(i2c);
650 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100651 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
Russell Kingb652b432005-06-15 12:38:14 +0100652 goto out;
653 }
654
655 spin_lock_irq(&i2c->lock);
656
657 i2c->msg = msg;
658 i2c->msg_num = num;
659 i2c->msg_idx = 0;
660 i2c->msg_ptr = 0;
661 i2c->irqlogidx = 0;
662
663 i2c_pxa_start_message(i2c);
664
665 spin_unlock_irq(&i2c->lock);
666
667 /*
668 * The rest of the processing occurs in the interrupt handler.
669 */
670 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
Jean Delvare7d054812007-05-01 23:26:33 +0200671 i2c_pxa_stop_message(i2c);
Russell Kingb652b432005-06-15 12:38:14 +0100672
673 /*
674 * We place the return code in i2c->msg_idx.
675 */
676 ret = i2c->msg_idx;
677
678 if (timeout == 0)
679 i2c_pxa_scream_blue_murder(i2c, "timeout");
680
681 out:
682 return ret;
683}
684
Mike Rapoportb7a36702008-01-27 18:14:50 +0100685static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
686 struct i2c_msg msgs[], int num)
687{
688 struct pxa_i2c *i2c = adap->algo_data;
689 int ret, i;
690
691 /* If the I2C controller is disabled we need to reset it
692 (probably due to a suspend/resume destroying state). We do
693 this here as we can then avoid worrying about resuming the
694 controller before its users. */
695 if (!(readl(_ICR(i2c)) & ICR_IUE))
696 i2c_pxa_reset(i2c);
697
698 for (i = adap->retries; i >= 0; i--) {
699 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
700 if (ret != I2C_RETRY)
701 goto out;
702
703 if (i2c_debug)
704 dev_dbg(&adap->dev, "Retrying transmission\n");
705 udelay(100);
706 }
707 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
708 ret = -EREMOTEIO;
709 out:
710 i2c_pxa_set_slave(i2c, ret);
711 return ret;
712}
713
Russell Kingb652b432005-06-15 12:38:14 +0100714/*
715 * i2c_pxa_master_complete - complete the message and wake up.
716 */
717static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
718{
719 i2c->msg_ptr = 0;
720 i2c->msg = NULL;
721 i2c->msg_idx ++;
722 i2c->msg_num = 0;
723 if (ret)
724 i2c->msg_idx = ret;
Mike Rapoportb7a36702008-01-27 18:14:50 +0100725 if (!i2c->use_pio)
726 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +0100727}
728
729static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
730{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100731 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100732
733 again:
734 /*
735 * If ISR_ALD is set, we lost arbitration.
736 */
737 if (isr & ISR_ALD) {
738 /*
739 * Do we need to do anything here? The PXA docs
740 * are vague about what happens.
741 */
742 i2c_pxa_scream_blue_murder(i2c, "ALD set");
743
744 /*
745 * We ignore this error. We seem to see spurious ALDs
746 * for seemingly no reason. If we handle them as I think
747 * they should, we end up causing an I2C error, which
748 * is painful for some systems.
749 */
750 return; /* ignore */
751 }
752
753 if (isr & ISR_BED) {
754 int ret = BUS_ERROR;
755
756 /*
757 * I2C bus error - either the device NAK'd us, or
758 * something more serious happened. If we were NAK'd
759 * on the initial address phase, we can retry.
760 */
761 if (isr & ISR_ACKNAK) {
762 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
763 ret = I2C_RETRY;
764 else
765 ret = XFER_NAKED;
766 }
767 i2c_pxa_master_complete(i2c, ret);
768 } else if (isr & ISR_RWM) {
769 /*
770 * Read mode. We have just sent the address byte, and
771 * now we must initiate the transfer.
772 */
773 if (i2c->msg_ptr == i2c->msg->len - 1 &&
774 i2c->msg_idx == i2c->msg_num - 1)
775 icr |= ICR_STOP | ICR_ACKNAK;
776
777 icr |= ICR_ALDIE | ICR_TB;
778 } else if (i2c->msg_ptr < i2c->msg->len) {
779 /*
780 * Write mode. Write the next data byte.
781 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100782 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100783
784 icr |= ICR_ALDIE | ICR_TB;
785
786 /*
787 * If this is the last byte of the last message, send
788 * a STOP.
789 */
790 if (i2c->msg_ptr == i2c->msg->len &&
791 i2c->msg_idx == i2c->msg_num - 1)
792 icr |= ICR_STOP;
793 } else if (i2c->msg_idx < i2c->msg_num - 1) {
794 /*
795 * Next segment of the message.
796 */
797 i2c->msg_ptr = 0;
798 i2c->msg_idx ++;
799 i2c->msg++;
800
801 /*
802 * If we aren't doing a repeated start and address,
803 * go back and try to send the next byte. Note that
804 * we do not support switching the R/W direction here.
805 */
806 if (i2c->msg->flags & I2C_M_NOSTART)
807 goto again;
808
809 /*
810 * Write the next address.
811 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100812 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100813
814 /*
815 * And trigger a repeated start, and send the byte.
816 */
817 icr &= ~ICR_ALDIE;
818 icr |= ICR_START | ICR_TB;
819 } else {
820 if (i2c->msg->len == 0) {
821 /*
822 * Device probes have a message length of zero
823 * and need the bus to be reset before it can
824 * be used again.
825 */
826 i2c_pxa_reset(i2c);
827 }
828 i2c_pxa_master_complete(i2c, 0);
829 }
830
831 i2c->icrlog[i2c->irqlogidx-1] = icr;
832
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100833 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100834 show_state(i2c);
835}
836
837static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
838{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100839 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100840
841 /*
842 * Read the byte.
843 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100844 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100845
846 if (i2c->msg_ptr < i2c->msg->len) {
847 /*
848 * If this is the last byte of the last
849 * message, send a STOP.
850 */
851 if (i2c->msg_ptr == i2c->msg->len - 1)
852 icr |= ICR_STOP | ICR_ACKNAK;
853
854 icr |= ICR_ALDIE | ICR_TB;
855 } else {
856 i2c_pxa_master_complete(i2c, 0);
857 }
858
859 i2c->icrlog[i2c->irqlogidx-1] = icr;
860
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100861 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100862}
863
David Howells7d12e782006-10-05 14:55:46 +0100864static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
Russell Kingb652b432005-06-15 12:38:14 +0100865{
866 struct pxa_i2c *i2c = dev_id;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100867 u32 isr = readl(_ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100868
869 if (i2c_debug > 2 && 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100870 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100871 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100872 decode_ISR(isr);
873 }
874
Tobias Klauser7e3d7db2006-01-09 23:19:51 +0100875 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
Russell Kingb652b432005-06-15 12:38:14 +0100876 i2c->isrlog[i2c->irqlogidx++] = isr;
877
878 show_state(i2c);
879
880 /*
881 * Always clear all pending IRQs.
882 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100883 writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100884
885 if (isr & ISR_SAD)
886 i2c_pxa_slave_start(i2c, isr);
887 if (isr & ISR_SSD)
888 i2c_pxa_slave_stop(i2c);
889
890 if (i2c_pxa_is_slavemode(i2c)) {
891 if (isr & ISR_ITE)
892 i2c_pxa_slave_txempty(i2c, isr);
893 if (isr & ISR_IRF)
894 i2c_pxa_slave_rxfull(i2c, isr);
895 } else if (i2c->msg) {
896 if (isr & ISR_ITE)
897 i2c_pxa_irq_txempty(i2c, isr);
898 if (isr & ISR_IRF)
899 i2c_pxa_irq_rxfull(i2c, isr);
900 } else {
901 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
902 }
903
904 return IRQ_HANDLED;
905}
906
907
908static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
909{
910 struct pxa_i2c *i2c = adap->algo_data;
911 int ret, i;
912
913 for (i = adap->retries; i >= 0; i--) {
914 ret = i2c_pxa_do_xfer(i2c, msgs, num);
915 if (ret != I2C_RETRY)
916 goto out;
917
918 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100919 dev_dbg(&adap->dev, "Retrying transmission\n");
Russell Kingb652b432005-06-15 12:38:14 +0100920 udelay(100);
921 }
922 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
923 ret = -EREMOTEIO;
924 out:
925 i2c_pxa_set_slave(i2c, ret);
926 return ret;
927}
928
Russell Kingda16e322005-09-14 22:54:45 +0100929static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
930{
931 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
932}
933
Jean Delvare8f9082c2006-09-03 22:39:46 +0200934static const struct i2c_algorithm i2c_pxa_algorithm = {
Russell Kingb652b432005-06-15 12:38:14 +0100935 .master_xfer = i2c_pxa_xfer,
Russell Kingda16e322005-09-14 22:54:45 +0100936 .functionality = i2c_pxa_functionality,
Russell Kingb652b432005-06-15 12:38:14 +0100937};
938
Mike Rapoportb7a36702008-01-27 18:14:50 +0100939static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
940 .master_xfer = i2c_pxa_pio_xfer,
941 .functionality = i2c_pxa_functionality,
942};
943
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100944#define res_len(r) ((r)->end - (r)->start + 1)
Russell King3ae5eae2005-11-09 22:32:44 +0000945static int i2c_pxa_probe(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +0100946{
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100947 struct pxa_i2c *i2c;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100948 struct resource *res;
Russell King3ae5eae2005-11-09 22:32:44 +0000949 struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
Russell Kingb652b432005-06-15 12:38:14 +0100950 int ret;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100951 int irq;
Russell Kingb652b432005-06-15 12:38:14 +0100952
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100953 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
954 irq = platform_get_irq(dev, 0);
955 if (res == NULL || irq < 0)
956 return -ENODEV;
957
958 if (!request_mem_region(res->start, res_len(res), res->name))
959 return -ENOMEM;
960
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100961 i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100962 if (!i2c) {
963 ret = -ENOMEM;
964 goto emalloc;
965 }
966
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100967 i2c->adap.owner = THIS_MODULE;
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100968 i2c->adap.retries = 5;
969
970 spin_lock_init(&i2c->lock);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100971 init_waitqueue_head(&i2c->wait);
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100972
Wolfram Sanga92b36e2008-02-24 20:03:42 +0100973 /*
974 * If "dev->id" is negative we consider it as zero.
975 * The reason to do so is to avoid sysfs names that only make
976 * sense when there are multiple adapters.
977 */
978 i2c->adap.nr = dev->id != -1 ? dev->id : 0;
979 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
980 i2c->adap.nr);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100981
Russell Kingc3cef3f2007-08-20 10:19:10 +0100982 i2c->clk = clk_get(&dev->dev, "I2CCLK");
983 if (IS_ERR(i2c->clk)) {
984 ret = PTR_ERR(i2c->clk);
985 goto eclk;
986 }
987
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100988 i2c->reg_base = ioremap(res->start, res_len(res));
989 if (!i2c->reg_base) {
990 ret = -EIO;
991 goto eremap;
992 }
Mike Rapoport9ba63c42008-08-17 06:23:05 +0100993 i2c->reg_shift = (cpu_is_pxa3xx() && (dev->id == 1)) ? 0 : 1;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100994
995 i2c->iobase = res->start;
996 i2c->iosize = res_len(res);
997
998 i2c->irq = irq;
Russell Kingb652b432005-06-15 12:38:14 +0100999
1000 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
1001
1002#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +01001003 if (plat) {
1004 i2c->slave_addr = plat->slave_addr;
Russell Kingbeea4942006-11-07 21:03:20 +00001005 i2c->slave = plat->slave;
Russell Kingb652b432005-06-15 12:38:14 +01001006 }
1007#endif
1008
Russell Kingc3cef3f2007-08-20 10:19:10 +01001009 clk_enable(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001010
Mike Rapoportb7a36702008-01-27 18:14:50 +01001011 if (plat) {
1012 i2c->adap.class = plat->class;
1013 i2c->use_pio = plat->use_pio;
Jonathan Cameronc46c9482008-10-03 15:07:36 +01001014 i2c->fast_mode = plat->fast_mode;
Mike Rapoportb7a36702008-01-27 18:14:50 +01001015 }
1016
1017 if (i2c->use_pio) {
1018 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1019 } else {
1020 i2c->adap.algo = &i2c_pxa_algorithm;
1021 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
1022 i2c->adap.name, i2c);
1023 if (ret)
1024 goto ereqirq;
1025 }
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001026
Russell Kingb652b432005-06-15 12:38:14 +01001027 i2c_pxa_reset(i2c);
1028
1029 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001030 i2c->adap.dev.parent = &dev->dev;
Russell Kingb652b432005-06-15 12:38:14 +01001031
Rodolfo Giometti066af982007-07-12 14:12:30 +02001032 ret = i2c_add_numbered_adapter(&i2c->adap);
Russell Kingb652b432005-06-15 12:38:14 +01001033 if (ret < 0) {
1034 printk(KERN_INFO "I2C: Failed to add bus\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001035 goto eadapt;
Russell Kingb652b432005-06-15 12:38:14 +01001036 }
1037
Russell King3ae5eae2005-11-09 22:32:44 +00001038 platform_set_drvdata(dev, i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001039
1040#ifdef CONFIG_I2C_PXA_SLAVE
1041 printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
1042 i2c->adap.dev.bus_id, i2c->slave_addr);
1043#else
1044 printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
1045 i2c->adap.dev.bus_id);
1046#endif
1047 return 0;
1048
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001049eadapt:
Mike Rapoportb7a36702008-01-27 18:14:50 +01001050 if (!i2c->use_pio)
1051 free_irq(irq, i2c);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001052ereqirq:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001053 clk_disable(i2c->clk);
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001054 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001055eremap:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001056 clk_put(i2c->clk);
1057eclk:
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001058 kfree(i2c);
1059emalloc:
1060 release_mem_region(res->start, res_len(res));
Russell Kingb652b432005-06-15 12:38:14 +01001061 return ret;
1062}
1063
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001064static int __exit i2c_pxa_remove(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001065{
Russell King3ae5eae2005-11-09 22:32:44 +00001066 struct pxa_i2c *i2c = platform_get_drvdata(dev);
Russell Kingb652b432005-06-15 12:38:14 +01001067
Russell King3ae5eae2005-11-09 22:32:44 +00001068 platform_set_drvdata(dev, NULL);
Russell Kingb652b432005-06-15 12:38:14 +01001069
1070 i2c_del_adapter(&i2c->adap);
Mike Rapoportb7a36702008-01-27 18:14:50 +01001071 if (!i2c->use_pio)
1072 free_irq(i2c->irq, i2c);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001073
1074 clk_disable(i2c->clk);
1075 clk_put(i2c->clk);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001076
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001077 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001078 release_mem_region(i2c->iobase, i2c->iosize);
1079 kfree(i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001080
1081 return 0;
1082}
1083
Russell Kinge7d48fa2008-08-26 10:40:50 +01001084#ifdef CONFIG_PM
1085static int i2c_pxa_suspend_late(struct platform_device *dev, pm_message_t state)
1086{
1087 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1088 clk_disable(i2c->clk);
1089 return 0;
1090}
1091
1092static int i2c_pxa_resume_early(struct platform_device *dev)
1093{
1094 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1095
1096 clk_enable(i2c->clk);
1097 i2c_pxa_reset(i2c);
1098
1099 return 0;
1100}
1101#else
1102#define i2c_pxa_suspend_late NULL
1103#define i2c_pxa_resume_early NULL
1104#endif
1105
Russell King3ae5eae2005-11-09 22:32:44 +00001106static struct platform_driver i2c_pxa_driver = {
Russell Kingb652b432005-06-15 12:38:14 +01001107 .probe = i2c_pxa_probe,
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001108 .remove = __exit_p(i2c_pxa_remove),
Russell Kinge7d48fa2008-08-26 10:40:50 +01001109 .suspend_late = i2c_pxa_suspend_late,
1110 .resume_early = i2c_pxa_resume_early,
Russell King3ae5eae2005-11-09 22:32:44 +00001111 .driver = {
1112 .name = "pxa2xx-i2c",
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001113 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001114 },
Russell Kingb652b432005-06-15 12:38:14 +01001115};
1116
1117static int __init i2c_adap_pxa_init(void)
1118{
Russell King3ae5eae2005-11-09 22:32:44 +00001119 return platform_driver_register(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001120}
1121
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001122static void __exit i2c_adap_pxa_exit(void)
Russell Kingb652b432005-06-15 12:38:14 +01001123{
Holger Schurigd6a7b5f2008-02-11 16:51:41 +01001124 platform_driver_unregister(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001125}
1126
Richard Purdieece5f7b2006-01-12 16:30:23 +00001127MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +02001128MODULE_ALIAS("platform:pxa2xx-i2c");
Richard Purdieece5f7b2006-01-12 16:30:23 +00001129
Uli Luckas47a9b132008-07-14 22:38:30 +02001130subsys_initcall(i2c_adap_pxa_init);
Russell Kingb652b432005-06-15 12:38:14 +01001131module_exit(i2c_adap_pxa_exit);