1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005-2006 Micronas USA Inc.
4  */
5 
6 #include <linux/module.h>
7 #include <linux/delay.h>
8 #include <linux/sched.h>
9 #include <linux/list.h>
10 #include <linux/unistd.h>
11 #include <linux/time.h>
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/mutex.h>
15 #include <linux/uaccess.h>
16 
17 #include "go7007-priv.h"
18 
19 /********************* Driver for on-board I2C adapter *********************/
20 
21 /* #define GO7007_I2C_DEBUG */
22 
23 #define SPI_I2C_ADDR_BASE		0x1400
24 #define STATUS_REG_ADDR			(SPI_I2C_ADDR_BASE + 0x2)
25 #define I2C_CTRL_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x6)
26 #define I2C_DEV_UP_ADDR_REG_ADDR	(SPI_I2C_ADDR_BASE + 0x7)
27 #define I2C_LO_ADDR_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x8)
28 #define I2C_DATA_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x9)
29 #define I2C_CLKFREQ_REG_ADDR		(SPI_I2C_ADDR_BASE + 0xa)
30 
31 #define I2C_STATE_MASK			0x0007
32 #define I2C_READ_READY_MASK		0x0008
33 
34 /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
35  * on the Adlink PCI-MPG24, so access is shared between all of them. */
36 static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
37 
38 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
39 		u16 command, int flags, u8 *data)
40 {
41 	int i, ret = -EIO;
42 	u16 val;
43 
44 	if (go->status == STATUS_SHUTDOWN)
45 		return -ENODEV;
46 
47 #ifdef GO7007_I2C_DEBUG
48 	if (read)
49 		dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
50 			command, addr);
51 	else
52 		dev_dbg(go->dev,
53 			"go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
54 			*data, command, addr);
55 #endif
56 
57 	mutex_lock(&go->hw_lock);
58 
59 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
60 		/* Bridge the I2C port on this GO7007 to the shared bus */
61 		mutex_lock(&adlink_mpg24_i2c_lock);
62 		go7007_write_addr(go, 0x3c82, 0x0020);
63 	}
64 
65 	/* Wait for I2C adapter to be ready */
66 	for (i = 0; i < 10; ++i) {
67 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
68 			goto i2c_done;
69 		if (!(val & I2C_STATE_MASK))
70 			break;
71 		msleep(100);
72 	}
73 	if (i == 10) {
74 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
75 		goto i2c_done;
76 	}
77 
78 	/* Set target register (command) */
79 	go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
80 	go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
81 
82 	/* If we're writing, send the data and target address and we're done */
83 	if (!read) {
84 		go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
85 		go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
86 					(addr << 9) | (command >> 8));
87 		ret = 0;
88 		goto i2c_done;
89 	}
90 
91 	/* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
92 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
93 		goto i2c_done;
94 
95 	/* Send the target address plus read flag */
96 	go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
97 			(addr << 9) | 0x0100 | (command >> 8));
98 
99 	/* Wait for i2c_rx_data_rdy */
100 	for (i = 0; i < 10; ++i) {
101 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
102 			goto i2c_done;
103 		if (val & I2C_READ_READY_MASK)
104 			break;
105 		msleep(100);
106 	}
107 	if (i == 10) {
108 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
109 		goto i2c_done;
110 	}
111 
112 	/* Retrieve the read byte */
113 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
114 		goto i2c_done;
115 	*data = val;
116 	ret = 0;
117 
118 i2c_done:
119 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
120 		/* Isolate the I2C port on this GO7007 from the shared bus */
121 		go7007_write_addr(go, 0x3c82, 0x0000);
122 		mutex_unlock(&adlink_mpg24_i2c_lock);
123 	}
124 	mutex_unlock(&go->hw_lock);
125 	return ret;
126 }
127 
128 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
129 		unsigned short flags, char read_write,
130 		u8 command, int size, union i2c_smbus_data *data)
131 {
132 	struct go7007 *go = i2c_get_adapdata(adapter);
133 
134 	if (size != I2C_SMBUS_BYTE_DATA)
135 		return -EIO;
136 	return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
137 			flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
138 }
139 
140 /* VERY LIMITED I2C master xfer function -- only needed because the
141  * SMBus functions only support 8-bit commands and the SAA7135 uses
142  * 16-bit commands.  The I2C interface on the GO7007, as limited as
143  * it is, does support this mode. */
144 
145 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
146 					struct i2c_msg msgs[], int num)
147 {
148 	struct go7007 *go = i2c_get_adapdata(adapter);
149 	int i;
150 
151 	for (i = 0; i < num; ++i) {
152 		/* We can only do two things here -- write three bytes, or
153 		 * write two bytes and read one byte. */
154 		if (msgs[i].len == 2) {
155 			if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
156 					(msgs[i].flags & I2C_M_RD) ||
157 					!(msgs[i + 1].flags & I2C_M_RD) ||
158 					msgs[i + 1].len != 1)
159 				return -EIO;
160 			if (go7007_i2c_xfer(go, msgs[i].addr, 1,
161 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
162 					0x01, &msgs[i + 1].buf[0]) < 0)
163 				return -EIO;
164 			++i;
165 		} else if (msgs[i].len == 3) {
166 			if (msgs[i].flags & I2C_M_RD)
167 				return -EIO;
168 			if (go7007_i2c_xfer(go, msgs[i].addr, 0,
169 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
170 					0x01, &msgs[i].buf[2]) < 0)
171 				return -EIO;
172 		} else
173 			return -EIO;
174 	}
175 
176 	return num;
177 }
178 
179 static u32 go7007_functionality(struct i2c_adapter *adapter)
180 {
181 	return I2C_FUNC_SMBUS_BYTE_DATA;
182 }
183 
184 static const struct i2c_algorithm go7007_algo = {
185 	.smbus_xfer	= go7007_smbus_xfer,
186 	.master_xfer	= go7007_i2c_master_xfer,
187 	.functionality	= go7007_functionality,
188 };
189 
190 static struct i2c_adapter go7007_adap_templ = {
191 	.owner			= THIS_MODULE,
192 	.name			= "WIS GO7007SB",
193 	.algo			= &go7007_algo,
194 };
195 
196 int go7007_i2c_init(struct go7007 *go)
197 {
198 	memcpy(&go->i2c_adapter, &go7007_adap_templ,
199 			sizeof(go7007_adap_templ));
200 	go->i2c_adapter.dev.parent = go->dev;
201 	i2c_set_adapdata(&go->i2c_adapter, go);
202 	if (i2c_add_adapter(&go->i2c_adapter) < 0) {
203 		dev_err(go->dev,
204 			"go7007-i2c: error: i2c_add_adapter failed\n");
205 		return -1;
206 	}
207 	return 0;
208 }
209