2018-05-30 22:35:54 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-02-16 15:39:16 +03:00
|
|
|
/*
|
|
|
|
* Freescale MXS I2C bus driver
|
|
|
|
*
|
2013-10-06 16:02:13 +04:00
|
|
|
* Copyright (C) 2012-2013 Marek Vasut <marex@denx.de>
|
2012-10-12 14:55:16 +04:00
|
|
|
* Copyright (C) 2011-2012 Wolfram Sang, Pengutronix e.K.
|
2011-02-16 15:39:16 +03:00
|
|
|
*
|
|
|
|
* based on a (non-working) driver which was:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/completion.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/io.h>
|
2011-08-31 22:37:50 +04:00
|
|
|
#include <linux/stmp_device.h>
|
2012-05-12 09:43:32 +04:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2012-08-24 07:44:31 +04:00
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <linux/dmaengine.h>
|
2011-02-16 15:39:16 +03:00
|
|
|
|
|
|
|
#define DRIVER_NAME "mxs-i2c"
|
|
|
|
|
|
|
|
#define MXS_I2C_CTRL0 (0x00)
|
|
|
|
#define MXS_I2C_CTRL0_SET (0x04)
|
2013-09-30 03:23:55 +04:00
|
|
|
#define MXS_I2C_CTRL0_CLR (0x08)
|
2011-02-16 15:39:16 +03:00
|
|
|
|
|
|
|
#define MXS_I2C_CTRL0_SFTRST 0x80000000
|
2013-01-24 16:56:21 +04:00
|
|
|
#define MXS_I2C_CTRL0_RUN 0x20000000
|
2011-02-16 15:39:16 +03:00
|
|
|
#define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
|
2013-09-30 03:23:55 +04:00
|
|
|
#define MXS_I2C_CTRL0_PIO_MODE 0x01000000
|
2011-02-16 15:39:16 +03:00
|
|
|
#define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
|
|
|
|
#define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
|
|
|
|
#define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
|
|
|
|
#define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
|
|
|
|
#define MXS_I2C_CTRL0_DIRECTION 0x00010000
|
|
|
|
#define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
|
|
|
|
|
2012-07-09 20:22:53 +04:00
|
|
|
#define MXS_I2C_TIMING0 (0x10)
|
|
|
|
#define MXS_I2C_TIMING1 (0x20)
|
|
|
|
#define MXS_I2C_TIMING2 (0x30)
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
#define MXS_I2C_CTRL1 (0x40)
|
|
|
|
#define MXS_I2C_CTRL1_SET (0x44)
|
|
|
|
#define MXS_I2C_CTRL1_CLR (0x48)
|
|
|
|
|
2013-04-15 04:16:55 +04:00
|
|
|
#define MXS_I2C_CTRL1_CLR_GOT_A_NAK 0x10000000
|
2011-02-16 15:39:16 +03:00
|
|
|
#define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
|
|
|
|
#define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
|
|
|
|
#define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
|
|
|
|
#define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
|
|
|
|
#define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
|
|
|
|
#define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
|
|
|
|
#define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
|
|
|
|
#define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
|
|
|
|
|
2013-04-15 04:16:54 +04:00
|
|
|
#define MXS_I2C_STAT (0x50)
|
2013-10-06 16:02:13 +04:00
|
|
|
#define MXS_I2C_STAT_GOT_A_NAK 0x10000000
|
2013-04-15 04:16:54 +04:00
|
|
|
#define MXS_I2C_STAT_BUS_BUSY 0x00000800
|
|
|
|
#define MXS_I2C_STAT_CLK_GEN_BUSY 0x00000400
|
|
|
|
|
2013-09-30 03:23:55 +04:00
|
|
|
#define MXS_I2C_DATA(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x60 : 0xa0)
|
2013-01-24 16:56:21 +04:00
|
|
|
|
2013-09-30 03:23:55 +04:00
|
|
|
#define MXS_I2C_DEBUG0_CLR(i2c) ((i2c->dev_type == MXS_I2C_V1) ? 0x78 : 0xb8)
|
2013-01-24 16:56:21 +04:00
|
|
|
|
|
|
|
#define MXS_I2C_DEBUG0_DMAREQ 0x80000000
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
#define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
|
|
|
|
MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
|
|
|
|
MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
|
|
|
|
MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
|
|
|
|
MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
|
|
|
|
MXS_I2C_CTRL1_SLAVE_IRQ)
|
|
|
|
|
|
|
|
|
|
|
|
#define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
|
|
|
|
MXS_I2C_CTRL0_PRE_SEND_START | \
|
|
|
|
MXS_I2C_CTRL0_MASTER_MODE | \
|
|
|
|
MXS_I2C_CTRL0_DIRECTION | \
|
|
|
|
MXS_I2C_CTRL0_XFER_COUNT(1))
|
|
|
|
|
|
|
|
#define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
|
|
|
|
MXS_I2C_CTRL0_MASTER_MODE | \
|
|
|
|
MXS_I2C_CTRL0_DIRECTION)
|
|
|
|
|
|
|
|
#define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
|
|
|
|
MXS_I2C_CTRL0_MASTER_MODE)
|
|
|
|
|
2013-09-30 03:23:53 +04:00
|
|
|
enum mxs_i2c_devtype {
|
|
|
|
MXS_I2C_UNKNOWN = 0,
|
|
|
|
MXS_I2C_V1,
|
|
|
|
MXS_I2C_V2,
|
|
|
|
};
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
/**
|
|
|
|
* struct mxs_i2c_dev - per device, private MXS-I2C data
|
|
|
|
*
|
|
|
|
* @dev: driver model device node
|
2013-09-30 03:23:53 +04:00
|
|
|
* @dev_type: distinguish i.MX23/i.MX28 features
|
2011-02-16 15:39:16 +03:00
|
|
|
* @regs: IO registers pointer
|
|
|
|
* @cmd_complete: completion object for transaction wait
|
|
|
|
* @cmd_err: error code for last transaction
|
|
|
|
* @adapter: i2c subsystem adapter node
|
|
|
|
*/
|
|
|
|
struct mxs_i2c_dev {
|
|
|
|
struct device *dev;
|
2013-09-30 03:23:53 +04:00
|
|
|
enum mxs_i2c_devtype dev_type;
|
2011-02-16 15:39:16 +03:00
|
|
|
void __iomem *regs;
|
|
|
|
struct completion cmd_complete;
|
2013-01-08 04:32:06 +04:00
|
|
|
int cmd_err;
|
2011-02-16 15:39:16 +03:00
|
|
|
struct i2c_adapter adapter;
|
2012-11-30 21:48:35 +04:00
|
|
|
|
|
|
|
uint32_t timing0;
|
|
|
|
uint32_t timing1;
|
2013-07-05 20:28:00 +04:00
|
|
|
uint32_t timing2;
|
2012-08-24 07:44:31 +04:00
|
|
|
|
|
|
|
/* DMA support components */
|
2013-07-05 20:28:00 +04:00
|
|
|
struct dma_chan *dmach;
|
2012-08-24 07:44:31 +04:00
|
|
|
uint32_t pio_data[2];
|
|
|
|
uint32_t addr_data;
|
|
|
|
struct scatterlist sg_io[2];
|
|
|
|
bool dma_read;
|
2011-02-16 15:39:16 +03:00
|
|
|
};
|
|
|
|
|
2013-07-11 05:19:28 +04:00
|
|
|
static int mxs_i2c_reset(struct mxs_i2c_dev *i2c)
|
2011-02-16 15:39:16 +03:00
|
|
|
{
|
2013-07-11 05:19:28 +04:00
|
|
|
int ret = stmp_reset_block(i2c->regs);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-07-09 20:22:53 +04:00
|
|
|
|
2012-11-30 21:48:35 +04:00
|
|
|
/*
|
|
|
|
* Configure timing for the I2C block. The I2C TIMING2 register has to
|
|
|
|
* be programmed with this particular magic number. The rest is derived
|
|
|
|
* from the XTAL speed and requested I2C speed.
|
|
|
|
*
|
|
|
|
* For details, see i.MX233 [25.4.2 - 25.4.4] and i.MX28 [27.5.2 - 27.5.4].
|
|
|
|
*/
|
|
|
|
writel(i2c->timing0, i2c->regs + MXS_I2C_TIMING0);
|
|
|
|
writel(i2c->timing1, i2c->regs + MXS_I2C_TIMING1);
|
2013-07-05 20:28:00 +04:00
|
|
|
writel(i2c->timing2, i2c->regs + MXS_I2C_TIMING2);
|
2012-07-09 20:22:53 +04:00
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
|
2013-07-11 05:19:28 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-02-16 15:39:16 +03:00
|
|
|
}
|
|
|
|
|
2012-08-24 07:44:31 +04:00
|
|
|
static void mxs_i2c_dma_finish(struct mxs_i2c_dev *i2c)
|
|
|
|
{
|
|
|
|
if (i2c->dma_read) {
|
|
|
|
dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
|
|
|
|
dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
|
|
|
|
} else {
|
|
|
|
dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mxs_i2c_dma_irq_callback(void *param)
|
|
|
|
{
|
|
|
|
struct mxs_i2c_dev *i2c = param;
|
|
|
|
|
|
|
|
complete(&i2c->cmd_complete);
|
|
|
|
mxs_i2c_dma_finish(i2c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mxs_i2c_dma_setup_xfer(struct i2c_adapter *adap,
|
|
|
|
struct i2c_msg *msg, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct dma_async_tx_descriptor *desc;
|
|
|
|
struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
|
|
|
|
|
2018-05-16 10:16:47 +03:00
|
|
|
i2c->addr_data = i2c_8bit_addr_from_msg(msg);
|
|
|
|
|
2012-08-24 07:44:31 +04:00
|
|
|
if (msg->flags & I2C_M_RD) {
|
2018-01-23 20:38:27 +03:00
|
|
|
i2c->dma_read = true;
|
2012-08-24 07:44:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SELECT command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Queue the PIO register write transfer. */
|
|
|
|
i2c->pio_data[0] = MXS_CMD_I2C_SELECT;
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach,
|
|
|
|
(struct scatterlist *)&i2c->pio_data[0],
|
|
|
|
1, DMA_TRANS_NONE, 0);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get PIO reg. write descriptor.\n");
|
|
|
|
goto select_init_pio_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queue the DMA data transfer. */
|
|
|
|
sg_init_one(&i2c->sg_io[0], &i2c->addr_data, 1);
|
|
|
|
dma_map_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[0], 1,
|
|
|
|
DMA_MEM_TO_DEV,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get DMA data write descriptor.\n");
|
|
|
|
goto select_init_dma_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* READ command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Queue the PIO register write transfer. */
|
|
|
|
i2c->pio_data[1] = flags | MXS_CMD_I2C_READ |
|
|
|
|
MXS_I2C_CTRL0_XFER_COUNT(msg->len);
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach,
|
|
|
|
(struct scatterlist *)&i2c->pio_data[1],
|
|
|
|
1, DMA_TRANS_NONE, DMA_PREP_INTERRUPT);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get PIO reg. write descriptor.\n");
|
|
|
|
goto select_init_dma_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queue the DMA data transfer. */
|
|
|
|
sg_init_one(&i2c->sg_io[1], msg->buf, msg->len);
|
|
|
|
dma_map_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach, &i2c->sg_io[1], 1,
|
|
|
|
DMA_DEV_TO_MEM,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get DMA data write descriptor.\n");
|
|
|
|
goto read_init_dma_fail;
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-23 20:38:27 +03:00
|
|
|
i2c->dma_read = false;
|
2012-08-24 07:44:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* WRITE command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Queue the PIO register write transfer. */
|
|
|
|
i2c->pio_data[0] = flags | MXS_CMD_I2C_WRITE |
|
|
|
|
MXS_I2C_CTRL0_XFER_COUNT(msg->len + 1);
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach,
|
|
|
|
(struct scatterlist *)&i2c->pio_data[0],
|
|
|
|
1, DMA_TRANS_NONE, 0);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get PIO reg. write descriptor.\n");
|
|
|
|
goto write_init_pio_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queue the DMA data transfer. */
|
|
|
|
sg_init_table(i2c->sg_io, 2);
|
|
|
|
sg_set_buf(&i2c->sg_io[0], &i2c->addr_data, 1);
|
|
|
|
sg_set_buf(&i2c->sg_io[1], msg->buf, msg->len);
|
|
|
|
dma_map_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
|
|
|
|
desc = dmaengine_prep_slave_sg(i2c->dmach, i2c->sg_io, 2,
|
|
|
|
DMA_MEM_TO_DEV,
|
|
|
|
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
|
|
|
|
if (!desc) {
|
|
|
|
dev_err(i2c->dev,
|
|
|
|
"Failed to get DMA data write descriptor.\n");
|
|
|
|
goto write_init_dma_fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last descriptor must have this callback,
|
|
|
|
* to finish the DMA transaction.
|
|
|
|
*/
|
|
|
|
desc->callback = mxs_i2c_dma_irq_callback;
|
|
|
|
desc->callback_param = i2c;
|
|
|
|
|
|
|
|
/* Start the transfer. */
|
|
|
|
dmaengine_submit(desc);
|
|
|
|
dma_async_issue_pending(i2c->dmach);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Read failpath. */
|
|
|
|
read_init_dma_fail:
|
|
|
|
dma_unmap_sg(i2c->dev, &i2c->sg_io[1], 1, DMA_FROM_DEVICE);
|
|
|
|
select_init_dma_fail:
|
|
|
|
dma_unmap_sg(i2c->dev, &i2c->sg_io[0], 1, DMA_TO_DEVICE);
|
|
|
|
select_init_pio_fail:
|
2012-11-18 09:25:07 +04:00
|
|
|
dmaengine_terminate_all(i2c->dmach);
|
2012-08-24 07:44:31 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Write failpath. */
|
|
|
|
write_init_dma_fail:
|
|
|
|
dma_unmap_sg(i2c->dev, i2c->sg_io, 2, DMA_TO_DEVICE);
|
|
|
|
write_init_pio_fail:
|
2012-11-18 09:25:07 +04:00
|
|
|
dmaengine_terminate_all(i2c->dmach);
|
2012-08-24 07:44:31 +04:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
static int mxs_i2c_pio_wait_xfer_end(struct mxs_i2c_dev *i2c)
|
2013-01-24 16:56:21 +04:00
|
|
|
{
|
|
|
|
unsigned long timeout = jiffies + msecs_to_jiffies(1000);
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
while (readl(i2c->regs + MXS_I2C_CTRL0) & MXS_I2C_CTRL0_RUN) {
|
2014-09-23 14:48:26 +04:00
|
|
|
if (readl(i2c->regs + MXS_I2C_CTRL1) &
|
|
|
|
MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
|
|
|
|
return -ENXIO;
|
2013-04-15 04:16:54 +04:00
|
|
|
if (time_after(jiffies, timeout))
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:16:55 +04:00
|
|
|
static int mxs_i2c_pio_check_error_state(struct mxs_i2c_dev *i2c)
|
|
|
|
{
|
|
|
|
u32 state;
|
|
|
|
|
|
|
|
state = readl(i2c->regs + MXS_I2C_CTRL1_CLR) & MXS_I2C_IRQ_MASK;
|
|
|
|
|
|
|
|
if (state & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
|
|
|
|
i2c->cmd_err = -ENXIO;
|
|
|
|
else if (state & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
|
|
|
|
MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
|
|
|
|
MXS_I2C_CTRL1_SLAVE_STOP_IRQ |
|
|
|
|
MXS_I2C_CTRL1_SLAVE_IRQ))
|
|
|
|
i2c->cmd_err = -EIO;
|
|
|
|
|
|
|
|
return i2c->cmd_err;
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:16:54 +04:00
|
|
|
static void mxs_i2c_pio_trigger_cmd(struct mxs_i2c_dev *i2c, u32 cmd)
|
|
|
|
{
|
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
writel(cmd, i2c->regs + MXS_I2C_CTRL0);
|
|
|
|
|
|
|
|
/* readback makes sure the write is latched into hardware */
|
|
|
|
reg = readl(i2c->regs + MXS_I2C_CTRL0);
|
|
|
|
reg |= MXS_I2C_CTRL0_RUN;
|
|
|
|
writel(reg, i2c->regs + MXS_I2C_CTRL0);
|
|
|
|
}
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
/*
|
|
|
|
* Start WRITE transaction on the I2C bus. By studying i.MX23 datasheet,
|
|
|
|
* CTRL0::PIO_MODE bit description clarifies the order in which the registers
|
|
|
|
* must be written during PIO mode operation. First, the CTRL0 register has
|
|
|
|
* to be programmed with all the necessary bits but the RUN bit. Then the
|
|
|
|
* payload has to be written into the DATA register. Finally, the transmission
|
|
|
|
* is executed by setting the RUN bit in CTRL0.
|
|
|
|
*/
|
|
|
|
static void mxs_i2c_pio_trigger_write_cmd(struct mxs_i2c_dev *i2c, u32 cmd,
|
|
|
|
u32 data)
|
|
|
|
{
|
|
|
|
writel(cmd, i2c->regs + MXS_I2C_CTRL0);
|
2013-09-30 03:23:55 +04:00
|
|
|
|
|
|
|
if (i2c->dev_type == MXS_I2C_V1)
|
|
|
|
writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_SET);
|
|
|
|
|
|
|
|
writel(data, i2c->regs + MXS_I2C_DATA(i2c));
|
2013-10-06 16:02:13 +04:00
|
|
|
writel(MXS_I2C_CTRL0_RUN, i2c->regs + MXS_I2C_CTRL0_SET);
|
|
|
|
}
|
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
static int mxs_i2c_pio_setup_xfer(struct i2c_adapter *adap,
|
|
|
|
struct i2c_msg *msg, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
|
2018-05-16 10:16:47 +03:00
|
|
|
uint32_t addr_data = i2c_8bit_addr_from_msg(msg);
|
2013-01-24 16:56:21 +04:00
|
|
|
uint32_t data = 0;
|
2013-10-06 16:02:13 +04:00
|
|
|
int i, ret, xlen = 0, xmit = 0;
|
|
|
|
uint32_t start;
|
2013-01-24 16:56:21 +04:00
|
|
|
|
|
|
|
/* Mute IRQs coming from this block. */
|
|
|
|
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_CLR);
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
/*
|
|
|
|
* MX23 idea:
|
|
|
|
* - Enable CTRL0::PIO_MODE (1 << 24)
|
|
|
|
* - Enable CTRL1::ACK_MODE (1 << 27)
|
|
|
|
*
|
|
|
|
* WARNING! The MX23 is broken in some way, even if it claims
|
|
|
|
* to support PIO, when we try to transfer any amount of data
|
|
|
|
* that is not aligned to 4 bytes, the DMA engine will have
|
|
|
|
* bits in DEBUG1::DMA_BYTES_ENABLES still set even after the
|
|
|
|
* transfer. This in turn will mess up the next transfer as
|
|
|
|
* the block it emit one byte write onto the bus terminated
|
|
|
|
* with a NAK+STOP. A possible workaround is to reset the IP
|
|
|
|
* block after every PIO transmission, which might just work.
|
|
|
|
*
|
|
|
|
* NOTE: The CTRL0::PIO_MODE description is important, since
|
|
|
|
* it outlines how the PIO mode is really supposed to work.
|
|
|
|
*/
|
2013-01-24 16:56:21 +04:00
|
|
|
if (msg->flags & I2C_M_RD) {
|
2013-10-06 16:02:13 +04:00
|
|
|
/*
|
|
|
|
* PIO READ transfer:
|
|
|
|
*
|
|
|
|
* This transfer MUST be limited to 4 bytes maximum. It is not
|
|
|
|
* possible to transfer more than four bytes via PIO, since we
|
|
|
|
* can not in any way make sure we can read the data from the
|
|
|
|
* DATA register fast enough. Besides, the RX FIFO is only four
|
|
|
|
* bytes deep, thus we can only really read up to four bytes at
|
|
|
|
* time. Finally, there is no bit indicating us that new data
|
|
|
|
* arrived at the FIFO and can thus be fetched from the DATA
|
|
|
|
* register.
|
|
|
|
*/
|
|
|
|
BUG_ON(msg->len > 4);
|
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
/* SELECT command. */
|
2013-10-06 16:02:13 +04:00
|
|
|
mxs_i2c_pio_trigger_write_cmd(i2c, MXS_CMD_I2C_SELECT,
|
|
|
|
addr_data);
|
2013-01-24 16:56:21 +04:00
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
ret = mxs_i2c_pio_wait_xfer_end(i2c);
|
|
|
|
if (ret) {
|
2017-05-31 12:40:03 +03:00
|
|
|
dev_dbg(i2c->dev,
|
2013-10-06 16:02:13 +04:00
|
|
|
"PIO: Failed to send SELECT command!\n");
|
2013-04-15 04:16:55 +04:00
|
|
|
goto cleanup;
|
2013-10-06 16:02:13 +04:00
|
|
|
}
|
2013-04-15 04:16:55 +04:00
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
/* READ command. */
|
2013-04-15 04:16:54 +04:00
|
|
|
mxs_i2c_pio_trigger_cmd(i2c,
|
|
|
|
MXS_CMD_I2C_READ | flags |
|
|
|
|
MXS_I2C_CTRL0_XFER_COUNT(msg->len));
|
2013-01-24 16:56:21 +04:00
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
ret = mxs_i2c_pio_wait_xfer_end(i2c);
|
|
|
|
if (ret) {
|
2017-05-31 12:40:03 +03:00
|
|
|
dev_dbg(i2c->dev,
|
2014-09-09 20:19:30 +04:00
|
|
|
"PIO: Failed to send READ command!\n");
|
2013-10-06 16:02:13 +04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:23:55 +04:00
|
|
|
data = readl(i2c->regs + MXS_I2C_DATA(i2c));
|
2013-01-24 16:56:21 +04:00
|
|
|
for (i = 0; i < msg->len; i++) {
|
|
|
|
msg->buf[i] = data & 0xff;
|
|
|
|
data >>= 8;
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-06 16:02:13 +04:00
|
|
|
/*
|
|
|
|
* PIO WRITE transfer:
|
|
|
|
*
|
|
|
|
* The code below implements clock stretching to circumvent
|
|
|
|
* the possibility of kernel not being able to supply data
|
|
|
|
* fast enough. It is possible to transfer arbitrary amount
|
|
|
|
* of data using PIO write.
|
|
|
|
*/
|
2013-01-24 16:56:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The LSB of data buffer is the first byte blasted across
|
|
|
|
* the bus. Higher order bytes follow. Thus the following
|
|
|
|
* filling schematic.
|
|
|
|
*/
|
2013-10-06 16:02:13 +04:00
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
data = addr_data << 24;
|
2013-10-06 16:02:13 +04:00
|
|
|
|
|
|
|
/* Start the transfer with START condition. */
|
|
|
|
start = MXS_I2C_CTRL0_PRE_SEND_START;
|
|
|
|
|
|
|
|
/* If the transfer is long, use clock stretching. */
|
|
|
|
if (msg->len > 3)
|
|
|
|
start |= MXS_I2C_CTRL0_RETAIN_CLOCK;
|
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
for (i = 0; i < msg->len; i++) {
|
|
|
|
data >>= 8;
|
|
|
|
data |= (msg->buf[i] << 24);
|
2013-10-06 16:02:13 +04:00
|
|
|
|
|
|
|
xmit = 0;
|
|
|
|
|
|
|
|
/* This is the last transfer of the message. */
|
|
|
|
if (i + 1 == msg->len) {
|
|
|
|
/* Add optional STOP flag. */
|
|
|
|
start |= flags;
|
|
|
|
/* Remove RETAIN_CLOCK bit. */
|
|
|
|
start &= ~MXS_I2C_CTRL0_RETAIN_CLOCK;
|
|
|
|
xmit = 1;
|
2013-01-24 16:56:21 +04:00
|
|
|
}
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
/* Four bytes are ready in the "data" variable. */
|
|
|
|
if ((i & 3) == 2)
|
|
|
|
xmit = 1;
|
|
|
|
|
|
|
|
/* Nothing interesting happened, continue stuffing. */
|
|
|
|
if (!xmit)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the size of the transfer and shift the
|
|
|
|
* data accordingly.
|
|
|
|
*
|
|
|
|
* i = (4k + 0) .... xlen = 2
|
|
|
|
* i = (4k + 1) .... xlen = 3
|
|
|
|
* i = (4k + 2) .... xlen = 4
|
|
|
|
* i = (4k + 3) .... xlen = 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((i % 4) == 3)
|
|
|
|
xlen = 1;
|
|
|
|
else
|
|
|
|
xlen = (i % 4) + 2;
|
|
|
|
|
|
|
|
data >>= (4 - xlen) * 8;
|
|
|
|
|
|
|
|
dev_dbg(i2c->dev,
|
|
|
|
"PIO: len=%i pos=%i total=%i [W%s%s%s]\n",
|
|
|
|
xlen, i, msg->len,
|
|
|
|
start & MXS_I2C_CTRL0_PRE_SEND_START ? "S" : "",
|
|
|
|
start & MXS_I2C_CTRL0_POST_SEND_STOP ? "E" : "",
|
|
|
|
start & MXS_I2C_CTRL0_RETAIN_CLOCK ? "C" : "");
|
|
|
|
|
2013-04-15 04:16:54 +04:00
|
|
|
writel(MXS_I2C_DEBUG0_DMAREQ,
|
2013-09-30 03:23:55 +04:00
|
|
|
i2c->regs + MXS_I2C_DEBUG0_CLR(i2c));
|
2013-10-06 16:02:13 +04:00
|
|
|
|
|
|
|
mxs_i2c_pio_trigger_write_cmd(i2c,
|
|
|
|
start | MXS_I2C_CTRL0_MASTER_MODE |
|
|
|
|
MXS_I2C_CTRL0_DIRECTION |
|
|
|
|
MXS_I2C_CTRL0_XFER_COUNT(xlen), data);
|
|
|
|
|
|
|
|
/* The START condition is sent only once. */
|
|
|
|
start &= ~MXS_I2C_CTRL0_PRE_SEND_START;
|
|
|
|
|
|
|
|
/* Wait for the end of the transfer. */
|
|
|
|
ret = mxs_i2c_pio_wait_xfer_end(i2c);
|
|
|
|
if (ret) {
|
2017-05-31 12:40:03 +03:00
|
|
|
dev_dbg(i2c->dev,
|
2013-10-06 16:02:13 +04:00
|
|
|
"PIO: Failed to finish WRITE cmd!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check NAK here. */
|
|
|
|
ret = readl(i2c->regs + MXS_I2C_STAT) &
|
|
|
|
MXS_I2C_STAT_GOT_A_NAK;
|
|
|
|
if (ret) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-01-24 16:56:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:16:55 +04:00
|
|
|
/* make sure we capture any occurred error into cmd_err */
|
2013-10-06 16:02:13 +04:00
|
|
|
ret = mxs_i2c_pio_check_error_state(i2c);
|
2013-04-15 04:16:55 +04:00
|
|
|
|
|
|
|
cleanup:
|
2013-01-24 16:56:21 +04:00
|
|
|
/* Clear any dangling IRQs and re-enable interrupts. */
|
|
|
|
writel(MXS_I2C_IRQ_MASK, i2c->regs + MXS_I2C_CTRL1_CLR);
|
|
|
|
writel(MXS_I2C_IRQ_MASK << 8, i2c->regs + MXS_I2C_CTRL1_SET);
|
|
|
|
|
2013-09-30 03:23:55 +04:00
|
|
|
/* Clear the PIO_MODE on i.MX23 */
|
|
|
|
if (i2c->dev_type == MXS_I2C_V1)
|
|
|
|
writel(MXS_I2C_CTRL0_PIO_MODE, i2c->regs + MXS_I2C_CTRL0_CLR);
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
return ret;
|
2013-01-24 16:56:21 +04:00
|
|
|
}
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
/*
|
|
|
|
* Low level master read/write transaction.
|
|
|
|
*/
|
|
|
|
static int mxs_i2c_xfer_msg(struct i2c_adapter *adap, struct i2c_msg *msg,
|
|
|
|
int stop)
|
|
|
|
{
|
|
|
|
struct mxs_i2c_dev *i2c = i2c_get_adapdata(adap);
|
2013-10-06 16:02:13 +04:00
|
|
|
int ret;
|
2011-02-16 15:39:16 +03:00
|
|
|
int flags;
|
2013-10-06 16:02:13 +04:00
|
|
|
int use_pio = 0;
|
2015-03-10 21:08:13 +03:00
|
|
|
unsigned long time_left;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2012-08-24 07:44:31 +04:00
|
|
|
flags = stop ? MXS_I2C_CTRL0_POST_SEND_STOP : 0;
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
dev_dbg(i2c->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
|
|
|
|
msg->addr, msg->len, msg->flags, stop);
|
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
/*
|
2013-10-06 16:02:13 +04:00
|
|
|
* The MX28 I2C IP block can only do PIO READ for transfer of to up
|
|
|
|
* 4 bytes of length. The write transfer is not limited as it can use
|
|
|
|
* clock stretching to avoid FIFO underruns.
|
2013-01-24 16:56:21 +04:00
|
|
|
*/
|
2013-10-06 16:02:13 +04:00
|
|
|
if ((msg->flags & I2C_M_RD) && (msg->len <= 4))
|
|
|
|
use_pio = 1;
|
|
|
|
if (!(msg->flags & I2C_M_RD) && (msg->len < 7))
|
|
|
|
use_pio = 1;
|
|
|
|
|
2013-04-15 04:16:55 +04:00
|
|
|
i2c->cmd_err = 0;
|
2013-10-06 16:02:13 +04:00
|
|
|
if (use_pio) {
|
2013-01-24 16:56:21 +04:00
|
|
|
ret = mxs_i2c_pio_setup_xfer(adap, msg, flags);
|
2013-10-06 16:02:13 +04:00
|
|
|
/* No need to reset the block if NAK was received. */
|
|
|
|
if (ret && (ret != -ENXIO))
|
|
|
|
mxs_i2c_reset(i2c);
|
2013-01-24 16:56:21 +04:00
|
|
|
} else {
|
2013-11-15 02:32:02 +04:00
|
|
|
reinit_completion(&i2c->cmd_complete);
|
2013-01-24 16:56:21 +04:00
|
|
|
ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2015-03-10 21:08:13 +03:00
|
|
|
time_left = wait_for_completion_timeout(&i2c->cmd_complete,
|
2011-02-16 15:39:16 +03:00
|
|
|
msecs_to_jiffies(1000));
|
2015-03-10 21:08:13 +03:00
|
|
|
if (!time_left)
|
2013-01-24 16:56:21 +04:00
|
|
|
goto timeout;
|
2013-10-06 16:02:13 +04:00
|
|
|
|
|
|
|
ret = i2c->cmd_err;
|
2013-04-15 04:16:55 +04:00
|
|
|
}
|
2013-01-24 16:56:21 +04:00
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
if (ret == -ENXIO) {
|
2013-04-15 04:16:55 +04:00
|
|
|
/*
|
|
|
|
* If the transfer fails with a NAK from the slave the
|
|
|
|
* controller halts until it gets told to return to idle state.
|
|
|
|
*/
|
|
|
|
writel(MXS_I2C_CTRL1_CLR_GOT_A_NAK,
|
|
|
|
i2c->regs + MXS_I2C_CTRL1_SET);
|
2013-01-24 16:56:21 +04:00
|
|
|
}
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2013-09-30 03:23:55 +04:00
|
|
|
/*
|
|
|
|
* WARNING!
|
|
|
|
* The i.MX23 is strange. After each and every operation, it's I2C IP
|
|
|
|
* block must be reset, otherwise the IP block will misbehave. This can
|
|
|
|
* be observed on the bus by the block sending out one single byte onto
|
|
|
|
* the bus. In case such an error happens, bit 27 will be set in the
|
|
|
|
* DEBUG0 register. This bit is not documented in the i.MX23 datasheet
|
|
|
|
* and is marked as "TBD" instead. To reset this bit to a correct state,
|
|
|
|
* reset the whole block. Since the block reset does not take long, do
|
|
|
|
* reset the block after every transfer to play safe.
|
|
|
|
*/
|
|
|
|
if (i2c->dev_type == MXS_I2C_V1)
|
|
|
|
mxs_i2c_reset(i2c);
|
2013-04-15 04:16:55 +04:00
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
dev_dbg(i2c->dev, "Done with err=%d\n", ret);
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2013-01-24 16:56:21 +04:00
|
|
|
return ret;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
|
|
|
timeout:
|
|
|
|
dev_dbg(i2c->dev, "Timeout!\n");
|
2012-10-12 14:55:16 +04:00
|
|
|
mxs_i2c_dma_finish(i2c);
|
2013-07-11 05:19:28 +04:00
|
|
|
ret = mxs_i2c_reset(i2c);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mxs_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
|
|
|
|
int num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 mxs_i2c_func(struct i2c_adapter *adap)
|
|
|
|
{
|
2012-11-18 09:25:08 +04:00
|
|
|
return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
|
2011-02-16 15:39:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t mxs_i2c_isr(int this_irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct mxs_i2c_dev *i2c = dev_id;
|
|
|
|
u32 stat = readl(i2c->regs + MXS_I2C_CTRL1) & MXS_I2C_IRQ_MASK;
|
|
|
|
|
|
|
|
if (!stat)
|
|
|
|
return IRQ_NONE;
|
|
|
|
|
|
|
|
if (stat & MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ)
|
|
|
|
i2c->cmd_err = -ENXIO;
|
|
|
|
else if (stat & (MXS_I2C_CTRL1_EARLY_TERM_IRQ |
|
|
|
|
MXS_I2C_CTRL1_MASTER_LOSS_IRQ |
|
|
|
|
MXS_I2C_CTRL1_SLAVE_STOP_IRQ | MXS_I2C_CTRL1_SLAVE_IRQ))
|
|
|
|
/* MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ is only for slaves */
|
|
|
|
i2c->cmd_err = -EIO;
|
|
|
|
|
|
|
|
writel(stat, i2c->regs + MXS_I2C_CTRL1_CLR);
|
2012-01-13 15:14:26 +04:00
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_algorithm mxs_i2c_algo = {
|
|
|
|
.master_xfer = mxs_i2c_xfer,
|
|
|
|
.functionality = mxs_i2c_func,
|
|
|
|
};
|
|
|
|
|
2018-07-23 23:26:07 +03:00
|
|
|
static const struct i2c_adapter_quirks mxs_i2c_quirks = {
|
|
|
|
.flags = I2C_AQ_NO_ZERO_LEN,
|
|
|
|
};
|
|
|
|
|
2013-07-05 20:28:00 +04:00
|
|
|
static void mxs_i2c_derive_timing(struct mxs_i2c_dev *i2c, uint32_t speed)
|
2012-11-30 21:48:35 +04:00
|
|
|
{
|
2013-07-05 20:28:00 +04:00
|
|
|
/* The I2C block clock runs at 24MHz */
|
2012-11-30 21:48:35 +04:00
|
|
|
const uint32_t clk = 24000000;
|
2013-07-05 20:28:00 +04:00
|
|
|
uint32_t divider;
|
2012-11-30 21:48:35 +04:00
|
|
|
uint16_t high_count, low_count, rcv_count, xmit_count;
|
2013-07-05 20:28:00 +04:00
|
|
|
uint32_t bus_free, leadin;
|
2012-11-30 21:48:35 +04:00
|
|
|
struct device *dev = i2c->dev;
|
|
|
|
|
2013-07-05 20:28:00 +04:00
|
|
|
divider = DIV_ROUND_UP(clk, speed);
|
|
|
|
|
|
|
|
if (divider < 25) {
|
|
|
|
/*
|
|
|
|
* limit the divider, so that min(low_count, high_count)
|
|
|
|
* is >= 1
|
|
|
|
*/
|
|
|
|
divider = 25;
|
|
|
|
dev_warn(dev,
|
|
|
|
"Speed too high (%u.%03u kHz), using %u.%03u kHz\n",
|
|
|
|
speed / 1000, speed % 1000,
|
|
|
|
clk / divider / 1000, clk / divider % 1000);
|
|
|
|
} else if (divider > 1897) {
|
|
|
|
/*
|
|
|
|
* limit the divider, so that max(low_count, high_count)
|
|
|
|
* cannot exceed 1023
|
|
|
|
*/
|
|
|
|
divider = 1897;
|
|
|
|
dev_warn(dev,
|
|
|
|
"Speed too low (%u.%03u kHz), using %u.%03u kHz\n",
|
|
|
|
speed / 1000, speed % 1000,
|
|
|
|
clk / divider / 1000, clk / divider % 1000);
|
2012-11-30 21:48:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-07-05 20:28:00 +04:00
|
|
|
* The I2C spec specifies the following timing data:
|
|
|
|
* standard mode fast mode Bitfield name
|
|
|
|
* tLOW (SCL LOW period) 4700 ns 1300 ns
|
|
|
|
* tHIGH (SCL HIGH period) 4000 ns 600 ns
|
|
|
|
* tSU;DAT (data setup time) 250 ns 100 ns
|
|
|
|
* tHD;STA (START hold time) 4000 ns 600 ns
|
|
|
|
* tBUF (bus free time) 4700 ns 1300 ns
|
2012-11-30 21:48:35 +04:00
|
|
|
*
|
2013-07-05 20:28:00 +04:00
|
|
|
* The hardware (of the i.MX28 at least) seems to add 2 additional
|
|
|
|
* clock cycles to the low_count and 7 cycles to the high_count.
|
|
|
|
* This is compensated for by subtracting the respective constants
|
|
|
|
* from the values written to the timing registers.
|
2012-11-30 21:48:35 +04:00
|
|
|
*/
|
2013-07-05 20:28:00 +04:00
|
|
|
if (speed > 100000) {
|
|
|
|
/* fast mode */
|
|
|
|
low_count = DIV_ROUND_CLOSEST(divider * 13, (13 + 6));
|
|
|
|
high_count = DIV_ROUND_CLOSEST(divider * 6, (13 + 6));
|
|
|
|
leadin = DIV_ROUND_UP(600 * (clk / 1000000), 1000);
|
|
|
|
bus_free = DIV_ROUND_UP(1300 * (clk / 1000000), 1000);
|
|
|
|
} else {
|
|
|
|
/* normal mode */
|
|
|
|
low_count = DIV_ROUND_CLOSEST(divider * 47, (47 + 40));
|
|
|
|
high_count = DIV_ROUND_CLOSEST(divider * 40, (47 + 40));
|
|
|
|
leadin = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
|
|
|
|
bus_free = DIV_ROUND_UP(4700 * (clk / 1000000), 1000);
|
|
|
|
}
|
|
|
|
rcv_count = high_count * 3 / 8;
|
|
|
|
xmit_count = low_count * 3 / 8;
|
|
|
|
|
|
|
|
dev_dbg(dev,
|
|
|
|
"speed=%u(actual %u) divider=%u low=%u high=%u xmit=%u rcv=%u leadin=%u bus_free=%u\n",
|
|
|
|
speed, clk / divider, divider, low_count, high_count,
|
|
|
|
xmit_count, rcv_count, leadin, bus_free);
|
2012-11-30 21:48:35 +04:00
|
|
|
|
2013-07-05 20:28:00 +04:00
|
|
|
low_count -= 2;
|
|
|
|
high_count -= 7;
|
2012-11-30 21:48:35 +04:00
|
|
|
i2c->timing0 = (high_count << 16) | rcv_count;
|
|
|
|
i2c->timing1 = (low_count << 16) | xmit_count;
|
2013-07-05 20:28:00 +04:00
|
|
|
i2c->timing2 = (bus_free << 16 | leadin);
|
2012-11-30 21:48:35 +04:00
|
|
|
}
|
|
|
|
|
2012-07-09 20:22:53 +04:00
|
|
|
static int mxs_i2c_get_ofdata(struct mxs_i2c_dev *i2c)
|
|
|
|
{
|
|
|
|
uint32_t speed;
|
|
|
|
struct device *dev = i2c->dev;
|
|
|
|
struct device_node *node = dev->of_node;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32(node, "clock-frequency", &speed);
|
2012-11-30 21:48:35 +04:00
|
|
|
if (ret) {
|
2012-07-09 20:22:53 +04:00
|
|
|
dev_warn(dev, "No I2C speed selected, using 100kHz\n");
|
2012-11-30 21:48:35 +04:00
|
|
|
speed = 100000;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxs_i2c_derive_timing(i2c, speed);
|
2012-07-09 20:22:53 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-01 18:54:25 +03:00
|
|
|
static const struct platform_device_id mxs_i2c_devtype[] = {
|
2013-09-30 03:23:53 +04:00
|
|
|
{
|
|
|
|
.name = "imx23-i2c",
|
|
|
|
.driver_data = MXS_I2C_V1,
|
|
|
|
}, {
|
|
|
|
.name = "imx28-i2c",
|
|
|
|
.driver_data = MXS_I2C_V2,
|
|
|
|
}, { /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(platform, mxs_i2c_devtype);
|
|
|
|
|
|
|
|
static const struct of_device_id mxs_i2c_dt_ids[] = {
|
|
|
|
{ .compatible = "fsl,imx23-i2c", .data = &mxs_i2c_devtype[0], },
|
|
|
|
{ .compatible = "fsl,imx28-i2c", .data = &mxs_i2c_devtype[1], },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, mxs_i2c_dt_ids);
|
|
|
|
|
2012-11-28 00:59:38 +04:00
|
|
|
static int mxs_i2c_probe(struct platform_device *pdev)
|
2011-02-16 15:39:16 +03:00
|
|
|
{
|
2013-09-30 03:23:53 +04:00
|
|
|
const struct of_device_id *of_id =
|
|
|
|
of_match_device(mxs_i2c_dt_ids, &pdev->dev);
|
2011-02-16 15:39:16 +03:00
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct mxs_i2c_dev *i2c;
|
|
|
|
struct i2c_adapter *adap;
|
2013-02-26 07:20:22 +04:00
|
|
|
int err, irq;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2014-11-07 05:44:34 +03:00
|
|
|
i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
|
2011-02-16 15:39:16 +03:00
|
|
|
if (!i2c)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-09-30 03:23:53 +04:00
|
|
|
if (of_id) {
|
|
|
|
const struct platform_device_id *device_id = of_id->data;
|
|
|
|
i2c->dev_type = device_id->driver_data;
|
|
|
|
}
|
|
|
|
|
2019-07-17 11:40:16 +03:00
|
|
|
i2c->regs = devm_platform_ioremap_resource(pdev, 0);
|
2014-03-10 03:34:10 +04:00
|
|
|
if (IS_ERR(i2c->regs))
|
|
|
|
return PTR_ERR(i2c->regs);
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2014-03-10 03:34:10 +04:00
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq < 0)
|
|
|
|
return irq;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
|
|
|
err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
i2c->dev = dev;
|
2012-07-09 20:22:53 +04:00
|
|
|
|
2012-11-21 09:19:06 +04:00
|
|
|
init_completion(&i2c->cmd_complete);
|
|
|
|
|
2012-09-08 19:28:06 +04:00
|
|
|
if (dev->of_node) {
|
|
|
|
err = mxs_i2c_get_ofdata(i2c);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
2012-07-09 20:22:53 +04:00
|
|
|
|
2012-08-24 07:44:31 +04:00
|
|
|
/* Setup the DMA */
|
2019-12-17 10:45:05 +03:00
|
|
|
i2c->dmach = dma_request_chan(dev, "rx-tx");
|
|
|
|
if (IS_ERR(i2c->dmach)) {
|
2012-10-12 14:55:16 +04:00
|
|
|
dev_err(dev, "Failed to request dma\n");
|
2019-12-17 10:45:05 +03:00
|
|
|
return PTR_ERR(i2c->dmach);
|
2012-08-24 07:44:31 +04:00
|
|
|
}
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
platform_set_drvdata(pdev, i2c);
|
|
|
|
|
|
|
|
/* Do reset to enforce correct startup after pinmuxing */
|
2013-07-11 05:19:28 +04:00
|
|
|
err = mxs_i2c_reset(i2c);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-02-16 15:39:16 +03:00
|
|
|
|
|
|
|
adap = &i2c->adapter;
|
|
|
|
strlcpy(adap->name, "MXS I2C adapter", sizeof(adap->name));
|
|
|
|
adap->owner = THIS_MODULE;
|
|
|
|
adap->algo = &mxs_i2c_algo;
|
2018-07-23 23:26:07 +03:00
|
|
|
adap->quirks = &mxs_i2c_quirks;
|
2011-02-16 15:39:16 +03:00
|
|
|
adap->dev.parent = dev;
|
|
|
|
adap->nr = pdev->id;
|
2012-05-12 09:43:32 +04:00
|
|
|
adap->dev.of_node = pdev->dev.of_node;
|
2011-02-16 15:39:16 +03:00
|
|
|
i2c_set_adapdata(adap, i2c);
|
|
|
|
err = i2c_add_numbered_adapter(adap);
|
|
|
|
if (err) {
|
|
|
|
writel(MXS_I2C_CTRL0_SFTRST,
|
|
|
|
i2c->regs + MXS_I2C_CTRL0_SET);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 00:59:38 +04:00
|
|
|
static int mxs_i2c_remove(struct platform_device *pdev)
|
2011-02-16 15:39:16 +03:00
|
|
|
{
|
|
|
|
struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
|
|
|
|
|
2013-03-09 12:16:46 +04:00
|
|
|
i2c_del_adapter(&i2c->adapter);
|
2011-02-16 15:39:16 +03:00
|
|
|
|
2012-08-24 07:44:31 +04:00
|
|
|
if (i2c->dmach)
|
|
|
|
dma_release_channel(i2c->dmach);
|
|
|
|
|
2011-02-16 15:39:16 +03:00
|
|
|
writel(MXS_I2C_CTRL0_SFTRST, i2c->regs + MXS_I2C_CTRL0_SET);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver mxs_i2c_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = DRIVER_NAME,
|
2012-05-12 09:43:32 +04:00
|
|
|
.of_match_table = mxs_i2c_dt_ids,
|
2011-02-16 15:39:16 +03:00
|
|
|
},
|
2013-10-09 00:35:35 +04:00
|
|
|
.probe = mxs_i2c_probe,
|
2012-11-28 00:59:38 +04:00
|
|
|
.remove = mxs_i2c_remove,
|
2011-02-16 15:39:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init mxs_i2c_init(void)
|
|
|
|
{
|
2013-10-09 00:35:35 +04:00
|
|
|
return platform_driver_register(&mxs_i2c_driver);
|
2011-02-16 15:39:16 +03:00
|
|
|
}
|
|
|
|
subsys_initcall(mxs_i2c_init);
|
|
|
|
|
|
|
|
static void __exit mxs_i2c_exit(void)
|
|
|
|
{
|
|
|
|
platform_driver_unregister(&mxs_i2c_driver);
|
|
|
|
}
|
|
|
|
module_exit(mxs_i2c_exit);
|
|
|
|
|
2013-10-06 16:02:13 +04:00
|
|
|
MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
|
2015-04-20 16:51:39 +03:00
|
|
|
MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
|
2011-02-16 15:39:16 +03:00
|
|
|
MODULE_DESCRIPTION("MXS I2C Bus Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:" DRIVER_NAME);
|