18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  i2c Support for Atmel's AT91 Two-Wire Interface (TWI)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2011 Weinmann Medical GmbH
68c2ecf20Sopenharmony_ci *  Author: Nikolaus Voss <n.voss@weinmann.de>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Evolved from original work by:
98c2ecf20Sopenharmony_ci *  Copyright (C) 2004 Rick Bronson
108c2ecf20Sopenharmony_ci *  Converted to 2.6 by Andrew Victor <andrew@sanpeople.com>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  Borrowed heavily from original work by:
138c2ecf20Sopenharmony_ci *  Copyright (C) 2000 Philip Edelbrock <phil@stimpy.netroedge.com>
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/clk.h>
178c2ecf20Sopenharmony_ci#include <linux/completion.h>
188c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
198c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
208c2ecf20Sopenharmony_ci#include <linux/err.h>
218c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
228c2ecf20Sopenharmony_ci#include <linux/i2c.h>
238c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
248c2ecf20Sopenharmony_ci#include <linux/io.h>
258c2ecf20Sopenharmony_ci#include <linux/of.h>
268c2ecf20Sopenharmony_ci#include <linux/of_device.h>
278c2ecf20Sopenharmony_ci#include <linux/pinctrl/consumer.h>
288c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
298c2ecf20Sopenharmony_ci#include <linux/platform_data/dma-atmel.h>
308c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "i2c-at91.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_civoid at91_init_twi_bus_master(struct at91_twi_dev *dev)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct at91_twi_pdata *pdata = dev->pdata;
378c2ecf20Sopenharmony_ci	u32 filtr = 0;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	/* FIFO should be enabled immediately after the software reset */
408c2ecf20Sopenharmony_ci	if (dev->fifo_size)
418c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
428c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_MSEN);
438c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SVDIS);
448c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* enable digital filter */
478c2ecf20Sopenharmony_ci	if (pdata->has_dig_filtr && dev->enable_dig_filt)
488c2ecf20Sopenharmony_ci		filtr |= AT91_TWI_FILTR_FILT;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	/* enable advanced digital filter */
518c2ecf20Sopenharmony_ci	if (pdata->has_adv_dig_filtr && dev->enable_dig_filt)
528c2ecf20Sopenharmony_ci		filtr |= AT91_TWI_FILTR_FILT |
538c2ecf20Sopenharmony_ci			 (AT91_TWI_FILTR_THRES(dev->filter_width) &
548c2ecf20Sopenharmony_ci			 AT91_TWI_FILTR_THRES_MASK);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	/* enable analog filter */
578c2ecf20Sopenharmony_ci	if (pdata->has_ana_filtr && dev->enable_ana_filt)
588c2ecf20Sopenharmony_ci		filtr |= AT91_TWI_FILTR_PADFEN;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (filtr)
618c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_FILTR, filtr);
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * Calculate symmetric clock as stated in datasheet:
668c2ecf20Sopenharmony_ci * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistatic void at91_calc_twi_clock(struct at91_twi_dev *dev)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	int ckdiv, cdiv, div, hold = 0, filter_width = 0;
718c2ecf20Sopenharmony_ci	struct at91_twi_pdata *pdata = dev->pdata;
728c2ecf20Sopenharmony_ci	int offset = pdata->clk_offset;
738c2ecf20Sopenharmony_ci	int max_ckdiv = pdata->clk_max_div;
748c2ecf20Sopenharmony_ci	struct i2c_timings timings, *t = &timings;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	i2c_parse_fw_timings(dev->dev, t, true);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	div = max(0, (int)DIV_ROUND_UP(clk_get_rate(dev->clk),
798c2ecf20Sopenharmony_ci				       2 * t->bus_freq_hz) - offset);
808c2ecf20Sopenharmony_ci	ckdiv = fls(div >> 8);
818c2ecf20Sopenharmony_ci	cdiv = div >> ckdiv;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (ckdiv > max_ckdiv) {
848c2ecf20Sopenharmony_ci		dev_warn(dev->dev, "%d exceeds ckdiv max value which is %d.\n",
858c2ecf20Sopenharmony_ci			 ckdiv, max_ckdiv);
868c2ecf20Sopenharmony_ci		ckdiv = max_ckdiv;
878c2ecf20Sopenharmony_ci		cdiv = 255;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (pdata->has_hold_field) {
918c2ecf20Sopenharmony_ci		/*
928c2ecf20Sopenharmony_ci		 * hold time = HOLD + 3 x T_peripheral_clock
938c2ecf20Sopenharmony_ci		 * Use clk rate in kHz to prevent overflows when computing
948c2ecf20Sopenharmony_ci		 * hold.
958c2ecf20Sopenharmony_ci		 */
968c2ecf20Sopenharmony_ci		hold = DIV_ROUND_UP(t->sda_hold_ns
978c2ecf20Sopenharmony_ci				    * (clk_get_rate(dev->clk) / 1000), 1000000);
988c2ecf20Sopenharmony_ci		hold -= 3;
998c2ecf20Sopenharmony_ci		if (hold < 0)
1008c2ecf20Sopenharmony_ci			hold = 0;
1018c2ecf20Sopenharmony_ci		if (hold > AT91_TWI_CWGR_HOLD_MAX) {
1028c2ecf20Sopenharmony_ci			dev_warn(dev->dev,
1038c2ecf20Sopenharmony_ci				 "HOLD field set to its maximum value (%d instead of %d)\n",
1048c2ecf20Sopenharmony_ci				 AT91_TWI_CWGR_HOLD_MAX, hold);
1058c2ecf20Sopenharmony_ci			hold = AT91_TWI_CWGR_HOLD_MAX;
1068c2ecf20Sopenharmony_ci		}
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (pdata->has_adv_dig_filtr) {
1108c2ecf20Sopenharmony_ci		/*
1118c2ecf20Sopenharmony_ci		 * filter width = 0 to AT91_TWI_FILTR_THRES_MAX
1128c2ecf20Sopenharmony_ci		 * peripheral clocks
1138c2ecf20Sopenharmony_ci		 */
1148c2ecf20Sopenharmony_ci		filter_width = DIV_ROUND_UP(t->digital_filter_width_ns
1158c2ecf20Sopenharmony_ci				* (clk_get_rate(dev->clk) / 1000), 1000000);
1168c2ecf20Sopenharmony_ci		if (filter_width > AT91_TWI_FILTR_THRES_MAX) {
1178c2ecf20Sopenharmony_ci			dev_warn(dev->dev,
1188c2ecf20Sopenharmony_ci				"Filter threshold set to its maximum value (%d instead of %d)\n",
1198c2ecf20Sopenharmony_ci				AT91_TWI_FILTR_THRES_MAX, filter_width);
1208c2ecf20Sopenharmony_ci			filter_width = AT91_TWI_FILTR_THRES_MAX;
1218c2ecf20Sopenharmony_ci		}
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	dev->twi_cwgr_reg = (ckdiv << 16) | (cdiv << 8) | cdiv
1258c2ecf20Sopenharmony_ci			    | AT91_TWI_CWGR_HOLD(hold);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	dev->filter_width = filter_width;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "cdiv %d ckdiv %d hold %d (%d ns), filter_width %d (%d ns)\n",
1308c2ecf20Sopenharmony_ci		cdiv, ckdiv, hold, t->sda_hold_ns, filter_width,
1318c2ecf20Sopenharmony_ci		t->digital_filter_width_ns);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void at91_twi_dma_cleanup(struct at91_twi_dev *dev)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct at91_twi_dma *dma = &dev->dma;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	at91_twi_irq_save(dev);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (dma->xfer_in_progress) {
1418c2ecf20Sopenharmony_ci		if (dma->direction == DMA_FROM_DEVICE)
1428c2ecf20Sopenharmony_ci			dmaengine_terminate_all(dma->chan_rx);
1438c2ecf20Sopenharmony_ci		else
1448c2ecf20Sopenharmony_ci			dmaengine_terminate_all(dma->chan_tx);
1458c2ecf20Sopenharmony_ci		dma->xfer_in_progress = false;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci	if (dma->buf_mapped) {
1488c2ecf20Sopenharmony_ci		dma_unmap_single(dev->dev, sg_dma_address(&dma->sg[0]),
1498c2ecf20Sopenharmony_ci				 dev->buf_len, dma->direction);
1508c2ecf20Sopenharmony_ci		dma->buf_mapped = false;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	at91_twi_irq_restore(dev);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic void at91_twi_write_next_byte(struct at91_twi_dev *dev)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	if (!dev->buf_len)
1598c2ecf20Sopenharmony_ci		return;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	/* 8bit write works with and without FIFO */
1628c2ecf20Sopenharmony_ci	writeb_relaxed(*dev->buf, dev->base + AT91_TWI_THR);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* send stop when last byte has been written */
1658c2ecf20Sopenharmony_ci	if (--dev->buf_len == 0) {
1668c2ecf20Sopenharmony_ci		if (!dev->use_alt_cmd)
1678c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
1688c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_IDR, AT91_TWI_TXRDY);
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "wrote 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	++dev->buf;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void at91_twi_write_data_dma_callback(void *data)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
1818c2ecf20Sopenharmony_ci			 dev->buf_len, DMA_TO_DEVICE);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/*
1848c2ecf20Sopenharmony_ci	 * When this callback is called, THR/TX FIFO is likely not to be empty
1858c2ecf20Sopenharmony_ci	 * yet. So we have to wait for TXCOMP or NACK bits to be set into the
1868c2ecf20Sopenharmony_ci	 * Status Register to be sure that the STOP bit has been sent and the
1878c2ecf20Sopenharmony_ci	 * transfer is completed. The NACK interrupt has already been enabled,
1888c2ecf20Sopenharmony_ci	 * we just have to enable TXCOMP one.
1898c2ecf20Sopenharmony_ci	 */
1908c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
1918c2ecf20Sopenharmony_ci	if (!dev->use_alt_cmd)
1928c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic void at91_twi_write_data_dma(struct at91_twi_dev *dev)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
1988c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *txdesc;
1998c2ecf20Sopenharmony_ci	struct at91_twi_dma *dma = &dev->dma;
2008c2ecf20Sopenharmony_ci	struct dma_chan *chan_tx = dma->chan_tx;
2018c2ecf20Sopenharmony_ci	unsigned int sg_len = 1;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (!dev->buf_len)
2048c2ecf20Sopenharmony_ci		return;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	dma->direction = DMA_TO_DEVICE;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	at91_twi_irq_save(dev);
2098c2ecf20Sopenharmony_ci	dma_addr = dma_map_single(dev->dev, dev->buf, dev->buf_len,
2108c2ecf20Sopenharmony_ci				  DMA_TO_DEVICE);
2118c2ecf20Sopenharmony_ci	if (dma_mapping_error(dev->dev, dma_addr)) {
2128c2ecf20Sopenharmony_ci		dev_err(dev->dev, "dma map failed\n");
2138c2ecf20Sopenharmony_ci		return;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci	dma->buf_mapped = true;
2168c2ecf20Sopenharmony_ci	at91_twi_irq_restore(dev);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (dev->fifo_size) {
2198c2ecf20Sopenharmony_ci		size_t part1_len, part2_len;
2208c2ecf20Sopenharmony_ci		struct scatterlist *sg;
2218c2ecf20Sopenharmony_ci		unsigned fifo_mr;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		sg_len = 0;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci		part1_len = dev->buf_len & ~0x3;
2268c2ecf20Sopenharmony_ci		if (part1_len) {
2278c2ecf20Sopenharmony_ci			sg = &dma->sg[sg_len++];
2288c2ecf20Sopenharmony_ci			sg_dma_len(sg) = part1_len;
2298c2ecf20Sopenharmony_ci			sg_dma_address(sg) = dma_addr;
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		part2_len = dev->buf_len & 0x3;
2338c2ecf20Sopenharmony_ci		if (part2_len) {
2348c2ecf20Sopenharmony_ci			sg = &dma->sg[sg_len++];
2358c2ecf20Sopenharmony_ci			sg_dma_len(sg) = part2_len;
2368c2ecf20Sopenharmony_ci			sg_dma_address(sg) = dma_addr + part1_len;
2378c2ecf20Sopenharmony_ci		}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		/*
2408c2ecf20Sopenharmony_ci		 * DMA controller is triggered when at least 4 data can be
2418c2ecf20Sopenharmony_ci		 * written into the TX FIFO
2428c2ecf20Sopenharmony_ci		 */
2438c2ecf20Sopenharmony_ci		fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
2448c2ecf20Sopenharmony_ci		fifo_mr &= ~AT91_TWI_FMR_TXRDYM_MASK;
2458c2ecf20Sopenharmony_ci		fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_FOUR_DATA);
2468c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
2478c2ecf20Sopenharmony_ci	} else {
2488c2ecf20Sopenharmony_ci		sg_dma_len(&dma->sg[0]) = dev->buf_len;
2498c2ecf20Sopenharmony_ci		sg_dma_address(&dma->sg[0]) = dma_addr;
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	txdesc = dmaengine_prep_slave_sg(chan_tx, dma->sg, sg_len,
2538c2ecf20Sopenharmony_ci					 DMA_MEM_TO_DEV,
2548c2ecf20Sopenharmony_ci					 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
2558c2ecf20Sopenharmony_ci	if (!txdesc) {
2568c2ecf20Sopenharmony_ci		dev_err(dev->dev, "dma prep slave sg failed\n");
2578c2ecf20Sopenharmony_ci		goto error;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	txdesc->callback = at91_twi_write_data_dma_callback;
2618c2ecf20Sopenharmony_ci	txdesc->callback_param = dev;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	dma->xfer_in_progress = true;
2648c2ecf20Sopenharmony_ci	dmaengine_submit(txdesc);
2658c2ecf20Sopenharmony_ci	dma_async_issue_pending(chan_tx);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cierror:
2708c2ecf20Sopenharmony_ci	at91_twi_dma_cleanup(dev);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void at91_twi_read_next_byte(struct at91_twi_dev *dev)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * If we are in this case, it means there is garbage data in RHR, so
2778c2ecf20Sopenharmony_ci	 * delete them.
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	if (!dev->buf_len) {
2808c2ecf20Sopenharmony_ci		at91_twi_read(dev, AT91_TWI_RHR);
2818c2ecf20Sopenharmony_ci		return;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* 8bit read works with and without FIFO */
2858c2ecf20Sopenharmony_ci	*dev->buf = readb_relaxed(dev->base + AT91_TWI_RHR);
2868c2ecf20Sopenharmony_ci	--dev->buf_len;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* return if aborting, we only needed to read RHR to clear RXRDY*/
2898c2ecf20Sopenharmony_ci	if (dev->recv_len_abort)
2908c2ecf20Sopenharmony_ci		return;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* handle I2C_SMBUS_BLOCK_DATA */
2938c2ecf20Sopenharmony_ci	if (unlikely(dev->msg->flags & I2C_M_RECV_LEN)) {
2948c2ecf20Sopenharmony_ci		/* ensure length byte is a valid value */
2958c2ecf20Sopenharmony_ci		if (*dev->buf <= I2C_SMBUS_BLOCK_MAX && *dev->buf > 0) {
2968c2ecf20Sopenharmony_ci			dev->msg->flags &= ~I2C_M_RECV_LEN;
2978c2ecf20Sopenharmony_ci			dev->buf_len += *dev->buf;
2988c2ecf20Sopenharmony_ci			dev->msg->len = dev->buf_len + 1;
2998c2ecf20Sopenharmony_ci			dev_dbg(dev->dev, "received block length %zu\n",
3008c2ecf20Sopenharmony_ci					 dev->buf_len);
3018c2ecf20Sopenharmony_ci		} else {
3028c2ecf20Sopenharmony_ci			/* abort and send the stop by reading one more byte */
3038c2ecf20Sopenharmony_ci			dev->recv_len_abort = true;
3048c2ecf20Sopenharmony_ci			dev->buf_len = 1;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/* send stop if second but last byte has been read */
3098c2ecf20Sopenharmony_ci	if (!dev->use_alt_cmd && dev->buf_len == 1)
3108c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_STOP);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "read 0x%x, to go %zu\n", *dev->buf, dev->buf_len);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	++dev->buf;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic void at91_twi_read_data_dma_callback(void *data)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct at91_twi_dev *dev = (struct at91_twi_dev *)data;
3208c2ecf20Sopenharmony_ci	unsigned ier = AT91_TWI_TXCOMP;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	dma_unmap_single(dev->dev, sg_dma_address(&dev->dma.sg[0]),
3238c2ecf20Sopenharmony_ci			 dev->buf_len, DMA_FROM_DEVICE);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (!dev->use_alt_cmd) {
3268c2ecf20Sopenharmony_ci		/* The last two bytes have to be read without using dma */
3278c2ecf20Sopenharmony_ci		dev->buf += dev->buf_len - 2;
3288c2ecf20Sopenharmony_ci		dev->buf_len = 2;
3298c2ecf20Sopenharmony_ci		ier |= AT91_TWI_RXRDY;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_IER, ier);
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic void at91_twi_read_data_dma(struct at91_twi_dev *dev)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	dma_addr_t dma_addr;
3378c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *rxdesc;
3388c2ecf20Sopenharmony_ci	struct at91_twi_dma *dma = &dev->dma;
3398c2ecf20Sopenharmony_ci	struct dma_chan *chan_rx = dma->chan_rx;
3408c2ecf20Sopenharmony_ci	size_t buf_len;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	buf_len = (dev->use_alt_cmd) ? dev->buf_len : dev->buf_len - 2;
3438c2ecf20Sopenharmony_ci	dma->direction = DMA_FROM_DEVICE;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Keep in mind that we won't use dma to read the last two bytes */
3468c2ecf20Sopenharmony_ci	at91_twi_irq_save(dev);
3478c2ecf20Sopenharmony_ci	dma_addr = dma_map_single(dev->dev, dev->buf, buf_len, DMA_FROM_DEVICE);
3488c2ecf20Sopenharmony_ci	if (dma_mapping_error(dev->dev, dma_addr)) {
3498c2ecf20Sopenharmony_ci		dev_err(dev->dev, "dma map failed\n");
3508c2ecf20Sopenharmony_ci		return;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci	dma->buf_mapped = true;
3538c2ecf20Sopenharmony_ci	at91_twi_irq_restore(dev);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (dev->fifo_size && IS_ALIGNED(buf_len, 4)) {
3568c2ecf20Sopenharmony_ci		unsigned fifo_mr;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		/*
3598c2ecf20Sopenharmony_ci		 * DMA controller is triggered when at least 4 data can be
3608c2ecf20Sopenharmony_ci		 * read from the RX FIFO
3618c2ecf20Sopenharmony_ci		 */
3628c2ecf20Sopenharmony_ci		fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
3638c2ecf20Sopenharmony_ci		fifo_mr &= ~AT91_TWI_FMR_RXRDYM_MASK;
3648c2ecf20Sopenharmony_ci		fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_FOUR_DATA);
3658c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	sg_dma_len(&dma->sg[0]) = buf_len;
3698c2ecf20Sopenharmony_ci	sg_dma_address(&dma->sg[0]) = dma_addr;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	rxdesc = dmaengine_prep_slave_sg(chan_rx, dma->sg, 1, DMA_DEV_TO_MEM,
3728c2ecf20Sopenharmony_ci					 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
3738c2ecf20Sopenharmony_ci	if (!rxdesc) {
3748c2ecf20Sopenharmony_ci		dev_err(dev->dev, "dma prep slave sg failed\n");
3758c2ecf20Sopenharmony_ci		goto error;
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	rxdesc->callback = at91_twi_read_data_dma_callback;
3798c2ecf20Sopenharmony_ci	rxdesc->callback_param = dev;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	dma->xfer_in_progress = true;
3828c2ecf20Sopenharmony_ci	dmaengine_submit(rxdesc);
3838c2ecf20Sopenharmony_ci	dma_async_issue_pending(dma->chan_rx);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cierror:
3888c2ecf20Sopenharmony_ci	at91_twi_dma_cleanup(dev);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct at91_twi_dev *dev = dev_id;
3948c2ecf20Sopenharmony_ci	const unsigned status = at91_twi_read(dev, AT91_TWI_SR);
3958c2ecf20Sopenharmony_ci	const unsigned irqstatus = status & at91_twi_read(dev, AT91_TWI_IMR);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (!irqstatus)
3988c2ecf20Sopenharmony_ci		return IRQ_NONE;
3998c2ecf20Sopenharmony_ci	/*
4008c2ecf20Sopenharmony_ci	 * In reception, the behavior of the twi device (before sama5d2) is
4018c2ecf20Sopenharmony_ci	 * weird. There is some magic about RXRDY flag! When a data has been
4028c2ecf20Sopenharmony_ci	 * almost received, the reception of a new one is anticipated if there
4038c2ecf20Sopenharmony_ci	 * is no stop command to send. That is the reason why ask for sending
4048c2ecf20Sopenharmony_ci	 * the stop command not on the last data but on the second last one.
4058c2ecf20Sopenharmony_ci	 *
4068c2ecf20Sopenharmony_ci	 * Unfortunately, we could still have the RXRDY flag set even if the
4078c2ecf20Sopenharmony_ci	 * transfer is done and we have read the last data. It might happen
4088c2ecf20Sopenharmony_ci	 * when the i2c slave device sends too quickly data after receiving the
4098c2ecf20Sopenharmony_ci	 * ack from the master. The data has been almost received before having
4108c2ecf20Sopenharmony_ci	 * the order to send stop. In this case, sending the stop command could
4118c2ecf20Sopenharmony_ci	 * cause a RXRDY interrupt with a TXCOMP one. It is better to manage
4128c2ecf20Sopenharmony_ci	 * the RXRDY interrupt first in order to not keep garbage data in the
4138c2ecf20Sopenharmony_ci	 * Receive Holding Register for the next transfer.
4148c2ecf20Sopenharmony_ci	 */
4158c2ecf20Sopenharmony_ci	if (irqstatus & AT91_TWI_RXRDY) {
4168c2ecf20Sopenharmony_ci		/*
4178c2ecf20Sopenharmony_ci		 * Read all available bytes at once by polling RXRDY usable w/
4188c2ecf20Sopenharmony_ci		 * and w/o FIFO. With FIFO enabled we could also read RXFL and
4198c2ecf20Sopenharmony_ci		 * avoid polling RXRDY.
4208c2ecf20Sopenharmony_ci		 */
4218c2ecf20Sopenharmony_ci		do {
4228c2ecf20Sopenharmony_ci			at91_twi_read_next_byte(dev);
4238c2ecf20Sopenharmony_ci		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/*
4278c2ecf20Sopenharmony_ci	 * When a NACK condition is detected, the I2C controller sets the NACK,
4288c2ecf20Sopenharmony_ci	 * TXCOMP and TXRDY bits all together in the Status Register (SR).
4298c2ecf20Sopenharmony_ci	 *
4308c2ecf20Sopenharmony_ci	 * 1 - Handling NACK errors with CPU write transfer.
4318c2ecf20Sopenharmony_ci	 *
4328c2ecf20Sopenharmony_ci	 * In such case, we should not write the next byte into the Transmit
4338c2ecf20Sopenharmony_ci	 * Holding Register (THR) otherwise the I2C controller would start a new
4348c2ecf20Sopenharmony_ci	 * transfer and the I2C slave is likely to reply by another NACK.
4358c2ecf20Sopenharmony_ci	 *
4368c2ecf20Sopenharmony_ci	 * 2 - Handling NACK errors with DMA write transfer.
4378c2ecf20Sopenharmony_ci	 *
4388c2ecf20Sopenharmony_ci	 * By setting the TXRDY bit in the SR, the I2C controller also triggers
4398c2ecf20Sopenharmony_ci	 * the DMA controller to write the next data into the THR. Then the
4408c2ecf20Sopenharmony_ci	 * result depends on the hardware version of the I2C controller.
4418c2ecf20Sopenharmony_ci	 *
4428c2ecf20Sopenharmony_ci	 * 2a - Without support of the Alternative Command mode.
4438c2ecf20Sopenharmony_ci	 *
4448c2ecf20Sopenharmony_ci	 * This is the worst case: the DMA controller is triggered to write the
4458c2ecf20Sopenharmony_ci	 * next data into the THR, hence starting a new transfer: the I2C slave
4468c2ecf20Sopenharmony_ci	 * is likely to reply by another NACK.
4478c2ecf20Sopenharmony_ci	 * Concurrently, this interrupt handler is likely to be called to manage
4488c2ecf20Sopenharmony_ci	 * the first NACK before the I2C controller detects the second NACK and
4498c2ecf20Sopenharmony_ci	 * sets once again the NACK bit into the SR.
4508c2ecf20Sopenharmony_ci	 * When handling the first NACK, this interrupt handler disables the I2C
4518c2ecf20Sopenharmony_ci	 * controller interruptions, especially the NACK interrupt.
4528c2ecf20Sopenharmony_ci	 * Hence, the NACK bit is pending into the SR. This is why we should
4538c2ecf20Sopenharmony_ci	 * read the SR to clear all pending interrupts at the beginning of
4548c2ecf20Sopenharmony_ci	 * at91_do_twi_transfer() before actually starting a new transfer.
4558c2ecf20Sopenharmony_ci	 *
4568c2ecf20Sopenharmony_ci	 * 2b - With support of the Alternative Command mode.
4578c2ecf20Sopenharmony_ci	 *
4588c2ecf20Sopenharmony_ci	 * When a NACK condition is detected, the I2C controller also locks the
4598c2ecf20Sopenharmony_ci	 * THR (and sets the LOCK bit in the SR): even though the DMA controller
4608c2ecf20Sopenharmony_ci	 * is triggered by the TXRDY bit to write the next data into the THR,
4618c2ecf20Sopenharmony_ci	 * this data actually won't go on the I2C bus hence a second NACK is not
4628c2ecf20Sopenharmony_ci	 * generated.
4638c2ecf20Sopenharmony_ci	 */
4648c2ecf20Sopenharmony_ci	if (irqstatus & (AT91_TWI_TXCOMP | AT91_TWI_NACK)) {
4658c2ecf20Sopenharmony_ci		at91_disable_twi_interrupts(dev);
4668c2ecf20Sopenharmony_ci		complete(&dev->cmd_complete);
4678c2ecf20Sopenharmony_ci	} else if (irqstatus & AT91_TWI_TXRDY) {
4688c2ecf20Sopenharmony_ci		at91_twi_write_next_byte(dev);
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* catch error flags */
4728c2ecf20Sopenharmony_ci	dev->transfer_status |= status;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int at91_do_twi_transfer(struct at91_twi_dev *dev)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	int ret;
4808c2ecf20Sopenharmony_ci	unsigned long time_left;
4818c2ecf20Sopenharmony_ci	bool has_unre_flag = dev->pdata->has_unre_flag;
4828c2ecf20Sopenharmony_ci	bool has_alt_cmd = dev->pdata->has_alt_cmd;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/*
4858c2ecf20Sopenharmony_ci	 * WARNING: the TXCOMP bit in the Status Register is NOT a clear on
4868c2ecf20Sopenharmony_ci	 * read flag but shows the state of the transmission at the time the
4878c2ecf20Sopenharmony_ci	 * Status Register is read. According to the programmer datasheet,
4888c2ecf20Sopenharmony_ci	 * TXCOMP is set when both holding register and internal shifter are
4898c2ecf20Sopenharmony_ci	 * empty and STOP condition has been sent.
4908c2ecf20Sopenharmony_ci	 * Consequently, we should enable NACK interrupt rather than TXCOMP to
4918c2ecf20Sopenharmony_ci	 * detect transmission failure.
4928c2ecf20Sopenharmony_ci	 * Indeed let's take the case of an i2c write command using DMA.
4938c2ecf20Sopenharmony_ci	 * Whenever the slave doesn't acknowledge a byte, the LOCK, NACK and
4948c2ecf20Sopenharmony_ci	 * TXCOMP bits are set together into the Status Register.
4958c2ecf20Sopenharmony_ci	 * LOCK is a clear on write bit, which is set to prevent the DMA
4968c2ecf20Sopenharmony_ci	 * controller from sending new data on the i2c bus after a NACK
4978c2ecf20Sopenharmony_ci	 * condition has happened. Once locked, this i2c peripheral stops
4988c2ecf20Sopenharmony_ci	 * triggering the DMA controller for new data but it is more than
4998c2ecf20Sopenharmony_ci	 * likely that a new DMA transaction is already in progress, writing
5008c2ecf20Sopenharmony_ci	 * into the Transmit Holding Register. Since the peripheral is locked,
5018c2ecf20Sopenharmony_ci	 * these new data won't be sent to the i2c bus but they will remain
5028c2ecf20Sopenharmony_ci	 * into the Transmit Holding Register, so TXCOMP bit is cleared.
5038c2ecf20Sopenharmony_ci	 * Then when the interrupt handler is called, the Status Register is
5048c2ecf20Sopenharmony_ci	 * read: the TXCOMP bit is clear but NACK bit is still set. The driver
5058c2ecf20Sopenharmony_ci	 * manage the error properly, without waiting for timeout.
5068c2ecf20Sopenharmony_ci	 * This case can be reproduced easyly when writing into an at24 eeprom.
5078c2ecf20Sopenharmony_ci	 *
5088c2ecf20Sopenharmony_ci	 * Besides, the TXCOMP bit is already set before the i2c transaction
5098c2ecf20Sopenharmony_ci	 * has been started. For read transactions, this bit is cleared when
5108c2ecf20Sopenharmony_ci	 * writing the START bit into the Control Register. So the
5118c2ecf20Sopenharmony_ci	 * corresponding interrupt can safely be enabled just after.
5128c2ecf20Sopenharmony_ci	 * However for write transactions managed by the CPU, we first write
5138c2ecf20Sopenharmony_ci	 * into THR, so TXCOMP is cleared. Then we can safely enable TXCOMP
5148c2ecf20Sopenharmony_ci	 * interrupt. If TXCOMP interrupt were enabled before writing into THR,
5158c2ecf20Sopenharmony_ci	 * the interrupt handler would be called immediately and the i2c command
5168c2ecf20Sopenharmony_ci	 * would be reported as completed.
5178c2ecf20Sopenharmony_ci	 * Also when a write transaction is managed by the DMA controller,
5188c2ecf20Sopenharmony_ci	 * enabling the TXCOMP interrupt in this function may lead to a race
5198c2ecf20Sopenharmony_ci	 * condition since we don't know whether the TXCOMP interrupt is enabled
5208c2ecf20Sopenharmony_ci	 * before or after the DMA has started to write into THR. So the TXCOMP
5218c2ecf20Sopenharmony_ci	 * interrupt is enabled later by at91_twi_write_data_dma_callback().
5228c2ecf20Sopenharmony_ci	 * Immediately after in that DMA callback, if the alternative command
5238c2ecf20Sopenharmony_ci	 * mode is not used, we still need to send the STOP condition manually
5248c2ecf20Sopenharmony_ci	 * writing the corresponding bit into the Control Register.
5258c2ecf20Sopenharmony_ci	 */
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "transfer: %s %zu bytes.\n",
5288c2ecf20Sopenharmony_ci		(dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	reinit_completion(&dev->cmd_complete);
5318c2ecf20Sopenharmony_ci	dev->transfer_status = 0;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Clear pending interrupts, such as NACK. */
5348c2ecf20Sopenharmony_ci	at91_twi_read(dev, AT91_TWI_SR);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	if (dev->fifo_size) {
5378c2ecf20Sopenharmony_ci		unsigned fifo_mr = at91_twi_read(dev, AT91_TWI_FMR);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci		/* Reset FIFO mode register */
5408c2ecf20Sopenharmony_ci		fifo_mr &= ~(AT91_TWI_FMR_TXRDYM_MASK |
5418c2ecf20Sopenharmony_ci			     AT91_TWI_FMR_RXRDYM_MASK);
5428c2ecf20Sopenharmony_ci		fifo_mr |= AT91_TWI_FMR_TXRDYM(AT91_TWI_ONE_DATA);
5438c2ecf20Sopenharmony_ci		fifo_mr |= AT91_TWI_FMR_RXRDYM(AT91_TWI_ONE_DATA);
5448c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_FMR, fifo_mr);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		/* Flush FIFOs */
5478c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR,
5488c2ecf20Sopenharmony_ci			       AT91_TWI_THRCLR | AT91_TWI_RHRCLR);
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	if (!dev->buf_len) {
5528c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_QUICK);
5538c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_TXCOMP);
5548c2ecf20Sopenharmony_ci	} else if (dev->msg->flags & I2C_M_RD) {
5558c2ecf20Sopenharmony_ci		unsigned start_flags = AT91_TWI_START;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		/* if only one byte is to be read, immediately stop transfer */
5588c2ecf20Sopenharmony_ci		if (!dev->use_alt_cmd && dev->buf_len <= 1 &&
5598c2ecf20Sopenharmony_ci		    !(dev->msg->flags & I2C_M_RECV_LEN))
5608c2ecf20Sopenharmony_ci			start_flags |= AT91_TWI_STOP;
5618c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, start_flags);
5628c2ecf20Sopenharmony_ci		/*
5638c2ecf20Sopenharmony_ci		 * When using dma without alternative command mode, the last
5648c2ecf20Sopenharmony_ci		 * byte has to be read manually in order to not send the stop
5658c2ecf20Sopenharmony_ci		 * command too late and then to receive extra data.
5668c2ecf20Sopenharmony_ci		 * In practice, there are some issues if you use the dma to
5678c2ecf20Sopenharmony_ci		 * read n-1 bytes because of latency.
5688c2ecf20Sopenharmony_ci		 * Reading n-2 bytes with dma and the two last ones manually
5698c2ecf20Sopenharmony_ci		 * seems to be the best solution.
5708c2ecf20Sopenharmony_ci		 */
5718c2ecf20Sopenharmony_ci		if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
5728c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
5738c2ecf20Sopenharmony_ci			at91_twi_read_data_dma(dev);
5748c2ecf20Sopenharmony_ci		} else {
5758c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_IER,
5768c2ecf20Sopenharmony_ci				       AT91_TWI_TXCOMP |
5778c2ecf20Sopenharmony_ci				       AT91_TWI_NACK |
5788c2ecf20Sopenharmony_ci				       AT91_TWI_RXRDY);
5798c2ecf20Sopenharmony_ci		}
5808c2ecf20Sopenharmony_ci	} else {
5818c2ecf20Sopenharmony_ci		if (dev->use_dma && (dev->buf_len > AT91_I2C_DMA_THRESHOLD)) {
5828c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_IER, AT91_TWI_NACK);
5838c2ecf20Sopenharmony_ci			at91_twi_write_data_dma(dev);
5848c2ecf20Sopenharmony_ci		} else {
5858c2ecf20Sopenharmony_ci			at91_twi_write_next_byte(dev);
5868c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_IER,
5878c2ecf20Sopenharmony_ci				       AT91_TWI_TXCOMP | AT91_TWI_NACK |
5888c2ecf20Sopenharmony_ci				       (dev->buf_len ? AT91_TWI_TXRDY : 0));
5898c2ecf20Sopenharmony_ci		}
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	time_left = wait_for_completion_timeout(&dev->cmd_complete,
5938c2ecf20Sopenharmony_ci					      dev->adapter.timeout);
5948c2ecf20Sopenharmony_ci	if (time_left == 0) {
5958c2ecf20Sopenharmony_ci		dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
5968c2ecf20Sopenharmony_ci		dev_err(dev->dev, "controller timed out\n");
5978c2ecf20Sopenharmony_ci		at91_init_twi_bus(dev);
5988c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
5998c2ecf20Sopenharmony_ci		goto error;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci	if (dev->transfer_status & AT91_TWI_NACK) {
6028c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "received nack\n");
6038c2ecf20Sopenharmony_ci		ret = -EREMOTEIO;
6048c2ecf20Sopenharmony_ci		goto error;
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci	if (dev->transfer_status & AT91_TWI_OVRE) {
6078c2ecf20Sopenharmony_ci		dev_err(dev->dev, "overrun while reading\n");
6088c2ecf20Sopenharmony_ci		ret = -EIO;
6098c2ecf20Sopenharmony_ci		goto error;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci	if (has_unre_flag && dev->transfer_status & AT91_TWI_UNRE) {
6128c2ecf20Sopenharmony_ci		dev_err(dev->dev, "underrun while writing\n");
6138c2ecf20Sopenharmony_ci		ret = -EIO;
6148c2ecf20Sopenharmony_ci		goto error;
6158c2ecf20Sopenharmony_ci	}
6168c2ecf20Sopenharmony_ci	if ((has_alt_cmd || dev->fifo_size) &&
6178c2ecf20Sopenharmony_ci	    (dev->transfer_status & AT91_TWI_LOCK)) {
6188c2ecf20Sopenharmony_ci		dev_err(dev->dev, "tx locked\n");
6198c2ecf20Sopenharmony_ci		ret = -EIO;
6208c2ecf20Sopenharmony_ci		goto error;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci	if (dev->recv_len_abort) {
6238c2ecf20Sopenharmony_ci		dev_err(dev->dev, "invalid smbus block length recvd\n");
6248c2ecf20Sopenharmony_ci		ret = -EPROTO;
6258c2ecf20Sopenharmony_ci		goto error;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	dev_dbg(dev->dev, "transfer complete\n");
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return 0;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cierror:
6338c2ecf20Sopenharmony_ci	/* first stop DMA transfer if still in progress */
6348c2ecf20Sopenharmony_ci	at91_twi_dma_cleanup(dev);
6358c2ecf20Sopenharmony_ci	/* then flush THR/FIFO and unlock TX if locked */
6368c2ecf20Sopenharmony_ci	if ((has_alt_cmd || dev->fifo_size) &&
6378c2ecf20Sopenharmony_ci	    (dev->transfer_status & AT91_TWI_LOCK)) {
6388c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "unlock tx\n");
6398c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR,
6408c2ecf20Sopenharmony_ci			       AT91_TWI_THRCLR | AT91_TWI_LOCKCLR);
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	/*
6448c2ecf20Sopenharmony_ci	 * some faulty I2C slave devices might hold SDA down;
6458c2ecf20Sopenharmony_ci	 * we can send a bus clear command, hoping that the pins will be
6468c2ecf20Sopenharmony_ci	 * released
6478c2ecf20Sopenharmony_ci	 */
6488c2ecf20Sopenharmony_ci	i2c_recover_bus(&dev->adapter);
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	return ret;
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_cistatic int at91_twi_xfer(struct i2c_adapter *adap, struct i2c_msg *msg, int num)
6548c2ecf20Sopenharmony_ci{
6558c2ecf20Sopenharmony_ci	struct at91_twi_dev *dev = i2c_get_adapdata(adap);
6568c2ecf20Sopenharmony_ci	int ret;
6578c2ecf20Sopenharmony_ci	unsigned int_addr_flag = 0;
6588c2ecf20Sopenharmony_ci	struct i2c_msg *m_start = msg;
6598c2ecf20Sopenharmony_ci	bool is_read;
6608c2ecf20Sopenharmony_ci	u8 *dma_buf = NULL;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	dev_dbg(&adap->dev, "at91_xfer: processing %d messages:\n", num);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(dev->dev);
6658c2ecf20Sopenharmony_ci	if (ret < 0)
6668c2ecf20Sopenharmony_ci		goto out;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	if (num == 2) {
6698c2ecf20Sopenharmony_ci		int internal_address = 0;
6708c2ecf20Sopenharmony_ci		int i;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		/* 1st msg is put into the internal address, start with 2nd */
6738c2ecf20Sopenharmony_ci		m_start = &msg[1];
6748c2ecf20Sopenharmony_ci		for (i = 0; i < msg->len; ++i) {
6758c2ecf20Sopenharmony_ci			const unsigned addr = msg->buf[msg->len - 1 - i];
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci			internal_address |= addr << (8 * i);
6788c2ecf20Sopenharmony_ci			int_addr_flag += AT91_TWI_IADRSZ_1;
6798c2ecf20Sopenharmony_ci		}
6808c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_IADR, internal_address);
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	dev->use_alt_cmd = false;
6848c2ecf20Sopenharmony_ci	is_read = (m_start->flags & I2C_M_RD);
6858c2ecf20Sopenharmony_ci	if (dev->pdata->has_alt_cmd) {
6868c2ecf20Sopenharmony_ci		if (m_start->len > 0 &&
6878c2ecf20Sopenharmony_ci		    m_start->len < AT91_I2C_MAX_ALT_CMD_DATA_SIZE) {
6888c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMEN);
6898c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_ACR,
6908c2ecf20Sopenharmony_ci				       AT91_TWI_ACR_DATAL(m_start->len) |
6918c2ecf20Sopenharmony_ci				       ((is_read) ? AT91_TWI_ACR_DIR : 0));
6928c2ecf20Sopenharmony_ci			dev->use_alt_cmd = true;
6938c2ecf20Sopenharmony_ci		} else {
6948c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_ACMDIS);
6958c2ecf20Sopenharmony_ci		}
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	at91_twi_write(dev, AT91_TWI_MMR,
6998c2ecf20Sopenharmony_ci		       (m_start->addr << 16) |
7008c2ecf20Sopenharmony_ci		       int_addr_flag |
7018c2ecf20Sopenharmony_ci		       ((!dev->use_alt_cmd && is_read) ? AT91_TWI_MREAD : 0));
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	dev->buf_len = m_start->len;
7048c2ecf20Sopenharmony_ci	dev->buf = m_start->buf;
7058c2ecf20Sopenharmony_ci	dev->msg = m_start;
7068c2ecf20Sopenharmony_ci	dev->recv_len_abort = false;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (dev->use_dma) {
7098c2ecf20Sopenharmony_ci		dma_buf = i2c_get_dma_safe_msg_buf(m_start, 1);
7108c2ecf20Sopenharmony_ci		if (!dma_buf) {
7118c2ecf20Sopenharmony_ci			ret = -ENOMEM;
7128c2ecf20Sopenharmony_ci			goto out;
7138c2ecf20Sopenharmony_ci		}
7148c2ecf20Sopenharmony_ci		dev->buf = dma_buf;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	ret = at91_do_twi_transfer(dev);
7188c2ecf20Sopenharmony_ci	i2c_put_dma_safe_msg_buf(dma_buf, m_start, !ret);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	ret = (ret < 0) ? ret : num;
7218c2ecf20Sopenharmony_ciout:
7228c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(dev->dev);
7238c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(dev->dev);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	return ret;
7268c2ecf20Sopenharmony_ci}
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci/*
7298c2ecf20Sopenharmony_ci * The hardware can handle at most two messages concatenated by a
7308c2ecf20Sopenharmony_ci * repeated start via it's internal address feature.
7318c2ecf20Sopenharmony_ci */
7328c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks at91_twi_quirks = {
7338c2ecf20Sopenharmony_ci	.flags = I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | I2C_AQ_COMB_SAME_ADDR,
7348c2ecf20Sopenharmony_ci	.max_comb_1st_msg_len = 3,
7358c2ecf20Sopenharmony_ci};
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_cistatic u32 at91_twi_func(struct i2c_adapter *adapter)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
7408c2ecf20Sopenharmony_ci		| I2C_FUNC_SMBUS_READ_BLOCK_DATA;
7418c2ecf20Sopenharmony_ci}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cistatic const struct i2c_algorithm at91_twi_algorithm = {
7448c2ecf20Sopenharmony_ci	.master_xfer	= at91_twi_xfer,
7458c2ecf20Sopenharmony_ci	.functionality	= at91_twi_func,
7468c2ecf20Sopenharmony_ci};
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_cistatic int at91_twi_configure_dma(struct at91_twi_dev *dev, u32 phy_addr)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	int ret = 0;
7518c2ecf20Sopenharmony_ci	struct dma_slave_config slave_config;
7528c2ecf20Sopenharmony_ci	struct at91_twi_dma *dma = &dev->dma;
7538c2ecf20Sopenharmony_ci	enum dma_slave_buswidth addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	/*
7568c2ecf20Sopenharmony_ci	 * The actual width of the access will be chosen in
7578c2ecf20Sopenharmony_ci	 * dmaengine_prep_slave_sg():
7588c2ecf20Sopenharmony_ci	 * for each buffer in the scatter-gather list, if its size is aligned
7598c2ecf20Sopenharmony_ci	 * to addr_width then addr_width accesses will be performed to transfer
7608c2ecf20Sopenharmony_ci	 * the buffer. On the other hand, if the buffer size is not aligned to
7618c2ecf20Sopenharmony_ci	 * addr_width then the buffer is transferred using single byte accesses.
7628c2ecf20Sopenharmony_ci	 * Please refer to the Atmel eXtended DMA controller driver.
7638c2ecf20Sopenharmony_ci	 * When FIFOs are used, the TXRDYM threshold can always be set to
7648c2ecf20Sopenharmony_ci	 * trigger the XDMAC when at least 4 data can be written into the TX
7658c2ecf20Sopenharmony_ci	 * FIFO, even if single byte accesses are performed.
7668c2ecf20Sopenharmony_ci	 * However the RXRDYM threshold must be set to fit the access width,
7678c2ecf20Sopenharmony_ci	 * deduced from buffer length, so the XDMAC is triggered properly to
7688c2ecf20Sopenharmony_ci	 * read data from the RX FIFO.
7698c2ecf20Sopenharmony_ci	 */
7708c2ecf20Sopenharmony_ci	if (dev->fifo_size)
7718c2ecf20Sopenharmony_ci		addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	memset(&slave_config, 0, sizeof(slave_config));
7748c2ecf20Sopenharmony_ci	slave_config.src_addr = (dma_addr_t)phy_addr + AT91_TWI_RHR;
7758c2ecf20Sopenharmony_ci	slave_config.src_addr_width = addr_width;
7768c2ecf20Sopenharmony_ci	slave_config.src_maxburst = 1;
7778c2ecf20Sopenharmony_ci	slave_config.dst_addr = (dma_addr_t)phy_addr + AT91_TWI_THR;
7788c2ecf20Sopenharmony_ci	slave_config.dst_addr_width = addr_width;
7798c2ecf20Sopenharmony_ci	slave_config.dst_maxburst = 1;
7808c2ecf20Sopenharmony_ci	slave_config.device_fc = false;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	dma->chan_tx = dma_request_chan(dev->dev, "tx");
7838c2ecf20Sopenharmony_ci	if (IS_ERR(dma->chan_tx)) {
7848c2ecf20Sopenharmony_ci		ret = PTR_ERR(dma->chan_tx);
7858c2ecf20Sopenharmony_ci		dma->chan_tx = NULL;
7868c2ecf20Sopenharmony_ci		goto error;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	dma->chan_rx = dma_request_chan(dev->dev, "rx");
7908c2ecf20Sopenharmony_ci	if (IS_ERR(dma->chan_rx)) {
7918c2ecf20Sopenharmony_ci		ret = PTR_ERR(dma->chan_rx);
7928c2ecf20Sopenharmony_ci		dma->chan_rx = NULL;
7938c2ecf20Sopenharmony_ci		goto error;
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	slave_config.direction = DMA_MEM_TO_DEV;
7978c2ecf20Sopenharmony_ci	if (dmaengine_slave_config(dma->chan_tx, &slave_config)) {
7988c2ecf20Sopenharmony_ci		dev_err(dev->dev, "failed to configure tx channel\n");
7998c2ecf20Sopenharmony_ci		ret = -EINVAL;
8008c2ecf20Sopenharmony_ci		goto error;
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	slave_config.direction = DMA_DEV_TO_MEM;
8048c2ecf20Sopenharmony_ci	if (dmaengine_slave_config(dma->chan_rx, &slave_config)) {
8058c2ecf20Sopenharmony_ci		dev_err(dev->dev, "failed to configure rx channel\n");
8068c2ecf20Sopenharmony_ci		ret = -EINVAL;
8078c2ecf20Sopenharmony_ci		goto error;
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	sg_init_table(dma->sg, 2);
8118c2ecf20Sopenharmony_ci	dma->buf_mapped = false;
8128c2ecf20Sopenharmony_ci	dma->xfer_in_progress = false;
8138c2ecf20Sopenharmony_ci	dev->use_dma = true;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	dev_info(dev->dev, "using %s (tx) and %s (rx) for DMA transfers\n",
8168c2ecf20Sopenharmony_ci		 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	return ret;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cierror:
8218c2ecf20Sopenharmony_ci	if (ret != -EPROBE_DEFER)
8228c2ecf20Sopenharmony_ci		dev_info(dev->dev, "can't get DMA channel, continue without DMA support\n");
8238c2ecf20Sopenharmony_ci	if (dma->chan_rx)
8248c2ecf20Sopenharmony_ci		dma_release_channel(dma->chan_rx);
8258c2ecf20Sopenharmony_ci	if (dma->chan_tx)
8268c2ecf20Sopenharmony_ci		dma_release_channel(dma->chan_tx);
8278c2ecf20Sopenharmony_ci	return ret;
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic int at91_init_twi_recovery_gpio(struct platform_device *pdev,
8318c2ecf20Sopenharmony_ci				       struct at91_twi_dev *dev)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci	struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	rinfo->pinctrl = devm_pinctrl_get(&pdev->dev);
8368c2ecf20Sopenharmony_ci	if (!rinfo->pinctrl || IS_ERR(rinfo->pinctrl)) {
8378c2ecf20Sopenharmony_ci		dev_info(dev->dev, "can't get pinctrl, bus recovery not supported\n");
8388c2ecf20Sopenharmony_ci		return PTR_ERR(rinfo->pinctrl);
8398c2ecf20Sopenharmony_ci	}
8408c2ecf20Sopenharmony_ci	dev->adapter.bus_recovery_info = rinfo;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	return 0;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic int at91_twi_recover_bus_cmd(struct i2c_adapter *adap)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct at91_twi_dev *dev = i2c_get_adapdata(adap);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	dev->transfer_status |= at91_twi_read(dev, AT91_TWI_SR);
8508c2ecf20Sopenharmony_ci	if (!(dev->transfer_status & AT91_TWI_SDA)) {
8518c2ecf20Sopenharmony_ci		dev_dbg(dev->dev, "SDA is down; sending bus clear command\n");
8528c2ecf20Sopenharmony_ci		if (dev->use_alt_cmd) {
8538c2ecf20Sopenharmony_ci			unsigned int acr;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci			acr = at91_twi_read(dev, AT91_TWI_ACR);
8568c2ecf20Sopenharmony_ci			acr &= ~AT91_TWI_ACR_DATAL_MASK;
8578c2ecf20Sopenharmony_ci			at91_twi_write(dev, AT91_TWI_ACR, acr);
8588c2ecf20Sopenharmony_ci		}
8598c2ecf20Sopenharmony_ci		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_CLEAR);
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	return 0;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic int at91_init_twi_recovery_info(struct platform_device *pdev,
8668c2ecf20Sopenharmony_ci				       struct at91_twi_dev *dev)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
8698c2ecf20Sopenharmony_ci	bool has_clear_cmd = dev->pdata->has_clear_cmd;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (!has_clear_cmd)
8728c2ecf20Sopenharmony_ci		return at91_init_twi_recovery_gpio(pdev, dev);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	rinfo->recover_bus = at91_twi_recover_bus_cmd;
8758c2ecf20Sopenharmony_ci	dev->adapter.bus_recovery_info = rinfo;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	return 0;
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ciint at91_twi_probe_master(struct platform_device *pdev,
8818c2ecf20Sopenharmony_ci			  u32 phy_addr, struct at91_twi_dev *dev)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	int rc;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	init_completion(&dev->cmd_complete);
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
8888c2ecf20Sopenharmony_ci			      dev_name(dev->dev), dev);
8898c2ecf20Sopenharmony_ci	if (rc) {
8908c2ecf20Sopenharmony_ci		dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
8918c2ecf20Sopenharmony_ci		return rc;
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	if (dev->dev->of_node) {
8958c2ecf20Sopenharmony_ci		rc = at91_twi_configure_dma(dev, phy_addr);
8968c2ecf20Sopenharmony_ci		if (rc == -EPROBE_DEFER)
8978c2ecf20Sopenharmony_ci			return rc;
8988c2ecf20Sopenharmony_ci	}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
9018c2ecf20Sopenharmony_ci				  &dev->fifo_size)) {
9028c2ecf20Sopenharmony_ci		dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	dev->enable_dig_filt = of_property_read_bool(pdev->dev.of_node,
9068c2ecf20Sopenharmony_ci						     "i2c-digital-filter");
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	dev->enable_ana_filt = of_property_read_bool(pdev->dev.of_node,
9098c2ecf20Sopenharmony_ci						     "i2c-analog-filter");
9108c2ecf20Sopenharmony_ci	at91_calc_twi_clock(dev);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	rc = at91_init_twi_recovery_info(pdev, dev);
9138c2ecf20Sopenharmony_ci	if (rc == -EPROBE_DEFER)
9148c2ecf20Sopenharmony_ci		return rc;
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	dev->adapter.algo = &at91_twi_algorithm;
9178c2ecf20Sopenharmony_ci	dev->adapter.quirks = &at91_twi_quirks;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	return 0;
9208c2ecf20Sopenharmony_ci}
921