2018-08-22 06:02:16 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2012-09-28 14:44:25 +08:00
|
|
|
/*
|
2017-05-28 17:30:45 +08:00
|
|
|
* Driver for the Renesas R-Car I2C unit
|
2012-09-28 14:44:25 +08:00
|
|
|
*
|
2019-01-22 01:07:59 +08:00
|
|
|
* Copyright (C) 2014-19 Wolfram Sang <wsa@sang-engineering.com>
|
|
|
|
* Copyright (C) 2011-2019 Renesas Electronics Corporation
|
2014-05-28 15:44:46 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012-14 Renesas Solutions Corp.
|
2012-09-28 14:44:25 +08: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>
|
|
|
|
*/
|
2018-08-08 15:59:27 +08:00
|
|
|
#include <linux/bitops.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
2016-05-14 20:17:08 +08:00
|
|
|
#include <linux/dmaengine.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/io.h>
|
2020-08-30 04:38:09 +08:00
|
|
|
#include <linux/iopoll.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#include <linux/i2c.h>
|
2020-09-10 17:11:18 +08:00
|
|
|
#include <linux/i2c-smbus.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#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>
|
2018-06-29 04:45:38 +08:00
|
|
|
#include <linux/reset.h>
|
2012-09-28 14:44:25 +08:00
|
|
|
#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 */
|
2019-02-05 21:37:25 +08:00
|
|
|
#define ICFBSCR 0x38 /* first bit setup cycle (Gen3) */
|
|
|
|
#define ICDMAER 0x3c /* DMA enable (Gen3) */
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-11-19 00:04:55 +08:00
|
|
|
/* ICSCR */
|
2022-05-21 04:29:16 +08:00
|
|
|
#define SDBS BIT(3) /* slave data buffer select */
|
|
|
|
#define SIE BIT(2) /* slave interface enable */
|
|
|
|
#define GCAE BIT(1) /* general call address enable */
|
|
|
|
#define FNA BIT(0) /* forced non acknowledgment */
|
2014-11-19 00:04:55 +08:00
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
/* ICMCR */
|
2022-05-21 04:29:16 +08:00
|
|
|
#define MDBS BIT(7) /* non-fifo mode switch */
|
|
|
|
#define FSCL BIT(6) /* override SCL pin */
|
|
|
|
#define FSDA BIT(5) /* override SDA pin */
|
|
|
|
#define OBPC BIT(4) /* override pins */
|
|
|
|
#define MIE BIT(3) /* master if enable */
|
|
|
|
#define TSBE BIT(2)
|
|
|
|
#define FSB BIT(1) /* force stop bit */
|
|
|
|
#define ESG BIT(0) /* enable start bit gen */
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2014-11-19 00:04:55 +08:00
|
|
|
/* ICSSR (also for ICSIER) */
|
2022-05-21 04:29:16 +08:00
|
|
|
#define GCAR BIT(6) /* general call received */
|
|
|
|
#define STM BIT(5) /* slave transmit mode */
|
|
|
|
#define SSR BIT(4) /* stop received */
|
|
|
|
#define SDE BIT(3) /* slave data empty */
|
|
|
|
#define SDT BIT(2) /* slave data transmitted */
|
|
|
|
#define SDR BIT(1) /* slave data received */
|
|
|
|
#define SAR BIT(0) /* slave addr received */
|
2014-11-19 00:04:55 +08:00
|
|
|
|
2014-05-28 15:44:44 +08:00
|
|
|
/* ICMSR (also for ICMIE) */
|
2022-05-21 04:29:16 +08:00
|
|
|
#define MNR BIT(6) /* nack received */
|
|
|
|
#define MAL BIT(5) /* arbitration lost */
|
|
|
|
#define MST BIT(4) /* sent a stop */
|
|
|
|
#define MDE BIT(3)
|
|
|
|
#define MDT BIT(2)
|
|
|
|
#define MDR BIT(1)
|
|
|
|
#define MAT BIT(0) /* slave addr xfer done */
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2016-05-14 20:17:08 +08:00
|
|
|
/* ICDMAER */
|
2022-05-21 04:29:16 +08:00
|
|
|
#define RSDMAE BIT(3) /* DMA Slave Received Enable */
|
|
|
|
#define TSDMAE BIT(2) /* DMA Slave Transmitted Enable */
|
|
|
|
#define RMDMAE BIT(1) /* DMA Master Received Enable */
|
|
|
|
#define TMDMAE BIT(0) /* DMA Master Transmitted Enable */
|
2016-05-14 20:17:08 +08:00
|
|
|
|
|
|
|
/* ICFBSCR */
|
|
|
|
#define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */
|
|
|
|
|
2019-03-06 01:54:32 +08:00
|
|
|
#define RCAR_MIN_DMA_LEN 8
|
2012-09-28 14:44:25 +08:00
|
|
|
|
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:44 +08: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 14:44:25 +08:00
|
|
|
|
2022-05-21 04:29:16 +08:00
|
|
|
#define ID_LAST_MSG BIT(0)
|
2022-05-21 04:29:17 +08:00
|
|
|
#define ID_REP_AFTER_RD BIT(1)
|
2022-05-21 04:29:16 +08:00
|
|
|
#define ID_DONE BIT(2)
|
|
|
|
#define ID_ARBLOST BIT(3)
|
|
|
|
#define ID_NACK BIT(4)
|
|
|
|
#define ID_EPROTO BIT(5)
|
2015-12-24 00:56:34 +08:00
|
|
|
/* persistent flags */
|
2022-05-21 04:29:18 +08:00
|
|
|
#define ID_P_NOT_ATOMIC BIT(28)
|
2022-05-21 04:29:17 +08:00
|
|
|
#define ID_P_HOST_NOTIFY BIT(29)
|
2018-08-08 15:59:27 +08:00
|
|
|
#define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */
|
|
|
|
#define ID_P_PM_BLOCKED BIT(31)
|
2022-05-21 04:29:18 +08:00
|
|
|
#define ID_P_MASK GENMASK(31, 28)
|
2012-09-28 14:44:25 +08:00
|
|
|
|
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,
|
2015-08-05 21:18:25 +08:00
|
|
|
I2C_RCAR_GEN3,
|
2013-09-03 08:09:25 +08:00
|
|
|
};
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
struct rcar_i2c_priv {
|
2020-12-24 01:21:52 +08:00
|
|
|
u32 flags;
|
2012-09-28 14:44:25 +08:00
|
|
|
void __iomem *io;
|
|
|
|
struct i2c_adapter adap;
|
2015-11-19 23:56:45 +08:00
|
|
|
struct i2c_msg *msg;
|
|
|
|
int msgs_left;
|
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;
|
2018-01-09 21:58:59 +08:00
|
|
|
u8 recovery_icmcr; /* protected by adapter lock */
|
2014-05-28 15:44:45 +08:00
|
|
|
enum rcar_i2c_type devtype;
|
2014-11-19 00:04:55 +08:00
|
|
|
struct i2c_client *slave;
|
2016-05-14 20:17:08 +08:00
|
|
|
|
|
|
|
struct resource *res;
|
|
|
|
struct dma_chan *dma_tx;
|
|
|
|
struct dma_chan *dma_rx;
|
|
|
|
struct scatterlist sg;
|
|
|
|
enum dma_data_direction dma_direction;
|
2018-06-29 04:45:38 +08:00
|
|
|
|
|
|
|
struct reset_control *rstc;
|
2019-08-09 03:39:10 +08:00
|
|
|
int irq;
|
2020-09-10 17:11:18 +08:00
|
|
|
|
|
|
|
struct i2c_client *host_notify_client;
|
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)
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:33:24 +08:00
|
|
|
static void rcar_i2c_clear_irq(struct rcar_i2c_priv *priv, u32 val)
|
|
|
|
{
|
|
|
|
writel(~val & 0x7f, priv->io + ICMSR);
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:58:59 +08:00
|
|
|
static int rcar_i2c_get_scl(struct i2c_adapter *adap)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
|
|
|
|
|
|
|
|
return !!(rcar_i2c_read(priv, ICMCR) & FSCL);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static void rcar_i2c_set_scl(struct i2c_adapter *adap, int val)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
priv->recovery_icmcr |= FSCL;
|
|
|
|
else
|
|
|
|
priv->recovery_icmcr &= ~FSCL;
|
|
|
|
|
|
|
|
rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
|
|
|
|
|
|
|
|
if (val)
|
|
|
|
priv->recovery_icmcr |= FSDA;
|
|
|
|
else
|
|
|
|
priv->recovery_icmcr &= ~FSDA;
|
|
|
|
|
|
|
|
rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
|
|
|
|
};
|
|
|
|
|
2018-07-11 06:24:23 +08:00
|
|
|
static int rcar_i2c_get_bus_free(struct i2c_adapter *adap)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
|
|
|
|
|
|
|
|
return !(rcar_i2c_read(priv, ICMCR) & FSDA);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-01-09 21:58:59 +08:00
|
|
|
static struct i2c_bus_recovery_info rcar_i2c_bri = {
|
|
|
|
.get_scl = rcar_i2c_get_scl,
|
|
|
|
.set_scl = rcar_i2c_set_scl,
|
|
|
|
.set_sda = rcar_i2c_set_sda,
|
2018-07-11 06:24:23 +08:00
|
|
|
.get_bus_free = rcar_i2c_get_bus_free,
|
2018-01-09 21:58:59 +08:00
|
|
|
.recover_bus = i2c_generic_scl_recovery,
|
|
|
|
};
|
2012-09-28 14:44:25 +08:00
|
|
|
static void rcar_i2c_init(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
|
|
|
/* reset master mode */
|
|
|
|
rcar_i2c_write(priv, ICMIER, 0);
|
2015-11-19 23:56:42 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, MDBS);
|
2012-09-28 14:44:25 +08:00
|
|
|
rcar_i2c_write(priv, ICMSR, 0);
|
2015-11-19 23:56:42 +08:00
|
|
|
/* start clock */
|
|
|
|
rcar_i2c_write(priv, ICCCR, priv->icccr);
|
2019-02-05 21:37:25 +08:00
|
|
|
|
|
|
|
if (priv->devtype == I2C_RCAR_GEN3)
|
|
|
|
rcar_i2c_write(priv, ICFBSCR, TCYC17);
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
2020-08-30 04:38:09 +08:00
|
|
|
int ret;
|
|
|
|
u32 val;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2020-08-30 04:38:09 +08:00
|
|
|
ret = readl_poll_timeout(priv->io + ICMCR, val, !(val & FSDA), 10,
|
|
|
|
priv->adap.timeout);
|
|
|
|
if (ret) {
|
|
|
|
/* Waiting did not help, try to recover */
|
|
|
|
priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL;
|
|
|
|
ret = i2c_recover_bus(&priv->adap);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2020-08-30 04:38:09 +08:00
|
|
|
return ret;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 20:32:13 +08:00
|
|
|
static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
2015-12-08 17:37:49 +08:00
|
|
|
u32 scgd, cdf, round, ick, sum, scl, cdf_width;
|
2013-09-12 20:36:45 +08:00
|
|
|
unsigned long rate;
|
2015-12-08 17:37:47 +08:00
|
|
|
struct device *dev = rcar_i2c_priv_to_dev(priv);
|
2020-03-26 18:07:21 +08:00
|
|
|
struct i2c_timings t = {
|
2020-03-24 20:32:13 +08:00
|
|
|
.bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ,
|
|
|
|
.scl_fall_ns = 35,
|
|
|
|
.scl_rise_ns = 200,
|
|
|
|
.scl_int_delay_ns = 50,
|
2020-03-26 18:07:21 +08:00
|
|
|
};
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-12-08 17:37:48 +08:00
|
|
|
/* Fall back to previously used values if not supplied */
|
2020-03-26 18:07:21 +08:00
|
|
|
i2c_parse_fw_timings(dev, &t, false);
|
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:
|
2015-08-05 21:18:25 +08:00
|
|
|
case I2C_RCAR_GEN3:
|
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
|
2015-12-08 17:37:49 +08:00
|
|
|
* ticf : I2C SCL falling time
|
|
|
|
* tr : I2C SCL rising time
|
|
|
|
* intd : LSI internal delay
|
2012-09-28 14:44:25 +08:00
|
|
|
* 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;
|
2014-09-20 18:07:37 +08:00
|
|
|
if (cdf >= 1U << cdf_width) {
|
2013-09-12 20:36:45 +08:00
|
|
|
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
|
|
|
|
*
|
2015-12-08 17:37:49 +08:00
|
|
|
* F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
|
|
|
|
* = F[sum * ick / 1000000000]
|
|
|
|
* = F[(ick / 1000000) * sum / 1000]
|
2012-09-28 14:44:25 +08:00
|
|
|
*/
|
2020-03-26 18:07:21 +08:00
|
|
|
sum = t.scl_fall_ns + t.scl_rise_ns + t.scl_int_delay_ns;
|
2015-12-08 17:37:49 +08:00
|
|
|
round = (ick + 500000) / 1000000 * sum;
|
2012-09-28 14:44:25 +08: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 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);
|
2020-03-26 18:07:21 +08:00
|
|
|
if (scl <= t.bus_freq_hz)
|
2012-09-28 14:44:25 +08: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",
|
2020-03-26 18:07:21 +08:00
|
|
|
scl, t.bus_freq_hz, rate, round, cdf, scgd);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-11-19 23:56:50 +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;
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:33:25 +08:00
|
|
|
/*
|
|
|
|
* We don't have a test case but the HW engineers say that the write order of
|
|
|
|
* ICMSR and ICMCR depends on whether we issue START or REP_START. So, ICMSR
|
|
|
|
* handling is outside of this function. First messages clear ICMSR before this
|
|
|
|
* function, interrupt handlers clear the relevant bits after this function.
|
|
|
|
*/
|
2014-09-15 04:15:46 +08:00
|
|
|
static void 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);
|
2022-05-21 04:29:17 +08:00
|
|
|
bool rep_start = !(priv->flags & ID_REP_AFTER_RD);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-11-19 23:56:45 +08:00
|
|
|
priv->pos = 0;
|
2022-05-20 18:33:25 +08:00
|
|
|
priv->flags &= ID_P_MASK;
|
|
|
|
|
2015-11-19 23:56:45 +08:00
|
|
|
if (priv->msgs_left == 1)
|
2015-12-24 00:56:33 +08:00
|
|
|
priv->flags |= ID_LAST_MSG;
|
2015-11-19 23:56:45 +08:00
|
|
|
|
2018-05-16 15:16:47 +08:00
|
|
|
rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg));
|
2022-05-21 04:29:18 +08:00
|
|
|
if (priv->flags & ID_P_NOT_ATOMIC)
|
2021-09-15 21:48:27 +08:00
|
|
|
rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
|
|
|
|
|
2022-05-21 04:29:17 +08:00
|
|
|
if (rep_start)
|
2015-11-19 23:56:51 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
|
2022-05-20 18:33:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_i2c_first_msg(struct rcar_i2c_priv *priv,
|
|
|
|
struct i2c_msg *msgs, int num)
|
|
|
|
{
|
|
|
|
priv->msg = msgs;
|
|
|
|
priv->msgs_left = num;
|
|
|
|
rcar_i2c_write(priv, ICMSR, 0); /* must be before preparing msg */
|
|
|
|
rcar_i2c_prepare_msg(priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:56:46 +08:00
|
|
|
static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
|
|
|
priv->msg++;
|
|
|
|
priv->msgs_left--;
|
|
|
|
rcar_i2c_prepare_msg(priv);
|
2022-05-20 18:33:25 +08:00
|
|
|
/* ICMSR handling must come afterwards in the irq handler */
|
2015-11-19 23:56:46 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:45:42 +08:00
|
|
|
static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv, bool terminate)
|
2016-05-14 20:17:08 +08:00
|
|
|
{
|
|
|
|
struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE
|
|
|
|
? priv->dma_rx : priv->dma_tx;
|
|
|
|
|
2021-12-08 16:45:42 +08:00
|
|
|
/* only allowed from thread context! */
|
|
|
|
if (terminate)
|
|
|
|
dmaengine_terminate_sync(chan);
|
|
|
|
|
2016-05-14 20:17:08 +08:00
|
|
|
dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
|
2017-05-28 15:52:17 +08:00
|
|
|
sg_dma_len(&priv->sg), priv->dma_direction);
|
2016-05-14 20:17:08 +08:00
|
|
|
|
2018-06-29 04:45:38 +08:00
|
|
|
/* Gen3 can only do one RXDMA per transfer and we just completed it */
|
|
|
|
if (priv->devtype == I2C_RCAR_GEN3 &&
|
|
|
|
priv->dma_direction == DMA_FROM_DEVICE)
|
|
|
|
priv->flags |= ID_P_NO_RXDMA;
|
|
|
|
|
2016-05-14 20:17:08 +08:00
|
|
|
priv->dma_direction = DMA_NONE;
|
2019-03-03 23:03:13 +08:00
|
|
|
|
|
|
|
/* Disable DMA Master Received/Transmitted, must be last! */
|
|
|
|
rcar_i2c_write(priv, ICDMAER, 0);
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_i2c_dma_callback(void *data)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = data;
|
|
|
|
|
|
|
|
priv->pos += sg_dma_len(&priv->sg);
|
|
|
|
|
2021-12-08 16:45:42 +08:00
|
|
|
rcar_i2c_cleanup_dma(priv, false);
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 01:54:33 +08:00
|
|
|
static bool rcar_i2c_dma(struct rcar_i2c_priv *priv)
|
2016-05-14 20:17:08 +08:00
|
|
|
{
|
|
|
|
struct device *dev = rcar_i2c_priv_to_dev(priv);
|
|
|
|
struct i2c_msg *msg = priv->msg;
|
|
|
|
bool read = msg->flags & I2C_M_RD;
|
|
|
|
enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
|
|
|
|
struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx;
|
|
|
|
struct dma_async_tx_descriptor *txdesc;
|
|
|
|
dma_addr_t dma_addr;
|
|
|
|
dma_cookie_t cookie;
|
|
|
|
unsigned char *buf;
|
|
|
|
int len;
|
|
|
|
|
2018-06-29 04:45:38 +08:00
|
|
|
/* Do various checks to see if DMA is feasible at all */
|
2022-05-21 04:29:18 +08:00
|
|
|
if (!(priv->flags & ID_P_NOT_ATOMIC) || IS_ERR(chan) || msg->len < RCAR_MIN_DMA_LEN ||
|
2019-03-06 01:54:32 +08:00
|
|
|
!(msg->flags & I2C_M_DMA_SAFE) || (read && priv->flags & ID_P_NO_RXDMA))
|
2019-03-06 01:54:33 +08:00
|
|
|
return false;
|
2016-05-14 20:17:08 +08:00
|
|
|
|
|
|
|
if (read) {
|
|
|
|
/*
|
|
|
|
* The last two bytes needs to be fetched using PIO in
|
|
|
|
* order for the STOP phase to work.
|
|
|
|
*/
|
|
|
|
buf = priv->msg->buf;
|
|
|
|
len = priv->msg->len - 2;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* First byte in message was sent using PIO.
|
|
|
|
*/
|
|
|
|
buf = priv->msg->buf + 1;
|
|
|
|
len = priv->msg->len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
|
2016-08-24 17:19:29 +08:00
|
|
|
if (dma_mapping_error(chan->device->dev, dma_addr)) {
|
2016-05-14 20:17:08 +08:00
|
|
|
dev_dbg(dev, "dma map failed, using PIO\n");
|
2019-03-06 01:54:33 +08:00
|
|
|
return false;
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sg_dma_len(&priv->sg) = len;
|
|
|
|
sg_dma_address(&priv->sg) = dma_addr;
|
|
|
|
|
|
|
|
priv->dma_direction = dir;
|
|
|
|
|
|
|
|
txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1,
|
|
|
|
read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!txdesc) {
|
|
|
|
dev_dbg(dev, "dma prep slave sg failed, using PIO\n");
|
2021-12-08 16:45:42 +08:00
|
|
|
rcar_i2c_cleanup_dma(priv, false);
|
2019-03-06 01:54:33 +08:00
|
|
|
return false;
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
txdesc->callback = rcar_i2c_dma_callback;
|
|
|
|
txdesc->callback_param = priv;
|
|
|
|
|
|
|
|
cookie = dmaengine_submit(txdesc);
|
|
|
|
if (dma_submit_error(cookie)) {
|
|
|
|
dev_dbg(dev, "submitting dma failed, using PIO\n");
|
2021-12-08 16:45:42 +08:00
|
|
|
rcar_i2c_cleanup_dma(priv, false);
|
2019-03-06 01:54:33 +08:00
|
|
|
return false;
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable DMA Master Received/Transmitted */
|
|
|
|
if (read)
|
|
|
|
rcar_i2c_write(priv, ICDMAER, RMDMAE);
|
|
|
|
else
|
|
|
|
rcar_i2c_write(priv, ICDMAER, TMDMAE);
|
|
|
|
|
|
|
|
dma_async_issue_pending(chan);
|
2019-03-06 01:54:33 +08:00
|
|
|
return true;
|
2016-05-14 20:17:08 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:56:46 +08:00
|
|
|
static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
|
|
|
struct i2c_msg *msg = priv->msg;
|
2022-05-20 18:33:24 +08:00
|
|
|
u32 irqs_to_clear = MDE;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-11-19 23:56:50 +08:00
|
|
|
/* FIXME: sometimes, unknown interrupt happened. Do nothing */
|
2012-09-28 14:44:25 +08:00
|
|
|
if (!(msr & MDE))
|
2015-11-19 23:56:46 +08:00
|
|
|
return;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2022-05-20 18:33:24 +08:00
|
|
|
if (msr & MAT)
|
|
|
|
irqs_to_clear |= MAT;
|
|
|
|
|
2019-03-06 01:54:34 +08:00
|
|
|
/* Check if DMA can be enabled and take over */
|
|
|
|
if (priv->pos == 1 && rcar_i2c_dma(priv))
|
|
|
|
return;
|
|
|
|
|
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]
|
|
|
|
*/
|
|
|
|
|
2022-05-20 18:33:24 +08:00
|
|
|
if (priv->flags & ID_LAST_MSG)
|
2012-09-28 14:44:25 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
2022-05-20 18:33:24 +08:00
|
|
|
else
|
2015-11-19 23:56:46 +08:00
|
|
|
rcar_i2c_next_msg(priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2022-05-20 18:33:24 +08:00
|
|
|
rcar_i2c_clear_irq(priv, irqs_to_clear);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:56:46 +08:00
|
|
|
static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
|
|
|
struct i2c_msg *msg = priv->msg;
|
2022-04-05 18:07:56 +08:00
|
|
|
bool recv_len_init = priv->pos == 0 && msg->flags & I2C_M_RECV_LEN;
|
2022-05-20 18:33:24 +08:00
|
|
|
u32 irqs_to_clear = MDR;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-11-19 23:56:50 +08:00
|
|
|
/* FIXME: sometimes, unknown interrupt happened. Do nothing */
|
2012-09-28 14:44:25 +08:00
|
|
|
if (!(msr & MDR))
|
2015-11-19 23:56:46 +08:00
|
|
|
return;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
if (msr & MAT) {
|
2022-05-20 18:33:24 +08:00
|
|
|
irqs_to_clear |= MAT;
|
2016-05-14 20:17:08 +08:00
|
|
|
/*
|
|
|
|
* Address transfer phase finished, but no data at this point.
|
|
|
|
* Try to use DMA to receive data.
|
|
|
|
*/
|
|
|
|
rcar_i2c_dma(priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
} else if (priv->pos < msg->len) {
|
2015-11-19 23:56:50 +08:00
|
|
|
/* get received data */
|
2022-04-05 18:07:56 +08:00
|
|
|
u8 data = rcar_i2c_read(priv, ICRXTX);
|
|
|
|
|
|
|
|
msg->buf[priv->pos] = data;
|
|
|
|
if (recv_len_init) {
|
|
|
|
if (data == 0 || data > I2C_SMBUS_BLOCK_MAX) {
|
|
|
|
priv->flags |= ID_DONE | ID_EPROTO;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msg->len += msg->buf[0];
|
|
|
|
/* Enough data for DMA? */
|
|
|
|
if (rcar_i2c_dma(priv))
|
|
|
|
return;
|
|
|
|
/* new length after RECV_LEN now properly initialized */
|
|
|
|
recv_len_init = false;
|
|
|
|
}
|
2012-09-28 14:44:25 +08:00
|
|
|
priv->pos++;
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:07:56 +08:00
|
|
|
/*
|
|
|
|
* If next received data is the _LAST_ and we are not waiting for a new
|
|
|
|
* length because of RECV_LEN, then go to a new phase.
|
|
|
|
*/
|
|
|
|
if (priv->pos + 1 == msg->len && !recv_len_init) {
|
2018-08-08 15:59:28 +08:00
|
|
|
if (priv->flags & ID_LAST_MSG) {
|
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
|
|
|
|
} else {
|
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
|
2022-05-21 04:29:17 +08:00
|
|
|
priv->flags |= ID_REP_AFTER_RD;
|
2018-08-08 15:59:28 +08:00
|
|
|
}
|
|
|
|
}
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2015-11-19 23:56:46 +08:00
|
|
|
if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
|
|
|
|
rcar_i2c_next_msg(priv);
|
2022-05-20 18:33:24 +08:00
|
|
|
|
|
|
|
rcar_i2c_clear_irq(priv, irqs_to_clear);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2014-11-19 00:04:55 +08: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 16:26:36 +08:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
|
2014-11-19 00:04:55 +08:00
|
|
|
rcar_i2c_write(priv, ICRXTX, value);
|
|
|
|
rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
|
|
|
|
} else {
|
2015-03-23 16:26:36 +08:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
|
2014-11-19 00:04:55 +08:00
|
|
|
rcar_i2c_read(priv, ICRXTX); /* dummy read */
|
|
|
|
rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:38:07 +08:00
|
|
|
/* Clear SSR, too, because of old STOPs to other clients than us */
|
|
|
|
rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
|
2014-11-19 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* master sent stop */
|
|
|
|
if (ssr_filtered & SSR) {
|
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
|
2020-08-17 20:19:30 +08:00
|
|
|
rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
|
2020-06-29 23:38:07 +08:00
|
|
|
rcar_i2c_write(priv, ICSIER, SAR);
|
2014-11-19 00:04:55 +08:00
|
|
|
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 16:26:36 +08:00
|
|
|
ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
|
2014-11-19 00:04:55 +08: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 16:26:36 +08:00
|
|
|
i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
|
2014-11-19 00:04:55 +08:00
|
|
|
rcar_i2c_write(priv, ICRXTX, value);
|
|
|
|
rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-03 23:03:14 +08:00
|
|
|
/*
|
|
|
|
* This driver has a lock-free design because there are IP cores (at least
|
|
|
|
* R-Car Gen2) which have an inherent race condition in their hardware design.
|
2020-12-24 01:21:51 +08:00
|
|
|
* There, we need to switch to RCAR_BUS_PHASE_DATA as soon as possible after
|
2019-03-03 23:03:14 +08:00
|
|
|
* the interrupt was generated, otherwise an unwanted repeated message gets
|
|
|
|
* generated. It turned out that taking a spinlock at the beginning of the ISR
|
|
|
|
* was already causing repeated messages. Thus, this driver was converted to
|
|
|
|
* the now lockless behaviour. Please keep this in mind when hacking the driver.
|
2020-12-24 01:21:54 +08:00
|
|
|
* R-Car Gen3 seems to have this fixed but earlier versions than R-Car Gen2 are
|
|
|
|
* likely affected. Therefore, we have different interrupt handler entries.
|
2019-03-03 23:03:14 +08:00
|
|
|
*/
|
2020-12-24 01:21:54 +08:00
|
|
|
static irqreturn_t rcar_i2c_irq(int irq, struct rcar_i2c_priv *priv, u32 msr)
|
2012-09-28 14:44:25 +08:00
|
|
|
{
|
2014-09-15 04:20:19 +08:00
|
|
|
if (!msr) {
|
2015-11-19 23:56:48 +08:00
|
|
|
if (rcar_i2c_slave_irq(priv))
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
|
|
|
|
return IRQ_NONE;
|
2014-09-15 04:20:19 +08:00
|
|
|
}
|
2014-09-02 05:15:26 +08:00
|
|
|
|
2014-05-28 15:44:45 +08:00
|
|
|
/* Arbitration lost */
|
2012-09-28 14:44:25 +08:00
|
|
|
if (msr & MAL) {
|
2015-12-24 00:56:33 +08:00
|
|
|
priv->flags |= ID_DONE | ID_ARBLOST;
|
2012-09-28 14:44:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:44:45 +08:00
|
|
|
/* Nack */
|
2012-09-28 14:44:25 +08:00
|
|
|
if (msr & MNR) {
|
2015-11-19 23:56:47 +08:00
|
|
|
/* HW automatically sends STOP after received NACK */
|
2022-05-21 04:29:18 +08:00
|
|
|
if (priv->flags & ID_P_NOT_ATOMIC)
|
2021-02-13 00:45:41 +08:00
|
|
|
rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
|
2015-12-24 00:56:33 +08:00
|
|
|
priv->flags |= ID_NACK;
|
2012-09-28 14:44:25 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-09-02 05:15:26 +08:00
|
|
|
/* Stop */
|
|
|
|
if (msr & MST) {
|
2015-11-19 23:56:46 +08:00
|
|
|
priv->msgs_left--; /* The last message also made it */
|
2015-12-24 00:56:33 +08:00
|
|
|
priv->flags |= ID_DONE;
|
2014-09-02 05:15:26 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
if (rcar_i2c_is_recv(priv))
|
2015-11-19 23:56:46 +08:00
|
|
|
rcar_i2c_irq_recv(priv, msr);
|
2012-09-28 14:44:25 +08:00
|
|
|
else
|
2015-11-19 23:56:46 +08:00
|
|
|
rcar_i2c_irq_send(priv, msr);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
|
|
|
out:
|
2015-12-24 00:56:33 +08:00
|
|
|
if (priv->flags & 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);
|
2022-05-21 04:29:18 +08:00
|
|
|
if (priv->flags & ID_P_NOT_ATOMIC)
|
2021-02-13 00:45:41 +08:00
|
|
|
wake_up(&priv->wait);
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 23:56:48 +08:00
|
|
|
return IRQ_HANDLED;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:21:54 +08:00
|
|
|
static irqreturn_t rcar_i2c_gen2_irq(int irq, void *ptr)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = ptr;
|
|
|
|
u32 msr;
|
|
|
|
|
|
|
|
/* Clear START or STOP immediately, except for REPSTART after read */
|
2022-05-21 04:29:17 +08:00
|
|
|
if (likely(!(priv->flags & ID_REP_AFTER_RD)))
|
2020-12-24 01:21:54 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
|
|
|
|
|
|
|
|
/* Only handle interrupts that are currently enabled */
|
|
|
|
msr = rcar_i2c_read(priv, ICMSR);
|
2022-05-21 04:29:18 +08:00
|
|
|
if (priv->flags & ID_P_NOT_ATOMIC)
|
2021-02-13 00:45:41 +08:00
|
|
|
msr &= rcar_i2c_read(priv, ICMIER);
|
2020-12-24 01:21:54 +08:00
|
|
|
|
|
|
|
return rcar_i2c_irq(irq, priv, msr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t rcar_i2c_gen3_irq(int irq, void *ptr)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = ptr;
|
|
|
|
u32 msr;
|
|
|
|
|
|
|
|
/* Only handle interrupts that are currently enabled */
|
|
|
|
msr = rcar_i2c_read(priv, ICMSR);
|
2022-05-21 04:29:18 +08:00
|
|
|
if (priv->flags & ID_P_NOT_ATOMIC)
|
2021-02-13 00:45:41 +08:00
|
|
|
msr &= rcar_i2c_read(priv, ICMIER);
|
2020-12-24 01:21:54 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear START or STOP immediately, except for REPSTART after read or
|
|
|
|
* if a spurious interrupt was detected.
|
|
|
|
*/
|
2022-05-21 04:29:17 +08:00
|
|
|
if (likely(!(priv->flags & ID_REP_AFTER_RD) && msr))
|
2020-12-24 01:21:54 +08:00
|
|
|
rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_DATA);
|
|
|
|
|
|
|
|
return rcar_i2c_irq(irq, priv, msr);
|
|
|
|
}
|
|
|
|
|
2016-05-14 20:17:08 +08:00
|
|
|
static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev,
|
|
|
|
enum dma_transfer_direction dir,
|
|
|
|
dma_addr_t port_addr)
|
|
|
|
{
|
|
|
|
struct dma_chan *chan;
|
|
|
|
struct dma_slave_config cfg;
|
|
|
|
char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
|
|
|
|
int ret;
|
|
|
|
|
2016-05-19 16:29:17 +08:00
|
|
|
chan = dma_request_chan(dev, chan_name);
|
2016-05-14 20:17:08 +08:00
|
|
|
if (IS_ERR(chan)) {
|
2017-08-16 04:36:23 +08:00
|
|
|
dev_dbg(dev, "request_channel failed for %s (%ld)\n",
|
|
|
|
chan_name, PTR_ERR(chan));
|
2016-05-14 20:17:08 +08:00
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
|
|
cfg.direction = dir;
|
|
|
|
if (dir == DMA_MEM_TO_DEV) {
|
|
|
|
cfg.dst_addr = port_addr;
|
|
|
|
cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
|
|
|
|
} else {
|
|
|
|
cfg.src_addr = port_addr;
|
|
|
|
cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dmaengine_slave_config(chan, &cfg);
|
|
|
|
if (ret) {
|
|
|
|
dev_dbg(dev, "slave_config failed for %s (%d)\n",
|
|
|
|
chan_name, ret);
|
|
|
|
dma_release_channel(chan);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(dev, "got DMA channel for %s\n", chan_name);
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv,
|
|
|
|
struct i2c_msg *msg)
|
|
|
|
{
|
|
|
|
struct device *dev = rcar_i2c_priv_to_dev(priv);
|
|
|
|
bool read;
|
|
|
|
struct dma_chan *chan;
|
|
|
|
enum dma_transfer_direction dir;
|
|
|
|
|
|
|
|
read = msg->flags & I2C_M_RD;
|
|
|
|
|
|
|
|
chan = read ? priv->dma_rx : priv->dma_tx;
|
|
|
|
if (PTR_ERR(chan) != -EPROBE_DEFER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
|
|
|
|
chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX);
|
|
|
|
|
|
|
|
if (read)
|
|
|
|
priv->dma_rx = chan;
|
|
|
|
else
|
|
|
|
priv->dma_tx = chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
|
|
|
if (!IS_ERR(priv->dma_tx)) {
|
|
|
|
dma_release_channel(priv->dma_tx);
|
|
|
|
priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_ERR(priv->dma_rx)) {
|
|
|
|
dma_release_channel(priv->dma_rx);
|
|
|
|
priv->dma_rx = ERR_PTR(-EPROBE_DEFER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 04:45:38 +08:00
|
|
|
/* I2C is a special case, we need to poll the status of a reset */
|
|
|
|
static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv)
|
|
|
|
{
|
2020-08-30 04:38:10 +08:00
|
|
|
int ret;
|
2018-06-29 04:45:38 +08:00
|
|
|
|
|
|
|
ret = reset_control_reset(priv->rstc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-08-30 04:38:10 +08:00
|
|
|
return read_poll_timeout_atomic(reset_control_status, ret, ret == 0, 1,
|
|
|
|
100, false, priv->rstc);
|
2018-06-29 04:45:38 +08:00
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08: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-21 03:03:20 +08:00
|
|
|
int i, ret;
|
2015-11-19 23:56:44 +08:00
|
|
|
long time_left;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2022-05-21 04:29:18 +08:00
|
|
|
priv->flags |= ID_P_NOT_ATOMIC;
|
2021-02-13 00:45:41 +08:00
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
|
2018-11-13 19:15:42 +08:00
|
|
|
/* Check bus state before init otherwise bus busy info will be lost */
|
|
|
|
ret = rcar_i2c_bus_barrier(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2018-06-29 04:45:38 +08:00
|
|
|
/* Gen3 needs a reset before allowing RXDMA once */
|
|
|
|
if (priv->devtype == I2C_RCAR_GEN3) {
|
|
|
|
priv->flags |= ID_P_NO_RXDMA;
|
|
|
|
if (!IS_ERR(priv->rstc)) {
|
|
|
|
ret = rcar_i2c_do_reset(priv);
|
|
|
|
if (ret == 0)
|
|
|
|
priv->flags &= ~ID_P_NO_RXDMA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 02:38:35 +08:00
|
|
|
rcar_i2c_init(priv);
|
|
|
|
|
2018-07-24 04:26:14 +08:00
|
|
|
for (i = 0; i < num; i++)
|
2016-05-14 20:17:08 +08:00
|
|
|
rcar_i2c_request_dma(priv, msgs + i);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2022-05-20 18:33:25 +08:00
|
|
|
rcar_i2c_first_msg(priv, msgs, num);
|
2015-11-19 23:56:46 +08:00
|
|
|
|
2015-12-24 00:56:33 +08:00
|
|
|
time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
|
2015-11-19 23:56:46 +08:00
|
|
|
num * adap->timeout);
|
2018-10-20 03:15:26 +08:00
|
|
|
|
|
|
|
/* cleanup DMA if it couldn't complete properly due to an error */
|
|
|
|
if (priv->dma_direction != DMA_NONE)
|
2021-12-08 16:45:42 +08:00
|
|
|
rcar_i2c_cleanup_dma(priv, true);
|
2018-10-20 03:15:26 +08:00
|
|
|
|
|
|
|
if (!time_left) {
|
2015-11-19 23:56:46 +08:00
|
|
|
rcar_i2c_init(priv);
|
|
|
|
ret = -ETIMEDOUT;
|
2015-12-24 00:56:33 +08:00
|
|
|
} else if (priv->flags & ID_NACK) {
|
2015-11-19 23:56:46 +08:00
|
|
|
ret = -ENXIO;
|
2015-12-24 00:56:33 +08:00
|
|
|
} else if (priv->flags & ID_ARBLOST) {
|
2015-11-19 23:56:46 +08:00
|
|
|
ret = -EAGAIN;
|
2022-04-05 18:07:56 +08:00
|
|
|
} else if (priv->flags & ID_EPROTO) {
|
|
|
|
ret = -EPROTO;
|
2015-11-19 23:56:46 +08:00
|
|
|
} else {
|
|
|
|
ret = num - priv->msgs_left; /* The number of transfer */
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-13 00:45:41 +08:00
|
|
|
static int rcar_i2c_master_xfer_atomic(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);
|
|
|
|
unsigned long j;
|
|
|
|
bool time_left;
|
|
|
|
int ret;
|
|
|
|
|
2022-05-21 04:29:18 +08:00
|
|
|
priv->flags &= ~ID_P_NOT_ATOMIC;
|
2021-02-13 00:45:41 +08:00
|
|
|
|
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
|
|
|
|
/* Check bus state before init otherwise bus busy info will be lost */
|
|
|
|
ret = rcar_i2c_bus_barrier(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
rcar_i2c_init(priv);
|
2022-05-20 18:33:25 +08:00
|
|
|
rcar_i2c_first_msg(priv, msgs, num);
|
2021-02-13 00:45:41 +08:00
|
|
|
|
|
|
|
j = jiffies + num * adap->timeout;
|
|
|
|
do {
|
|
|
|
u32 msr = rcar_i2c_read(priv, ICMSR);
|
|
|
|
|
|
|
|
msr &= (rcar_i2c_is_recv(priv) ? RCAR_IRQ_RECV : RCAR_IRQ_SEND) | RCAR_IRQ_STOP;
|
|
|
|
|
|
|
|
if (msr) {
|
|
|
|
if (priv->devtype < I2C_RCAR_GEN3)
|
|
|
|
rcar_i2c_gen2_irq(0, priv);
|
|
|
|
else
|
|
|
|
rcar_i2c_gen3_irq(0, priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_left = time_before_eq(jiffies, j);
|
|
|
|
} while (!(priv->flags & ID_DONE) && time_left);
|
|
|
|
|
|
|
|
if (!time_left) {
|
|
|
|
rcar_i2c_init(priv);
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
} else if (priv->flags & ID_NACK) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
} else if (priv->flags & ID_ARBLOST) {
|
|
|
|
ret = -EAGAIN;
|
2022-04-05 18:07:56 +08:00
|
|
|
} else if (priv->flags & ID_EPROTO) {
|
|
|
|
ret = -EPROTO;
|
2021-02-13 00:45:41 +08:00
|
|
|
} else {
|
|
|
|
ret = num - priv->msgs_left; /* The number of transfer */
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
|
|
|
|
if (ret < 0 && ret != -ENXIO)
|
|
|
|
dev_err(dev, "error %d : %x\n", ret, priv->flags);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-19 00:04:55 +08: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;
|
|
|
|
|
2017-04-20 18:04:33 +08:00
|
|
|
/* Keep device active for slave address detection logic */
|
2015-12-17 03:05:18 +08:00
|
|
|
pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
|
2014-11-19 00:04:55 +08:00
|
|
|
|
|
|
|
priv->slave = slave;
|
|
|
|
rcar_i2c_write(priv, ICSAR, slave->addr);
|
|
|
|
rcar_i2c_write(priv, ICSSR, 0);
|
2020-06-29 23:38:07 +08:00
|
|
|
rcar_i2c_write(priv, ICSIER, SAR);
|
2014-11-19 00:04:55 +08:00
|
|
|
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);
|
|
|
|
|
2020-07-27 00:16:06 +08:00
|
|
|
/* ensure no irq is running before clearing ptr */
|
|
|
|
disable_irq(priv->irq);
|
2014-11-19 00:04:55 +08:00
|
|
|
rcar_i2c_write(priv, ICSIER, 0);
|
2020-07-27 00:16:06 +08:00
|
|
|
rcar_i2c_write(priv, ICSSR, 0);
|
|
|
|
enable_irq(priv->irq);
|
|
|
|
rcar_i2c_write(priv, ICSCR, SDBS);
|
2020-07-04 21:38:29 +08:00
|
|
|
rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
|
2014-11-19 00:04:55 +08:00
|
|
|
|
|
|
|
priv->slave = NULL;
|
|
|
|
|
2015-12-17 03:05:18 +08:00
|
|
|
pm_runtime_put(rcar_i2c_priv_to_dev(priv));
|
2014-11-19 00:04:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
static u32 rcar_i2c_func(struct i2c_adapter *adap)
|
|
|
|
{
|
2020-09-10 17:11:18 +08:00
|
|
|
struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
|
|
|
|
|
2017-06-20 05:41:46 +08:00
|
|
|
/*
|
|
|
|
* This HW can't do:
|
|
|
|
* I2C_SMBUS_QUICK (setting FSB during START didn't work)
|
|
|
|
* I2C_M_NOSTART (automatically sends address after START)
|
|
|
|
* I2C_M_IGNORE_NAK (automatically sends STOP after NAK)
|
|
|
|
*/
|
2020-09-10 17:11:18 +08:00
|
|
|
u32 func = I2C_FUNC_I2C | I2C_FUNC_SLAVE |
|
2022-04-05 18:07:56 +08:00
|
|
|
(I2C_FUNC_SMBUS_EMUL_ALL & ~I2C_FUNC_SMBUS_QUICK);
|
2020-09-10 17:11:18 +08:00
|
|
|
|
|
|
|
if (priv->flags & ID_P_HOST_NOTIFY)
|
|
|
|
func |= I2C_FUNC_SMBUS_HOST_NOTIFY;
|
|
|
|
|
|
|
|
return func;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_algorithm rcar_i2c_algo = {
|
|
|
|
.master_xfer = rcar_i2c_master_xfer,
|
2021-02-13 00:45:41 +08:00
|
|
|
.master_xfer_atomic = rcar_i2c_master_xfer_atomic,
|
2012-09-28 14:44:25 +08:00
|
|
|
.functionality = rcar_i2c_func,
|
2014-11-19 00:04:55 +08:00
|
|
|
.reg_slave = rcar_reg_slave,
|
|
|
|
.unreg_slave = rcar_unreg_slave,
|
2012-09-28 14:44:25 +08:00
|
|
|
};
|
|
|
|
|
2018-07-24 04:26:14 +08:00
|
|
|
static const struct i2c_adapter_quirks rcar_i2c_quirks = {
|
|
|
|
.flags = I2C_AQ_NO_ZERO_LEN,
|
|
|
|
};
|
|
|
|
|
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-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 },
|
2015-08-05 21:18:25 +08:00
|
|
|
{ .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
|
2016-09-15 00:46:06 +08:00
|
|
|
{ .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
|
2016-12-07 00:01:28 +08:00
|
|
|
{ .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
|
|
|
|
{ .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
|
|
|
|
{ .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
|
2022-02-03 22:33:17 +08:00
|
|
|
{ .compatible = "renesas,rcar-gen4-i2c", .data = (void *)I2C_RCAR_GEN3 },
|
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
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv;
|
|
|
|
struct i2c_adapter *adap;
|
|
|
|
struct device *dev = &pdev->dev;
|
2020-12-24 01:21:53 +08:00
|
|
|
unsigned long irqflags = 0;
|
2020-12-24 01:21:54 +08:00
|
|
|
irqreturn_t (*irqhandler)(int irq, void *ptr) = rcar_i2c_gen3_irq;
|
2019-08-09 03:39:10 +08:00
|
|
|
int ret;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2019-03-06 01:54:32 +08:00
|
|
|
/* Otherwise logic will break because some bytes must always use PIO */
|
|
|
|
BUILD_BUG_ON_MSG(RCAR_MIN_DMA_LEN < 3, "Invalid min DMA length");
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
|
2014-05-13 09:51:58 +08:00
|
|
|
if (!priv)
|
2012-09-28 14:44:25 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:48:27 +08:00
|
|
|
priv->io = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res);
|
2013-01-21 18:09:03 +08:00
|
|
|
if (IS_ERR(priv->io))
|
|
|
|
return PTR_ERR(priv->io);
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2016-03-02 00:36:43 +08:00
|
|
|
priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
|
2012-09-28 14:44:25 +08:00
|
|
|
init_waitqueue_head(&priv->wait);
|
|
|
|
|
2014-07-10 19:46:31 +08: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;
|
2018-01-09 21:58:59 +08:00
|
|
|
adap->bus_recovery_info = &rcar_i2c_bri;
|
2018-07-24 04:26:14 +08:00
|
|
|
adap->quirks = &rcar_i2c_quirks;
|
2012-09-28 14:44:25 +08:00
|
|
|
i2c_set_adapdata(adap, priv);
|
2022-08-11 15:10:30 +08:00
|
|
|
strscpy(adap->name, pdev->name, sizeof(adap->name));
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2016-05-14 20:17:08 +08:00
|
|
|
/* Init DMA */
|
|
|
|
sg_init_table(&priv->sg, 1);
|
|
|
|
priv->dma_direction = DMA_NONE;
|
|
|
|
priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
|
|
|
|
|
2017-04-20 18:04:33 +08:00
|
|
|
/* Activate device for clock calculation */
|
2015-12-08 17:37:47 +08:00
|
|
|
pm_runtime_enable(dev);
|
|
|
|
pm_runtime_get_sync(dev);
|
2020-03-24 20:32:13 +08:00
|
|
|
ret = rcar_i2c_clock_calculate(priv);
|
2022-05-20 17:54:21 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
goto out_pm_disable;
|
|
|
|
}
|
2015-12-08 17:37:47 +08:00
|
|
|
|
2020-07-04 21:38:29 +08:00
|
|
|
rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
|
|
|
|
|
2020-12-24 01:21:54 +08:00
|
|
|
if (priv->devtype < I2C_RCAR_GEN3) {
|
2020-12-24 01:21:53 +08:00
|
|
|
irqflags |= IRQF_NO_THREAD;
|
2020-12-24 01:21:54 +08:00
|
|
|
irqhandler = rcar_i2c_gen2_irq;
|
|
|
|
}
|
2020-12-24 01:21:53 +08:00
|
|
|
|
2018-06-29 04:45:38 +08:00
|
|
|
if (priv->devtype == I2C_RCAR_GEN3) {
|
|
|
|
priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
|
|
|
|
if (!IS_ERR(priv->rstc)) {
|
|
|
|
ret = reset_control_status(priv->rstc);
|
|
|
|
if (ret < 0)
|
|
|
|
priv->rstc = ERR_PTR(-ENOTSUPP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 18:04:33 +08:00
|
|
|
/* Stay always active when multi-master to keep arbitration working */
|
2015-12-24 00:56:34 +08:00
|
|
|
if (of_property_read_bool(dev->of_node, "multi-master"))
|
|
|
|
priv->flags |= ID_P_PM_BLOCKED;
|
|
|
|
else
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
|
2020-09-10 17:11:18 +08:00
|
|
|
if (of_property_read_bool(dev->of_node, "smbus"))
|
|
|
|
priv->flags |= ID_P_HOST_NOTIFY;
|
2015-12-08 17:37:47 +08:00
|
|
|
|
2021-04-11 04:23:33 +08:00
|
|
|
ret = platform_get_irq(pdev, 0);
|
|
|
|
if (ret < 0)
|
2022-05-20 17:54:21 +08:00
|
|
|
goto out_pm_put;
|
2021-04-11 04:23:33 +08:00
|
|
|
priv->irq = ret;
|
2020-12-24 01:21:54 +08:00
|
|
|
ret = devm_request_irq(dev, priv->irq, irqhandler, irqflags, dev_name(dev), priv);
|
2012-09-28 14:44:25 +08:00
|
|
|
if (ret < 0) {
|
2019-08-09 03:39:10 +08:00
|
|
|
dev_err(dev, "cannot get irq %d\n", priv->irq);
|
2022-05-20 17:54:21 +08:00
|
|
|
goto out_pm_put;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:39:25 +08:00
|
|
|
platform_set_drvdata(pdev, priv);
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
ret = i2c_add_numbered_adapter(adap);
|
2016-08-09 19:36:17 +08:00
|
|
|
if (ret < 0)
|
2022-05-20 17:54:21 +08:00
|
|
|
goto out_pm_put;
|
2012-09-28 14:44:25 +08:00
|
|
|
|
2020-09-10 17:11:18 +08:00
|
|
|
if (priv->flags & ID_P_HOST_NOTIFY) {
|
|
|
|
priv->host_notify_client = i2c_new_slave_host_notify_device(adap);
|
|
|
|
if (IS_ERR(priv->host_notify_client)) {
|
|
|
|
ret = PTR_ERR(priv->host_notify_client);
|
|
|
|
goto out_del_device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:44:25 +08:00
|
|
|
dev_info(dev, "probed\n");
|
|
|
|
|
|
|
|
return 0;
|
2015-11-19 23:56:41 +08:00
|
|
|
|
2020-09-10 17:11:18 +08:00
|
|
|
out_del_device:
|
|
|
|
i2c_del_adapter(&priv->adap);
|
2015-11-19 23:56:41 +08:00
|
|
|
out_pm_put:
|
2022-05-20 17:54:21 +08:00
|
|
|
if (priv->flags & ID_P_PM_BLOCKED)
|
|
|
|
pm_runtime_put(dev);
|
2015-11-19 23:56:41 +08:00
|
|
|
out_pm_disable:
|
|
|
|
pm_runtime_disable(dev);
|
|
|
|
return ret;
|
2012-09-28 14:44:25 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-09-10 17:11:18 +08:00
|
|
|
if (priv->host_notify_client)
|
|
|
|
i2c_free_slave_host_notify_device(priv->host_notify_client);
|
2012-09-28 14:44:25 +08:00
|
|
|
i2c_del_adapter(&priv->adap);
|
2016-05-14 20:17:08 +08:00
|
|
|
rcar_i2c_release_dma(priv);
|
2015-12-24 00:56:34 +08:00
|
|
|
if (priv->flags & ID_P_PM_BLOCKED)
|
|
|
|
pm_runtime_put(dev);
|
2012-09-28 14:44:25 +08:00
|
|
|
pm_runtime_disable(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-20 00:48:26 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int rcar_i2c_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
i2c_mark_adapter_suspended(&priv->adap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rcar_i2c_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct rcar_i2c_priv *priv = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
i2c_mark_adapter_resumed(&priv->adap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:03:57 +08:00
|
|
|
static const struct dev_pm_ops rcar_i2c_pm_ops = {
|
|
|
|
SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rcar_i2c_suspend, rcar_i2c_resume)
|
|
|
|
};
|
2018-12-20 00:48:26 +08:00
|
|
|
|
|
|
|
#define DEV_PM_OPS (&rcar_i2c_pm_ops)
|
|
|
|
#else
|
|
|
|
#define DEV_PM_OPS NULL
|
|
|
|
#endif /* CONFIG_PM_SLEEP */
|
|
|
|
|
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",
|
2013-09-12 20:36:46 +08:00
|
|
|
.of_match_table = rcar_i2c_dt_ids,
|
2018-12-20 00:48:26 +08:00
|
|
|
.pm = DEV_PM_OPS,
|
2012-09-28 14:44:25 +08:00
|
|
|
},
|
|
|
|
.probe = rcar_i2c_probe,
|
2012-11-28 04:59:38 +08:00
|
|
|
.remove = rcar_i2c_remove,
|
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
|
|
|
|
2014-05-28 15:44:46 +08:00
|
|
|
MODULE_LICENSE("GPL v2");
|
2012-09-28 14:44:25 +08:00
|
|
|
MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
|
|
|
|
MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
|