i2c-rcar.c 17.0 KB
Newer Older
1
/*
2
 * Driver for the Renesas RCar I2C unit
3
 *
4 5 6
 * Copyright (C) 2014 Wolfram Sang <wsa@sang-engineering.com>
 *
 * Copyright (C) 2012-14 Renesas Solutions Corp.
7 8 9 10 11 12 13 14 15 16
 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
 *
 * This file is based on the drivers/i2c/busses/i2c-sh7760.c
 * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
 *
 * This file used out-of-tree driver i2c-rcar.c
 * Copyright (C) 2011-2012 Renesas Electronics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; version 2 of the License.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/i2c.h>
#include <linux/i2c/i2c-rcar.h>
#include <linux/kernel.h>
#include <linux/module.h>
33
#include <linux/of_device.h>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>

/* register offsets */
#define ICSCR	0x00	/* slave ctrl */
#define ICMCR	0x04	/* master ctrl */
#define ICSSR	0x08	/* slave status */
#define ICMSR	0x0C	/* master status */
#define ICSIER	0x10	/* slave irq enable */
#define ICMIER	0x14	/* master irq enable */
#define ICCCR	0x18	/* clock dividers */
#define ICSAR	0x1C	/* slave address */
#define ICMAR	0x20	/* master address */
#define ICRXTX	0x24	/* data port */

W
Wolfram Sang 已提交
50 51 52 53 54 55
/* ICSCR */
#define SDBS	(1 << 3)	/* slave data buffer select */
#define SIE	(1 << 2)	/* slave interface enable */
#define GCAE	(1 << 1)	/* general call address enable */
#define FNA	(1 << 0)	/* forced non acknowledgment */

56 57 58 59 60 61 62 63 64 65
/* ICMCR */
#define MDBS	(1 << 7)	/* non-fifo mode switch */
#define FSCL	(1 << 6)	/* override SCL pin */
#define FSDA	(1 << 5)	/* override SDA pin */
#define OBPC	(1 << 4)	/* override pins */
#define MIE	(1 << 3)	/* master if enable */
#define TSBE	(1 << 2)
#define FSB	(1 << 1)	/* force stop bit */
#define ESG	(1 << 0)	/* en startbit gen */

W
Wolfram Sang 已提交
66 67 68 69 70 71 72 73 74
/* ICSSR (also for ICSIER) */
#define GCAR	(1 << 6)	/* general call received */
#define STM	(1 << 5)	/* slave transmit mode */
#define SSR	(1 << 4)	/* stop received */
#define SDE	(1 << 3)	/* slave data empty */
#define SDT	(1 << 2)	/* slave data transmitted */
#define SDR	(1 << 1)	/* slave data received */
#define SAR	(1 << 0)	/* slave addr received */

75
/* ICMSR (also for ICMIE) */
76 77 78 79 80 81 82 83 84
#define MNR	(1 << 6)	/* nack received */
#define MAL	(1 << 5)	/* arbitration lost */
#define MST	(1 << 4)	/* sent a stop */
#define MDE	(1 << 3)
#define MDT	(1 << 2)
#define MDR	(1 << 1)
#define MAT	(1 << 0)	/* slave addr xfer done */


85 86 87
#define RCAR_BUS_PHASE_START	(MDBS | MIE | ESG)
#define RCAR_BUS_PHASE_DATA	(MDBS | MIE)
#define RCAR_BUS_PHASE_STOP	(MDBS | MIE | FSB)
88

89 90 91
#define RCAR_IRQ_SEND	(MNR | MAL | MST | MAT | MDE)
#define RCAR_IRQ_RECV	(MNR | MAL | MST | MAT | MDR)
#define RCAR_IRQ_STOP	(MST)
92

93 94
#define RCAR_IRQ_ACK_SEND	(~(MAT | MDE) & 0xFF)
#define RCAR_IRQ_ACK_RECV	(~(MAT | MDR) & 0xFF)
95

96 97 98 99 100
#define ID_LAST_MSG	(1 << 0)
#define ID_DONE		(1 << 2)
#define ID_ARBLOST	(1 << 3)
#define ID_NACK		(1 << 4)

101
enum rcar_i2c_type {
102 103
	I2C_RCAR_GEN1,
	I2C_RCAR_GEN2,
104 105
};

106 107 108
struct rcar_i2c_priv {
	void __iomem *io;
	struct i2c_adapter adap;
W
Wolfram Sang 已提交
109 110
	struct i2c_msg *msg;
	int msgs_left;
111
	struct clk *clk;
112 113 114 115 116 117

	wait_queue_head_t wait;

	int pos;
	u32 icccr;
	u32 flags;
118
	enum rcar_i2c_type devtype;
W
Wolfram Sang 已提交
119
	struct i2c_client *slave;
120 121 122 123 124 125 126 127 128 129
};

#define rcar_i2c_priv_to_dev(p)		((p)->adap.dev.parent)
#define rcar_i2c_is_recv(p)		((p)->msg->flags & I2C_M_RD)

#define rcar_i2c_flags_set(p, f)	((p)->flags |= (f))
#define rcar_i2c_flags_has(p, f)	((p)->flags & (f))

#define LOOP_TIMEOUT	1024

130

131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
{
	writel(val, priv->io + reg);
}

static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
{
	return readl(priv->io + reg);
}

static void rcar_i2c_init(struct rcar_i2c_priv *priv)
{
	/* reset master mode */
	rcar_i2c_write(priv, ICMIER, 0);
W
Wolfram Sang 已提交
145
	rcar_i2c_write(priv, ICMCR, MDBS);
146
	rcar_i2c_write(priv, ICMSR, 0);
W
Wolfram Sang 已提交
147 148
	/* start clock */
	rcar_i2c_write(priv, ICCCR, priv->icccr);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}

static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
{
	int i;

	for (i = 0; i < LOOP_TIMEOUT; i++) {
		/* make sure that bus is not busy */
		if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
			return 0;
		udelay(1);
	}

	return -EBUSY;
}

static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv,
				    u32 bus_speed,
				    struct device *dev)
{
	u32 scgd, cdf;
	u32 round, ick;
	u32 scl;
172
	u32 cdf_width;
173
	unsigned long rate;
174

175
	switch (priv->devtype) {
176
	case I2C_RCAR_GEN1:
177 178
		cdf_width = 2;
		break;
179
	case I2C_RCAR_GEN2:
180 181 182 183 184 185 186
		cdf_width = 3;
		break;
	default:
		dev_err(dev, "device type error\n");
		return -EIO;
	}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	/*
	 * calculate SCL clock
	 * see
	 *	ICCCR
	 *
	 * ick	= clkp / (1 + CDF)
	 * SCL	= ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
	 *
	 * ick  : I2C internal clock < 20 MHz
	 * ticf : I2C SCL falling time  =  35 ns here
	 * tr   : I2C SCL rising  time  = 200 ns here
	 * intd : LSI internal delay    =  50 ns here
	 * clkp : peripheral_clk
	 * F[]  : integer up-valuation
	 */
202
	rate = clk_get_rate(priv->clk);
203
	cdf = rate / 20000000;
204
	if (cdf >= 1U << cdf_width) {
205 206
		dev_err(dev, "Input clock %lu too high\n", rate);
		return -EIO;
207
	}
208
	ick = rate / (cdf + 1);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	/*
	 * it is impossible to calculate large scale
	 * number on u32. separate it
	 *
	 * F[(ticf + tr + intd) * ick]
	 *  = F[(35 + 200 + 50)ns * ick]
	 *  = F[285 * ick / 1000000000]
	 *  = F[(ick / 1000000) * 285 / 1000]
	 */
	round = (ick + 500000) / 1000000 * 285;
	round = (round + 500) / 1000;

	/*
	 * SCL	= ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
	 *
	 * Calculation result (= SCL) should be less than
	 * bus_speed for hardware safety
227 228 229 230 231 232
	 *
	 * We could use something along the lines of
	 *	div = ick / (bus_speed + 1) + 1;
	 *	scgd = (div - 20 - round + 7) / 8;
	 *	scl = ick / (20 + (scgd * 8) + round);
	 * (not fully verified) but that would get pretty involved
233 234 235 236 237 238 239 240 241 242 243
	 */
	for (scgd = 0; scgd < 0x40; scgd++) {
		scl = ick / (20 + (scgd * 8) + round);
		if (scl <= bus_speed)
			goto scgd_find;
	}
	dev_err(dev, "it is impossible to calculate best SCL\n");
	return -EIO;

scgd_find:
	dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
244
		scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
245 246 247 248

	/*
	 * keep icccr value
	 */
249
	priv->icccr = scgd << cdf_width | cdf;
250 251 252 253

	return 0;
}

254
static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
255
{
256
	int read = !!rcar_i2c_is_recv(priv);
257

W
Wolfram Sang 已提交
258 259 260 261 262
	priv->pos = 0;
	priv->flags = 0;
	if (priv->msgs_left == 1)
		rcar_i2c_flags_set(priv, ID_LAST_MSG);

263
	rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
264
	rcar_i2c_write(priv, ICMSR, 0);
265
	rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
266
	rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

/*
 *		interrupt functions
 */
static int rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
{
	struct i2c_msg *msg = priv->msg;

	/*
	 * FIXME
	 * sometimes, unknown interrupt happened.
	 * Do nothing
	 */
	if (!(msr & MDE))
		return 0;

	/*
	 * If address transfer phase finished,
	 * goto data phase.
	 */
	if (msr & MAT)
289
		rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	if (priv->pos < msg->len) {
		/*
		 * Prepare next data to ICRXTX register.
		 * This data will go to _SHIFT_ register.
		 *
		 *    *
		 * [ICRXTX] -> [SHIFT] -> [I2C bus]
		 */
		rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
		priv->pos++;

	} else {
		/*
		 * The last data was pushed to ICRXTX on _PREV_ empty irq.
		 * It is on _SHIFT_ register, and will sent to I2C bus.
		 *
		 *		  *
		 * [ICRXTX] -> [SHIFT] -> [I2C bus]
		 */

		if (priv->flags & ID_LAST_MSG)
			/*
			 * If current msg is the _LAST_ msg,
			 * prepare stop condition here.
			 * ID_DONE will be set on STOP irq.
			 */
317
			rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
318 319 320 321 322 323 324 325 326 327
		else
			/*
			 * If current msg is _NOT_ last msg,
			 * it doesn't call stop phase.
			 * thus, there is no STOP irq.
			 * return ID_DONE here.
			 */
			return ID_DONE;
	}

328
	rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

	return 0;
}

static int rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
{
	struct i2c_msg *msg = priv->msg;

	/*
	 * FIXME
	 * sometimes, unknown interrupt happened.
	 * Do nothing
	 */
	if (!(msr & MDR))
		return 0;

	if (msr & MAT) {
		/*
		 * Address transfer phase finished,
		 * but, there is no data at this point.
		 * Do nothing.
		 */
	} else if (priv->pos < msg->len) {
		/*
		 * get received data
		 */
		msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
		priv->pos++;
	}

	/*
	 * If next received data is the _LAST_,
	 * go to STOP phase,
	 * otherwise, go to DATA phase.
	 */
	if (priv->pos + 1 >= msg->len)
365
		rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
366
	else
367
		rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
368

369
	rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
370 371 372 373

	return 0;
}

W
Wolfram Sang 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
{
	u32 ssr_raw, ssr_filtered;
	u8 value;

	ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
	ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);

	if (!ssr_filtered)
		return false;

	/* address detected */
	if (ssr_filtered & SAR) {
		/* read or write request */
		if (ssr_raw & STM) {
W
Wolfram Sang 已提交
389
			i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
W
Wolfram Sang 已提交
390 391 392
			rcar_i2c_write(priv, ICRXTX, value);
			rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
		} else {
W
Wolfram Sang 已提交
393
			i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
W
Wolfram Sang 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
			rcar_i2c_read(priv, ICRXTX);	/* dummy read */
			rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
		}

		rcar_i2c_write(priv, ICSSR, ~SAR & 0xff);
	}

	/* master sent stop */
	if (ssr_filtered & SSR) {
		i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
		rcar_i2c_write(priv, ICSIER, SAR | SSR);
		rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
	}

	/* master wants to write to us */
	if (ssr_filtered & SDR) {
		int ret;

		value = rcar_i2c_read(priv, ICRXTX);
W
Wolfram Sang 已提交
413
		ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
W
Wolfram Sang 已提交
414 415 416 417 418 419 420
		/* Send NACK in case of error */
		rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
		rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
	}

	/* master wants to read from us */
	if (ssr_filtered & SDE) {
W
Wolfram Sang 已提交
421
		i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
W
Wolfram Sang 已提交
422 423 424 425 426 427 428
		rcar_i2c_write(priv, ICRXTX, value);
		rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
	}

	return true;
}

429 430 431
static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
{
	struct rcar_i2c_priv *priv = ptr;
432
	irqreturn_t result = IRQ_HANDLED;
433 434
	u32 msr;

W
Wolfram Sang 已提交
435 436 437
	if (rcar_i2c_slave_irq(priv))
		goto exit;

438
	msr = rcar_i2c_read(priv, ICMSR);
439

440 441
	/* Only handle interrupts that are currently enabled */
	msr &= rcar_i2c_read(priv, ICMIER);
442 443 444 445
	if (!msr) {
		result = IRQ_NONE;
		goto exit;
	}
446

447
	/* Arbitration lost */
448 449 450 451 452
	if (msr & MAL) {
		rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
		goto out;
	}

453
	/* Nack */
454 455
	if (msr & MNR) {
		/* go to stop phase */
456
		rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
457
		rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
458 459 460 461
		rcar_i2c_flags_set(priv, ID_NACK);
		goto out;
	}

462 463 464 465 466 467
	/* Stop */
	if (msr & MST) {
		rcar_i2c_flags_set(priv, ID_DONE);
		goto out;
	}

468 469 470 471 472 473 474
	if (rcar_i2c_is_recv(priv))
		rcar_i2c_flags_set(priv, rcar_i2c_irq_recv(priv, msr));
	else
		rcar_i2c_flags_set(priv, rcar_i2c_irq_send(priv, msr));

out:
	if (rcar_i2c_flags_has(priv, ID_DONE)) {
475
		rcar_i2c_write(priv, ICMIER, 0);
476
		rcar_i2c_write(priv, ICMSR, 0);
477 478 479
		wake_up(&priv->wait);
	}

480 481
exit:
	return result;
482 483 484 485 486 487 488 489
}

static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
				struct i2c_msg *msgs,
				int num)
{
	struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
	struct device *dev = rcar_i2c_priv_to_dev(priv);
490
	int i, ret;
W
Wolfram Sang 已提交
491
	long time_left;
492 493 494

	pm_runtime_get_sync(dev);

495 496 497 498
	ret = rcar_i2c_bus_barrier(priv);
	if (ret < 0)
		goto out;

499
	for (i = 0; i < num; i++) {
500 501 502 503 504 505
		/* This HW can't send STOP after address phase */
		if (msgs[i].len == 0) {
			ret = -EOPNOTSUPP;
			break;
		}

506
		/* init each data */
W
Wolfram Sang 已提交
507 508
		priv->msg = &msgs[i];
		priv->msgs_left = num - i;
509

510
		rcar_i2c_prepare_msg(priv);
511

W
Wolfram Sang 已提交
512
		time_left = wait_event_timeout(priv->wait,
513
					     rcar_i2c_flags_has(priv, ID_DONE),
514
					     adap->timeout);
W
Wolfram Sang 已提交
515 516
		if (!time_left) {
			rcar_i2c_init(priv);
517 518 519 520 521
			ret = -ETIMEDOUT;
			break;
		}

		if (rcar_i2c_flags_has(priv, ID_NACK)) {
B
Ben Dooks 已提交
522
			ret = -ENXIO;
523 524 525 526 527 528 529 530 531 532
			break;
		}

		if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
			ret = -EAGAIN;
			break;
		}

		ret = i + 1; /* The number of transfer */
	}
533
out:
534 535
	pm_runtime_put(dev);

B
Ben Dooks 已提交
536
	if (ret < 0 && ret != -ENXIO)
537 538 539 540 541
		dev_err(dev, "error %d : %x\n", ret, priv->flags);

	return ret;
}

W
Wolfram Sang 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static int rcar_reg_slave(struct i2c_client *slave)
{
	struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);

	if (priv->slave)
		return -EBUSY;

	if (slave->flags & I2C_CLIENT_TEN)
		return -EAFNOSUPPORT;

	pm_runtime_forbid(rcar_i2c_priv_to_dev(priv));

	priv->slave = slave;
	rcar_i2c_write(priv, ICSAR, slave->addr);
	rcar_i2c_write(priv, ICSSR, 0);
	rcar_i2c_write(priv, ICSIER, SAR | SSR);
	rcar_i2c_write(priv, ICSCR, SIE | SDBS);

	return 0;
}

static int rcar_unreg_slave(struct i2c_client *slave)
{
	struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);

	WARN_ON(!priv->slave);

	rcar_i2c_write(priv, ICSIER, 0);
	rcar_i2c_write(priv, ICSCR, 0);

	priv->slave = NULL;

	pm_runtime_allow(rcar_i2c_priv_to_dev(priv));

	return 0;
}

579 580
static u32 rcar_i2c_func(struct i2c_adapter *adap)
{
581
	/* This HW can't do SMBUS_QUICK and NOSTART */
582 583
	return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
		(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
584 585 586 587 588
}

static const struct i2c_algorithm rcar_i2c_algo = {
	.master_xfer	= rcar_i2c_master_xfer,
	.functionality	= rcar_i2c_func,
W
Wolfram Sang 已提交
589 590
	.reg_slave	= rcar_reg_slave,
	.unreg_slave	= rcar_unreg_slave,
591 592
};

593
static const struct of_device_id rcar_i2c_dt_ids[] = {
594 595 596 597
	{ .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 },
	{ .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
	{ .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
	{ .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
598
	{ .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
599 600 601
	{ .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
	{ .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
	{ .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
602 603 604 605
	{},
};
MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);

606
static int rcar_i2c_probe(struct platform_device *pdev)
607
{
J
Jingoo Han 已提交
608
	struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
609 610 611 612 613
	struct rcar_i2c_priv *priv;
	struct i2c_adapter *adap;
	struct resource *res;
	struct device *dev = &pdev->dev;
	u32 bus_speed;
614
	int irq, ret;
615 616

	priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
617
	if (!priv)
618 619
		return -ENOMEM;

620 621 622 623 624 625
	priv->clk = devm_clk_get(dev, NULL);
	if (IS_ERR(priv->clk)) {
		dev_err(dev, "cannot get clock\n");
		return PTR_ERR(priv->clk);
	}

626
	bus_speed = 100000; /* default 100 kHz */
627 628
	ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
	if (ret < 0 && pdata && pdata->bus_speed)
629
		bus_speed = pdata->bus_speed;
630

631 632 633 634 635
	if (pdev->dev.of_node)
		priv->devtype = (long)of_match_device(rcar_i2c_dt_ids,
						      dev)->data;
	else
		priv->devtype = platform_get_device_id(pdev)->driver_data;
636

637 638 639 640
	ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
	if (ret < 0)
		return ret;

641
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
642 643 644
	priv->io = devm_ioremap_resource(dev, res);
	if (IS_ERR(priv->io))
		return PTR_ERR(priv->io);
645

W
Wolfram Sang 已提交
646 647
	rcar_i2c_init(priv);

648
	irq = platform_get_irq(pdev, 0);
649 650
	init_waitqueue_head(&priv->wait);

651 652 653 654 655 656 657
	adap = &priv->adap;
	adap->nr = pdev->id;
	adap->algo = &rcar_i2c_algo;
	adap->class = I2C_CLASS_DEPRECATED;
	adap->retries = 3;
	adap->dev.parent = dev;
	adap->dev.of_node = dev->of_node;
658 659 660
	i2c_set_adapdata(adap, priv);
	strlcpy(adap->name, pdev->name, sizeof(adap->name));

661
	ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
662 663
			       dev_name(dev), priv);
	if (ret < 0) {
664
		dev_err(dev, "cannot get irq %d\n", irq);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
		return ret;
	}

	ret = i2c_add_numbered_adapter(adap);
	if (ret < 0) {
		dev_err(dev, "reg adap failed: %d\n", ret);
		return ret;
	}

	pm_runtime_enable(dev);
	platform_set_drvdata(pdev, priv);

	dev_info(dev, "probed\n");

	return 0;
}

682
static int rcar_i2c_remove(struct platform_device *pdev)
683 684 685 686 687 688 689 690 691 692
{
	struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;

	i2c_del_adapter(&priv->adap);
	pm_runtime_disable(dev);

	return 0;
}

693
static const struct platform_device_id rcar_i2c_id_table[] = {
694 695 696
	{ "i2c-rcar",		I2C_RCAR_GEN1 },
	{ "i2c-rcar_gen1",	I2C_RCAR_GEN1 },
	{ "i2c-rcar_gen2",	I2C_RCAR_GEN2 },
697 698 699 700
	{},
};
MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);

W
Wolfram Sang 已提交
701
static struct platform_driver rcar_i2c_driver = {
702 703
	.driver	= {
		.name	= "i2c-rcar",
704
		.of_match_table = rcar_i2c_dt_ids,
705 706
	},
	.probe		= rcar_i2c_probe,
707
	.remove		= rcar_i2c_remove,
708
	.id_table	= rcar_i2c_id_table,
709 710
};

W
Wolfram Sang 已提交
711
module_platform_driver(rcar_i2c_driver);
712

713
MODULE_LICENSE("GPL v2");
714 715
MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");