18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Driver for the i2c controller on the Marvell line of host bridges
38c2ecf20Sopenharmony_ci * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family).
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Mark A. Greer <mgreer@mvista.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * 2005 (c) MontaVista, Software, Inc.  This file is licensed under
88c2ecf20Sopenharmony_ci * the terms of the GNU General Public License version 2.  This program
98c2ecf20Sopenharmony_ci * is licensed "as is" without any warranty of any kind, whether express
108c2ecf20Sopenharmony_ci * or implied.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
168c2ecf20Sopenharmony_ci#include <linux/i2c.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/mv643xx_i2c.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/reset.h>
218c2ecf20Sopenharmony_ci#include <linux/io.h>
228c2ecf20Sopenharmony_ci#include <linux/of.h>
238c2ecf20Sopenharmony_ci#include <linux/of_device.h>
248c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
258c2ecf20Sopenharmony_ci#include <linux/clk.h>
268c2ecf20Sopenharmony_ci#include <linux/err.h>
278c2ecf20Sopenharmony_ci#include <linux/delay.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define MV64XXX_I2C_ADDR_ADDR(val)			((val & 0x7f) << 1)
308c2ecf20Sopenharmony_ci#define MV64XXX_I2C_BAUD_DIV_N(val)			(val & 0x7)
318c2ecf20Sopenharmony_ci#define MV64XXX_I2C_BAUD_DIV_M(val)			((val & 0xf) << 3)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_ACK			BIT(2)
348c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_IFLG			BIT(3)
358c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_STOP			BIT(4)
368c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_START			BIT(5)
378c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_TWSIEN			BIT(6)
388c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_CONTROL_INTEN			BIT(7)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Ctlr status values */
418c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_BUS_ERR			0x00
428c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_START			0x08
438c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_REPEAT_START		0x10
448c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK		0x18
458c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK		0x20
468c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_ACK			0x28
478c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_NO_ACK		0x30
488c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_LOST_ARB		0x38
498c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK		0x40
508c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK		0x48
518c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK		0x50
528c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK		0x58
538c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK		0xd0
548c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK	0xd8
558c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK		0xe0
568c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK	0xe8
578c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_STATUS_NO_STATUS			0xf8
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* Register defines (I2C bridge) */
608c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_TX_DATA_LO			0xc0
618c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_TX_DATA_HI			0xc4
628c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_RX_DATA_LO			0xc8
638c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_RX_DATA_HI			0xcc
648c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_BRIDGE_CONTROL			0xd0
658c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_BRIDGE_STATUS			0xd4
668c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE		0xd8
678c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_BRIDGE_INTR_MASK		0xdC
688c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_REG_BRIDGE_TIMING			0xe0
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* Bridge Control values */
718c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_WR			BIT(0)
728c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_RD			BIT(1)
738c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT		2
748c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT		BIT(12)
758c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT	13
768c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT	16
778c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_ENABLE		BIT(19)
788c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START	BIT(20)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Bridge Status values */
818c2ecf20Sopenharmony_ci#define	MV64XXX_I2C_BRIDGE_STATUS_ERROR			BIT(0)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* Driver states */
848c2ecf20Sopenharmony_cienum {
858c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_INVALID,
868c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_IDLE,
878c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_START_COND,
888c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_RESTART,
898c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK,
908c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK,
918c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK,
928c2ecf20Sopenharmony_ci	MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA,
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/* Driver actions */
968c2ecf20Sopenharmony_cienum {
978c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_INVALID,
988c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_CONTINUE,
998c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_SEND_RESTART,
1008c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_SEND_ADDR_1,
1018c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_SEND_ADDR_2,
1028c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_SEND_DATA,
1038c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_RCV_DATA,
1048c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_RCV_DATA_STOP,
1058c2ecf20Sopenharmony_ci	MV64XXX_I2C_ACTION_SEND_STOP,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistruct mv64xxx_i2c_regs {
1098c2ecf20Sopenharmony_ci	u8	addr;
1108c2ecf20Sopenharmony_ci	u8	ext_addr;
1118c2ecf20Sopenharmony_ci	u8	data;
1128c2ecf20Sopenharmony_ci	u8	control;
1138c2ecf20Sopenharmony_ci	u8	status;
1148c2ecf20Sopenharmony_ci	u8	clock;
1158c2ecf20Sopenharmony_ci	u8	soft_reset;
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistruct mv64xxx_i2c_data {
1198c2ecf20Sopenharmony_ci	struct i2c_msg		*msgs;
1208c2ecf20Sopenharmony_ci	int			num_msgs;
1218c2ecf20Sopenharmony_ci	int			irq;
1228c2ecf20Sopenharmony_ci	u32			state;
1238c2ecf20Sopenharmony_ci	u32			action;
1248c2ecf20Sopenharmony_ci	u32			aborting;
1258c2ecf20Sopenharmony_ci	u32			cntl_bits;
1268c2ecf20Sopenharmony_ci	void __iomem		*reg_base;
1278c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_regs	reg_offsets;
1288c2ecf20Sopenharmony_ci	u32			addr1;
1298c2ecf20Sopenharmony_ci	u32			addr2;
1308c2ecf20Sopenharmony_ci	u32			bytes_left;
1318c2ecf20Sopenharmony_ci	u32			byte_posn;
1328c2ecf20Sopenharmony_ci	u32			send_stop;
1338c2ecf20Sopenharmony_ci	u32			block;
1348c2ecf20Sopenharmony_ci	int			rc;
1358c2ecf20Sopenharmony_ci	u32			freq_m;
1368c2ecf20Sopenharmony_ci	u32			freq_n;
1378c2ecf20Sopenharmony_ci	struct clk              *clk;
1388c2ecf20Sopenharmony_ci	struct clk              *reg_clk;
1398c2ecf20Sopenharmony_ci	wait_queue_head_t	waitq;
1408c2ecf20Sopenharmony_ci	spinlock_t		lock;
1418c2ecf20Sopenharmony_ci	struct i2c_msg		*msg;
1428c2ecf20Sopenharmony_ci	struct i2c_adapter	adapter;
1438c2ecf20Sopenharmony_ci	bool			offload_enabled;
1448c2ecf20Sopenharmony_ci/* 5us delay in order to avoid repeated start timing violation */
1458c2ecf20Sopenharmony_ci	bool			errata_delay;
1468c2ecf20Sopenharmony_ci	struct reset_control	*rstc;
1478c2ecf20Sopenharmony_ci	bool			irq_clear_inverted;
1488c2ecf20Sopenharmony_ci	/* Clk div is 2 to the power n, not 2 to the power n + 1 */
1498c2ecf20Sopenharmony_ci	bool			clk_n_base_0;
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic struct mv64xxx_i2c_regs mv64xxx_i2c_regs_mv64xxx = {
1538c2ecf20Sopenharmony_ci	.addr		= 0x00,
1548c2ecf20Sopenharmony_ci	.ext_addr	= 0x10,
1558c2ecf20Sopenharmony_ci	.data		= 0x04,
1568c2ecf20Sopenharmony_ci	.control	= 0x08,
1578c2ecf20Sopenharmony_ci	.status		= 0x0c,
1588c2ecf20Sopenharmony_ci	.clock		= 0x0c,
1598c2ecf20Sopenharmony_ci	.soft_reset	= 0x1c,
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct mv64xxx_i2c_regs mv64xxx_i2c_regs_sun4i = {
1638c2ecf20Sopenharmony_ci	.addr		= 0x00,
1648c2ecf20Sopenharmony_ci	.ext_addr	= 0x04,
1658c2ecf20Sopenharmony_ci	.data		= 0x08,
1668c2ecf20Sopenharmony_ci	.control	= 0x0c,
1678c2ecf20Sopenharmony_ci	.status		= 0x10,
1688c2ecf20Sopenharmony_ci	.clock		= 0x14,
1698c2ecf20Sopenharmony_ci	.soft_reset	= 0x18,
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void
1738c2ecf20Sopenharmony_cimv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data *drv_data,
1748c2ecf20Sopenharmony_ci	struct i2c_msg *msg)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	u32	dir = 0;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	drv_data->cntl_bits = MV64XXX_I2C_REG_CONTROL_ACK |
1798c2ecf20Sopenharmony_ci		MV64XXX_I2C_REG_CONTROL_INTEN | MV64XXX_I2C_REG_CONTROL_TWSIEN;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (msg->flags & I2C_M_RD)
1828c2ecf20Sopenharmony_ci		dir = 1;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	if (msg->flags & I2C_M_TEN) {
1858c2ecf20Sopenharmony_ci		drv_data->addr1 = 0xf0 | (((u32)msg->addr & 0x300) >> 7) | dir;
1868c2ecf20Sopenharmony_ci		drv_data->addr2 = (u32)msg->addr & 0xff;
1878c2ecf20Sopenharmony_ci	} else {
1888c2ecf20Sopenharmony_ci		drv_data->addr1 = MV64XXX_I2C_ADDR_ADDR((u32)msg->addr) | dir;
1898c2ecf20Sopenharmony_ci		drv_data->addr2 = 0;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/*
1948c2ecf20Sopenharmony_ci *****************************************************************************
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci *	Finite State Machine & Interrupt Routines
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci *****************************************************************************
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* Reset hardware and initialize FSM */
2028c2ecf20Sopenharmony_cistatic void
2038c2ecf20Sopenharmony_cimv64xxx_i2c_hw_init(struct mv64xxx_i2c_data *drv_data)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	if (drv_data->offload_enabled) {
2068c2ecf20Sopenharmony_ci		writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
2078c2ecf20Sopenharmony_ci		writel(0, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_TIMING);
2088c2ecf20Sopenharmony_ci		writel(0, drv_data->reg_base +
2098c2ecf20Sopenharmony_ci			MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
2108c2ecf20Sopenharmony_ci		writel(0, drv_data->reg_base +
2118c2ecf20Sopenharmony_ci			MV64XXX_I2C_REG_BRIDGE_INTR_MASK);
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	writel(0, drv_data->reg_base + drv_data->reg_offsets.soft_reset);
2158c2ecf20Sopenharmony_ci	writel(MV64XXX_I2C_BAUD_DIV_M(drv_data->freq_m) | MV64XXX_I2C_BAUD_DIV_N(drv_data->freq_n),
2168c2ecf20Sopenharmony_ci		drv_data->reg_base + drv_data->reg_offsets.clock);
2178c2ecf20Sopenharmony_ci	writel(0, drv_data->reg_base + drv_data->reg_offsets.addr);
2188c2ecf20Sopenharmony_ci	writel(0, drv_data->reg_base + drv_data->reg_offsets.ext_addr);
2198c2ecf20Sopenharmony_ci	writel(MV64XXX_I2C_REG_CONTROL_TWSIEN | MV64XXX_I2C_REG_CONTROL_STOP,
2208c2ecf20Sopenharmony_ci		drv_data->reg_base + drv_data->reg_offsets.control);
2218c2ecf20Sopenharmony_ci	drv_data->state = MV64XXX_I2C_STATE_IDLE;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void
2258c2ecf20Sopenharmony_cimv64xxx_i2c_fsm(struct mv64xxx_i2c_data *drv_data, u32 status)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	/*
2288c2ecf20Sopenharmony_ci	 * If state is idle, then this is likely the remnants of an old
2298c2ecf20Sopenharmony_ci	 * operation that driver has given up on or the user has killed.
2308c2ecf20Sopenharmony_ci	 * If so, issue the stop condition and go to idle.
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	if (drv_data->state == MV64XXX_I2C_STATE_IDLE) {
2338c2ecf20Sopenharmony_ci		drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
2348c2ecf20Sopenharmony_ci		return;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/* The status from the ctlr [mostly] tells us what to do next */
2388c2ecf20Sopenharmony_ci	switch (status) {
2398c2ecf20Sopenharmony_ci	/* Start condition interrupt */
2408c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_START: /* 0x08 */
2418c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_REPEAT_START: /* 0x10 */
2428c2ecf20Sopenharmony_ci		drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_1;
2438c2ecf20Sopenharmony_ci		drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK;
2448c2ecf20Sopenharmony_ci		break;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* Performing a write */
2478c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK: /* 0x18 */
2488c2ecf20Sopenharmony_ci		if (drv_data->msg->flags & I2C_M_TEN) {
2498c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
2508c2ecf20Sopenharmony_ci			drv_data->state =
2518c2ecf20Sopenharmony_ci				MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
2528c2ecf20Sopenharmony_ci			break;
2538c2ecf20Sopenharmony_ci		}
2548c2ecf20Sopenharmony_ci		fallthrough;
2558c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK: /* 0xd0 */
2568c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_WR_ACK: /* 0x28 */
2578c2ecf20Sopenharmony_ci		if ((drv_data->bytes_left == 0)
2588c2ecf20Sopenharmony_ci				|| (drv_data->aborting
2598c2ecf20Sopenharmony_ci					&& (drv_data->byte_posn != 0))) {
2608c2ecf20Sopenharmony_ci			if (drv_data->send_stop || drv_data->aborting) {
2618c2ecf20Sopenharmony_ci				drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
2628c2ecf20Sopenharmony_ci				drv_data->state = MV64XXX_I2C_STATE_IDLE;
2638c2ecf20Sopenharmony_ci			} else {
2648c2ecf20Sopenharmony_ci				drv_data->action =
2658c2ecf20Sopenharmony_ci					MV64XXX_I2C_ACTION_SEND_RESTART;
2668c2ecf20Sopenharmony_ci				drv_data->state =
2678c2ecf20Sopenharmony_ci					MV64XXX_I2C_STATE_WAITING_FOR_RESTART;
2688c2ecf20Sopenharmony_ci			}
2698c2ecf20Sopenharmony_ci		} else {
2708c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_SEND_DATA;
2718c2ecf20Sopenharmony_ci			drv_data->state =
2728c2ecf20Sopenharmony_ci				MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK;
2738c2ecf20Sopenharmony_ci			drv_data->bytes_left--;
2748c2ecf20Sopenharmony_ci		}
2758c2ecf20Sopenharmony_ci		break;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Performing a read */
2788c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK: /* 40 */
2798c2ecf20Sopenharmony_ci		if (drv_data->msg->flags & I2C_M_TEN) {
2808c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_SEND_ADDR_2;
2818c2ecf20Sopenharmony_ci			drv_data->state =
2828c2ecf20Sopenharmony_ci				MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK;
2838c2ecf20Sopenharmony_ci			break;
2848c2ecf20Sopenharmony_ci		}
2858c2ecf20Sopenharmony_ci		fallthrough;
2868c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK: /* 0xe0 */
2878c2ecf20Sopenharmony_ci		if (drv_data->bytes_left == 0) {
2888c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
2898c2ecf20Sopenharmony_ci			drv_data->state = MV64XXX_I2C_STATE_IDLE;
2908c2ecf20Sopenharmony_ci			break;
2918c2ecf20Sopenharmony_ci		}
2928c2ecf20Sopenharmony_ci		fallthrough;
2938c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK: /* 0x50 */
2948c2ecf20Sopenharmony_ci		if (status != MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK)
2958c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_CONTINUE;
2968c2ecf20Sopenharmony_ci		else {
2978c2ecf20Sopenharmony_ci			drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA;
2988c2ecf20Sopenharmony_ci			drv_data->bytes_left--;
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci		drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		if ((drv_data->bytes_left == 1) || drv_data->aborting)
3038c2ecf20Sopenharmony_ci			drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_ACK;
3048c2ecf20Sopenharmony_ci		break;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK: /* 0x58 */
3078c2ecf20Sopenharmony_ci		drv_data->action = MV64XXX_I2C_ACTION_RCV_DATA_STOP;
3088c2ecf20Sopenharmony_ci		drv_data->state = MV64XXX_I2C_STATE_IDLE;
3098c2ecf20Sopenharmony_ci		break;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK: /* 0x20 */
3128c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK: /* 30 */
3138c2ecf20Sopenharmony_ci	case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK: /* 48 */
3148c2ecf20Sopenharmony_ci		/* Doesn't seem to be a device at other end */
3158c2ecf20Sopenharmony_ci		drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
3168c2ecf20Sopenharmony_ci		drv_data->state = MV64XXX_I2C_STATE_IDLE;
3178c2ecf20Sopenharmony_ci		drv_data->rc = -ENXIO;
3188c2ecf20Sopenharmony_ci		break;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	default:
3218c2ecf20Sopenharmony_ci		dev_err(&drv_data->adapter.dev,
3228c2ecf20Sopenharmony_ci			"mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
3238c2ecf20Sopenharmony_ci			"status: 0x%x, addr: 0x%x, flags: 0x%x\n",
3248c2ecf20Sopenharmony_ci			 drv_data->state, status, drv_data->msg->addr,
3258c2ecf20Sopenharmony_ci			 drv_data->msg->flags);
3268c2ecf20Sopenharmony_ci		drv_data->action = MV64XXX_I2C_ACTION_SEND_STOP;
3278c2ecf20Sopenharmony_ci		mv64xxx_i2c_hw_init(drv_data);
3288c2ecf20Sopenharmony_ci		drv_data->rc = -EIO;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_cistatic void mv64xxx_i2c_send_start(struct mv64xxx_i2c_data *drv_data)
3338c2ecf20Sopenharmony_ci{
3348c2ecf20Sopenharmony_ci	drv_data->msg = drv_data->msgs;
3358c2ecf20Sopenharmony_ci	drv_data->byte_posn = 0;
3368c2ecf20Sopenharmony_ci	drv_data->bytes_left = drv_data->msg->len;
3378c2ecf20Sopenharmony_ci	drv_data->aborting = 0;
3388c2ecf20Sopenharmony_ci	drv_data->rc = 0;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	mv64xxx_i2c_prepare_for_io(drv_data, drv_data->msgs);
3418c2ecf20Sopenharmony_ci	writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_START,
3428c2ecf20Sopenharmony_ci	       drv_data->reg_base + drv_data->reg_offsets.control);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic void
3468c2ecf20Sopenharmony_cimv64xxx_i2c_do_action(struct mv64xxx_i2c_data *drv_data)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	switch(drv_data->action) {
3498c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_SEND_RESTART:
3508c2ecf20Sopenharmony_ci		/* We should only get here if we have further messages */
3518c2ecf20Sopenharmony_ci		BUG_ON(drv_data->num_msgs == 0);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		drv_data->msgs++;
3548c2ecf20Sopenharmony_ci		drv_data->num_msgs--;
3558c2ecf20Sopenharmony_ci		mv64xxx_i2c_send_start(drv_data);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		if (drv_data->errata_delay)
3588c2ecf20Sopenharmony_ci			udelay(5);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		/*
3618c2ecf20Sopenharmony_ci		 * We're never at the start of the message here, and by this
3628c2ecf20Sopenharmony_ci		 * time it's already too late to do any protocol mangling.
3638c2ecf20Sopenharmony_ci		 * Thankfully, do not advertise support for that feature.
3648c2ecf20Sopenharmony_ci		 */
3658c2ecf20Sopenharmony_ci		drv_data->send_stop = drv_data->num_msgs == 1;
3668c2ecf20Sopenharmony_ci		break;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_CONTINUE:
3698c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits,
3708c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
3718c2ecf20Sopenharmony_ci		break;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_SEND_ADDR_1:
3748c2ecf20Sopenharmony_ci		writel(drv_data->addr1,
3758c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.data);
3768c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits,
3778c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
3788c2ecf20Sopenharmony_ci		break;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_SEND_ADDR_2:
3818c2ecf20Sopenharmony_ci		writel(drv_data->addr2,
3828c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.data);
3838c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits,
3848c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
3858c2ecf20Sopenharmony_ci		break;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_SEND_DATA:
3888c2ecf20Sopenharmony_ci		writel(drv_data->msg->buf[drv_data->byte_posn++],
3898c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.data);
3908c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits,
3918c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
3928c2ecf20Sopenharmony_ci		break;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_RCV_DATA:
3958c2ecf20Sopenharmony_ci		drv_data->msg->buf[drv_data->byte_posn++] =
3968c2ecf20Sopenharmony_ci			readl(drv_data->reg_base + drv_data->reg_offsets.data);
3978c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits,
3988c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
3998c2ecf20Sopenharmony_ci		break;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_RCV_DATA_STOP:
4028c2ecf20Sopenharmony_ci		drv_data->msg->buf[drv_data->byte_posn++] =
4038c2ecf20Sopenharmony_ci			readl(drv_data->reg_base + drv_data->reg_offsets.data);
4048c2ecf20Sopenharmony_ci		drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
4058c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
4068c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
4078c2ecf20Sopenharmony_ci		drv_data->block = 0;
4088c2ecf20Sopenharmony_ci		if (drv_data->errata_delay)
4098c2ecf20Sopenharmony_ci			udelay(5);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci		wake_up(&drv_data->waitq);
4128c2ecf20Sopenharmony_ci		break;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_INVALID:
4158c2ecf20Sopenharmony_ci	default:
4168c2ecf20Sopenharmony_ci		dev_err(&drv_data->adapter.dev,
4178c2ecf20Sopenharmony_ci			"mv64xxx_i2c_do_action: Invalid action: %d\n",
4188c2ecf20Sopenharmony_ci			drv_data->action);
4198c2ecf20Sopenharmony_ci		drv_data->rc = -EIO;
4208c2ecf20Sopenharmony_ci		fallthrough;
4218c2ecf20Sopenharmony_ci	case MV64XXX_I2C_ACTION_SEND_STOP:
4228c2ecf20Sopenharmony_ci		drv_data->cntl_bits &= ~MV64XXX_I2C_REG_CONTROL_INTEN;
4238c2ecf20Sopenharmony_ci		writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_STOP,
4248c2ecf20Sopenharmony_ci			drv_data->reg_base + drv_data->reg_offsets.control);
4258c2ecf20Sopenharmony_ci		drv_data->block = 0;
4268c2ecf20Sopenharmony_ci		wake_up(&drv_data->waitq);
4278c2ecf20Sopenharmony_ci		break;
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic void
4328c2ecf20Sopenharmony_cimv64xxx_i2c_read_offload_rx_data(struct mv64xxx_i2c_data *drv_data,
4338c2ecf20Sopenharmony_ci				 struct i2c_msg *msg)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	u32 buf[2];
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	buf[0] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_LO);
4388c2ecf20Sopenharmony_ci	buf[1] = readl(drv_data->reg_base + MV64XXX_I2C_REG_RX_DATA_HI);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	memcpy(msg->buf, buf, msg->len);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic int
4448c2ecf20Sopenharmony_cimv64xxx_i2c_intr_offload(struct mv64xxx_i2c_data *drv_data)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	u32 cause, status;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	cause = readl(drv_data->reg_base +
4498c2ecf20Sopenharmony_ci		      MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
4508c2ecf20Sopenharmony_ci	if (!cause)
4518c2ecf20Sopenharmony_ci		return IRQ_NONE;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	status = readl(drv_data->reg_base +
4548c2ecf20Sopenharmony_ci		       MV64XXX_I2C_REG_BRIDGE_STATUS);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (status & MV64XXX_I2C_BRIDGE_STATUS_ERROR) {
4578c2ecf20Sopenharmony_ci		drv_data->rc = -EIO;
4588c2ecf20Sopenharmony_ci		goto out;
4598c2ecf20Sopenharmony_ci	}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	drv_data->rc = 0;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	/*
4648c2ecf20Sopenharmony_ci	 * Transaction is a one message read transaction, read data
4658c2ecf20Sopenharmony_ci	 * for this message.
4668c2ecf20Sopenharmony_ci	 */
4678c2ecf20Sopenharmony_ci	if (drv_data->num_msgs == 1 && drv_data->msgs[0].flags & I2C_M_RD) {
4688c2ecf20Sopenharmony_ci		mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs);
4698c2ecf20Sopenharmony_ci		drv_data->msgs++;
4708c2ecf20Sopenharmony_ci		drv_data->num_msgs--;
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci	/*
4738c2ecf20Sopenharmony_ci	 * Transaction is a two messages write/read transaction, read
4748c2ecf20Sopenharmony_ci	 * data for the second (read) message.
4758c2ecf20Sopenharmony_ci	 */
4768c2ecf20Sopenharmony_ci	else if (drv_data->num_msgs == 2 &&
4778c2ecf20Sopenharmony_ci		 !(drv_data->msgs[0].flags & I2C_M_RD) &&
4788c2ecf20Sopenharmony_ci		 drv_data->msgs[1].flags & I2C_M_RD) {
4798c2ecf20Sopenharmony_ci		mv64xxx_i2c_read_offload_rx_data(drv_data, drv_data->msgs + 1);
4808c2ecf20Sopenharmony_ci		drv_data->msgs += 2;
4818c2ecf20Sopenharmony_ci		drv_data->num_msgs -= 2;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ciout:
4858c2ecf20Sopenharmony_ci	writel(0, drv_data->reg_base +	MV64XXX_I2C_REG_BRIDGE_CONTROL);
4868c2ecf20Sopenharmony_ci	writel(0, drv_data->reg_base +
4878c2ecf20Sopenharmony_ci	       MV64XXX_I2C_REG_BRIDGE_INTR_CAUSE);
4888c2ecf20Sopenharmony_ci	drv_data->block = 0;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	wake_up(&drv_data->waitq);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic irqreturn_t
4968c2ecf20Sopenharmony_cimv64xxx_i2c_intr(int irq, void *dev_id)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_data	*drv_data = dev_id;
4998c2ecf20Sopenharmony_ci	u32		status;
5008c2ecf20Sopenharmony_ci	irqreturn_t	rc = IRQ_NONE;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	spin_lock(&drv_data->lock);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (drv_data->offload_enabled)
5058c2ecf20Sopenharmony_ci		rc = mv64xxx_i2c_intr_offload(drv_data);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	while (readl(drv_data->reg_base + drv_data->reg_offsets.control) &
5088c2ecf20Sopenharmony_ci						MV64XXX_I2C_REG_CONTROL_IFLG) {
5098c2ecf20Sopenharmony_ci		status = readl(drv_data->reg_base + drv_data->reg_offsets.status);
5108c2ecf20Sopenharmony_ci		mv64xxx_i2c_fsm(drv_data, status);
5118c2ecf20Sopenharmony_ci		mv64xxx_i2c_do_action(drv_data);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci		if (drv_data->irq_clear_inverted)
5148c2ecf20Sopenharmony_ci			writel(drv_data->cntl_bits | MV64XXX_I2C_REG_CONTROL_IFLG,
5158c2ecf20Sopenharmony_ci			       drv_data->reg_base + drv_data->reg_offsets.control);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		rc = IRQ_HANDLED;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci	spin_unlock(&drv_data->lock);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return rc;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci/*
5258c2ecf20Sopenharmony_ci *****************************************************************************
5268c2ecf20Sopenharmony_ci *
5278c2ecf20Sopenharmony_ci *	I2C Msg Execution Routines
5288c2ecf20Sopenharmony_ci *
5298c2ecf20Sopenharmony_ci *****************************************************************************
5308c2ecf20Sopenharmony_ci */
5318c2ecf20Sopenharmony_cistatic void
5328c2ecf20Sopenharmony_cimv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data *drv_data)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	long		time_left;
5358c2ecf20Sopenharmony_ci	unsigned long	flags;
5368c2ecf20Sopenharmony_ci	char		abort = 0;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	time_left = wait_event_timeout(drv_data->waitq,
5398c2ecf20Sopenharmony_ci		!drv_data->block, drv_data->adapter.timeout);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	spin_lock_irqsave(&drv_data->lock, flags);
5428c2ecf20Sopenharmony_ci	if (!time_left) { /* Timed out */
5438c2ecf20Sopenharmony_ci		drv_data->rc = -ETIMEDOUT;
5448c2ecf20Sopenharmony_ci		abort = 1;
5458c2ecf20Sopenharmony_ci	} else if (time_left < 0) { /* Interrupted/Error */
5468c2ecf20Sopenharmony_ci		drv_data->rc = time_left; /* errno value */
5478c2ecf20Sopenharmony_ci		abort = 1;
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	if (abort && drv_data->block) {
5518c2ecf20Sopenharmony_ci		drv_data->aborting = 1;
5528c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&drv_data->lock, flags);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		time_left = wait_event_timeout(drv_data->waitq,
5558c2ecf20Sopenharmony_ci			!drv_data->block, drv_data->adapter.timeout);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		if ((time_left <= 0) && drv_data->block) {
5588c2ecf20Sopenharmony_ci			drv_data->state = MV64XXX_I2C_STATE_IDLE;
5598c2ecf20Sopenharmony_ci			dev_err(&drv_data->adapter.dev,
5608c2ecf20Sopenharmony_ci				"mv64xxx: I2C bus locked, block: %d, "
5618c2ecf20Sopenharmony_ci				"time_left: %d\n", drv_data->block,
5628c2ecf20Sopenharmony_ci				(int)time_left);
5638c2ecf20Sopenharmony_ci			mv64xxx_i2c_hw_init(drv_data);
5648c2ecf20Sopenharmony_ci		}
5658c2ecf20Sopenharmony_ci	} else
5668c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&drv_data->lock, flags);
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic int
5708c2ecf20Sopenharmony_cimv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data *drv_data, struct i2c_msg *msg,
5718c2ecf20Sopenharmony_ci				int is_last)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	unsigned long	flags;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&drv_data->lock, flags);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	drv_data->state = MV64XXX_I2C_STATE_WAITING_FOR_START_COND;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	drv_data->send_stop = is_last;
5808c2ecf20Sopenharmony_ci	drv_data->block = 1;
5818c2ecf20Sopenharmony_ci	mv64xxx_i2c_send_start(drv_data);
5828c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&drv_data->lock, flags);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	mv64xxx_i2c_wait_for_completion(drv_data);
5858c2ecf20Sopenharmony_ci	return drv_data->rc;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_cistatic void
5898c2ecf20Sopenharmony_cimv64xxx_i2c_prepare_tx(struct mv64xxx_i2c_data *drv_data)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct i2c_msg *msg = drv_data->msgs;
5928c2ecf20Sopenharmony_ci	u32 buf[2];
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	memcpy(buf, msg->buf, msg->len);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	writel(buf[0], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_LO);
5978c2ecf20Sopenharmony_ci	writel(buf[1], drv_data->reg_base + MV64XXX_I2C_REG_TX_DATA_HI);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic int
6018c2ecf20Sopenharmony_cimv64xxx_i2c_offload_xfer(struct mv64xxx_i2c_data *drv_data)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct i2c_msg *msgs = drv_data->msgs;
6048c2ecf20Sopenharmony_ci	int num = drv_data->num_msgs;
6058c2ecf20Sopenharmony_ci	unsigned long ctrl_reg;
6068c2ecf20Sopenharmony_ci	unsigned long flags;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	spin_lock_irqsave(&drv_data->lock, flags);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* Build transaction */
6118c2ecf20Sopenharmony_ci	ctrl_reg = MV64XXX_I2C_BRIDGE_CONTROL_ENABLE |
6128c2ecf20Sopenharmony_ci		(msgs[0].addr << MV64XXX_I2C_BRIDGE_CONTROL_ADDR_SHIFT);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (msgs[0].flags & I2C_M_TEN)
6158c2ecf20Sopenharmony_ci		ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_ADDR_EXT;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Single write message transaction */
6188c2ecf20Sopenharmony_ci	if (num == 1 && !(msgs[0].flags & I2C_M_RD)) {
6198c2ecf20Sopenharmony_ci		size_t len = msgs[0].len - 1;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci		ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_WR |
6228c2ecf20Sopenharmony_ci			(len << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT);
6238c2ecf20Sopenharmony_ci		mv64xxx_i2c_prepare_tx(drv_data);
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci	/* Single read message transaction */
6268c2ecf20Sopenharmony_ci	else if (num == 1 && msgs[0].flags & I2C_M_RD) {
6278c2ecf20Sopenharmony_ci		size_t len = msgs[0].len - 1;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci		ctrl_reg |= MV64XXX_I2C_BRIDGE_CONTROL_RD |
6308c2ecf20Sopenharmony_ci			(len << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT);
6318c2ecf20Sopenharmony_ci	}
6328c2ecf20Sopenharmony_ci	/*
6338c2ecf20Sopenharmony_ci	 * Transaction with one write and one read message. This is
6348c2ecf20Sopenharmony_ci	 * guaranteed by the mv64xx_i2c_can_offload() checks.
6358c2ecf20Sopenharmony_ci	 */
6368c2ecf20Sopenharmony_ci	else if (num == 2) {
6378c2ecf20Sopenharmony_ci		size_t lentx = msgs[0].len - 1;
6388c2ecf20Sopenharmony_ci		size_t lenrx = msgs[1].len - 1;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci		ctrl_reg |=
6418c2ecf20Sopenharmony_ci			MV64XXX_I2C_BRIDGE_CONTROL_RD |
6428c2ecf20Sopenharmony_ci			MV64XXX_I2C_BRIDGE_CONTROL_WR |
6438c2ecf20Sopenharmony_ci			(lentx << MV64XXX_I2C_BRIDGE_CONTROL_TX_SIZE_SHIFT) |
6448c2ecf20Sopenharmony_ci			(lenrx << MV64XXX_I2C_BRIDGE_CONTROL_RX_SIZE_SHIFT) |
6458c2ecf20Sopenharmony_ci			MV64XXX_I2C_BRIDGE_CONTROL_REPEATED_START;
6468c2ecf20Sopenharmony_ci		mv64xxx_i2c_prepare_tx(drv_data);
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* Execute transaction */
6508c2ecf20Sopenharmony_ci	drv_data->block = 1;
6518c2ecf20Sopenharmony_ci	writel(ctrl_reg, drv_data->reg_base + MV64XXX_I2C_REG_BRIDGE_CONTROL);
6528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&drv_data->lock, flags);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	mv64xxx_i2c_wait_for_completion(drv_data);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	return drv_data->rc;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_cistatic bool
6608c2ecf20Sopenharmony_cimv64xxx_i2c_valid_offload_sz(struct i2c_msg *msg)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	return msg->len <= 8 && msg->len >= 1;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic bool
6668c2ecf20Sopenharmony_cimv64xxx_i2c_can_offload(struct mv64xxx_i2c_data *drv_data)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	struct i2c_msg *msgs = drv_data->msgs;
6698c2ecf20Sopenharmony_ci	int num = drv_data->num_msgs;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (!drv_data->offload_enabled)
6728c2ecf20Sopenharmony_ci		return false;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/*
6758c2ecf20Sopenharmony_ci	 * We can offload a transaction consisting of a single
6768c2ecf20Sopenharmony_ci	 * message, as long as the message has a length between 1 and
6778c2ecf20Sopenharmony_ci	 * 8 bytes.
6788c2ecf20Sopenharmony_ci	 */
6798c2ecf20Sopenharmony_ci	if (num == 1 && mv64xxx_i2c_valid_offload_sz(msgs))
6808c2ecf20Sopenharmony_ci		return true;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/*
6838c2ecf20Sopenharmony_ci	 * We can offload a transaction consisting of two messages, if
6848c2ecf20Sopenharmony_ci	 * the first is a write and a second is a read, and both have
6858c2ecf20Sopenharmony_ci	 * a length between 1 and 8 bytes.
6868c2ecf20Sopenharmony_ci	 */
6878c2ecf20Sopenharmony_ci	if (num == 2 &&
6888c2ecf20Sopenharmony_ci	    mv64xxx_i2c_valid_offload_sz(msgs) &&
6898c2ecf20Sopenharmony_ci	    mv64xxx_i2c_valid_offload_sz(msgs + 1) &&
6908c2ecf20Sopenharmony_ci	    !(msgs[0].flags & I2C_M_RD) &&
6918c2ecf20Sopenharmony_ci	    msgs[1].flags & I2C_M_RD)
6928c2ecf20Sopenharmony_ci		return true;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	return false;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci/*
6988c2ecf20Sopenharmony_ci *****************************************************************************
6998c2ecf20Sopenharmony_ci *
7008c2ecf20Sopenharmony_ci *	I2C Core Support Routines (Interface to higher level I2C code)
7018c2ecf20Sopenharmony_ci *
7028c2ecf20Sopenharmony_ci *****************************************************************************
7038c2ecf20Sopenharmony_ci */
7048c2ecf20Sopenharmony_cistatic u32
7058c2ecf20Sopenharmony_cimv64xxx_i2c_functionality(struct i2c_adapter *adap)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic int
7118c2ecf20Sopenharmony_cimv64xxx_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_data *drv_data = i2c_get_adapdata(adap);
7148c2ecf20Sopenharmony_ci	int rc, ret = num;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	BUG_ON(drv_data->msgs != NULL);
7178c2ecf20Sopenharmony_ci	drv_data->msgs = msgs;
7188c2ecf20Sopenharmony_ci	drv_data->num_msgs = num;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	if (mv64xxx_i2c_can_offload(drv_data))
7218c2ecf20Sopenharmony_ci		rc = mv64xxx_i2c_offload_xfer(drv_data);
7228c2ecf20Sopenharmony_ci	else
7238c2ecf20Sopenharmony_ci		rc = mv64xxx_i2c_execute_msg(drv_data, &msgs[0], num == 1);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	if (rc < 0)
7268c2ecf20Sopenharmony_ci		ret = rc;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	drv_data->num_msgs = 0;
7298c2ecf20Sopenharmony_ci	drv_data->msgs = NULL;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	return ret;
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_cistatic const struct i2c_algorithm mv64xxx_i2c_algo = {
7358c2ecf20Sopenharmony_ci	.master_xfer = mv64xxx_i2c_xfer,
7368c2ecf20Sopenharmony_ci	.functionality = mv64xxx_i2c_functionality,
7378c2ecf20Sopenharmony_ci};
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci/*
7408c2ecf20Sopenharmony_ci *****************************************************************************
7418c2ecf20Sopenharmony_ci *
7428c2ecf20Sopenharmony_ci *	Driver Interface & Early Init Routines
7438c2ecf20Sopenharmony_ci *
7448c2ecf20Sopenharmony_ci *****************************************************************************
7458c2ecf20Sopenharmony_ci */
7468c2ecf20Sopenharmony_cistatic const struct of_device_id mv64xxx_i2c_of_match_table[] = {
7478c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun4i-a10-i2c", .data = &mv64xxx_i2c_regs_sun4i},
7488c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun6i-a31-i2c", .data = &mv64xxx_i2c_regs_sun4i},
7498c2ecf20Sopenharmony_ci	{ .compatible = "marvell,mv64xxx-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
7508c2ecf20Sopenharmony_ci	{ .compatible = "marvell,mv78230-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
7518c2ecf20Sopenharmony_ci	{ .compatible = "marvell,mv78230-a0-i2c", .data = &mv64xxx_i2c_regs_mv64xxx},
7528c2ecf20Sopenharmony_ci	{}
7538c2ecf20Sopenharmony_ci};
7548c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mv64xxx_i2c_of_match_table);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
7578c2ecf20Sopenharmony_cistatic int
7588c2ecf20Sopenharmony_cimv64xxx_calc_freq(struct mv64xxx_i2c_data *drv_data,
7598c2ecf20Sopenharmony_ci		  const int tclk, const int n, const int m)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	if (drv_data->clk_n_base_0)
7628c2ecf20Sopenharmony_ci		return tclk / (10 * (m + 1) * (1 << n));
7638c2ecf20Sopenharmony_ci	else
7648c2ecf20Sopenharmony_ci		return tclk / (10 * (m + 1) * (2 << n));
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic bool
7688c2ecf20Sopenharmony_cimv64xxx_find_baud_factors(struct mv64xxx_i2c_data *drv_data,
7698c2ecf20Sopenharmony_ci			  const u32 req_freq, const u32 tclk)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	int freq, delta, best_delta = INT_MAX;
7728c2ecf20Sopenharmony_ci	int m, n;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	for (n = 0; n <= 7; n++)
7758c2ecf20Sopenharmony_ci		for (m = 0; m <= 15; m++) {
7768c2ecf20Sopenharmony_ci			freq = mv64xxx_calc_freq(drv_data, tclk, n, m);
7778c2ecf20Sopenharmony_ci			delta = req_freq - freq;
7788c2ecf20Sopenharmony_ci			if (delta >= 0 && delta < best_delta) {
7798c2ecf20Sopenharmony_ci				drv_data->freq_m = m;
7808c2ecf20Sopenharmony_ci				drv_data->freq_n = n;
7818c2ecf20Sopenharmony_ci				best_delta = delta;
7828c2ecf20Sopenharmony_ci			}
7838c2ecf20Sopenharmony_ci			if (best_delta == 0)
7848c2ecf20Sopenharmony_ci				return true;
7858c2ecf20Sopenharmony_ci		}
7868c2ecf20Sopenharmony_ci	if (best_delta == INT_MAX)
7878c2ecf20Sopenharmony_ci		return false;
7888c2ecf20Sopenharmony_ci	return true;
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_cistatic int
7928c2ecf20Sopenharmony_cimv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
7938c2ecf20Sopenharmony_ci		  struct device *dev)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	const struct of_device_id *device;
7968c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
7978c2ecf20Sopenharmony_ci	u32 bus_freq, tclk;
7988c2ecf20Sopenharmony_ci	int rc = 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	/* CLK is mandatory when using DT to describe the i2c bus. We
8018c2ecf20Sopenharmony_ci	 * need to know tclk in order to calculate bus clock
8028c2ecf20Sopenharmony_ci	 * factors.
8038c2ecf20Sopenharmony_ci	 */
8048c2ecf20Sopenharmony_ci	if (IS_ERR(drv_data->clk)) {
8058c2ecf20Sopenharmony_ci		rc = -ENODEV;
8068c2ecf20Sopenharmony_ci		goto out;
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci	tclk = clk_get_rate(drv_data->clk);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "clock-frequency", &bus_freq))
8118c2ecf20Sopenharmony_ci		bus_freq = I2C_MAX_STANDARD_MODE_FREQ; /* 100kHz by default */
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun4i-a10-i2c") ||
8148c2ecf20Sopenharmony_ci	    of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
8158c2ecf20Sopenharmony_ci		drv_data->clk_n_base_0 = true;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (!mv64xxx_find_baud_factors(drv_data, bus_freq, tclk)) {
8188c2ecf20Sopenharmony_ci		rc = -EINVAL;
8198c2ecf20Sopenharmony_ci		goto out;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	drv_data->rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
8238c2ecf20Sopenharmony_ci	if (IS_ERR(drv_data->rstc)) {
8248c2ecf20Sopenharmony_ci		rc = PTR_ERR(drv_data->rstc);
8258c2ecf20Sopenharmony_ci		goto out;
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci	reset_control_deassert(drv_data->rstc);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	/* Its not yet defined how timeouts will be specified in device tree.
8308c2ecf20Sopenharmony_ci	 * So hard code the value to 1 second.
8318c2ecf20Sopenharmony_ci	 */
8328c2ecf20Sopenharmony_ci	drv_data->adapter.timeout = HZ;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	device = of_match_device(mv64xxx_i2c_of_match_table, dev);
8358c2ecf20Sopenharmony_ci	if (!device)
8368c2ecf20Sopenharmony_ci		return -ENODEV;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	memcpy(&drv_data->reg_offsets, device->data, sizeof(drv_data->reg_offsets));
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	/*
8418c2ecf20Sopenharmony_ci	 * For controllers embedded in new SoCs activate the
8428c2ecf20Sopenharmony_ci	 * Transaction Generator support and the errata fix.
8438c2ecf20Sopenharmony_ci	 */
8448c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "marvell,mv78230-i2c")) {
8458c2ecf20Sopenharmony_ci		drv_data->offload_enabled = true;
8468c2ecf20Sopenharmony_ci		/* The delay is only needed in standard mode (100kHz) */
8478c2ecf20Sopenharmony_ci		if (bus_freq <= I2C_MAX_STANDARD_MODE_FREQ)
8488c2ecf20Sopenharmony_ci			drv_data->errata_delay = true;
8498c2ecf20Sopenharmony_ci	}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "marvell,mv78230-a0-i2c")) {
8528c2ecf20Sopenharmony_ci		drv_data->offload_enabled = false;
8538c2ecf20Sopenharmony_ci		/* The delay is only needed in standard mode (100kHz) */
8548c2ecf20Sopenharmony_ci		if (bus_freq <= I2C_MAX_STANDARD_MODE_FREQ)
8558c2ecf20Sopenharmony_ci			drv_data->errata_delay = true;
8568c2ecf20Sopenharmony_ci	}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun6i-a31-i2c"))
8598c2ecf20Sopenharmony_ci		drv_data->irq_clear_inverted = true;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ciout:
8628c2ecf20Sopenharmony_ci	return rc;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci#else /* CONFIG_OF */
8658c2ecf20Sopenharmony_cistatic int
8668c2ecf20Sopenharmony_cimv64xxx_of_config(struct mv64xxx_i2c_data *drv_data,
8678c2ecf20Sopenharmony_ci		  struct device *dev)
8688c2ecf20Sopenharmony_ci{
8698c2ecf20Sopenharmony_ci	return -ENODEV;
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci#endif /* CONFIG_OF */
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic int
8748c2ecf20Sopenharmony_cimv64xxx_i2c_probe(struct platform_device *pd)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_data		*drv_data;
8778c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_pdata	*pdata = dev_get_platdata(&pd->dev);
8788c2ecf20Sopenharmony_ci	int	rc;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	if ((!pdata && !pd->dev.of_node))
8818c2ecf20Sopenharmony_ci		return -ENODEV;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	drv_data = devm_kzalloc(&pd->dev, sizeof(struct mv64xxx_i2c_data),
8848c2ecf20Sopenharmony_ci				GFP_KERNEL);
8858c2ecf20Sopenharmony_ci	if (!drv_data)
8868c2ecf20Sopenharmony_ci		return -ENOMEM;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	drv_data->reg_base = devm_platform_ioremap_resource(pd, 0);
8898c2ecf20Sopenharmony_ci	if (IS_ERR(drv_data->reg_base))
8908c2ecf20Sopenharmony_ci		return PTR_ERR(drv_data->reg_base);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	strlcpy(drv_data->adapter.name, MV64XXX_I2C_CTLR_NAME " adapter",
8938c2ecf20Sopenharmony_ci		sizeof(drv_data->adapter.name));
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	init_waitqueue_head(&drv_data->waitq);
8968c2ecf20Sopenharmony_ci	spin_lock_init(&drv_data->lock);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	/* Not all platforms have clocks */
8998c2ecf20Sopenharmony_ci	drv_data->clk = devm_clk_get(&pd->dev, NULL);
9008c2ecf20Sopenharmony_ci	if (PTR_ERR(drv_data->clk) == -EPROBE_DEFER)
9018c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
9028c2ecf20Sopenharmony_ci	if (!IS_ERR(drv_data->clk))
9038c2ecf20Sopenharmony_ci		clk_prepare_enable(drv_data->clk);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	drv_data->reg_clk = devm_clk_get(&pd->dev, "reg");
9068c2ecf20Sopenharmony_ci	if (PTR_ERR(drv_data->reg_clk) == -EPROBE_DEFER)
9078c2ecf20Sopenharmony_ci		return -EPROBE_DEFER;
9088c2ecf20Sopenharmony_ci	if (!IS_ERR(drv_data->reg_clk))
9098c2ecf20Sopenharmony_ci		clk_prepare_enable(drv_data->reg_clk);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	drv_data->irq = platform_get_irq(pd, 0);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	if (pdata) {
9148c2ecf20Sopenharmony_ci		drv_data->freq_m = pdata->freq_m;
9158c2ecf20Sopenharmony_ci		drv_data->freq_n = pdata->freq_n;
9168c2ecf20Sopenharmony_ci		drv_data->adapter.timeout = msecs_to_jiffies(pdata->timeout);
9178c2ecf20Sopenharmony_ci		drv_data->offload_enabled = false;
9188c2ecf20Sopenharmony_ci		memcpy(&drv_data->reg_offsets, &mv64xxx_i2c_regs_mv64xxx, sizeof(drv_data->reg_offsets));
9198c2ecf20Sopenharmony_ci	} else if (pd->dev.of_node) {
9208c2ecf20Sopenharmony_ci		rc = mv64xxx_of_config(drv_data, &pd->dev);
9218c2ecf20Sopenharmony_ci		if (rc)
9228c2ecf20Sopenharmony_ci			goto exit_clk;
9238c2ecf20Sopenharmony_ci	}
9248c2ecf20Sopenharmony_ci	if (drv_data->irq < 0) {
9258c2ecf20Sopenharmony_ci		rc = drv_data->irq;
9268c2ecf20Sopenharmony_ci		goto exit_reset;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	drv_data->adapter.dev.parent = &pd->dev;
9308c2ecf20Sopenharmony_ci	drv_data->adapter.algo = &mv64xxx_i2c_algo;
9318c2ecf20Sopenharmony_ci	drv_data->adapter.owner = THIS_MODULE;
9328c2ecf20Sopenharmony_ci	drv_data->adapter.class = I2C_CLASS_DEPRECATED;
9338c2ecf20Sopenharmony_ci	drv_data->adapter.nr = pd->id;
9348c2ecf20Sopenharmony_ci	drv_data->adapter.dev.of_node = pd->dev.of_node;
9358c2ecf20Sopenharmony_ci	platform_set_drvdata(pd, drv_data);
9368c2ecf20Sopenharmony_ci	i2c_set_adapdata(&drv_data->adapter, drv_data);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	mv64xxx_i2c_hw_init(drv_data);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	rc = request_irq(drv_data->irq, mv64xxx_i2c_intr, 0,
9418c2ecf20Sopenharmony_ci			 MV64XXX_I2C_CTLR_NAME, drv_data);
9428c2ecf20Sopenharmony_ci	if (rc) {
9438c2ecf20Sopenharmony_ci		dev_err(&drv_data->adapter.dev,
9448c2ecf20Sopenharmony_ci			"mv64xxx: Can't register intr handler irq%d: %d\n",
9458c2ecf20Sopenharmony_ci			drv_data->irq, rc);
9468c2ecf20Sopenharmony_ci		goto exit_reset;
9478c2ecf20Sopenharmony_ci	} else if ((rc = i2c_add_numbered_adapter(&drv_data->adapter)) != 0) {
9488c2ecf20Sopenharmony_ci		dev_err(&drv_data->adapter.dev,
9498c2ecf20Sopenharmony_ci			"mv64xxx: Can't add i2c adapter, rc: %d\n", -rc);
9508c2ecf20Sopenharmony_ci		goto exit_free_irq;
9518c2ecf20Sopenharmony_ci	}
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	return 0;
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ciexit_free_irq:
9568c2ecf20Sopenharmony_ci	free_irq(drv_data->irq, drv_data);
9578c2ecf20Sopenharmony_ciexit_reset:
9588c2ecf20Sopenharmony_ci	reset_control_assert(drv_data->rstc);
9598c2ecf20Sopenharmony_ciexit_clk:
9608c2ecf20Sopenharmony_ci	clk_disable_unprepare(drv_data->reg_clk);
9618c2ecf20Sopenharmony_ci	clk_disable_unprepare(drv_data->clk);
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	return rc;
9648c2ecf20Sopenharmony_ci}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_cistatic int
9678c2ecf20Sopenharmony_cimv64xxx_i2c_remove(struct platform_device *dev)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_data		*drv_data = platform_get_drvdata(dev);
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	i2c_del_adapter(&drv_data->adapter);
9728c2ecf20Sopenharmony_ci	free_irq(drv_data->irq, drv_data);
9738c2ecf20Sopenharmony_ci	reset_control_assert(drv_data->rstc);
9748c2ecf20Sopenharmony_ci	clk_disable_unprepare(drv_data->reg_clk);
9758c2ecf20Sopenharmony_ci	clk_disable_unprepare(drv_data->clk);
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	return 0;
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
9818c2ecf20Sopenharmony_cistatic int mv64xxx_i2c_resume(struct device *dev)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct mv64xxx_i2c_data *drv_data = dev_get_drvdata(dev);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	mv64xxx_i2c_hw_init(drv_data);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	return 0;
9888c2ecf20Sopenharmony_ci}
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mv64xxx_i2c_pm = {
9918c2ecf20Sopenharmony_ci	.resume = mv64xxx_i2c_resume,
9928c2ecf20Sopenharmony_ci};
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci#define mv64xxx_i2c_pm_ops (&mv64xxx_i2c_pm)
9958c2ecf20Sopenharmony_ci#else
9968c2ecf20Sopenharmony_ci#define mv64xxx_i2c_pm_ops NULL
9978c2ecf20Sopenharmony_ci#endif
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_cistatic struct platform_driver mv64xxx_i2c_driver = {
10008c2ecf20Sopenharmony_ci	.probe	= mv64xxx_i2c_probe,
10018c2ecf20Sopenharmony_ci	.remove	= mv64xxx_i2c_remove,
10028c2ecf20Sopenharmony_ci	.driver	= {
10038c2ecf20Sopenharmony_ci		.name	= MV64XXX_I2C_CTLR_NAME,
10048c2ecf20Sopenharmony_ci		.pm     = mv64xxx_i2c_pm_ops,
10058c2ecf20Sopenharmony_ci		.of_match_table = mv64xxx_i2c_of_match_table,
10068c2ecf20Sopenharmony_ci	},
10078c2ecf20Sopenharmony_ci};
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_cimodule_platform_driver(mv64xxx_i2c_driver);
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ciMODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
10128c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
10138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1014