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