xref: /openbmc/u-boot/drivers/i2c/rk_i2c.c (revision 2ae23a28)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
234374699SSimon Glass /*
334374699SSimon Glass  * (C) Copyright 2015 Google, Inc
434374699SSimon Glass  *
534374699SSimon Glass  * (C) Copyright 2008-2014 Rockchip Electronics
634374699SSimon Glass  * Peter, Software Engineering, <superpeter.cai@gmail.com>.
734374699SSimon Glass  */
834374699SSimon Glass 
934374699SSimon Glass #include <common.h>
1034374699SSimon Glass #include <clk.h>
1134374699SSimon Glass #include <dm.h>
1234374699SSimon Glass #include <errno.h>
1334374699SSimon Glass #include <i2c.h>
1434374699SSimon Glass #include <asm/io.h>
1534374699SSimon Glass #include <asm/arch/clock.h>
1634374699SSimon Glass #include <asm/arch/i2c.h>
1734374699SSimon Glass #include <asm/arch/periph.h>
1834374699SSimon Glass #include <dm/pinctrl.h>
1934374699SSimon Glass #include <linux/sizes.h>
2034374699SSimon Glass 
2134374699SSimon Glass /* i2c timerout */
2234374699SSimon Glass #define I2C_TIMEOUT_MS		100
2334374699SSimon Glass #define I2C_RETRY_COUNT		3
2434374699SSimon Glass 
2534374699SSimon Glass /* rk i2c fifo max transfer bytes */
2634374699SSimon Glass #define RK_I2C_FIFO_SIZE	32
2734374699SSimon Glass 
2834374699SSimon Glass struct rk_i2c {
29135aa950SStephen Warren 	struct clk clk;
3034374699SSimon Glass 	struct i2c_regs *regs;
3134374699SSimon Glass 	unsigned int speed;
3234374699SSimon Glass };
3334374699SSimon Glass 
34*5944bd9aSAlexander Kochetkov enum {
35*5944bd9aSAlexander Kochetkov 	RK_I2C_LEGACY,
36*5944bd9aSAlexander Kochetkov 	RK_I2C_NEW,
37*5944bd9aSAlexander Kochetkov };
38*5944bd9aSAlexander Kochetkov 
39*5944bd9aSAlexander Kochetkov /**
40*5944bd9aSAlexander Kochetkov  * @controller_type: i2c controller type
41*5944bd9aSAlexander Kochetkov  */
42*5944bd9aSAlexander Kochetkov struct rk_i2c_soc_data {
43*5944bd9aSAlexander Kochetkov 	int controller_type;
44*5944bd9aSAlexander Kochetkov };
45*5944bd9aSAlexander Kochetkov 
rk_i2c_get_div(int div,int * divh,int * divl)4634374699SSimon Glass static inline void rk_i2c_get_div(int div, int *divh, int *divl)
4734374699SSimon Glass {
4834374699SSimon Glass 	*divl = div / 2;
4934374699SSimon Glass 	if (div % 2 == 0)
5034374699SSimon Glass 		*divh = div / 2;
5134374699SSimon Glass 	else
5234374699SSimon Glass 		*divh = DIV_ROUND_UP(div, 2);
5334374699SSimon Glass }
5434374699SSimon Glass 
5534374699SSimon Glass /*
5634374699SSimon Glass  * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1)
5734374699SSimon Glass  * SCL = PCLK / SCLK Divisor
5834374699SSimon Glass  * i2c_rate = PCLK
5934374699SSimon Glass  */
rk_i2c_set_clk(struct rk_i2c * i2c,uint32_t scl_rate)6034374699SSimon Glass static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate)
6134374699SSimon Glass {
6234374699SSimon Glass 	uint32_t i2c_rate;
6334374699SSimon Glass 	int div, divl, divh;
6434374699SSimon Glass 
6534374699SSimon Glass 	/* First get i2c rate from pclk */
66135aa950SStephen Warren 	i2c_rate = clk_get_rate(&i2c->clk);
6734374699SSimon Glass 
6834374699SSimon Glass 	div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2;
6934374699SSimon Glass 	divh = 0;
7034374699SSimon Glass 	divl = 0;
7134374699SSimon Glass 	if (div >= 0)
7234374699SSimon Glass 		rk_i2c_get_div(div, &divh, &divl);
7334374699SSimon Glass 	writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv);
7434374699SSimon Glass 
7534374699SSimon Glass 	debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate,
7634374699SSimon Glass 	      scl_rate);
7734374699SSimon Glass 	debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl);
7834374699SSimon Glass 	debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv));
7934374699SSimon Glass }
8034374699SSimon Glass 
rk_i2c_show_regs(struct i2c_regs * regs)8134374699SSimon Glass static void rk_i2c_show_regs(struct i2c_regs *regs)
8234374699SSimon Glass {
8334374699SSimon Glass #ifdef DEBUG
8434374699SSimon Glass 	uint i;
8534374699SSimon Glass 
8634374699SSimon Glass 	debug("i2c_con: 0x%08x\n", readl(&regs->con));
8734374699SSimon Glass 	debug("i2c_clkdiv: 0x%08x\n", readl(&regs->clkdiv));
8834374699SSimon Glass 	debug("i2c_mrxaddr: 0x%08x\n", readl(&regs->mrxaddr));
8934374699SSimon Glass 	debug("i2c_mrxraddR: 0x%08x\n", readl(&regs->mrxraddr));
9034374699SSimon Glass 	debug("i2c_mtxcnt: 0x%08x\n", readl(&regs->mtxcnt));
9134374699SSimon Glass 	debug("i2c_mrxcnt: 0x%08x\n", readl(&regs->mrxcnt));
9234374699SSimon Glass 	debug("i2c_ien: 0x%08x\n", readl(&regs->ien));
9334374699SSimon Glass 	debug("i2c_ipd: 0x%08x\n", readl(&regs->ipd));
9434374699SSimon Glass 	debug("i2c_fcnt: 0x%08x\n", readl(&regs->fcnt));
9534374699SSimon Glass 	for (i = 0; i < 8; i++)
9634374699SSimon Glass 		debug("i2c_txdata%d: 0x%08x\n", i, readl(&regs->txdata[i]));
9734374699SSimon Glass 	for (i = 0; i < 8; i++)
9834374699SSimon Glass 		debug("i2c_rxdata%d: 0x%08x\n", i, readl(&regs->rxdata[i]));
9934374699SSimon Glass #endif
10034374699SSimon Glass }
10134374699SSimon Glass 
rk_i2c_send_start_bit(struct rk_i2c * i2c)10234374699SSimon Glass static int rk_i2c_send_start_bit(struct rk_i2c *i2c)
10334374699SSimon Glass {
10434374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
10534374699SSimon Glass 	ulong start;
10634374699SSimon Glass 
10734374699SSimon Glass 	debug("I2c Send Start bit.\n");
10834374699SSimon Glass 	writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
10934374699SSimon Glass 
11034374699SSimon Glass 	writel(I2C_CON_EN | I2C_CON_START, &regs->con);
11134374699SSimon Glass 	writel(I2C_STARTIEN, &regs->ien);
11234374699SSimon Glass 
11334374699SSimon Glass 	start = get_timer(0);
11434374699SSimon Glass 	while (1) {
11534374699SSimon Glass 		if (readl(&regs->ipd) & I2C_STARTIPD) {
11634374699SSimon Glass 			writel(I2C_STARTIPD, &regs->ipd);
11734374699SSimon Glass 			break;
11834374699SSimon Glass 		}
11934374699SSimon Glass 		if (get_timer(start) > I2C_TIMEOUT_MS) {
12034374699SSimon Glass 			debug("I2C Send Start Bit Timeout\n");
12134374699SSimon Glass 			rk_i2c_show_regs(regs);
12234374699SSimon Glass 			return -ETIMEDOUT;
12334374699SSimon Glass 		}
12434374699SSimon Glass 		udelay(1);
12534374699SSimon Glass 	}
12634374699SSimon Glass 
12734374699SSimon Glass 	return 0;
12834374699SSimon Glass }
12934374699SSimon Glass 
rk_i2c_send_stop_bit(struct rk_i2c * i2c)13034374699SSimon Glass static int rk_i2c_send_stop_bit(struct rk_i2c *i2c)
13134374699SSimon Glass {
13234374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
13334374699SSimon Glass 	ulong start;
13434374699SSimon Glass 
13534374699SSimon Glass 	debug("I2c Send Stop bit.\n");
13634374699SSimon Glass 	writel(I2C_IPD_ALL_CLEAN, &regs->ipd);
13734374699SSimon Glass 
13834374699SSimon Glass 	writel(I2C_CON_EN | I2C_CON_STOP, &regs->con);
13934374699SSimon Glass 	writel(I2C_CON_STOP, &regs->ien);
14034374699SSimon Glass 
14134374699SSimon Glass 	start = get_timer(0);
14234374699SSimon Glass 	while (1) {
14334374699SSimon Glass 		if (readl(&regs->ipd) & I2C_STOPIPD) {
14434374699SSimon Glass 			writel(I2C_STOPIPD, &regs->ipd);
14534374699SSimon Glass 			break;
14634374699SSimon Glass 		}
14734374699SSimon Glass 		if (get_timer(start) > I2C_TIMEOUT_MS) {
14834374699SSimon Glass 			debug("I2C Send Start Bit Timeout\n");
14934374699SSimon Glass 			rk_i2c_show_regs(regs);
15034374699SSimon Glass 			return -ETIMEDOUT;
15134374699SSimon Glass 		}
15234374699SSimon Glass 		udelay(1);
15334374699SSimon Glass 	}
15434374699SSimon Glass 
15534374699SSimon Glass 	return 0;
15634374699SSimon Glass }
15734374699SSimon Glass 
rk_i2c_disable(struct rk_i2c * i2c)15834374699SSimon Glass static inline void rk_i2c_disable(struct rk_i2c *i2c)
15934374699SSimon Glass {
16034374699SSimon Glass 	writel(0, &i2c->regs->con);
16134374699SSimon Glass }
16234374699SSimon Glass 
rk_i2c_read(struct rk_i2c * i2c,uchar chip,uint reg,uint r_len,uchar * buf,uint b_len)16334374699SSimon Glass static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
16434374699SSimon Glass 		       uchar *buf, uint b_len)
16534374699SSimon Glass {
16634374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
16734374699SSimon Glass 	uchar *pbuf = buf;
16834374699SSimon Glass 	uint bytes_remain_len = b_len;
16934374699SSimon Glass 	uint bytes_xferred = 0;
17034374699SSimon Glass 	uint words_xferred = 0;
17134374699SSimon Glass 	ulong start;
17234374699SSimon Glass 	uint con = 0;
17334374699SSimon Glass 	uint rxdata;
17434374699SSimon Glass 	uint i, j;
17534374699SSimon Glass 	int err;
1765deaa530SWadim Egorov 	bool snd_chunk = false;
17734374699SSimon Glass 
17834374699SSimon Glass 	debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
17934374699SSimon Glass 	      chip, reg, r_len, b_len);
18034374699SSimon Glass 
18134374699SSimon Glass 	err = rk_i2c_send_start_bit(i2c);
18234374699SSimon Glass 	if (err)
18334374699SSimon Glass 		return err;
18434374699SSimon Glass 
18534374699SSimon Glass 	writel(I2C_MRXADDR_SET(1, chip << 1 | 1), &regs->mrxaddr);
18634374699SSimon Glass 	if (r_len == 0) {
18734374699SSimon Glass 		writel(0, &regs->mrxraddr);
18834374699SSimon Glass 	} else if (r_len < 4) {
18934374699SSimon Glass 		writel(I2C_MRXRADDR_SET(r_len, reg), &regs->mrxraddr);
19034374699SSimon Glass 	} else {
19134374699SSimon Glass 		debug("I2C Read: addr len %d not supported\n", r_len);
19234374699SSimon Glass 		return -EIO;
19334374699SSimon Glass 	}
19434374699SSimon Glass 
19534374699SSimon Glass 	while (bytes_remain_len) {
19634374699SSimon Glass 		if (bytes_remain_len > RK_I2C_FIFO_SIZE) {
1975deaa530SWadim Egorov 			con = I2C_CON_EN;
19834374699SSimon Glass 			bytes_xferred = 32;
19934374699SSimon Glass 		} else {
2005deaa530SWadim Egorov 			/*
2015deaa530SWadim Egorov 			 * The hw can read up to 32 bytes at a time. If we need
2025deaa530SWadim Egorov 			 * more than one chunk, send an ACK after the last byte.
2035deaa530SWadim Egorov 			 */
2045deaa530SWadim Egorov 			con = I2C_CON_EN | I2C_CON_LASTACK;
20534374699SSimon Glass 			bytes_xferred = bytes_remain_len;
20634374699SSimon Glass 		}
20734374699SSimon Glass 		words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
20834374699SSimon Glass 
2095deaa530SWadim Egorov 		/*
2105deaa530SWadim Egorov 		 * make sure we are in plain RX mode if we read a second chunk
2115deaa530SWadim Egorov 		 */
2125deaa530SWadim Egorov 		if (snd_chunk)
2135deaa530SWadim Egorov 			con |= I2C_CON_MOD(I2C_MODE_RX);
2145deaa530SWadim Egorov 		else
2155deaa530SWadim Egorov 			con |= I2C_CON_MOD(I2C_MODE_TRX);
2165deaa530SWadim Egorov 
21734374699SSimon Glass 		writel(con, &regs->con);
21834374699SSimon Glass 		writel(bytes_xferred, &regs->mrxcnt);
21934374699SSimon Glass 		writel(I2C_MBRFIEN | I2C_NAKRCVIEN, &regs->ien);
22034374699SSimon Glass 
22134374699SSimon Glass 		start = get_timer(0);
22234374699SSimon Glass 		while (1) {
22334374699SSimon Glass 			if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
22434374699SSimon Glass 				writel(I2C_NAKRCVIPD, &regs->ipd);
22534374699SSimon Glass 				err = -EREMOTEIO;
22634374699SSimon Glass 			}
22734374699SSimon Glass 			if (readl(&regs->ipd) & I2C_MBRFIPD) {
22834374699SSimon Glass 				writel(I2C_MBRFIPD, &regs->ipd);
22934374699SSimon Glass 				break;
23034374699SSimon Glass 			}
23134374699SSimon Glass 			if (get_timer(start) > I2C_TIMEOUT_MS) {
23234374699SSimon Glass 				debug("I2C Read Data Timeout\n");
23334374699SSimon Glass 				err =  -ETIMEDOUT;
23434374699SSimon Glass 				rk_i2c_show_regs(regs);
23534374699SSimon Glass 				goto i2c_exit;
23634374699SSimon Glass 			}
23734374699SSimon Glass 			udelay(1);
23834374699SSimon Glass 		}
23934374699SSimon Glass 
24034374699SSimon Glass 		for (i = 0; i < words_xferred; i++) {
24134374699SSimon Glass 			rxdata = readl(&regs->rxdata[i]);
24234374699SSimon Glass 			debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata);
24334374699SSimon Glass 			for (j = 0; j < 4; j++) {
24434374699SSimon Glass 				if ((i * 4 + j) == bytes_xferred)
24534374699SSimon Glass 					break;
24634374699SSimon Glass 				*pbuf++ = (rxdata >> (j * 8)) & 0xff;
24734374699SSimon Glass 			}
24834374699SSimon Glass 		}
24934374699SSimon Glass 
25034374699SSimon Glass 		bytes_remain_len -= bytes_xferred;
2515deaa530SWadim Egorov 		snd_chunk = true;
25234374699SSimon Glass 		debug("I2C Read bytes_remain_len %d\n", bytes_remain_len);
25334374699SSimon Glass 	}
25434374699SSimon Glass 
25534374699SSimon Glass i2c_exit:
25634374699SSimon Glass 	rk_i2c_send_stop_bit(i2c);
25734374699SSimon Glass 	rk_i2c_disable(i2c);
25834374699SSimon Glass 
25934374699SSimon Glass 	return err;
26034374699SSimon Glass }
26134374699SSimon Glass 
rk_i2c_write(struct rk_i2c * i2c,uchar chip,uint reg,uint r_len,uchar * buf,uint b_len)26234374699SSimon Glass static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len,
26334374699SSimon Glass 			uchar *buf, uint b_len)
26434374699SSimon Glass {
26534374699SSimon Glass 	struct i2c_regs *regs = i2c->regs;
26634374699SSimon Glass 	int err;
26734374699SSimon Glass 	uchar *pbuf = buf;
26834374699SSimon Glass 	uint bytes_remain_len = b_len + r_len + 1;
26934374699SSimon Glass 	uint bytes_xferred = 0;
27034374699SSimon Glass 	uint words_xferred = 0;
27134374699SSimon Glass 	ulong start;
27234374699SSimon Glass 	uint txdata;
27334374699SSimon Glass 	uint i, j;
27434374699SSimon Glass 
27534374699SSimon Glass 	debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n",
27634374699SSimon Glass 	      chip, reg, r_len, b_len);
27734374699SSimon Glass 	err = rk_i2c_send_start_bit(i2c);
27834374699SSimon Glass 	if (err)
27934374699SSimon Glass 		return err;
28034374699SSimon Glass 
28134374699SSimon Glass 	while (bytes_remain_len) {
28234374699SSimon Glass 		if (bytes_remain_len > RK_I2C_FIFO_SIZE)
28380333fd8SJohn Keeping 			bytes_xferred = RK_I2C_FIFO_SIZE;
28434374699SSimon Glass 		else
28534374699SSimon Glass 			bytes_xferred = bytes_remain_len;
28634374699SSimon Glass 		words_xferred = DIV_ROUND_UP(bytes_xferred, 4);
28734374699SSimon Glass 
28834374699SSimon Glass 		for (i = 0; i < words_xferred; i++) {
28934374699SSimon Glass 			txdata = 0;
29034374699SSimon Glass 			for (j = 0; j < 4; j++) {
29134374699SSimon Glass 				if ((i * 4 + j) == bytes_xferred)
29234374699SSimon Glass 					break;
29334374699SSimon Glass 
29421d4b7d4SJohn Keeping 				if (i == 0 && j == 0 && pbuf == buf) {
29534374699SSimon Glass 					txdata |= (chip << 1);
29621d4b7d4SJohn Keeping 				} else if (i == 0 && j <= r_len && pbuf == buf) {
29734374699SSimon Glass 					txdata |= (reg &
29834374699SSimon Glass 						(0xff << ((j - 1) * 8))) << 8;
29934374699SSimon Glass 				} else {
30034374699SSimon Glass 					txdata |= (*pbuf++)<<(j * 8);
30134374699SSimon Glass 				}
30234374699SSimon Glass 			}
303551288bdSJohn Keeping 			writel(txdata, &regs->txdata[i]);
304551288bdSJohn Keeping 			debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata);
30534374699SSimon Glass 		}
30634374699SSimon Glass 
30734374699SSimon Glass 		writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), &regs->con);
30834374699SSimon Glass 		writel(bytes_xferred, &regs->mtxcnt);
30934374699SSimon Glass 		writel(I2C_MBTFIEN | I2C_NAKRCVIEN, &regs->ien);
31034374699SSimon Glass 
31134374699SSimon Glass 		start = get_timer(0);
31234374699SSimon Glass 		while (1) {
31334374699SSimon Glass 			if (readl(&regs->ipd) & I2C_NAKRCVIPD) {
31434374699SSimon Glass 				writel(I2C_NAKRCVIPD, &regs->ipd);
31534374699SSimon Glass 				err = -EREMOTEIO;
31634374699SSimon Glass 			}
31734374699SSimon Glass 			if (readl(&regs->ipd) & I2C_MBTFIPD) {
31834374699SSimon Glass 				writel(I2C_MBTFIPD, &regs->ipd);
31934374699SSimon Glass 				break;
32034374699SSimon Glass 			}
32134374699SSimon Glass 			if (get_timer(start) > I2C_TIMEOUT_MS) {
32234374699SSimon Glass 				debug("I2C Write Data Timeout\n");
32334374699SSimon Glass 				err =  -ETIMEDOUT;
32434374699SSimon Glass 				rk_i2c_show_regs(regs);
32534374699SSimon Glass 				goto i2c_exit;
32634374699SSimon Glass 			}
32734374699SSimon Glass 			udelay(1);
32834374699SSimon Glass 		}
32934374699SSimon Glass 
33034374699SSimon Glass 		bytes_remain_len -= bytes_xferred;
33134374699SSimon Glass 		debug("I2C Write bytes_remain_len %d\n", bytes_remain_len);
33234374699SSimon Glass 	}
33334374699SSimon Glass 
33434374699SSimon Glass i2c_exit:
33534374699SSimon Glass 	rk_i2c_send_stop_bit(i2c);
33634374699SSimon Glass 	rk_i2c_disable(i2c);
33734374699SSimon Glass 
33834374699SSimon Glass 	return err;
33934374699SSimon Glass }
34034374699SSimon Glass 
rockchip_i2c_xfer(struct udevice * bus,struct i2c_msg * msg,int nmsgs)34134374699SSimon Glass static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
34234374699SSimon Glass 			     int nmsgs)
34334374699SSimon Glass {
34434374699SSimon Glass 	struct rk_i2c *i2c = dev_get_priv(bus);
34534374699SSimon Glass 	int ret;
34634374699SSimon Glass 
34734374699SSimon Glass 	debug("i2c_xfer: %d messages\n", nmsgs);
34834374699SSimon Glass 	for (; nmsgs > 0; nmsgs--, msg++) {
34934374699SSimon Glass 		debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
35034374699SSimon Glass 		if (msg->flags & I2C_M_RD) {
35134374699SSimon Glass 			ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf,
35234374699SSimon Glass 					  msg->len);
35334374699SSimon Glass 		} else {
35434374699SSimon Glass 			ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf,
35534374699SSimon Glass 					   msg->len);
35634374699SSimon Glass 		}
35734374699SSimon Glass 		if (ret) {
35834374699SSimon Glass 			debug("i2c_write: error sending\n");
35934374699SSimon Glass 			return -EREMOTEIO;
36034374699SSimon Glass 		}
36134374699SSimon Glass 	}
36234374699SSimon Glass 
36334374699SSimon Glass 	return 0;
36434374699SSimon Glass }
36534374699SSimon Glass 
rockchip_i2c_set_bus_speed(struct udevice * bus,unsigned int speed)36634374699SSimon Glass int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
36734374699SSimon Glass {
36834374699SSimon Glass 	struct rk_i2c *i2c = dev_get_priv(bus);
36934374699SSimon Glass 
37034374699SSimon Glass 	rk_i2c_set_clk(i2c, speed);
37134374699SSimon Glass 
37234374699SSimon Glass 	return 0;
37334374699SSimon Glass }
37434374699SSimon Glass 
rockchip_i2c_ofdata_to_platdata(struct udevice * bus)375930bc374SSimon Glass static int rockchip_i2c_ofdata_to_platdata(struct udevice *bus)
37634374699SSimon Glass {
377930bc374SSimon Glass 	struct rk_i2c *priv = dev_get_priv(bus);
37834374699SSimon Glass 	int ret;
37934374699SSimon Glass 
380930bc374SSimon Glass 	ret = clk_get_by_index(bus, 0, &priv->clk);
381930bc374SSimon Glass 	if (ret < 0) {
382930bc374SSimon Glass 		debug("%s: Could not get clock for %s: %d\n", __func__,
383930bc374SSimon Glass 		      bus->name, ret);
38434374699SSimon Glass 		return ret;
385930bc374SSimon Glass 	}
386930bc374SSimon Glass 
387930bc374SSimon Glass 	return 0;
388930bc374SSimon Glass }
389930bc374SSimon Glass 
rockchip_i2c_probe(struct udevice * bus)390930bc374SSimon Glass static int rockchip_i2c_probe(struct udevice *bus)
391930bc374SSimon Glass {
392930bc374SSimon Glass 	struct rk_i2c *priv = dev_get_priv(bus);
393*5944bd9aSAlexander Kochetkov 	struct rk_i2c_soc_data *soc_data;
394*5944bd9aSAlexander Kochetkov 	struct udevice *pinctrl;
395*5944bd9aSAlexander Kochetkov 	int bus_nr;
396*5944bd9aSAlexander Kochetkov 	int ret;
397930bc374SSimon Glass 
398cc91bdf8SPhilipp Tomsich 	priv->regs = dev_read_addr_ptr(bus);
399930bc374SSimon Glass 
400*5944bd9aSAlexander Kochetkov 	soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus);
401*5944bd9aSAlexander Kochetkov 
402*5944bd9aSAlexander Kochetkov 	if (soc_data->controller_type == RK_I2C_LEGACY) {
403*5944bd9aSAlexander Kochetkov 		ret = dev_read_alias_seq(bus, &bus_nr);
404*5944bd9aSAlexander Kochetkov 		if (ret < 0) {
405*5944bd9aSAlexander Kochetkov 			debug("%s: Could not get alias for %s: %d\n",
406*5944bd9aSAlexander Kochetkov 			 __func__, bus->name, ret);
407*5944bd9aSAlexander Kochetkov 			return ret;
408*5944bd9aSAlexander Kochetkov 		}
409*5944bd9aSAlexander Kochetkov 
410*5944bd9aSAlexander Kochetkov 		ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl);
411*5944bd9aSAlexander Kochetkov 		if (ret) {
412*5944bd9aSAlexander Kochetkov 			debug("%s: Cannot find pinctrl device\n", __func__);
413*5944bd9aSAlexander Kochetkov 			return ret;
414*5944bd9aSAlexander Kochetkov 		}
415*5944bd9aSAlexander Kochetkov 
416*5944bd9aSAlexander Kochetkov 		/* pinctrl will switch I2C to new type */
417*5944bd9aSAlexander Kochetkov 		ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr);
418*5944bd9aSAlexander Kochetkov 		if (ret) {
419*5944bd9aSAlexander Kochetkov 			debug("%s: Failed to switch I2C to new type %s: %d\n",
420*5944bd9aSAlexander Kochetkov 				__func__, bus->name, ret);
421*5944bd9aSAlexander Kochetkov 			return ret;
422*5944bd9aSAlexander Kochetkov 		}
423*5944bd9aSAlexander Kochetkov 	}
424*5944bd9aSAlexander Kochetkov 
425930bc374SSimon Glass 	return 0;
42634374699SSimon Glass }
42734374699SSimon Glass 
42834374699SSimon Glass static const struct dm_i2c_ops rockchip_i2c_ops = {
42934374699SSimon Glass 	.xfer		= rockchip_i2c_xfer,
43034374699SSimon Glass 	.set_bus_speed	= rockchip_i2c_set_bus_speed,
43134374699SSimon Glass };
43234374699SSimon Glass 
433*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3066_soc_data = {
434*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_LEGACY,
435*5944bd9aSAlexander Kochetkov };
436*5944bd9aSAlexander Kochetkov 
437*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3188_soc_data = {
438*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_LEGACY,
439*5944bd9aSAlexander Kochetkov };
440*5944bd9aSAlexander Kochetkov 
441*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3228_soc_data = {
442*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_NEW,
443*5944bd9aSAlexander Kochetkov };
444*5944bd9aSAlexander Kochetkov 
445*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3288_soc_data = {
446*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_NEW,
447*5944bd9aSAlexander Kochetkov };
448*5944bd9aSAlexander Kochetkov 
449*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3328_soc_data = {
450*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_NEW,
451*5944bd9aSAlexander Kochetkov };
452*5944bd9aSAlexander Kochetkov 
453*5944bd9aSAlexander Kochetkov static const struct rk_i2c_soc_data rk3399_soc_data = {
454*5944bd9aSAlexander Kochetkov 	.controller_type = RK_I2C_NEW,
455*5944bd9aSAlexander Kochetkov };
456*5944bd9aSAlexander Kochetkov 
45734374699SSimon Glass static const struct udevice_id rockchip_i2c_ids[] = {
458*5944bd9aSAlexander Kochetkov 	{
459*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3066-i2c",
460*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3066_soc_data,
461*5944bd9aSAlexander Kochetkov 	},
462*5944bd9aSAlexander Kochetkov 	{
463*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3188-i2c",
464*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3188_soc_data,
465*5944bd9aSAlexander Kochetkov 	},
466*5944bd9aSAlexander Kochetkov 	{
467*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3228-i2c",
468*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3228_soc_data,
469*5944bd9aSAlexander Kochetkov 	},
470*5944bd9aSAlexander Kochetkov 	{
471*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3288-i2c",
472*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3288_soc_data,
473*5944bd9aSAlexander Kochetkov 	},
474*5944bd9aSAlexander Kochetkov 	{
475*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3328-i2c",
476*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3328_soc_data,
477*5944bd9aSAlexander Kochetkov 	},
478*5944bd9aSAlexander Kochetkov 	{
479*5944bd9aSAlexander Kochetkov 		.compatible = "rockchip,rk3399-i2c",
480*5944bd9aSAlexander Kochetkov 		.data = (ulong)&rk3399_soc_data,
481*5944bd9aSAlexander Kochetkov 	},
48234374699SSimon Glass 	{ }
48334374699SSimon Glass };
48434374699SSimon Glass 
48534374699SSimon Glass U_BOOT_DRIVER(i2c_rockchip) = {
48634374699SSimon Glass 	.name	= "i2c_rockchip",
48734374699SSimon Glass 	.id	= UCLASS_I2C,
48834374699SSimon Glass 	.of_match = rockchip_i2c_ids,
489930bc374SSimon Glass 	.ofdata_to_platdata = rockchip_i2c_ofdata_to_platdata,
49034374699SSimon Glass 	.probe	= rockchip_i2c_probe,
49134374699SSimon Glass 	.priv_auto_alloc_size = sizeof(struct rk_i2c),
49234374699SSimon Glass 	.ops	= &rockchip_i2c_ops,
49334374699SSimon Glass };
494