18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * i2c-exynos5.c - Samsung Exynos5 I2C Controller Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 Samsung Electronics Co., Ltd.
68c2ecf20Sopenharmony_ci*/
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/time.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/errno.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/clk.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <linux/of_address.h>
228c2ecf20Sopenharmony_ci#include <linux/of_device.h>
238c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
248c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * HSI2C controller from Samsung supports 2 modes of operation
288c2ecf20Sopenharmony_ci * 1. Auto mode: Where in master automatically controls the whole transaction
298c2ecf20Sopenharmony_ci * 2. Manual mode: Software controls the transaction by issuing commands
308c2ecf20Sopenharmony_ci *    START, READ, WRITE, STOP, RESTART in I2C_MANUAL_CMD register.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Operation mode can be selected by setting AUTO_MODE bit in I2C_CONF register
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * Special bits are available for both modes of operation to set commands
358c2ecf20Sopenharmony_ci * and for checking transfer status
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Register Map */
398c2ecf20Sopenharmony_ci#define HSI2C_CTL		0x00
408c2ecf20Sopenharmony_ci#define HSI2C_FIFO_CTL		0x04
418c2ecf20Sopenharmony_ci#define HSI2C_TRAILIG_CTL	0x08
428c2ecf20Sopenharmony_ci#define HSI2C_CLK_CTL		0x0C
438c2ecf20Sopenharmony_ci#define HSI2C_CLK_SLOT		0x10
448c2ecf20Sopenharmony_ci#define HSI2C_INT_ENABLE	0x20
458c2ecf20Sopenharmony_ci#define HSI2C_INT_STATUS	0x24
468c2ecf20Sopenharmony_ci#define HSI2C_ERR_STATUS	0x2C
478c2ecf20Sopenharmony_ci#define HSI2C_FIFO_STATUS	0x30
488c2ecf20Sopenharmony_ci#define HSI2C_TX_DATA		0x34
498c2ecf20Sopenharmony_ci#define HSI2C_RX_DATA		0x38
508c2ecf20Sopenharmony_ci#define HSI2C_CONF		0x40
518c2ecf20Sopenharmony_ci#define HSI2C_AUTO_CONF		0x44
528c2ecf20Sopenharmony_ci#define HSI2C_TIMEOUT		0x48
538c2ecf20Sopenharmony_ci#define HSI2C_MANUAL_CMD	0x4C
548c2ecf20Sopenharmony_ci#define HSI2C_TRANS_STATUS	0x50
558c2ecf20Sopenharmony_ci#define HSI2C_TIMING_HS1	0x54
568c2ecf20Sopenharmony_ci#define HSI2C_TIMING_HS2	0x58
578c2ecf20Sopenharmony_ci#define HSI2C_TIMING_HS3	0x5C
588c2ecf20Sopenharmony_ci#define HSI2C_TIMING_FS1	0x60
598c2ecf20Sopenharmony_ci#define HSI2C_TIMING_FS2	0x64
608c2ecf20Sopenharmony_ci#define HSI2C_TIMING_FS3	0x68
618c2ecf20Sopenharmony_ci#define HSI2C_TIMING_SLA	0x6C
628c2ecf20Sopenharmony_ci#define HSI2C_ADDR		0x70
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/* I2C_CTL Register bits */
658c2ecf20Sopenharmony_ci#define HSI2C_FUNC_MODE_I2C			(1u << 0)
668c2ecf20Sopenharmony_ci#define HSI2C_MASTER				(1u << 3)
678c2ecf20Sopenharmony_ci#define HSI2C_RXCHON				(1u << 6)
688c2ecf20Sopenharmony_ci#define HSI2C_TXCHON				(1u << 7)
698c2ecf20Sopenharmony_ci#define HSI2C_SW_RST				(1u << 31)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* I2C_FIFO_CTL Register bits */
728c2ecf20Sopenharmony_ci#define HSI2C_RXFIFO_EN				(1u << 0)
738c2ecf20Sopenharmony_ci#define HSI2C_TXFIFO_EN				(1u << 1)
748c2ecf20Sopenharmony_ci#define HSI2C_RXFIFO_TRIGGER_LEVEL(x)		((x) << 4)
758c2ecf20Sopenharmony_ci#define HSI2C_TXFIFO_TRIGGER_LEVEL(x)		((x) << 16)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/* I2C_TRAILING_CTL Register bits */
788c2ecf20Sopenharmony_ci#define HSI2C_TRAILING_COUNT			(0xf)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* I2C_INT_EN Register bits */
818c2ecf20Sopenharmony_ci#define HSI2C_INT_TX_ALMOSTEMPTY_EN		(1u << 0)
828c2ecf20Sopenharmony_ci#define HSI2C_INT_RX_ALMOSTFULL_EN		(1u << 1)
838c2ecf20Sopenharmony_ci#define HSI2C_INT_TRAILING_EN			(1u << 6)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/* I2C_INT_STAT Register bits */
868c2ecf20Sopenharmony_ci#define HSI2C_INT_TX_ALMOSTEMPTY		(1u << 0)
878c2ecf20Sopenharmony_ci#define HSI2C_INT_RX_ALMOSTFULL			(1u << 1)
888c2ecf20Sopenharmony_ci#define HSI2C_INT_TX_UNDERRUN			(1u << 2)
898c2ecf20Sopenharmony_ci#define HSI2C_INT_TX_OVERRUN			(1u << 3)
908c2ecf20Sopenharmony_ci#define HSI2C_INT_RX_UNDERRUN			(1u << 4)
918c2ecf20Sopenharmony_ci#define HSI2C_INT_RX_OVERRUN			(1u << 5)
928c2ecf20Sopenharmony_ci#define HSI2C_INT_TRAILING			(1u << 6)
938c2ecf20Sopenharmony_ci#define HSI2C_INT_I2C				(1u << 9)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define HSI2C_INT_TRANS_DONE			(1u << 7)
968c2ecf20Sopenharmony_ci#define HSI2C_INT_TRANS_ABORT			(1u << 8)
978c2ecf20Sopenharmony_ci#define HSI2C_INT_NO_DEV_ACK			(1u << 9)
988c2ecf20Sopenharmony_ci#define HSI2C_INT_NO_DEV			(1u << 10)
998c2ecf20Sopenharmony_ci#define HSI2C_INT_TIMEOUT			(1u << 11)
1008c2ecf20Sopenharmony_ci#define HSI2C_INT_I2C_TRANS			(HSI2C_INT_TRANS_DONE |	\
1018c2ecf20Sopenharmony_ci						HSI2C_INT_TRANS_ABORT |	\
1028c2ecf20Sopenharmony_ci						HSI2C_INT_NO_DEV_ACK |	\
1038c2ecf20Sopenharmony_ci						HSI2C_INT_NO_DEV |	\
1048c2ecf20Sopenharmony_ci						HSI2C_INT_TIMEOUT)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* I2C_FIFO_STAT Register bits */
1078c2ecf20Sopenharmony_ci#define HSI2C_RX_FIFO_EMPTY			(1u << 24)
1088c2ecf20Sopenharmony_ci#define HSI2C_RX_FIFO_FULL			(1u << 23)
1098c2ecf20Sopenharmony_ci#define HSI2C_RX_FIFO_LVL(x)			((x >> 16) & 0x7f)
1108c2ecf20Sopenharmony_ci#define HSI2C_TX_FIFO_EMPTY			(1u << 8)
1118c2ecf20Sopenharmony_ci#define HSI2C_TX_FIFO_FULL			(1u << 7)
1128c2ecf20Sopenharmony_ci#define HSI2C_TX_FIFO_LVL(x)			((x >> 0) & 0x7f)
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* I2C_CONF Register bits */
1158c2ecf20Sopenharmony_ci#define HSI2C_AUTO_MODE				(1u << 31)
1168c2ecf20Sopenharmony_ci#define HSI2C_10BIT_ADDR_MODE			(1u << 30)
1178c2ecf20Sopenharmony_ci#define HSI2C_HS_MODE				(1u << 29)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* I2C_AUTO_CONF Register bits */
1208c2ecf20Sopenharmony_ci#define HSI2C_READ_WRITE			(1u << 16)
1218c2ecf20Sopenharmony_ci#define HSI2C_STOP_AFTER_TRANS			(1u << 17)
1228c2ecf20Sopenharmony_ci#define HSI2C_MASTER_RUN			(1u << 31)
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci/* I2C_TIMEOUT Register bits */
1258c2ecf20Sopenharmony_ci#define HSI2C_TIMEOUT_EN			(1u << 31)
1268c2ecf20Sopenharmony_ci#define HSI2C_TIMEOUT_MASK			0xff
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/* I2C_MANUAL_CMD register bits */
1298c2ecf20Sopenharmony_ci#define HSI2C_CMD_READ_DATA			(1u << 4)
1308c2ecf20Sopenharmony_ci#define HSI2C_CMD_SEND_STOP			(1u << 2)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* I2C_TRANS_STATUS register bits */
1338c2ecf20Sopenharmony_ci#define HSI2C_MASTER_BUSY			(1u << 17)
1348c2ecf20Sopenharmony_ci#define HSI2C_SLAVE_BUSY			(1u << 16)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* I2C_TRANS_STATUS register bits for Exynos5 variant */
1378c2ecf20Sopenharmony_ci#define HSI2C_TIMEOUT_AUTO			(1u << 4)
1388c2ecf20Sopenharmony_ci#define HSI2C_NO_DEV				(1u << 3)
1398c2ecf20Sopenharmony_ci#define HSI2C_NO_DEV_ACK			(1u << 2)
1408c2ecf20Sopenharmony_ci#define HSI2C_TRANS_ABORT			(1u << 1)
1418c2ecf20Sopenharmony_ci#define HSI2C_TRANS_DONE			(1u << 0)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* I2C_TRANS_STATUS register bits for Exynos7 variant */
1448c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_MASK			0xf
1458c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_IDLE			0x0
1468c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_START			0x1
1478c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_RESTART			0x2
1488c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_STOP			0x3
1498c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_MASTER_ID		0x4
1508c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_ADDR0			0x5
1518c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_ADDR1			0x6
1528c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_ADDR2			0x7
1538c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_ADDR_SR			0x8
1548c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_READ			0x9
1558c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_WRITE			0xa
1568c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_NO_ACK			0xb
1578c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_LOSE			0xc
1588c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_WAIT			0xd
1598c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ST_WAIT_CMD		0xe
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* I2C_ADDR register bits */
1628c2ecf20Sopenharmony_ci#define HSI2C_SLV_ADDR_SLV(x)			((x & 0x3ff) << 0)
1638c2ecf20Sopenharmony_ci#define HSI2C_SLV_ADDR_MAS(x)			((x & 0x3ff) << 10)
1648c2ecf20Sopenharmony_ci#define HSI2C_MASTER_ID(x)			((x & 0xff) << 24)
1658c2ecf20Sopenharmony_ci#define MASTER_ID(x)				((x & 0x7) + 0x08)
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#define EXYNOS5_I2C_TIMEOUT (msecs_to_jiffies(100))
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cienum i2c_type_exynos {
1708c2ecf20Sopenharmony_ci	I2C_TYPE_EXYNOS5,
1718c2ecf20Sopenharmony_ci	I2C_TYPE_EXYNOS7,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistruct exynos5_i2c {
1758c2ecf20Sopenharmony_ci	struct i2c_adapter	adap;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	struct i2c_msg		*msg;
1788c2ecf20Sopenharmony_ci	struct completion	msg_complete;
1798c2ecf20Sopenharmony_ci	unsigned int		msg_ptr;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	unsigned int		irq;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	void __iomem		*regs;
1848c2ecf20Sopenharmony_ci	struct clk		*clk;
1858c2ecf20Sopenharmony_ci	struct device		*dev;
1868c2ecf20Sopenharmony_ci	int			state;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	spinlock_t		lock;		/* IRQ synchronization */
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/*
1918c2ecf20Sopenharmony_ci	 * Since the TRANS_DONE bit is cleared on read, and we may read it
1928c2ecf20Sopenharmony_ci	 * either during an IRQ or after a transaction, keep track of its
1938c2ecf20Sopenharmony_ci	 * state here.
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci	int			trans_done;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* Controller operating frequency */
1988c2ecf20Sopenharmony_ci	unsigned int		op_clock;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Version of HS-I2C Hardware */
2018c2ecf20Sopenharmony_ci	const struct exynos_hsi2c_variant *variant;
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/**
2058c2ecf20Sopenharmony_ci * struct exynos_hsi2c_variant - platform specific HSI2C driver data
2068c2ecf20Sopenharmony_ci * @fifo_depth: the fifo depth supported by the HSI2C module
2078c2ecf20Sopenharmony_ci * @hw: the hardware variant of Exynos I2C controller
2088c2ecf20Sopenharmony_ci *
2098c2ecf20Sopenharmony_ci * Specifies platform specific configuration of HSI2C module.
2108c2ecf20Sopenharmony_ci * Note: A structure for driver specific platform data is used for future
2118c2ecf20Sopenharmony_ci * expansion of its usage.
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_cistruct exynos_hsi2c_variant {
2148c2ecf20Sopenharmony_ci	unsigned int		fifo_depth;
2158c2ecf20Sopenharmony_ci	enum i2c_type_exynos	hw;
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic const struct exynos_hsi2c_variant exynos5250_hsi2c_data = {
2198c2ecf20Sopenharmony_ci	.fifo_depth	= 64,
2208c2ecf20Sopenharmony_ci	.hw		= I2C_TYPE_EXYNOS5,
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic const struct exynos_hsi2c_variant exynos5260_hsi2c_data = {
2248c2ecf20Sopenharmony_ci	.fifo_depth	= 16,
2258c2ecf20Sopenharmony_ci	.hw		= I2C_TYPE_EXYNOS5,
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic const struct exynos_hsi2c_variant exynos7_hsi2c_data = {
2298c2ecf20Sopenharmony_ci	.fifo_depth	= 16,
2308c2ecf20Sopenharmony_ci	.hw		= I2C_TYPE_EXYNOS7,
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic const struct of_device_id exynos5_i2c_match[] = {
2348c2ecf20Sopenharmony_ci	{
2358c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5-hsi2c",
2368c2ecf20Sopenharmony_ci		.data = &exynos5250_hsi2c_data
2378c2ecf20Sopenharmony_ci	}, {
2388c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5250-hsi2c",
2398c2ecf20Sopenharmony_ci		.data = &exynos5250_hsi2c_data
2408c2ecf20Sopenharmony_ci	}, {
2418c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos5260-hsi2c",
2428c2ecf20Sopenharmony_ci		.data = &exynos5260_hsi2c_data
2438c2ecf20Sopenharmony_ci	}, {
2448c2ecf20Sopenharmony_ci		.compatible = "samsung,exynos7-hsi2c",
2458c2ecf20Sopenharmony_ci		.data = &exynos7_hsi2c_data
2468c2ecf20Sopenharmony_ci	}, {},
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, exynos5_i2c_match);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic void exynos5_i2c_clr_pend_irq(struct exynos5_i2c *i2c)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	writel(readl(i2c->regs + HSI2C_INT_STATUS),
2538c2ecf20Sopenharmony_ci				i2c->regs + HSI2C_INT_STATUS);
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/*
2578c2ecf20Sopenharmony_ci * exynos5_i2c_set_timing: updates the registers with appropriate
2588c2ecf20Sopenharmony_ci * timing values calculated
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * Timing values for operation are calculated against either 100kHz
2618c2ecf20Sopenharmony_ci * or 1MHz controller operating frequency.
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci * Returns 0 on success, -EINVAL if the cycle length cannot
2648c2ecf20Sopenharmony_ci * be calculated.
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_cistatic int exynos5_i2c_set_timing(struct exynos5_i2c *i2c, bool hs_timings)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	u32 i2c_timing_s1;
2698c2ecf20Sopenharmony_ci	u32 i2c_timing_s2;
2708c2ecf20Sopenharmony_ci	u32 i2c_timing_s3;
2718c2ecf20Sopenharmony_ci	u32 i2c_timing_sla;
2728c2ecf20Sopenharmony_ci	unsigned int t_start_su, t_start_hd;
2738c2ecf20Sopenharmony_ci	unsigned int t_stop_su;
2748c2ecf20Sopenharmony_ci	unsigned int t_data_su, t_data_hd;
2758c2ecf20Sopenharmony_ci	unsigned int t_scl_l, t_scl_h;
2768c2ecf20Sopenharmony_ci	unsigned int t_sr_release;
2778c2ecf20Sopenharmony_ci	unsigned int t_ftl_cycle;
2788c2ecf20Sopenharmony_ci	unsigned int clkin = clk_get_rate(i2c->clk);
2798c2ecf20Sopenharmony_ci	unsigned int op_clk = hs_timings ? i2c->op_clock :
2808c2ecf20Sopenharmony_ci		(i2c->op_clock >= I2C_MAX_FAST_MODE_PLUS_FREQ) ? I2C_MAX_STANDARD_MODE_FREQ :
2818c2ecf20Sopenharmony_ci		i2c->op_clock;
2828c2ecf20Sopenharmony_ci	int div, clk_cycle, temp;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/*
2858c2ecf20Sopenharmony_ci	 * In case of HSI2C controller in Exynos5 series
2868c2ecf20Sopenharmony_ci	 * FPCLK / FI2C =
2878c2ecf20Sopenharmony_ci	 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + 2 * FLT_CYCLE
2888c2ecf20Sopenharmony_ci	 *
2898c2ecf20Sopenharmony_ci	 * In case of HSI2C controllers in Exynos7 series
2908c2ecf20Sopenharmony_ci	 * FPCLK / FI2C =
2918c2ecf20Sopenharmony_ci	 * (CLK_DIV + 1) * (TSCLK_L + TSCLK_H + 2) + 8 + FLT_CYCLE
2928c2ecf20Sopenharmony_ci	 *
2938c2ecf20Sopenharmony_ci	 * clk_cycle := TSCLK_L + TSCLK_H
2948c2ecf20Sopenharmony_ci	 * temp := (CLK_DIV + 1) * (clk_cycle + 2)
2958c2ecf20Sopenharmony_ci	 *
2968c2ecf20Sopenharmony_ci	 * Constraints: 4 <= temp, 0 <= CLK_DIV < 256, 2 <= clk_cycle <= 510
2978c2ecf20Sopenharmony_ci	 *
2988c2ecf20Sopenharmony_ci	 */
2998c2ecf20Sopenharmony_ci	t_ftl_cycle = (readl(i2c->regs + HSI2C_CONF) >> 16) & 0x7;
3008c2ecf20Sopenharmony_ci	temp = clkin / op_clk - 8 - t_ftl_cycle;
3018c2ecf20Sopenharmony_ci	if (i2c->variant->hw != I2C_TYPE_EXYNOS7)
3028c2ecf20Sopenharmony_ci		temp -= t_ftl_cycle;
3038c2ecf20Sopenharmony_ci	div = temp / 512;
3048c2ecf20Sopenharmony_ci	clk_cycle = temp / (div + 1) - 2;
3058c2ecf20Sopenharmony_ci	if (temp < 4 || div >= 256 || clk_cycle < 2) {
3068c2ecf20Sopenharmony_ci		dev_err(i2c->dev, "%s clock set-up failed\n",
3078c2ecf20Sopenharmony_ci			hs_timings ? "HS" : "FS");
3088c2ecf20Sopenharmony_ci		return -EINVAL;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	t_scl_l = clk_cycle / 2;
3128c2ecf20Sopenharmony_ci	t_scl_h = clk_cycle / 2;
3138c2ecf20Sopenharmony_ci	t_start_su = t_scl_l;
3148c2ecf20Sopenharmony_ci	t_start_hd = t_scl_l;
3158c2ecf20Sopenharmony_ci	t_stop_su = t_scl_l;
3168c2ecf20Sopenharmony_ci	t_data_su = t_scl_l / 2;
3178c2ecf20Sopenharmony_ci	t_data_hd = t_scl_l / 2;
3188c2ecf20Sopenharmony_ci	t_sr_release = clk_cycle;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	i2c_timing_s1 = t_start_su << 24 | t_start_hd << 16 | t_stop_su << 8;
3218c2ecf20Sopenharmony_ci	i2c_timing_s2 = t_data_su << 24 | t_scl_l << 8 | t_scl_h << 0;
3228c2ecf20Sopenharmony_ci	i2c_timing_s3 = div << 16 | t_sr_release << 0;
3238c2ecf20Sopenharmony_ci	i2c_timing_sla = t_data_hd << 0;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "tSTART_SU: %X, tSTART_HD: %X, tSTOP_SU: %X\n",
3268c2ecf20Sopenharmony_ci		t_start_su, t_start_hd, t_stop_su);
3278c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "tDATA_SU: %X, tSCL_L: %X, tSCL_H: %X\n",
3288c2ecf20Sopenharmony_ci		t_data_su, t_scl_l, t_scl_h);
3298c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "nClkDiv: %X, tSR_RELEASE: %X\n",
3308c2ecf20Sopenharmony_ci		div, t_sr_release);
3318c2ecf20Sopenharmony_ci	dev_dbg(i2c->dev, "tDATA_HD: %X\n", t_data_hd);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (hs_timings) {
3348c2ecf20Sopenharmony_ci		writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_HS1);
3358c2ecf20Sopenharmony_ci		writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_HS2);
3368c2ecf20Sopenharmony_ci		writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_HS3);
3378c2ecf20Sopenharmony_ci	} else {
3388c2ecf20Sopenharmony_ci		writel(i2c_timing_s1, i2c->regs + HSI2C_TIMING_FS1);
3398c2ecf20Sopenharmony_ci		writel(i2c_timing_s2, i2c->regs + HSI2C_TIMING_FS2);
3408c2ecf20Sopenharmony_ci		writel(i2c_timing_s3, i2c->regs + HSI2C_TIMING_FS3);
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci	writel(i2c_timing_sla, i2c->regs + HSI2C_TIMING_SLA);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int exynos5_hsi2c_clock_setup(struct exynos5_i2c *i2c)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	/* always set Fast Speed timings */
3508c2ecf20Sopenharmony_ci	int ret = exynos5_i2c_set_timing(i2c, false);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (ret < 0 || i2c->op_clock < I2C_MAX_FAST_MODE_PLUS_FREQ)
3538c2ecf20Sopenharmony_ci		return ret;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	return exynos5_i2c_set_timing(i2c, true);
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci/*
3598c2ecf20Sopenharmony_ci * exynos5_i2c_init: configures the controller for I2C functionality
3608c2ecf20Sopenharmony_ci * Programs I2C controller for Master mode operation
3618c2ecf20Sopenharmony_ci */
3628c2ecf20Sopenharmony_cistatic void exynos5_i2c_init(struct exynos5_i2c *i2c)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	u32 i2c_conf = readl(i2c->regs + HSI2C_CONF);
3658c2ecf20Sopenharmony_ci	u32 i2c_timeout = readl(i2c->regs + HSI2C_TIMEOUT);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* Clear to disable Timeout */
3688c2ecf20Sopenharmony_ci	i2c_timeout &= ~HSI2C_TIMEOUT_EN;
3698c2ecf20Sopenharmony_ci	writel(i2c_timeout, i2c->regs + HSI2C_TIMEOUT);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	writel((HSI2C_FUNC_MODE_I2C | HSI2C_MASTER),
3728c2ecf20Sopenharmony_ci					i2c->regs + HSI2C_CTL);
3738c2ecf20Sopenharmony_ci	writel(HSI2C_TRAILING_COUNT, i2c->regs + HSI2C_TRAILIG_CTL);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	if (i2c->op_clock >= I2C_MAX_FAST_MODE_PLUS_FREQ) {
3768c2ecf20Sopenharmony_ci		writel(HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr)),
3778c2ecf20Sopenharmony_ci					i2c->regs + HSI2C_ADDR);
3788c2ecf20Sopenharmony_ci		i2c_conf |= HSI2C_HS_MODE;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	writel(i2c_conf | HSI2C_AUTO_MODE, i2c->regs + HSI2C_CONF);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic void exynos5_i2c_reset(struct exynos5_i2c *i2c)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	u32 i2c_ctl;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* Set and clear the bit for reset */
3898c2ecf20Sopenharmony_ci	i2c_ctl = readl(i2c->regs + HSI2C_CTL);
3908c2ecf20Sopenharmony_ci	i2c_ctl |= HSI2C_SW_RST;
3918c2ecf20Sopenharmony_ci	writel(i2c_ctl, i2c->regs + HSI2C_CTL);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	i2c_ctl = readl(i2c->regs + HSI2C_CTL);
3948c2ecf20Sopenharmony_ci	i2c_ctl &= ~HSI2C_SW_RST;
3958c2ecf20Sopenharmony_ci	writel(i2c_ctl, i2c->regs + HSI2C_CTL);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	/* We don't expect calculations to fail during the run */
3988c2ecf20Sopenharmony_ci	exynos5_hsi2c_clock_setup(i2c);
3998c2ecf20Sopenharmony_ci	/* Initialize the configure registers */
4008c2ecf20Sopenharmony_ci	exynos5_i2c_init(i2c);
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/*
4048c2ecf20Sopenharmony_ci * exynos5_i2c_irq: top level IRQ servicing routine
4058c2ecf20Sopenharmony_ci *
4068c2ecf20Sopenharmony_ci * INT_STATUS registers gives the interrupt details. Further,
4078c2ecf20Sopenharmony_ci * FIFO_STATUS or TRANS_STATUS registers are to be check for detailed
4088c2ecf20Sopenharmony_ci * state of the bus.
4098c2ecf20Sopenharmony_ci */
4108c2ecf20Sopenharmony_cistatic irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c = dev_id;
4138c2ecf20Sopenharmony_ci	u32 fifo_level, int_status, fifo_status, trans_status;
4148c2ecf20Sopenharmony_ci	unsigned char byte;
4158c2ecf20Sopenharmony_ci	int len = 0;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	i2c->state = -EINVAL;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	spin_lock(&i2c->lock);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	int_status = readl(i2c->regs + HSI2C_INT_STATUS);
4228c2ecf20Sopenharmony_ci	writel(int_status, i2c->regs + HSI2C_INT_STATUS);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	/* handle interrupt related to the transfer status */
4258c2ecf20Sopenharmony_ci	if (i2c->variant->hw == I2C_TYPE_EXYNOS7) {
4268c2ecf20Sopenharmony_ci		if (int_status & HSI2C_INT_TRANS_DONE) {
4278c2ecf20Sopenharmony_ci			i2c->trans_done = 1;
4288c2ecf20Sopenharmony_ci			i2c->state = 0;
4298c2ecf20Sopenharmony_ci		} else if (int_status & HSI2C_INT_TRANS_ABORT) {
4308c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "Deal with arbitration lose\n");
4318c2ecf20Sopenharmony_ci			i2c->state = -EAGAIN;
4328c2ecf20Sopenharmony_ci			goto stop;
4338c2ecf20Sopenharmony_ci		} else if (int_status & HSI2C_INT_NO_DEV_ACK) {
4348c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "No ACK from device\n");
4358c2ecf20Sopenharmony_ci			i2c->state = -ENXIO;
4368c2ecf20Sopenharmony_ci			goto stop;
4378c2ecf20Sopenharmony_ci		} else if (int_status & HSI2C_INT_NO_DEV) {
4388c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "No device\n");
4398c2ecf20Sopenharmony_ci			i2c->state = -ENXIO;
4408c2ecf20Sopenharmony_ci			goto stop;
4418c2ecf20Sopenharmony_ci		} else if (int_status & HSI2C_INT_TIMEOUT) {
4428c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "Accessing device timed out\n");
4438c2ecf20Sopenharmony_ci			i2c->state = -ETIMEDOUT;
4448c2ecf20Sopenharmony_ci			goto stop;
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci	} else if (int_status & HSI2C_INT_I2C) {
4478c2ecf20Sopenharmony_ci		trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
4488c2ecf20Sopenharmony_ci		if (trans_status & HSI2C_NO_DEV_ACK) {
4498c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "No ACK from device\n");
4508c2ecf20Sopenharmony_ci			i2c->state = -ENXIO;
4518c2ecf20Sopenharmony_ci			goto stop;
4528c2ecf20Sopenharmony_ci		} else if (trans_status & HSI2C_NO_DEV) {
4538c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "No device\n");
4548c2ecf20Sopenharmony_ci			i2c->state = -ENXIO;
4558c2ecf20Sopenharmony_ci			goto stop;
4568c2ecf20Sopenharmony_ci		} else if (trans_status & HSI2C_TRANS_ABORT) {
4578c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "Deal with arbitration lose\n");
4588c2ecf20Sopenharmony_ci			i2c->state = -EAGAIN;
4598c2ecf20Sopenharmony_ci			goto stop;
4608c2ecf20Sopenharmony_ci		} else if (trans_status & HSI2C_TIMEOUT_AUTO) {
4618c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "Accessing device timed out\n");
4628c2ecf20Sopenharmony_ci			i2c->state = -ETIMEDOUT;
4638c2ecf20Sopenharmony_ci			goto stop;
4648c2ecf20Sopenharmony_ci		} else if (trans_status & HSI2C_TRANS_DONE) {
4658c2ecf20Sopenharmony_ci			i2c->trans_done = 1;
4668c2ecf20Sopenharmony_ci			i2c->state = 0;
4678c2ecf20Sopenharmony_ci		}
4688c2ecf20Sopenharmony_ci	}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if ((i2c->msg->flags & I2C_M_RD) && (int_status &
4718c2ecf20Sopenharmony_ci			(HSI2C_INT_TRAILING | HSI2C_INT_RX_ALMOSTFULL))) {
4728c2ecf20Sopenharmony_ci		fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
4738c2ecf20Sopenharmony_ci		fifo_level = HSI2C_RX_FIFO_LVL(fifo_status);
4748c2ecf20Sopenharmony_ci		len = min(fifo_level, i2c->msg->len - i2c->msg_ptr);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		while (len > 0) {
4778c2ecf20Sopenharmony_ci			byte = (unsigned char)
4788c2ecf20Sopenharmony_ci				readl(i2c->regs + HSI2C_RX_DATA);
4798c2ecf20Sopenharmony_ci			i2c->msg->buf[i2c->msg_ptr++] = byte;
4808c2ecf20Sopenharmony_ci			len--;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci		i2c->state = 0;
4838c2ecf20Sopenharmony_ci	} else if (int_status & HSI2C_INT_TX_ALMOSTEMPTY) {
4848c2ecf20Sopenharmony_ci		fifo_status = readl(i2c->regs + HSI2C_FIFO_STATUS);
4858c2ecf20Sopenharmony_ci		fifo_level = HSI2C_TX_FIFO_LVL(fifo_status);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci		len = i2c->variant->fifo_depth - fifo_level;
4888c2ecf20Sopenharmony_ci		if (len > (i2c->msg->len - i2c->msg_ptr)) {
4898c2ecf20Sopenharmony_ci			u32 int_en = readl(i2c->regs + HSI2C_INT_ENABLE);
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci			int_en &= ~HSI2C_INT_TX_ALMOSTEMPTY_EN;
4928c2ecf20Sopenharmony_ci			writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
4938c2ecf20Sopenharmony_ci			len = i2c->msg->len - i2c->msg_ptr;
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		while (len > 0) {
4978c2ecf20Sopenharmony_ci			byte = i2c->msg->buf[i2c->msg_ptr++];
4988c2ecf20Sopenharmony_ci			writel(byte, i2c->regs + HSI2C_TX_DATA);
4998c2ecf20Sopenharmony_ci			len--;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci		i2c->state = 0;
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci stop:
5058c2ecf20Sopenharmony_ci	if ((i2c->trans_done && (i2c->msg->len == i2c->msg_ptr)) ||
5068c2ecf20Sopenharmony_ci	    (i2c->state < 0)) {
5078c2ecf20Sopenharmony_ci		writel(0, i2c->regs + HSI2C_INT_ENABLE);
5088c2ecf20Sopenharmony_ci		exynos5_i2c_clr_pend_irq(i2c);
5098c2ecf20Sopenharmony_ci		complete(&i2c->msg_complete);
5108c2ecf20Sopenharmony_ci	}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	spin_unlock(&i2c->lock);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci/*
5188c2ecf20Sopenharmony_ci * exynos5_i2c_wait_bus_idle
5198c2ecf20Sopenharmony_ci *
5208c2ecf20Sopenharmony_ci * Wait for the bus to go idle, indicated by the MASTER_BUSY bit being
5218c2ecf20Sopenharmony_ci * cleared.
5228c2ecf20Sopenharmony_ci *
5238c2ecf20Sopenharmony_ci * Returns -EBUSY if the bus cannot be bought to idle
5248c2ecf20Sopenharmony_ci */
5258c2ecf20Sopenharmony_cistatic int exynos5_i2c_wait_bus_idle(struct exynos5_i2c *i2c)
5268c2ecf20Sopenharmony_ci{
5278c2ecf20Sopenharmony_ci	unsigned long stop_time;
5288c2ecf20Sopenharmony_ci	u32 trans_status;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* wait for 100 milli seconds for the bus to be idle */
5318c2ecf20Sopenharmony_ci	stop_time = jiffies + msecs_to_jiffies(100) + 1;
5328c2ecf20Sopenharmony_ci	do {
5338c2ecf20Sopenharmony_ci		trans_status = readl(i2c->regs + HSI2C_TRANS_STATUS);
5348c2ecf20Sopenharmony_ci		if (!(trans_status & HSI2C_MASTER_BUSY))
5358c2ecf20Sopenharmony_ci			return 0;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		usleep_range(50, 200);
5388c2ecf20Sopenharmony_ci	} while (time_before(jiffies, stop_time));
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	return -EBUSY;
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic void exynos5_i2c_bus_recover(struct exynos5_i2c *i2c)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	u32 val;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	val = readl(i2c->regs + HSI2C_CTL) | HSI2C_RXCHON;
5488c2ecf20Sopenharmony_ci	writel(val, i2c->regs + HSI2C_CTL);
5498c2ecf20Sopenharmony_ci	val = readl(i2c->regs + HSI2C_CONF) & ~HSI2C_AUTO_MODE;
5508c2ecf20Sopenharmony_ci	writel(val, i2c->regs + HSI2C_CONF);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/*
5538c2ecf20Sopenharmony_ci	 * Specification says master should send nine clock pulses. It can be
5548c2ecf20Sopenharmony_ci	 * emulated by sending manual read command (nine pulses for read eight
5558c2ecf20Sopenharmony_ci	 * bits + one pulse for NACK).
5568c2ecf20Sopenharmony_ci	 */
5578c2ecf20Sopenharmony_ci	writel(HSI2C_CMD_READ_DATA, i2c->regs + HSI2C_MANUAL_CMD);
5588c2ecf20Sopenharmony_ci	exynos5_i2c_wait_bus_idle(i2c);
5598c2ecf20Sopenharmony_ci	writel(HSI2C_CMD_SEND_STOP, i2c->regs + HSI2C_MANUAL_CMD);
5608c2ecf20Sopenharmony_ci	exynos5_i2c_wait_bus_idle(i2c);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	val = readl(i2c->regs + HSI2C_CTL) & ~HSI2C_RXCHON;
5638c2ecf20Sopenharmony_ci	writel(val, i2c->regs + HSI2C_CTL);
5648c2ecf20Sopenharmony_ci	val = readl(i2c->regs + HSI2C_CONF) | HSI2C_AUTO_MODE;
5658c2ecf20Sopenharmony_ci	writel(val, i2c->regs + HSI2C_CONF);
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic void exynos5_i2c_bus_check(struct exynos5_i2c *i2c)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	unsigned long timeout;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	if (i2c->variant->hw != I2C_TYPE_EXYNOS7)
5738c2ecf20Sopenharmony_ci		return;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/*
5768c2ecf20Sopenharmony_ci	 * HSI2C_MASTER_ST_LOSE state in EXYNOS7 variant before transaction
5778c2ecf20Sopenharmony_ci	 * indicates that bus is stuck (SDA is low). In such case bus recovery
5788c2ecf20Sopenharmony_ci	 * can be performed.
5798c2ecf20Sopenharmony_ci	 */
5808c2ecf20Sopenharmony_ci	timeout = jiffies + msecs_to_jiffies(100);
5818c2ecf20Sopenharmony_ci	for (;;) {
5828c2ecf20Sopenharmony_ci		u32 st = readl(i2c->regs + HSI2C_TRANS_STATUS);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci		if ((st & HSI2C_MASTER_ST_MASK) != HSI2C_MASTER_ST_LOSE)
5858c2ecf20Sopenharmony_ci			return;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci		if (time_is_before_jiffies(timeout))
5888c2ecf20Sopenharmony_ci			return;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci		exynos5_i2c_bus_recover(i2c);
5918c2ecf20Sopenharmony_ci	}
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci/*
5958c2ecf20Sopenharmony_ci * exynos5_i2c_message_start: Configures the bus and starts the xfer
5968c2ecf20Sopenharmony_ci * i2c: struct exynos5_i2c pointer for the current bus
5978c2ecf20Sopenharmony_ci * stop: Enables stop after transfer if set. Set for last transfer of
5988c2ecf20Sopenharmony_ci *       in the list of messages.
5998c2ecf20Sopenharmony_ci *
6008c2ecf20Sopenharmony_ci * Configures the bus for read/write function
6018c2ecf20Sopenharmony_ci * Sets chip address to talk to, message length to be sent.
6028c2ecf20Sopenharmony_ci * Enables appropriate interrupts and sends start xfer command.
6038c2ecf20Sopenharmony_ci */
6048c2ecf20Sopenharmony_cistatic void exynos5_i2c_message_start(struct exynos5_i2c *i2c, int stop)
6058c2ecf20Sopenharmony_ci{
6068c2ecf20Sopenharmony_ci	u32 i2c_ctl;
6078c2ecf20Sopenharmony_ci	u32 int_en = 0;
6088c2ecf20Sopenharmony_ci	u32 i2c_auto_conf = 0;
6098c2ecf20Sopenharmony_ci	u32 i2c_addr = 0;
6108c2ecf20Sopenharmony_ci	u32 fifo_ctl;
6118c2ecf20Sopenharmony_ci	unsigned long flags;
6128c2ecf20Sopenharmony_ci	unsigned short trig_lvl;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (i2c->variant->hw == I2C_TYPE_EXYNOS7)
6158c2ecf20Sopenharmony_ci		int_en |= HSI2C_INT_I2C_TRANS;
6168c2ecf20Sopenharmony_ci	else
6178c2ecf20Sopenharmony_ci		int_en |= HSI2C_INT_I2C;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	i2c_ctl = readl(i2c->regs + HSI2C_CTL);
6208c2ecf20Sopenharmony_ci	i2c_ctl &= ~(HSI2C_TXCHON | HSI2C_RXCHON);
6218c2ecf20Sopenharmony_ci	fifo_ctl = HSI2C_RXFIFO_EN | HSI2C_TXFIFO_EN;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	if (i2c->msg->flags & I2C_M_RD) {
6248c2ecf20Sopenharmony_ci		i2c_ctl |= HSI2C_RXCHON;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci		i2c_auto_conf |= HSI2C_READ_WRITE;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
6298c2ecf20Sopenharmony_ci			(i2c->variant->fifo_depth * 3 / 4) : i2c->msg->len;
6308c2ecf20Sopenharmony_ci		fifo_ctl |= HSI2C_RXFIFO_TRIGGER_LEVEL(trig_lvl);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		int_en |= (HSI2C_INT_RX_ALMOSTFULL_EN |
6338c2ecf20Sopenharmony_ci			HSI2C_INT_TRAILING_EN);
6348c2ecf20Sopenharmony_ci	} else {
6358c2ecf20Sopenharmony_ci		i2c_ctl |= HSI2C_TXCHON;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci		trig_lvl = (i2c->msg->len > i2c->variant->fifo_depth) ?
6388c2ecf20Sopenharmony_ci			(i2c->variant->fifo_depth * 1 / 4) : i2c->msg->len;
6398c2ecf20Sopenharmony_ci		fifo_ctl |= HSI2C_TXFIFO_TRIGGER_LEVEL(trig_lvl);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci		int_en |= HSI2C_INT_TX_ALMOSTEMPTY_EN;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	i2c_addr = HSI2C_SLV_ADDR_MAS(i2c->msg->addr);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (i2c->op_clock >= I2C_MAX_FAST_MODE_PLUS_FREQ)
6478c2ecf20Sopenharmony_ci		i2c_addr |= HSI2C_MASTER_ID(MASTER_ID(i2c->adap.nr));
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	writel(i2c_addr, i2c->regs + HSI2C_ADDR);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	writel(fifo_ctl, i2c->regs + HSI2C_FIFO_CTL);
6528c2ecf20Sopenharmony_ci	writel(i2c_ctl, i2c->regs + HSI2C_CTL);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	exynos5_i2c_bus_check(i2c);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/*
6578c2ecf20Sopenharmony_ci	 * Enable interrupts before starting the transfer so that we don't
6588c2ecf20Sopenharmony_ci	 * miss any INT_I2C interrupts.
6598c2ecf20Sopenharmony_ci	 */
6608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&i2c->lock, flags);
6618c2ecf20Sopenharmony_ci	writel(int_en, i2c->regs + HSI2C_INT_ENABLE);
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	if (stop == 1)
6648c2ecf20Sopenharmony_ci		i2c_auto_conf |= HSI2C_STOP_AFTER_TRANS;
6658c2ecf20Sopenharmony_ci	i2c_auto_conf |= i2c->msg->len;
6668c2ecf20Sopenharmony_ci	i2c_auto_conf |= HSI2C_MASTER_RUN;
6678c2ecf20Sopenharmony_ci	writel(i2c_auto_conf, i2c->regs + HSI2C_AUTO_CONF);
6688c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&i2c->lock, flags);
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
6728c2ecf20Sopenharmony_ci			      struct i2c_msg *msgs, int stop)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	unsigned long timeout;
6758c2ecf20Sopenharmony_ci	int ret;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	i2c->msg = msgs;
6788c2ecf20Sopenharmony_ci	i2c->msg_ptr = 0;
6798c2ecf20Sopenharmony_ci	i2c->trans_done = 0;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	reinit_completion(&i2c->msg_complete);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	exynos5_i2c_message_start(i2c, stop);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	timeout = wait_for_completion_timeout(&i2c->msg_complete,
6868c2ecf20Sopenharmony_ci					      EXYNOS5_I2C_TIMEOUT);
6878c2ecf20Sopenharmony_ci	if (timeout == 0)
6888c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
6898c2ecf20Sopenharmony_ci	else
6908c2ecf20Sopenharmony_ci		ret = i2c->state;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	/*
6938c2ecf20Sopenharmony_ci	 * If this is the last message to be transfered (stop == 1)
6948c2ecf20Sopenharmony_ci	 * Then check if the bus can be brought back to idle.
6958c2ecf20Sopenharmony_ci	 */
6968c2ecf20Sopenharmony_ci	if (ret == 0 && stop)
6978c2ecf20Sopenharmony_ci		ret = exynos5_i2c_wait_bus_idle(i2c);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (ret < 0) {
7008c2ecf20Sopenharmony_ci		exynos5_i2c_reset(i2c);
7018c2ecf20Sopenharmony_ci		if (ret == -ETIMEDOUT)
7028c2ecf20Sopenharmony_ci			dev_warn(i2c->dev, "%s timeout\n",
7038c2ecf20Sopenharmony_ci				 (msgs->flags & I2C_M_RD) ? "rx" : "tx");
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	/* Return the state as in interrupt routine */
7078c2ecf20Sopenharmony_ci	return ret;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic int exynos5_i2c_xfer(struct i2c_adapter *adap,
7118c2ecf20Sopenharmony_ci			struct i2c_msg *msgs, int num)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c = adap->algo_data;
7148c2ecf20Sopenharmony_ci	int i, ret;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	ret = clk_enable(i2c->clk);
7178c2ecf20Sopenharmony_ci	if (ret)
7188c2ecf20Sopenharmony_ci		return ret;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	for (i = 0; i < num; ++i) {
7218c2ecf20Sopenharmony_ci		ret = exynos5_i2c_xfer_msg(i2c, msgs + i, i + 1 == num);
7228c2ecf20Sopenharmony_ci		if (ret)
7238c2ecf20Sopenharmony_ci			break;
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	clk_disable(i2c->clk);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	return ret ?: num;
7298c2ecf20Sopenharmony_ci}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_cistatic u32 exynos5_i2c_func(struct i2c_adapter *adap)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic const struct i2c_algorithm exynos5_i2c_algorithm = {
7378c2ecf20Sopenharmony_ci	.master_xfer		= exynos5_i2c_xfer,
7388c2ecf20Sopenharmony_ci	.functionality		= exynos5_i2c_func,
7398c2ecf20Sopenharmony_ci};
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_cistatic int exynos5_i2c_probe(struct platform_device *pdev)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct device_node *np = pdev->dev.of_node;
7448c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c;
7458c2ecf20Sopenharmony_ci	int ret;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	i2c = devm_kzalloc(&pdev->dev, sizeof(struct exynos5_i2c), GFP_KERNEL);
7488c2ecf20Sopenharmony_ci	if (!i2c)
7498c2ecf20Sopenharmony_ci		return -ENOMEM;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "clock-frequency", &i2c->op_clock))
7528c2ecf20Sopenharmony_ci		i2c->op_clock = I2C_MAX_STANDARD_MODE_FREQ;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	strlcpy(i2c->adap.name, "exynos5-i2c", sizeof(i2c->adap.name));
7558c2ecf20Sopenharmony_ci	i2c->adap.owner   = THIS_MODULE;
7568c2ecf20Sopenharmony_ci	i2c->adap.algo    = &exynos5_i2c_algorithm;
7578c2ecf20Sopenharmony_ci	i2c->adap.retries = 3;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	i2c->dev = &pdev->dev;
7608c2ecf20Sopenharmony_ci	i2c->clk = devm_clk_get(&pdev->dev, "hsi2c");
7618c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->clk)) {
7628c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot get clock\n");
7638c2ecf20Sopenharmony_ci		return -ENOENT;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2c->clk);
7678c2ecf20Sopenharmony_ci	if (ret)
7688c2ecf20Sopenharmony_ci		return ret;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	i2c->regs = devm_platform_ioremap_resource(pdev, 0);
7718c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->regs)) {
7728c2ecf20Sopenharmony_ci		ret = PTR_ERR(i2c->regs);
7738c2ecf20Sopenharmony_ci		goto err_clk;
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	i2c->adap.dev.of_node = np;
7778c2ecf20Sopenharmony_ci	i2c->adap.algo_data = i2c;
7788c2ecf20Sopenharmony_ci	i2c->adap.dev.parent = &pdev->dev;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	/* Clear pending interrupts from u-boot or misc causes */
7818c2ecf20Sopenharmony_ci	exynos5_i2c_clr_pend_irq(i2c);
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	spin_lock_init(&i2c->lock);
7848c2ecf20Sopenharmony_ci	init_completion(&i2c->msg_complete);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	i2c->irq = ret = platform_get_irq(pdev, 0);
7878c2ecf20Sopenharmony_ci	if (ret <= 0) {
7888c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot find HS-I2C IRQ\n");
7898c2ecf20Sopenharmony_ci		ret = -EINVAL;
7908c2ecf20Sopenharmony_ci		goto err_clk;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, i2c->irq, exynos5_i2c_irq,
7948c2ecf20Sopenharmony_ci			       IRQF_NO_SUSPEND, dev_name(&pdev->dev), i2c);
7958c2ecf20Sopenharmony_ci	if (ret != 0) {
7968c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot request HS-I2C IRQ %d\n", i2c->irq);
7978c2ecf20Sopenharmony_ci		goto err_clk;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	i2c->variant = of_device_get_match_data(&pdev->dev);
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	ret = exynos5_hsi2c_clock_setup(i2c);
8038c2ecf20Sopenharmony_ci	if (ret)
8048c2ecf20Sopenharmony_ci		goto err_clk;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	exynos5_i2c_reset(i2c);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	ret = i2c_add_adapter(&i2c->adap);
8098c2ecf20Sopenharmony_ci	if (ret < 0)
8108c2ecf20Sopenharmony_ci		goto err_clk;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, i2c);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	clk_disable(i2c->clk);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	return 0;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci err_clk:
8198c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk);
8208c2ecf20Sopenharmony_ci	return ret;
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic int exynos5_i2c_remove(struct platform_device *pdev)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c = platform_get_drvdata(pdev);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	i2c_del_adapter(&i2c->adap);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	clk_unprepare(i2c->clk);
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	return 0;
8328c2ecf20Sopenharmony_ci}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
8358c2ecf20Sopenharmony_cistatic int exynos5_i2c_suspend_noirq(struct device *dev)
8368c2ecf20Sopenharmony_ci{
8378c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c = dev_get_drvdata(dev);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	i2c_mark_adapter_suspended(&i2c->adap);
8408c2ecf20Sopenharmony_ci	clk_unprepare(i2c->clk);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	return 0;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cistatic int exynos5_i2c_resume_noirq(struct device *dev)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct exynos5_i2c *i2c = dev_get_drvdata(dev);
8488c2ecf20Sopenharmony_ci	int ret = 0;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2c->clk);
8518c2ecf20Sopenharmony_ci	if (ret)
8528c2ecf20Sopenharmony_ci		return ret;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	ret = exynos5_hsi2c_clock_setup(i2c);
8558c2ecf20Sopenharmony_ci	if (ret) {
8568c2ecf20Sopenharmony_ci		clk_disable_unprepare(i2c->clk);
8578c2ecf20Sopenharmony_ci		return ret;
8588c2ecf20Sopenharmony_ci	}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	exynos5_i2c_init(i2c);
8618c2ecf20Sopenharmony_ci	clk_disable(i2c->clk);
8628c2ecf20Sopenharmony_ci	i2c_mark_adapter_resumed(&i2c->adap);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	return 0;
8658c2ecf20Sopenharmony_ci}
8668c2ecf20Sopenharmony_ci#endif
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic const struct dev_pm_ops exynos5_i2c_dev_pm_ops = {
8698c2ecf20Sopenharmony_ci	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos5_i2c_suspend_noirq,
8708c2ecf20Sopenharmony_ci				      exynos5_i2c_resume_noirq)
8718c2ecf20Sopenharmony_ci};
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic struct platform_driver exynos5_i2c_driver = {
8748c2ecf20Sopenharmony_ci	.probe		= exynos5_i2c_probe,
8758c2ecf20Sopenharmony_ci	.remove		= exynos5_i2c_remove,
8768c2ecf20Sopenharmony_ci	.driver		= {
8778c2ecf20Sopenharmony_ci		.name	= "exynos5-hsi2c",
8788c2ecf20Sopenharmony_ci		.pm	= &exynos5_i2c_dev_pm_ops,
8798c2ecf20Sopenharmony_ci		.of_match_table = exynos5_i2c_match,
8808c2ecf20Sopenharmony_ci	},
8818c2ecf20Sopenharmony_ci};
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cimodule_platform_driver(exynos5_i2c_driver);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
8868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Naveen Krishna Chatradhi <ch.naveen@samsung.com>");
8878c2ecf20Sopenharmony_ciMODULE_AUTHOR("Taekgyun Ko <taeggyun.ko@samsung.com>");
8888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
889