18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * i2c-xiic.c
48c2ecf20Sopenharmony_ci * Copyright (c) 2002-2007 Xilinx Inc.
58c2ecf20Sopenharmony_ci * Copyright (c) 2009-2010 Intel Corporation
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This code was implemented by Mocean Laboratories AB when porting linux
88c2ecf20Sopenharmony_ci * to the automotive development board Russellville. The copyright holder
98c2ecf20Sopenharmony_ci * as seen in the header is Intel corporation.
108c2ecf20Sopenharmony_ci * Mocean Laboratories forked off the GNU/Linux platform work into a
118c2ecf20Sopenharmony_ci * separate company called Pelagicore AB, which committed the code to the
128c2ecf20Sopenharmony_ci * kernel.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* Supports:
168c2ecf20Sopenharmony_ci * Xilinx IIC
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/errno.h>
218c2ecf20Sopenharmony_ci#include <linux/err.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
248c2ecf20Sopenharmony_ci#include <linux/i2c.h>
258c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
268c2ecf20Sopenharmony_ci#include <linux/wait.h>
278c2ecf20Sopenharmony_ci#include <linux/platform_data/i2c-xiic.h>
288c2ecf20Sopenharmony_ci#include <linux/io.h>
298c2ecf20Sopenharmony_ci#include <linux/slab.h>
308c2ecf20Sopenharmony_ci#include <linux/of.h>
318c2ecf20Sopenharmony_ci#include <linux/clk.h>
328c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define DRIVER_NAME "xiic-i2c"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cienum xilinx_i2c_state {
378c2ecf20Sopenharmony_ci	STATE_DONE,
388c2ecf20Sopenharmony_ci	STATE_ERROR,
398c2ecf20Sopenharmony_ci	STATE_START
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cienum xiic_endian {
438c2ecf20Sopenharmony_ci	LITTLE,
448c2ecf20Sopenharmony_ci	BIG
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/**
488c2ecf20Sopenharmony_ci * struct xiic_i2c - Internal representation of the XIIC I2C bus
498c2ecf20Sopenharmony_ci * @dev: Pointer to device structure
508c2ecf20Sopenharmony_ci * @base: Memory base of the HW registers
518c2ecf20Sopenharmony_ci * @wait: Wait queue for callers
528c2ecf20Sopenharmony_ci * @adap: Kernel adapter representation
538c2ecf20Sopenharmony_ci * @tx_msg: Messages from above to be sent
548c2ecf20Sopenharmony_ci * @lock: Mutual exclusion
558c2ecf20Sopenharmony_ci * @tx_pos: Current pos in TX message
568c2ecf20Sopenharmony_ci * @nmsgs: Number of messages in tx_msg
578c2ecf20Sopenharmony_ci * @rx_msg: Current RX message
588c2ecf20Sopenharmony_ci * @rx_pos: Position within current RX message
598c2ecf20Sopenharmony_ci * @endianness: big/little-endian byte order
608c2ecf20Sopenharmony_ci * @clk: Pointer to AXI4-lite input clock
618c2ecf20Sopenharmony_ci * @state: See STATE_
628c2ecf20Sopenharmony_ci * @singlemaster: Indicates bus is single master
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_cistruct xiic_i2c {
658c2ecf20Sopenharmony_ci	struct device *dev;
668c2ecf20Sopenharmony_ci	void __iomem *base;
678c2ecf20Sopenharmony_ci	wait_queue_head_t wait;
688c2ecf20Sopenharmony_ci	struct i2c_adapter adap;
698c2ecf20Sopenharmony_ci	struct i2c_msg *tx_msg;
708c2ecf20Sopenharmony_ci	struct mutex lock;
718c2ecf20Sopenharmony_ci	unsigned int tx_pos;
728c2ecf20Sopenharmony_ci	unsigned int nmsgs;
738c2ecf20Sopenharmony_ci	struct i2c_msg *rx_msg;
748c2ecf20Sopenharmony_ci	int rx_pos;
758c2ecf20Sopenharmony_ci	enum xiic_endian endianness;
768c2ecf20Sopenharmony_ci	struct clk *clk;
778c2ecf20Sopenharmony_ci	enum xilinx_i2c_state state;
788c2ecf20Sopenharmony_ci	bool singlemaster;
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define XIIC_MSB_OFFSET 0
838c2ecf20Sopenharmony_ci#define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Register offsets in bytes from RegisterBase. Three is added to the
878c2ecf20Sopenharmony_ci * base offset to access LSB (IBM style) of the word
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci#define XIIC_CR_REG_OFFSET   (0x00+XIIC_REG_OFFSET)	/* Control Register   */
908c2ecf20Sopenharmony_ci#define XIIC_SR_REG_OFFSET   (0x04+XIIC_REG_OFFSET)	/* Status Register    */
918c2ecf20Sopenharmony_ci#define XIIC_DTR_REG_OFFSET  (0x08+XIIC_REG_OFFSET)	/* Data Tx Register   */
928c2ecf20Sopenharmony_ci#define XIIC_DRR_REG_OFFSET  (0x0C+XIIC_REG_OFFSET)	/* Data Rx Register   */
938c2ecf20Sopenharmony_ci#define XIIC_ADR_REG_OFFSET  (0x10+XIIC_REG_OFFSET)	/* Address Register   */
948c2ecf20Sopenharmony_ci#define XIIC_TFO_REG_OFFSET  (0x14+XIIC_REG_OFFSET)	/* Tx FIFO Occupancy  */
958c2ecf20Sopenharmony_ci#define XIIC_RFO_REG_OFFSET  (0x18+XIIC_REG_OFFSET)	/* Rx FIFO Occupancy  */
968c2ecf20Sopenharmony_ci#define XIIC_TBA_REG_OFFSET  (0x1C+XIIC_REG_OFFSET)	/* 10 Bit Address reg */
978c2ecf20Sopenharmony_ci#define XIIC_RFD_REG_OFFSET  (0x20+XIIC_REG_OFFSET)	/* Rx FIFO Depth reg  */
988c2ecf20Sopenharmony_ci#define XIIC_GPO_REG_OFFSET  (0x24+XIIC_REG_OFFSET)	/* Output Register    */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Control Register masks */
1018c2ecf20Sopenharmony_ci#define XIIC_CR_ENABLE_DEVICE_MASK        0x01	/* Device enable = 1      */
1028c2ecf20Sopenharmony_ci#define XIIC_CR_TX_FIFO_RESET_MASK        0x02	/* Transmit FIFO reset=1  */
1038c2ecf20Sopenharmony_ci#define XIIC_CR_MSMS_MASK                 0x04	/* Master starts Txing=1  */
1048c2ecf20Sopenharmony_ci#define XIIC_CR_DIR_IS_TX_MASK            0x08	/* Dir of tx. Txing=1     */
1058c2ecf20Sopenharmony_ci#define XIIC_CR_NO_ACK_MASK               0x10	/* Tx Ack. NO ack = 1     */
1068c2ecf20Sopenharmony_ci#define XIIC_CR_REPEATED_START_MASK       0x20	/* Repeated start = 1     */
1078c2ecf20Sopenharmony_ci#define XIIC_CR_GENERAL_CALL_MASK         0x40	/* Gen Call enabled = 1   */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Status Register masks */
1108c2ecf20Sopenharmony_ci#define XIIC_SR_GEN_CALL_MASK             0x01	/* 1=a mstr issued a GC   */
1118c2ecf20Sopenharmony_ci#define XIIC_SR_ADDR_AS_SLAVE_MASK        0x02	/* 1=when addr as slave   */
1128c2ecf20Sopenharmony_ci#define XIIC_SR_BUS_BUSY_MASK             0x04	/* 1 = bus is busy        */
1138c2ecf20Sopenharmony_ci#define XIIC_SR_MSTR_RDING_SLAVE_MASK     0x08	/* 1=Dir: mstr <-- slave  */
1148c2ecf20Sopenharmony_ci#define XIIC_SR_TX_FIFO_FULL_MASK         0x10	/* 1 = Tx FIFO full       */
1158c2ecf20Sopenharmony_ci#define XIIC_SR_RX_FIFO_FULL_MASK         0x20	/* 1 = Rx FIFO full       */
1168c2ecf20Sopenharmony_ci#define XIIC_SR_RX_FIFO_EMPTY_MASK        0x40	/* 1 = Rx FIFO empty      */
1178c2ecf20Sopenharmony_ci#define XIIC_SR_TX_FIFO_EMPTY_MASK        0x80	/* 1 = Tx FIFO empty      */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* Interrupt Status Register masks    Interrupt occurs when...       */
1208c2ecf20Sopenharmony_ci#define XIIC_INTR_ARB_LOST_MASK           0x01	/* 1 = arbitration lost   */
1218c2ecf20Sopenharmony_ci#define XIIC_INTR_TX_ERROR_MASK           0x02	/* 1=Tx error/msg complete */
1228c2ecf20Sopenharmony_ci#define XIIC_INTR_TX_EMPTY_MASK           0x04	/* 1 = Tx FIFO/reg empty  */
1238c2ecf20Sopenharmony_ci#define XIIC_INTR_RX_FULL_MASK            0x08	/* 1=Rx FIFO/reg=OCY level */
1248c2ecf20Sopenharmony_ci#define XIIC_INTR_BNB_MASK                0x10	/* 1 = Bus not busy       */
1258c2ecf20Sopenharmony_ci#define XIIC_INTR_AAS_MASK                0x20	/* 1 = when addr as slave */
1268c2ecf20Sopenharmony_ci#define XIIC_INTR_NAAS_MASK               0x40	/* 1 = not addr as slave  */
1278c2ecf20Sopenharmony_ci#define XIIC_INTR_TX_HALF_MASK            0x80	/* 1 = TX FIFO half empty */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* The following constants specify the depth of the FIFOs */
1308c2ecf20Sopenharmony_ci#define IIC_RX_FIFO_DEPTH         16	/* Rx fifo capacity               */
1318c2ecf20Sopenharmony_ci#define IIC_TX_FIFO_DEPTH         16	/* Tx fifo capacity               */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/* The following constants specify groups of interrupts that are typically
1348c2ecf20Sopenharmony_ci * enabled or disables at the same time
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ci#define XIIC_TX_INTERRUPTS                           \
1378c2ecf20Sopenharmony_ci(XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/*
1428c2ecf20Sopenharmony_ci * Tx Fifo upper bit masks.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ci#define XIIC_TX_DYN_START_MASK            0x0100 /* 1 = Set dynamic start */
1458c2ecf20Sopenharmony_ci#define XIIC_TX_DYN_STOP_MASK             0x0200 /* 1 = Set dynamic stop */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/*
1488c2ecf20Sopenharmony_ci * The following constants define the register offsets for the Interrupt
1498c2ecf20Sopenharmony_ci * registers. There are some holes in the memory map for reserved addresses
1508c2ecf20Sopenharmony_ci * to allow other registers to be added and still match the memory map of the
1518c2ecf20Sopenharmony_ci * interrupt controller registers
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_ci#define XIIC_DGIER_OFFSET    0x1C /* Device Global Interrupt Enable Register */
1548c2ecf20Sopenharmony_ci#define XIIC_IISR_OFFSET     0x20 /* Interrupt Status Register */
1558c2ecf20Sopenharmony_ci#define XIIC_IIER_OFFSET     0x28 /* Interrupt Enable Register */
1568c2ecf20Sopenharmony_ci#define XIIC_RESETR_OFFSET   0x40 /* Reset Register */
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define XIIC_RESET_MASK             0xAUL
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define XIIC_PM_TIMEOUT		1000	/* ms */
1618c2ecf20Sopenharmony_ci/* timeout waiting for the controller to respond */
1628c2ecf20Sopenharmony_ci#define XIIC_I2C_TIMEOUT	(msecs_to_jiffies(1000))
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * The following constant is used for the device global interrupt enable
1658c2ecf20Sopenharmony_ci * register, to enable all interrupts for the device, this is the only bit
1668c2ecf20Sopenharmony_ci * in the register
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ci#define XIIC_GINTR_ENABLE_MASK      0x80000000UL
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
1718c2ecf20Sopenharmony_ci#define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int xiic_start_xfer(struct xiic_i2c *i2c);
1748c2ecf20Sopenharmony_cistatic void __xiic_start_xfer(struct xiic_i2c *i2c);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/*
1778c2ecf20Sopenharmony_ci * For the register read and write functions, a little-endian and big-endian
1788c2ecf20Sopenharmony_ci * version are necessary. Endianness is detected during the probe function.
1798c2ecf20Sopenharmony_ci * Only the least significant byte [doublet] of the register are ever
1808c2ecf20Sopenharmony_ci * accessed. This requires an offset of 3 [2] from the base address for
1818c2ecf20Sopenharmony_ci * big-endian systems.
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	if (i2c->endianness == LITTLE)
1878c2ecf20Sopenharmony_ci		iowrite8(value, i2c->base + reg);
1888c2ecf20Sopenharmony_ci	else
1898c2ecf20Sopenharmony_ci		iowrite8(value, i2c->base + reg + 3);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	u8 ret;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (i2c->endianness == LITTLE)
1978c2ecf20Sopenharmony_ci		ret = ioread8(i2c->base + reg);
1988c2ecf20Sopenharmony_ci	else
1998c2ecf20Sopenharmony_ci		ret = ioread8(i2c->base + reg + 3);
2008c2ecf20Sopenharmony_ci	return ret;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	if (i2c->endianness == LITTLE)
2068c2ecf20Sopenharmony_ci		iowrite16(value, i2c->base + reg);
2078c2ecf20Sopenharmony_ci	else
2088c2ecf20Sopenharmony_ci		iowrite16be(value, i2c->base + reg + 2);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	if (i2c->endianness == LITTLE)
2148c2ecf20Sopenharmony_ci		iowrite32(value, i2c->base + reg);
2158c2ecf20Sopenharmony_ci	else
2168c2ecf20Sopenharmony_ci		iowrite32be(value, i2c->base + reg);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	u32 ret;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (i2c->endianness == LITTLE)
2248c2ecf20Sopenharmony_ci		ret = ioread32(i2c->base + reg);
2258c2ecf20Sopenharmony_ci	else
2268c2ecf20Sopenharmony_ci		ret = ioread32be(i2c->base + reg);
2278c2ecf20Sopenharmony_ci	return ret;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
2338c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
2398c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
2458c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	xiic_irq_clr(i2c, mask);
2518c2ecf20Sopenharmony_ci	xiic_irq_en(i2c, mask);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int xiic_clear_rx_fifo(struct xiic_i2c *i2c)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	u8 sr;
2578c2ecf20Sopenharmony_ci	unsigned long timeout;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	timeout = jiffies + XIIC_I2C_TIMEOUT;
2608c2ecf20Sopenharmony_ci	for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
2618c2ecf20Sopenharmony_ci		!(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);
2628c2ecf20Sopenharmony_ci		sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)) {
2638c2ecf20Sopenharmony_ci		xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
2648c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
2658c2ecf20Sopenharmony_ci			dev_err(i2c->dev, "Failed to clear rx fifo\n");
2668c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return 0;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic int xiic_reinit(struct xiic_i2c *i2c)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	int ret;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* Set receive Fifo depth to maximum (zero based). */
2808c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/* Reset Tx Fifo. */
2838c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* Enable IIC Device, remove Tx Fifo reset & disable general call. */
2868c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* make sure RX fifo is empty */
2898c2ecf20Sopenharmony_ci	ret = xiic_clear_rx_fifo(i2c);
2908c2ecf20Sopenharmony_ci	if (ret)
2918c2ecf20Sopenharmony_ci		return ret;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	/* Enable interrupts */
2948c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	xiic_irq_clr_en(i2c, XIIC_INTR_ARB_LOST_MASK);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic void xiic_deinit(struct xiic_i2c *i2c)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	u8 cr;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* Disable IIC Device. */
3088c2ecf20Sopenharmony_ci	cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
3098c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic void xiic_read_rx(struct xiic_i2c *i2c)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	u8 bytes_in_fifo;
3158c2ecf20Sopenharmony_ci	int i;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent,
3208c2ecf20Sopenharmony_ci		"%s entry, bytes in fifo: %d, msg: %d, SR: 0x%x, CR: 0x%x\n",
3218c2ecf20Sopenharmony_ci		__func__, bytes_in_fifo, xiic_rx_space(i2c),
3228c2ecf20Sopenharmony_ci		xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
3238c2ecf20Sopenharmony_ci		xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	if (bytes_in_fifo > xiic_rx_space(i2c))
3268c2ecf20Sopenharmony_ci		bytes_in_fifo = xiic_rx_space(i2c);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	for (i = 0; i < bytes_in_fifo; i++)
3298c2ecf20Sopenharmony_ci		i2c->rx_msg->buf[i2c->rx_pos++] =
3308c2ecf20Sopenharmony_ci			xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET,
3338c2ecf20Sopenharmony_ci		(xiic_rx_space(i2c) > IIC_RX_FIFO_DEPTH) ?
3348c2ecf20Sopenharmony_ci		IIC_RX_FIFO_DEPTH - 1 :  xiic_rx_space(i2c) - 1);
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int xiic_tx_fifo_space(struct xiic_i2c *i2c)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	/* return the actual space left in the FIFO */
3408c2ecf20Sopenharmony_ci	return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic void xiic_fill_tx_fifo(struct xiic_i2c *i2c)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	u8 fifo_space = xiic_tx_fifo_space(i2c);
3468c2ecf20Sopenharmony_ci	int len = xiic_tx_space(i2c);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	len = (len > fifo_space) ? fifo_space : len;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",
3518c2ecf20Sopenharmony_ci		__func__, len, fifo_space);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	while (len--) {
3548c2ecf20Sopenharmony_ci		u16 data = i2c->tx_msg->buf[i2c->tx_pos++];
3558c2ecf20Sopenharmony_ci		if ((xiic_tx_space(i2c) == 0) && (i2c->nmsgs == 1)) {
3568c2ecf20Sopenharmony_ci			/* last message in transfer -> STOP */
3578c2ecf20Sopenharmony_ci			data |= XIIC_TX_DYN_STOP_MASK;
3588c2ecf20Sopenharmony_ci			dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);
3598c2ecf20Sopenharmony_ci		}
3608c2ecf20Sopenharmony_ci		xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic void xiic_wakeup(struct xiic_i2c *i2c, int code)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	i2c->tx_msg = NULL;
3678c2ecf20Sopenharmony_ci	i2c->rx_msg = NULL;
3688c2ecf20Sopenharmony_ci	i2c->nmsgs = 0;
3698c2ecf20Sopenharmony_ci	i2c->state = code;
3708c2ecf20Sopenharmony_ci	wake_up(&i2c->wait);
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic irqreturn_t xiic_process(int irq, void *dev_id)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = dev_id;
3768c2ecf20Sopenharmony_ci	u32 pend, isr, ier;
3778c2ecf20Sopenharmony_ci	u32 clr = 0;
3788c2ecf20Sopenharmony_ci	int xfer_more = 0;
3798c2ecf20Sopenharmony_ci	int wakeup_req = 0;
3808c2ecf20Sopenharmony_ci	int wakeup_code = 0;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* Get the interrupt Status from the IPIF. There is no clearing of
3838c2ecf20Sopenharmony_ci	 * interrupts in the IPIF. Interrupts must be cleared at the source.
3848c2ecf20Sopenharmony_ci	 * To find which interrupts are pending; AND interrupts pending with
3858c2ecf20Sopenharmony_ci	 * interrupts masked.
3868c2ecf20Sopenharmony_ci	 */
3878c2ecf20Sopenharmony_ci	mutex_lock(&i2c->lock);
3888c2ecf20Sopenharmony_ci	isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
3898c2ecf20Sopenharmony_ci	ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
3908c2ecf20Sopenharmony_ci	pend = isr & ier;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n",
3938c2ecf20Sopenharmony_ci		__func__, ier, isr, pend);
3948c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n",
3958c2ecf20Sopenharmony_ci		__func__, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
3968c2ecf20Sopenharmony_ci		i2c->tx_msg, i2c->nmsgs);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* Service requesting interrupt */
4008c2ecf20Sopenharmony_ci	if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
4018c2ecf20Sopenharmony_ci		((pend & XIIC_INTR_TX_ERROR_MASK) &&
4028c2ecf20Sopenharmony_ci		!(pend & XIIC_INTR_RX_FULL_MASK))) {
4038c2ecf20Sopenharmony_ci		/* bus arbritration lost, or...
4048c2ecf20Sopenharmony_ci		 * Transmit error _OR_ RX completed
4058c2ecf20Sopenharmony_ci		 * if this happens when RX_FULL is not set
4068c2ecf20Sopenharmony_ci		 * this is probably a TX error
4078c2ecf20Sopenharmony_ci		 */
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci		/* dynamic mode seem to suffer from problems if we just flushes
4128c2ecf20Sopenharmony_ci		 * fifos and the next message is a TX with len 0 (only addr)
4138c2ecf20Sopenharmony_ci		 * reset the IP instead of just flush fifos
4148c2ecf20Sopenharmony_ci		 */
4158c2ecf20Sopenharmony_ci		xiic_reinit(i2c);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		if (i2c->rx_msg) {
4188c2ecf20Sopenharmony_ci			wakeup_req = 1;
4198c2ecf20Sopenharmony_ci			wakeup_code = STATE_ERROR;
4208c2ecf20Sopenharmony_ci		}
4218c2ecf20Sopenharmony_ci		if (i2c->tx_msg) {
4228c2ecf20Sopenharmony_ci			wakeup_req = 1;
4238c2ecf20Sopenharmony_ci			wakeup_code = STATE_ERROR;
4248c2ecf20Sopenharmony_ci		}
4258c2ecf20Sopenharmony_ci		/* don't try to handle other events */
4268c2ecf20Sopenharmony_ci		goto out;
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci	if (pend & XIIC_INTR_RX_FULL_MASK) {
4298c2ecf20Sopenharmony_ci		/* Receive register/FIFO is full */
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci		clr |= XIIC_INTR_RX_FULL_MASK;
4328c2ecf20Sopenharmony_ci		if (!i2c->rx_msg) {
4338c2ecf20Sopenharmony_ci			dev_dbg(i2c->adap.dev.parent,
4348c2ecf20Sopenharmony_ci				"%s unexpected RX IRQ\n", __func__);
4358c2ecf20Sopenharmony_ci			xiic_clear_rx_fifo(i2c);
4368c2ecf20Sopenharmony_ci			goto out;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		xiic_read_rx(i2c);
4408c2ecf20Sopenharmony_ci		if (xiic_rx_space(i2c) == 0) {
4418c2ecf20Sopenharmony_ci			/* this is the last part of the message */
4428c2ecf20Sopenharmony_ci			i2c->rx_msg = NULL;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci			/* also clear TX error if there (RX complete) */
4458c2ecf20Sopenharmony_ci			clr |= (isr & XIIC_INTR_TX_ERROR_MASK);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci			dev_dbg(i2c->adap.dev.parent,
4488c2ecf20Sopenharmony_ci				"%s end of message, nmsgs: %d\n",
4498c2ecf20Sopenharmony_ci				__func__, i2c->nmsgs);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci			/* send next message if this wasn't the last,
4528c2ecf20Sopenharmony_ci			 * otherwise the transfer will be finialise when
4538c2ecf20Sopenharmony_ci			 * receiving the bus not busy interrupt
4548c2ecf20Sopenharmony_ci			 */
4558c2ecf20Sopenharmony_ci			if (i2c->nmsgs > 1) {
4568c2ecf20Sopenharmony_ci				i2c->nmsgs--;
4578c2ecf20Sopenharmony_ci				i2c->tx_msg++;
4588c2ecf20Sopenharmony_ci				dev_dbg(i2c->adap.dev.parent,
4598c2ecf20Sopenharmony_ci					"%s will start next...\n", __func__);
4608c2ecf20Sopenharmony_ci				xfer_more = 1;
4618c2ecf20Sopenharmony_ci			}
4628c2ecf20Sopenharmony_ci		}
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci	if (pend & XIIC_INTR_BNB_MASK) {
4658c2ecf20Sopenharmony_ci		/* IIC bus has transitioned to not busy */
4668c2ecf20Sopenharmony_ci		clr |= XIIC_INTR_BNB_MASK;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		/* The bus is not busy, disable BusNotBusy interrupt */
4698c2ecf20Sopenharmony_ci		xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		if (!i2c->tx_msg)
4728c2ecf20Sopenharmony_ci			goto out;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci		wakeup_req = 1;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		if (i2c->nmsgs == 1 && !i2c->rx_msg &&
4778c2ecf20Sopenharmony_ci		    xiic_tx_space(i2c) == 0)
4788c2ecf20Sopenharmony_ci			wakeup_code = STATE_DONE;
4798c2ecf20Sopenharmony_ci		else
4808c2ecf20Sopenharmony_ci			wakeup_code = STATE_ERROR;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci	if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
4838c2ecf20Sopenharmony_ci		/* Transmit register/FIFO is empty or ½ empty */
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		clr |= (pend &
4868c2ecf20Sopenharmony_ci			(XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK));
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		if (!i2c->tx_msg) {
4898c2ecf20Sopenharmony_ci			dev_dbg(i2c->adap.dev.parent,
4908c2ecf20Sopenharmony_ci				"%s unexpected TX IRQ\n", __func__);
4918c2ecf20Sopenharmony_ci			goto out;
4928c2ecf20Sopenharmony_ci		}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		xiic_fill_tx_fifo(i2c);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		/* current message sent and there is space in the fifo */
4978c2ecf20Sopenharmony_ci		if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
4988c2ecf20Sopenharmony_ci			dev_dbg(i2c->adap.dev.parent,
4998c2ecf20Sopenharmony_ci				"%s end of message sent, nmsgs: %d\n",
5008c2ecf20Sopenharmony_ci				__func__, i2c->nmsgs);
5018c2ecf20Sopenharmony_ci			if (i2c->nmsgs > 1) {
5028c2ecf20Sopenharmony_ci				i2c->nmsgs--;
5038c2ecf20Sopenharmony_ci				i2c->tx_msg++;
5048c2ecf20Sopenharmony_ci				xfer_more = 1;
5058c2ecf20Sopenharmony_ci			} else {
5068c2ecf20Sopenharmony_ci				xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci				dev_dbg(i2c->adap.dev.parent,
5098c2ecf20Sopenharmony_ci					"%s Got TX IRQ but no more to do...\n",
5108c2ecf20Sopenharmony_ci					__func__);
5118c2ecf20Sopenharmony_ci			}
5128c2ecf20Sopenharmony_ci		} else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
5138c2ecf20Sopenharmony_ci			/* current frame is sent and is last,
5148c2ecf20Sopenharmony_ci			 * make sure to disable tx half
5158c2ecf20Sopenharmony_ci			 */
5168c2ecf20Sopenharmony_ci			xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
5178c2ecf20Sopenharmony_ci	}
5188c2ecf20Sopenharmony_ciout:
5198c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
5228c2ecf20Sopenharmony_ci	if (xfer_more)
5238c2ecf20Sopenharmony_ci		__xiic_start_xfer(i2c);
5248c2ecf20Sopenharmony_ci	if (wakeup_req)
5258c2ecf20Sopenharmony_ci		xiic_wakeup(i2c, wakeup_code);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	WARN_ON(xfer_more && wakeup_req);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	mutex_unlock(&i2c->lock);
5308c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic int xiic_bus_busy(struct xiic_i2c *i2c)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
5388c2ecf20Sopenharmony_ci}
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_cistatic int xiic_busy(struct xiic_i2c *i2c)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	int tries = 3;
5438c2ecf20Sopenharmony_ci	int err;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	if (i2c->tx_msg)
5468c2ecf20Sopenharmony_ci		return -EBUSY;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* In single master mode bus can only be busy, when in use by this
5498c2ecf20Sopenharmony_ci	 * driver. If the register indicates bus being busy for some reason we
5508c2ecf20Sopenharmony_ci	 * should ignore it, since bus will never be released and i2c will be
5518c2ecf20Sopenharmony_ci	 * stuck forever.
5528c2ecf20Sopenharmony_ci	 */
5538c2ecf20Sopenharmony_ci	if (i2c->singlemaster) {
5548c2ecf20Sopenharmony_ci		return 0;
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* for instance if previous transfer was terminated due to TX error
5588c2ecf20Sopenharmony_ci	 * it might be that the bus is on it's way to become available
5598c2ecf20Sopenharmony_ci	 * give it at most 3 ms to wake
5608c2ecf20Sopenharmony_ci	 */
5618c2ecf20Sopenharmony_ci	err = xiic_bus_busy(i2c);
5628c2ecf20Sopenharmony_ci	while (err && tries--) {
5638c2ecf20Sopenharmony_ci		msleep(1);
5648c2ecf20Sopenharmony_ci		err = xiic_bus_busy(i2c);
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	return err;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic void xiic_start_recv(struct xiic_i2c *i2c)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	u8 rx_watermark;
5738c2ecf20Sopenharmony_ci	struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
5748c2ecf20Sopenharmony_ci	unsigned long flags;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	/* Clear and enable Rx full interrupt. */
5778c2ecf20Sopenharmony_ci	xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	/* we want to get all but last byte, because the TX_ERROR IRQ is used
5808c2ecf20Sopenharmony_ci	 * to inidicate error ACK on the address, and negative ack on the last
5818c2ecf20Sopenharmony_ci	 * received byte, so to not mix them receive all but last.
5828c2ecf20Sopenharmony_ci	 * In the case where there is only one byte to receive
5838c2ecf20Sopenharmony_ci	 * we can check if ERROR and RX full is set at the same time
5848c2ecf20Sopenharmony_ci	 */
5858c2ecf20Sopenharmony_ci	rx_watermark = msg->len;
5868c2ecf20Sopenharmony_ci	if (rx_watermark > IIC_RX_FIFO_DEPTH)
5878c2ecf20Sopenharmony_ci		rx_watermark = IIC_RX_FIFO_DEPTH;
5888c2ecf20Sopenharmony_ci	xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rx_watermark - 1);
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	local_irq_save(flags);
5918c2ecf20Sopenharmony_ci	if (!(msg->flags & I2C_M_NOSTART))
5928c2ecf20Sopenharmony_ci		/* write the address */
5938c2ecf20Sopenharmony_ci		xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
5948c2ecf20Sopenharmony_ci			i2c_8bit_addr_from_msg(msg) | XIIC_TX_DYN_START_MASK);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
5998c2ecf20Sopenharmony_ci		msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));
6008c2ecf20Sopenharmony_ci	local_irq_restore(flags);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (i2c->nmsgs == 1)
6038c2ecf20Sopenharmony_ci		/* very last, enable bus not busy as well */
6048c2ecf20Sopenharmony_ci		xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	/* the message is tx:ed */
6078c2ecf20Sopenharmony_ci	i2c->tx_pos = msg->len;
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic void xiic_start_send(struct xiic_i2c *i2c)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct i2c_msg *msg = i2c->tx_msg;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d",
6178c2ecf20Sopenharmony_ci		__func__, msg, msg->len);
6188c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
6198c2ecf20Sopenharmony_ci		__func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
6208c2ecf20Sopenharmony_ci		xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	if (!(msg->flags & I2C_M_NOSTART)) {
6238c2ecf20Sopenharmony_ci		/* write the address */
6248c2ecf20Sopenharmony_ci		u16 data = i2c_8bit_addr_from_msg(msg) |
6258c2ecf20Sopenharmony_ci			XIIC_TX_DYN_START_MASK;
6268c2ecf20Sopenharmony_ci		if ((i2c->nmsgs == 1) && msg->len == 0)
6278c2ecf20Sopenharmony_ci			/* no data and last message -> add STOP */
6288c2ecf20Sopenharmony_ci			data |= XIIC_TX_DYN_STOP_MASK;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci		xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	xiic_fill_tx_fifo(i2c);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* Clear any pending Tx empty, Tx Error and then enable them. */
6368c2ecf20Sopenharmony_ci	xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |
6378c2ecf20Sopenharmony_ci		XIIC_INTR_BNB_MASK);
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic irqreturn_t xiic_isr(int irq, void *dev_id)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = dev_id;
6438c2ecf20Sopenharmony_ci	u32 pend, isr, ier;
6448c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_NONE;
6458c2ecf20Sopenharmony_ci	/* Do not processes a devices interrupts if the device has no
6468c2ecf20Sopenharmony_ci	 * interrupts pending
6478c2ecf20Sopenharmony_ci	 */
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
6528c2ecf20Sopenharmony_ci	ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
6538c2ecf20Sopenharmony_ci	pend = isr & ier;
6548c2ecf20Sopenharmony_ci	if (pend)
6558c2ecf20Sopenharmony_ci		ret = IRQ_WAKE_THREAD;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	return ret;
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic void __xiic_start_xfer(struct xiic_i2c *i2c)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	int first = 1;
6638c2ecf20Sopenharmony_ci	int fifo_space = xiic_tx_fifo_space(i2c);
6648c2ecf20Sopenharmony_ci	dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
6658c2ecf20Sopenharmony_ci		__func__, i2c->tx_msg, fifo_space);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	if (!i2c->tx_msg)
6688c2ecf20Sopenharmony_ci		return;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	i2c->rx_pos = 0;
6718c2ecf20Sopenharmony_ci	i2c->tx_pos = 0;
6728c2ecf20Sopenharmony_ci	i2c->state = STATE_START;
6738c2ecf20Sopenharmony_ci	while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {
6748c2ecf20Sopenharmony_ci		if (!first) {
6758c2ecf20Sopenharmony_ci			i2c->nmsgs--;
6768c2ecf20Sopenharmony_ci			i2c->tx_msg++;
6778c2ecf20Sopenharmony_ci			i2c->tx_pos = 0;
6788c2ecf20Sopenharmony_ci		} else
6798c2ecf20Sopenharmony_ci			first = 0;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		if (i2c->tx_msg->flags & I2C_M_RD) {
6828c2ecf20Sopenharmony_ci			/* we dont date putting several reads in the FIFO */
6838c2ecf20Sopenharmony_ci			xiic_start_recv(i2c);
6848c2ecf20Sopenharmony_ci			return;
6858c2ecf20Sopenharmony_ci		} else {
6868c2ecf20Sopenharmony_ci			xiic_start_send(i2c);
6878c2ecf20Sopenharmony_ci			if (xiic_tx_space(i2c) != 0) {
6888c2ecf20Sopenharmony_ci				/* the message could not be completely sent */
6898c2ecf20Sopenharmony_ci				break;
6908c2ecf20Sopenharmony_ci			}
6918c2ecf20Sopenharmony_ci		}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci		fifo_space = xiic_tx_fifo_space(i2c);
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* there are more messages or the current one could not be completely
6978c2ecf20Sopenharmony_ci	 * put into the FIFO, also enable the half empty interrupt
6988c2ecf20Sopenharmony_ci	 */
6998c2ecf20Sopenharmony_ci	if (i2c->nmsgs > 1 || xiic_tx_space(i2c))
7008c2ecf20Sopenharmony_ci		xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic int xiic_start_xfer(struct xiic_i2c *i2c)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	int ret;
7078c2ecf20Sopenharmony_ci	mutex_lock(&i2c->lock);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	ret = xiic_reinit(i2c);
7108c2ecf20Sopenharmony_ci	if (!ret)
7118c2ecf20Sopenharmony_ci		__xiic_start_xfer(i2c);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	mutex_unlock(&i2c->lock);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	return ret;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = i2c_get_adapdata(adap);
7218c2ecf20Sopenharmony_ci	int err;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
7248c2ecf20Sopenharmony_ci		xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	err = pm_runtime_resume_and_get(i2c->dev);
7278c2ecf20Sopenharmony_ci	if (err < 0)
7288c2ecf20Sopenharmony_ci		return err;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	err = xiic_busy(i2c);
7318c2ecf20Sopenharmony_ci	if (err)
7328c2ecf20Sopenharmony_ci		goto out;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	i2c->tx_msg = msgs;
7358c2ecf20Sopenharmony_ci	i2c->nmsgs = num;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	err = xiic_start_xfer(i2c);
7388c2ecf20Sopenharmony_ci	if (err < 0) {
7398c2ecf20Sopenharmony_ci		dev_err(adap->dev.parent, "Error xiic_start_xfer\n");
7408c2ecf20Sopenharmony_ci		goto out;
7418c2ecf20Sopenharmony_ci	}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
7448c2ecf20Sopenharmony_ci		(i2c->state == STATE_DONE), HZ)) {
7458c2ecf20Sopenharmony_ci		err = (i2c->state == STATE_DONE) ? num : -EIO;
7468c2ecf20Sopenharmony_ci		goto out;
7478c2ecf20Sopenharmony_ci	} else {
7488c2ecf20Sopenharmony_ci		i2c->tx_msg = NULL;
7498c2ecf20Sopenharmony_ci		i2c->rx_msg = NULL;
7508c2ecf20Sopenharmony_ci		i2c->nmsgs = 0;
7518c2ecf20Sopenharmony_ci		err = -ETIMEDOUT;
7528c2ecf20Sopenharmony_ci		goto out;
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ciout:
7558c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(i2c->dev);
7568c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(i2c->dev);
7578c2ecf20Sopenharmony_ci	return err;
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_cistatic u32 xiic_func(struct i2c_adapter *adap)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
7638c2ecf20Sopenharmony_ci}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_cistatic const struct i2c_algorithm xiic_algorithm = {
7668c2ecf20Sopenharmony_ci	.master_xfer = xiic_xfer,
7678c2ecf20Sopenharmony_ci	.functionality = xiic_func,
7688c2ecf20Sopenharmony_ci};
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks xiic_quirks = {
7718c2ecf20Sopenharmony_ci	.max_read_len = 255,
7728c2ecf20Sopenharmony_ci};
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic const struct i2c_adapter xiic_adapter = {
7758c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
7768c2ecf20Sopenharmony_ci	.class = I2C_CLASS_DEPRECATED,
7778c2ecf20Sopenharmony_ci	.algo = &xiic_algorithm,
7788c2ecf20Sopenharmony_ci	.quirks = &xiic_quirks,
7798c2ecf20Sopenharmony_ci};
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cistatic int xiic_i2c_probe(struct platform_device *pdev)
7838c2ecf20Sopenharmony_ci{
7848c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c;
7858c2ecf20Sopenharmony_ci	struct xiic_i2c_platform_data *pdata;
7868c2ecf20Sopenharmony_ci	struct resource *res;
7878c2ecf20Sopenharmony_ci	int ret, irq;
7888c2ecf20Sopenharmony_ci	u8 i;
7898c2ecf20Sopenharmony_ci	u32 sr;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
7928c2ecf20Sopenharmony_ci	if (!i2c)
7938c2ecf20Sopenharmony_ci		return -ENOMEM;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7968c2ecf20Sopenharmony_ci	i2c->base = devm_ioremap_resource(&pdev->dev, res);
7978c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->base))
7988c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->base);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
8018c2ecf20Sopenharmony_ci	if (irq < 0)
8028c2ecf20Sopenharmony_ci		return irq;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	pdata = dev_get_platdata(&pdev->dev);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	/* hook up driver to tree */
8078c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, i2c);
8088c2ecf20Sopenharmony_ci	i2c->adap = xiic_adapter;
8098c2ecf20Sopenharmony_ci	i2c_set_adapdata(&i2c->adap, i2c);
8108c2ecf20Sopenharmony_ci	i2c->adap.dev.parent = &pdev->dev;
8118c2ecf20Sopenharmony_ci	i2c->adap.dev.of_node = pdev->dev.of_node;
8128c2ecf20Sopenharmony_ci	snprintf(i2c->adap.name, sizeof(i2c->adap.name),
8138c2ecf20Sopenharmony_ci		 DRIVER_NAME " %s", pdev->name);
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	mutex_init(&i2c->lock);
8168c2ecf20Sopenharmony_ci	init_waitqueue_head(&i2c->wait);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	i2c->clk = devm_clk_get(&pdev->dev, NULL);
8198c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->clk)) {
8208c2ecf20Sopenharmony_ci		if (PTR_ERR(i2c->clk) != -EPROBE_DEFER)
8218c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "input clock not found.\n");
8228c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->clk);
8238c2ecf20Sopenharmony_ci	}
8248c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2c->clk);
8258c2ecf20Sopenharmony_ci	if (ret) {
8268c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Unable to enable clock.\n");
8278c2ecf20Sopenharmony_ci		return ret;
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci	i2c->dev = &pdev->dev;
8308c2ecf20Sopenharmony_ci	pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT);
8318c2ecf20Sopenharmony_ci	pm_runtime_use_autosuspend(i2c->dev);
8328c2ecf20Sopenharmony_ci	pm_runtime_set_active(i2c->dev);
8338c2ecf20Sopenharmony_ci	pm_runtime_enable(i2c->dev);
8348c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr,
8358c2ecf20Sopenharmony_ci					xiic_process, IRQF_ONESHOT,
8368c2ecf20Sopenharmony_ci					pdev->name, i2c);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	if (ret < 0) {
8398c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Cannot claim IRQ\n");
8408c2ecf20Sopenharmony_ci		goto err_clk_dis;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	i2c->singlemaster =
8448c2ecf20Sopenharmony_ci		of_property_read_bool(pdev->dev.of_node, "single-master");
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	/*
8478c2ecf20Sopenharmony_ci	 * Detect endianness
8488c2ecf20Sopenharmony_ci	 * Try to reset the TX FIFO. Then check the EMPTY flag. If it is not
8498c2ecf20Sopenharmony_ci	 * set, assume that the endianness was wrong and swap.
8508c2ecf20Sopenharmony_ci	 */
8518c2ecf20Sopenharmony_ci	i2c->endianness = LITTLE;
8528c2ecf20Sopenharmony_ci	xiic_setreg32(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
8538c2ecf20Sopenharmony_ci	/* Reset is cleared in xiic_reinit */
8548c2ecf20Sopenharmony_ci	sr = xiic_getreg32(i2c, XIIC_SR_REG_OFFSET);
8558c2ecf20Sopenharmony_ci	if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK))
8568c2ecf20Sopenharmony_ci		i2c->endianness = BIG;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	ret = xiic_reinit(i2c);
8598c2ecf20Sopenharmony_ci	if (ret < 0) {
8608c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Cannot xiic_reinit\n");
8618c2ecf20Sopenharmony_ci		goto err_clk_dis;
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	/* add i2c adapter to i2c tree */
8658c2ecf20Sopenharmony_ci	ret = i2c_add_adapter(&i2c->adap);
8668c2ecf20Sopenharmony_ci	if (ret) {
8678c2ecf20Sopenharmony_ci		xiic_deinit(i2c);
8688c2ecf20Sopenharmony_ci		goto err_clk_dis;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (pdata) {
8728c2ecf20Sopenharmony_ci		/* add in known devices to the bus */
8738c2ecf20Sopenharmony_ci		for (i = 0; i < pdata->num_devices; i++)
8748c2ecf20Sopenharmony_ci			i2c_new_client_device(&i2c->adap, pdata->devices + i);
8758c2ecf20Sopenharmony_ci	}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	return 0;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_cierr_clk_dis:
8808c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&pdev->dev);
8818c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
8828c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk);
8838c2ecf20Sopenharmony_ci	return ret;
8848c2ecf20Sopenharmony_ci}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_cistatic int xiic_i2c_remove(struct platform_device *pdev)
8878c2ecf20Sopenharmony_ci{
8888c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = platform_get_drvdata(pdev);
8898c2ecf20Sopenharmony_ci	int ret;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	/* remove adapter & data */
8928c2ecf20Sopenharmony_ci	i2c_del_adapter(&i2c->adap);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	ret = pm_runtime_resume_and_get(i2c->dev);
8958c2ecf20Sopenharmony_ci	if (ret < 0)
8968c2ecf20Sopenharmony_ci		return ret;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	xiic_deinit(i2c);
8998c2ecf20Sopenharmony_ci	pm_runtime_put_sync(i2c->dev);
9008c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk);
9018c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
9028c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&pdev->dev);
9038c2ecf20Sopenharmony_ci	pm_runtime_dont_use_autosuspend(&pdev->dev);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	return 0;
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci#if defined(CONFIG_OF)
9098c2ecf20Sopenharmony_cistatic const struct of_device_id xiic_of_match[] = {
9108c2ecf20Sopenharmony_ci	{ .compatible = "xlnx,xps-iic-2.00.a", },
9118c2ecf20Sopenharmony_ci	{},
9128c2ecf20Sopenharmony_ci};
9138c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, xiic_of_match);
9148c2ecf20Sopenharmony_ci#endif
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_cistatic int __maybe_unused xiic_i2c_runtime_suspend(struct device *dev)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = dev_get_drvdata(dev);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	clk_disable(i2c->clk);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	return 0;
9238c2ecf20Sopenharmony_ci}
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_cistatic int __maybe_unused xiic_i2c_runtime_resume(struct device *dev)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	struct xiic_i2c *i2c = dev_get_drvdata(dev);
9288c2ecf20Sopenharmony_ci	int ret;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	ret = clk_enable(i2c->clk);
9318c2ecf20Sopenharmony_ci	if (ret) {
9328c2ecf20Sopenharmony_ci		dev_err(dev, "Cannot enable clock.\n");
9338c2ecf20Sopenharmony_ci		return ret;
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	return 0;
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic const struct dev_pm_ops xiic_dev_pm_ops = {
9408c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(xiic_i2c_runtime_suspend,
9418c2ecf20Sopenharmony_ci			   xiic_i2c_runtime_resume, NULL)
9428c2ecf20Sopenharmony_ci};
9438c2ecf20Sopenharmony_cistatic struct platform_driver xiic_i2c_driver = {
9448c2ecf20Sopenharmony_ci	.probe   = xiic_i2c_probe,
9458c2ecf20Sopenharmony_ci	.remove  = xiic_i2c_remove,
9468c2ecf20Sopenharmony_ci	.driver  = {
9478c2ecf20Sopenharmony_ci		.name = DRIVER_NAME,
9488c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(xiic_of_match),
9498c2ecf20Sopenharmony_ci		.pm = &xiic_dev_pm_ops,
9508c2ecf20Sopenharmony_ci	},
9518c2ecf20Sopenharmony_ci};
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_cimodule_platform_driver(xiic_i2c_driver);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRIVER_NAME);
9568c2ecf20Sopenharmony_ciMODULE_AUTHOR("info@mocean-labs.com");
9578c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Xilinx I2C bus driver");
9588c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
9598c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:"DRIVER_NAME);
960