diff options
Diffstat (limited to 'drivers/media/video/au0828/au0828-i2c.c')
-rw-r--r-- | drivers/media/video/au0828/au0828-i2c.c | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/drivers/media/video/au0828/au0828-i2c.c b/drivers/media/video/au0828/au0828-i2c.c new file mode 100644 index 000000000000..94c8b74a6651 --- /dev/null +++ b/drivers/media/video/au0828/au0828-i2c.c | |||
@@ -0,0 +1,385 @@ | |||
1 | /* | ||
2 | * Driver for the Auvitek AU0828 USB bridge | ||
3 | * | ||
4 | * Copyright (c) 2008 Steven Toth <stoth@hauppauge.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program; if not, write to the Free Software | ||
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
20 | */ | ||
21 | |||
22 | #include <linux/module.h> | ||
23 | #include <linux/moduleparam.h> | ||
24 | #include <linux/init.h> | ||
25 | #include <linux/delay.h> | ||
26 | #include <linux/io.h> | ||
27 | |||
28 | #include "au0828.h" | ||
29 | |||
30 | #include <media/v4l2-common.h> | ||
31 | |||
32 | unsigned int i2c_debug; | ||
33 | module_param(i2c_debug, int, 0444); | ||
34 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); | ||
35 | |||
36 | unsigned int i2c_scan; | ||
37 | module_param(i2c_scan, int, 0444); | ||
38 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); | ||
39 | |||
40 | #define I2C_WAIT_DELAY 512 | ||
41 | #define I2C_WAIT_RETRY 64 | ||
42 | |||
43 | static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap) | ||
44 | { | ||
45 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
46 | return au0828_read(dev, REG_201) & 0x08 ? 0 : 1; | ||
47 | } | ||
48 | |||
49 | static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap) | ||
50 | { | ||
51 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
52 | return au0828_read(dev, REG_201) & 0x02 ? 0 : 1; | ||
53 | } | ||
54 | |||
55 | static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap) | ||
56 | { | ||
57 | int count; | ||
58 | |||
59 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | ||
60 | if (!i2c_slave_did_read_ack(i2c_adap)) | ||
61 | break; | ||
62 | udelay(I2C_WAIT_DELAY); | ||
63 | } | ||
64 | |||
65 | if (I2C_WAIT_RETRY == count) | ||
66 | return 0; | ||
67 | |||
68 | return 1; | ||
69 | } | ||
70 | |||
71 | static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap) | ||
72 | { | ||
73 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
74 | return au0828_read(dev, REG_201) & 0x01 ? 0 : 1; | ||
75 | } | ||
76 | |||
77 | static int i2c_wait_read_done(struct i2c_adapter *i2c_adap) | ||
78 | { | ||
79 | int count; | ||
80 | |||
81 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | ||
82 | if (!i2c_is_read_busy(i2c_adap)) | ||
83 | break; | ||
84 | udelay(I2C_WAIT_DELAY); | ||
85 | } | ||
86 | |||
87 | if (I2C_WAIT_RETRY == count) | ||
88 | return 0; | ||
89 | |||
90 | return 1; | ||
91 | } | ||
92 | |||
93 | static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap) | ||
94 | { | ||
95 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
96 | return au0828_read(dev, REG_201) & 0x04 ? 1 : 0; | ||
97 | } | ||
98 | |||
99 | static int i2c_wait_write_done(struct i2c_adapter *i2c_adap) | ||
100 | { | ||
101 | int count; | ||
102 | |||
103 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | ||
104 | if (i2c_is_write_done(i2c_adap)) | ||
105 | break; | ||
106 | udelay(I2C_WAIT_DELAY); | ||
107 | } | ||
108 | |||
109 | if (I2C_WAIT_RETRY == count) | ||
110 | return 0; | ||
111 | |||
112 | return 1; | ||
113 | } | ||
114 | |||
115 | static inline int i2c_is_busy(struct i2c_adapter *i2c_adap) | ||
116 | { | ||
117 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
118 | return au0828_read(dev, REG_201) & 0x10 ? 1 : 0; | ||
119 | } | ||
120 | |||
121 | static int i2c_wait_done(struct i2c_adapter *i2c_adap) | ||
122 | { | ||
123 | int count; | ||
124 | |||
125 | for (count = 0; count < I2C_WAIT_RETRY; count++) { | ||
126 | if (!i2c_is_busy(i2c_adap)) | ||
127 | break; | ||
128 | udelay(I2C_WAIT_DELAY); | ||
129 | } | ||
130 | |||
131 | if (I2C_WAIT_RETRY == count) | ||
132 | return 0; | ||
133 | |||
134 | return 1; | ||
135 | } | ||
136 | |||
137 | /* FIXME: Implement join handling correctly */ | ||
138 | static int i2c_sendbytes(struct i2c_adapter *i2c_adap, | ||
139 | const struct i2c_msg *msg, int joined_rlen) | ||
140 | { | ||
141 | int i, strobe = 0; | ||
142 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
143 | |||
144 | dprintk(4, "%s()\n", __func__); | ||
145 | |||
146 | au0828_write(dev, REG_2FF, 0x01); | ||
147 | au0828_write(dev, REG_202, 0x07); | ||
148 | |||
149 | /* Hardware needs 8 bit addresses */ | ||
150 | au0828_write(dev, REG_203, msg->addr << 1); | ||
151 | |||
152 | dprintk(4, "SEND: %02x\n", msg->addr); | ||
153 | |||
154 | for (i = 0; i < msg->len;) { | ||
155 | |||
156 | dprintk(4, " %02x\n", msg->buf[i]); | ||
157 | |||
158 | au0828_write(dev, REG_205, msg->buf[i]); | ||
159 | |||
160 | strobe++; | ||
161 | i++; | ||
162 | |||
163 | if ((strobe >= 4) || (i >= msg->len)) { | ||
164 | |||
165 | /* Strobe the byte into the bus */ | ||
166 | if (i < msg->len) | ||
167 | au0828_write(dev, REG_200, 0x41); | ||
168 | else | ||
169 | au0828_write(dev, REG_200, 0x01); | ||
170 | |||
171 | /* Reset strobe trigger */ | ||
172 | strobe = 0; | ||
173 | |||
174 | if (!i2c_wait_write_done(i2c_adap)) | ||
175 | return -EIO; | ||
176 | |||
177 | } | ||
178 | |||
179 | } | ||
180 | if (!i2c_wait_done(i2c_adap)) | ||
181 | return -EIO; | ||
182 | |||
183 | dprintk(4, "\n"); | ||
184 | |||
185 | return msg->len; | ||
186 | } | ||
187 | |||
188 | /* FIXME: Implement join handling correctly */ | ||
189 | static int i2c_readbytes(struct i2c_adapter *i2c_adap, | ||
190 | const struct i2c_msg *msg, int joined) | ||
191 | { | ||
192 | struct au0828_dev *dev = i2c_adap->algo_data; | ||
193 | int i; | ||
194 | |||
195 | dprintk(4, "%s()\n", __func__); | ||
196 | |||
197 | au0828_write(dev, REG_2FF, 0x01); | ||
198 | au0828_write(dev, REG_202, 0x07); | ||
199 | |||
200 | /* Hardware needs 8 bit addresses */ | ||
201 | au0828_write(dev, REG_203, msg->addr << 1); | ||
202 | |||
203 | dprintk(4, " RECV:\n"); | ||
204 | |||
205 | /* Deal with i2c_scan */ | ||
206 | if (msg->len == 0) { | ||
207 | au0828_write(dev, REG_200, 0x20); | ||
208 | if (i2c_wait_read_ack(i2c_adap)) | ||
209 | return -EIO; | ||
210 | return 0; | ||
211 | } | ||
212 | |||
213 | for (i = 0; i < msg->len;) { | ||
214 | |||
215 | i++; | ||
216 | |||
217 | if (i < msg->len) | ||
218 | au0828_write(dev, REG_200, 0x60); | ||
219 | else | ||
220 | au0828_write(dev, REG_200, 0x20); | ||
221 | |||
222 | if (!i2c_wait_read_done(i2c_adap)) | ||
223 | return -EIO; | ||
224 | |||
225 | msg->buf[i-1] = au0828_read(dev, REG_209) & 0xff; | ||
226 | |||
227 | dprintk(4, " %02x\n", msg->buf[i-1]); | ||
228 | } | ||
229 | if (!i2c_wait_done(i2c_adap)) | ||
230 | return -EIO; | ||
231 | |||
232 | dprintk(4, "\n"); | ||
233 | |||
234 | return msg->len; | ||
235 | } | ||
236 | |||
237 | static int i2c_xfer(struct i2c_adapter *i2c_adap, | ||
238 | struct i2c_msg *msgs, int num) | ||
239 | { | ||
240 | int i, retval = 0; | ||
241 | |||
242 | dprintk(4, "%s(num = %d)\n", __func__, num); | ||
243 | |||
244 | for (i = 0; i < num; i++) { | ||
245 | dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n", | ||
246 | __func__, num, msgs[i].addr, msgs[i].len); | ||
247 | if (msgs[i].flags & I2C_M_RD) { | ||
248 | /* read */ | ||
249 | retval = i2c_readbytes(i2c_adap, &msgs[i], 0); | ||
250 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && | ||
251 | msgs[i].addr == msgs[i + 1].addr) { | ||
252 | /* write then read from same address */ | ||
253 | retval = i2c_sendbytes(i2c_adap, &msgs[i], | ||
254 | msgs[i + 1].len); | ||
255 | if (retval < 0) | ||
256 | goto err; | ||
257 | i++; | ||
258 | retval = i2c_readbytes(i2c_adap, &msgs[i], 1); | ||
259 | } else { | ||
260 | /* write */ | ||
261 | retval = i2c_sendbytes(i2c_adap, &msgs[i], 0); | ||
262 | } | ||
263 | if (retval < 0) | ||
264 | goto err; | ||
265 | } | ||
266 | return num; | ||
267 | |||
268 | err: | ||
269 | return retval; | ||
270 | } | ||
271 | |||
272 | static int attach_inform(struct i2c_client *client) | ||
273 | { | ||
274 | dprintk(1, "%s i2c attach [addr=0x%x,client=%s]\n", | ||
275 | client->driver->driver.name, client->addr, client->name); | ||
276 | |||
277 | if (!client->driver->command) | ||
278 | return 0; | ||
279 | |||
280 | return 0; | ||
281 | } | ||
282 | |||
283 | static int detach_inform(struct i2c_client *client) | ||
284 | { | ||
285 | dprintk(1, "i2c detach [client=%s]\n", client->name); | ||
286 | |||
287 | return 0; | ||
288 | } | ||
289 | |||
290 | void au0828_call_i2c_clients(struct au0828_dev *dev, | ||
291 | unsigned int cmd, void *arg) | ||
292 | { | ||
293 | if (dev->i2c_rc != 0) | ||
294 | return; | ||
295 | |||
296 | i2c_clients_command(&dev->i2c_adap, cmd, arg); | ||
297 | } | ||
298 | |||
299 | static u32 au0828_functionality(struct i2c_adapter *adap) | ||
300 | { | ||
301 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; | ||
302 | } | ||
303 | |||
304 | static struct i2c_algorithm au0828_i2c_algo_template = { | ||
305 | .master_xfer = i2c_xfer, | ||
306 | .functionality = au0828_functionality, | ||
307 | }; | ||
308 | |||
309 | /* ----------------------------------------------------------------------- */ | ||
310 | |||
311 | static struct i2c_adapter au0828_i2c_adap_template = { | ||
312 | .name = DRIVER_NAME, | ||
313 | .owner = THIS_MODULE, | ||
314 | .id = I2C_HW_B_AU0828, | ||
315 | .algo = &au0828_i2c_algo_template, | ||
316 | .class = I2C_CLASS_TV_ANALOG, | ||
317 | .client_register = attach_inform, | ||
318 | .client_unregister = detach_inform, | ||
319 | }; | ||
320 | |||
321 | static struct i2c_client au0828_i2c_client_template = { | ||
322 | .name = "au0828 internal", | ||
323 | }; | ||
324 | |||
325 | static char *i2c_devs[128] = { | ||
326 | [0x8e >> 1] = "au8522", | ||
327 | [0xa0 >> 1] = "eeprom", | ||
328 | [0xc2 >> 1] = "tuner/xc5000", | ||
329 | }; | ||
330 | |||
331 | static void do_i2c_scan(char *name, struct i2c_client *c) | ||
332 | { | ||
333 | unsigned char buf; | ||
334 | int i, rc; | ||
335 | |||
336 | for (i = 0; i < 128; i++) { | ||
337 | c->addr = i; | ||
338 | rc = i2c_master_recv(c, &buf, 0); | ||
339 | if (rc < 0) | ||
340 | continue; | ||
341 | printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n", | ||
342 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); | ||
343 | } | ||
344 | } | ||
345 | |||
346 | /* init + register i2c algo-bit adapter */ | ||
347 | int au0828_i2c_register(struct au0828_dev *dev) | ||
348 | { | ||
349 | dprintk(1, "%s()\n", __func__); | ||
350 | |||
351 | memcpy(&dev->i2c_adap, &au0828_i2c_adap_template, | ||
352 | sizeof(dev->i2c_adap)); | ||
353 | memcpy(&dev->i2c_algo, &au0828_i2c_algo_template, | ||
354 | sizeof(dev->i2c_algo)); | ||
355 | memcpy(&dev->i2c_client, &au0828_i2c_client_template, | ||
356 | sizeof(dev->i2c_client)); | ||
357 | |||
358 | dev->i2c_adap.dev.parent = &dev->usbdev->dev; | ||
359 | |||
360 | strlcpy(dev->i2c_adap.name, DRIVER_NAME, | ||
361 | sizeof(dev->i2c_adap.name)); | ||
362 | |||
363 | dev->i2c_algo.data = dev; | ||
364 | dev->i2c_adap.algo_data = dev; | ||
365 | i2c_set_adapdata(&dev->i2c_adap, dev); | ||
366 | i2c_add_adapter(&dev->i2c_adap); | ||
367 | |||
368 | dev->i2c_client.adapter = &dev->i2c_adap; | ||
369 | |||
370 | if (0 == dev->i2c_rc) { | ||
371 | printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME); | ||
372 | if (i2c_scan) | ||
373 | do_i2c_scan(DRIVER_NAME, &dev->i2c_client); | ||
374 | } else | ||
375 | printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME); | ||
376 | |||
377 | return dev->i2c_rc; | ||
378 | } | ||
379 | |||
380 | int au0828_i2c_unregister(struct au0828_dev *dev) | ||
381 | { | ||
382 | i2c_del_adapter(&dev->i2c_adap); | ||
383 | return 0; | ||
384 | } | ||
385 | |||