18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 FUJITSU SEMICONDUCTOR LIMITED
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/acpi.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/delay.h>
98c2ecf20Sopenharmony_ci#include <linux/device.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/i2c.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/sched.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define WAIT_PCLK(n, rate)	\
238c2ecf20Sopenharmony_ci	ndelay(DIV_ROUND_UP(DIV_ROUND_UP(1000000000, rate), n) + 10)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* I2C register address definitions */
268c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_BSR		(0x00 << 2) // Bus Status
278c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_BCR		(0x01 << 2) // Bus Control
288c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_CCR		(0x02 << 2) // Clock Control
298c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_ADR		(0x03 << 2) // Address
308c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_DAR		(0x04 << 2) // Data
318c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_CSR		(0x05 << 2) // Expansion CS
328c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_FSR		(0x06 << 2) // Bus Clock Freq
338c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_REG_BC2R		(0x07 << 2) // Bus Control 2
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* I2C register bit definitions */
368c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_FBT		BIT(0)	// First Byte Transfer
378c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_GCA		BIT(1)	// General Call Address
388c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_AAS		BIT(2)	// Address as Slave
398c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_TRX		BIT(3)	// Transfer/Receive
408c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_LRB		BIT(4)	// Last Received Bit
418c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_AL		BIT(5)	// Arbitration Lost
428c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_RSC		BIT(6)	// Repeated Start Cond.
438c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BSR_BB		BIT(7)	// Bus Busy
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_INT		BIT(0)	// Interrupt
468c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_INTE		BIT(1)	// Interrupt Enable
478c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_GCAA		BIT(2)	// Gen. Call Access Ack.
488c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_ACK		BIT(3)	// Acknowledge
498c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_MSS		BIT(4)	// Master Slave Select
508c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_SCC		BIT(5)	// Start Condition Cont.
518c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_BEIE		BIT(6)	// Bus Error Int Enable
528c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BCR_BER		BIT(7)	// Bus Error
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_CS_MASK	(0x1f)	// CCR Clock Period Sel.
558c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_EN		BIT(5)	// Enable
568c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_FM		BIT(6)	// Speed Mode Select
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CSR_CS_MASK	(0x3f)	// CSR Clock Period Sel.
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BC2R_SCLL		BIT(0)	// SCL Low Drive
618c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BC2R_SDAL		BIT(1)	// SDA Low Drive
628c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BC2R_SCLS		BIT(4)	// SCL Status
638c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BC2R_SDAS		BIT(5)	// SDA Status
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* PCLK frequency */
668c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_BUS_CLK_FR(rate)	(((rate) / 20000000) + 1)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* STANDARD MODE frequency */
698c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CLK_MASTER_STD(rate)			\
708c2ecf20Sopenharmony_ci	DIV_ROUND_UP(DIV_ROUND_UP((rate), I2C_MAX_STANDARD_MODE_FREQ) - 2, 2)
718c2ecf20Sopenharmony_ci/* FAST MODE frequency */
728c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CLK_MASTER_FAST(rate)			\
738c2ecf20Sopenharmony_ci	DIV_ROUND_UP((DIV_ROUND_UP((rate), I2C_MAX_FAST_MODE_FREQ) - 2) * 2, 3)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* (clkrate <= 18000000) */
768c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CCR register on standard mode */
778c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_CS_STD_MAX_18M(rate)			\
788c2ecf20Sopenharmony_ci	   ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 65)		\
798c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CCR_CS_MASK)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CSR register on standard mode */
828c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CSR_CS_STD_MAX_18M(rate)		0x00
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CCR register on fast mode */
858c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_CS_FAST_MAX_18M(rate)			\
868c2ecf20Sopenharmony_ci	   ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1)		\
878c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CCR_CS_MASK)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CSR register on fast mode */
908c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CSR_CS_FAST_MAX_18M(rate)		0x00
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/* (clkrate > 18000000) */
938c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CCR register on standard mode */
948c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_CS_STD_MIN_18M(rate)			\
958c2ecf20Sopenharmony_ci	   ((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1)		\
968c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CCR_CS_MASK)
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CSR register on standard mode */
998c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CSR_CS_STD_MIN_18M(rate)			\
1008c2ecf20Sopenharmony_ci	   (((SYNQUACER_I2C_CLK_MASTER_STD(rate) - 1) >> 5)	\
1018c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CSR_CS_MASK)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CCR register on fast mode */
1048c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CCR_CS_FAST_MIN_18M(rate)			\
1058c2ecf20Sopenharmony_ci	   ((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1)		\
1068c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CCR_CS_MASK)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* calculate the value of CS bits in CSR register on fast mode */
1098c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CSR_CS_FAST_MIN_18M(rate)			\
1108c2ecf20Sopenharmony_ci	   (((SYNQUACER_I2C_CLK_MASTER_FAST(rate) - 1) >> 5)	\
1118c2ecf20Sopenharmony_ci					& SYNQUACER_I2C_CSR_CS_MASK)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* min I2C clock frequency 14M */
1148c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_MIN_CLK_RATE	(14 * 1000000)
1158c2ecf20Sopenharmony_ci/* max I2C clock frequency 200M */
1168c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_MAX_CLK_RATE	(200 * 1000000)
1178c2ecf20Sopenharmony_ci/* I2C clock frequency 18M */
1188c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_CLK_RATE_18M	(18 * 1000000)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_SPEED_FM		400	// Fast Mode
1218c2ecf20Sopenharmony_ci#define SYNQUACER_I2C_SPEED_SM		100	// Standard Mode
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cienum i2c_state {
1248c2ecf20Sopenharmony_ci	STATE_IDLE,
1258c2ecf20Sopenharmony_ci	STATE_START,
1268c2ecf20Sopenharmony_ci	STATE_READ,
1278c2ecf20Sopenharmony_ci	STATE_WRITE
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct synquacer_i2c {
1318c2ecf20Sopenharmony_ci	struct completion	completion;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	struct i2c_msg		*msg;
1348c2ecf20Sopenharmony_ci	u32			msg_num;
1358c2ecf20Sopenharmony_ci	u32			msg_idx;
1368c2ecf20Sopenharmony_ci	u32			msg_ptr;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	int			irq;
1398c2ecf20Sopenharmony_ci	struct device		*dev;
1408c2ecf20Sopenharmony_ci	void __iomem		*base;
1418c2ecf20Sopenharmony_ci	struct clk		*pclk;
1428c2ecf20Sopenharmony_ci	u32			pclkrate;
1438c2ecf20Sopenharmony_ci	u32			speed_khz;
1448c2ecf20Sopenharmony_ci	u32			timeout_ms;
1458c2ecf20Sopenharmony_ci	enum i2c_state		state;
1468c2ecf20Sopenharmony_ci	struct i2c_adapter	adapter;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic inline int is_lastmsg(struct synquacer_i2c *i2c)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	return i2c->msg_idx >= (i2c->msg_num - 1);
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic inline int is_msglast(struct synquacer_i2c *i2c)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	return i2c->msg_ptr == (i2c->msg->len - 1);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic inline int is_msgend(struct synquacer_i2c *i2c)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	return i2c->msg_ptr >= i2c->msg->len;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline unsigned long calc_timeout_ms(struct synquacer_i2c *i2c,
1658c2ecf20Sopenharmony_ci					    struct i2c_msg *msgs,
1668c2ecf20Sopenharmony_ci					    int num)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	unsigned long bit_count = 0;
1698c2ecf20Sopenharmony_ci	int i;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++, msgs++)
1728c2ecf20Sopenharmony_ci		bit_count += msgs->len;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return DIV_ROUND_UP((bit_count * 9 + num * 10) * 3, 200) + 10;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic void synquacer_i2c_stop(struct synquacer_i2c *i2c, int ret)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	/*
1808c2ecf20Sopenharmony_ci	 * clear IRQ (INT=0, BER=0)
1818c2ecf20Sopenharmony_ci	 * set Stop Condition (MSS=0)
1828c2ecf20Sopenharmony_ci	 * Interrupt Disable
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	i2c->state = STATE_IDLE;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	i2c->msg_ptr = 0;
1898c2ecf20Sopenharmony_ci	i2c->msg = NULL;
1908c2ecf20Sopenharmony_ci	i2c->msg_idx++;
1918c2ecf20Sopenharmony_ci	i2c->msg_num = 0;
1928c2ecf20Sopenharmony_ci	if (ret)
1938c2ecf20Sopenharmony_ci		i2c->msg_idx = ret;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	complete(&i2c->completion);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic void synquacer_i2c_hw_init(struct synquacer_i2c *i2c)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	unsigned char ccr_cs, csr_cs;
2018c2ecf20Sopenharmony_ci	u32 rt = i2c->pclkrate;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/* Set own Address */
2048c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_ADR);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* Set PCLK frequency */
2078c2ecf20Sopenharmony_ci	writeb(SYNQUACER_I2C_BUS_CLK_FR(i2c->pclkrate),
2088c2ecf20Sopenharmony_ci	       i2c->base + SYNQUACER_I2C_REG_FSR);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	switch (i2c->speed_khz) {
2118c2ecf20Sopenharmony_ci	case SYNQUACER_I2C_SPEED_FM:
2128c2ecf20Sopenharmony_ci		if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) {
2138c2ecf20Sopenharmony_ci			ccr_cs = SYNQUACER_I2C_CCR_CS_FAST_MAX_18M(rt);
2148c2ecf20Sopenharmony_ci			csr_cs = SYNQUACER_I2C_CSR_CS_FAST_MAX_18M(rt);
2158c2ecf20Sopenharmony_ci		} else {
2168c2ecf20Sopenharmony_ci			ccr_cs = SYNQUACER_I2C_CCR_CS_FAST_MIN_18M(rt);
2178c2ecf20Sopenharmony_ci			csr_cs = SYNQUACER_I2C_CSR_CS_FAST_MIN_18M(rt);
2188c2ecf20Sopenharmony_ci		}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci		/* Set Clock and enable, Set fast mode */
2218c2ecf20Sopenharmony_ci		writeb(ccr_cs | SYNQUACER_I2C_CCR_FM |
2228c2ecf20Sopenharmony_ci		       SYNQUACER_I2C_CCR_EN,
2238c2ecf20Sopenharmony_ci		       i2c->base + SYNQUACER_I2C_REG_CCR);
2248c2ecf20Sopenharmony_ci		writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR);
2258c2ecf20Sopenharmony_ci		break;
2268c2ecf20Sopenharmony_ci	case SYNQUACER_I2C_SPEED_SM:
2278c2ecf20Sopenharmony_ci		if (i2c->pclkrate <= SYNQUACER_I2C_CLK_RATE_18M) {
2288c2ecf20Sopenharmony_ci			ccr_cs = SYNQUACER_I2C_CCR_CS_STD_MAX_18M(rt);
2298c2ecf20Sopenharmony_ci			csr_cs = SYNQUACER_I2C_CSR_CS_STD_MAX_18M(rt);
2308c2ecf20Sopenharmony_ci		} else {
2318c2ecf20Sopenharmony_ci			ccr_cs = SYNQUACER_I2C_CCR_CS_STD_MIN_18M(rt);
2328c2ecf20Sopenharmony_ci			csr_cs = SYNQUACER_I2C_CSR_CS_STD_MIN_18M(rt);
2338c2ecf20Sopenharmony_ci		}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		/* Set Clock and enable, Set standard mode */
2368c2ecf20Sopenharmony_ci		writeb(ccr_cs | SYNQUACER_I2C_CCR_EN,
2378c2ecf20Sopenharmony_ci		      i2c->base + SYNQUACER_I2C_REG_CCR);
2388c2ecf20Sopenharmony_ci		writeb(csr_cs, i2c->base + SYNQUACER_I2C_REG_CSR);
2398c2ecf20Sopenharmony_ci		break;
2408c2ecf20Sopenharmony_ci	default:
2418c2ecf20Sopenharmony_ci		WARN_ON(1);
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	/* clear IRQ (INT=0, BER=0), Interrupt Disable */
2458c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_BCR);
2468c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_BC2R);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void synquacer_i2c_hw_reset(struct synquacer_i2c *i2c)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	/* Disable clock */
2528c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_CCR);
2538c2ecf20Sopenharmony_ci	writeb(0, i2c->base + SYNQUACER_I2C_REG_CSR);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	WAIT_PCLK(100, i2c->pclkrate);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic int synquacer_i2c_master_start(struct synquacer_i2c *i2c,
2598c2ecf20Sopenharmony_ci				      struct i2c_msg *pmsg)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	unsigned char bsr, bcr;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	writeb(i2c_8bit_addr_from_msg(pmsg), i2c->base + SYNQUACER_I2C_REG_DAR);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "slave:0x%02x\n", pmsg->addr);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/* Generate Start Condition */
2688c2ecf20Sopenharmony_ci	bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR);
2698c2ecf20Sopenharmony_ci	bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR);
2708c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if ((bsr & SYNQUACER_I2C_BSR_BB) &&
2738c2ecf20Sopenharmony_ci	    !(bcr & SYNQUACER_I2C_BCR_MSS)) {
2748c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "bus is busy");
2758c2ecf20Sopenharmony_ci		return -EBUSY;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (bsr & SYNQUACER_I2C_BSR_BB) { /* Bus is busy */
2798c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "Continuous Start");
2808c2ecf20Sopenharmony_ci		writeb(bcr | SYNQUACER_I2C_BCR_SCC,
2818c2ecf20Sopenharmony_ci		       i2c->base + SYNQUACER_I2C_REG_BCR);
2828c2ecf20Sopenharmony_ci	} else {
2838c2ecf20Sopenharmony_ci		if (bcr & SYNQUACER_I2C_BCR_MSS) {
2848c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "not in master mode");
2858c2ecf20Sopenharmony_ci			return -EAGAIN;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "Start Condition");
2888c2ecf20Sopenharmony_ci		/* Start Condition + Enable Interrupts */
2898c2ecf20Sopenharmony_ci		writeb(bcr | SYNQUACER_I2C_BCR_MSS |
2908c2ecf20Sopenharmony_ci		       SYNQUACER_I2C_BCR_INTE | SYNQUACER_I2C_BCR_BEIE,
2918c2ecf20Sopenharmony_ci		       i2c->base + SYNQUACER_I2C_REG_BCR);
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	WAIT_PCLK(10, i2c->pclkrate);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* get BSR & BCR registers */
2978c2ecf20Sopenharmony_ci	bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR);
2988c2ecf20Sopenharmony_ci	bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR);
2998c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if ((bsr & SYNQUACER_I2C_BSR_AL) ||
3028c2ecf20Sopenharmony_ci	    !(bcr & SYNQUACER_I2C_BCR_MSS)) {
3038c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "arbitration lost\n");
3048c2ecf20Sopenharmony_ci		return -EAGAIN;
3058c2ecf20Sopenharmony_ci	}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return 0;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int synquacer_i2c_doxfer(struct synquacer_i2c *i2c,
3118c2ecf20Sopenharmony_ci				struct i2c_msg *msgs, int num)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	unsigned char bsr;
3148c2ecf20Sopenharmony_ci	unsigned long timeout;
3158c2ecf20Sopenharmony_ci	int ret;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	synquacer_i2c_hw_init(i2c);
3188c2ecf20Sopenharmony_ci	bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR);
3198c2ecf20Sopenharmony_ci	if (bsr & SYNQUACER_I2C_BSR_BB) {
3208c2ecf20Sopenharmony_ci		dev_err(i2c->dev, "cannot get bus (bus busy)\n");
3218c2ecf20Sopenharmony_ci		return -EBUSY;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	reinit_completion(&i2c->completion);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	i2c->msg = msgs;
3278c2ecf20Sopenharmony_ci	i2c->msg_num = num;
3288c2ecf20Sopenharmony_ci	i2c->msg_ptr = 0;
3298c2ecf20Sopenharmony_ci	i2c->msg_idx = 0;
3308c2ecf20Sopenharmony_ci	i2c->state = STATE_START;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	ret = synquacer_i2c_master_start(i2c, i2c->msg);
3338c2ecf20Sopenharmony_ci	if (ret < 0) {
3348c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "Address failed: (%d)\n", ret);
3358c2ecf20Sopenharmony_ci		return ret;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	timeout = wait_for_completion_timeout(&i2c->completion,
3398c2ecf20Sopenharmony_ci					msecs_to_jiffies(i2c->timeout_ms));
3408c2ecf20Sopenharmony_ci	if (timeout == 0) {
3418c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "timeout\n");
3428c2ecf20Sopenharmony_ci		return -EAGAIN;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	ret = i2c->msg_idx;
3468c2ecf20Sopenharmony_ci	if (ret != num) {
3478c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
3488c2ecf20Sopenharmony_ci		return -EAGAIN;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	/* wait 2 clock periods to ensure the stop has been through the bus */
3528c2ecf20Sopenharmony_ci	udelay(DIV_ROUND_UP(2 * 1000, i2c->speed_khz));
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return ret;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic irqreturn_t synquacer_i2c_isr(int irq, void *dev_id)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	struct synquacer_i2c *i2c = dev_id;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	unsigned char byte;
3628c2ecf20Sopenharmony_ci	unsigned char bsr, bcr;
3638c2ecf20Sopenharmony_ci	int ret;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	bcr = readb(i2c->base + SYNQUACER_I2C_REG_BCR);
3668c2ecf20Sopenharmony_ci	bsr = readb(i2c->base + SYNQUACER_I2C_REG_BSR);
3678c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "bsr:0x%02x, bcr:0x%02x\n", bsr, bcr);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (bcr & SYNQUACER_I2C_BCR_BER) {
3708c2ecf20Sopenharmony_ci		dev_err(i2c->dev, "bus error\n");
3718c2ecf20Sopenharmony_ci		synquacer_i2c_stop(i2c, -EAGAIN);
3728c2ecf20Sopenharmony_ci		goto out;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci	if ((bsr & SYNQUACER_I2C_BSR_AL) ||
3758c2ecf20Sopenharmony_ci	    !(bcr & SYNQUACER_I2C_BCR_MSS)) {
3768c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "arbitration lost\n");
3778c2ecf20Sopenharmony_ci		synquacer_i2c_stop(i2c, -EAGAIN);
3788c2ecf20Sopenharmony_ci		goto out;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	switch (i2c->state) {
3828c2ecf20Sopenharmony_ci	case STATE_START:
3838c2ecf20Sopenharmony_ci		if (bsr & SYNQUACER_I2C_BSR_LRB) {
3848c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "ack was not received\n");
3858c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, -EAGAIN);
3868c2ecf20Sopenharmony_ci			goto out;
3878c2ecf20Sopenharmony_ci		}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci		if (i2c->msg->flags & I2C_M_RD)
3908c2ecf20Sopenharmony_ci			i2c->state = STATE_READ;
3918c2ecf20Sopenharmony_ci		else
3928c2ecf20Sopenharmony_ci			i2c->state = STATE_WRITE;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci		if (is_lastmsg(i2c) && i2c->msg->len == 0) {
3958c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, 0);
3968c2ecf20Sopenharmony_ci			goto out;
3978c2ecf20Sopenharmony_ci		}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci		if (i2c->state == STATE_READ)
4008c2ecf20Sopenharmony_ci			goto prepare_read;
4018c2ecf20Sopenharmony_ci		fallthrough;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	case STATE_WRITE:
4048c2ecf20Sopenharmony_ci		if (bsr & SYNQUACER_I2C_BSR_LRB) {
4058c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "WRITE: No Ack\n");
4068c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, -EAGAIN);
4078c2ecf20Sopenharmony_ci			goto out;
4088c2ecf20Sopenharmony_ci		}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci		if (!is_msgend(i2c)) {
4118c2ecf20Sopenharmony_ci			writeb(i2c->msg->buf[i2c->msg_ptr++],
4128c2ecf20Sopenharmony_ci			       i2c->base + SYNQUACER_I2C_REG_DAR);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci			/* clear IRQ, and continue */
4158c2ecf20Sopenharmony_ci			writeb(SYNQUACER_I2C_BCR_BEIE |
4168c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_MSS |
4178c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_INTE,
4188c2ecf20Sopenharmony_ci			       i2c->base + SYNQUACER_I2C_REG_BCR);
4198c2ecf20Sopenharmony_ci			break;
4208c2ecf20Sopenharmony_ci		}
4218c2ecf20Sopenharmony_ci		if (is_lastmsg(i2c)) {
4228c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, 0);
4238c2ecf20Sopenharmony_ci			break;
4248c2ecf20Sopenharmony_ci		}
4258c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "WRITE: Next Message\n");
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci		i2c->msg_ptr = 0;
4288c2ecf20Sopenharmony_ci		i2c->msg_idx++;
4298c2ecf20Sopenharmony_ci		i2c->msg++;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci		/* send the new start */
4328c2ecf20Sopenharmony_ci		ret = synquacer_i2c_master_start(i2c, i2c->msg);
4338c2ecf20Sopenharmony_ci		if (ret < 0) {
4348c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "restart error (%d)\n", ret);
4358c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, -EAGAIN);
4368c2ecf20Sopenharmony_ci			break;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci		i2c->state = STATE_START;
4398c2ecf20Sopenharmony_ci		break;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	case STATE_READ:
4428c2ecf20Sopenharmony_ci		byte = readb(i2c->base + SYNQUACER_I2C_REG_DAR);
4438c2ecf20Sopenharmony_ci		if (!(bsr & SYNQUACER_I2C_BSR_FBT)) /* data */
4448c2ecf20Sopenharmony_ci			i2c->msg->buf[i2c->msg_ptr++] = byte;
4458c2ecf20Sopenharmony_ci		else /* address */
4468c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "address:0x%02x. ignore it.\n", byte);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ciprepare_read:
4498c2ecf20Sopenharmony_ci		if (is_msglast(i2c)) {
4508c2ecf20Sopenharmony_ci			writeb(SYNQUACER_I2C_BCR_MSS |
4518c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_BEIE |
4528c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_INTE,
4538c2ecf20Sopenharmony_ci			       i2c->base + SYNQUACER_I2C_REG_BCR);
4548c2ecf20Sopenharmony_ci			break;
4558c2ecf20Sopenharmony_ci		}
4568c2ecf20Sopenharmony_ci		if (!is_msgend(i2c)) {
4578c2ecf20Sopenharmony_ci			writeb(SYNQUACER_I2C_BCR_MSS |
4588c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_BEIE |
4598c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_INTE |
4608c2ecf20Sopenharmony_ci			       SYNQUACER_I2C_BCR_ACK,
4618c2ecf20Sopenharmony_ci			       i2c->base + SYNQUACER_I2C_REG_BCR);
4628c2ecf20Sopenharmony_ci			break;
4638c2ecf20Sopenharmony_ci		}
4648c2ecf20Sopenharmony_ci		if (is_lastmsg(i2c)) {
4658c2ecf20Sopenharmony_ci			/* last message, send stop and complete */
4668c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "READ: Send Stop\n");
4678c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, 0);
4688c2ecf20Sopenharmony_ci			break;
4698c2ecf20Sopenharmony_ci		}
4708c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "READ: Next Transfer\n");
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci		i2c->msg_ptr = 0;
4738c2ecf20Sopenharmony_ci		i2c->msg_idx++;
4748c2ecf20Sopenharmony_ci		i2c->msg++;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		ret = synquacer_i2c_master_start(i2c, i2c->msg);
4778c2ecf20Sopenharmony_ci		if (ret < 0) {
4788c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "restart error (%d)\n", ret);
4798c2ecf20Sopenharmony_ci			synquacer_i2c_stop(i2c, -EAGAIN);
4808c2ecf20Sopenharmony_ci		} else {
4818c2ecf20Sopenharmony_ci			i2c->state = STATE_START;
4828c2ecf20Sopenharmony_ci		}
4838c2ecf20Sopenharmony_ci		break;
4848c2ecf20Sopenharmony_ci	default:
4858c2ecf20Sopenharmony_ci		dev_err(i2c->dev, "called in err STATE (%d)\n", i2c->state);
4868c2ecf20Sopenharmony_ci		break;
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ciout:
4908c2ecf20Sopenharmony_ci	WAIT_PCLK(10, i2c->pclkrate);
4918c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4928c2ecf20Sopenharmony_ci}
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic int synquacer_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
4958c2ecf20Sopenharmony_ci			      int num)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	struct synquacer_i2c *i2c;
4988c2ecf20Sopenharmony_ci	int retry;
4998c2ecf20Sopenharmony_ci	int ret;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	i2c = i2c_get_adapdata(adap);
5028c2ecf20Sopenharmony_ci	i2c->timeout_ms = calc_timeout_ms(i2c, msgs, num);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "calculated timeout %d ms\n", i2c->timeout_ms);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	for (retry = 0; retry <= adap->retries; retry++) {
5078c2ecf20Sopenharmony_ci		ret = synquacer_i2c_doxfer(i2c, msgs, num);
5088c2ecf20Sopenharmony_ci		if (ret != -EAGAIN)
5098c2ecf20Sopenharmony_ci			return ret;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci		synquacer_i2c_hw_reset(i2c);
5148c2ecf20Sopenharmony_ci	}
5158c2ecf20Sopenharmony_ci	return -EIO;
5168c2ecf20Sopenharmony_ci}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic u32 synquacer_i2c_functionality(struct i2c_adapter *adap)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic const struct i2c_algorithm synquacer_i2c_algo = {
5248c2ecf20Sopenharmony_ci	.master_xfer	= synquacer_i2c_xfer,
5258c2ecf20Sopenharmony_ci	.functionality	= synquacer_i2c_functionality,
5268c2ecf20Sopenharmony_ci};
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic const struct i2c_adapter synquacer_i2c_ops = {
5298c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
5308c2ecf20Sopenharmony_ci	.name		= "synquacer_i2c-adapter",
5318c2ecf20Sopenharmony_ci	.algo		= &synquacer_i2c_algo,
5328c2ecf20Sopenharmony_ci	.retries	= 5,
5338c2ecf20Sopenharmony_ci};
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_cistatic int synquacer_i2c_probe(struct platform_device *pdev)
5368c2ecf20Sopenharmony_ci{
5378c2ecf20Sopenharmony_ci	struct synquacer_i2c *i2c;
5388c2ecf20Sopenharmony_ci	u32 bus_speed;
5398c2ecf20Sopenharmony_ci	int ret;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
5428c2ecf20Sopenharmony_ci	if (!i2c)
5438c2ecf20Sopenharmony_ci		return -ENOMEM;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	bus_speed = i2c_acpi_find_bus_speed(&pdev->dev);
5468c2ecf20Sopenharmony_ci	if (!bus_speed)
5478c2ecf20Sopenharmony_ci		device_property_read_u32(&pdev->dev, "clock-frequency",
5488c2ecf20Sopenharmony_ci					 &bus_speed);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	device_property_read_u32(&pdev->dev, "socionext,pclk-rate",
5518c2ecf20Sopenharmony_ci				 &i2c->pclkrate);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	i2c->pclk = devm_clk_get(&pdev->dev, "pclk");
5548c2ecf20Sopenharmony_ci	if (PTR_ERR(i2c->pclk) == -EPROBE_DEFER)
5558c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
5568c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(i2c->pclk)) {
5578c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "clock source %p\n", i2c->pclk);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(i2c->pclk);
5608c2ecf20Sopenharmony_ci		if (ret) {
5618c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "failed to enable clock (%d)\n",
5628c2ecf20Sopenharmony_ci				ret);
5638c2ecf20Sopenharmony_ci			return ret;
5648c2ecf20Sopenharmony_ci		}
5658c2ecf20Sopenharmony_ci		i2c->pclkrate = clk_get_rate(i2c->pclk);
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (i2c->pclkrate < SYNQUACER_I2C_MIN_CLK_RATE ||
5698c2ecf20Sopenharmony_ci	    i2c->pclkrate > SYNQUACER_I2C_MAX_CLK_RATE) {
5708c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "PCLK missing or out of range (%d)\n",
5718c2ecf20Sopenharmony_ci			i2c->pclkrate);
5728c2ecf20Sopenharmony_ci		return -EINVAL;
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	i2c->base = devm_platform_ioremap_resource(pdev, 0);
5768c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->base))
5778c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->base);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	i2c->irq = platform_get_irq(pdev, 0);
5808c2ecf20Sopenharmony_ci	if (i2c->irq < 0)
5818c2ecf20Sopenharmony_ci		return i2c->irq;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, i2c->irq, synquacer_i2c_isr,
5848c2ecf20Sopenharmony_ci			       0, dev_name(&pdev->dev), i2c);
5858c2ecf20Sopenharmony_ci	if (ret < 0) {
5868c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
5878c2ecf20Sopenharmony_ci		return ret;
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	i2c->state = STATE_IDLE;
5918c2ecf20Sopenharmony_ci	i2c->dev = &pdev->dev;
5928c2ecf20Sopenharmony_ci	i2c->adapter = synquacer_i2c_ops;
5938c2ecf20Sopenharmony_ci	i2c_set_adapdata(&i2c->adapter, i2c);
5948c2ecf20Sopenharmony_ci	i2c->adapter.dev.parent = &pdev->dev;
5958c2ecf20Sopenharmony_ci	i2c->adapter.dev.of_node = pdev->dev.of_node;
5968c2ecf20Sopenharmony_ci	ACPI_COMPANION_SET(&i2c->adapter.dev, ACPI_COMPANION(&pdev->dev));
5978c2ecf20Sopenharmony_ci	i2c->adapter.nr = pdev->id;
5988c2ecf20Sopenharmony_ci	init_completion(&i2c->completion);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (bus_speed < I2C_MAX_FAST_MODE_FREQ)
6018c2ecf20Sopenharmony_ci		i2c->speed_khz = SYNQUACER_I2C_SPEED_SM;
6028c2ecf20Sopenharmony_ci	else
6038c2ecf20Sopenharmony_ci		i2c->speed_khz = SYNQUACER_I2C_SPEED_FM;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	synquacer_i2c_hw_init(i2c);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	ret = i2c_add_numbered_adapter(&i2c->adapter);
6088c2ecf20Sopenharmony_ci	if (ret) {
6098c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to add bus to i2c core\n");
6108c2ecf20Sopenharmony_ci		return ret;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, i2c);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "%s: synquacer_i2c adapter\n",
6168c2ecf20Sopenharmony_ci		 dev_name(&i2c->adapter.dev));
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return 0;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_cistatic int synquacer_i2c_remove(struct platform_device *pdev)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct synquacer_i2c *i2c = platform_get_drvdata(pdev);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	i2c_del_adapter(&i2c->adapter);
6268c2ecf20Sopenharmony_ci	if (!IS_ERR(i2c->pclk))
6278c2ecf20Sopenharmony_ci		clk_disable_unprepare(i2c->pclk);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	return 0;
6308c2ecf20Sopenharmony_ci};
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic const struct of_device_id synquacer_i2c_dt_ids[] = {
6338c2ecf20Sopenharmony_ci	{ .compatible = "socionext,synquacer-i2c" },
6348c2ecf20Sopenharmony_ci	{ /* sentinel */ }
6358c2ecf20Sopenharmony_ci};
6368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, synquacer_i2c_dt_ids);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI
6398c2ecf20Sopenharmony_cistatic const struct acpi_device_id synquacer_i2c_acpi_ids[] = {
6408c2ecf20Sopenharmony_ci	{ "SCX0003" },
6418c2ecf20Sopenharmony_ci	{ /* sentinel */ }
6428c2ecf20Sopenharmony_ci};
6438c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, synquacer_i2c_acpi_ids);
6448c2ecf20Sopenharmony_ci#endif
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_cistatic struct platform_driver synquacer_i2c_driver = {
6478c2ecf20Sopenharmony_ci	.probe	= synquacer_i2c_probe,
6488c2ecf20Sopenharmony_ci	.remove	= synquacer_i2c_remove,
6498c2ecf20Sopenharmony_ci	.driver	= {
6508c2ecf20Sopenharmony_ci		.name = "synquacer_i2c",
6518c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(synquacer_i2c_dt_ids),
6528c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(synquacer_i2c_acpi_ids),
6538c2ecf20Sopenharmony_ci	},
6548c2ecf20Sopenharmony_ci};
6558c2ecf20Sopenharmony_cimodule_platform_driver(synquacer_i2c_driver);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ciMODULE_AUTHOR("Fujitsu Semiconductor Ltd");
6588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Socionext SynQuacer I2C Driver");
6598c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
660