2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <linux/list.h>
23 #include <linux/unistd.h>
24 #include <linux/time.h>
25 #include <linux/device.h>
26 #include <linux/i2c.h>
27 #include <linux/semaphore.h>
28 #include <linux/uaccess.h>
29 #include <asm/system.h>
31 #include "go7007-priv.h"
34 /************** Registration interface for I2C client drivers **************/
36 /* Since there's no way to auto-probe the I2C devices connected to the I2C
37 * bus on the go7007, we have this silly little registration system that
38 * client drivers can use to register their I2C driver ID and their
39 * detect_client function (the one that's normally passed to i2c_probe).
41 * When a new go7007 device is connected, we can look up in a board info
42 * table by the USB or PCI vendor/product/revision ID to determine
43 * which I2C client module to load. The client driver module will register
44 * itself here, and then we can call the registered detect_client function
45 * to force-load a new client at the address listed in the board info table.
47 * Really the I2C subsystem should have a way to force-load I2C client
48 * drivers when we have a priori knowledge of what's on the bus, especially
49 * since the existing I2C auto-probe mechanism is so hokey, but we'll use
50 * our own mechanism for the time being. */
52 struct wis_i2c_client_driver {
54 found_proc found_proc;
55 struct list_head list;
58 static LIST_HEAD(i2c_client_drivers);
59 static DECLARE_MUTEX(i2c_client_driver_list_lock);
61 /* Client drivers register here by their I2C driver ID */
62 int wis_i2c_add_driver(unsigned int id, found_proc found_proc)
64 struct wis_i2c_client_driver *driver;
66 driver = kmalloc(sizeof(struct wis_i2c_client_driver), GFP_KERNEL);
70 driver->found_proc = found_proc;
72 down(&i2c_client_driver_list_lock);
73 list_add_tail(&driver->list, &i2c_client_drivers);
74 up(&i2c_client_driver_list_lock);
78 EXPORT_SYMBOL(wis_i2c_add_driver);
80 void wis_i2c_del_driver(found_proc found_proc)
82 struct wis_i2c_client_driver *driver, *next;
84 down(&i2c_client_driver_list_lock);
85 list_for_each_entry_safe(driver, next, &i2c_client_drivers, list)
86 if (driver->found_proc == found_proc) {
87 list_del(&driver->list);
90 up(&i2c_client_driver_list_lock);
92 EXPORT_SYMBOL(wis_i2c_del_driver);
94 /* The main go7007 driver calls this to instantiate a client by driver
95 * ID and bus address, which are both stored in the board info table */
96 int wis_i2c_probe_device(struct i2c_adapter *adapter,
97 unsigned int id, int addr)
99 struct wis_i2c_client_driver *driver;
102 if (addr < 0 || addr > 0x7f)
104 down(&i2c_client_driver_list_lock);
105 list_for_each_entry(driver, &i2c_client_drivers, list)
106 if (driver->id == id) {
107 if (driver->found_proc(adapter, addr, 0) == 0)
111 up(&i2c_client_driver_list_lock);
115 /********************* Driver for on-board I2C adapter *********************/
117 /* #define GO7007_I2C_DEBUG */
119 #define SPI_I2C_ADDR_BASE 0x1400
120 #define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
121 #define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
122 #define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
123 #define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
124 #define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
125 #define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
127 #define I2C_STATE_MASK 0x0007
128 #define I2C_READ_READY_MASK 0x0008
130 /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
131 * on the Adlink PCI-MPG24, so access is shared between all of them. */
132 static DECLARE_MUTEX(adlink_mpg24_i2c_lock);
134 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
135 u16 command, int flags, u8 *data)
140 if (go->status == STATUS_SHUTDOWN)
143 #ifdef GO7007_I2C_DEBUG
145 printk(KERN_DEBUG "go7007-i2c: reading 0x%02x on 0x%02x\n",
149 "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
150 *data, command, addr);
155 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
156 /* Bridge the I2C port on this GO7007 to the shared bus */
157 down(&adlink_mpg24_i2c_lock);
158 go7007_write_addr(go, 0x3c82, 0x0020);
161 /* Wait for I2C adapter to be ready */
162 for (i = 0; i < 10; ++i) {
163 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
165 if (!(val & I2C_STATE_MASK))
170 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
174 /* Set target register (command) */
175 go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
176 go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
178 /* If we're writing, send the data and target address and we're done */
180 go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
181 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
182 (addr << 9) | (command >> 8));
187 /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
188 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
191 /* Send the target address plus read flag */
192 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
193 (addr << 9) | 0x0100 | (command >> 8));
195 /* Wait for i2c_rx_data_rdy */
196 for (i = 0; i < 10; ++i) {
197 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
199 if (val & I2C_READ_READY_MASK)
204 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
208 /* Retrieve the read byte */
209 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
215 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
216 /* Isolate the I2C port on this GO7007 from the shared bus */
217 go7007_write_addr(go, 0x3c82, 0x0000);
218 up(&adlink_mpg24_i2c_lock);
224 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
225 unsigned short flags, char read_write,
226 u8 command, int size, union i2c_smbus_data *data)
228 struct go7007 *go = i2c_get_adapdata(adapter);
230 if (size != I2C_SMBUS_BYTE_DATA)
232 return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
233 flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
236 /* VERY LIMITED I2C master xfer function -- only needed because the
237 * SMBus functions only support 8-bit commands and the SAA7135 uses
238 * 16-bit commands. The I2C interface on the GO7007, as limited as
239 * it is, does support this mode. */
241 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
242 struct i2c_msg msgs[], int num)
244 struct go7007 *go = i2c_get_adapdata(adapter);
247 for (i = 0; i < num; ++i) {
248 /* We can only do two things here -- write three bytes, or
249 * write two bytes and read one byte. */
250 if (msgs[i].len == 2) {
251 if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
252 (msgs[i].flags & I2C_M_RD) ||
253 !(msgs[i + 1].flags & I2C_M_RD) ||
254 msgs[i + 1].len != 1)
256 if (go7007_i2c_xfer(go, msgs[i].addr, 1,
257 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
258 0x01, &msgs[i + 1].buf[0]) < 0)
261 } else if (msgs[i].len == 3) {
262 if (msgs[i].flags & I2C_M_RD)
264 if (msgs[i].len != 3)
266 if (go7007_i2c_xfer(go, msgs[i].addr, 0,
267 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
268 0x01, &msgs[i].buf[2]) < 0)
277 static u32 go7007_functionality(struct i2c_adapter *adapter)
279 return I2C_FUNC_SMBUS_BYTE_DATA;
282 static struct i2c_algorithm go7007_algo = {
283 .smbus_xfer = go7007_smbus_xfer,
284 .master_xfer = go7007_i2c_master_xfer,
285 .functionality = go7007_functionality,
288 static struct i2c_adapter go7007_adap_templ = {
289 .owner = THIS_MODULE,
290 .class = I2C_CLASS_TV_ANALOG,
291 .name = "WIS GO7007SB",
292 .id = I2C_ALGO_GO7007,
293 .algo = &go7007_algo,
296 int go7007_i2c_init(struct go7007 *go)
298 memcpy(&go->i2c_adapter, &go7007_adap_templ,
299 sizeof(go7007_adap_templ));
300 go->i2c_adapter.dev.parent = go->dev;
301 i2c_set_adapdata(&go->i2c_adapter, go);
302 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
304 "go7007-i2c: error: i2c_add_adapter failed\n");