i2c-pxa.c 28.1 KB
Newer Older
R
Russell King 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  i2c_adap_pxa.c
 *
 *  I2C adapter for the PXA I2C bus access.
 *
 *  Copyright (C) 2002 Intrinsyc Software Inc.
 *  Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  History:
 *    Apr 2002: Initial version [CS]
D
Daniel Mack 已提交
15
 *    Jun 2002: Properly separated algo/adap [FB]
R
Russell King 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *    Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
 *    Jan 2003: added limited signal handling [Kai-Uwe Bloem]
 *    Sep 2004: Major rework to ensure efficient bus handling [RMK]
 *    Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
 *    Feb 2005: Rework slave mode handling [RMK]
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/i2c-pxa.h>
32
#include <linux/platform_device.h>
33 34
#include <linux/err.h>
#include <linux/clk.h>
35
#include <linux/slab.h>
36
#include <linux/io.h>
37
#include <linux/i2c/pxa-i2c.h>
R
Russell King 已提交
38 39

#include <asm/irq.h>
40

41 42 43 44 45 46 47
#ifndef CONFIG_HAVE_CLK
#define clk_get(dev, id)	NULL
#define clk_put(clk)		do { } while (0)
#define clk_disable(clk)	do { } while (0)
#define clk_enable(clk)		do { } while (0)
#endif

48 49 50 51 52 53 54 55 56 57 58
struct pxa_reg_layout {
	u32 ibmr;
	u32 idbr;
	u32 icr;
	u32 isr;
	u32 isar;
};

enum pxa_i2c_types {
	REGS_PXA2XX,
	REGS_PXA3XX,
59
	REGS_CE4100,
60 61
};

62
/*
63
 * I2C registers definitions
64
 */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static struct pxa_reg_layout pxa_reg_layout[] = {
	[REGS_PXA2XX] = {
		.ibmr =	0x00,
		.idbr =	0x10,
		.icr =	0x20,
		.isr =	0x30,
		.isar =	0x40,
	},
	[REGS_PXA3XX] = {
		.ibmr =	0x00,
		.idbr =	0x08,
		.icr =	0x10,
		.isr =	0x18,
		.isar =	0x20,
	},
80 81 82 83 84 85 86
	[REGS_CE4100] = {
		.ibmr =	0x14,
		.idbr =	0x0c,
		.icr =	0x00,
		.isr =	0x04,
		/* no isar register */
	},
87
};
88 89

static const struct platform_device_id i2c_pxa_id_table[] = {
90 91
	{ "pxa2xx-i2c",		REGS_PXA2XX },
	{ "pxa3xx-pwri2c",	REGS_PXA3XX },
92
	{ "ce4100-i2c",		REGS_CE4100 },
93 94 95 96
	{ },
};
MODULE_DEVICE_TABLE(platform, i2c_pxa_id_table);

97
/*
98
 * I2C bit definitions
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
 */

#define ICR_START	(1 << 0)	   /* start bit */
#define ICR_STOP	(1 << 1)	   /* stop bit */
#define ICR_ACKNAK	(1 << 2)	   /* send ACK(0) or NAK(1) */
#define ICR_TB		(1 << 3)	   /* transfer byte bit */
#define ICR_MA		(1 << 4)	   /* master abort */
#define ICR_SCLE	(1 << 5)	   /* master clock enable */
#define ICR_IUE		(1 << 6)	   /* unit enable */
#define ICR_GCD		(1 << 7)	   /* general call disable */
#define ICR_ITEIE	(1 << 8)	   /* enable tx interrupts */
#define ICR_IRFIE	(1 << 9)	   /* enable rx interrupts */
#define ICR_BEIE	(1 << 10)	   /* enable bus error ints */
#define ICR_SSDIE	(1 << 11)	   /* slave STOP detected int enable */
#define ICR_ALDIE	(1 << 12)	   /* enable arbitration interrupt */
#define ICR_SADIE	(1 << 13)	   /* slave address detected int enable */
#define ICR_UR		(1 << 14)	   /* unit reset */
#define ICR_FM		(1 << 15)	   /* fast mode */

#define ISR_RWM		(1 << 0)	   /* read/write mode */
#define ISR_ACKNAK	(1 << 1)	   /* ack/nak status */
#define ISR_UB		(1 << 2)	   /* unit busy */
#define ISR_IBB		(1 << 3)	   /* bus busy */
#define ISR_SSD		(1 << 4)	   /* slave stop detected */
#define ISR_ALD		(1 << 5)	   /* arbitration loss detected */
#define ISR_ITE		(1 << 6)	   /* tx buffer empty */
#define ISR_IRF		(1 << 7)	   /* rx buffer full */
#define ISR_GCAD	(1 << 8)	   /* general call address detected */
#define ISR_SAD		(1 << 9)	   /* slave address detected */
#define ISR_BED		(1 << 10)	   /* bus error no ACK/NAK */
R
Russell King 已提交
129 130 131 132 133 134 135 136 137 138 139

struct pxa_i2c {
	spinlock_t		lock;
	wait_queue_head_t	wait;
	struct i2c_msg		*msg;
	unsigned int		msg_num;
	unsigned int		msg_idx;
	unsigned int		msg_ptr;
	unsigned int		slave_addr;

	struct i2c_adapter	adap;
140
	struct clk		*clk;
R
Russell King 已提交
141 142 143 144 145 146 147
#ifdef CONFIG_I2C_PXA_SLAVE
	struct i2c_slave_client *slave;
#endif

	unsigned int		irqlogidx;
	u32			isrlog[32];
	u32			icrlog[32];
148 149

	void __iomem		*reg_base;
150 151 152 153 154
	void __iomem		*reg_ibmr;
	void __iomem		*reg_idbr;
	void __iomem		*reg_icr;
	void __iomem		*reg_isr;
	void __iomem		*reg_isar;
155 156 157 158 159

	unsigned long		iobase;
	unsigned long		iosize;

	int			irq;
160 161
	unsigned int		use_pio :1;
	unsigned int		fast_mode :1;
R
Russell King 已提交
162 163
};

164 165 166 167 168
#define _IBMR(i2c)	((i2c)->reg_ibmr)
#define _IDBR(i2c)	((i2c)->reg_idbr)
#define _ICR(i2c)	((i2c)->reg_icr)
#define _ISR(i2c)	((i2c)->reg_isr)
#define _ISAR(i2c)	((i2c)->reg_isar)
169

R
Russell King 已提交
170 171 172 173 174 175 176 177 178 179 180 181
/*
 * I2C Slave mode address
 */
#define I2C_PXA_SLAVE_ADDR      0x1

#ifdef DEBUG

struct bits {
	u32	mask;
	const char *set;
	const char *unset;
};
J
Jiri Slaby 已提交
182
#define PXA_BIT(m, s, u)	{ .mask = m, .set = s, .unset = u }
R
Russell King 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196

static inline void
decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
{
	printk("%s %08x: ", prefix, val);
	while (num--) {
		const char *str = val & bits->mask ? bits->set : bits->unset;
		if (str)
			printk("%s ", str);
		bits++;
	}
}

static const struct bits isr_bits[] = {
J
Jiri Slaby 已提交
197 198 199 200 201 202 203 204 205 206 207
	PXA_BIT(ISR_RWM,	"RX",		"TX"),
	PXA_BIT(ISR_ACKNAK,	"NAK",		"ACK"),
	PXA_BIT(ISR_UB,		"Bsy",		"Rdy"),
	PXA_BIT(ISR_IBB,	"BusBsy",	"BusRdy"),
	PXA_BIT(ISR_SSD,	"SlaveStop",	NULL),
	PXA_BIT(ISR_ALD,	"ALD",		NULL),
	PXA_BIT(ISR_ITE,	"TxEmpty",	NULL),
	PXA_BIT(ISR_IRF,	"RxFull",	NULL),
	PXA_BIT(ISR_GCAD,	"GenCall",	NULL),
	PXA_BIT(ISR_SAD,	"SlaveAddr",	NULL),
	PXA_BIT(ISR_BED,	"BusErr",	NULL),
R
Russell King 已提交
208 209 210 211
};

static void decode_ISR(unsigned int val)
{
212
	decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
R
Russell King 已提交
213 214 215 216
	printk("\n");
}

static const struct bits icr_bits[] = {
J
Jiri Slaby 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	PXA_BIT(ICR_START,  "START",	NULL),
	PXA_BIT(ICR_STOP,   "STOP",	NULL),
	PXA_BIT(ICR_ACKNAK, "ACKNAK",	NULL),
	PXA_BIT(ICR_TB,     "TB",	NULL),
	PXA_BIT(ICR_MA,     "MA",	NULL),
	PXA_BIT(ICR_SCLE,   "SCLE",	"scle"),
	PXA_BIT(ICR_IUE,    "IUE",	"iue"),
	PXA_BIT(ICR_GCD,    "GCD",	NULL),
	PXA_BIT(ICR_ITEIE,  "ITEIE",	NULL),
	PXA_BIT(ICR_IRFIE,  "IRFIE",	NULL),
	PXA_BIT(ICR_BEIE,   "BEIE",	NULL),
	PXA_BIT(ICR_SSDIE,  "SSDIE",	NULL),
	PXA_BIT(ICR_ALDIE,  "ALDIE",	NULL),
	PXA_BIT(ICR_SADIE,  "SADIE",	NULL),
	PXA_BIT(ICR_UR,     "UR",		"ur"),
R
Russell King 已提交
232 233
};

234
#ifdef CONFIG_I2C_PXA_SLAVE
R
Russell King 已提交
235 236
static void decode_ICR(unsigned int val)
{
237
	decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
R
Russell King 已提交
238 239
	printk("\n");
}
240
#endif
R
Russell King 已提交
241 242 243 244 245

static unsigned int i2c_debug = DEBUG;

static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
{
246 247
	dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
		readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
R
Russell King 已提交
248 249
}

250
#define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __func__)
R
Russell King 已提交
251 252 253 254

static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
{
	unsigned int i;
255 256
	printk(KERN_ERR "i2c: error: %s\n", why);
	printk(KERN_ERR "i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
R
Russell King 已提交
257
		i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
258 259 260
	printk(KERN_ERR "i2c: ICR: %08x ISR: %08x\n",
	       readl(_ICR(i2c)), readl(_ISR(i2c)));
	printk(KERN_DEBUG "i2c: log: ");
R
Russell King 已提交
261 262 263 264 265
	for (i = 0; i < i2c->irqlogidx; i++)
		printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
	printk("\n");
}

266 267 268 269 270 271 272 273 274 275 276 277 278 279
#else /* ifdef DEBUG */

#define i2c_debug	0

#define show_state(i2c) do { } while (0)
#define decode_ISR(val) do { } while (0)
#define decode_ICR(val) do { } while (0)
#define i2c_pxa_scream_blue_murder(i2c, why) do { } while (0)

#endif /* ifdef DEBUG / else */

static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);

R
Russell King 已提交
280 281
static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
{
282
	return !(readl(_ICR(i2c)) & ICR_SCLE);
R
Russell King 已提交
283 284 285 286
}

static void i2c_pxa_abort(struct pxa_i2c *i2c)
{
287
	int i = 250;
R
Russell King 已提交
288 289

	if (i2c_pxa_is_slavemode(i2c)) {
290
		dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
R
Russell King 已提交
291 292 293
		return;
	}

294
	while ((i > 0) && (readl(_IBMR(i2c)) & 0x1) == 0) {
295
		unsigned long icr = readl(_ICR(i2c));
R
Russell King 已提交
296 297 298 299

		icr &= ~ICR_START;
		icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;

300
		writel(icr, _ICR(i2c));
R
Russell King 已提交
301 302 303

		show_state(i2c);

304 305
		mdelay(1);
		i --;
R
Russell King 已提交
306 307
	}

308 309
	writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
	       _ICR(i2c));
R
Russell King 已提交
310 311 312 313 314 315
}

static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
{
	int timeout = DEF_TIMEOUT;

316 317
	while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
		if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
R
Russell King 已提交
318 319 320 321 322 323
			timeout += 4;

		msleep(2);
		show_state(i2c);
	}

R
Roel Kluin 已提交
324
	if (timeout < 0)
R
Russell King 已提交
325 326
		show_state(i2c);

R
Roel Kluin 已提交
327
	return timeout < 0 ? I2C_RETRY : 0;
R
Russell King 已提交
328 329 330 331 332 333 334 335
}

static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
{
	unsigned long timeout = jiffies + HZ*4;

	while (time_before(jiffies, timeout)) {
		if (i2c_debug > 1)
336
			dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
337
				__func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
R
Russell King 已提交
338

339
		if (readl(_ISR(i2c)) & ISR_SAD) {
R
Russell King 已提交
340
			if (i2c_debug > 0)
341
				dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
R
Russell King 已提交
342 343 344 345 346 347 348
			goto out;
		}

		/* wait for unit and bus being not busy, and we also do a
		 * quick check of the i2c lines themselves to ensure they've
		 * gone high...
		 */
349
		if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
R
Russell King 已提交
350
			if (i2c_debug > 0)
351
				dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
R
Russell King 已提交
352 353 354 355 356 357 358
			return 1;
		}

		msleep(1);
	}

	if (i2c_debug > 0)
359
		dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
R
Russell King 已提交
360 361 362 363 364 365 366
 out:
	return 0;
}

static int i2c_pxa_set_master(struct pxa_i2c *i2c)
{
	if (i2c_debug)
367
		dev_dbg(&i2c->adap.dev, "setting to bus master\n");
R
Russell King 已提交
368

369
	if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
370
		dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
R
Russell King 已提交
371
		if (!i2c_pxa_wait_master(i2c)) {
372
			dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
R
Russell King 已提交
373 374 375 376
			return I2C_RETRY;
		}
	}

377
	writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
R
Russell King 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391
	return 0;
}

#ifdef CONFIG_I2C_PXA_SLAVE
static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
{
	unsigned long timeout = jiffies + HZ*1;

	/* wait for stop */

	show_state(i2c);

	while (time_before(jiffies, timeout)) {
		if (i2c_debug > 1)
392
			dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
393
				__func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
R
Russell King 已提交
394

395 396 397
		if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
		    (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
		    (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
R
Russell King 已提交
398
			if (i2c_debug > 1)
399
				dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
R
Russell King 已提交
400 401 402 403 404 405 406
			return 1;
		}

		msleep(1);
	}

	if (i2c_debug > 0)
407
		dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
R
Russell King 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	return 0;
}

/*
 * clear the hold on the bus, and take of anything else
 * that has been configured
 */
static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
{
	show_state(i2c);

	if (errcode < 0) {
		udelay(100);   /* simple delay */
	} else {
		/* we need to wait for the stop condition to end */

		/* if we where in stop, then clear... */
425
		if (readl(_ICR(i2c)) & ICR_STOP) {
R
Russell King 已提交
426
			udelay(100);
427
			writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
R
Russell King 已提交
428 429 430
		}

		if (!i2c_pxa_wait_slave(i2c)) {
431 432
			dev_err(&i2c->adap.dev, "%s: wait timedout\n",
				__func__);
R
Russell King 已提交
433 434 435 436
			return;
		}
	}

437 438
	writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
	writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
R
Russell King 已提交
439 440

	if (i2c_debug) {
441 442
		dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
		decode_ICR(readl(_ICR(i2c)));
R
Russell King 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456
	}
}
#else
#define i2c_pxa_set_slave(i2c, err)	do { } while (0)
#endif

static void i2c_pxa_reset(struct pxa_i2c *i2c)
{
	pr_debug("Resetting I2C Controller Unit\n");

	/* abort any transfer currently under way */
	i2c_pxa_abort(i2c);

	/* reset according to 9.8 */
457 458 459
	writel(ICR_UR, _ICR(i2c));
	writel(I2C_ISR_INIT, _ISR(i2c));
	writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
R
Russell King 已提交
460

461 462
	if (i2c->reg_isar)
		writel(i2c->slave_addr, _ISAR(i2c));
R
Russell King 已提交
463 464

	/* set control register values */
465
	writel(I2C_ICR_INIT | (i2c->fast_mode ? ICR_FM : 0), _ICR(i2c));
R
Russell King 已提交
466 467

#ifdef CONFIG_I2C_PXA_SLAVE
468
	dev_info(&i2c->adap.dev, "Enabling slave mode\n");
469
	writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
R
Russell King 已提交
470 471 472 473 474
#endif

	i2c_pxa_set_slave(i2c, 0);

	/* enable unit */
475
	writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
R
Russell King 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489
	udelay(100);
}


#ifdef CONFIG_I2C_PXA_SLAVE
/*
 * PXA I2C Slave mode
 */

static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
{
	if (isr & ISR_BED) {
		/* what should we do here? */
	} else {
490 491 492 493
		int ret = 0;

		if (i2c->slave != NULL)
			ret = i2c->slave->read(i2c->slave->data);
R
Russell King 已提交
494

495 496
		writel(ret, _IDBR(i2c));
		writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));   /* allow next byte */
R
Russell King 已提交
497 498 499 500 501
	}
}

static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
{
502
	unsigned int byte = readl(_IDBR(i2c));
R
Russell King 已提交
503 504 505 506

	if (i2c->slave != NULL)
		i2c->slave->write(i2c->slave->data, byte);

507
	writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
R
Russell King 已提交
508 509 510 511 512 513 514
}

static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
{
	int timeout;

	if (i2c_debug > 0)
515
		dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
R
Russell King 已提交
516 517 518 519 520 521 522 523 524 525 526
		       (isr & ISR_RWM) ? 'r' : 't');

	if (i2c->slave != NULL)
		i2c->slave->event(i2c->slave->data,
				 (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);

	/*
	 * slave could interrupt in the middle of us generating a
	 * start condition... if this happens, we'd better back off
	 * and stop holding the poor thing up
	 */
527 528
	writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
	writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
R
Russell King 已提交
529 530 531 532

	timeout = 0x10000;

	while (1) {
533
		if ((readl(_IBMR(i2c)) & 2) == 2)
R
Russell King 已提交
534 535 536 537 538
			break;

		timeout--;

		if (timeout <= 0) {
539
			dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
R
Russell King 已提交
540 541 542 543
			break;
		}
	}

544
	writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
R
Russell King 已提交
545 546 547 548 549
}

static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
{
	if (i2c_debug > 2)
550
		dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
R
Russell King 已提交
551 552 553 554 555

	if (i2c->slave != NULL)
		i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);

	if (i2c_debug > 2)
556
		dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
R
Russell King 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570

	/*
	 * If we have a master-mode message waiting,
	 * kick it off now that the slave has completed.
	 */
	if (i2c->msg)
		i2c_pxa_master_complete(i2c, I2C_RETRY);
}
#else
static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
{
	if (isr & ISR_BED) {
		/* what should we do here? */
	} else {
571 572
		writel(0, _IDBR(i2c));
		writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
R
Russell King 已提交
573 574 575 576 577
	}
}

static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
{
578
	writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
R
Russell King 已提交
579 580 581 582 583 584 585 586 587 588 589
}

static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
{
	int timeout;

	/*
	 * slave could interrupt in the middle of us generating a
	 * start condition... if this happens, we'd better back off
	 * and stop holding the poor thing up
	 */
590 591
	writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
	writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
R
Russell King 已提交
592 593 594 595

	timeout = 0x10000;

	while (1) {
596
		if ((readl(_IBMR(i2c)) & 2) == 2)
R
Russell King 已提交
597 598 599 600 601
			break;

		timeout--;

		if (timeout <= 0) {
602
			dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
R
Russell King 已提交
603 604 605 606
			break;
		}
	}

607
	writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
R
Russell King 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
}

static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
{
	if (i2c->msg)
		i2c_pxa_master_complete(i2c, I2C_RETRY);
}
#endif

/*
 * PXA I2C Master mode
 */

static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
{
	unsigned int addr = (msg->addr & 0x7f) << 1;

	if (msg->flags & I2C_M_RD)
		addr |= 1;

	return addr;
}

static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
{
	u32 icr;

	/*
	 * Step 1: target slave address into IDBR
	 */
638
	writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
R
Russell King 已提交
639 640 641 642

	/*
	 * Step 2: initiate the write.
	 */
643 644
	icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
	writel(icr | ICR_START | ICR_TB, _ICR(i2c));
R
Russell King 已提交
645 646
}

J
Jean Delvare 已提交
647 648 649 650 651 652 653 654 655
static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
{
	u32 icr;

	/*
	 * Clear the STOP and ACK flags
	 */
	icr = readl(_ICR(i2c));
	icr &= ~(ICR_STOP | ICR_ACKNAK);
R
Russell King 已提交
656
	writel(icr, _ICR(i2c));
J
Jean Delvare 已提交
657 658
}

M
Mike Rapoport 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671
static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
{
	/* make timeout the same as for interrupt based functions */
	long timeout = 2 * DEF_TIMEOUT;

	/*
	 * Wait for the bus to become free.
	 */
	while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
		udelay(1000);
		show_state(i2c);
	}

R
Roel Kluin 已提交
672
	if (timeout < 0) {
M
Mike Rapoport 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		show_state(i2c);
		dev_err(&i2c->adap.dev,
			"i2c_pxa: timeout waiting for bus free\n");
		return I2C_RETRY;
	}

	/*
	 * Set master mode.
	 */
	writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));

	return 0;
}

static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
			       struct i2c_msg *msg, int num)
{
	unsigned long timeout = 500000; /* 5 seconds */
	int ret = 0;

	ret = i2c_pxa_pio_set_master(i2c);
	if (ret)
		goto out;

	i2c->msg = msg;
	i2c->msg_num = num;
	i2c->msg_idx = 0;
	i2c->msg_ptr = 0;
	i2c->irqlogidx = 0;

	i2c_pxa_start_message(i2c);

R
Roel Kluin 已提交
705
	while (i2c->msg_num > 0 && --timeout) {
M
Mike Rapoport 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
		i2c_pxa_handler(0, i2c);
		udelay(10);
	}

	i2c_pxa_stop_message(i2c);

	/*
	 * We place the return code in i2c->msg_idx.
	 */
	ret = i2c->msg_idx;

out:
	if (timeout == 0)
		i2c_pxa_scream_blue_murder(i2c, "timeout");

	return ret;
}

R
Russell King 已提交
724
/*
725
 * We are protected by the adapter bus mutex.
R
Russell King 已提交
726 727 728 729 730 731 732 733 734 735 736
 */
static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
{
	long timeout;
	int ret;

	/*
	 * Wait for the bus to become free.
	 */
	ret = i2c_pxa_wait_bus_not_busy(i2c);
	if (ret) {
737
		dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
R
Russell King 已提交
738 739 740 741 742 743 744 745
		goto out;
	}

	/*
	 * Set master mode.
	 */
	ret = i2c_pxa_set_master(i2c);
	if (ret) {
746
		dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
R
Russell King 已提交
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
		goto out;
	}

	spin_lock_irq(&i2c->lock);

	i2c->msg = msg;
	i2c->msg_num = num;
	i2c->msg_idx = 0;
	i2c->msg_ptr = 0;
	i2c->irqlogidx = 0;

	i2c_pxa_start_message(i2c);

	spin_unlock_irq(&i2c->lock);

	/*
	 * The rest of the processing occurs in the interrupt handler.
	 */
	timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
J
Jean Delvare 已提交
766
	i2c_pxa_stop_message(i2c);
R
Russell King 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779

	/*
	 * We place the return code in i2c->msg_idx.
	 */
	ret = i2c->msg_idx;

	if (timeout == 0)
		i2c_pxa_scream_blue_murder(i2c, "timeout");

 out:
	return ret;
}

M
Mike Rapoport 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
			    struct i2c_msg msgs[], int num)
{
	struct pxa_i2c *i2c = adap->algo_data;
	int ret, i;

	/* If the I2C controller is disabled we need to reset it
	  (probably due to a suspend/resume destroying state). We do
	  this here as we can then avoid worrying about resuming the
	  controller before its users. */
	if (!(readl(_ICR(i2c)) & ICR_IUE))
		i2c_pxa_reset(i2c);

	for (i = adap->retries; i >= 0; i--) {
		ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
		if (ret != I2C_RETRY)
			goto out;

		if (i2c_debug)
			dev_dbg(&adap->dev, "Retrying transmission\n");
		udelay(100);
	}
	i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
	ret = -EREMOTEIO;
 out:
	i2c_pxa_set_slave(i2c, ret);
	return ret;
}

R
Russell King 已提交
809 810 811 812 813 814 815 816 817 818 819
/*
 * i2c_pxa_master_complete - complete the message and wake up.
 */
static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
{
	i2c->msg_ptr = 0;
	i2c->msg = NULL;
	i2c->msg_idx ++;
	i2c->msg_num = 0;
	if (ret)
		i2c->msg_idx = ret;
M
Mike Rapoport 已提交
820 821
	if (!i2c->use_pio)
		wake_up(&i2c->wait);
R
Russell King 已提交
822 823 824 825
}

static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
{
826
	u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
R
Russell King 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

 again:
	/*
	 * If ISR_ALD is set, we lost arbitration.
	 */
	if (isr & ISR_ALD) {
		/*
		 * Do we need to do anything here?  The PXA docs
		 * are vague about what happens.
		 */
		i2c_pxa_scream_blue_murder(i2c, "ALD set");

		/*
		 * We ignore this error.  We seem to see spurious ALDs
		 * for seemingly no reason.  If we handle them as I think
		 * they should, we end up causing an I2C error, which
		 * is painful for some systems.
		 */
		return; /* ignore */
	}

	if (isr & ISR_BED) {
		int ret = BUS_ERROR;

		/*
		 * I2C bus error - either the device NAK'd us, or
		 * something more serious happened.  If we were NAK'd
		 * on the initial address phase, we can retry.
		 */
		if (isr & ISR_ACKNAK) {
			if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
				ret = I2C_RETRY;
			else
				ret = XFER_NAKED;
		}
		i2c_pxa_master_complete(i2c, ret);
	} else if (isr & ISR_RWM) {
		/*
		 * Read mode.  We have just sent the address byte, and
		 * now we must initiate the transfer.
		 */
		if (i2c->msg_ptr == i2c->msg->len - 1 &&
		    i2c->msg_idx == i2c->msg_num - 1)
			icr |= ICR_STOP | ICR_ACKNAK;

		icr |= ICR_ALDIE | ICR_TB;
	} else if (i2c->msg_ptr < i2c->msg->len) {
		/*
		 * Write mode.  Write the next data byte.
		 */
877
		writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
R
Russell King 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

		icr |= ICR_ALDIE | ICR_TB;

		/*
		 * If this is the last byte of the last message, send
		 * a STOP.
		 */
		if (i2c->msg_ptr == i2c->msg->len &&
		    i2c->msg_idx == i2c->msg_num - 1)
			icr |= ICR_STOP;
	} else if (i2c->msg_idx < i2c->msg_num - 1) {
		/*
		 * Next segment of the message.
		 */
		i2c->msg_ptr = 0;
		i2c->msg_idx ++;
		i2c->msg++;

		/*
		 * If we aren't doing a repeated start and address,
		 * go back and try to send the next byte.  Note that
		 * we do not support switching the R/W direction here.
		 */
		if (i2c->msg->flags & I2C_M_NOSTART)
			goto again;

		/*
		 * Write the next address.
		 */
907
		writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
R
Russell King 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

		/*
		 * And trigger a repeated start, and send the byte.
		 */
		icr &= ~ICR_ALDIE;
		icr |= ICR_START | ICR_TB;
	} else {
		if (i2c->msg->len == 0) {
			/*
			 * Device probes have a message length of zero
			 * and need the bus to be reset before it can
			 * be used again.
			 */
			i2c_pxa_reset(i2c);
		}
		i2c_pxa_master_complete(i2c, 0);
	}

	i2c->icrlog[i2c->irqlogidx-1] = icr;

928
	writel(icr, _ICR(i2c));
R
Russell King 已提交
929 930 931 932 933
	show_state(i2c);
}

static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
{
934
	u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
R
Russell King 已提交
935 936 937 938

	/*
	 * Read the byte.
	 */
939
	i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
R
Russell King 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

	if (i2c->msg_ptr < i2c->msg->len) {
		/*
		 * If this is the last byte of the last
		 * message, send a STOP.
		 */
		if (i2c->msg_ptr == i2c->msg->len - 1)
			icr |= ICR_STOP | ICR_ACKNAK;

		icr |= ICR_ALDIE | ICR_TB;
	} else {
		i2c_pxa_master_complete(i2c, 0);
	}

	i2c->icrlog[i2c->irqlogidx-1] = icr;

956
	writel(icr, _ICR(i2c));
R
Russell King 已提交
957 958
}

959 960
#define VALID_INT_SOURCE	(ISR_SSD | ISR_ALD | ISR_ITE | ISR_IRF | \
				ISR_SAD | ISR_BED)
961
static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
R
Russell King 已提交
962 963
{
	struct pxa_i2c *i2c = dev_id;
964
	u32 isr = readl(_ISR(i2c));
R
Russell King 已提交
965

966 967 968 969
	isr &= VALID_INT_SOURCE;
	if (!isr)
		return IRQ_NONE;

R
Russell King 已提交
970
	if (i2c_debug > 2 && 0) {
971
		dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
972
			__func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
R
Russell King 已提交
973 974 975
		decode_ISR(isr);
	}

976
	if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
R
Russell King 已提交
977 978 979 980 981 982 983
		i2c->isrlog[i2c->irqlogidx++] = isr;

	show_state(i2c);

	/*
	 * Always clear all pending IRQs.
	 */
984
	writel(isr, _ISR(i2c));
R
Russell King 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

	if (isr & ISR_SAD)
		i2c_pxa_slave_start(i2c, isr);
	if (isr & ISR_SSD)
		i2c_pxa_slave_stop(i2c);

	if (i2c_pxa_is_slavemode(i2c)) {
		if (isr & ISR_ITE)
			i2c_pxa_slave_txempty(i2c, isr);
		if (isr & ISR_IRF)
			i2c_pxa_slave_rxfull(i2c, isr);
	} else if (i2c->msg) {
		if (isr & ISR_ITE)
			i2c_pxa_irq_txempty(i2c, isr);
		if (isr & ISR_IRF)
			i2c_pxa_irq_rxfull(i2c, isr);
	} else {
		i2c_pxa_scream_blue_murder(i2c, "spurious irq");
	}

	return IRQ_HANDLED;
}


static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
{
	struct pxa_i2c *i2c = adap->algo_data;
	int ret, i;

	for (i = adap->retries; i >= 0; i--) {
		ret = i2c_pxa_do_xfer(i2c, msgs, num);
		if (ret != I2C_RETRY)
			goto out;

		if (i2c_debug)
1020
			dev_dbg(&adap->dev, "Retrying transmission\n");
R
Russell King 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029
		udelay(100);
	}
	i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
	ret = -EREMOTEIO;
 out:
	i2c_pxa_set_slave(i2c, ret);
	return ret;
}

1030 1031 1032 1033 1034
static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

1035
static const struct i2c_algorithm i2c_pxa_algorithm = {
R
Russell King 已提交
1036
	.master_xfer	= i2c_pxa_xfer,
1037
	.functionality	= i2c_pxa_functionality,
R
Russell King 已提交
1038 1039
};

M
Mike Rapoport 已提交
1040 1041 1042 1043 1044
static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
	.master_xfer	= i2c_pxa_pio_xfer,
	.functionality	= i2c_pxa_functionality,
};

1045
static int i2c_pxa_probe(struct platform_device *dev)
R
Russell King 已提交
1046
{
1047
	struct pxa_i2c *i2c;
1048
	struct resource *res;
1049
	struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
1050
	const struct platform_device_id *id = platform_get_device_id(dev);
1051
	enum pxa_i2c_types i2c_type = id->driver_data;
R
Russell King 已提交
1052
	int ret;
1053
	int irq;
R
Russell King 已提交
1054

1055 1056 1057 1058 1059
	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
	irq = platform_get_irq(dev, 0);
	if (res == NULL || irq < 0)
		return -ENODEV;

L
Linus Walleij 已提交
1060
	if (!request_mem_region(res->start, resource_size(res), res->name))
1061 1062
		return -ENOMEM;

1063
	i2c = kzalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
1064 1065 1066 1067 1068
	if (!i2c) {
		ret = -ENOMEM;
		goto emalloc;
	}

1069 1070 1071 1072
	i2c->adap.owner   = THIS_MODULE;
	i2c->adap.retries = 5;

	spin_lock_init(&i2c->lock);
1073
	init_waitqueue_head(&i2c->wait);
1074

W
Wolfram Sang 已提交
1075 1076 1077 1078 1079 1080 1081 1082
	/*
	 * If "dev->id" is negative we consider it as zero.
	 * The reason to do so is to avoid sysfs names that only make
	 * sense when there are multiple adapters.
	 */
	i2c->adap.nr = dev->id != -1 ? dev->id : 0;
	snprintf(i2c->adap.name, sizeof(i2c->adap.name), "pxa_i2c-i2c.%u",
		 i2c->adap.nr);
1083

1084
	i2c->clk = clk_get(&dev->dev, NULL);
1085 1086 1087 1088 1089
	if (IS_ERR(i2c->clk)) {
		ret = PTR_ERR(i2c->clk);
		goto eclk;
	}

L
Linus Walleij 已提交
1090
	i2c->reg_base = ioremap(res->start, resource_size(res));
1091 1092 1093 1094
	if (!i2c->reg_base) {
		ret = -EIO;
		goto eremap;
	}
1095 1096 1097 1098 1099

	i2c->reg_ibmr = i2c->reg_base + pxa_reg_layout[i2c_type].ibmr;
	i2c->reg_idbr = i2c->reg_base + pxa_reg_layout[i2c_type].idbr;
	i2c->reg_icr = i2c->reg_base + pxa_reg_layout[i2c_type].icr;
	i2c->reg_isr = i2c->reg_base + pxa_reg_layout[i2c_type].isr;
1100 1101
	if (i2c_type != REGS_CE4100)
		i2c->reg_isar = i2c->reg_base + pxa_reg_layout[i2c_type].isar;
1102 1103

	i2c->iobase = res->start;
L
Linus Walleij 已提交
1104
	i2c->iosize = resource_size(res);
1105 1106

	i2c->irq = irq;
R
Russell King 已提交
1107 1108 1109 1110 1111 1112

	i2c->slave_addr = I2C_PXA_SLAVE_ADDR;

#ifdef CONFIG_I2C_PXA_SLAVE
	if (plat) {
		i2c->slave_addr = plat->slave_addr;
1113
		i2c->slave = plat->slave;
R
Russell King 已提交
1114 1115 1116
	}
#endif

1117
	clk_enable(i2c->clk);
1118

M
Mike Rapoport 已提交
1119 1120 1121
	if (plat) {
		i2c->adap.class = plat->class;
		i2c->use_pio = plat->use_pio;
1122
		i2c->fast_mode = plat->fast_mode;
M
Mike Rapoport 已提交
1123 1124 1125 1126 1127 1128
	}

	if (i2c->use_pio) {
		i2c->adap.algo = &i2c_pxa_pio_algorithm;
	} else {
		i2c->adap.algo = &i2c_pxa_algorithm;
1129
		ret = request_irq(irq, i2c_pxa_handler, IRQF_SHARED,
M
Mike Rapoport 已提交
1130 1131 1132 1133
				  i2c->adap.name, i2c);
		if (ret)
			goto ereqirq;
	}
1134

R
Russell King 已提交
1135 1136 1137
	i2c_pxa_reset(i2c);

	i2c->adap.algo_data = i2c;
1138
	i2c->adap.dev.parent = &dev->dev;
R
Russell King 已提交
1139

1140 1141 1142 1143
	if (i2c_type == REGS_CE4100)
		ret = i2c_add_adapter(&i2c->adap);
	else
		ret = i2c_add_numbered_adapter(&i2c->adap);
R
Russell King 已提交
1144 1145
	if (ret < 0) {
		printk(KERN_INFO "I2C: Failed to add bus\n");
1146
		goto eadapt;
R
Russell King 已提交
1147 1148
	}

1149
	platform_set_drvdata(dev, i2c);
R
Russell King 已提交
1150 1151 1152

#ifdef CONFIG_I2C_PXA_SLAVE
	printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
1153
	       dev_name(&i2c->adap.dev), i2c->slave_addr);
R
Russell King 已提交
1154 1155
#else
	printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
1156
	       dev_name(&i2c->adap.dev));
R
Russell King 已提交
1157 1158 1159
#endif
	return 0;

1160
eadapt:
M
Mike Rapoport 已提交
1161 1162
	if (!i2c->use_pio)
		free_irq(irq, i2c);
1163
ereqirq:
1164
	clk_disable(i2c->clk);
W
Wolfram Sang 已提交
1165
	iounmap(i2c->reg_base);
1166
eremap:
1167 1168
	clk_put(i2c->clk);
eclk:
1169 1170
	kfree(i2c);
emalloc:
L
Linus Walleij 已提交
1171
	release_mem_region(res->start, resource_size(res));
R
Russell King 已提交
1172 1173 1174
	return ret;
}

W
Wolfram Sang 已提交
1175
static int __exit i2c_pxa_remove(struct platform_device *dev)
R
Russell King 已提交
1176
{
1177
	struct pxa_i2c *i2c = platform_get_drvdata(dev);
R
Russell King 已提交
1178

1179
	platform_set_drvdata(dev, NULL);
R
Russell King 已提交
1180 1181

	i2c_del_adapter(&i2c->adap);
M
Mike Rapoport 已提交
1182 1183
	if (!i2c->use_pio)
		free_irq(i2c->irq, i2c);
1184 1185 1186 1187

	clk_disable(i2c->clk);
	clk_put(i2c->clk);

W
Wolfram Sang 已提交
1188
	iounmap(i2c->reg_base);
1189 1190
	release_mem_region(i2c->iobase, i2c->iosize);
	kfree(i2c);
R
Russell King 已提交
1191 1192 1193 1194

	return 0;
}

1195
#ifdef CONFIG_PM
1196
static int i2c_pxa_suspend_noirq(struct device *dev)
1197
{
1198 1199 1200
	struct platform_device *pdev = to_platform_device(dev);
	struct pxa_i2c *i2c = platform_get_drvdata(pdev);

1201
	clk_disable(i2c->clk);
1202

1203 1204 1205
	return 0;
}

1206
static int i2c_pxa_resume_noirq(struct device *dev)
1207
{
1208 1209
	struct platform_device *pdev = to_platform_device(dev);
	struct pxa_i2c *i2c = platform_get_drvdata(pdev);
1210 1211 1212 1213 1214 1215

	clk_enable(i2c->clk);
	i2c_pxa_reset(i2c);

	return 0;
}
1216

1217
static const struct dev_pm_ops i2c_pxa_dev_pm_ops = {
1218 1219 1220 1221 1222
	.suspend_noirq = i2c_pxa_suspend_noirq,
	.resume_noirq = i2c_pxa_resume_noirq,
};

#define I2C_PXA_DEV_PM_OPS (&i2c_pxa_dev_pm_ops)
1223
#else
1224
#define I2C_PXA_DEV_PM_OPS NULL
1225 1226
#endif

1227
static struct platform_driver i2c_pxa_driver = {
R
Russell King 已提交
1228
	.probe		= i2c_pxa_probe,
W
Wolfram Sang 已提交
1229
	.remove		= __exit_p(i2c_pxa_remove),
1230 1231
	.driver		= {
		.name	= "pxa2xx-i2c",
W
Wolfram Sang 已提交
1232
		.owner	= THIS_MODULE,
1233
		.pm	= I2C_PXA_DEV_PM_OPS,
1234
	},
1235
	.id_table	= i2c_pxa_id_table,
R
Russell King 已提交
1236 1237 1238 1239
};

static int __init i2c_adap_pxa_init(void)
{
1240
	return platform_driver_register(&i2c_pxa_driver);
R
Russell King 已提交
1241 1242
}

W
Wolfram Sang 已提交
1243
static void __exit i2c_adap_pxa_exit(void)
R
Russell King 已提交
1244
{
1245
	platform_driver_unregister(&i2c_pxa_driver);
R
Russell King 已提交
1246 1247
}

1248
MODULE_LICENSE("GPL");
1249
MODULE_ALIAS("platform:pxa2xx-i2c");
1250

U
Uli Luckas 已提交
1251
subsys_initcall(i2c_adap_pxa_init);
R
Russell King 已提交
1252
module_exit(i2c_adap_pxa_exit);