xref: /linux/drivers/media/usb/go7007/go7007-i2c.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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_mutex);
37 
adlink_mpg24_i2c_lock(struct go7007 * go)38 static inline void adlink_mpg24_i2c_lock(struct go7007 *go)
39 {
40 	/* Bridge the I2C port on this GO7007 to the shared bus */
41 	mutex_lock(&adlink_mpg24_i2c_mutex);
42 	go7007_write_addr(go, 0x3c82, 0x0020);
43 }
44 
adlink_mpg24_i2c_unlock(struct go7007 * go)45 static inline void adlink_mpg24_i2c_unlock(struct go7007 *go)
46 {
47 	/* Isolate the I2C port on this GO7007 from the shared bus */
48 	go7007_write_addr(go, 0x3c82, 0x0000);
49 	mutex_unlock(&adlink_mpg24_i2c_mutex);
50 }
51 
go7007_i2c_xfer(struct go7007 * go,u16 addr,int read,u16 command,int flags,u8 * data)52 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
53 		u16 command, int flags, u8 *data)
54 {
55 	int i, ret = -EIO;
56 	u16 val;
57 
58 	if (go->status == STATUS_SHUTDOWN)
59 		return -ENODEV;
60 
61 #ifdef GO7007_I2C_DEBUG
62 	if (read)
63 		dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
64 			command, addr);
65 	else
66 		dev_dbg(go->dev,
67 			"go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
68 			*data, command, addr);
69 #endif
70 
71 	mutex_lock(&go->hw_lock);
72 
73 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
74 		adlink_mpg24_i2c_lock(go);
75 
76 	/* Wait for I2C adapter to be ready */
77 	for (i = 0; i < 10; ++i) {
78 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
79 			goto i2c_done;
80 		if (!(val & I2C_STATE_MASK))
81 			break;
82 		msleep(100);
83 	}
84 	if (i == 10) {
85 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
86 		goto i2c_done;
87 	}
88 
89 	/* Set target register (command) */
90 	go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
91 	go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
92 
93 	/* If we're writing, send the data and target address and we're done */
94 	if (!read) {
95 		go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
96 		go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
97 					(addr << 9) | (command >> 8));
98 		ret = 0;
99 		goto i2c_done;
100 	}
101 
102 	/* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
103 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
104 		goto i2c_done;
105 
106 	/* Send the target address plus read flag */
107 	go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
108 			(addr << 9) | 0x0100 | (command >> 8));
109 
110 	/* Wait for i2c_rx_data_rdy */
111 	for (i = 0; i < 10; ++i) {
112 		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
113 			goto i2c_done;
114 		if (val & I2C_READ_READY_MASK)
115 			break;
116 		msleep(100);
117 	}
118 	if (i == 10) {
119 		dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
120 		goto i2c_done;
121 	}
122 
123 	/* Retrieve the read byte */
124 	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
125 		goto i2c_done;
126 	*data = val;
127 	ret = 0;
128 
129 i2c_done:
130 	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24)
131 		adlink_mpg24_i2c_unlock(go);
132 	mutex_unlock(&go->hw_lock);
133 	return ret;
134 }
135 
go7007_smbus_xfer(struct i2c_adapter * adapter,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)136 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
137 		unsigned short flags, char read_write,
138 		u8 command, int size, union i2c_smbus_data *data)
139 {
140 	struct go7007 *go = i2c_get_adapdata(adapter);
141 
142 	if (size != I2C_SMBUS_BYTE_DATA)
143 		return -EIO;
144 	return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
145 			flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
146 }
147 
148 /* VERY LIMITED I2C master xfer function -- only needed because the
149  * SMBus functions only support 8-bit commands and the SAA7135 uses
150  * 16-bit commands.  The I2C interface on the GO7007, as limited as
151  * it is, does support this mode. */
152 
go7007_i2c_master_xfer(struct i2c_adapter * adapter,struct i2c_msg msgs[],int num)153 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
154 					struct i2c_msg msgs[], int num)
155 {
156 	struct go7007 *go = i2c_get_adapdata(adapter);
157 	int i;
158 
159 	for (i = 0; i < num; ++i) {
160 		/* We can only do two things here -- write three bytes, or
161 		 * write two bytes and read one byte. */
162 		if (msgs[i].len == 2) {
163 			if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
164 					(msgs[i].flags & I2C_M_RD) ||
165 					!(msgs[i + 1].flags & I2C_M_RD) ||
166 					msgs[i + 1].len != 1)
167 				return -EIO;
168 			if (go7007_i2c_xfer(go, msgs[i].addr, 1,
169 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
170 					0x01, &msgs[i + 1].buf[0]) < 0)
171 				return -EIO;
172 			++i;
173 		} else if (msgs[i].len == 3) {
174 			if (msgs[i].flags & I2C_M_RD)
175 				return -EIO;
176 			if (go7007_i2c_xfer(go, msgs[i].addr, 0,
177 					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
178 					0x01, &msgs[i].buf[2]) < 0)
179 				return -EIO;
180 		} else
181 			return -EIO;
182 	}
183 
184 	return num;
185 }
186 
go7007_functionality(struct i2c_adapter * adapter)187 static u32 go7007_functionality(struct i2c_adapter *adapter)
188 {
189 	return I2C_FUNC_SMBUS_BYTE_DATA;
190 }
191 
192 static const struct i2c_algorithm go7007_algo = {
193 	.smbus_xfer	= go7007_smbus_xfer,
194 	.master_xfer	= go7007_i2c_master_xfer,
195 	.functionality	= go7007_functionality,
196 };
197 
198 static struct i2c_adapter go7007_adap_templ = {
199 	.owner			= THIS_MODULE,
200 	.name			= "WIS GO7007SB",
201 	.algo			= &go7007_algo,
202 };
203 
go7007_i2c_init(struct go7007 * go)204 int go7007_i2c_init(struct go7007 *go)
205 {
206 	memcpy(&go->i2c_adapter, &go7007_adap_templ,
207 			sizeof(go7007_adap_templ));
208 	go->i2c_adapter.dev.parent = go->dev;
209 	i2c_set_adapdata(&go->i2c_adapter, go);
210 	if (i2c_add_adapter(&go->i2c_adapter) < 0) {
211 		dev_err(go->dev,
212 			"go7007-i2c: error: i2c_add_adapter failed\n");
213 		return -1;
214 	}
215 	return 0;
216 }
217