/* FIFO should be enabled immediately after the software reset */ if (dev->fifo_size)
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
/* enable digital filter */ if (pdata->has_dig_filtr && dev->enable_dig_filt)
filtr |= AT91_TWI_FILTR_FILT;
/* enable advanced digital filter */ if (pdata->has_adv_dig_filtr && dev->enable_dig_filt)
filtr |= AT91_TWI_FILTR_FILT |
(AT91_TWI_FILTR_THRES(dev->filter_width) &
AT91_TWI_FILTR_THRES_MASK);
/* enable analog filter */ if (pdata->has_ana_filtr && dev->enable_ana_filt)
filtr |= AT91_TWI_FILTR_PADFEN;
if (filtr)
at91_twi_write(dev, AT91_TWI_FILTR, filtr);
}
/* *Calculatesymmetricclockasstatedindatasheet: *twi_clk=F_MAIN/(2*(cdiv*(1<<ckdiv)+offset))
*/ staticvoid at91_calc_twi_clock(struct at91_twi_dev *dev)
{ int ckdiv, cdiv, div, hold = 0, filter_width = 0; struct at91_twi_pdata *pdata = dev->pdata; int offset = pdata->clk_offset; int max_ckdiv = pdata->clk_max_div; struct i2c_timings timings, *t = &timings;
i2c_parse_fw_timings(dev->dev, t, true);
div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk), 2 * t->bus_freq_hz) - offset);
ckdiv = fls(div >> 8);
cdiv = div >> ckdiv;
if (ckdiv > max_ckdiv) {
dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
ckdiv, max_ckdiv);
ckdiv = max_ckdiv;
cdiv = 255;
}
if (pdata->has_hold_field) { /* *holdtime=HOLD+3xT_peripheral_clock *UseclkrateinkHztopreventoverflowswhencomputing *hold.
*/
hold = DIV_ROUND_UP(t->sda_hold_ns
* (clk_get_rate(dev->clk) / 1000), 1000000);
hold -= 3; if (hold < 0)
hold = 0; if (hold > AT91_TWI_CWGR_HOLD_MAX) {
dev_warn(dev->dev, "HOLD field set to its maximum value (%d instead of %d)\n",
AT91_TWI_CWGR_HOLD_MAX, hold);
hold = AT91_TWI_CWGR_HOLD_MAX;
}
}
if (pdata->has_adv_dig_filtr) { /* *filterwidth=0toAT91_TWI_FILTR_THRES_MAX *peripheralclocks
*/
filter_width = DIV_ROUND_UP(t->digital_filter_width_ns
* (clk_get_rate(dev->clk) / 1000), 1000000); if (filter_width > AT91_TWI_FILTR_THRES_MAX) {
dev_warn(dev->dev, "Filter threshold set to its maximum value (%d instead of %d)\n",
AT91_TWI_FILTR_THRES_MAX, filter_width);
filter_width = AT91_TWI_FILTR_THRES_MAX;
}
}
if (dma->xfer_in_progress) { if (dma->direction == DMA_FROM_DEVICE)
dmaengine_terminate_sync(dma->chan_rx); else
dmaengine_terminate_sync(dma->chan_tx);
dma->xfer_in_progress = false;
} if (dma->buf_mapped) {
dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
dev->buf_len, dma->direction);
dma->buf_mapped = false;
}
at91_twi_irq_restore(dev);
}
staticvoid at91_twi_write_next_byte(struct at91_twi_dev *dev)
{ if (!dev->buf_len) return;
/* 8bit write works with and without FIFO */
writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
/* send stop when last byte has been written */ if (--dev->buf_len == 0) { if (!dev->use_alt_cmd)
at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY);
}
dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
if (!dev->use_alt_cmd) { /* The last two bytes have to be read without using dma */
dev->buf += dev->buf_len - 2;
dev->buf_len = 2;
ier |= AT91_TWI_RXRDY;
}
at91_twi_write(dev, AT91_TWI_IER, ier);
}
/* Keep in mind that we won't use dma to read the last two bytes */
at91_twi_irq_save(dev);
dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE); if (dma_mapping_error(dev->dev, dma_addr)) {
dev_err(dev->dev, "dma map failed\n"); return;
}
dma->buf_mapped = true;
at91_twi_irq_restore(dev);
if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) { unsigned fifo_mr;
/* if only one byte is to be read, immediately stop transfer */ if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
!(dev->msg->flags & I2C_M_RECV_LEN))
start_flags |= AT91_TWI_STOP;
at91_twi_write(dev, AT91_TWI_CR, start_flags); /* *Whenusingdmawithoutalternativecommandmode,thelast *bytehastobereadmanuallyinordertonotsendthestop *commandtoolateandthentoreceiveextradata. *Inpractice,therearesomeissuesifyouusethedmato *readn-1bytesbecauseoflatency. *Readingn-2byteswithdmaandthetwolastonesmanually *seemstobethebestsolution.
*/ if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
at91_twi_read_data_dma(dev);
} else {
at91_twi_write(dev, AT91_TWI_IER,
AT91_TWI_TXCOMP |
AT91_TWI_NACK |
AT91_TWI_RXRDY);
}
} else { if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
at91_twi_write_data_dma(dev);
} else {
at91_twi_write_next_byte(dev);
at91_twi_write(dev, AT91_TWI_IER,
AT91_TWI_TXCOMP | AT91_TWI_NACK |
(dev->buf_len ? AT91_TWI_TXRDY : 0));
}
}
time_left = wait_for_completion_timeout(&dev->cmd_complete,
dev->adapter.timeout); if (time_left == 0) {
dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
at91_init_twi_bus(dev);
ret = -ETIMEDOUT; goto error;
} if (dev->transfer_status & AT91_TWI_NACK) {
dev_dbg(dev->dev, "received nack\n");
ret = -EREMOTEIO; goto error;
} if (dev->transfer_status & AT91_TWI_OVRE) {
dev_err(dev->dev, "overrun while reading\n");
ret = -EIO; goto error;
} if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
dev_err(dev->dev, "underrun while writing\n");
ret = -EIO; goto error;
} if ((has_alt_cmd || dev->fifo_size) &&
(dev->transfer_status & AT91_TWI_LOCK)) {
dev_err(dev->dev, "tx locked\n");
ret = -EIO; goto error;
} if (dev->recv_len_abort) {
dev_err(dev->dev, "invalid smbus block length recvd\n");
ret = -EPROTO; goto error;
}
dev_dbg(dev->dev, "transfer complete\n");
return0;
error: /* first stop DMA transfer if still in progress */
at91_twi_dma_cleanup(dev); /* then flush THR/FIFO and unlock TX if locked */ if ((has_alt_cmd || dev->fifo_size) &&
(dev->transfer_status & AT91_TWI_LOCK)) {
dev_dbg(dev->dev, "unlock tx\n");
at91_twi_write(dev, AT91_TWI_CR,
AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
}
ret = pm_runtime_get_sync(dev->dev); if (ret < 0) goto out;
if (num == 2) { int internal_address = 0; int i;
/* 1st msg is put into the internal address, start with 2nd */
m_start = &msg[1]; for (i = 0; i < msg->len; ++i) { constunsigned addr = msg->buf[msg->len - 1 - i];
dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
return ret;
error: if (ret != -EPROBE_DEFER)
dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n"); if (dma->chan_rx)
dma_release_channel(dma->chan_rx); if (dma->chan_tx)
dma_release_channel(dma->chan_tx); return ret;
}
rinfo->pinctrl = devm_pinctrl_get(&pdev->dev); if (!rinfo->pinctrl) {
dev_info(dev->dev, "pinctrl unavailable, bus recovery not supported\n"); return0;
} if (IS_ERR(rinfo->pinctrl)) {
dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n"); return PTR_ERR(rinfo->pinctrl);
}
dev->adapter.bus_recovery_info = rinfo;
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.