/* This will be the driver name the kernel reports */ #define DRIVER_NAME "imx-i2c"
#define I2C_IMX_CHECK_DELAY 30000 /* Time to check for bus idle, in NS */
/* * Enable DMA if transfer byte size is bigger than this threshold. * As the hardware request, it must bigger than 4 bytes.\ * I have set '16' here, maybe it's not the best but I think it's * the appropriate.
*/ #define DMA_THRESHOLD 16 #define DMA_TIMEOUT 1000
/* IMX I2C registers: * the I2C register offset is different between SoCs, * to provide support for all these chips, split the * register offset into a fixed base address and a * variable shift value, then the full register offset * will be calculated by * reg_off = ( reg_base_addr << reg_shift)
*/ #define IMX_I2C_IADR 0x00 /* i2c slave address */ #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */ #define IMX_I2C_I2CR 0x02 /* i2c control */ #define IMX_I2C_I2SR 0x03 /* i2c status */ #define IMX_I2C_I2DR 0x04 /* i2c transfer data */
/* * All of the layerscape series SoCs support IBIC register.
*/ #define IMX_I2C_IBIC 0x05 /* i2c bus interrupt config */
/* register bits different operating codes definition: * 1) I2SR: Interrupt flags clear operation differ between SoCs: * - write zero to clear(w0c) INT flag on i.MX, * - but write one to clear(w1c) INT flag on Vybrid. * 2) I2CR: I2C module enable operation also differ between SoCs: * - set I2CR_IEN bit enable the module on i.MX, * - but clear I2CR_IEN bit enable the module on Vybrid.
*/ #define I2SR_CLR_OPCODE_W0C 0x0 #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF) #define I2CR_IEN_OPCODE_0 0x0 #define I2CR_IEN_OPCODE_1 I2CR_IEN
#define I2C_PM_TIMEOUT 10 /* ms */
/* * sorted list of clock divider, register value pairs * taken from table 26-5, p.26-9, Freescale i.MX * Integrated Portable System Processor Reference Manual * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007 * * Duplicated divider values removed from list
*/ struct imx_i2c_clk_pair {
u16 div;
u16 val;
};
struct imx_i2c_hwdata { enum imx_i2c_type devtype; unsignedint regshift; struct imx_i2c_clk_pair *clk_div; unsignedint ndivs; unsignedint i2sr_clr_opcode; unsignedint i2cr_ien_opcode; /* * Errata ERR007805 or e7805: * I2C: When the I2C clock speed is configured for 400 kHz, * the SCL low period violates the I2C spec of 1.3 uS min.
*/ bool has_err007805;
};
/* * i2sr_clr_opcode is the value to clear all interrupts. Here we want to * clear only <bits>, so we write ~i2sr_clr_opcode with just <bits> * toggled. This is required because i.MX needs W0C and Vybrid uses W1C.
*/
temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
}
/* Set up i2c controller register and i2c status register to default value. */ staticvoid i2c_imx_reset_regs(struct imx_i2c_struct *i2c_imx)
{
imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
i2c_imx, IMX_I2C_I2CR);
i2c_imx_clear_irq(i2c_imx, I2SR_IIF | I2SR_IAL);
}
/* Functions for DMA support */ staticint i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx, dma_addr_t phy_addr)
{ struct imx_i2c_dma *dma; struct dma_slave_config dma_sconfig; struct device *dev = i2c_imx->adapter.dev.parent; int ret;
dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL); if (!dma) return -ENOMEM;
dma->chan_tx = dma_request_chan(dev, "tx"); if (IS_ERR(dma->chan_tx)) {
ret = PTR_ERR(dma->chan_tx); if (ret != -ENODEV && ret != -EPROBE_DEFER)
dev_err(dev, "can't request DMA tx channel (%d)\n", ret); goto fail_al;
}
txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
dma->dma_len, dma->dma_transfer_dir,
DMA_PREP_INTERRUPT | DMA_CTRL_ACK); if (!txdesc) {
dev_err(dev, "Not able to get desc for DMA xfer\n"); goto err_desc;
}
/* * The formula for the poll timeout is documented in the RM * Rev.5 on page 1878: * T_min = 10/F_scl * Set the value hard as it is done for the non-atomic use-case. * Use 10 kHz for the calculation since this is the minimum * allowed SMBus frequency. Also add an offset of 100us since it * turned out that the I2SR_IIF bit isn't set correctly within * the minimum timeout in polling mode.
*/
readb_poll_timeout_atomic(addr, regval, regval & I2SR_IIF, 5, 1000 + 100);
i2c_imx->i2csr = regval;
i2c_imx_clear_irq(i2c_imx, I2SR_IIF | I2SR_IAL);
} else {
wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
}
if (i2c_imx->hwdata->has_err007805 && i2c_imx->bitrate > 384000) {
dev_dbg(&i2c_imx->adapter.dev, "SoC errata ERR007805 or e7805 applies, bus frequency limited from %d Hz to 384000 Hz.\n",
i2c_imx->bitrate);
i2c_imx->bitrate = 384000;
}
/* Divider value calculation */ if (i2c_imx->cur_clk == i2c_clk_rate) return 0;
/* Keep the denominator of the following program always NOT equal to 0. */ if (!(i2c_clk_rate / 2)) return -EINVAL;
i2c_imx->cur_clk = i2c_clk_rate;
div = DIV_ROUND_UP(i2c_clk_rate, i2c_imx->bitrate); if (div < i2c_clk_div[0].div)
i = 0; elseif (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
i = i2c_imx->hwdata->ndivs - 1; else for (i = 0; i2c_clk_div[i].div < div; i++)
;
/* Store divider value */
i2c_imx->ifdr = i2c_clk_div[i].val;
/* * There dummy delay is calculated. * It should be about one I2C clock period long. * This delay is used in I2C bus disable function * to fix chip hardware bug.
*/
i2c_imx->disable_delay = DIV_ROUND_UP(500000U * i2c_clk_div[i].div,
i2c_clk_rate / 2);
if (!i2c_imx->stopped) { /* Stop I2C transaction */
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); if (!(temp & I2CR_MSTA))
i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX); if (i2c_imx->dma)
temp &= ~I2CR_DMAEN;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
} if (is_imx1_i2c(i2c_imx)) { /* * This delay caused by an i.MXL hardware bug. * If no (or too short) delay, no "STOP" bit will be generated.
*/
udelay(i2c_imx->disable_delay);
}
if (!i2c_imx->stopped)
i2c_imx_bus_busy(i2c_imx, 0, atomic);
/* * Enable bus idle interrupts * Note: IBIC register will be cleared after disabled i2c module. * All of layerscape series SoCs support IBIC register.
*/ staticvoid i2c_imx_enable_bus_idle(struct imx_i2c_struct *i2c_imx)
{ if (is_vf610_i2c(i2c_imx)) { unsignedint temp;
staticvoid i2c_imx_slave_finish_op(struct imx_i2c_struct *i2c_imx)
{
u8 val = 0;
while (i2c_imx->last_slave_event != I2C_SLAVE_STOP) { switch (i2c_imx->last_slave_event) { case I2C_SLAVE_READ_REQUESTED:
i2c_imx_slave_event(i2c_imx, I2C_SLAVE_READ_PROCESSED,
&val); break;
case I2C_SLAVE_WRITE_REQUESTED: case I2C_SLAVE_READ_PROCESSED: case I2C_SLAVE_WRITE_RECEIVED:
i2c_imx_slave_event(i2c_imx, I2C_SLAVE_STOP, &val); break;
case I2C_SLAVE_STOP: break;
}
}
}
/* Returns true if the timer should be restarted, false if not. */ static irqreturn_t i2c_imx_slave_handle(struct imx_i2c_struct *i2c_imx, unsignedint status, unsignedint ctl)
{
u8 value = 0;
if (status & I2SR_IAL) { /* Arbitration lost */
i2c_imx_clear_irq(i2c_imx, I2SR_IAL); if (!(status & I2SR_IAAS)) return IRQ_HANDLED;
}
if (!(status & I2SR_IBB)) { /* No master on the bus, that could mean a stop condition. */
i2c_imx_slave_finish_op(i2c_imx); return IRQ_HANDLED;
}
if (!(status & I2SR_ICF)) /* Data transfer still in progress, ignore this. */ goto out;
if (status & I2SR_IAAS) { /* Addressed as a slave */
i2c_imx_slave_finish_op(i2c_imx); if (status & I2SR_SRW) { /* Master wants to read from us*/
dev_dbg(&i2c_imx->adapter.dev, "read requested");
i2c_imx_slave_event(i2c_imx,
I2C_SLAVE_READ_REQUESTED, &value);
out: /* * No need to check the return value here. If it returns 0 or * 1, then everything is fine. If it returns -1, then the * timer is running in the handler. This will still work, * though it may be redone (or already have been done) by the * timer function.
*/
hrtimer_try_to_cancel(&i2c_imx->slave_timer);
hrtimer_forward_now(&i2c_imx->slave_timer, I2C_IMX_CHECK_DELAY);
hrtimer_restart(&i2c_imx->slave_timer); return IRQ_HANDLED;
}
static irqreturn_t i2c_imx_master_isr(struct imx_i2c_struct *i2c_imx, unsignedint status)
{ /* * This state machine handles I2C reception and transmission in non-DMA * mode. We must process all the data in the ISR to reduce the delay * between two consecutive messages. If the data is not processed in * the ISR, SMBus devices may timeout, leading to a bus error.
*/ switch (i2c_imx->state) { case IMX_I2C_STATE_DMA:
i2c_imx->i2csr = status;
wake_up(&i2c_imx->queue); break;
case IMX_I2C_STATE_READ: if (i2c_imx_isr_read(i2c_imx)) break;
i2c_imx->state = IMX_I2C_STATE_READ_CONTINUE; break;
case IMX_I2C_STATE_READ_CONTINUE:
i2c_imx_isr_read_continue(i2c_imx); if (i2c_imx->msg_buf_idx == i2c_imx->msg->len) {
i2c_imx->state = IMX_I2C_STATE_DONE;
wake_up(&i2c_imx->queue);
} break;
case IMX_I2C_STATE_READ_BLOCK_DATA: if (i2c_imx_isr_read(i2c_imx)) break;
i2c_imx->state = IMX_I2C_STATE_READ_BLOCK_DATA_LEN; break;
case IMX_I2C_STATE_READ_BLOCK_DATA_LEN:
i2c_imx_isr_read_block_data_len(i2c_imx);
i2c_imx->state = IMX_I2C_STATE_READ_CONTINUE; break;
case IMX_I2C_STATE_WRITE: if (i2c_imx_isr_write(i2c_imx)) break;
i2c_imx->state = IMX_I2C_STATE_DONE;
wake_up(&i2c_imx->queue); break;
/* * Write slave address. * The first byte must be transmitted by the CPU.
*/
imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
time_left = wait_for_completion_timeout(
&i2c_imx->dma->cmd_complete,
msecs_to_jiffies(DMA_TIMEOUT)); if (time_left == 0) {
dmaengine_terminate_sync(dma->chan_using); return -ETIMEDOUT;
}
/* Waiting for transfer complete. */ while (1) {
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR); if (temp & I2SR_ICF) break; if (time_after(jiffies, orig_jiffies +
msecs_to_jiffies(DMA_TIMEOUT))) {
dev_dbg(dev, "<%s> Timeout\n", __func__); return -ETIMEDOUT;
}
schedule();
}
/* The last data byte must be transferred by the CPU. */
imx_i2c_write_reg(msgs->buf[msgs->len-1],
i2c_imx, IMX_I2C_I2DR);
result = i2c_imx_trx_complete(i2c_imx, false); if (result) return result;
/* setup bus to read data */
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
temp &= ~I2CR_MTX;
/* * Reset the I2CR_TXAK flag initially for SMBus block read since the * length is unknown
*/ if (msgs->len - 1)
temp &= ~I2CR_TXAK; if (use_dma)
temp |= I2CR_DMAEN;
dma->chan_using = dma->chan_rx;
dma->dma_transfer_dir = DMA_DEV_TO_MEM;
dma->dma_data_dir = DMA_FROM_DEVICE; /* The last two data bytes must be transferred by the CPU. */
dma->dma_len = msgs->len - 2;
result = i2c_imx_dma_xfer(i2c_imx, msgs); if (result) return result;
msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* read n byte data */
result = i2c_imx_trx_complete(i2c_imx, false); if (result) return result;
if (is_lastmsg) { /* * It must generate STOP before read I2DR to prevent * controller from generating another clock cycle
*/
dev_dbg(dev, "<%s> clear MSTA\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); if (!(temp & I2CR_MSTA))
i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); if (!i2c_imx->stopped)
i2c_imx_bus_busy(i2c_imx, 0, false);
} else { /* * For i2c master receiver repeat restart operation like: * read -> repeat MSTA -> read/write * The controller must set MTX before read the last byte in * the first read operation, otherwise the first read cost * one extra clock cycle.
*/
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
temp |= I2CR_MTX;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
}
msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
return 0;
}
staticint i2c_imx_atomic_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
{ int i, result;
/* write slave address */
imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
result = i2c_imx_trx_complete(i2c_imx, true); if (result) return result;
result = i2c_imx_acked(i2c_imx); if (result) return result;
dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
/* write data */ for (i = 0; i < msgs->len; i++) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> write byte: B%d=0x%X\n",
__func__, i, msgs->buf[i]);
imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
result = i2c_imx_trx_complete(i2c_imx, true); if (result) return result;
result = i2c_imx_acked(i2c_imx); if (result) return result;
} return 0;
}
/* * By writing the device address we start the state machine in the ISR. * The ISR will report when it is done or when it fails.
*/
imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
wait_event_timeout(i2c_imx->queue,
i2c_imx->state == IMX_I2C_STATE_DONE ||
i2c_imx->state == IMX_I2C_STATE_FAILED,
(msgs->len + 1) * HZ / 10); if (i2c_imx->state == IMX_I2C_STATE_FAILED) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> write failed with %d\n",
__func__, i2c_imx->isr_result); return i2c_imx->isr_result;
} if (i2c_imx->state != IMX_I2C_STATE_DONE) {
dev_err(&i2c_imx->adapter.dev, "<%s> write timedout\n", __func__); return -ETIMEDOUT;
} return 0;
}
staticint i2c_imx_atomic_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
{ int i, result; unsignedint temp; int block_data = msgs->flags & I2C_M_RECV_LEN;
result = i2c_imx_prepare_read(i2c_imx, msgs, false); if (result) return result;
/* * By writing the device address we start the state machine in the ISR. * The ISR will report when it is done or when it fails.
*/
imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
wait_event_timeout(i2c_imx->queue,
i2c_imx->state == IMX_I2C_STATE_DONE ||
i2c_imx->state == IMX_I2C_STATE_FAILED,
(msgs->len + 1) * HZ / 10); if (i2c_imx->state == IMX_I2C_STATE_FAILED) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> read failed with %d\n",
__func__, i2c_imx->isr_result); return i2c_imx->isr_result;
} if (i2c_imx->state != IMX_I2C_STATE_DONE) {
dev_err(&i2c_imx->adapter.dev, "<%s> read timedout\n", __func__); return -ETIMEDOUT;
} if (!i2c_imx->stopped) return i2c_imx_bus_busy(i2c_imx, 0, false);
return 0;
}
staticint i2c_imx_xfer_common(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num, bool atomic)
{ unsignedint i, temp; int result; bool is_lastmsg = false; struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); int use_dma = 0;
/* Start I2C transfer */
result = i2c_imx_start(i2c_imx, atomic); if (result) { /* * Bus recovery uses gpiod_get_value_cansleep() which is not * allowed within atomic context.
*/ if (!atomic && i2c_imx->adapter.bus_recovery_info) {
i2c_recover_bus(&i2c_imx->adapter);
result = i2c_imx_start(i2c_imx, atomic);
}
}
if (result) goto fail0;
/* read/write data */ for (i = 0; i < num; i++) { if (i == num - 1)
is_lastmsg = true;
staticint i2c_imx_xfer_atomic(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
{ struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); int result;
result = clk_enable(i2c_imx->clk); if (result) return result;
result = i2c_imx_xfer_common(adapter, msgs, num, true);
clk_disable(i2c_imx->clk);
return result;
}
/* * We switch SCL and SDA to their GPIO function and do some bitbanging * for bus recovery. These alternative pinmux settings can be * described in the device tree by a separate pinctrl state "gpio". If * this is missing this is not a big problem, the only implication is * that we can't do bus recovery.
*/ staticint i2c_imx_init_recovery_info(struct imx_i2c_struct *i2c_imx, struct platform_device *pdev)
{ struct i2c_bus_recovery_info *bri = &i2c_imx->rinfo;
bri->pinctrl = devm_pinctrl_get(&pdev->dev); if (IS_ERR(bri->pinctrl)) return PTR_ERR(bri->pinctrl);
irq = platform_get_irq(pdev, 0); if (irq < 0) return dev_err_probe(&pdev->dev, irq, "can't get IRQ\n");
base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); if (IS_ERR(base)) return dev_err_probe(&pdev->dev, PTR_ERR(base), "can't get IO memory\n");
/* * We use the single-master property for backward compatibility. * By default multi master mode is enabled.
*/
i2c_imx->multi_master = !of_property_read_bool(pdev->dev.of_node, "single-master");
/* Set up clock divider */
i2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ;
ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency", &i2c_imx->bitrate); if (ret < 0 && pdata && pdata->bitrate)
i2c_imx->bitrate = pdata->bitrate;
i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
ret = i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk)); if (ret < 0) {
dev_err(&pdev->dev, "can't get I2C clock\n"); goto clk_notifier_unregister;
}
i2c_imx_reset_regs(i2c_imx);
/* Init optional bus recovery function */
ret = i2c_imx_init_recovery_info(i2c_imx, pdev); /* Give it another chance if pinctrl used is not ready yet */ if (ret == -EPROBE_DEFER) goto clk_notifier_unregister;
/* * DMA mode should be optional for I2C, when encountering DMA errors, * no need to exit I2C probe. Only print warning to show DMA error and * use PIO mode directly to ensure I2C bus available as much as possible.
*/
ret = i2c_imx_dma_request(i2c_imx, phy_addr); if (ret) { if (ret == -EPROBE_DEFER) {
dev_err_probe(&pdev->dev, ret, "can't get DMA channels\n"); goto clk_notifier_unregister;
} elseif (ret == -ENODEV) {
dev_dbg(&pdev->dev, "Only use PIO mode\n");
} else {
dev_warn(&pdev->dev, "Failed to setup DMA (%pe), only use PIO mode\n",
ERR_PTR(ret));
}
}
/* Add I2C adapter */
ret = i2c_add_numbered_adapter(&i2c_imx->adapter); if (ret < 0) goto clk_notifier_unregister;
ret = pinctrl_pm_select_default_state(dev); if (ret) return ret;
ret = clk_enable(i2c_imx->clk); if (ret)
dev_err(dev, "can't enable I2C clock, ret=%d\n", ret);
return ret;
}
staticint i2c_imx_suspend(struct device *dev)
{ /* * Some I2C devices may need the I2C controller to remain active * during resume_noirq() or suspend_noirq(). If the controller is * autosuspended, there is no way to wake it up once runtime PM is * disabled (in suspend_late()). * * During system resume, the I2C controller will be available only * after runtime PM is re-enabled (in resume_early()). However, this * may be too late for some devices. * * Wake up the controller in the suspend() callback while runtime PM * is still enabled. The I2C controller will remain available until * the suspend_noirq() callback (pm_runtime_force_suspend()) is * called. During resume, the I2C controller can be restored by the * resume_noirq() callback (pm_runtime_force_resume()). * * Finally, the resume() callback re-enables autosuspend, ensuring * the I2C controller remains available until the system enters * suspend_noirq() and from resume_noirq().
*/ return pm_runtime_resume_and_get(dev);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.