]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/go7007/go7007-i2c.c
dock: make dock driver not a module
[linux-2.6-omap-h63xx.git] / drivers / staging / go7007 / go7007-i2c.c
1 /*
2  * Copyright (C) 2005-2006 Micronas USA Inc.
3  *
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.
7  *
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.
12  *
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.
16  */
17
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>
31
32 #include "go7007-priv.h"
33 #include "wis-i2c.h"
34
35 /************** Registration interface for I2C client drivers **************/
36
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).
41  *
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.
47  *
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. */
52
53 struct wis_i2c_client_driver {
54         unsigned int id;
55         found_proc found_proc;
56         struct list_head list;
57 };
58
59 static LIST_HEAD(i2c_client_drivers);
60 static DECLARE_MUTEX(i2c_client_driver_list_lock);
61
62 /* Client drivers register here by their I2C driver ID */
63 int wis_i2c_add_driver(unsigned int id, found_proc found_proc)
64 {
65         struct wis_i2c_client_driver *driver;
66
67         driver = kmalloc(sizeof(struct wis_i2c_client_driver), GFP_KERNEL);
68         if (driver == NULL)
69                 return -ENOMEM;
70         driver->id = id;
71         driver->found_proc = found_proc;
72
73         down(&i2c_client_driver_list_lock);
74         list_add_tail(&driver->list, &i2c_client_drivers);
75         up(&i2c_client_driver_list_lock);
76
77         return 0;
78 }
79 EXPORT_SYMBOL(wis_i2c_add_driver);
80
81 void wis_i2c_del_driver(found_proc found_proc)
82 {
83         struct wis_i2c_client_driver *driver, *next;
84
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);
89                         kfree(driver);
90                 }
91         up(&i2c_client_driver_list_lock);
92 }
93 EXPORT_SYMBOL(wis_i2c_del_driver);
94
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)
99 {
100         struct wis_i2c_client_driver *driver;
101         int found = 0;
102
103         if (addr < 0 || addr > 0x7f)
104                 return -1;
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)
109                                 found = 1;
110                         break;
111                 }
112         up(&i2c_client_driver_list_lock);
113         return found;
114 }
115
116 /********************* Driver for on-board I2C adapter *********************/
117
118 /* #define GO7007_I2C_DEBUG */
119
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)
127
128 #define I2C_STATE_MASK                  0x0007
129 #define I2C_READ_READY_MASK             0x0008
130
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);
134
135 static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
136                 u16 command, int flags, u8 *data)
137 {
138         int i, ret = -1;
139         u16 val;
140
141         if (go->status == STATUS_SHUTDOWN)
142                 return -1;
143
144 #ifdef GO7007_I2C_DEBUG
145         if (read)
146                 printk(KERN_DEBUG "go7007-i2c: reading 0x%02x on 0x%02x\n",
147                         command, addr);
148         else
149                 printk(KERN_DEBUG
150                         "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
151                         *data, command, addr);
152 #endif
153
154         down(&go->hw_lock);
155
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);
160         }
161
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)
165                         goto i2c_done;
166                 if (!(val & I2C_STATE_MASK))
167                         break;
168                 msleep(100);
169         }
170         if (i == 10) {
171                 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
172                 goto i2c_done;
173         }
174
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);
178
179         /* If we're writing, send the data and target address and we're done */
180         if (!read) {
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));
184                 ret = 0;
185                 goto i2c_done;
186         }
187
188         /* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
189         if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
190                 goto i2c_done;
191
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));
195
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)
199                         goto i2c_done;
200                 if (val & I2C_READ_READY_MASK)
201                         break;
202                 msleep(100);
203         }
204         if (i == 10) {
205                 printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
206                 goto i2c_done;
207         }
208
209         /* Retrieve the read byte */
210         if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
211                 goto i2c_done;
212         *data = val;
213         ret = 0;
214
215 i2c_done:
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);
220         }
221         up(&go->hw_lock);
222         return ret;
223 }
224
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)
228 {
229         struct go7007 *go = i2c_get_adapdata(adapter);
230
231         if (size != I2C_SMBUS_BYTE_DATA)
232                 return -1;
233         return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
234                         flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
235 }
236
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. */
241
242 static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
243                                         struct i2c_msg msgs[], int num)
244 {
245         struct go7007 *go = i2c_get_adapdata(adapter);
246         int i;
247
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)
256                                 return -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)
260                                 return -1;
261                         ++i;
262                 } else if (msgs[i].len == 3) {
263                         if (msgs[i].flags & I2C_M_RD)
264                                 return -1;
265                         if (msgs[i].len != 3)
266                                 return -1;
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)
270                                 return -1;
271                 } else
272                         return -1;
273         }
274
275         return 0;
276 }
277
278 static u32 go7007_functionality(struct i2c_adapter *adapter)
279 {
280         return I2C_FUNC_SMBUS_BYTE_DATA;
281 }
282
283 static struct i2c_algorithm go7007_algo = {
284         .smbus_xfer     = go7007_smbus_xfer,
285         .master_xfer    = go7007_i2c_master_xfer,
286         .functionality  = go7007_functionality,
287 };
288
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,
295 };
296
297 int go7007_i2c_init(struct go7007 *go)
298 {
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) {
304                 printk(KERN_ERR
305                         "go7007-i2c: error: i2c_add_adapter failed\n");
306                 return -1;
307         }
308         return 0;
309 }