xref: /openbmc/linux/drivers/i2c/busses/i2c-mlxcpld.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1f069291bSVadim Pasternak // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
26bec23bfSVadim Pasternak /*
3f069291bSVadim Pasternak  * Mellanox i2c driver
46bec23bfSVadim Pasternak  *
5f069291bSVadim Pasternak  * Copyright (C) 2016-2020 Mellanox Technologies
66bec23bfSVadim Pasternak  */
76bec23bfSVadim Pasternak 
86bec23bfSVadim Pasternak #include <linux/delay.h>
96bec23bfSVadim Pasternak #include <linux/i2c.h>
106bec23bfSVadim Pasternak #include <linux/init.h>
116bec23bfSVadim Pasternak #include <linux/io.h>
126bec23bfSVadim Pasternak #include <linux/kernel.h>
136bec23bfSVadim Pasternak #include <linux/module.h>
1466b0c284SVadim Pasternak #include <linux/platform_data/mlxreg.h>
156bec23bfSVadim Pasternak #include <linux/platform_device.h>
1666b0c284SVadim Pasternak #include <linux/regmap.h>
176bec23bfSVadim Pasternak 
186bec23bfSVadim Pasternak /* General defines */
196bec23bfSVadim Pasternak #define MLXPLAT_CPLD_LPC_I2C_BASE_ADDR	0x2000
206bec23bfSVadim Pasternak #define MLXCPLD_I2C_DEVICE_NAME		"i2c_mlxcpld"
216bec23bfSVadim Pasternak #define MLXCPLD_I2C_VALID_FLAG		(I2C_M_RECV_LEN | I2C_M_RD)
226bec23bfSVadim Pasternak #define MLXCPLD_I2C_BUS_NUM		1
236bec23bfSVadim Pasternak #define MLXCPLD_I2C_DATA_REG_SZ		36
24313ce648SMichael Shych #define MLXCPLD_I2C_DATA_SZ_BIT		BIT(5)
25*2ed4fa9cSVadim Pasternak #define MLXCPLD_I2C_DATA_EXT2_SZ_BIT	BIT(6)
26313ce648SMichael Shych #define MLXCPLD_I2C_DATA_SZ_MASK	GENMASK(6, 5)
27c9bfdc7cSMichael Shych #define MLXCPLD_I2C_SMBUS_BLK_BIT	BIT(7)
286bec23bfSVadim Pasternak #define MLXCPLD_I2C_MAX_ADDR_LEN	4
296bec23bfSVadim Pasternak #define MLXCPLD_I2C_RETR_NUM		2
306bec23bfSVadim Pasternak #define MLXCPLD_I2C_XFER_TO		500000 /* usec */
31669b2e4aSVadim Pasternak #define MLXCPLD_I2C_POLL_TIME		200   /* usec */
326bec23bfSVadim Pasternak 
336bec23bfSVadim Pasternak /* LPC I2C registers */
34313ce648SMichael Shych #define MLXCPLD_LPCI2C_CPBLTY_REG	0x0
356bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_CTRL_REG		0x1
366bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_HALF_CYC_REG	0x4
376bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_I2C_HOLD_REG	0x5
386bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_CMD_REG		0x6
396bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_NUM_DAT_REG	0x7
406bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_NUM_ADDR_REG	0x8
416bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_STATUS_REG	0x9
426bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_DATA_REG		0xa
436bec23bfSVadim Pasternak 
44fdf23c62SJiangshan Yi /* LPC I2C masks and parameters */
456bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_RST_SEL_MASK	0x1
466bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_TRANS_END	0x1
476bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_STATUS_NACK	0x10
486bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_NO_IND		0
496bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_ACK_IND		1
506bec23bfSVadim Pasternak #define MLXCPLD_LPCI2C_NACK_IND		2
516bec23bfSVadim Pasternak 
5266b0c284SVadim Pasternak #define MLXCPLD_I2C_FREQ_1000KHZ_SET	0x04
53e1f77eccSVadim Pasternak #define MLXCPLD_I2C_FREQ_400KHZ_SET	0x0e
5466b0c284SVadim Pasternak #define MLXCPLD_I2C_FREQ_100KHZ_SET	0x42
5566b0c284SVadim Pasternak 
5666b0c284SVadim Pasternak enum mlxcpld_i2c_frequency {
5766b0c284SVadim Pasternak 	MLXCPLD_I2C_FREQ_1000KHZ = 1,
5866b0c284SVadim Pasternak 	MLXCPLD_I2C_FREQ_400KHZ = 2,
5966b0c284SVadim Pasternak 	MLXCPLD_I2C_FREQ_100KHZ = 3,
6066b0c284SVadim Pasternak };
6166b0c284SVadim Pasternak 
626bec23bfSVadim Pasternak struct  mlxcpld_i2c_curr_xfer {
636bec23bfSVadim Pasternak 	u8 cmd;
646bec23bfSVadim Pasternak 	u8 addr_width;
656bec23bfSVadim Pasternak 	u8 data_len;
666bec23bfSVadim Pasternak 	u8 msg_num;
676bec23bfSVadim Pasternak 	struct i2c_msg *msg;
686bec23bfSVadim Pasternak };
696bec23bfSVadim Pasternak 
706bec23bfSVadim Pasternak struct mlxcpld_i2c_priv {
716bec23bfSVadim Pasternak 	struct i2c_adapter adap;
726bec23bfSVadim Pasternak 	u32 base_addr;
736bec23bfSVadim Pasternak 	struct mutex lock;
746bec23bfSVadim Pasternak 	struct  mlxcpld_i2c_curr_xfer xfer;
756bec23bfSVadim Pasternak 	struct device *dev;
76c9bfdc7cSMichael Shych 	bool smbus_block;
77712d6617SVadim Pasternak 	int polling_time;
786bec23bfSVadim Pasternak };
796bec23bfSVadim Pasternak 
mlxcpld_i2c_lpc_write_buf(u8 * data,u8 len,u32 addr)806bec23bfSVadim Pasternak static void mlxcpld_i2c_lpc_write_buf(u8 *data, u8 len, u32 addr)
816bec23bfSVadim Pasternak {
826bec23bfSVadim Pasternak 	int i;
836bec23bfSVadim Pasternak 
846bec23bfSVadim Pasternak 	for (i = 0; i < len - len % 4; i += 4)
856bec23bfSVadim Pasternak 		outl(*(u32 *)(data + i), addr + i);
866bec23bfSVadim Pasternak 	for (; i < len; ++i)
876bec23bfSVadim Pasternak 		outb(*(data + i), addr + i);
886bec23bfSVadim Pasternak }
896bec23bfSVadim Pasternak 
mlxcpld_i2c_lpc_read_buf(u8 * data,u8 len,u32 addr)906bec23bfSVadim Pasternak static void mlxcpld_i2c_lpc_read_buf(u8 *data, u8 len, u32 addr)
916bec23bfSVadim Pasternak {
926bec23bfSVadim Pasternak 	int i;
936bec23bfSVadim Pasternak 
946bec23bfSVadim Pasternak 	for (i = 0; i < len - len % 4; i += 4)
956bec23bfSVadim Pasternak 		*(u32 *)(data + i) = inl(addr + i);
966bec23bfSVadim Pasternak 	for (; i < len; ++i)
976bec23bfSVadim Pasternak 		*(data + i) = inb(addr + i);
986bec23bfSVadim Pasternak }
996bec23bfSVadim Pasternak 
mlxcpld_i2c_read_comm(struct mlxcpld_i2c_priv * priv,u8 offs,u8 * data,u8 datalen)1006bec23bfSVadim Pasternak static void mlxcpld_i2c_read_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
1016bec23bfSVadim Pasternak 				  u8 *data, u8 datalen)
1026bec23bfSVadim Pasternak {
1036bec23bfSVadim Pasternak 	u32 addr = priv->base_addr + offs;
1046bec23bfSVadim Pasternak 
1056bec23bfSVadim Pasternak 	switch (datalen) {
1066bec23bfSVadim Pasternak 	case 1:
1076bec23bfSVadim Pasternak 		*(data) = inb(addr);
1086bec23bfSVadim Pasternak 		break;
1096bec23bfSVadim Pasternak 	case 2:
1106bec23bfSVadim Pasternak 		*((u16 *)data) = inw(addr);
1116bec23bfSVadim Pasternak 		break;
1126bec23bfSVadim Pasternak 	case 3:
1136bec23bfSVadim Pasternak 		*((u16 *)data) = inw(addr);
1146bec23bfSVadim Pasternak 		*(data + 2) = inb(addr + 2);
1156bec23bfSVadim Pasternak 		break;
1166bec23bfSVadim Pasternak 	case 4:
1176bec23bfSVadim Pasternak 		*((u32 *)data) = inl(addr);
1186bec23bfSVadim Pasternak 		break;
1196bec23bfSVadim Pasternak 	default:
1206bec23bfSVadim Pasternak 		mlxcpld_i2c_lpc_read_buf(data, datalen, addr);
1216bec23bfSVadim Pasternak 		break;
1226bec23bfSVadim Pasternak 	}
1236bec23bfSVadim Pasternak }
1246bec23bfSVadim Pasternak 
mlxcpld_i2c_write_comm(struct mlxcpld_i2c_priv * priv,u8 offs,u8 * data,u8 datalen)1256bec23bfSVadim Pasternak static void mlxcpld_i2c_write_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
1266bec23bfSVadim Pasternak 				   u8 *data, u8 datalen)
1276bec23bfSVadim Pasternak {
1286bec23bfSVadim Pasternak 	u32 addr = priv->base_addr + offs;
1296bec23bfSVadim Pasternak 
1306bec23bfSVadim Pasternak 	switch (datalen) {
1316bec23bfSVadim Pasternak 	case 1:
1326bec23bfSVadim Pasternak 		outb(*(data), addr);
1336bec23bfSVadim Pasternak 		break;
1346bec23bfSVadim Pasternak 	case 2:
1356bec23bfSVadim Pasternak 		outw(*((u16 *)data), addr);
1366bec23bfSVadim Pasternak 		break;
1376bec23bfSVadim Pasternak 	case 3:
1386bec23bfSVadim Pasternak 		outw(*((u16 *)data), addr);
1396bec23bfSVadim Pasternak 		outb(*(data + 2), addr + 2);
1406bec23bfSVadim Pasternak 		break;
1416bec23bfSVadim Pasternak 	case 4:
1426bec23bfSVadim Pasternak 		outl(*((u32 *)data), addr);
1436bec23bfSVadim Pasternak 		break;
1446bec23bfSVadim Pasternak 	default:
1456bec23bfSVadim Pasternak 		mlxcpld_i2c_lpc_write_buf(data, datalen, addr);
1466bec23bfSVadim Pasternak 		break;
1476bec23bfSVadim Pasternak 	}
1486bec23bfSVadim Pasternak }
1496bec23bfSVadim Pasternak 
1506bec23bfSVadim Pasternak /*
1516bec23bfSVadim Pasternak  * Check validity of received i2c messages parameters.
1526bec23bfSVadim Pasternak  * Returns 0 if OK, other - in case of invalid parameters.
1536bec23bfSVadim Pasternak  */
mlxcpld_i2c_check_msg_params(struct mlxcpld_i2c_priv * priv,struct i2c_msg * msgs,int num)1546bec23bfSVadim Pasternak static int mlxcpld_i2c_check_msg_params(struct mlxcpld_i2c_priv *priv,
1556bec23bfSVadim Pasternak 					struct i2c_msg *msgs, int num)
1566bec23bfSVadim Pasternak {
1576bec23bfSVadim Pasternak 	int i;
1586bec23bfSVadim Pasternak 
1596bec23bfSVadim Pasternak 	if (!num) {
1606bec23bfSVadim Pasternak 		dev_err(priv->dev, "Incorrect 0 num of messages\n");
1616bec23bfSVadim Pasternak 		return -EINVAL;
1626bec23bfSVadim Pasternak 	}
1636bec23bfSVadim Pasternak 
1646bec23bfSVadim Pasternak 	if (unlikely(msgs[0].addr > 0x7f)) {
1656bec23bfSVadim Pasternak 		dev_err(priv->dev, "Invalid address 0x%03x\n",
1666bec23bfSVadim Pasternak 			msgs[0].addr);
1676bec23bfSVadim Pasternak 		return -EINVAL;
1686bec23bfSVadim Pasternak 	}
1696bec23bfSVadim Pasternak 
1706bec23bfSVadim Pasternak 	for (i = 0; i < num; ++i) {
1716bec23bfSVadim Pasternak 		if (unlikely(!msgs[i].buf)) {
1726bec23bfSVadim Pasternak 			dev_err(priv->dev, "Invalid buf in msg[%d]\n",
1736bec23bfSVadim Pasternak 				i);
1746bec23bfSVadim Pasternak 			return -EINVAL;
1756bec23bfSVadim Pasternak 		}
1766bec23bfSVadim Pasternak 		if (unlikely(msgs[0].addr != msgs[i].addr)) {
1776bec23bfSVadim Pasternak 			dev_err(priv->dev, "Invalid addr in msg[%d]\n",
1786bec23bfSVadim Pasternak 				i);
1796bec23bfSVadim Pasternak 			return -EINVAL;
1806bec23bfSVadim Pasternak 		}
1816bec23bfSVadim Pasternak 	}
1826bec23bfSVadim Pasternak 
1836bec23bfSVadim Pasternak 	return 0;
1846bec23bfSVadim Pasternak }
1856bec23bfSVadim Pasternak 
1866bec23bfSVadim Pasternak /*
1876bec23bfSVadim Pasternak  * Check if transfer is completed and status of operation.
1886bec23bfSVadim Pasternak  * Returns 0 - transfer completed (both ACK or NACK),
1896bec23bfSVadim Pasternak  * negative - transfer isn't finished.
1906bec23bfSVadim Pasternak  */
mlxcpld_i2c_check_status(struct mlxcpld_i2c_priv * priv,int * status)1916bec23bfSVadim Pasternak static int mlxcpld_i2c_check_status(struct mlxcpld_i2c_priv *priv, int *status)
1926bec23bfSVadim Pasternak {
1936bec23bfSVadim Pasternak 	u8 val;
1946bec23bfSVadim Pasternak 
1956bec23bfSVadim Pasternak 	mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
1966bec23bfSVadim Pasternak 
1976bec23bfSVadim Pasternak 	if (val & MLXCPLD_LPCI2C_TRANS_END) {
1986bec23bfSVadim Pasternak 		if (val & MLXCPLD_LPCI2C_STATUS_NACK)
1996bec23bfSVadim Pasternak 			/*
2006bec23bfSVadim Pasternak 			 * The slave is unable to accept the data. No such
2016bec23bfSVadim Pasternak 			 * slave, command not understood, or unable to accept
2026bec23bfSVadim Pasternak 			 * any more data.
2036bec23bfSVadim Pasternak 			 */
2046bec23bfSVadim Pasternak 			*status = MLXCPLD_LPCI2C_NACK_IND;
2056bec23bfSVadim Pasternak 		else
2066bec23bfSVadim Pasternak 			*status = MLXCPLD_LPCI2C_ACK_IND;
2076bec23bfSVadim Pasternak 		return 0;
2086bec23bfSVadim Pasternak 	}
2096bec23bfSVadim Pasternak 	*status = MLXCPLD_LPCI2C_NO_IND;
2106bec23bfSVadim Pasternak 
2116bec23bfSVadim Pasternak 	return -EIO;
2126bec23bfSVadim Pasternak }
2136bec23bfSVadim Pasternak 
mlxcpld_i2c_set_transf_data(struct mlxcpld_i2c_priv * priv,struct i2c_msg * msgs,int num,u8 comm_len)2146bec23bfSVadim Pasternak static void mlxcpld_i2c_set_transf_data(struct mlxcpld_i2c_priv *priv,
2156bec23bfSVadim Pasternak 					struct i2c_msg *msgs, int num,
2166bec23bfSVadim Pasternak 					u8 comm_len)
2176bec23bfSVadim Pasternak {
2186bec23bfSVadim Pasternak 	priv->xfer.msg = msgs;
2196bec23bfSVadim Pasternak 	priv->xfer.msg_num = num;
2206bec23bfSVadim Pasternak 
2216bec23bfSVadim Pasternak 	/*
2226bec23bfSVadim Pasternak 	 * All upper layers currently are never use transfer with more than
2236bec23bfSVadim Pasternak 	 * 2 messages. Actually, it's also not so relevant in Mellanox systems
2246bec23bfSVadim Pasternak 	 * because of HW limitation. Max size of transfer is not more than 32
225313ce648SMichael Shych 	 * or 68 bytes in the current x86 LPCI2C bridge.
2266bec23bfSVadim Pasternak 	 */
2276bec23bfSVadim Pasternak 	priv->xfer.cmd = msgs[num - 1].flags & I2C_M_RD;
2286bec23bfSVadim Pasternak 
2296bec23bfSVadim Pasternak 	if (priv->xfer.cmd == I2C_M_RD && comm_len != msgs[0].len) {
2306bec23bfSVadim Pasternak 		priv->xfer.addr_width = msgs[0].len;
2316bec23bfSVadim Pasternak 		priv->xfer.data_len = comm_len - priv->xfer.addr_width;
2326bec23bfSVadim Pasternak 	} else {
2336bec23bfSVadim Pasternak 		priv->xfer.addr_width = 0;
2346bec23bfSVadim Pasternak 		priv->xfer.data_len = comm_len;
2356bec23bfSVadim Pasternak 	}
2366bec23bfSVadim Pasternak }
2376bec23bfSVadim Pasternak 
2386bec23bfSVadim Pasternak /* Reset CPLD LPCI2C block */
mlxcpld_i2c_reset(struct mlxcpld_i2c_priv * priv)2396bec23bfSVadim Pasternak static void mlxcpld_i2c_reset(struct mlxcpld_i2c_priv *priv)
2406bec23bfSVadim Pasternak {
2416bec23bfSVadim Pasternak 	u8 val;
2426bec23bfSVadim Pasternak 
2436bec23bfSVadim Pasternak 	mutex_lock(&priv->lock);
2446bec23bfSVadim Pasternak 
2456bec23bfSVadim Pasternak 	mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
2466bec23bfSVadim Pasternak 	val &= ~MLXCPLD_LPCI2C_RST_SEL_MASK;
2476bec23bfSVadim Pasternak 	mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
2486bec23bfSVadim Pasternak 
2496bec23bfSVadim Pasternak 	mutex_unlock(&priv->lock);
2506bec23bfSVadim Pasternak }
2516bec23bfSVadim Pasternak 
2526bec23bfSVadim Pasternak /* Make sure the CPLD is ready to start transmitting. */
mlxcpld_i2c_check_busy(struct mlxcpld_i2c_priv * priv)2536bec23bfSVadim Pasternak static int mlxcpld_i2c_check_busy(struct mlxcpld_i2c_priv *priv)
2546bec23bfSVadim Pasternak {
2556bec23bfSVadim Pasternak 	u8 val;
2566bec23bfSVadim Pasternak 
2576bec23bfSVadim Pasternak 	mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
2586bec23bfSVadim Pasternak 
2596bec23bfSVadim Pasternak 	if (val & MLXCPLD_LPCI2C_TRANS_END)
2606bec23bfSVadim Pasternak 		return 0;
2616bec23bfSVadim Pasternak 
2626bec23bfSVadim Pasternak 	return -EIO;
2636bec23bfSVadim Pasternak }
2646bec23bfSVadim Pasternak 
mlxcpld_i2c_wait_for_free(struct mlxcpld_i2c_priv * priv)2656bec23bfSVadim Pasternak static int mlxcpld_i2c_wait_for_free(struct mlxcpld_i2c_priv *priv)
2666bec23bfSVadim Pasternak {
2676bec23bfSVadim Pasternak 	int timeout = 0;
2686bec23bfSVadim Pasternak 
2696bec23bfSVadim Pasternak 	do {
2706bec23bfSVadim Pasternak 		if (!mlxcpld_i2c_check_busy(priv))
2716bec23bfSVadim Pasternak 			break;
272712d6617SVadim Pasternak 		usleep_range(priv->polling_time / 2, priv->polling_time);
273712d6617SVadim Pasternak 		timeout += priv->polling_time;
2746bec23bfSVadim Pasternak 	} while (timeout <= MLXCPLD_I2C_XFER_TO);
2756bec23bfSVadim Pasternak 
2766bec23bfSVadim Pasternak 	if (timeout > MLXCPLD_I2C_XFER_TO)
2776bec23bfSVadim Pasternak 		return -ETIMEDOUT;
2786bec23bfSVadim Pasternak 
2796bec23bfSVadim Pasternak 	return 0;
2806bec23bfSVadim Pasternak }
2816bec23bfSVadim Pasternak 
2826bec23bfSVadim Pasternak /*
2836bec23bfSVadim Pasternak  * Wait for master transfer to complete.
2846bec23bfSVadim Pasternak  * It puts current process to sleep until we get interrupt or timeout expires.
2856bec23bfSVadim Pasternak  * Returns the number of transferred or read bytes or error (<0).
2866bec23bfSVadim Pasternak  */
mlxcpld_i2c_wait_for_tc(struct mlxcpld_i2c_priv * priv)2876bec23bfSVadim Pasternak static int mlxcpld_i2c_wait_for_tc(struct mlxcpld_i2c_priv *priv)
2886bec23bfSVadim Pasternak {
2896bec23bfSVadim Pasternak 	int status, i, timeout = 0;
290c9bfdc7cSMichael Shych 	u8 datalen, val;
2916bec23bfSVadim Pasternak 
2926bec23bfSVadim Pasternak 	do {
293712d6617SVadim Pasternak 		usleep_range(priv->polling_time / 2, priv->polling_time);
2946bec23bfSVadim Pasternak 		if (!mlxcpld_i2c_check_status(priv, &status))
2956bec23bfSVadim Pasternak 			break;
296712d6617SVadim Pasternak 		timeout += priv->polling_time;
2976bec23bfSVadim Pasternak 	} while (status == 0 && timeout < MLXCPLD_I2C_XFER_TO);
2986bec23bfSVadim Pasternak 
2996bec23bfSVadim Pasternak 	switch (status) {
3006bec23bfSVadim Pasternak 	case MLXCPLD_LPCI2C_NO_IND:
3016bec23bfSVadim Pasternak 		return -ETIMEDOUT;
3026bec23bfSVadim Pasternak 
3036bec23bfSVadim Pasternak 	case MLXCPLD_LPCI2C_ACK_IND:
3046bec23bfSVadim Pasternak 		if (priv->xfer.cmd != I2C_M_RD)
3056bec23bfSVadim Pasternak 			return (priv->xfer.addr_width + priv->xfer.data_len);
3066bec23bfSVadim Pasternak 
3076bec23bfSVadim Pasternak 		if (priv->xfer.msg_num == 1)
3086bec23bfSVadim Pasternak 			i = 0;
3096bec23bfSVadim Pasternak 		else
3106bec23bfSVadim Pasternak 			i = 1;
3116bec23bfSVadim Pasternak 
3126bec23bfSVadim Pasternak 		if (!priv->xfer.msg[i].buf)
3136bec23bfSVadim Pasternak 			return -EINVAL;
3146bec23bfSVadim Pasternak 
3156bec23bfSVadim Pasternak 		/*
3166bec23bfSVadim Pasternak 		 * Actual read data len will be always the same as
3176bec23bfSVadim Pasternak 		 * requested len. 0xff (line pull-up) will be returned
3186bec23bfSVadim Pasternak 		 * if slave has no data to return. Thus don't read
319c9bfdc7cSMichael Shych 		 * MLXCPLD_LPCI2C_NUM_DAT_REG reg from CPLD.  Only in case of
320c9bfdc7cSMichael Shych 		 * SMBus block read transaction data len can be different,
321c9bfdc7cSMichael Shych 		 * check this case.
3226bec23bfSVadim Pasternak 		 */
323c9bfdc7cSMichael Shych 		mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG, &val,
324c9bfdc7cSMichael Shych 				      1);
325c9bfdc7cSMichael Shych 		if (priv->smbus_block && (val & MLXCPLD_I2C_SMBUS_BLK_BIT)) {
326c9bfdc7cSMichael Shych 			mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG,
327c9bfdc7cSMichael Shych 					      &datalen, 1);
32859791128SWolfram Sang 			if (unlikely(datalen > I2C_SMBUS_BLOCK_MAX)) {
329c9bfdc7cSMichael Shych 				dev_err(priv->dev, "Incorrect smbus block read message len\n");
33059791128SWolfram Sang 				return -EPROTO;
331c9bfdc7cSMichael Shych 			}
332c9bfdc7cSMichael Shych 		} else {
3336bec23bfSVadim Pasternak 			datalen = priv->xfer.data_len;
334c9bfdc7cSMichael Shych 		}
3356bec23bfSVadim Pasternak 
3366bec23bfSVadim Pasternak 		mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_DATA_REG,
3376bec23bfSVadim Pasternak 				      priv->xfer.msg[i].buf, datalen);
3386bec23bfSVadim Pasternak 
3396bec23bfSVadim Pasternak 		return datalen;
3406bec23bfSVadim Pasternak 
3416bec23bfSVadim Pasternak 	case MLXCPLD_LPCI2C_NACK_IND:
3426bec23bfSVadim Pasternak 		return -ENXIO;
3436bec23bfSVadim Pasternak 
3446bec23bfSVadim Pasternak 	default:
3456bec23bfSVadim Pasternak 		return -EINVAL;
3466bec23bfSVadim Pasternak 	}
3476bec23bfSVadim Pasternak }
3486bec23bfSVadim Pasternak 
mlxcpld_i2c_xfer_msg(struct mlxcpld_i2c_priv * priv)3496bec23bfSVadim Pasternak static void mlxcpld_i2c_xfer_msg(struct mlxcpld_i2c_priv *priv)
3506bec23bfSVadim Pasternak {
3516bec23bfSVadim Pasternak 	int i, len = 0;
352c9bfdc7cSMichael Shych 	u8 cmd, val;
3536bec23bfSVadim Pasternak 
3546bec23bfSVadim Pasternak 	mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG,
3556bec23bfSVadim Pasternak 			       &priv->xfer.data_len, 1);
356c9bfdc7cSMichael Shych 
357c9bfdc7cSMichael Shych 	val = priv->xfer.addr_width;
358c9bfdc7cSMichael Shych 	/* Notify HW about SMBus block read transaction */
359c9bfdc7cSMichael Shych 	if (priv->smbus_block && priv->xfer.msg_num >= 2 &&
360c9bfdc7cSMichael Shych 	    priv->xfer.msg[1].len == 1 &&
361c9bfdc7cSMichael Shych 	    (priv->xfer.msg[1].flags & I2C_M_RECV_LEN) &&
362c9bfdc7cSMichael Shych 	    (priv->xfer.msg[1].flags & I2C_M_RD))
363c9bfdc7cSMichael Shych 		val |= MLXCPLD_I2C_SMBUS_BLK_BIT;
364c9bfdc7cSMichael Shych 
365c9bfdc7cSMichael Shych 	mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG, &val, 1);
3666bec23bfSVadim Pasternak 
3676bec23bfSVadim Pasternak 	for (i = 0; i < priv->xfer.msg_num; i++) {
3686bec23bfSVadim Pasternak 		if ((priv->xfer.msg[i].flags & I2C_M_RD) != I2C_M_RD) {
3696bec23bfSVadim Pasternak 			/* Don't write to CPLD buffer in read transaction */
3706bec23bfSVadim Pasternak 			mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_DATA_REG +
3716bec23bfSVadim Pasternak 					       len, priv->xfer.msg[i].buf,
3726bec23bfSVadim Pasternak 					       priv->xfer.msg[i].len);
3736bec23bfSVadim Pasternak 			len += priv->xfer.msg[i].len;
3746bec23bfSVadim Pasternak 		}
3756bec23bfSVadim Pasternak 	}
3766bec23bfSVadim Pasternak 
3776bec23bfSVadim Pasternak 	/*
3786bec23bfSVadim Pasternak 	 * Set target slave address with command for master transfer.
3796bec23bfSVadim Pasternak 	 * It should be latest executed function before CPLD transaction.
3806bec23bfSVadim Pasternak 	 */
3816bec23bfSVadim Pasternak 	cmd = (priv->xfer.msg[0].addr << 1) | priv->xfer.cmd;
3826bec23bfSVadim Pasternak 	mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CMD_REG, &cmd, 1);
3836bec23bfSVadim Pasternak }
3846bec23bfSVadim Pasternak 
3856bec23bfSVadim Pasternak /*
3866bec23bfSVadim Pasternak  * Generic lpc-i2c transfer.
3876bec23bfSVadim Pasternak  * Returns the number of processed messages or error (<0).
3886bec23bfSVadim Pasternak  */
mlxcpld_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)3896bec23bfSVadim Pasternak static int mlxcpld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
3906bec23bfSVadim Pasternak 			    int num)
3916bec23bfSVadim Pasternak {
3926bec23bfSVadim Pasternak 	struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap);
3936bec23bfSVadim Pasternak 	u8 comm_len = 0;
3946bec23bfSVadim Pasternak 	int i, err;
3956bec23bfSVadim Pasternak 
3966bec23bfSVadim Pasternak 	err = mlxcpld_i2c_check_msg_params(priv, msgs, num);
3976bec23bfSVadim Pasternak 	if (err) {
3986bec23bfSVadim Pasternak 		dev_err(priv->dev, "Incorrect message\n");
3996bec23bfSVadim Pasternak 		return err;
4006bec23bfSVadim Pasternak 	}
4016bec23bfSVadim Pasternak 
4026bec23bfSVadim Pasternak 	for (i = 0; i < num; ++i)
4036bec23bfSVadim Pasternak 		comm_len += msgs[i].len;
4046bec23bfSVadim Pasternak 
4056bec23bfSVadim Pasternak 	/* Check bus state */
4066bec23bfSVadim Pasternak 	if (mlxcpld_i2c_wait_for_free(priv)) {
4076bec23bfSVadim Pasternak 		dev_err(priv->dev, "LPCI2C bridge is busy\n");
4086bec23bfSVadim Pasternak 
4096bec23bfSVadim Pasternak 		/*
4106bec23bfSVadim Pasternak 		 * Usually it means something serious has happened.
4116bec23bfSVadim Pasternak 		 * We can not have unfinished previous transfer
4126bec23bfSVadim Pasternak 		 * so it doesn't make any sense to try to stop it.
4136bec23bfSVadim Pasternak 		 * Probably we were not able to recover from the
4146bec23bfSVadim Pasternak 		 * previous error.
4156bec23bfSVadim Pasternak 		 * The only reasonable thing - is soft reset.
4166bec23bfSVadim Pasternak 		 */
4176bec23bfSVadim Pasternak 		mlxcpld_i2c_reset(priv);
4186bec23bfSVadim Pasternak 		if (mlxcpld_i2c_check_busy(priv)) {
4196bec23bfSVadim Pasternak 			dev_err(priv->dev, "LPCI2C bridge is busy after reset\n");
4206bec23bfSVadim Pasternak 			return -EIO;
4216bec23bfSVadim Pasternak 		}
4226bec23bfSVadim Pasternak 	}
4236bec23bfSVadim Pasternak 
4246bec23bfSVadim Pasternak 	mlxcpld_i2c_set_transf_data(priv, msgs, num, comm_len);
4256bec23bfSVadim Pasternak 
4266bec23bfSVadim Pasternak 	mutex_lock(&priv->lock);
4276bec23bfSVadim Pasternak 
4286bec23bfSVadim Pasternak 	/* Do real transfer. Can't fail */
4296bec23bfSVadim Pasternak 	mlxcpld_i2c_xfer_msg(priv);
4306bec23bfSVadim Pasternak 
4316bec23bfSVadim Pasternak 	/* Wait for transaction complete */
4326bec23bfSVadim Pasternak 	err = mlxcpld_i2c_wait_for_tc(priv);
4336bec23bfSVadim Pasternak 
4346bec23bfSVadim Pasternak 	mutex_unlock(&priv->lock);
4356bec23bfSVadim Pasternak 
4366bec23bfSVadim Pasternak 	return err < 0 ? err : num;
4376bec23bfSVadim Pasternak }
4386bec23bfSVadim Pasternak 
mlxcpld_i2c_func(struct i2c_adapter * adap)4396bec23bfSVadim Pasternak static u32 mlxcpld_i2c_func(struct i2c_adapter *adap)
4406bec23bfSVadim Pasternak {
441845f2a6dSMichael Shych 	struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap);
442845f2a6dSMichael Shych 
443845f2a6dSMichael Shych 	if (priv->smbus_block)
444845f2a6dSMichael Shych 		return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
445845f2a6dSMichael Shych 			I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_BLOCK_DATA;
446845f2a6dSMichael Shych 	else
447845f2a6dSMichael Shych 		return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
448845f2a6dSMichael Shych 			I2C_FUNC_SMBUS_I2C_BLOCK;
4496bec23bfSVadim Pasternak }
4506bec23bfSVadim Pasternak 
4516bec23bfSVadim Pasternak static const struct i2c_algorithm mlxcpld_i2c_algo = {
4526bec23bfSVadim Pasternak 	.master_xfer	= mlxcpld_i2c_xfer,
4536bec23bfSVadim Pasternak 	.functionality	= mlxcpld_i2c_func
4546bec23bfSVadim Pasternak };
4556bec23bfSVadim Pasternak 
456ae3923a2SBhumika Goyal static const struct i2c_adapter_quirks mlxcpld_i2c_quirks = {
4576bec23bfSVadim Pasternak 	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
4586bec23bfSVadim Pasternak 	.max_read_len = MLXCPLD_I2C_DATA_REG_SZ - MLXCPLD_I2C_MAX_ADDR_LEN,
4596bec23bfSVadim Pasternak 	.max_write_len = MLXCPLD_I2C_DATA_REG_SZ,
4606bec23bfSVadim Pasternak 	.max_comb_1st_msg_len = 4,
4616bec23bfSVadim Pasternak };
4626bec23bfSVadim Pasternak 
463313ce648SMichael Shych static const struct i2c_adapter_quirks mlxcpld_i2c_quirks_ext = {
464313ce648SMichael Shych 	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
465313ce648SMichael Shych 	.max_read_len = MLXCPLD_I2C_DATA_REG_SZ * 2 - MLXCPLD_I2C_MAX_ADDR_LEN,
466313ce648SMichael Shych 	.max_write_len = MLXCPLD_I2C_DATA_REG_SZ * 2,
467313ce648SMichael Shych 	.max_comb_1st_msg_len = 4,
468313ce648SMichael Shych };
469313ce648SMichael Shych 
470*2ed4fa9cSVadim Pasternak static const struct i2c_adapter_quirks mlxcpld_i2c_quirks_ext2 = {
471*2ed4fa9cSVadim Pasternak 	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
472*2ed4fa9cSVadim Pasternak 	.max_read_len = (MLXCPLD_I2C_DATA_REG_SZ - 4) * 4,
473*2ed4fa9cSVadim Pasternak 	.max_write_len = (MLXCPLD_I2C_DATA_REG_SZ - 4) * 4 + MLXCPLD_I2C_MAX_ADDR_LEN,
474*2ed4fa9cSVadim Pasternak 	.max_comb_1st_msg_len = 4,
475*2ed4fa9cSVadim Pasternak };
476*2ed4fa9cSVadim Pasternak 
4776bec23bfSVadim Pasternak static struct i2c_adapter mlxcpld_i2c_adapter = {
4786bec23bfSVadim Pasternak 	.owner          = THIS_MODULE,
4796bec23bfSVadim Pasternak 	.name           = "i2c-mlxcpld",
4806bec23bfSVadim Pasternak 	.class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
4816bec23bfSVadim Pasternak 	.algo           = &mlxcpld_i2c_algo,
4826bec23bfSVadim Pasternak 	.quirks		= &mlxcpld_i2c_quirks,
4836bec23bfSVadim Pasternak 	.retries	= MLXCPLD_I2C_RETR_NUM,
4846bec23bfSVadim Pasternak 	.nr		= MLXCPLD_I2C_BUS_NUM,
4856bec23bfSVadim Pasternak };
4866bec23bfSVadim Pasternak 
48766b0c284SVadim Pasternak static int
mlxcpld_i2c_set_frequency(struct mlxcpld_i2c_priv * priv,struct mlxreg_core_hotplug_platform_data * pdata)48866b0c284SVadim Pasternak mlxcpld_i2c_set_frequency(struct mlxcpld_i2c_priv *priv,
48966b0c284SVadim Pasternak 			  struct mlxreg_core_hotplug_platform_data *pdata)
49066b0c284SVadim Pasternak {
49166b0c284SVadim Pasternak 	struct mlxreg_core_item *item = pdata->items;
49266b0c284SVadim Pasternak 	struct mlxreg_core_data *data;
49366b0c284SVadim Pasternak 	u32 regval;
49466b0c284SVadim Pasternak 	u8 freq;
49566b0c284SVadim Pasternak 	int err;
49666b0c284SVadim Pasternak 
49766b0c284SVadim Pasternak 	if (!item)
49866b0c284SVadim Pasternak 		return 0;
49966b0c284SVadim Pasternak 
50066b0c284SVadim Pasternak 	/* Read frequency setting. */
50166b0c284SVadim Pasternak 	data = item->data;
50266b0c284SVadim Pasternak 	err = regmap_read(pdata->regmap, data->reg, &regval);
50366b0c284SVadim Pasternak 	if (err)
50466b0c284SVadim Pasternak 		return err;
50566b0c284SVadim Pasternak 
50666b0c284SVadim Pasternak 	/* Set frequency only if it is not 100KHz, which is default. */
50752f57396SVadim Pasternak 	switch ((regval & data->mask) >> data->bit) {
50866b0c284SVadim Pasternak 	case MLXCPLD_I2C_FREQ_1000KHZ:
50966b0c284SVadim Pasternak 		freq = MLXCPLD_I2C_FREQ_1000KHZ_SET;
510712d6617SVadim Pasternak 		priv->polling_time /= 4;
51166b0c284SVadim Pasternak 		break;
51266b0c284SVadim Pasternak 	case MLXCPLD_I2C_FREQ_400KHZ:
51366b0c284SVadim Pasternak 		freq = MLXCPLD_I2C_FREQ_400KHZ_SET;
514712d6617SVadim Pasternak 		priv->polling_time /= 4;
51566b0c284SVadim Pasternak 		break;
51666b0c284SVadim Pasternak 	default:
51766b0c284SVadim Pasternak 		return 0;
51866b0c284SVadim Pasternak 	}
51966b0c284SVadim Pasternak 
52066b0c284SVadim Pasternak 	mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_HALF_CYC_REG, &freq, 1);
52166b0c284SVadim Pasternak 
52266b0c284SVadim Pasternak 	return 0;
52366b0c284SVadim Pasternak }
52466b0c284SVadim Pasternak 
mlxcpld_i2c_probe(struct platform_device * pdev)5256bec23bfSVadim Pasternak static int mlxcpld_i2c_probe(struct platform_device *pdev)
5266bec23bfSVadim Pasternak {
52766b0c284SVadim Pasternak 	struct mlxreg_core_hotplug_platform_data *pdata;
5286bec23bfSVadim Pasternak 	struct mlxcpld_i2c_priv *priv;
5296bec23bfSVadim Pasternak 	int err;
530313ce648SMichael Shych 	u8 val;
5316bec23bfSVadim Pasternak 
5326bec23bfSVadim Pasternak 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
5336bec23bfSVadim Pasternak 	if (!priv)
5346bec23bfSVadim Pasternak 		return -ENOMEM;
5356bec23bfSVadim Pasternak 
5366bec23bfSVadim Pasternak 	mutex_init(&priv->lock);
5376bec23bfSVadim Pasternak 	platform_set_drvdata(pdev, priv);
5386bec23bfSVadim Pasternak 
5396bec23bfSVadim Pasternak 	priv->dev = &pdev->dev;
54013067ef7SVadim Pasternak 	priv->base_addr = MLXPLAT_CPLD_LPC_I2C_BASE_ADDR;
541712d6617SVadim Pasternak 	priv->polling_time = MLXCPLD_I2C_POLL_TIME;
5426bec23bfSVadim Pasternak 
54366b0c284SVadim Pasternak 	/* Set I2C bus frequency if platform data provides this info. */
54466b0c284SVadim Pasternak 	pdata = dev_get_platdata(&pdev->dev);
54566b0c284SVadim Pasternak 	if (pdata) {
54666b0c284SVadim Pasternak 		err = mlxcpld_i2c_set_frequency(priv, pdata);
54766b0c284SVadim Pasternak 		if (err)
54866b0c284SVadim Pasternak 			goto mlxcpld_i2_probe_failed;
54966b0c284SVadim Pasternak 	}
55066b0c284SVadim Pasternak 
5516bec23bfSVadim Pasternak 	/* Register with i2c layer */
5526bec23bfSVadim Pasternak 	mlxcpld_i2c_adapter.timeout = usecs_to_jiffies(MLXCPLD_I2C_XFER_TO);
553313ce648SMichael Shych 	/* Read capability register */
554313ce648SMichael Shych 	mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CPBLTY_REG, &val, 1);
555313ce648SMichael Shych 	/* Check support for extended transaction length */
556313ce648SMichael Shych 	if ((val & MLXCPLD_I2C_DATA_SZ_MASK) == MLXCPLD_I2C_DATA_SZ_BIT)
557313ce648SMichael Shych 		mlxcpld_i2c_adapter.quirks = &mlxcpld_i2c_quirks_ext;
558*2ed4fa9cSVadim Pasternak 	else if ((val & MLXCPLD_I2C_DATA_SZ_MASK) == MLXCPLD_I2C_DATA_EXT2_SZ_BIT)
559*2ed4fa9cSVadim Pasternak 		mlxcpld_i2c_adapter.quirks = &mlxcpld_i2c_quirks_ext2;
560c9bfdc7cSMichael Shych 	/* Check support for smbus block transaction */
561c9bfdc7cSMichael Shych 	if (val & MLXCPLD_I2C_SMBUS_BLK_BIT)
562c9bfdc7cSMichael Shych 		priv->smbus_block = true;
563ae4aa68dSMichael Shych 	if (pdev->id >= -1)
564ae4aa68dSMichael Shych 		mlxcpld_i2c_adapter.nr = pdev->id;
5656bec23bfSVadim Pasternak 	priv->adap = mlxcpld_i2c_adapter;
5666bec23bfSVadim Pasternak 	priv->adap.dev.parent = &pdev->dev;
5676bec23bfSVadim Pasternak 	i2c_set_adapdata(&priv->adap, priv);
5686bec23bfSVadim Pasternak 
5696bec23bfSVadim Pasternak 	err = i2c_add_numbered_adapter(&priv->adap);
5706bec23bfSVadim Pasternak 	if (err)
57166b0c284SVadim Pasternak 		goto mlxcpld_i2_probe_failed;
5726bec23bfSVadim Pasternak 
5731f438d23SVadim Pasternak 	/* Notify caller when adapter is added. */
5741f438d23SVadim Pasternak 	if (pdata && pdata->completion_notify)
5751f438d23SVadim Pasternak 		pdata->completion_notify(pdata->handle, mlxcpld_i2c_adapter.nr);
5761f438d23SVadim Pasternak 
57766b0c284SVadim Pasternak 	return 0;
57866b0c284SVadim Pasternak 
57966b0c284SVadim Pasternak mlxcpld_i2_probe_failed:
58066b0c284SVadim Pasternak 	mutex_destroy(&priv->lock);
5816bec23bfSVadim Pasternak 	return err;
5826bec23bfSVadim Pasternak }
5836bec23bfSVadim Pasternak 
mlxcpld_i2c_remove(struct platform_device * pdev)584e190a0c3SUwe Kleine-König static void mlxcpld_i2c_remove(struct platform_device *pdev)
5856bec23bfSVadim Pasternak {
5866bec23bfSVadim Pasternak 	struct mlxcpld_i2c_priv *priv = platform_get_drvdata(pdev);
5876bec23bfSVadim Pasternak 
5886bec23bfSVadim Pasternak 	i2c_del_adapter(&priv->adap);
5896bec23bfSVadim Pasternak 	mutex_destroy(&priv->lock);
5906bec23bfSVadim Pasternak }
5916bec23bfSVadim Pasternak 
5926bec23bfSVadim Pasternak static struct platform_driver mlxcpld_i2c_driver = {
5936bec23bfSVadim Pasternak 	.probe		= mlxcpld_i2c_probe,
594e190a0c3SUwe Kleine-König 	.remove_new	= mlxcpld_i2c_remove,
5956bec23bfSVadim Pasternak 	.driver = {
5966bec23bfSVadim Pasternak 		.name = MLXCPLD_I2C_DEVICE_NAME,
5976bec23bfSVadim Pasternak 	},
5986bec23bfSVadim Pasternak };
5996bec23bfSVadim Pasternak 
6006bec23bfSVadim Pasternak module_platform_driver(mlxcpld_i2c_driver);
6016bec23bfSVadim Pasternak 
6026bec23bfSVadim Pasternak MODULE_AUTHOR("Michael Shych <michaels@mellanox.com>");
6036bec23bfSVadim Pasternak MODULE_DESCRIPTION("Mellanox I2C-CPLD controller driver");
6046bec23bfSVadim Pasternak MODULE_LICENSE("Dual BSD/GPL");
6056bec23bfSVadim Pasternak MODULE_ALIAS("platform:i2c-mlxcpld");
606