1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005-2006 Micronas USA Inc.
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>
17 #include "go7007-priv.h"
19 /********************* Driver for on-board I2C adapter *********************/
21 /* #define GO7007_I2C_DEBUG */
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)
31 #define I2C_STATE_MASK 0x0007
32 #define I2C_READ_READY_MASK 0x0008
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);
38 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
39 u16 command, int flags, u8 *data)
44 if (go->status == STATUS_SHUTDOWN)
47 #ifdef GO7007_I2C_DEBUG
49 dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
53 "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
54 *data, command, addr);
57 mutex_lock(&go->hw_lock);
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);
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)
69 if (!(val & I2C_STATE_MASK))
74 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
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);
82 /* If we're writing, send the data and target address and we're done */
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));
91 /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
92 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
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));
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)
103 if (val & I2C_READ_READY_MASK)
108 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
112 /* Retrieve the read byte */
113 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
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);
124 mutex_unlock(&go->hw_lock);
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)
132 struct go7007 *go = i2c_get_adapdata(adapter);
134 if (size != I2C_SMBUS_BYTE_DATA)
136 return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
137 flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
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. */
145 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
146 struct i2c_msg msgs[], int num)
148 struct go7007 *go = i2c_get_adapdata(adapter);
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)
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)
165 } else if (msgs[i].len == 3) {
166 if (msgs[i].flags & I2C_M_RD)
168 if (msgs[i].len != 3)
170 if (go7007_i2c_xfer(go, msgs[i].addr, 0,
171 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
172 0x01, &msgs[i].buf[2]) < 0)
181 static u32 go7007_functionality(struct i2c_adapter *adapter)
183 return I2C_FUNC_SMBUS_BYTE_DATA;
186 static const struct i2c_algorithm go7007_algo = {
187 .smbus_xfer = go7007_smbus_xfer,
188 .master_xfer = go7007_i2c_master_xfer,
189 .functionality = go7007_functionality,
192 static struct i2c_adapter go7007_adap_templ = {
193 .owner = THIS_MODULE,
194 .name = "WIS GO7007SB",
195 .algo = &go7007_algo,
198 int go7007_i2c_init(struct go7007 *go)
200 memcpy(&go->i2c_adapter, &go7007_adap_templ,
201 sizeof(go7007_adap_templ));
202 go->i2c_adapter.dev.parent = go->dev;
203 i2c_set_adapdata(&go->i2c_adapter, go);
204 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
206 "go7007-i2c: error: i2c_add_adapter failed\n");