blob: 518b57c795c819cb41a0bb9b012de3011eb2c50e [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;
Mike Rapoportb7a36702008-01-27 18:14:50 +010069 int use_pio;
Russell Kingb652b432005-06-15 12:38:14 +010070};
71
Mike Rapoport9ba63c42008-08-17 06:23:05 +010072#define _IBMR(i2c) ((i2c)->reg_base + (0x0 << (i2c)->reg_shift))
73#define _IDBR(i2c) ((i2c)->reg_base + (0x4 << (i2c)->reg_shift))
74#define _ICR(i2c) ((i2c)->reg_base + (0x8 << (i2c)->reg_shift))
75#define _ISR(i2c) ((i2c)->reg_base + (0xc << (i2c)->reg_shift))
76#define _ISAR(i2c) ((i2c)->reg_base + (0x10 << (i2c)->reg_shift))
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +010077
Russell Kingb652b432005-06-15 12:38:14 +010078/*
79 * I2C Slave mode address
80 */
81#define I2C_PXA_SLAVE_ADDR 0x1
82
Russell Kingb652b432005-06-15 12:38:14 +010083#ifdef DEBUG
84
85struct bits {
86 u32 mask;
87 const char *set;
88 const char *unset;
89};
Jiri Slabyed113992007-10-18 23:40:28 -070090#define PXA_BIT(m, s, u) { .mask = m, .set = s, .unset = u }
Russell Kingb652b432005-06-15 12:38:14 +010091
92static inline void
93decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
94{
95 printk("%s %08x: ", prefix, val);
96 while (num--) {
97 const char *str = val & bits->mask ? bits->set : bits->unset;
98 if (str)
99 printk("%s ", str);
100 bits++;
101 }
102}
103
104static const struct bits isr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700105 PXA_BIT(ISR_RWM, "RX", "TX"),
106 PXA_BIT(ISR_ACKNAK, "NAK", "ACK"),
107 PXA_BIT(ISR_UB, "Bsy", "Rdy"),
108 PXA_BIT(ISR_IBB, "BusBsy", "BusRdy"),
109 PXA_BIT(ISR_SSD, "SlaveStop", NULL),
110 PXA_BIT(ISR_ALD, "ALD", NULL),
111 PXA_BIT(ISR_ITE, "TxEmpty", NULL),
112 PXA_BIT(ISR_IRF, "RxFull", NULL),
113 PXA_BIT(ISR_GCAD, "GenCall", NULL),
114 PXA_BIT(ISR_SAD, "SlaveAddr", NULL),
115 PXA_BIT(ISR_BED, "BusErr", NULL),
Russell Kingb652b432005-06-15 12:38:14 +0100116};
117
118static void decode_ISR(unsigned int val)
119{
Russell King6fd60fa2005-09-08 21:04:58 +0100120 decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100121 printk("\n");
122}
123
124static const struct bits icr_bits[] = {
Jiri Slabyed113992007-10-18 23:40:28 -0700125 PXA_BIT(ICR_START, "START", NULL),
126 PXA_BIT(ICR_STOP, "STOP", NULL),
127 PXA_BIT(ICR_ACKNAK, "ACKNAK", NULL),
128 PXA_BIT(ICR_TB, "TB", NULL),
129 PXA_BIT(ICR_MA, "MA", NULL),
130 PXA_BIT(ICR_SCLE, "SCLE", "scle"),
131 PXA_BIT(ICR_IUE, "IUE", "iue"),
132 PXA_BIT(ICR_GCD, "GCD", NULL),
133 PXA_BIT(ICR_ITEIE, "ITEIE", NULL),
134 PXA_BIT(ICR_IRFIE, "IRFIE", NULL),
135 PXA_BIT(ICR_BEIE, "BEIE", NULL),
136 PXA_BIT(ICR_SSDIE, "SSDIE", NULL),
137 PXA_BIT(ICR_ALDIE, "ALDIE", NULL),
138 PXA_BIT(ICR_SADIE, "SADIE", NULL),
139 PXA_BIT(ICR_UR, "UR", "ur"),
Russell Kingb652b432005-06-15 12:38:14 +0100140};
141
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100142#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +0100143static void decode_ICR(unsigned int val)
144{
Russell King6fd60fa2005-09-08 21:04:58 +0100145 decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
Russell Kingb652b432005-06-15 12:38:14 +0100146 printk("\n");
147}
Holger Schurigd6a7b5f2008-02-11 16:51:41 +0100148#endif
Russell Kingb652b432005-06-15 12:38:14 +0100149
150static unsigned int i2c_debug = DEBUG;
151
152static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
153{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100154 dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
155 readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100156}
157
Harvey Harrison08882d22008-04-22 22:16:47 +0200158#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
Russell Kingb652b432005-06-15 12:38:14 +0100159#else
160#define i2c_debug 0
161
162#define show_state(i2c) do { } while (0)
163#define decode_ISR(val) do { } while (0)
164#define decode_ICR(val) do { } while (0)
165#endif
166
Russell King6fd60fa2005-09-08 21:04:58 +0100167#define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
Russell Kingb652b432005-06-15 12:38:14 +0100168
169static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
Mike Rapoportb7a36702008-01-27 18:14:50 +0100170static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
Russell Kingb652b432005-06-15 12:38:14 +0100171
172static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
173{
174 unsigned int i;
175 printk("i2c: error: %s\n", why);
176 printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
177 i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
Russell King6fd60fa2005-09-08 21:04:58 +0100178 printk("i2c: ICR: %08x ISR: %08x\n"
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100179 "i2c: log: ", readl(_ICR(i2c)), readl(_ISR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100180 for (i = 0; i < i2c->irqlogidx; i++)
181 printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
182 printk("\n");
183}
184
185static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
186{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100187 return !(readl(_ICR(i2c)) & ICR_SCLE);
Russell Kingb652b432005-06-15 12:38:14 +0100188}
189
190static void i2c_pxa_abort(struct pxa_i2c *i2c)
191{
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100192 int i = 250;
Russell Kingb652b432005-06-15 12:38:14 +0100193
194 if (i2c_pxa_is_slavemode(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100195 dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100196 return;
197 }
198
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100199 while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100200 unsigned long icr = readl(_ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100201
202 icr &= ~ICR_START;
203 icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
204
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100205 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100206
207 show_state(i2c);
208
Dmitry Baryshkov387fa6a2008-08-18 14:38:48 +0100209 mdelay(1);
210 i --;
Russell Kingb652b432005-06-15 12:38:14 +0100211 }
212
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100213 writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
214 _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100215}
216
217static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
218{
219 int timeout = DEF_TIMEOUT;
220
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100221 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
222 if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
Russell Kingb652b432005-06-15 12:38:14 +0100223 timeout += 4;
224
225 msleep(2);
226 show_state(i2c);
227 }
228
229 if (timeout <= 0)
230 show_state(i2c);
231
232 return timeout <= 0 ? I2C_RETRY : 0;
233}
234
235static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
236{
237 unsigned long timeout = jiffies + HZ*4;
238
239 while (time_before(jiffies, timeout)) {
240 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100241 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100242 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100243
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100244 if (readl(_ISR(i2c)) & ISR_SAD) {
Russell Kingb652b432005-06-15 12:38:14 +0100245 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100246 dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100247 goto out;
248 }
249
250 /* wait for unit and bus being not busy, and we also do a
251 * quick check of the i2c lines themselves to ensure they've
252 * gone high...
253 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100254 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
Russell Kingb652b432005-06-15 12:38:14 +0100255 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100256 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100257 return 1;
258 }
259
260 msleep(1);
261 }
262
263 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100264 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100265 out:
266 return 0;
267}
268
269static int i2c_pxa_set_master(struct pxa_i2c *i2c)
270{
271 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100272 dev_dbg(&i2c->adap.dev, "setting to bus master\n");
Russell Kingb652b432005-06-15 12:38:14 +0100273
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100274 if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100275 dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100276 if (!i2c_pxa_wait_master(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100277 dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100278 return I2C_RETRY;
279 }
280 }
281
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100282 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100283 return 0;
284}
285
286#ifdef CONFIG_I2C_PXA_SLAVE
287static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
288{
289 unsigned long timeout = jiffies + HZ*1;
290
291 /* wait for stop */
292
293 show_state(i2c);
294
295 while (time_before(jiffies, timeout)) {
296 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100297 dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100298 __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100299
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100300 if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
301 (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
302 (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
Russell Kingb652b432005-06-15 12:38:14 +0100303 if (i2c_debug > 1)
Russell King6fd60fa2005-09-08 21:04:58 +0100304 dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100305 return 1;
306 }
307
308 msleep(1);
309 }
310
311 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100312 dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100313 return 0;
314}
315
316/*
317 * clear the hold on the bus, and take of anything else
318 * that has been configured
319 */
320static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
321{
322 show_state(i2c);
323
324 if (errcode < 0) {
325 udelay(100); /* simple delay */
326 } else {
327 /* we need to wait for the stop condition to end */
328
329 /* if we where in stop, then clear... */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100330 if (readl(_ICR(i2c)) & ICR_STOP) {
Russell Kingb652b432005-06-15 12:38:14 +0100331 udelay(100);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100332 writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100333 }
334
335 if (!i2c_pxa_wait_slave(i2c)) {
Russell King6fd60fa2005-09-08 21:04:58 +0100336 dev_err(&i2c->adap.dev, "%s: wait timedout\n",
337 __func__);
Russell Kingb652b432005-06-15 12:38:14 +0100338 return;
339 }
340 }
341
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100342 writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
343 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100344
345 if (i2c_debug) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100346 dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
347 decode_ICR(readl(_ICR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100348 }
349}
350#else
351#define i2c_pxa_set_slave(i2c, err) do { } while (0)
352#endif
353
354static void i2c_pxa_reset(struct pxa_i2c *i2c)
355{
356 pr_debug("Resetting I2C Controller Unit\n");
357
358 /* abort any transfer currently under way */
359 i2c_pxa_abort(i2c);
360
361 /* reset according to 9.8 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100362 writel(ICR_UR, _ICR(i2c));
363 writel(I2C_ISR_INIT, _ISR(i2c));
364 writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100365
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100366 writel(i2c->slave_addr, _ISAR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100367
368 /* set control register values */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100369 writel(I2C_ICR_INIT, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100370
371#ifdef CONFIG_I2C_PXA_SLAVE
Russell King6fd60fa2005-09-08 21:04:58 +0100372 dev_info(&i2c->adap.dev, "Enabling slave mode\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100373 writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100374#endif
375
376 i2c_pxa_set_slave(i2c, 0);
377
378 /* enable unit */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100379 writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100380 udelay(100);
381}
382
383
384#ifdef CONFIG_I2C_PXA_SLAVE
385/*
Russell Kingb652b432005-06-15 12:38:14 +0100386 * PXA I2C Slave mode
387 */
388
389static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
390{
391 if (isr & ISR_BED) {
392 /* what should we do here? */
393 } else {
Russell King84b5abe2006-10-28 22:30:17 +0100394 int ret = 0;
395
396 if (i2c->slave != NULL)
397 ret = i2c->slave->read(i2c->slave->data);
Russell Kingb652b432005-06-15 12:38:14 +0100398
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100399 writel(ret, _IDBR(i2c));
400 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
Russell Kingb652b432005-06-15 12:38:14 +0100401 }
402}
403
404static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
405{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100406 unsigned int byte = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100407
408 if (i2c->slave != NULL)
409 i2c->slave->write(i2c->slave->data, byte);
410
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100411 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100412}
413
414static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
415{
416 int timeout;
417
418 if (i2c_debug > 0)
Russell King6fd60fa2005-09-08 21:04:58 +0100419 dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
Russell Kingb652b432005-06-15 12:38:14 +0100420 (isr & ISR_RWM) ? 'r' : 't');
421
422 if (i2c->slave != NULL)
423 i2c->slave->event(i2c->slave->data,
424 (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
425
426 /*
427 * slave could interrupt in the middle of us generating a
428 * start condition... if this happens, we'd better back off
429 * and stop holding the poor thing up
430 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100431 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
432 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100433
434 timeout = 0x10000;
435
436 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100437 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100438 break;
439
440 timeout--;
441
442 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100443 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100444 break;
445 }
446 }
447
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100448 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100449}
450
451static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
452{
453 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100454 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
Russell Kingb652b432005-06-15 12:38:14 +0100455
456 if (i2c->slave != NULL)
457 i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
458
459 if (i2c_debug > 2)
Russell King6fd60fa2005-09-08 21:04:58 +0100460 dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
Russell Kingb652b432005-06-15 12:38:14 +0100461
462 /*
463 * If we have a master-mode message waiting,
464 * kick it off now that the slave has completed.
465 */
466 if (i2c->msg)
467 i2c_pxa_master_complete(i2c, I2C_RETRY);
468}
469#else
470static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
471{
472 if (isr & ISR_BED) {
473 /* what should we do here? */
474 } else {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100475 writel(0, _IDBR(i2c));
476 writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100477 }
478}
479
480static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
481{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100482 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100483}
484
485static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
486{
487 int timeout;
488
489 /*
490 * slave could interrupt in the middle of us generating a
491 * start condition... if this happens, we'd better back off
492 * and stop holding the poor thing up
493 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100494 writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
495 writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100496
497 timeout = 0x10000;
498
499 while (1) {
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100500 if ((readl(_IBMR(i2c)) & 2) == 2)
Russell Kingb652b432005-06-15 12:38:14 +0100501 break;
502
503 timeout--;
504
505 if (timeout <= 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100506 dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
Russell Kingb652b432005-06-15 12:38:14 +0100507 break;
508 }
509 }
510
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100511 writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100512}
513
514static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
515{
516 if (i2c->msg)
517 i2c_pxa_master_complete(i2c, I2C_RETRY);
518}
519#endif
520
521/*
522 * PXA I2C Master mode
523 */
524
525static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
526{
527 unsigned int addr = (msg->addr & 0x7f) << 1;
528
529 if (msg->flags & I2C_M_RD)
530 addr |= 1;
531
532 return addr;
533}
534
535static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
536{
537 u32 icr;
538
539 /*
540 * Step 1: target slave address into IDBR
541 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100542 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100543
544 /*
545 * Step 2: initiate the write.
546 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100547 icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
548 writel(icr | ICR_START | ICR_TB, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100549}
550
Jean Delvare7d054812007-05-01 23:26:33 +0200551static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
552{
553 u32 icr;
554
555 /*
556 * Clear the STOP and ACK flags
557 */
558 icr = readl(_ICR(i2c));
559 icr &= ~(ICR_STOP | ICR_ACKNAK);
Russell King0cfe61e2007-05-10 03:15:32 -0700560 writel(icr, _ICR(i2c));
Jean Delvare7d054812007-05-01 23:26:33 +0200561}
562
Mike Rapoportb7a36702008-01-27 18:14:50 +0100563static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
564{
565 /* make timeout the same as for interrupt based functions */
566 long timeout = 2 * DEF_TIMEOUT;
567
568 /*
569 * Wait for the bus to become free.
570 */
571 while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
572 udelay(1000);
573 show_state(i2c);
574 }
575
576 if (timeout <= 0) {
577 show_state(i2c);
578 dev_err(&i2c->adap.dev,
579 "i2c_pxa: timeout waiting for bus free\n");
580 return I2C_RETRY;
581 }
582
583 /*
584 * Set master mode.
585 */
586 writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
587
588 return 0;
589}
590
591static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
592 struct i2c_msg *msg, int num)
593{
594 unsigned long timeout = 500000; /* 5 seconds */
595 int ret = 0;
596
597 ret = i2c_pxa_pio_set_master(i2c);
598 if (ret)
599 goto out;
600
601 i2c->msg = msg;
602 i2c->msg_num = num;
603 i2c->msg_idx = 0;
604 i2c->msg_ptr = 0;
605 i2c->irqlogidx = 0;
606
607 i2c_pxa_start_message(i2c);
608
609 while (timeout-- && i2c->msg_num > 0) {
610 i2c_pxa_handler(0, i2c);
611 udelay(10);
612 }
613
614 i2c_pxa_stop_message(i2c);
615
616 /*
617 * We place the return code in i2c->msg_idx.
618 */
619 ret = i2c->msg_idx;
620
621out:
622 if (timeout == 0)
623 i2c_pxa_scream_blue_murder(i2c, "timeout");
624
625 return ret;
626}
627
Russell Kingb652b432005-06-15 12:38:14 +0100628/*
Jean Delvare3fb9a652006-01-18 23:17:01 +0100629 * We are protected by the adapter bus mutex.
Russell Kingb652b432005-06-15 12:38:14 +0100630 */
631static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
632{
633 long timeout;
634 int ret;
635
636 /*
637 * Wait for the bus to become free.
638 */
639 ret = i2c_pxa_wait_bus_not_busy(i2c);
640 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100641 dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
Russell Kingb652b432005-06-15 12:38:14 +0100642 goto out;
643 }
644
645 /*
646 * Set master mode.
647 */
648 ret = i2c_pxa_set_master(i2c);
649 if (ret) {
Russell King6fd60fa2005-09-08 21:04:58 +0100650 dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
Russell Kingb652b432005-06-15 12:38:14 +0100651 goto out;
652 }
653
654 spin_lock_irq(&i2c->lock);
655
656 i2c->msg = msg;
657 i2c->msg_num = num;
658 i2c->msg_idx = 0;
659 i2c->msg_ptr = 0;
660 i2c->irqlogidx = 0;
661
662 i2c_pxa_start_message(i2c);
663
664 spin_unlock_irq(&i2c->lock);
665
666 /*
667 * The rest of the processing occurs in the interrupt handler.
668 */
669 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
Jean Delvare7d054812007-05-01 23:26:33 +0200670 i2c_pxa_stop_message(i2c);
Russell Kingb652b432005-06-15 12:38:14 +0100671
672 /*
673 * We place the return code in i2c->msg_idx.
674 */
675 ret = i2c->msg_idx;
676
677 if (timeout == 0)
678 i2c_pxa_scream_blue_murder(i2c, "timeout");
679
680 out:
681 return ret;
682}
683
Mike Rapoportb7a36702008-01-27 18:14:50 +0100684static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
685 struct i2c_msg msgs[], int num)
686{
687 struct pxa_i2c *i2c = adap->algo_data;
688 int ret, i;
689
690 /* If the I2C controller is disabled we need to reset it
691 (probably due to a suspend/resume destroying state). We do
692 this here as we can then avoid worrying about resuming the
693 controller before its users. */
694 if (!(readl(_ICR(i2c)) & ICR_IUE))
695 i2c_pxa_reset(i2c);
696
697 for (i = adap->retries; i >= 0; i--) {
698 ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
699 if (ret != I2C_RETRY)
700 goto out;
701
702 if (i2c_debug)
703 dev_dbg(&adap->dev, "Retrying transmission\n");
704 udelay(100);
705 }
706 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
707 ret = -EREMOTEIO;
708 out:
709 i2c_pxa_set_slave(i2c, ret);
710 return ret;
711}
712
Russell Kingb652b432005-06-15 12:38:14 +0100713/*
714 * i2c_pxa_master_complete - complete the message and wake up.
715 */
716static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
717{
718 i2c->msg_ptr = 0;
719 i2c->msg = NULL;
720 i2c->msg_idx ++;
721 i2c->msg_num = 0;
722 if (ret)
723 i2c->msg_idx = ret;
Mike Rapoportb7a36702008-01-27 18:14:50 +0100724 if (!i2c->use_pio)
725 wake_up(&i2c->wait);
Russell Kingb652b432005-06-15 12:38:14 +0100726}
727
728static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
729{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100730 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100731
732 again:
733 /*
734 * If ISR_ALD is set, we lost arbitration.
735 */
736 if (isr & ISR_ALD) {
737 /*
738 * Do we need to do anything here? The PXA docs
739 * are vague about what happens.
740 */
741 i2c_pxa_scream_blue_murder(i2c, "ALD set");
742
743 /*
744 * We ignore this error. We seem to see spurious ALDs
745 * for seemingly no reason. If we handle them as I think
746 * they should, we end up causing an I2C error, which
747 * is painful for some systems.
748 */
749 return; /* ignore */
750 }
751
752 if (isr & ISR_BED) {
753 int ret = BUS_ERROR;
754
755 /*
756 * I2C bus error - either the device NAK'd us, or
757 * something more serious happened. If we were NAK'd
758 * on the initial address phase, we can retry.
759 */
760 if (isr & ISR_ACKNAK) {
761 if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
762 ret = I2C_RETRY;
763 else
764 ret = XFER_NAKED;
765 }
766 i2c_pxa_master_complete(i2c, ret);
767 } else if (isr & ISR_RWM) {
768 /*
769 * Read mode. We have just sent the address byte, and
770 * now we must initiate the transfer.
771 */
772 if (i2c->msg_ptr == i2c->msg->len - 1 &&
773 i2c->msg_idx == i2c->msg_num - 1)
774 icr |= ICR_STOP | ICR_ACKNAK;
775
776 icr |= ICR_ALDIE | ICR_TB;
777 } else if (i2c->msg_ptr < i2c->msg->len) {
778 /*
779 * Write mode. Write the next data byte.
780 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100781 writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100782
783 icr |= ICR_ALDIE | ICR_TB;
784
785 /*
786 * If this is the last byte of the last message, send
787 * a STOP.
788 */
789 if (i2c->msg_ptr == i2c->msg->len &&
790 i2c->msg_idx == i2c->msg_num - 1)
791 icr |= ICR_STOP;
792 } else if (i2c->msg_idx < i2c->msg_num - 1) {
793 /*
794 * Next segment of the message.
795 */
796 i2c->msg_ptr = 0;
797 i2c->msg_idx ++;
798 i2c->msg++;
799
800 /*
801 * If we aren't doing a repeated start and address,
802 * go back and try to send the next byte. Note that
803 * we do not support switching the R/W direction here.
804 */
805 if (i2c->msg->flags & I2C_M_NOSTART)
806 goto again;
807
808 /*
809 * Write the next address.
810 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100811 writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100812
813 /*
814 * And trigger a repeated start, and send the byte.
815 */
816 icr &= ~ICR_ALDIE;
817 icr |= ICR_START | ICR_TB;
818 } else {
819 if (i2c->msg->len == 0) {
820 /*
821 * Device probes have a message length of zero
822 * and need the bus to be reset before it can
823 * be used again.
824 */
825 i2c_pxa_reset(i2c);
826 }
827 i2c_pxa_master_complete(i2c, 0);
828 }
829
830 i2c->icrlog[i2c->irqlogidx-1] = icr;
831
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100832 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100833 show_state(i2c);
834}
835
836static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
837{
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100838 u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
Russell Kingb652b432005-06-15 12:38:14 +0100839
840 /*
841 * Read the byte.
842 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100843 i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100844
845 if (i2c->msg_ptr < i2c->msg->len) {
846 /*
847 * If this is the last byte of the last
848 * message, send a STOP.
849 */
850 if (i2c->msg_ptr == i2c->msg->len - 1)
851 icr |= ICR_STOP | ICR_ACKNAK;
852
853 icr |= ICR_ALDIE | ICR_TB;
854 } else {
855 i2c_pxa_master_complete(i2c, 0);
856 }
857
858 i2c->icrlog[i2c->irqlogidx-1] = icr;
859
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100860 writel(icr, _ICR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100861}
862
David Howells7d12e782006-10-05 14:55:46 +0100863static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
Russell Kingb652b432005-06-15 12:38:14 +0100864{
865 struct pxa_i2c *i2c = dev_id;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100866 u32 isr = readl(_ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100867
868 if (i2c_debug > 2 && 0) {
Russell King6fd60fa2005-09-08 21:04:58 +0100869 dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100870 __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
Russell Kingb652b432005-06-15 12:38:14 +0100871 decode_ISR(isr);
872 }
873
Tobias Klauser7e3d7db2006-01-09 23:19:51 +0100874 if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
Russell Kingb652b432005-06-15 12:38:14 +0100875 i2c->isrlog[i2c->irqlogidx++] = isr;
876
877 show_state(i2c);
878
879 /*
880 * Always clear all pending IRQs.
881 */
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100882 writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
Russell Kingb652b432005-06-15 12:38:14 +0100883
884 if (isr & ISR_SAD)
885 i2c_pxa_slave_start(i2c, isr);
886 if (isr & ISR_SSD)
887 i2c_pxa_slave_stop(i2c);
888
889 if (i2c_pxa_is_slavemode(i2c)) {
890 if (isr & ISR_ITE)
891 i2c_pxa_slave_txempty(i2c, isr);
892 if (isr & ISR_IRF)
893 i2c_pxa_slave_rxfull(i2c, isr);
894 } else if (i2c->msg) {
895 if (isr & ISR_ITE)
896 i2c_pxa_irq_txempty(i2c, isr);
897 if (isr & ISR_IRF)
898 i2c_pxa_irq_rxfull(i2c, isr);
899 } else {
900 i2c_pxa_scream_blue_murder(i2c, "spurious irq");
901 }
902
903 return IRQ_HANDLED;
904}
905
906
907static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
908{
909 struct pxa_i2c *i2c = adap->algo_data;
910 int ret, i;
911
912 for (i = adap->retries; i >= 0; i--) {
913 ret = i2c_pxa_do_xfer(i2c, msgs, num);
914 if (ret != I2C_RETRY)
915 goto out;
916
917 if (i2c_debug)
Russell King6fd60fa2005-09-08 21:04:58 +0100918 dev_dbg(&adap->dev, "Retrying transmission\n");
Russell Kingb652b432005-06-15 12:38:14 +0100919 udelay(100);
920 }
921 i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
922 ret = -EREMOTEIO;
923 out:
924 i2c_pxa_set_slave(i2c, ret);
925 return ret;
926}
927
Russell Kingda16e322005-09-14 22:54:45 +0100928static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
929{
930 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
931}
932
Jean Delvare8f9082c2006-09-03 22:39:46 +0200933static const struct i2c_algorithm i2c_pxa_algorithm = {
Russell Kingb652b432005-06-15 12:38:14 +0100934 .master_xfer = i2c_pxa_xfer,
Russell Kingda16e322005-09-14 22:54:45 +0100935 .functionality = i2c_pxa_functionality,
Russell Kingb652b432005-06-15 12:38:14 +0100936};
937
Mike Rapoportb7a36702008-01-27 18:14:50 +0100938static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
939 .master_xfer = i2c_pxa_pio_xfer,
940 .functionality = i2c_pxa_functionality,
941};
942
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100943#define res_len(r) ((r)->end - (r)->start + 1)
Russell King3ae5eae2005-11-09 22:32:44 +0000944static int i2c_pxa_probe(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +0100945{
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100946 struct pxa_i2c *i2c;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100947 struct resource *res;
Russell King3ae5eae2005-11-09 22:32:44 +0000948 struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
Russell Kingb652b432005-06-15 12:38:14 +0100949 int ret;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100950 int irq;
Russell Kingb652b432005-06-15 12:38:14 +0100951
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100952 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
953 irq = platform_get_irq(dev, 0);
954 if (res == NULL || irq < 0)
955 return -ENODEV;
956
957 if (!request_mem_region(res->start, res_len(res), res->name))
958 return -ENOMEM;
959
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100960 i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100961 if (!i2c) {
962 ret = -ENOMEM;
963 goto emalloc;
964 }
965
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100966 i2c->adap.owner = THIS_MODULE;
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100967 i2c->adap.retries = 5;
968
969 spin_lock_init(&i2c->lock);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100970 init_waitqueue_head(&i2c->wait);
Enrico Scholz6776f3d2007-05-21 12:29:40 +0100971
Wolfram Sanga92b36e2008-02-24 20:03:42 +0100972 /*
973 * If "dev->id" is negative we consider it as zero.
974 * The reason to do so is to avoid sysfs names that only make
975 * sense when there are multiple adapters.
976 */
977 i2c->adap.nr = dev->id != -1 ? dev->id : 0;
978 snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
979 i2c->adap.nr);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100980
Russell Kingc3cef3f2007-08-20 10:19:10 +0100981 i2c->clk = clk_get(&dev->dev, "I2CCLK");
982 if (IS_ERR(i2c->clk)) {
983 ret = PTR_ERR(i2c->clk);
984 goto eclk;
985 }
986
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100987 i2c->reg_base = ioremap(res->start, res_len(res));
988 if (!i2c->reg_base) {
989 ret = -EIO;
990 goto eremap;
991 }
Mike Rapoport9ba63c42008-08-17 06:23:05 +0100992 i2c->reg_shift = (cpu_is_pxa3xx() && (dev->id == 1)) ? 0 : 1;
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +0100993
994 i2c->iobase = res->start;
995 i2c->iosize = res_len(res);
996
997 i2c->irq = irq;
Russell Kingb652b432005-06-15 12:38:14 +0100998
999 i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
1000
1001#ifdef CONFIG_I2C_PXA_SLAVE
Russell Kingb652b432005-06-15 12:38:14 +01001002 if (plat) {
1003 i2c->slave_addr = plat->slave_addr;
Russell Kingbeea4942006-11-07 21:03:20 +00001004 i2c->slave = plat->slave;
Russell Kingb652b432005-06-15 12:38:14 +01001005 }
1006#endif
1007
Russell Kingc3cef3f2007-08-20 10:19:10 +01001008 clk_enable(i2c->clk);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001009
Mike Rapoportb7a36702008-01-27 18:14:50 +01001010 if (plat) {
1011 i2c->adap.class = plat->class;
1012 i2c->use_pio = plat->use_pio;
1013 }
1014
1015 if (i2c->use_pio) {
1016 i2c->adap.algo = &i2c_pxa_pio_algorithm;
1017 } else {
1018 i2c->adap.algo = &i2c_pxa_algorithm;
1019 ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
1020 i2c->adap.name, i2c);
1021 if (ret)
1022 goto ereqirq;
1023 }
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001024
Russell Kingb652b432005-06-15 12:38:14 +01001025 i2c_pxa_reset(i2c);
1026
1027 i2c->adap.algo_data = i2c;
Russell King3ae5eae2005-11-09 22:32:44 +00001028 i2c->adap.dev.parent = &dev->dev;
Russell Kingb652b432005-06-15 12:38:14 +01001029
Rodolfo Giometti066af982007-07-12 14:12:30 +02001030 ret = i2c_add_numbered_adapter(&i2c->adap);
Russell Kingb652b432005-06-15 12:38:14 +01001031 if (ret < 0) {
1032 printk(KERN_INFO "I2C: Failed to add bus\n");
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001033 goto eadapt;
Russell Kingb652b432005-06-15 12:38:14 +01001034 }
1035
Russell King3ae5eae2005-11-09 22:32:44 +00001036 platform_set_drvdata(dev, i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001037
1038#ifdef CONFIG_I2C_PXA_SLAVE
1039 printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
1040 i2c->adap.dev.bus_id, i2c->slave_addr);
1041#else
1042 printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
1043 i2c->adap.dev.bus_id);
1044#endif
1045 return 0;
1046
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001047eadapt:
Mike Rapoportb7a36702008-01-27 18:14:50 +01001048 if (!i2c->use_pio)
1049 free_irq(irq, i2c);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001050ereqirq:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001051 clk_disable(i2c->clk);
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001052 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001053eremap:
Russell Kingc3cef3f2007-08-20 10:19:10 +01001054 clk_put(i2c->clk);
1055eclk:
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001056 kfree(i2c);
1057emalloc:
1058 release_mem_region(res->start, res_len(res));
Russell Kingb652b432005-06-15 12:38:14 +01001059 return ret;
1060}
1061
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001062static int __exit i2c_pxa_remove(struct platform_device *dev)
Russell Kingb652b432005-06-15 12:38:14 +01001063{
Russell King3ae5eae2005-11-09 22:32:44 +00001064 struct pxa_i2c *i2c = platform_get_drvdata(dev);
Russell Kingb652b432005-06-15 12:38:14 +01001065
Russell King3ae5eae2005-11-09 22:32:44 +00001066 platform_set_drvdata(dev, NULL);
Russell Kingb652b432005-06-15 12:38:14 +01001067
1068 i2c_del_adapter(&i2c->adap);
Mike Rapoportb7a36702008-01-27 18:14:50 +01001069 if (!i2c->use_pio)
1070 free_irq(i2c->irq, i2c);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001071
1072 clk_disable(i2c->clk);
1073 clk_put(i2c->clk);
Russell Kingc3cef3f2007-08-20 10:19:10 +01001074
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001075 iounmap(i2c->reg_base);
Guennadi Liakhovetskia7b4e552007-02-08 09:43:26 +01001076 release_mem_region(i2c->iobase, i2c->iosize);
1077 kfree(i2c);
Russell Kingb652b432005-06-15 12:38:14 +01001078
1079 return 0;
1080}
1081
Russell Kinge7d48fa2008-08-26 10:40:50 +01001082#ifdef CONFIG_PM
1083static int i2c_pxa_suspend_late(struct platform_device *dev, pm_message_t state)
1084{
1085 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1086 clk_disable(i2c->clk);
1087 return 0;
1088}
1089
1090static int i2c_pxa_resume_early(struct platform_device *dev)
1091{
1092 struct pxa_i2c *i2c = platform_get_drvdata(dev);
1093
1094 clk_enable(i2c->clk);
1095 i2c_pxa_reset(i2c);
1096
1097 return 0;
1098}
1099#else
1100#define i2c_pxa_suspend_late NULL
1101#define i2c_pxa_resume_early NULL
1102#endif
1103
Russell King3ae5eae2005-11-09 22:32:44 +00001104static struct platform_driver i2c_pxa_driver = {
Russell Kingb652b432005-06-15 12:38:14 +01001105 .probe = i2c_pxa_probe,
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001106 .remove = __exit_p(i2c_pxa_remove),
Russell Kinge7d48fa2008-08-26 10:40:50 +01001107 .suspend_late = i2c_pxa_suspend_late,
1108 .resume_early = i2c_pxa_resume_early,
Russell King3ae5eae2005-11-09 22:32:44 +00001109 .driver = {
1110 .name = "pxa2xx-i2c",
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001111 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001112 },
Russell Kingb652b432005-06-15 12:38:14 +01001113};
1114
1115static int __init i2c_adap_pxa_init(void)
1116{
Russell King3ae5eae2005-11-09 22:32:44 +00001117 return platform_driver_register(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001118}
1119
Wolfram Sanga92b36e2008-02-24 20:03:42 +01001120static void __exit i2c_adap_pxa_exit(void)
Russell Kingb652b432005-06-15 12:38:14 +01001121{
Holger Schurigd6a7b5f2008-02-11 16:51:41 +01001122 platform_driver_unregister(&i2c_pxa_driver);
Russell Kingb652b432005-06-15 12:38:14 +01001123}
1124
Richard Purdieece5f7b2006-01-12 16:30:23 +00001125MODULE_LICENSE("GPL");
Kay Sieversadd8eda2008-04-22 22:16:49 +02001126MODULE_ALIAS("platform:pxa2xx-i2c");
Richard Purdieece5f7b2006-01-12 16:30:23 +00001127
Uli Luckas47a9b132008-07-14 22:38:30 +02001128subsys_initcall(i2c_adap_pxa_init);
Russell Kingb652b432005-06-15 12:38:14 +01001129module_exit(i2c_adap_pxa_exit);