18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2014 MediaTek Inc.
48c2ecf20Sopenharmony_ci * Author: Xudong Chen <xudong.chen@mediatek.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/completion.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/device.h>
118c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/errno.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/mm.h>
208c2ecf20Sopenharmony_ci#include <linux/module.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/platform_device.h>
258c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
268c2ecf20Sopenharmony_ci#include <linux/sched.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define I2C_RS_TRANSFER			(1 << 4)
308c2ecf20Sopenharmony_ci#define I2C_ARB_LOST			(1 << 3)
318c2ecf20Sopenharmony_ci#define I2C_HS_NACKERR			(1 << 2)
328c2ecf20Sopenharmony_ci#define I2C_ACKERR			(1 << 1)
338c2ecf20Sopenharmony_ci#define I2C_TRANSAC_COMP		(1 << 0)
348c2ecf20Sopenharmony_ci#define I2C_TRANSAC_START		(1 << 0)
358c2ecf20Sopenharmony_ci#define I2C_RS_MUL_CNFG			(1 << 15)
368c2ecf20Sopenharmony_ci#define I2C_RS_MUL_TRIG			(1 << 14)
378c2ecf20Sopenharmony_ci#define I2C_DCM_DISABLE			0x0000
388c2ecf20Sopenharmony_ci#define I2C_IO_CONFIG_OPEN_DRAIN	0x0003
398c2ecf20Sopenharmony_ci#define I2C_IO_CONFIG_PUSH_PULL		0x0000
408c2ecf20Sopenharmony_ci#define I2C_SOFT_RST			0x0001
418c2ecf20Sopenharmony_ci#define I2C_HANDSHAKE_RST		0x0020
428c2ecf20Sopenharmony_ci#define I2C_FIFO_ADDR_CLR		0x0001
438c2ecf20Sopenharmony_ci#define I2C_DELAY_LEN			0x0002
448c2ecf20Sopenharmony_ci#define I2C_ST_START_CON		0x8001
458c2ecf20Sopenharmony_ci#define I2C_FS_START_CON		0x1800
468c2ecf20Sopenharmony_ci#define I2C_TIME_CLR_VALUE		0x0000
478c2ecf20Sopenharmony_ci#define I2C_TIME_DEFAULT_VALUE		0x0003
488c2ecf20Sopenharmony_ci#define I2C_WRRD_TRANAC_VALUE		0x0002
498c2ecf20Sopenharmony_ci#define I2C_RD_TRANAC_VALUE		0x0001
508c2ecf20Sopenharmony_ci#define I2C_SCL_MIS_COMP_VALUE		0x0000
518c2ecf20Sopenharmony_ci#define I2C_CHN_CLR_FLAG		0x0000
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define I2C_DMA_CON_TX			0x0000
548c2ecf20Sopenharmony_ci#define I2C_DMA_CON_RX			0x0001
558c2ecf20Sopenharmony_ci#define I2C_DMA_ASYNC_MODE		0x0004
568c2ecf20Sopenharmony_ci#define I2C_DMA_SKIP_CONFIG		0x0010
578c2ecf20Sopenharmony_ci#define I2C_DMA_DIR_CHANGE		0x0200
588c2ecf20Sopenharmony_ci#define I2C_DMA_START_EN		0x0001
598c2ecf20Sopenharmony_ci#define I2C_DMA_INT_FLAG_NONE		0x0000
608c2ecf20Sopenharmony_ci#define I2C_DMA_CLR_FLAG		0x0000
618c2ecf20Sopenharmony_ci#define I2C_DMA_WARM_RST		0x0001
628c2ecf20Sopenharmony_ci#define I2C_DMA_HARD_RST		0x0002
638c2ecf20Sopenharmony_ci#define I2C_DMA_HANDSHAKE_RST		0x0004
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define MAX_SAMPLE_CNT_DIV		8
668c2ecf20Sopenharmony_ci#define MAX_STEP_CNT_DIV		64
678c2ecf20Sopenharmony_ci#define MAX_CLOCK_DIV			256
688c2ecf20Sopenharmony_ci#define MAX_HS_STEP_CNT_DIV		8
698c2ecf20Sopenharmony_ci#define I2C_STANDARD_MODE_BUFFER	(1000 / 2)
708c2ecf20Sopenharmony_ci#define I2C_FAST_MODE_BUFFER		(300 / 2)
718c2ecf20Sopenharmony_ci#define I2C_FAST_MODE_PLUS_BUFFER	(20 / 2)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define I2C_CONTROL_RS                  (0x1 << 1)
748c2ecf20Sopenharmony_ci#define I2C_CONTROL_DMA_EN              (0x1 << 2)
758c2ecf20Sopenharmony_ci#define I2C_CONTROL_CLK_EXT_EN          (0x1 << 3)
768c2ecf20Sopenharmony_ci#define I2C_CONTROL_DIR_CHANGE          (0x1 << 4)
778c2ecf20Sopenharmony_ci#define I2C_CONTROL_ACKERR_DET_EN       (0x1 << 5)
788c2ecf20Sopenharmony_ci#define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
798c2ecf20Sopenharmony_ci#define I2C_CONTROL_DMAACK_EN           (0x1 << 8)
808c2ecf20Sopenharmony_ci#define I2C_CONTROL_ASYNC_MODE          (0x1 << 9)
818c2ecf20Sopenharmony_ci#define I2C_CONTROL_WRAPPER             (0x1 << 0)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define I2C_DRV_NAME		"i2c-mt65xx"
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cienum DMA_REGS_OFFSET {
868c2ecf20Sopenharmony_ci	OFFSET_INT_FLAG = 0x0,
878c2ecf20Sopenharmony_ci	OFFSET_INT_EN = 0x04,
888c2ecf20Sopenharmony_ci	OFFSET_EN = 0x08,
898c2ecf20Sopenharmony_ci	OFFSET_RST = 0x0c,
908c2ecf20Sopenharmony_ci	OFFSET_CON = 0x18,
918c2ecf20Sopenharmony_ci	OFFSET_TX_MEM_ADDR = 0x1c,
928c2ecf20Sopenharmony_ci	OFFSET_RX_MEM_ADDR = 0x20,
938c2ecf20Sopenharmony_ci	OFFSET_TX_LEN = 0x24,
948c2ecf20Sopenharmony_ci	OFFSET_RX_LEN = 0x28,
958c2ecf20Sopenharmony_ci	OFFSET_TX_4G_MODE = 0x54,
968c2ecf20Sopenharmony_ci	OFFSET_RX_4G_MODE = 0x58,
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cienum i2c_trans_st_rs {
1008c2ecf20Sopenharmony_ci	I2C_TRANS_STOP = 0,
1018c2ecf20Sopenharmony_ci	I2C_TRANS_REPEATED_START,
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cienum mtk_trans_op {
1058c2ecf20Sopenharmony_ci	I2C_MASTER_WR = 1,
1068c2ecf20Sopenharmony_ci	I2C_MASTER_RD,
1078c2ecf20Sopenharmony_ci	I2C_MASTER_WRRD,
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cienum I2C_REGS_OFFSET {
1118c2ecf20Sopenharmony_ci	OFFSET_DATA_PORT,
1128c2ecf20Sopenharmony_ci	OFFSET_SLAVE_ADDR,
1138c2ecf20Sopenharmony_ci	OFFSET_INTR_MASK,
1148c2ecf20Sopenharmony_ci	OFFSET_INTR_STAT,
1158c2ecf20Sopenharmony_ci	OFFSET_CONTROL,
1168c2ecf20Sopenharmony_ci	OFFSET_TRANSFER_LEN,
1178c2ecf20Sopenharmony_ci	OFFSET_TRANSAC_LEN,
1188c2ecf20Sopenharmony_ci	OFFSET_DELAY_LEN,
1198c2ecf20Sopenharmony_ci	OFFSET_TIMING,
1208c2ecf20Sopenharmony_ci	OFFSET_START,
1218c2ecf20Sopenharmony_ci	OFFSET_EXT_CONF,
1228c2ecf20Sopenharmony_ci	OFFSET_FIFO_STAT,
1238c2ecf20Sopenharmony_ci	OFFSET_FIFO_THRESH,
1248c2ecf20Sopenharmony_ci	OFFSET_FIFO_ADDR_CLR,
1258c2ecf20Sopenharmony_ci	OFFSET_IO_CONFIG,
1268c2ecf20Sopenharmony_ci	OFFSET_RSV_DEBUG,
1278c2ecf20Sopenharmony_ci	OFFSET_HS,
1288c2ecf20Sopenharmony_ci	OFFSET_SOFTRESET,
1298c2ecf20Sopenharmony_ci	OFFSET_DCM_EN,
1308c2ecf20Sopenharmony_ci	OFFSET_PATH_DIR,
1318c2ecf20Sopenharmony_ci	OFFSET_DEBUGSTAT,
1328c2ecf20Sopenharmony_ci	OFFSET_DEBUGCTRL,
1338c2ecf20Sopenharmony_ci	OFFSET_TRANSFER_LEN_AUX,
1348c2ecf20Sopenharmony_ci	OFFSET_CLOCK_DIV,
1358c2ecf20Sopenharmony_ci	OFFSET_LTIMING,
1368c2ecf20Sopenharmony_ci	OFFSET_SCL_HIGH_LOW_RATIO,
1378c2ecf20Sopenharmony_ci	OFFSET_HS_SCL_HIGH_LOW_RATIO,
1388c2ecf20Sopenharmony_ci	OFFSET_SCL_MIS_COMP_POINT,
1398c2ecf20Sopenharmony_ci	OFFSET_STA_STO_AC_TIMING,
1408c2ecf20Sopenharmony_ci	OFFSET_HS_STA_STO_AC_TIMING,
1418c2ecf20Sopenharmony_ci	OFFSET_SDA_TIMING,
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic const u16 mt_i2c_regs_v1[] = {
1458c2ecf20Sopenharmony_ci	[OFFSET_DATA_PORT] = 0x0,
1468c2ecf20Sopenharmony_ci	[OFFSET_SLAVE_ADDR] = 0x4,
1478c2ecf20Sopenharmony_ci	[OFFSET_INTR_MASK] = 0x8,
1488c2ecf20Sopenharmony_ci	[OFFSET_INTR_STAT] = 0xc,
1498c2ecf20Sopenharmony_ci	[OFFSET_CONTROL] = 0x10,
1508c2ecf20Sopenharmony_ci	[OFFSET_TRANSFER_LEN] = 0x14,
1518c2ecf20Sopenharmony_ci	[OFFSET_TRANSAC_LEN] = 0x18,
1528c2ecf20Sopenharmony_ci	[OFFSET_DELAY_LEN] = 0x1c,
1538c2ecf20Sopenharmony_ci	[OFFSET_TIMING] = 0x20,
1548c2ecf20Sopenharmony_ci	[OFFSET_START] = 0x24,
1558c2ecf20Sopenharmony_ci	[OFFSET_EXT_CONF] = 0x28,
1568c2ecf20Sopenharmony_ci	[OFFSET_FIFO_STAT] = 0x30,
1578c2ecf20Sopenharmony_ci	[OFFSET_FIFO_THRESH] = 0x34,
1588c2ecf20Sopenharmony_ci	[OFFSET_FIFO_ADDR_CLR] = 0x38,
1598c2ecf20Sopenharmony_ci	[OFFSET_IO_CONFIG] = 0x40,
1608c2ecf20Sopenharmony_ci	[OFFSET_RSV_DEBUG] = 0x44,
1618c2ecf20Sopenharmony_ci	[OFFSET_HS] = 0x48,
1628c2ecf20Sopenharmony_ci	[OFFSET_SOFTRESET] = 0x50,
1638c2ecf20Sopenharmony_ci	[OFFSET_DCM_EN] = 0x54,
1648c2ecf20Sopenharmony_ci	[OFFSET_PATH_DIR] = 0x60,
1658c2ecf20Sopenharmony_ci	[OFFSET_DEBUGSTAT] = 0x64,
1668c2ecf20Sopenharmony_ci	[OFFSET_DEBUGCTRL] = 0x68,
1678c2ecf20Sopenharmony_ci	[OFFSET_TRANSFER_LEN_AUX] = 0x6c,
1688c2ecf20Sopenharmony_ci	[OFFSET_CLOCK_DIV] = 0x70,
1698c2ecf20Sopenharmony_ci	[OFFSET_SCL_HIGH_LOW_RATIO] = 0x74,
1708c2ecf20Sopenharmony_ci	[OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78,
1718c2ecf20Sopenharmony_ci	[OFFSET_SCL_MIS_COMP_POINT] = 0x7C,
1728c2ecf20Sopenharmony_ci	[OFFSET_STA_STO_AC_TIMING] = 0x80,
1738c2ecf20Sopenharmony_ci	[OFFSET_HS_STA_STO_AC_TIMING] = 0x84,
1748c2ecf20Sopenharmony_ci	[OFFSET_SDA_TIMING] = 0x88,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic const u16 mt_i2c_regs_v2[] = {
1788c2ecf20Sopenharmony_ci	[OFFSET_DATA_PORT] = 0x0,
1798c2ecf20Sopenharmony_ci	[OFFSET_SLAVE_ADDR] = 0x4,
1808c2ecf20Sopenharmony_ci	[OFFSET_INTR_MASK] = 0x8,
1818c2ecf20Sopenharmony_ci	[OFFSET_INTR_STAT] = 0xc,
1828c2ecf20Sopenharmony_ci	[OFFSET_CONTROL] = 0x10,
1838c2ecf20Sopenharmony_ci	[OFFSET_TRANSFER_LEN] = 0x14,
1848c2ecf20Sopenharmony_ci	[OFFSET_TRANSAC_LEN] = 0x18,
1858c2ecf20Sopenharmony_ci	[OFFSET_DELAY_LEN] = 0x1c,
1868c2ecf20Sopenharmony_ci	[OFFSET_TIMING] = 0x20,
1878c2ecf20Sopenharmony_ci	[OFFSET_START] = 0x24,
1888c2ecf20Sopenharmony_ci	[OFFSET_EXT_CONF] = 0x28,
1898c2ecf20Sopenharmony_ci	[OFFSET_LTIMING] = 0x2c,
1908c2ecf20Sopenharmony_ci	[OFFSET_HS] = 0x30,
1918c2ecf20Sopenharmony_ci	[OFFSET_IO_CONFIG] = 0x34,
1928c2ecf20Sopenharmony_ci	[OFFSET_FIFO_ADDR_CLR] = 0x38,
1938c2ecf20Sopenharmony_ci	[OFFSET_SDA_TIMING] = 0x3c,
1948c2ecf20Sopenharmony_ci	[OFFSET_TRANSFER_LEN_AUX] = 0x44,
1958c2ecf20Sopenharmony_ci	[OFFSET_CLOCK_DIV] = 0x48,
1968c2ecf20Sopenharmony_ci	[OFFSET_SOFTRESET] = 0x50,
1978c2ecf20Sopenharmony_ci	[OFFSET_SCL_MIS_COMP_POINT] = 0x90,
1988c2ecf20Sopenharmony_ci	[OFFSET_DEBUGSTAT] = 0xe4,
1998c2ecf20Sopenharmony_ci	[OFFSET_DEBUGCTRL] = 0xe8,
2008c2ecf20Sopenharmony_ci	[OFFSET_FIFO_STAT] = 0xf4,
2018c2ecf20Sopenharmony_ci	[OFFSET_FIFO_THRESH] = 0xf8,
2028c2ecf20Sopenharmony_ci	[OFFSET_DCM_EN] = 0xf88,
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistruct mtk_i2c_compatible {
2068c2ecf20Sopenharmony_ci	const struct i2c_adapter_quirks *quirks;
2078c2ecf20Sopenharmony_ci	const u16 *regs;
2088c2ecf20Sopenharmony_ci	unsigned char pmic_i2c: 1;
2098c2ecf20Sopenharmony_ci	unsigned char dcm: 1;
2108c2ecf20Sopenharmony_ci	unsigned char auto_restart: 1;
2118c2ecf20Sopenharmony_ci	unsigned char aux_len_reg: 1;
2128c2ecf20Sopenharmony_ci	unsigned char timing_adjust: 1;
2138c2ecf20Sopenharmony_ci	unsigned char dma_sync: 1;
2148c2ecf20Sopenharmony_ci	unsigned char ltiming_adjust: 1;
2158c2ecf20Sopenharmony_ci	unsigned char apdma_sync: 1;
2168c2ecf20Sopenharmony_ci	unsigned char max_dma_support;
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistruct mtk_i2c_ac_timing {
2208c2ecf20Sopenharmony_ci	u16 htiming;
2218c2ecf20Sopenharmony_ci	u16 ltiming;
2228c2ecf20Sopenharmony_ci	u16 hs;
2238c2ecf20Sopenharmony_ci	u16 ext;
2248c2ecf20Sopenharmony_ci	u16 inter_clk_div;
2258c2ecf20Sopenharmony_ci	u16 scl_hl_ratio;
2268c2ecf20Sopenharmony_ci	u16 hs_scl_hl_ratio;
2278c2ecf20Sopenharmony_ci	u16 sta_stop;
2288c2ecf20Sopenharmony_ci	u16 hs_sta_stop;
2298c2ecf20Sopenharmony_ci	u16 sda_timing;
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistruct mtk_i2c {
2338c2ecf20Sopenharmony_ci	struct i2c_adapter adap;	/* i2c host adapter */
2348c2ecf20Sopenharmony_ci	struct device *dev;
2358c2ecf20Sopenharmony_ci	struct completion msg_complete;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/* set in i2c probe */
2388c2ecf20Sopenharmony_ci	void __iomem *base;		/* i2c base addr */
2398c2ecf20Sopenharmony_ci	void __iomem *pdmabase;		/* dma base address*/
2408c2ecf20Sopenharmony_ci	struct clk *clk_main;		/* main clock for i2c bus */
2418c2ecf20Sopenharmony_ci	struct clk *clk_dma;		/* DMA clock for i2c via DMA */
2428c2ecf20Sopenharmony_ci	struct clk *clk_pmic;		/* PMIC clock for i2c from PMIC */
2438c2ecf20Sopenharmony_ci	struct clk *clk_arb;		/* Arbitrator clock for i2c */
2448c2ecf20Sopenharmony_ci	bool have_pmic;			/* can use i2c pins from PMIC */
2458c2ecf20Sopenharmony_ci	bool use_push_pull;		/* IO config push-pull mode */
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	u16 irq_stat;			/* interrupt status */
2488c2ecf20Sopenharmony_ci	unsigned int clk_src_div;
2498c2ecf20Sopenharmony_ci	unsigned int speed_hz;		/* The speed in transfer */
2508c2ecf20Sopenharmony_ci	enum mtk_trans_op op;
2518c2ecf20Sopenharmony_ci	u16 timing_reg;
2528c2ecf20Sopenharmony_ci	u16 high_speed_reg;
2538c2ecf20Sopenharmony_ci	u16 ltiming_reg;
2548c2ecf20Sopenharmony_ci	unsigned char auto_restart;
2558c2ecf20Sopenharmony_ci	bool ignore_restart_irq;
2568c2ecf20Sopenharmony_ci	struct mtk_i2c_ac_timing ac_timing;
2578c2ecf20Sopenharmony_ci	const struct mtk_i2c_compatible *dev_comp;
2588c2ecf20Sopenharmony_ci};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/**
2618c2ecf20Sopenharmony_ci * struct i2c_spec_values:
2628c2ecf20Sopenharmony_ci * @min_low_ns: min LOW period of the SCL clock
2638c2ecf20Sopenharmony_ci * @min_su_sta_ns: min set-up time for a repeated START condition
2648c2ecf20Sopenharmony_ci * @max_hd_dat_ns: max data hold time
2658c2ecf20Sopenharmony_ci * @min_su_dat_ns: min data set-up time
2668c2ecf20Sopenharmony_ci */
2678c2ecf20Sopenharmony_cistruct i2c_spec_values {
2688c2ecf20Sopenharmony_ci	unsigned int min_low_ns;
2698c2ecf20Sopenharmony_ci	unsigned int min_su_sta_ns;
2708c2ecf20Sopenharmony_ci	unsigned int max_hd_dat_ns;
2718c2ecf20Sopenharmony_ci	unsigned int min_su_dat_ns;
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic const struct i2c_spec_values standard_mode_spec = {
2758c2ecf20Sopenharmony_ci	.min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
2768c2ecf20Sopenharmony_ci	.min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
2778c2ecf20Sopenharmony_ci	.max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER,
2788c2ecf20Sopenharmony_ci	.min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER,
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic const struct i2c_spec_values fast_mode_spec = {
2828c2ecf20Sopenharmony_ci	.min_low_ns = 1300 + I2C_FAST_MODE_BUFFER,
2838c2ecf20Sopenharmony_ci	.min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER,
2848c2ecf20Sopenharmony_ci	.max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER,
2858c2ecf20Sopenharmony_ci	.min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER,
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic const struct i2c_spec_values fast_mode_plus_spec = {
2898c2ecf20Sopenharmony_ci	.min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER,
2908c2ecf20Sopenharmony_ci	.min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER,
2918c2ecf20Sopenharmony_ci	.max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER,
2928c2ecf20Sopenharmony_ci	.min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER,
2938c2ecf20Sopenharmony_ci};
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks mt6577_i2c_quirks = {
2968c2ecf20Sopenharmony_ci	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
2978c2ecf20Sopenharmony_ci	.max_num_msgs = 1,
2988c2ecf20Sopenharmony_ci	.max_write_len = 255,
2998c2ecf20Sopenharmony_ci	.max_read_len = 255,
3008c2ecf20Sopenharmony_ci	.max_comb_1st_msg_len = 255,
3018c2ecf20Sopenharmony_ci	.max_comb_2nd_msg_len = 31,
3028c2ecf20Sopenharmony_ci};
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks mt7622_i2c_quirks = {
3058c2ecf20Sopenharmony_ci	.max_num_msgs = 255,
3068c2ecf20Sopenharmony_ci};
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic const struct i2c_adapter_quirks mt8183_i2c_quirks = {
3098c2ecf20Sopenharmony_ci	.flags = I2C_AQ_NO_ZERO_LEN,
3108c2ecf20Sopenharmony_ci};
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt2712_compat = {
3138c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v1,
3148c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3158c2ecf20Sopenharmony_ci	.dcm = 1,
3168c2ecf20Sopenharmony_ci	.auto_restart = 1,
3178c2ecf20Sopenharmony_ci	.aux_len_reg = 1,
3188c2ecf20Sopenharmony_ci	.timing_adjust = 1,
3198c2ecf20Sopenharmony_ci	.dma_sync = 0,
3208c2ecf20Sopenharmony_ci	.ltiming_adjust = 0,
3218c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3228c2ecf20Sopenharmony_ci	.max_dma_support = 33,
3238c2ecf20Sopenharmony_ci};
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt6577_compat = {
3268c2ecf20Sopenharmony_ci	.quirks = &mt6577_i2c_quirks,
3278c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v1,
3288c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3298c2ecf20Sopenharmony_ci	.dcm = 1,
3308c2ecf20Sopenharmony_ci	.auto_restart = 0,
3318c2ecf20Sopenharmony_ci	.aux_len_reg = 0,
3328c2ecf20Sopenharmony_ci	.timing_adjust = 0,
3338c2ecf20Sopenharmony_ci	.dma_sync = 0,
3348c2ecf20Sopenharmony_ci	.ltiming_adjust = 0,
3358c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3368c2ecf20Sopenharmony_ci	.max_dma_support = 32,
3378c2ecf20Sopenharmony_ci};
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt6589_compat = {
3408c2ecf20Sopenharmony_ci	.quirks = &mt6577_i2c_quirks,
3418c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v1,
3428c2ecf20Sopenharmony_ci	.pmic_i2c = 1,
3438c2ecf20Sopenharmony_ci	.dcm = 0,
3448c2ecf20Sopenharmony_ci	.auto_restart = 0,
3458c2ecf20Sopenharmony_ci	.aux_len_reg = 0,
3468c2ecf20Sopenharmony_ci	.timing_adjust = 0,
3478c2ecf20Sopenharmony_ci	.dma_sync = 0,
3488c2ecf20Sopenharmony_ci	.ltiming_adjust = 0,
3498c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3508c2ecf20Sopenharmony_ci	.max_dma_support = 32,
3518c2ecf20Sopenharmony_ci};
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt7622_compat = {
3548c2ecf20Sopenharmony_ci	.quirks = &mt7622_i2c_quirks,
3558c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v1,
3568c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3578c2ecf20Sopenharmony_ci	.dcm = 1,
3588c2ecf20Sopenharmony_ci	.auto_restart = 1,
3598c2ecf20Sopenharmony_ci	.aux_len_reg = 1,
3608c2ecf20Sopenharmony_ci	.timing_adjust = 0,
3618c2ecf20Sopenharmony_ci	.dma_sync = 0,
3628c2ecf20Sopenharmony_ci	.ltiming_adjust = 0,
3638c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3648c2ecf20Sopenharmony_ci	.max_dma_support = 32,
3658c2ecf20Sopenharmony_ci};
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt8173_compat = {
3688c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v1,
3698c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3708c2ecf20Sopenharmony_ci	.dcm = 1,
3718c2ecf20Sopenharmony_ci	.auto_restart = 1,
3728c2ecf20Sopenharmony_ci	.aux_len_reg = 1,
3738c2ecf20Sopenharmony_ci	.timing_adjust = 0,
3748c2ecf20Sopenharmony_ci	.dma_sync = 0,
3758c2ecf20Sopenharmony_ci	.ltiming_adjust = 0,
3768c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3778c2ecf20Sopenharmony_ci	.max_dma_support = 33,
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt8183_compat = {
3818c2ecf20Sopenharmony_ci	.quirks = &mt8183_i2c_quirks,
3828c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v2,
3838c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3848c2ecf20Sopenharmony_ci	.dcm = 0,
3858c2ecf20Sopenharmony_ci	.auto_restart = 1,
3868c2ecf20Sopenharmony_ci	.aux_len_reg = 1,
3878c2ecf20Sopenharmony_ci	.timing_adjust = 1,
3888c2ecf20Sopenharmony_ci	.dma_sync = 1,
3898c2ecf20Sopenharmony_ci	.ltiming_adjust = 1,
3908c2ecf20Sopenharmony_ci	.apdma_sync = 0,
3918c2ecf20Sopenharmony_ci	.max_dma_support = 33,
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic const struct mtk_i2c_compatible mt8192_compat = {
3958c2ecf20Sopenharmony_ci	.quirks = &mt8183_i2c_quirks,
3968c2ecf20Sopenharmony_ci	.regs = mt_i2c_regs_v2,
3978c2ecf20Sopenharmony_ci	.pmic_i2c = 0,
3988c2ecf20Sopenharmony_ci	.dcm = 0,
3998c2ecf20Sopenharmony_ci	.auto_restart = 1,
4008c2ecf20Sopenharmony_ci	.aux_len_reg = 1,
4018c2ecf20Sopenharmony_ci	.timing_adjust = 1,
4028c2ecf20Sopenharmony_ci	.dma_sync = 1,
4038c2ecf20Sopenharmony_ci	.ltiming_adjust = 1,
4048c2ecf20Sopenharmony_ci	.apdma_sync = 1,
4058c2ecf20Sopenharmony_ci	.max_dma_support = 36,
4068c2ecf20Sopenharmony_ci};
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic const struct of_device_id mtk_i2c_of_match[] = {
4098c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat },
4108c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat },
4118c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat },
4128c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat },
4138c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat },
4148c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat },
4158c2ecf20Sopenharmony_ci	{ .compatible = "mediatek,mt8192-i2c", .data = &mt8192_compat },
4168c2ecf20Sopenharmony_ci	{}
4178c2ecf20Sopenharmony_ci};
4188c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mtk_i2c_of_match);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	return readw(i2c->base + i2c->dev_comp->regs[reg]);
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val,
4268c2ecf20Sopenharmony_ci			   enum I2C_REGS_OFFSET reg)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	writew(val, i2c->base + i2c->dev_comp->regs[reg]);
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic int mtk_i2c_clock_enable(struct mtk_i2c *i2c)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	int ret;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2c->clk_dma);
4368c2ecf20Sopenharmony_ci	if (ret)
4378c2ecf20Sopenharmony_ci		return ret;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(i2c->clk_main);
4408c2ecf20Sopenharmony_ci	if (ret)
4418c2ecf20Sopenharmony_ci		goto err_main;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (i2c->have_pmic) {
4448c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(i2c->clk_pmic);
4458c2ecf20Sopenharmony_ci		if (ret)
4468c2ecf20Sopenharmony_ci			goto err_pmic;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (i2c->clk_arb) {
4508c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(i2c->clk_arb);
4518c2ecf20Sopenharmony_ci		if (ret)
4528c2ecf20Sopenharmony_ci			goto err_arb;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	return 0;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cierr_arb:
4588c2ecf20Sopenharmony_ci	if (i2c->have_pmic)
4598c2ecf20Sopenharmony_ci		clk_disable_unprepare(i2c->clk_pmic);
4608c2ecf20Sopenharmony_cierr_pmic:
4618c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk_main);
4628c2ecf20Sopenharmony_cierr_main:
4638c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk_dma);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	return ret;
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic void mtk_i2c_clock_disable(struct mtk_i2c *i2c)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	if (i2c->clk_arb)
4718c2ecf20Sopenharmony_ci		clk_disable_unprepare(i2c->clk_arb);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if (i2c->have_pmic)
4748c2ecf20Sopenharmony_ci		clk_disable_unprepare(i2c->clk_pmic);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk_main);
4778c2ecf20Sopenharmony_ci	clk_disable_unprepare(i2c->clk_dma);
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic void mtk_i2c_init_hw(struct mtk_i2c *i2c)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	u16 control_reg;
4838c2ecf20Sopenharmony_ci	u16 intr_stat_reg;
4848c2ecf20Sopenharmony_ci	u16 ext_conf_val;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_START);
4878c2ecf20Sopenharmony_ci	intr_stat_reg = mtk_i2c_readw(i2c, OFFSET_INTR_STAT);
4888c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, intr_stat_reg, OFFSET_INTR_STAT);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (i2c->dev_comp->apdma_sync) {
4918c2ecf20Sopenharmony_ci		writel(I2C_DMA_WARM_RST, i2c->pdmabase + OFFSET_RST);
4928c2ecf20Sopenharmony_ci		udelay(10);
4938c2ecf20Sopenharmony_ci		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST);
4948c2ecf20Sopenharmony_ci		udelay(10);
4958c2ecf20Sopenharmony_ci		writel(I2C_DMA_HANDSHAKE_RST | I2C_DMA_HARD_RST,
4968c2ecf20Sopenharmony_ci		       i2c->pdmabase + OFFSET_RST);
4978c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_HANDSHAKE_RST | I2C_SOFT_RST,
4988c2ecf20Sopenharmony_ci			       OFFSET_SOFTRESET);
4998c2ecf20Sopenharmony_ci		udelay(10);
5008c2ecf20Sopenharmony_ci		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST);
5018c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_CHN_CLR_FLAG, OFFSET_SOFTRESET);
5028c2ecf20Sopenharmony_ci	} else {
5038c2ecf20Sopenharmony_ci		writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST);
5048c2ecf20Sopenharmony_ci		udelay(50);
5058c2ecf20Sopenharmony_ci		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST);
5068c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET);
5078c2ecf20Sopenharmony_ci	}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	/* Set ioconfig */
5108c2ecf20Sopenharmony_ci	if (i2c->use_push_pull)
5118c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG);
5128c2ecf20Sopenharmony_ci	else
5138c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	if (i2c->dev_comp->dcm)
5168c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING);
5198c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS);
5208c2ecf20Sopenharmony_ci	if (i2c->dev_comp->ltiming_adjust)
5218c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	if (i2c->speed_hz <= I2C_MAX_STANDARD_MODE_FREQ)
5248c2ecf20Sopenharmony_ci		ext_conf_val = I2C_ST_START_CON;
5258c2ecf20Sopenharmony_ci	else
5268c2ecf20Sopenharmony_ci		ext_conf_val = I2C_FS_START_CON;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (i2c->dev_comp->timing_adjust) {
5298c2ecf20Sopenharmony_ci		ext_conf_val = i2c->ac_timing.ext;
5308c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div,
5318c2ecf20Sopenharmony_ci			       OFFSET_CLOCK_DIV);
5328c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE,
5338c2ecf20Sopenharmony_ci			       OFFSET_SCL_MIS_COMP_POINT);
5348c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing,
5358c2ecf20Sopenharmony_ci			       OFFSET_SDA_TIMING);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		if (i2c->dev_comp->ltiming_adjust) {
5388c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.htiming,
5398c2ecf20Sopenharmony_ci				       OFFSET_TIMING);
5408c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS);
5418c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.ltiming,
5428c2ecf20Sopenharmony_ci				       OFFSET_LTIMING);
5438c2ecf20Sopenharmony_ci		} else {
5448c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio,
5458c2ecf20Sopenharmony_ci				       OFFSET_SCL_HIGH_LOW_RATIO);
5468c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio,
5478c2ecf20Sopenharmony_ci				       OFFSET_HS_SCL_HIGH_LOW_RATIO);
5488c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop,
5498c2ecf20Sopenharmony_ci				       OFFSET_STA_STO_AC_TIMING);
5508c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop,
5518c2ecf20Sopenharmony_ci				       OFFSET_HS_STA_STO_AC_TIMING);
5528c2ecf20Sopenharmony_ci		}
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, ext_conf_val, OFFSET_EXT_CONF);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	/* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
5578c2ecf20Sopenharmony_ci	if (i2c->have_pmic)
5588c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	control_reg = I2C_CONTROL_ACKERR_DET_EN |
5618c2ecf20Sopenharmony_ci		      I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN;
5628c2ecf20Sopenharmony_ci	if (i2c->dev_comp->dma_sync)
5638c2ecf20Sopenharmony_ci		control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL);
5668c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN);
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	if (speed <= I2C_MAX_STANDARD_MODE_FREQ)
5728c2ecf20Sopenharmony_ci		return &standard_mode_spec;
5738c2ecf20Sopenharmony_ci	else if (speed <= I2C_MAX_FAST_MODE_FREQ)
5748c2ecf20Sopenharmony_ci		return &fast_mode_spec;
5758c2ecf20Sopenharmony_ci	else
5768c2ecf20Sopenharmony_ci		return &fast_mode_plus_spec;
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic int mtk_i2c_max_step_cnt(unsigned int target_speed)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ)
5828c2ecf20Sopenharmony_ci		return MAX_HS_STEP_CNT_DIV;
5838c2ecf20Sopenharmony_ci	else
5848c2ecf20Sopenharmony_ci		return MAX_STEP_CNT_DIV;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci/*
5888c2ecf20Sopenharmony_ci * Check and Calculate i2c ac-timing
5898c2ecf20Sopenharmony_ci *
5908c2ecf20Sopenharmony_ci * Hardware design:
5918c2ecf20Sopenharmony_ci * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src
5928c2ecf20Sopenharmony_ci * xxx_cnt_div =  spec->min_xxx_ns / sample_ns
5938c2ecf20Sopenharmony_ci *
5948c2ecf20Sopenharmony_ci * Sample_ns is rounded down for xxx_cnt_div would be greater
5958c2ecf20Sopenharmony_ci * than the smallest spec.
5968c2ecf20Sopenharmony_ci * The sda_timing is chosen as the middle value between
5978c2ecf20Sopenharmony_ci * the largest and smallest.
5988c2ecf20Sopenharmony_ci */
5998c2ecf20Sopenharmony_cistatic int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c,
6008c2ecf20Sopenharmony_ci				   unsigned int clk_src,
6018c2ecf20Sopenharmony_ci				   unsigned int check_speed,
6028c2ecf20Sopenharmony_ci				   unsigned int step_cnt,
6038c2ecf20Sopenharmony_ci				   unsigned int sample_cnt)
6048c2ecf20Sopenharmony_ci{
6058c2ecf20Sopenharmony_ci	const struct i2c_spec_values *spec;
6068c2ecf20Sopenharmony_ci	unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt;
6078c2ecf20Sopenharmony_ci	unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f;
6088c2ecf20Sopenharmony_ci	unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1),
6098c2ecf20Sopenharmony_ci					 clk_src);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci	if (!i2c->dev_comp->timing_adjust)
6128c2ecf20Sopenharmony_ci		return 0;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (i2c->dev_comp->ltiming_adjust)
6158c2ecf20Sopenharmony_ci		max_sta_cnt = 0x100;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	spec = mtk_i2c_get_spec(check_speed);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	if (i2c->dev_comp->ltiming_adjust)
6208c2ecf20Sopenharmony_ci		clk_ns = 1000000000 / clk_src;
6218c2ecf20Sopenharmony_ci	else
6228c2ecf20Sopenharmony_ci		clk_ns = sample_ns / 2;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns, clk_ns);
6258c2ecf20Sopenharmony_ci	if (su_sta_cnt > max_sta_cnt)
6268c2ecf20Sopenharmony_ci		return -1;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns);
6298c2ecf20Sopenharmony_ci	max_step_cnt = mtk_i2c_max_step_cnt(check_speed);
6308c2ecf20Sopenharmony_ci	if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) {
6318c2ecf20Sopenharmony_ci		if (low_cnt > step_cnt) {
6328c2ecf20Sopenharmony_ci			high_cnt = 2 * step_cnt - low_cnt;
6338c2ecf20Sopenharmony_ci		} else {
6348c2ecf20Sopenharmony_ci			high_cnt = step_cnt;
6358c2ecf20Sopenharmony_ci			low_cnt = step_cnt;
6368c2ecf20Sopenharmony_ci		}
6378c2ecf20Sopenharmony_ci	} else {
6388c2ecf20Sopenharmony_ci		return -2;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	sda_max = spec->max_hd_dat_ns / sample_ns;
6428c2ecf20Sopenharmony_ci	if (sda_max > low_cnt)
6438c2ecf20Sopenharmony_ci		sda_max = 0;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns);
6468c2ecf20Sopenharmony_ci	if (sda_min < low_cnt)
6478c2ecf20Sopenharmony_ci		sda_min = 0;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	if (sda_min > sda_max)
6508c2ecf20Sopenharmony_ci		return -3;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (check_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) {
6538c2ecf20Sopenharmony_ci		if (i2c->dev_comp->ltiming_adjust) {
6548c2ecf20Sopenharmony_ci			i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE |
6558c2ecf20Sopenharmony_ci				(sample_cnt << 12) | (high_cnt << 8);
6568c2ecf20Sopenharmony_ci			i2c->ac_timing.ltiming &= ~GENMASK(15, 9);
6578c2ecf20Sopenharmony_ci			i2c->ac_timing.ltiming |= (sample_cnt << 12) |
6588c2ecf20Sopenharmony_ci				(low_cnt << 9);
6598c2ecf20Sopenharmony_ci			i2c->ac_timing.ext &= ~GENMASK(7, 1);
6608c2ecf20Sopenharmony_ci			i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0);
6618c2ecf20Sopenharmony_ci		} else {
6628c2ecf20Sopenharmony_ci			i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) |
6638c2ecf20Sopenharmony_ci				(high_cnt << 6) | low_cnt;
6648c2ecf20Sopenharmony_ci			i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) |
6658c2ecf20Sopenharmony_ci				su_sta_cnt;
6668c2ecf20Sopenharmony_ci		}
6678c2ecf20Sopenharmony_ci		i2c->ac_timing.sda_timing &= ~GENMASK(11, 6);
6688c2ecf20Sopenharmony_ci		i2c->ac_timing.sda_timing |= (1 << 12) |
6698c2ecf20Sopenharmony_ci			((sda_max + sda_min) / 2) << 6;
6708c2ecf20Sopenharmony_ci	} else {
6718c2ecf20Sopenharmony_ci		if (i2c->dev_comp->ltiming_adjust) {
6728c2ecf20Sopenharmony_ci			i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt);
6738c2ecf20Sopenharmony_ci			i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt);
6748c2ecf20Sopenharmony_ci			i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0);
6758c2ecf20Sopenharmony_ci		} else {
6768c2ecf20Sopenharmony_ci			i2c->ac_timing.scl_hl_ratio = (1 << 12) |
6778c2ecf20Sopenharmony_ci				(high_cnt << 6) | low_cnt;
6788c2ecf20Sopenharmony_ci			i2c->ac_timing.sta_stop = (su_sta_cnt << 8) |
6798c2ecf20Sopenharmony_ci				su_sta_cnt;
6808c2ecf20Sopenharmony_ci		}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci		i2c->ac_timing.sda_timing = (1 << 12) |
6838c2ecf20Sopenharmony_ci			(sda_max + sda_min) / 2;
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	return 0;
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci/*
6908c2ecf20Sopenharmony_ci * Calculate i2c port speed
6918c2ecf20Sopenharmony_ci *
6928c2ecf20Sopenharmony_ci * Hardware design:
6938c2ecf20Sopenharmony_ci * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
6948c2ecf20Sopenharmony_ci * clock_div: fixed in hardware, but may be various in different SoCs
6958c2ecf20Sopenharmony_ci *
6968c2ecf20Sopenharmony_ci * The calculation want to pick the highest bus frequency that is still
6978c2ecf20Sopenharmony_ci * less than or equal to i2c->speed_hz. The calculation try to get
6988c2ecf20Sopenharmony_ci * sample_cnt and step_cn
6998c2ecf20Sopenharmony_ci */
7008c2ecf20Sopenharmony_cistatic int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src,
7018c2ecf20Sopenharmony_ci				   unsigned int target_speed,
7028c2ecf20Sopenharmony_ci				   unsigned int *timing_step_cnt,
7038c2ecf20Sopenharmony_ci				   unsigned int *timing_sample_cnt)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	unsigned int step_cnt;
7068c2ecf20Sopenharmony_ci	unsigned int sample_cnt;
7078c2ecf20Sopenharmony_ci	unsigned int max_step_cnt;
7088c2ecf20Sopenharmony_ci	unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV;
7098c2ecf20Sopenharmony_ci	unsigned int base_step_cnt;
7108c2ecf20Sopenharmony_ci	unsigned int opt_div;
7118c2ecf20Sopenharmony_ci	unsigned int best_mul;
7128c2ecf20Sopenharmony_ci	unsigned int cnt_mul;
7138c2ecf20Sopenharmony_ci	int ret = -EINVAL;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	if (target_speed > I2C_MAX_HIGH_SPEED_MODE_FREQ)
7168c2ecf20Sopenharmony_ci		target_speed = I2C_MAX_HIGH_SPEED_MODE_FREQ;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	max_step_cnt = mtk_i2c_max_step_cnt(target_speed);
7198c2ecf20Sopenharmony_ci	base_step_cnt = max_step_cnt;
7208c2ecf20Sopenharmony_ci	/* Find the best combination */
7218c2ecf20Sopenharmony_ci	opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed);
7228c2ecf20Sopenharmony_ci	best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	/* Search for the best pair (sample_cnt, step_cnt) with
7258c2ecf20Sopenharmony_ci	 * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
7268c2ecf20Sopenharmony_ci	 * 0 < step_cnt < max_step_cnt
7278c2ecf20Sopenharmony_ci	 * sample_cnt * step_cnt >= opt_div
7288c2ecf20Sopenharmony_ci	 * optimizing for sample_cnt * step_cnt being minimal
7298c2ecf20Sopenharmony_ci	 */
7308c2ecf20Sopenharmony_ci	for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
7318c2ecf20Sopenharmony_ci		step_cnt = DIV_ROUND_UP(opt_div, sample_cnt);
7328c2ecf20Sopenharmony_ci		cnt_mul = step_cnt * sample_cnt;
7338c2ecf20Sopenharmony_ci		if (step_cnt > max_step_cnt)
7348c2ecf20Sopenharmony_ci			continue;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		if (cnt_mul < best_mul) {
7378c2ecf20Sopenharmony_ci			ret = mtk_i2c_check_ac_timing(i2c, clk_src,
7388c2ecf20Sopenharmony_ci				target_speed, step_cnt - 1, sample_cnt - 1);
7398c2ecf20Sopenharmony_ci			if (ret)
7408c2ecf20Sopenharmony_ci				continue;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci			best_mul = cnt_mul;
7438c2ecf20Sopenharmony_ci			base_sample_cnt = sample_cnt;
7448c2ecf20Sopenharmony_ci			base_step_cnt = step_cnt;
7458c2ecf20Sopenharmony_ci			if (best_mul == opt_div)
7468c2ecf20Sopenharmony_ci				break;
7478c2ecf20Sopenharmony_ci		}
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (ret)
7518c2ecf20Sopenharmony_ci		return -EINVAL;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	sample_cnt = base_sample_cnt;
7548c2ecf20Sopenharmony_ci	step_cnt = base_step_cnt;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) {
7578c2ecf20Sopenharmony_ci		/* In this case, hardware can't support such
7588c2ecf20Sopenharmony_ci		 * low i2c_bus_freq
7598c2ecf20Sopenharmony_ci		 */
7608c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n",	target_speed);
7618c2ecf20Sopenharmony_ci		return -EINVAL;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	*timing_step_cnt = step_cnt - 1;
7658c2ecf20Sopenharmony_ci	*timing_sample_cnt = sample_cnt - 1;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	return 0;
7688c2ecf20Sopenharmony_ci}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_cistatic int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	unsigned int clk_src;
7738c2ecf20Sopenharmony_ci	unsigned int step_cnt;
7748c2ecf20Sopenharmony_ci	unsigned int sample_cnt;
7758c2ecf20Sopenharmony_ci	unsigned int l_step_cnt;
7768c2ecf20Sopenharmony_ci	unsigned int l_sample_cnt;
7778c2ecf20Sopenharmony_ci	unsigned int target_speed;
7788c2ecf20Sopenharmony_ci	unsigned int clk_div;
7798c2ecf20Sopenharmony_ci	unsigned int max_clk_div;
7808c2ecf20Sopenharmony_ci	int ret;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	target_speed = i2c->speed_hz;
7838c2ecf20Sopenharmony_ci	parent_clk /= i2c->clk_src_div;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	if (i2c->dev_comp->timing_adjust)
7868c2ecf20Sopenharmony_ci		max_clk_div = MAX_CLOCK_DIV;
7878c2ecf20Sopenharmony_ci	else
7888c2ecf20Sopenharmony_ci		max_clk_div = 1;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	for (clk_div = 1; clk_div <= max_clk_div; clk_div++) {
7918c2ecf20Sopenharmony_ci		clk_src = parent_clk / clk_div;
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci		if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ) {
7948c2ecf20Sopenharmony_ci			/* Set master code speed register */
7958c2ecf20Sopenharmony_ci			ret = mtk_i2c_calculate_speed(i2c, clk_src,
7968c2ecf20Sopenharmony_ci						      I2C_MAX_FAST_MODE_FREQ,
7978c2ecf20Sopenharmony_ci						      &l_step_cnt,
7988c2ecf20Sopenharmony_ci						      &l_sample_cnt);
7998c2ecf20Sopenharmony_ci			if (ret < 0)
8008c2ecf20Sopenharmony_ci				continue;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci			i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci			/* Set the high speed mode register */
8058c2ecf20Sopenharmony_ci			ret = mtk_i2c_calculate_speed(i2c, clk_src,
8068c2ecf20Sopenharmony_ci						      target_speed, &step_cnt,
8078c2ecf20Sopenharmony_ci						      &sample_cnt);
8088c2ecf20Sopenharmony_ci			if (ret < 0)
8098c2ecf20Sopenharmony_ci				continue;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci			i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE |
8128c2ecf20Sopenharmony_ci					(sample_cnt << 12) | (step_cnt << 8);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci			if (i2c->dev_comp->ltiming_adjust)
8158c2ecf20Sopenharmony_ci				i2c->ltiming_reg =
8168c2ecf20Sopenharmony_ci					(l_sample_cnt << 6) | l_step_cnt |
8178c2ecf20Sopenharmony_ci					(sample_cnt << 12) | (step_cnt << 9);
8188c2ecf20Sopenharmony_ci		} else {
8198c2ecf20Sopenharmony_ci			ret = mtk_i2c_calculate_speed(i2c, clk_src,
8208c2ecf20Sopenharmony_ci						      target_speed, &l_step_cnt,
8218c2ecf20Sopenharmony_ci						      &l_sample_cnt);
8228c2ecf20Sopenharmony_ci			if (ret < 0)
8238c2ecf20Sopenharmony_ci				continue;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci			i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci			/* Disable the high speed transaction */
8288c2ecf20Sopenharmony_ci			i2c->high_speed_reg = I2C_TIME_CLR_VALUE;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci			if (i2c->dev_comp->ltiming_adjust)
8318c2ecf20Sopenharmony_ci				i2c->ltiming_reg =
8328c2ecf20Sopenharmony_ci					(l_sample_cnt << 6) | l_step_cnt;
8338c2ecf20Sopenharmony_ci		}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci		break;
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	i2c->ac_timing.inter_clk_div = clk_div - 1;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	return 0;
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_cistatic int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
8448c2ecf20Sopenharmony_ci			       int num, int left_num)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	u16 addr_reg;
8478c2ecf20Sopenharmony_ci	u16 start_reg;
8488c2ecf20Sopenharmony_ci	u16 control_reg;
8498c2ecf20Sopenharmony_ci	u16 restart_flag = 0;
8508c2ecf20Sopenharmony_ci	u16 dma_sync = 0;
8518c2ecf20Sopenharmony_ci	u32 reg_4g_mode;
8528c2ecf20Sopenharmony_ci	u8 *dma_rd_buf = NULL;
8538c2ecf20Sopenharmony_ci	u8 *dma_wr_buf = NULL;
8548c2ecf20Sopenharmony_ci	dma_addr_t rpaddr = 0;
8558c2ecf20Sopenharmony_ci	dma_addr_t wpaddr = 0;
8568c2ecf20Sopenharmony_ci	int ret;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	i2c->irq_stat = 0;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	if (i2c->auto_restart)
8618c2ecf20Sopenharmony_ci		restart_flag = I2C_RS_TRANSFER;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	reinit_completion(&i2c->msg_complete);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) &
8668c2ecf20Sopenharmony_ci			~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS);
8678c2ecf20Sopenharmony_ci	if ((i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ) || (left_num >= 1))
8688c2ecf20Sopenharmony_ci		control_reg |= I2C_CONTROL_RS;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	if (i2c->op == I2C_MASTER_WRRD)
8718c2ecf20Sopenharmony_ci		control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	addr_reg = i2c_8bit_addr_from_msg(msgs);
8768c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Clear interrupt status */
8798c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
8808c2ecf20Sopenharmony_ci			    I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR);
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	/* Enable interrupt */
8858c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
8868c2ecf20Sopenharmony_ci			    I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	/* Set transfer and transaction len */
8898c2ecf20Sopenharmony_ci	if (i2c->op == I2C_MASTER_WRRD) {
8908c2ecf20Sopenharmony_ci		if (i2c->dev_comp->aux_len_reg) {
8918c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN);
8928c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, (msgs + 1)->len,
8938c2ecf20Sopenharmony_ci					    OFFSET_TRANSFER_LEN_AUX);
8948c2ecf20Sopenharmony_ci		} else {
8958c2ecf20Sopenharmony_ci			mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8,
8968c2ecf20Sopenharmony_ci					    OFFSET_TRANSFER_LEN);
8978c2ecf20Sopenharmony_ci		}
8988c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN);
8998c2ecf20Sopenharmony_ci	} else {
9008c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN);
9018c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN);
9028c2ecf20Sopenharmony_ci	}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	if (i2c->dev_comp->apdma_sync) {
9058c2ecf20Sopenharmony_ci		dma_sync = I2C_DMA_SKIP_CONFIG | I2C_DMA_ASYNC_MODE;
9068c2ecf20Sopenharmony_ci		if (i2c->op == I2C_MASTER_WRRD)
9078c2ecf20Sopenharmony_ci			dma_sync |= I2C_DMA_DIR_CHANGE;
9088c2ecf20Sopenharmony_ci	}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/* Prepare buffer data to start transfer */
9118c2ecf20Sopenharmony_ci	if (i2c->op == I2C_MASTER_RD) {
9128c2ecf20Sopenharmony_ci		writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
9138c2ecf20Sopenharmony_ci		writel(I2C_DMA_CON_RX | dma_sync, i2c->pdmabase + OFFSET_CON);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci		dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
9168c2ecf20Sopenharmony_ci		if (!dma_rd_buf)
9178c2ecf20Sopenharmony_ci			return -ENOMEM;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci		rpaddr = dma_map_single(i2c->dev, dma_rd_buf,
9208c2ecf20Sopenharmony_ci					msgs->len, DMA_FROM_DEVICE);
9218c2ecf20Sopenharmony_ci		if (dma_mapping_error(i2c->dev, rpaddr)) {
9228c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci			return -ENOMEM;
9258c2ecf20Sopenharmony_ci		}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci		if (i2c->dev_comp->max_dma_support > 32) {
9288c2ecf20Sopenharmony_ci			reg_4g_mode = upper_32_bits(rpaddr);
9298c2ecf20Sopenharmony_ci			writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
9308c2ecf20Sopenharmony_ci		}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci		writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
9338c2ecf20Sopenharmony_ci		writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN);
9348c2ecf20Sopenharmony_ci	} else if (i2c->op == I2C_MASTER_WR) {
9358c2ecf20Sopenharmony_ci		writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
9368c2ecf20Sopenharmony_ci		writel(I2C_DMA_CON_TX | dma_sync, i2c->pdmabase + OFFSET_CON);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci		dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
9398c2ecf20Sopenharmony_ci		if (!dma_wr_buf)
9408c2ecf20Sopenharmony_ci			return -ENOMEM;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		wpaddr = dma_map_single(i2c->dev, dma_wr_buf,
9438c2ecf20Sopenharmony_ci					msgs->len, DMA_TO_DEVICE);
9448c2ecf20Sopenharmony_ci		if (dma_mapping_error(i2c->dev, wpaddr)) {
9458c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci			return -ENOMEM;
9488c2ecf20Sopenharmony_ci		}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci		if (i2c->dev_comp->max_dma_support > 32) {
9518c2ecf20Sopenharmony_ci			reg_4g_mode = upper_32_bits(wpaddr);
9528c2ecf20Sopenharmony_ci			writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
9538c2ecf20Sopenharmony_ci		}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci		writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
9568c2ecf20Sopenharmony_ci		writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
9578c2ecf20Sopenharmony_ci	} else {
9588c2ecf20Sopenharmony_ci		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG);
9598c2ecf20Sopenharmony_ci		writel(I2C_DMA_CLR_FLAG | dma_sync, i2c->pdmabase + OFFSET_CON);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci		dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
9628c2ecf20Sopenharmony_ci		if (!dma_wr_buf)
9638c2ecf20Sopenharmony_ci			return -ENOMEM;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci		wpaddr = dma_map_single(i2c->dev, dma_wr_buf,
9668c2ecf20Sopenharmony_ci					msgs->len, DMA_TO_DEVICE);
9678c2ecf20Sopenharmony_ci		if (dma_mapping_error(i2c->dev, wpaddr)) {
9688c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci			return -ENOMEM;
9718c2ecf20Sopenharmony_ci		}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci		dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1);
9748c2ecf20Sopenharmony_ci		if (!dma_rd_buf) {
9758c2ecf20Sopenharmony_ci			dma_unmap_single(i2c->dev, wpaddr,
9768c2ecf20Sopenharmony_ci					 msgs->len, DMA_TO_DEVICE);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci			return -ENOMEM;
9818c2ecf20Sopenharmony_ci		}
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci		rpaddr = dma_map_single(i2c->dev, dma_rd_buf,
9848c2ecf20Sopenharmony_ci					(msgs + 1)->len,
9858c2ecf20Sopenharmony_ci					DMA_FROM_DEVICE);
9868c2ecf20Sopenharmony_ci		if (dma_mapping_error(i2c->dev, rpaddr)) {
9878c2ecf20Sopenharmony_ci			dma_unmap_single(i2c->dev, wpaddr,
9888c2ecf20Sopenharmony_ci					 msgs->len, DMA_TO_DEVICE);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
9918c2ecf20Sopenharmony_ci			i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci			return -ENOMEM;
9948c2ecf20Sopenharmony_ci		}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci		if (i2c->dev_comp->max_dma_support > 32) {
9978c2ecf20Sopenharmony_ci			reg_4g_mode = upper_32_bits(wpaddr);
9988c2ecf20Sopenharmony_ci			writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci			reg_4g_mode = upper_32_bits(rpaddr);
10018c2ecf20Sopenharmony_ci			writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
10028c2ecf20Sopenharmony_ci		}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci		writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
10058c2ecf20Sopenharmony_ci		writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
10068c2ecf20Sopenharmony_ci		writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
10078c2ecf20Sopenharmony_ci		writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN);
10088c2ecf20Sopenharmony_ci	}
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	if (!i2c->auto_restart) {
10138c2ecf20Sopenharmony_ci		start_reg = I2C_TRANSAC_START;
10148c2ecf20Sopenharmony_ci	} else {
10158c2ecf20Sopenharmony_ci		start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG;
10168c2ecf20Sopenharmony_ci		if (left_num >= 1)
10178c2ecf20Sopenharmony_ci			start_reg |= I2C_RS_MUL_CNFG;
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, start_reg, OFFSET_START);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	ret = wait_for_completion_timeout(&i2c->msg_complete,
10228c2ecf20Sopenharmony_ci					  i2c->adap.timeout);
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	/* Clear interrupt mask */
10258c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
10268c2ecf20Sopenharmony_ci			    I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	if (i2c->op == I2C_MASTER_WR) {
10298c2ecf20Sopenharmony_ci		dma_unmap_single(i2c->dev, wpaddr,
10308c2ecf20Sopenharmony_ci				 msgs->len, DMA_TO_DEVICE);
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci		i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true);
10338c2ecf20Sopenharmony_ci	} else if (i2c->op == I2C_MASTER_RD) {
10348c2ecf20Sopenharmony_ci		dma_unmap_single(i2c->dev, rpaddr,
10358c2ecf20Sopenharmony_ci				 msgs->len, DMA_FROM_DEVICE);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci		i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true);
10388c2ecf20Sopenharmony_ci	} else {
10398c2ecf20Sopenharmony_ci		dma_unmap_single(i2c->dev, wpaddr, msgs->len,
10408c2ecf20Sopenharmony_ci				 DMA_TO_DEVICE);
10418c2ecf20Sopenharmony_ci		dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len,
10428c2ecf20Sopenharmony_ci				 DMA_FROM_DEVICE);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true);
10458c2ecf20Sopenharmony_ci		i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true);
10468c2ecf20Sopenharmony_ci	}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	if (ret == 0) {
10498c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr);
10508c2ecf20Sopenharmony_ci		mtk_i2c_init_hw(i2c);
10518c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) {
10558c2ecf20Sopenharmony_ci		dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr);
10568c2ecf20Sopenharmony_ci		mtk_i2c_init_hw(i2c);
10578c2ecf20Sopenharmony_ci		return -ENXIO;
10588c2ecf20Sopenharmony_ci	}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	return 0;
10618c2ecf20Sopenharmony_ci}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistatic int mtk_i2c_transfer(struct i2c_adapter *adap,
10648c2ecf20Sopenharmony_ci			    struct i2c_msg msgs[], int num)
10658c2ecf20Sopenharmony_ci{
10668c2ecf20Sopenharmony_ci	int ret;
10678c2ecf20Sopenharmony_ci	int left_num = num;
10688c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c = i2c_get_adapdata(adap);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	ret = mtk_i2c_clock_enable(i2c);
10718c2ecf20Sopenharmony_ci	if (ret)
10728c2ecf20Sopenharmony_ci		return ret;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	i2c->auto_restart = i2c->dev_comp->auto_restart;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	/* checking if we can skip restart and optimize using WRRD mode */
10778c2ecf20Sopenharmony_ci	if (i2c->auto_restart && num == 2) {
10788c2ecf20Sopenharmony_ci		if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) &&
10798c2ecf20Sopenharmony_ci		    msgs[0].addr == msgs[1].addr) {
10808c2ecf20Sopenharmony_ci			i2c->auto_restart = 0;
10818c2ecf20Sopenharmony_ci		}
10828c2ecf20Sopenharmony_ci	}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	if (i2c->auto_restart && num >= 2 &&
10858c2ecf20Sopenharmony_ci		i2c->speed_hz > I2C_MAX_FAST_MODE_PLUS_FREQ)
10868c2ecf20Sopenharmony_ci		/* ignore the first restart irq after the master code,
10878c2ecf20Sopenharmony_ci		 * otherwise the first transfer will be discarded.
10888c2ecf20Sopenharmony_ci		 */
10898c2ecf20Sopenharmony_ci		i2c->ignore_restart_irq = true;
10908c2ecf20Sopenharmony_ci	else
10918c2ecf20Sopenharmony_ci		i2c->ignore_restart_irq = false;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	while (left_num--) {
10948c2ecf20Sopenharmony_ci		if (!msgs->buf) {
10958c2ecf20Sopenharmony_ci			dev_dbg(i2c->dev, "data buffer is NULL.\n");
10968c2ecf20Sopenharmony_ci			ret = -EINVAL;
10978c2ecf20Sopenharmony_ci			goto err_exit;
10988c2ecf20Sopenharmony_ci		}
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci		if (msgs->flags & I2C_M_RD)
11018c2ecf20Sopenharmony_ci			i2c->op = I2C_MASTER_RD;
11028c2ecf20Sopenharmony_ci		else
11038c2ecf20Sopenharmony_ci			i2c->op = I2C_MASTER_WR;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci		if (!i2c->auto_restart) {
11068c2ecf20Sopenharmony_ci			if (num > 1) {
11078c2ecf20Sopenharmony_ci				/* combined two messages into one transaction */
11088c2ecf20Sopenharmony_ci				i2c->op = I2C_MASTER_WRRD;
11098c2ecf20Sopenharmony_ci				left_num--;
11108c2ecf20Sopenharmony_ci			}
11118c2ecf20Sopenharmony_ci		}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci		/* always use DMA mode. */
11148c2ecf20Sopenharmony_ci		ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num);
11158c2ecf20Sopenharmony_ci		if (ret < 0)
11168c2ecf20Sopenharmony_ci			goto err_exit;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci		msgs++;
11198c2ecf20Sopenharmony_ci	}
11208c2ecf20Sopenharmony_ci	/* the return value is number of executed messages */
11218c2ecf20Sopenharmony_ci	ret = num;
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_cierr_exit:
11248c2ecf20Sopenharmony_ci	mtk_i2c_clock_disable(i2c);
11258c2ecf20Sopenharmony_ci	return ret;
11268c2ecf20Sopenharmony_ci}
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_cistatic irqreturn_t mtk_i2c_irq(int irqno, void *dev_id)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c = dev_id;
11318c2ecf20Sopenharmony_ci	u16 restart_flag = 0;
11328c2ecf20Sopenharmony_ci	u16 intr_stat;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	if (i2c->auto_restart)
11358c2ecf20Sopenharmony_ci		restart_flag = I2C_RS_TRANSFER;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT);
11388c2ecf20Sopenharmony_ci	mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	/*
11418c2ecf20Sopenharmony_ci	 * when occurs ack error, i2c controller generate two interrupts
11428c2ecf20Sopenharmony_ci	 * first is the ack error interrupt, then the complete interrupt
11438c2ecf20Sopenharmony_ci	 * i2c->irq_stat need keep the two interrupt value.
11448c2ecf20Sopenharmony_ci	 */
11458c2ecf20Sopenharmony_ci	i2c->irq_stat |= intr_stat;
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) {
11488c2ecf20Sopenharmony_ci		i2c->ignore_restart_irq = false;
11498c2ecf20Sopenharmony_ci		i2c->irq_stat = 0;
11508c2ecf20Sopenharmony_ci		mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG |
11518c2ecf20Sopenharmony_ci				    I2C_TRANSAC_START, OFFSET_START);
11528c2ecf20Sopenharmony_ci	} else {
11538c2ecf20Sopenharmony_ci		if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag))
11548c2ecf20Sopenharmony_ci			complete(&i2c->msg_complete);
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
11588c2ecf20Sopenharmony_ci}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_cistatic u32 mtk_i2c_functionality(struct i2c_adapter *adap)
11618c2ecf20Sopenharmony_ci{
11628c2ecf20Sopenharmony_ci	if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN))
11638c2ecf20Sopenharmony_ci		return I2C_FUNC_I2C |
11648c2ecf20Sopenharmony_ci			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
11658c2ecf20Sopenharmony_ci	else
11668c2ecf20Sopenharmony_ci		return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_cistatic const struct i2c_algorithm mtk_i2c_algorithm = {
11708c2ecf20Sopenharmony_ci	.master_xfer = mtk_i2c_transfer,
11718c2ecf20Sopenharmony_ci	.functionality = mtk_i2c_functionality,
11728c2ecf20Sopenharmony_ci};
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_cistatic int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c)
11758c2ecf20Sopenharmony_ci{
11768c2ecf20Sopenharmony_ci	int ret;
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz);
11798c2ecf20Sopenharmony_ci	if (ret < 0)
11808c2ecf20Sopenharmony_ci		i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div);
11838c2ecf20Sopenharmony_ci	if (ret < 0)
11848c2ecf20Sopenharmony_ci		return ret;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	if (i2c->clk_src_div == 0)
11878c2ecf20Sopenharmony_ci		return -EINVAL;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic");
11908c2ecf20Sopenharmony_ci	i2c->use_push_pull =
11918c2ecf20Sopenharmony_ci		of_property_read_bool(np, "mediatek,use-push-pull");
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	return 0;
11948c2ecf20Sopenharmony_ci}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_cistatic int mtk_i2c_probe(struct platform_device *pdev)
11978c2ecf20Sopenharmony_ci{
11988c2ecf20Sopenharmony_ci	int ret = 0;
11998c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c;
12008c2ecf20Sopenharmony_ci	struct clk *clk;
12018c2ecf20Sopenharmony_ci	struct resource *res;
12028c2ecf20Sopenharmony_ci	int irq;
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_ci	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
12058c2ecf20Sopenharmony_ci	if (!i2c)
12068c2ecf20Sopenharmony_ci		return -ENOMEM;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12098c2ecf20Sopenharmony_ci	i2c->base = devm_ioremap_resource(&pdev->dev, res);
12108c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->base))
12118c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->base);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
12148c2ecf20Sopenharmony_ci	i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res);
12158c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->pdmabase))
12168c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->pdmabase);
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
12198c2ecf20Sopenharmony_ci	if (irq < 0)
12208c2ecf20Sopenharmony_ci		return irq;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	init_completion(&i2c->msg_complete);
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	i2c->dev_comp = of_device_get_match_data(&pdev->dev);
12258c2ecf20Sopenharmony_ci	i2c->adap.dev.of_node = pdev->dev.of_node;
12268c2ecf20Sopenharmony_ci	i2c->dev = &pdev->dev;
12278c2ecf20Sopenharmony_ci	i2c->adap.dev.parent = &pdev->dev;
12288c2ecf20Sopenharmony_ci	i2c->adap.owner = THIS_MODULE;
12298c2ecf20Sopenharmony_ci	i2c->adap.algo = &mtk_i2c_algorithm;
12308c2ecf20Sopenharmony_ci	i2c->adap.quirks = i2c->dev_comp->quirks;
12318c2ecf20Sopenharmony_ci	i2c->adap.timeout = 2 * HZ;
12328c2ecf20Sopenharmony_ci	i2c->adap.retries = 1;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c);
12358c2ecf20Sopenharmony_ci	if (ret)
12368c2ecf20Sopenharmony_ci		return -EINVAL;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c)
12398c2ecf20Sopenharmony_ci		return -EINVAL;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	i2c->clk_main = devm_clk_get(&pdev->dev, "main");
12428c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->clk_main)) {
12438c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot get main clock\n");
12448c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->clk_main);
12458c2ecf20Sopenharmony_ci	}
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	i2c->clk_dma = devm_clk_get(&pdev->dev, "dma");
12488c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->clk_dma)) {
12498c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "cannot get dma clock\n");
12508c2ecf20Sopenharmony_ci		return PTR_ERR(i2c->clk_dma);
12518c2ecf20Sopenharmony_ci	}
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	i2c->clk_arb = devm_clk_get(&pdev->dev, "arb");
12548c2ecf20Sopenharmony_ci	if (IS_ERR(i2c->clk_arb))
12558c2ecf20Sopenharmony_ci		i2c->clk_arb = NULL;
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	clk = i2c->clk_main;
12588c2ecf20Sopenharmony_ci	if (i2c->have_pmic) {
12598c2ecf20Sopenharmony_ci		i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic");
12608c2ecf20Sopenharmony_ci		if (IS_ERR(i2c->clk_pmic)) {
12618c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "cannot get pmic clock\n");
12628c2ecf20Sopenharmony_ci			return PTR_ERR(i2c->clk_pmic);
12638c2ecf20Sopenharmony_ci		}
12648c2ecf20Sopenharmony_ci		clk = i2c->clk_pmic;
12658c2ecf20Sopenharmony_ci	}
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name));
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk));
12708c2ecf20Sopenharmony_ci	if (ret) {
12718c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to set the speed.\n");
12728c2ecf20Sopenharmony_ci		return -EINVAL;
12738c2ecf20Sopenharmony_ci	}
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	if (i2c->dev_comp->max_dma_support > 32) {
12768c2ecf20Sopenharmony_ci		ret = dma_set_mask(&pdev->dev,
12778c2ecf20Sopenharmony_ci				DMA_BIT_MASK(i2c->dev_comp->max_dma_support));
12788c2ecf20Sopenharmony_ci		if (ret) {
12798c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "dma_set_mask return error.\n");
12808c2ecf20Sopenharmony_ci			return ret;
12818c2ecf20Sopenharmony_ci		}
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	ret = mtk_i2c_clock_enable(i2c);
12858c2ecf20Sopenharmony_ci	if (ret) {
12868c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "clock enable failed!\n");
12878c2ecf20Sopenharmony_ci		return ret;
12888c2ecf20Sopenharmony_ci	}
12898c2ecf20Sopenharmony_ci	mtk_i2c_init_hw(i2c);
12908c2ecf20Sopenharmony_ci	mtk_i2c_clock_disable(i2c);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq,
12938c2ecf20Sopenharmony_ci			       IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE,
12948c2ecf20Sopenharmony_ci			       I2C_DRV_NAME, i2c);
12958c2ecf20Sopenharmony_ci	if (ret < 0) {
12968c2ecf20Sopenharmony_ci		dev_err(&pdev->dev,
12978c2ecf20Sopenharmony_ci			"Request I2C IRQ %d fail\n", irq);
12988c2ecf20Sopenharmony_ci		return ret;
12998c2ecf20Sopenharmony_ci	}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	i2c_set_adapdata(&i2c->adap, i2c);
13028c2ecf20Sopenharmony_ci	ret = i2c_add_adapter(&i2c->adap);
13038c2ecf20Sopenharmony_ci	if (ret)
13048c2ecf20Sopenharmony_ci		return ret;
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, i2c);
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	return 0;
13098c2ecf20Sopenharmony_ci}
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_cistatic int mtk_i2c_remove(struct platform_device *pdev)
13128c2ecf20Sopenharmony_ci{
13138c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c = platform_get_drvdata(pdev);
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	i2c_del_adapter(&i2c->adap);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	return 0;
13188c2ecf20Sopenharmony_ci}
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
13218c2ecf20Sopenharmony_cistatic int mtk_i2c_suspend_noirq(struct device *dev)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c = dev_get_drvdata(dev);
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	i2c_mark_adapter_suspended(&i2c->adap);
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	return 0;
13288c2ecf20Sopenharmony_ci}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_cistatic int mtk_i2c_resume_noirq(struct device *dev)
13318c2ecf20Sopenharmony_ci{
13328c2ecf20Sopenharmony_ci	int ret;
13338c2ecf20Sopenharmony_ci	struct mtk_i2c *i2c = dev_get_drvdata(dev);
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	ret = mtk_i2c_clock_enable(i2c);
13368c2ecf20Sopenharmony_ci	if (ret) {
13378c2ecf20Sopenharmony_ci		dev_err(dev, "clock enable failed!\n");
13388c2ecf20Sopenharmony_ci		return ret;
13398c2ecf20Sopenharmony_ci	}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	mtk_i2c_init_hw(i2c);
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	mtk_i2c_clock_disable(i2c);
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	i2c_mark_adapter_resumed(&i2c->adap);
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	return 0;
13488c2ecf20Sopenharmony_ci}
13498c2ecf20Sopenharmony_ci#endif
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mtk_i2c_pm = {
13528c2ecf20Sopenharmony_ci	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_i2c_suspend_noirq,
13538c2ecf20Sopenharmony_ci				      mtk_i2c_resume_noirq)
13548c2ecf20Sopenharmony_ci};
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_cistatic struct platform_driver mtk_i2c_driver = {
13578c2ecf20Sopenharmony_ci	.probe = mtk_i2c_probe,
13588c2ecf20Sopenharmony_ci	.remove = mtk_i2c_remove,
13598c2ecf20Sopenharmony_ci	.driver = {
13608c2ecf20Sopenharmony_ci		.name = I2C_DRV_NAME,
13618c2ecf20Sopenharmony_ci		.pm = &mtk_i2c_pm,
13628c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mtk_i2c_of_match),
13638c2ecf20Sopenharmony_ci	},
13648c2ecf20Sopenharmony_ci};
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_cimodule_platform_driver(mtk_i2c_driver);
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
13698c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MediaTek I2C Bus Driver");
13708c2ecf20Sopenharmony_ciMODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");
1371