2012-09-28 14:44:25 +08:00
|
|
|
/*
|
|
|
|
* drivers/i2c/busses/i2c-rcar.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Renesas Solutions Corp.
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#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>
|
2013-09-12 20:36:46 +08:00
|
|
|
#include <linux/of_device.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
/* ICMSR */
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
/* ICMIE */
|
|
|
|
#define MNRE (1 << 6) /* nack irq en */
|
|
|
|
#define MALE (1 << 5) /* arblos irq en */
|
|
|
|
#define MSTE (1 << 4) /* stop irq en */
|
|
|
|
#define MDEE (1 << 3)
|
|
|
|
#define MDTE (1 << 2)
|
|
|
|
#define MDRE (1 << 1)
|
|
|
|
#define MATE (1 << 0) /* address sent irq en */
|
|
|
|
|
|
|
|
|
2014-05-28 15:44:38 +08:00
|
|
|
#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
|
|
|
|
#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
|
|
|
|
#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:39 +08:00
|
|
|
#define RCAR_IRQ_SEND (MNRE | MALE | MSTE | MATE | MDEE)
|
|
|
|
#define RCAR_IRQ_RECV (MNRE | MALE | MSTE | MATE | MDRE)
|
|
|
|
#define RCAR_IRQ_STOP (MSTE)
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:42 +08:00
|
|
|
#define RCAR_IRQ_ACK_SEND (~(MAT | MDE))
|
|
|
|
#define RCAR_IRQ_ACK_RECV (~(MAT | MDR))
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
/*
|
|
|
|
* flags
|
|
|
|
*/
|
|
|
|
#define ID_LAST_MSG (1 << 0)
|
|
|
|
#define ID_IOERROR (1 << 1)
|
|
|
|
#define ID_DONE (1 << 2)
|
|
|
|
#define ID_ARBLOST (1 << 3)
|
|
|
|
#define ID_NACK (1 << 4)
|
|
|
|
|
2013-09-03 08:09:25 +08:00
|
|
|
enum rcar_i2c_type {
|
2013-10-21 16:04:32 +08:00
|
|
|
I2C_RCAR_GEN1,
|
|
|
|
I2C_RCAR_GEN2,
|
2013-09-03 08:09:25 +08:00
|
|
|
};
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
struct rcar_i2c_priv {
|
|
|
|
void __iomem *io;
|
|
|
|
struct i2c_adapter adap;
|
|
|
|
struct i2c_msg *msg;
|
2014-01-27 00:05:35 +08:00
|
|
|
struct clk *clk;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
|
|
|
|
int pos;
|
|
|
|
u32 icccr;
|
|
|
|
u32 flags;
|
2013-09-03 08:09:25 +08:00
|
|
|
enum rcar_i2c_type devtype;
|
2012-09-28 14:44:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
/*
|
|
|
|
* basic functions
|
|
|
|
*/
|
|
|
|
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 slave mode.
|
|
|
|
* slave mode is not used on this driver
|
|
|
|
*/
|
|
|
|
rcar_i2c_write(priv, ICSIER, 0);
|
|
|
|
rcar_i2c_write(priv, ICSAR, 0);
|
|
|
|
rcar_i2c_write(priv, ICSCR, 0);
|
|
|
|
rcar_i2c_write(priv, ICSSR, 0);
|
|
|
|
|
|
|
|
/* reset master mode */
|
|
|
|
rcar_i2c_write(priv, ICMIER, 0);
|
|
|
|
rcar_i2c_write(priv, ICMCR, 0);
|
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
|
|
|
rcar_i2c_write(priv, ICMAR, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bus control functions
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clock function
|
|
|
|
*/
|
|
|
|
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;
|
2013-09-03 08:09:25 +08:00
|
|
|
u32 cdf_width;
|
2013-09-12 20:36:45 +08:00
|
|
|
unsigned long rate;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2013-09-03 08:09:25 +08:00
|
|
|
switch (priv->devtype) {
|
2013-10-21 16:04:32 +08:00
|
|
|
case I2C_RCAR_GEN1:
|
2013-09-03 08:09:25 +08:00
|
|
|
cdf_width = 2;
|
|
|
|
break;
|
2013-10-21 16:04:32 +08:00
|
|
|
case I2C_RCAR_GEN2:
|
2013-09-03 08:09:25 +08:00
|
|
|
cdf_width = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "device type error\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2014-01-27 00:05:35 +08:00
|
|
|
rate = clk_get_rate(priv->clk);
|
2013-09-12 20:36:45 +08:00
|
|
|
cdf = rate / 20000000;
|
|
|
|
if (cdf >= 1 << cdf_width) {
|
|
|
|
dev_err(dev, "Input clock %lu too high\n", rate);
|
|
|
|
return -EIO;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
2013-09-12 20:36:45 +08:00
|
|
|
ick = rate / (cdf + 1);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2013-09-12 20:36:45 +08:00
|
|
|
*
|
|
|
|
* 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
|
2012-09-28 14:44:25 +08:00
|
|
|
*/
|
|
|
|
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",
|
2014-01-27 00:05:35 +08:00
|
|
|
scl, bus_speed, clk_get_rate(priv->clk), round, cdf, scgd);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* keep icccr value
|
|
|
|
*/
|
2013-09-12 20:36:44 +08:00
|
|
|
priv->icccr = scgd << cdf_width | cdf;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* status functions
|
|
|
|
*/
|
|
|
|
|
2014-05-28 15:44:41 +08:00
|
|
|
static int rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
2014-05-28 15:44:41 +08:00
|
|
|
int read = !!rcar_i2c_is_recv(priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:41 +08:00
|
|
|
rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
|
2014-05-28 15:44:42 +08:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
|
2014-05-28 15:44:41 +08:00
|
|
|
rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
2012-09-28 14:44:25 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:44:42 +08:00
|
|
|
rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
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)
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
2012-09-28 14:44:25 +08:00
|
|
|
else
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:42 +08:00
|
|
|
rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = ptr;
|
|
|
|
struct device *dev = rcar_i2c_priv_to_dev(priv);
|
|
|
|
u32 msr;
|
|
|
|
|
2014-05-28 15:44:36 +08:00
|
|
|
msr = rcar_i2c_read(priv, ICMSR);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Arbitration lost
|
|
|
|
*/
|
|
|
|
if (msr & MAL) {
|
|
|
|
/*
|
|
|
|
* CAUTION
|
|
|
|
*
|
|
|
|
* When arbitration lost, device become _slave_ mode.
|
|
|
|
*/
|
|
|
|
dev_dbg(dev, "Arbitration Lost\n");
|
|
|
|
rcar_i2c_flags_set(priv, (ID_DONE | ID_ARBLOST));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop
|
|
|
|
*/
|
|
|
|
if (msr & MST) {
|
|
|
|
dev_dbg(dev, "Stop\n");
|
|
|
|
rcar_i2c_flags_set(priv, ID_DONE);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nack
|
|
|
|
*/
|
|
|
|
if (msr & MNR) {
|
|
|
|
dev_dbg(dev, "Nack\n");
|
|
|
|
|
|
|
|
/* go to stop phase */
|
2014-05-28 15:44:38 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
2014-05-28 15:44:39 +08:00
|
|
|
rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
|
2012-09-28 14:44:25 +08:00
|
|
|
rcar_i2c_flags_set(priv, ID_NACK);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* recv/send
|
|
|
|
*/
|
|
|
|
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)) {
|
2014-05-28 15:44:39 +08:00
|
|
|
rcar_i2c_write(priv, ICMIER, 0);
|
2014-05-28 15:44:42 +08:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
2012-09-28 14:44:25 +08:00
|
|
|
wake_up(&priv->wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
int i, ret, timeout;
|
|
|
|
|
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
|
|
|
|
rcar_i2c_init(priv);
|
2014-05-28 15:44:36 +08:00
|
|
|
/* start clock */
|
|
|
|
rcar_i2c_write(priv, ICCCR, priv->icccr);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:40 +08:00
|
|
|
ret = rcar_i2c_bus_barrier(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
for (i = 0; i < num; i++) {
|
2014-05-06 00:36:21 +08:00
|
|
|
/* This HW can't send STOP after address phase */
|
|
|
|
if (msgs[i].len == 0) {
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
/* init each data */
|
|
|
|
priv->msg = &msgs[i];
|
|
|
|
priv->pos = 0;
|
|
|
|
priv->flags = 0;
|
|
|
|
if (priv->msg == &msgs[num - 1])
|
|
|
|
rcar_i2c_flags_set(priv, ID_LAST_MSG);
|
|
|
|
|
2014-05-28 15:44:41 +08:00
|
|
|
ret = rcar_i2c_prepare_msg(priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wait result
|
|
|
|
*/
|
|
|
|
timeout = wait_event_timeout(priv->wait,
|
|
|
|
rcar_i2c_flags_has(priv, ID_DONE),
|
|
|
|
5 * HZ);
|
|
|
|
if (!timeout) {
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* error handling
|
|
|
|
*/
|
|
|
|
if (rcar_i2c_flags_has(priv, ID_NACK)) {
|
2014-01-27 00:05:37 +08:00
|
|
|
ret = -ENXIO;
|
2012-09-28 14:44:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rcar_i2c_flags_has(priv, ID_ARBLOST)) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rcar_i2c_flags_has(priv, ID_IOERROR)) {
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i + 1; /* The number of transfer */
|
|
|
|
}
|
2014-05-28 15:44:40 +08:00
|
|
|
out:
|
2012-09-28 14:44:25 +08:00
|
|
|
pm_runtime_put(dev);
|
|
|
|
|
2014-01-27 00:05:37 +08:00
|
|
|
if (ret < 0 && ret != -ENXIO)
|
2012-09-28 14:44:25 +08:00
|
|
|
dev_err(dev, "error %d : %x\n", ret, priv->flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 rcar_i2c_func(struct i2c_adapter *adap)
|
|
|
|
{
|
2014-05-06 00:36:21 +08:00
|
|
|
/* This HW can't do SMBUS_QUICK and NOSTART */
|
|
|
|
return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_algorithm rcar_i2c_algo = {
|
|
|
|
.master_xfer = rcar_i2c_master_xfer,
|
|
|
|
.functionality = rcar_i2c_func,
|
|
|
|
};
|
|
|
|
|
2013-09-12 20:36:46 +08:00
|
|
|
static const struct of_device_id rcar_i2c_dt_ids[] = {
|
2013-10-21 16:04:32 +08:00
|
|
|
{ .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 },
|
2014-02-20 16:03:20 +08:00
|
|
|
{ .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
|
2014-05-27 20:06:28 +08:00
|
|
|
{ .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 },
|
2013-09-12 20:36:46 +08:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
|
|
|
|
|
2012-11-28 04:59:38 +08:00
|
|
|
static int rcar_i2c_probe(struct platform_device *pdev)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
2013-07-30 15:59:33 +08:00
|
|
|
struct i2c_rcar_platform_data *pdata = dev_get_platdata(&pdev->dev);
|
2012-09-28 14:44:25 +08:00
|
|
|
struct rcar_i2c_priv *priv;
|
|
|
|
struct i2c_adapter *adap;
|
|
|
|
struct resource *res;
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
u32 bus_speed;
|
2014-05-28 15:44:37 +08:00
|
|
|
int irq, ret;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
|
|
|
|
if (!priv) {
|
|
|
|
dev_err(dev, "no mem for private data\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2014-01-27 00:05:35 +08:00
|
|
|
priv->clk = devm_clk_get(dev, NULL);
|
|
|
|
if (IS_ERR(priv->clk)) {
|
|
|
|
dev_err(dev, "cannot get clock\n");
|
|
|
|
return PTR_ERR(priv->clk);
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
bus_speed = 100000; /* default 100 kHz */
|
2013-09-12 20:36:46 +08:00
|
|
|
ret = of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed);
|
|
|
|
if (ret < 0 && pdata && pdata->bus_speed)
|
2012-09-28 14:44:25 +08:00
|
|
|
bus_speed = pdata->bus_speed;
|
2013-09-03 08:09:25 +08:00
|
|
|
|
2013-09-12 20:36:46 +08:00
|
|
|
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;
|
2013-09-03 08:09:25 +08:00
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
ret = rcar_i2c_clock_calculate(priv, bus_speed, dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-05-10 16:16:54 +08:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-01-21 18:09:03 +08:00
|
|
|
priv->io = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(priv->io))
|
|
|
|
return PTR_ERR(priv->io);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-05-28 15:44:37 +08:00
|
|
|
irq = platform_get_irq(pdev, 0);
|
2012-09-28 14:44:25 +08:00
|
|
|
init_waitqueue_head(&priv->wait);
|
|
|
|
|
|
|
|
adap = &priv->adap;
|
|
|
|
adap->nr = pdev->id;
|
|
|
|
adap->algo = &rcar_i2c_algo;
|
2014-02-10 18:04:06 +08:00
|
|
|
adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD | I2C_CLASS_DEPRECATED;
|
2012-09-28 14:44:25 +08:00
|
|
|
adap->retries = 3;
|
|
|
|
adap->dev.parent = dev;
|
2013-09-12 20:36:46 +08:00
|
|
|
adap->dev.of_node = dev->of_node;
|
2012-09-28 14:44:25 +08:00
|
|
|
i2c_set_adapdata(adap, priv);
|
|
|
|
strlcpy(adap->name, pdev->name, sizeof(adap->name));
|
|
|
|
|
2014-05-28 15:44:37 +08:00
|
|
|
ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0,
|
2012-09-28 14:44:25 +08:00
|
|
|
dev_name(dev), priv);
|
|
|
|
if (ret < 0) {
|
2014-05-28 15:44:37 +08:00
|
|
|
dev_err(dev, "cannot get irq %d\n", irq);
|
2012-09-28 14:44:25 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-28 04:59:38 +08:00
|
|
|
static int rcar_i2c_remove(struct platform_device *pdev)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-03 08:09:25 +08:00
|
|
|
static struct platform_device_id rcar_i2c_id_table[] = {
|
2013-10-21 16:04:32 +08:00
|
|
|
{ "i2c-rcar", I2C_RCAR_GEN1 },
|
|
|
|
{ "i2c-rcar_gen1", I2C_RCAR_GEN1 },
|
|
|
|
{ "i2c-rcar_gen2", I2C_RCAR_GEN2 },
|
2013-09-03 08:09:25 +08:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(platform, rcar_i2c_id_table);
|
|
|
|
|
2012-11-13 18:24:15 +08:00
|
|
|
static struct platform_driver rcar_i2c_driver = {
|
2012-09-28 14:44:25 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "i2c-rcar",
|
|
|
|
.owner = THIS_MODULE,
|
2013-09-12 20:36:46 +08:00
|
|
|
.of_match_table = rcar_i2c_dt_ids,
|
2012-09-28 14:44:25 +08:00
|
|
|
},
|
|
|
|
.probe = rcar_i2c_probe,
|
2012-11-28 04:59:38 +08:00
|
|
|
.remove = rcar_i2c_remove,
|
2013-09-03 08:09:25 +08:00
|
|
|
.id_table = rcar_i2c_id_table,
|
2012-09-28 14:44:25 +08:00
|
|
|
};
|
|
|
|
|
2012-11-13 18:24:15 +08:00
|
|
|
module_platform_driver(rcar_i2c_driver);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
|
|
|
|
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
|