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/version.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/sched.h>
23 #include <linux/list.h>
24 #include <linux/unistd.h>
25 #include <linux/time.h>
26 #include <linux/device.h>
27 #include <linux/i2c.h>
28 #include <linux/semaphore.h>
29 #include <linux/uaccess.h>
30 #include <asm/system.h>
32 #include "go7007-priv.h"
35 /************** Registration interface for I2C client drivers **************/
37 /* Since there's no way to auto-probe the I2C devices connected to the I2C
38 * bus on the go7007, we have this silly little registration system that
39 * client drivers can use to register their I2C driver ID and their
40 * detect_client function (the one that's normally passed to i2c_probe).
42 * When a new go7007 device is connected, we can look up in a board info
43 * table by the USB or PCI vendor/product/revision ID to determine
44 * which I2C client module to load. The client driver module will register
45 * itself here, and then we can call the registered detect_client function
46 * to force-load a new client at the address listed in the board info table.
48 * Really the I2C subsystem should have a way to force-load I2C client
49 * drivers when we have a priori knowledge of what's on the bus, especially
50 * since the existing I2C auto-probe mechanism is so hokey, but we'll use
51 * our own mechanism for the time being. */
53 struct wis_i2c_client_driver {
55 found_proc found_proc;
56 struct list_head list;
59 static LIST_HEAD(i2c_client_drivers);
60 static DECLARE_MUTEX(i2c_client_driver_list_lock);
62 /* Client drivers register here by their I2C driver ID */
63 int wis_i2c_add_driver(unsigned int id, found_proc found_proc)
65 struct wis_i2c_client_driver *driver;
67 driver = kmalloc(sizeof(struct wis_i2c_client_driver), GFP_KERNEL);
71 driver->found_proc = found_proc;
73 down(&i2c_client_driver_list_lock);
74 list_add_tail(&driver->list, &i2c_client_drivers);
75 up(&i2c_client_driver_list_lock);
79 EXPORT_SYMBOL(wis_i2c_add_driver);
81 void wis_i2c_del_driver(found_proc found_proc)
83 struct wis_i2c_client_driver *driver, *next;
85 down(&i2c_client_driver_list_lock);
86 list_for_each_entry_safe(driver, next, &i2c_client_drivers, list)
87 if (driver->found_proc == found_proc) {
88 list_del(&driver->list);
91 up(&i2c_client_driver_list_lock);
93 EXPORT_SYMBOL(wis_i2c_del_driver);
95 /* The main go7007 driver calls this to instantiate a client by driver
96 * ID and bus address, which are both stored in the board info table */
97 int wis_i2c_probe_device(struct i2c_adapter *adapter,
98 unsigned int id, int addr)
100 struct wis_i2c_client_driver *driver;
103 if (addr < 0 || addr > 0x7f)
105 down(&i2c_client_driver_list_lock);
106 list_for_each_entry(driver, &i2c_client_drivers, list)
107 if (driver->id == id) {
108 if (driver->found_proc(adapter, addr, 0) == 0)
112 up(&i2c_client_driver_list_lock);
116 /********************* Driver for on-board I2C adapter *********************/
118 /* #define GO7007_I2C_DEBUG */
120 #define SPI_I2C_ADDR_BASE 0x1400
121 #define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
122 #define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
123 #define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
124 #define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
125 #define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
126 #define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
128 #define I2C_STATE_MASK 0x0007
129 #define I2C_READ_READY_MASK 0x0008
131 /* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
132 * on the Adlink PCI-MPG24, so access is shared between all of them. */
133 static DECLARE_MUTEX(adlink_mpg24_i2c_lock);
135 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
136 u16 command, int flags, u8 *data)
141 if (go->status == STATUS_SHUTDOWN)
144 #ifdef GO7007_I2C_DEBUG
146 printk(KERN_DEBUG "go7007-i2c: reading 0x%02x on 0x%02x\n",
150 "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
151 *data, command, addr);
156 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
157 /* Bridge the I2C port on this GO7007 to the shared bus */
158 down(&adlink_mpg24_i2c_lock);
159 go7007_write_addr(go, 0x3c82, 0x0020);
162 /* Wait for I2C adapter to be ready */
163 for (i = 0; i < 10; ++i) {
164 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
166 if (!(val & I2C_STATE_MASK))
171 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
175 /* Set target register (command) */
176 go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
177 go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
179 /* If we're writing, send the data and target address and we're done */
181 go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
182 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
183 (addr << 9) | (command >> 8));
188 /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
189 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
192 /* Send the target address plus read flag */
193 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
194 (addr << 9) | 0x0100 | (command >> 8));
196 /* Wait for i2c_rx_data_rdy */
197 for (i = 0; i < 10; ++i) {
198 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
200 if (val & I2C_READ_READY_MASK)
205 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
209 /* Retrieve the read byte */
210 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
216 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
217 /* Isolate the I2C port on this GO7007 from the shared bus */
218 go7007_write_addr(go, 0x3c82, 0x0000);
219 up(&adlink_mpg24_i2c_lock);
225 static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
226 unsigned short flags, char read_write,
227 u8 command, int size, union i2c_smbus_data *data)
229 struct go7007 *go = i2c_get_adapdata(adapter);
231 if (size != I2C_SMBUS_BYTE_DATA)
233 return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
234 flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
237 /* VERY LIMITED I2C master xfer function -- only needed because the
238 * SMBus functions only support 8-bit commands and the SAA7135 uses
239 * 16-bit commands. The I2C interface on the GO7007, as limited as
240 * it is, does support this mode. */
242 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
243 struct i2c_msg msgs[], int num)
245 struct go7007 *go = i2c_get_adapdata(adapter);
248 for (i = 0; i < num; ++i) {
249 /* We can only do two things here -- write three bytes, or
250 * write two bytes and read one byte. */
251 if (msgs[i].len == 2) {
252 if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
253 (msgs[i].flags & I2C_M_RD) ||
254 !(msgs[i + 1].flags & I2C_M_RD) ||
255 msgs[i + 1].len != 1)
257 if (go7007_i2c_xfer(go, msgs[i].addr, 1,
258 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
259 0x01, &msgs[i + 1].buf[0]) < 0)
262 } else if (msgs[i].len == 3) {
263 if (msgs[i].flags & I2C_M_RD)
265 if (msgs[i].len != 3)
267 if (go7007_i2c_xfer(go, msgs[i].addr, 0,
268 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
269 0x01, &msgs[i].buf[2]) < 0)
278 static u32 go7007_functionality(struct i2c_adapter *adapter)
280 return I2C_FUNC_SMBUS_BYTE_DATA;
283 static struct i2c_algorithm go7007_algo = {
284 .smbus_xfer = go7007_smbus_xfer,
285 .master_xfer = go7007_i2c_master_xfer,
286 .functionality = go7007_functionality,
289 static struct i2c_adapter go7007_adap_templ = {
290 .owner = THIS_MODULE,
291 .class = I2C_CLASS_TV_ANALOG,
292 .name = "WIS GO7007SB",
293 .id = I2C_ALGO_GO7007,
294 .algo = &go7007_algo,
297 int go7007_i2c_init(struct go7007 *go)
299 memcpy(&go->i2c_adapter, &go7007_adap_templ,
300 sizeof(go7007_adap_templ));
301 go->i2c_adapter.dev.parent = go->dev;
302 i2c_set_adapdata(&go->i2c_adapter, go);
303 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
305 "go7007-i2c: error: i2c_add_adapter failed\n");