2012-09-28 06:44:25 +00:00
|
|
|
/*
|
2014-05-28 07:44:46 +00:00
|
|
|
* Driver for the Renesas RCar I2C unit
|
2012-09-28 06:44:25 +00:00
|
|
|
*
|
2015-11-19 15:56:50 +00:00
|
|
|
* Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
|
|
|
|
* Copyright (C) 2011-2015 Renesas Electronics Corporation
|
2014-05-28 07:44:46 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012-14 Renesas Solutions Corp.
|
2012-09-28 06:44:25 +00:00
|
|
|
* 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 program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2014-05-28 07:44:46 +00:00
|
|
|
* the Free Software Foundation; version 2 of the License.
|
2012-09-28 06:44:25 +00:00
|
|
|
*
|
|
|
|
* 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/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2013-09-12 12:36:46 +00:00
|
|
|
#include <linux/of_device.h>
|
2012-09-28 06:44:25 +00: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 */
|
|
|
|
|
2014-11-18 16:04:55 +00:00
|
|
|
/* 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 */
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
/* 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 */
|
|
|
|
|
2014-11-18 16:04:55 +00:00
|
|
|
/* 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 */
|
|
|
|
|
2014-05-28 07:44:44 +00:00
|
|
|
/* ICMSR (also for ICMIE) */
|
2012-09-28 06:44:25 +00:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
|
2014-05-28 07:44:38 +00:00
|
|
|
#define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
|
|
|
|
#define RCAR_BUS_PHASE_DATA (MDBS | MIE)
|
2015-11-19 15:56:49 +00:00
|
|
|
#define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
|
2014-05-28 07:44:38 +00:00
|
|
|
#define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2014-05-28 07:44:44 +00:00
|
|
|
#define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
|
|
|
|
#define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
|
|
|
|
#define RCAR_IRQ_STOP (MST)
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2014-09-05 23:34:32 +00:00
|
|
|
#define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0xFF)
|
|
|
|
#define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0xFF)
|
2014-05-28 07:44:42 +00:00
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
#define ID_LAST_MSG (1 << 0)
|
2015-11-19 15:56:51 +00:00
|
|
|
#define ID_FIRST_MSG (1 << 1)
|
2012-09-28 06:44:25 +00:00
|
|
|
#define ID_DONE (1 << 2)
|
|
|
|
#define ID_ARBLOST (1 << 3)
|
|
|
|
#define ID_NACK (1 << 4)
|
2015-12-23 16:56:34 +00:00
|
|
|
/* persistent flags */
|
|
|
|
#define ID_P_PM_BLOCKED (1 << 31)
|
|
|
|
#define ID_P_MASK ID_P_PM_BLOCKED
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2013-09-03 00:09:25 +00:00
|
|
|
enum rcar_i2c_type {
|
2013-10-21 08:04:32 +00:00
|
|
|
I2C_RCAR_GEN1,
|
|
|
|
I2C_RCAR_GEN2,
|
2015-08-05 13:18:25 +00:00
|
|
|
I2C_RCAR_GEN3,
|
2013-09-03 00:09:25 +00:00
|
|
|
};
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
struct rcar_i2c_priv {
|
|
|
|
void __iomem *io;
|
|
|
|
struct i2c_adapter adap;
|
2015-11-19 15:56:45 +00:00
|
|
|
struct i2c_msg *msg;
|
|
|
|
int msgs_left;
|
2014-01-26 16:05:35 +00:00
|
|
|
struct clk *clk;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
wait_queue_head_t wait;
|
|
|
|
|
|
|
|
int pos;
|
|
|
|
u32 icccr;
|
|
|
|
u32 flags;
|
2014-05-28 07:44:45 +00:00
|
|
|
enum rcar_i2c_type devtype;
|
2014-11-18 16:04:55 +00:00
|
|
|
struct i2c_client *slave;
|
2012-09-28 06:44:25 +00: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 LOOP_TIMEOUT 1024
|
|
|
|
|
2014-05-28 07:44:45 +00:00
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
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);
|
2015-11-19 15:56:42 +00:00
|
|
|
rcar_i2c_write(priv, ICMCR, MDBS);
|
2012-09-28 06:44:25 +00:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
2015-11-19 15:56:42 +00:00
|
|
|
/* start clock */
|
|
|
|
rcar_i2c_write(priv, ICCCR, priv->icccr);
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-08 09:37:48 +00:00
|
|
|
static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
2015-12-08 09:37:49 +00:00
|
|
|
u32 scgd, cdf, round, ick, sum, scl, cdf_width;
|
2013-09-12 12:36:45 +00:00
|
|
|
unsigned long rate;
|
2015-12-08 09:37:47 +00:00
|
|
|
struct device *dev = rcar_i2c_priv_to_dev(priv);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2015-12-08 09:37:48 +00:00
|
|
|
/* Fall back to previously used values if not supplied */
|
|
|
|
t->bus_freq_hz = t->bus_freq_hz ?: 100000;
|
2015-12-08 09:37:49 +00:00
|
|
|
t->scl_fall_ns = t->scl_fall_ns ?: 35;
|
|
|
|
t->scl_rise_ns = t->scl_rise_ns ?: 200;
|
|
|
|
t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2013-09-03 00:09:25 +00:00
|
|
|
switch (priv->devtype) {
|
2013-10-21 08:04:32 +00:00
|
|
|
case I2C_RCAR_GEN1:
|
2013-09-03 00:09:25 +00:00
|
|
|
cdf_width = 2;
|
|
|
|
break;
|
2013-10-21 08:04:32 +00:00
|
|
|
case I2C_RCAR_GEN2:
|
2015-08-05 13:18:25 +00:00
|
|
|
case I2C_RCAR_GEN3:
|
2013-09-03 00:09:25 +00:00
|
|
|
cdf_width = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev_err(dev, "device type error\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:44:25 +00: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
|
2015-12-08 09:37:49 +00:00
|
|
|
* ticf : I2C SCL falling time
|
|
|
|
* tr : I2C SCL rising time
|
|
|
|
* intd : LSI internal delay
|
2012-09-28 06:44:25 +00:00
|
|
|
* clkp : peripheral_clk
|
|
|
|
* F[] : integer up-valuation
|
|
|
|
*/
|
2014-01-26 16:05:35 +00:00
|
|
|
rate = clk_get_rate(priv->clk);
|
2013-09-12 12:36:45 +00:00
|
|
|
cdf = rate / 20000000;
|
2014-09-20 10:07:37 +00:00
|
|
|
if (cdf >= 1U << cdf_width) {
|
2013-09-12 12:36:45 +00:00
|
|
|
dev_err(dev, "Input clock %lu too high\n", rate);
|
|
|
|
return -EIO;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
2013-09-12 12:36:45 +00:00
|
|
|
ick = rate / (cdf + 1);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* it is impossible to calculate large scale
|
|
|
|
* number on u32. separate it
|
|
|
|
*
|
2015-12-08 09:37:49 +00:00
|
|
|
* F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
|
|
|
|
* = F[sum * ick / 1000000000]
|
|
|
|
* = F[(ick / 1000000) * sum / 1000]
|
2012-09-28 06:44:25 +00:00
|
|
|
*/
|
2015-12-08 09:37:49 +00:00
|
|
|
sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns;
|
|
|
|
round = (ick + 500000) / 1000000 * sum;
|
2012-09-28 06:44:25 +00:00
|
|
|
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 12:36:45 +00: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 06:44:25 +00:00
|
|
|
*/
|
|
|
|
for (scgd = 0; scgd < 0x40; scgd++) {
|
|
|
|
scl = ick / (20 + (scgd * 8) + round);
|
2015-12-08 09:37:48 +00:00
|
|
|
if (scl <= t->bus_freq_hz)
|
2012-09-28 06:44:25 +00:00
|
|
|
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",
|
2015-12-08 09:37:48 +00:00
|
|
|
scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2015-11-19 15:56:50 +00:00
|
|
|
/* keep icccr value */
|
2013-09-12 12:36:44 +00:00
|
|
|
priv->icccr = scgd << cdf_width | cdf;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-14 20:15:46 +00:00
|
|
|
static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
2014-05-28 07:44:41 +00:00
|
|
|
int read = !!rcar_i2c_is_recv(priv);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2015-11-19 15:56:45 +00:00
|
|
|
priv->pos = 0;
|
|
|
|
if (priv->msgs_left == 1)
|
2015-12-23 16:56:33 +00:00
|
|
|
priv->flags |= ID_LAST_MSG;
|
2015-11-19 15:56:45 +00:00
|
|
|
|
2014-05-28 07:44:41 +00:00
|
|
|
rcar_i2c_write(priv, ICMAR, (priv->msg->addr << 1) | read);
|
2015-11-19 15:56:51 +00:00
|
|
|
/*
|
|
|
|
* We don't have a testcase but the HW engineers say that the write order
|
|
|
|
* of ICMSR and ICMCR depends on whether we issue START or REP_START. Since
|
|
|
|
* it didn't cause a drawback for me, let's rather be safe than sorry.
|
|
|
|
*/
|
2015-12-23 16:56:33 +00:00
|
|
|
if (priv->flags & ID_FIRST_MSG) {
|
2015-11-19 15:56:51 +00:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
|
|
|
|
} else {
|
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
|
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
|
|
|
}
|
2014-05-28 07:44:41 +00:00
|
|
|
rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:46 +00:00
|
|
|
static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
|
|
|
priv->msg++;
|
|
|
|
priv->msgs_left--;
|
2015-12-23 16:56:34 +00:00
|
|
|
priv->flags &= ID_P_MASK;
|
2015-11-19 15:56:46 +00:00
|
|
|
rcar_i2c_prepare_msg(priv);
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
/*
|
|
|
|
* interrupt functions
|
|
|
|
*/
|
2015-11-19 15:56:46 +00:00
|
|
|
static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
|
|
|
struct i2c_msg *msg = priv->msg;
|
|
|
|
|
2015-11-19 15:56:50 +00:00
|
|
|
/* FIXME: sometimes, unknown interrupt happened. Do nothing */
|
2012-09-28 06:44:25 +00:00
|
|
|
if (!(msr & MDE))
|
2015-11-19 15:56:46 +00:00
|
|
|
return;
|
2012-09-28 06:44:25 +00: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]
|
|
|
|
*/
|
|
|
|
|
2015-11-19 15:56:46 +00:00
|
|
|
if (priv->flags & ID_LAST_MSG) {
|
2012-09-28 06:44:25 +00:00
|
|
|
/*
|
|
|
|
* If current msg is the _LAST_ msg,
|
|
|
|
* prepare stop condition here.
|
|
|
|
* ID_DONE will be set on STOP irq.
|
|
|
|
*/
|
2014-05-28 07:44:38 +00:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
2015-11-19 15:56:46 +00:00
|
|
|
} else {
|
|
|
|
rcar_i2c_next_msg(priv);
|
|
|
|
return;
|
|
|
|
}
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 07:44:42 +00:00
|
|
|
rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:46 +00:00
|
|
|
static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
|
|
|
struct i2c_msg *msg = priv->msg;
|
|
|
|
|
2015-11-19 15:56:50 +00:00
|
|
|
/* FIXME: sometimes, unknown interrupt happened. Do nothing */
|
2012-09-28 06:44:25 +00:00
|
|
|
if (!(msr & MDR))
|
2015-11-19 15:56:46 +00:00
|
|
|
return;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
if (msr & MAT) {
|
2015-11-19 15:56:49 +00:00
|
|
|
/* Address transfer phase finished, but no data at this point. */
|
2012-09-28 06:44:25 +00:00
|
|
|
} else if (priv->pos < msg->len) {
|
2015-11-19 15:56:50 +00:00
|
|
|
/* get received data */
|
2012-09-28 06:44:25 +00:00
|
|
|
msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
|
|
|
|
priv->pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-11-19 15:56:50 +00:00
|
|
|
* If next received data is the _LAST_, go to STOP phase. Might be
|
|
|
|
* overwritten by REP START when setting up a new msg. Not elegant
|
|
|
|
* but the only stable sequence for REP START I have found so far.
|
2012-09-28 06:44:25 +00:00
|
|
|
*/
|
|
|
|
if (priv->pos + 1 >= msg->len)
|
2014-05-28 07:44:38 +00:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2015-11-19 15:56:46 +00:00
|
|
|
if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
|
|
|
|
rcar_i2c_next_msg(priv);
|
|
|
|
else
|
|
|
|
rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 16:04:55 +00:00
|
|
|
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) {
|
2015-03-23 08:26:36 +00:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
|
2014-11-18 16:04:55 +00:00
|
|
|
rcar_i2c_write(priv, ICRXTX, value);
|
|
|
|
rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
|
|
|
|
} else {
|
2015-03-23 08:26:36 +00:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
|
2014-11-18 16:04:55 +00:00
|
|
|
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);
|
2015-03-23 08:26:36 +00:00
|
|
|
ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
|
2014-11-18 16:04:55 +00:00
|
|
|
/* 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) {
|
2015-03-23 08:26:36 +00:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
|
2014-11-18 16:04:55 +00:00
|
|
|
rcar_i2c_write(priv, ICRXTX, value);
|
|
|
|
rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = ptr;
|
2015-11-19 15:56:49 +00:00
|
|
|
u32 msr, val;
|
2014-08-23 20:44:09 +00:00
|
|
|
|
2015-11-19 15:56:49 +00:00
|
|
|
/* Clear START or STOP as soon as we can */
|
|
|
|
val = rcar_i2c_read(priv, ICMCR);
|
|
|
|
rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
|
2014-11-18 16:04:55 +00:00
|
|
|
|
2014-05-28 07:44:36 +00:00
|
|
|
msr = rcar_i2c_read(priv, ICMSR);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2014-09-01 21:15:26 +00:00
|
|
|
/* Only handle interrupts that are currently enabled */
|
|
|
|
msr &= rcar_i2c_read(priv, ICMIER);
|
2014-09-14 20:20:19 +00:00
|
|
|
if (!msr) {
|
2015-11-19 15:56:48 +00:00
|
|
|
if (rcar_i2c_slave_irq(priv))
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
|
|
|
|
return IRQ_NONE;
|
2014-09-14 20:20:19 +00:00
|
|
|
}
|
2014-09-01 21:15:26 +00:00
|
|
|
|
2014-05-28 07:44:45 +00:00
|
|
|
/* Arbitration lost */
|
2012-09-28 06:44:25 +00:00
|
|
|
if (msr & MAL) {
|
2015-12-23 16:56:33 +00:00
|
|
|
priv->flags |= ID_DONE | ID_ARBLOST;
|
2012-09-28 06:44:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-05-28 07:44:45 +00:00
|
|
|
/* Nack */
|
2012-09-28 06:44:25 +00:00
|
|
|
if (msr & MNR) {
|
2015-11-19 15:56:47 +00:00
|
|
|
/* HW automatically sends STOP after received NACK */
|
2014-05-28 07:44:39 +00:00
|
|
|
rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
|
2015-12-23 16:56:33 +00:00
|
|
|
priv->flags |= ID_NACK;
|
2012-09-28 06:44:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-09-01 21:15:26 +00:00
|
|
|
/* Stop */
|
|
|
|
if (msr & MST) {
|
2015-11-19 15:56:46 +00:00
|
|
|
priv->msgs_left--; /* The last message also made it */
|
2015-12-23 16:56:33 +00:00
|
|
|
priv->flags |= ID_DONE;
|
2014-09-01 21:15:26 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
if (rcar_i2c_is_recv(priv))
|
2015-11-19 15:56:46 +00:00
|
|
|
rcar_i2c_irq_recv(priv, msr);
|
2012-09-28 06:44:25 +00:00
|
|
|
else
|
2015-11-19 15:56:46 +00:00
|
|
|
rcar_i2c_irq_send(priv, msr);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
out:
|
2015-12-23 16:56:33 +00:00
|
|
|
if (priv->flags & ID_DONE) {
|
2014-05-28 07:44:39 +00:00
|
|
|
rcar_i2c_write(priv, ICMIER, 0);
|
2014-05-28 07:44:42 +00:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
2012-09-28 06:44:25 +00:00
|
|
|
wake_up(&priv->wait);
|
|
|
|
}
|
|
|
|
|
2015-11-19 15:56:48 +00:00
|
|
|
return IRQ_HANDLED;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-06-20 19:03:20 +00:00
|
|
|
int i, ret;
|
2015-11-19 15:56:44 +00:00
|
|
|
long time_left;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
|
2014-05-28 07:44:40 +00:00
|
|
|
ret = rcar_i2c_bus_barrier(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2014-05-05 16:36:21 +00:00
|
|
|
/* This HW can't send STOP after address phase */
|
|
|
|
if (msgs[i].len == 0) {
|
|
|
|
ret = -EOPNOTSUPP;
|
2015-11-19 15:56:46 +00:00
|
|
|
goto out;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
2015-11-19 15:56:46 +00:00
|
|
|
}
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2015-11-19 15:56:51 +00:00
|
|
|
/* init first message */
|
2015-11-19 15:56:46 +00:00
|
|
|
priv->msg = msgs;
|
|
|
|
priv->msgs_left = num;
|
2015-12-23 16:56:34 +00:00
|
|
|
priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
|
2015-11-19 15:56:46 +00:00
|
|
|
rcar_i2c_prepare_msg(priv);
|
|
|
|
|
2015-12-23 16:56:33 +00:00
|
|
|
time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
|
2015-11-19 15:56:46 +00:00
|
|
|
num * adap->timeout);
|
|
|
|
if (!time_left) {
|
|
|
|
rcar_i2c_init(priv);
|
|
|
|
ret = -ETIMEDOUT;
|
2015-12-23 16:56:33 +00:00
|
|
|
} else if (priv->flags & ID_NACK) {
|
2015-11-19 15:56:46 +00:00
|
|
|
ret = -ENXIO;
|
2015-12-23 16:56:33 +00:00
|
|
|
} else if (priv->flags & ID_ARBLOST) {
|
2015-11-19 15:56:46 +00:00
|
|
|
ret = -EAGAIN;
|
|
|
|
} else {
|
|
|
|
ret = num - priv->msgs_left; /* The number of transfer */
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
2014-05-28 07:44:40 +00:00
|
|
|
out:
|
2012-09-28 06:44:25 +00:00
|
|
|
pm_runtime_put(dev);
|
|
|
|
|
2014-01-26 16:05:37 +00:00
|
|
|
if (ret < 0 && ret != -ENXIO)
|
2012-09-28 06:44:25 +00:00
|
|
|
dev_err(dev, "error %d : %x\n", ret, priv->flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:04:55 +00:00
|
|
|
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;
|
|
|
|
|
2015-12-16 19:05:18 +00:00
|
|
|
pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
|
2014-11-18 16:04:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-12-16 19:05:18 +00:00
|
|
|
pm_runtime_put(rcar_i2c_priv_to_dev(priv));
|
2014-11-18 16:04:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
static u32 rcar_i2c_func(struct i2c_adapter *adap)
|
|
|
|
{
|
2014-05-05 16:36:21 +00:00
|
|
|
/* This HW can't do SMBUS_QUICK and NOSTART */
|
2015-05-14 12:40:03 +00:00
|
|
|
return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
|
|
|
|
(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_algorithm rcar_i2c_algo = {
|
|
|
|
.master_xfer = rcar_i2c_master_xfer,
|
|
|
|
.functionality = rcar_i2c_func,
|
2014-11-18 16:04:55 +00:00
|
|
|
.reg_slave = rcar_reg_slave,
|
|
|
|
.unreg_slave = rcar_unreg_slave,
|
2012-09-28 06:44:25 +00:00
|
|
|
};
|
|
|
|
|
2013-09-12 12:36:46 +00:00
|
|
|
static const struct of_device_id rcar_i2c_dt_ids[] = {
|
2013-10-21 08:04:32 +00: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 08:03:20 +00:00
|
|
|
{ .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
|
2014-05-27 12:06:28 +00: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 },
|
2015-08-05 13:18:25 +00:00
|
|
|
{ .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
|
2013-09-12 12:36:46 +00:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
|
|
|
|
|
2012-11-27 20:59:38 +00:00
|
|
|
static int rcar_i2c_probe(struct platform_device *pdev)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv;
|
|
|
|
struct i2c_adapter *adap;
|
|
|
|
struct resource *res;
|
|
|
|
struct device *dev = &pdev->dev;
|
2015-12-08 09:37:48 +00:00
|
|
|
struct i2c_timings i2c_t;
|
2014-05-28 07:44:37 +00:00
|
|
|
int irq, ret;
|
2012-09-28 06:44:25 +00:00
|
|
|
|
|
|
|
priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
|
2014-05-13 01:51:58 +00:00
|
|
|
if (!priv)
|
2012-09-28 06:44:25 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-01-26 16:05:35 +00: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);
|
|
|
|
}
|
|
|
|
|
2013-05-10 08:16:54 +00:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-01-21 10:09:03 +00:00
|
|
|
priv->io = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(priv->io))
|
|
|
|
return PTR_ERR(priv->io);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2016-03-01 16:36:43 +00:00
|
|
|
priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
|
2012-09-28 06:44:25 +00:00
|
|
|
init_waitqueue_head(&priv->wait);
|
|
|
|
|
2014-07-10 11:46:31 +00:00
|
|
|
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;
|
2012-09-28 06:44:25 +00:00
|
|
|
i2c_set_adapdata(adap, priv);
|
|
|
|
strlcpy(adap->name, pdev->name, sizeof(adap->name));
|
|
|
|
|
2015-12-08 09:37:48 +00:00
|
|
|
i2c_parse_fw_timings(dev, &i2c_t, false);
|
2015-12-08 09:37:47 +00:00
|
|
|
|
|
|
|
pm_runtime_enable(dev);
|
|
|
|
pm_runtime_get_sync(dev);
|
2015-12-08 09:37:48 +00:00
|
|
|
ret = rcar_i2c_clock_calculate(priv, &i2c_t);
|
2015-12-08 09:37:47 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out_pm_put;
|
|
|
|
|
|
|
|
rcar_i2c_init(priv);
|
2015-12-23 16:56:34 +00:00
|
|
|
|
|
|
|
/* Don't suspend when multi-master to keep arbitration working */
|
|
|
|
if (of_property_read_bool(dev->of_node, "multi-master"))
|
|
|
|
priv->flags |= ID_P_PM_BLOCKED;
|
|
|
|
else
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
|
2015-12-08 09:37:47 +00:00
|
|
|
|
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
ret = devm_request_irq(dev, irq, rcar_i2c_irq, 0, dev_name(dev), priv);
|
2012-09-28 06:44:25 +00:00
|
|
|
if (ret < 0) {
|
2014-05-28 07:44:37 +00:00
|
|
|
dev_err(dev, "cannot get irq %d\n", irq);
|
2015-11-19 15:56:41 +00:00
|
|
|
goto out_pm_disable;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 09:39:25 +00:00
|
|
|
platform_set_drvdata(pdev, priv);
|
|
|
|
|
2012-09-28 06:44:25 +00:00
|
|
|
ret = i2c_add_numbered_adapter(adap);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(dev, "reg adap failed: %d\n", ret);
|
2015-11-19 15:56:41 +00:00
|
|
|
goto out_pm_disable;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_info(dev, "probed\n");
|
|
|
|
|
|
|
|
return 0;
|
2015-11-19 15:56:41 +00:00
|
|
|
|
|
|
|
out_pm_put:
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
out_pm_disable:
|
|
|
|
pm_runtime_disable(dev);
|
|
|
|
return ret;
|
2012-09-28 06:44:25 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 20:59:38 +00:00
|
|
|
static int rcar_i2c_remove(struct platform_device *pdev)
|
2012-09-28 06:44:25 +00:00
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
|
|
|
|
i2c_del_adapter(&priv->adap);
|
2015-12-23 16:56:34 +00:00
|
|
|
if (priv->flags & ID_P_PM_BLOCKED)
|
|
|
|
pm_runtime_put(dev);
|
2012-09-28 06:44:25 +00:00
|
|
|
pm_runtime_disable(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-13 10:24:15 +00:00
|
|
|
static struct platform_driver rcar_i2c_driver = {
|
2012-09-28 06:44:25 +00:00
|
|
|
.driver = {
|
|
|
|
.name = "i2c-rcar",
|
2013-09-12 12:36:46 +00:00
|
|
|
.of_match_table = rcar_i2c_dt_ids,
|
2012-09-28 06:44:25 +00:00
|
|
|
},
|
|
|
|
.probe = rcar_i2c_probe,
|
2012-11-27 20:59:38 +00:00
|
|
|
.remove = rcar_i2c_remove,
|
2012-09-28 06:44:25 +00:00
|
|
|
};
|
|
|
|
|
2012-11-13 10:24:15 +00:00
|
|
|
module_platform_driver(rcar_i2c_driver);
|
2012-09-28 06:44:25 +00:00
|
|
|
|
2014-05-28 07:44:46 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|
2012-09-28 06:44:25 +00:00
|
|
|
MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
|
|
|
|
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
|