Merge git://www.linux-watchdog.org/linux-watchdog
[linux-2.6-block.git] / drivers / media / usb / au0828 / au0828-i2c.c
CommitLineData
265a6510
ST
1/*
2 * Driver for the Auvitek AU0828 USB bridge
3 *
6d897616 4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
265a6510
ST
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>
18d73c58 26#include <linux/io.h>
265a6510
ST
27
28#include "au0828.h"
faaf01b2 29#include "media/tuner.h"
265a6510
ST
30#include <media/v4l2-common.h>
31
b33d24c4 32static int i2c_scan;
265a6510
ST
33module_param(i2c_scan, int, 0444);
34MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
35
baede40c
DH
36#define I2C_WAIT_DELAY 25
37#define I2C_WAIT_RETRY 1000
265a6510
ST
38
39static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
40{
41 struct au0828_dev *dev = i2c_adap->algo_data;
9beb0de9
DH
42 return au0828_read(dev, AU0828_I2C_STATUS_201) &
43 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
265a6510
ST
44}
45
46static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
47{
48 struct au0828_dev *dev = i2c_adap->algo_data;
9beb0de9
DH
49 return au0828_read(dev, AU0828_I2C_STATUS_201) &
50 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
265a6510
ST
51}
52
53static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
54{
55 int count;
56
57 for (count = 0; count < I2C_WAIT_RETRY; count++) {
58 if (!i2c_slave_did_read_ack(i2c_adap))
59 break;
60 udelay(I2C_WAIT_DELAY);
61 }
62
63 if (I2C_WAIT_RETRY == count)
64 return 0;
65
66 return 1;
67}
68
69static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
70{
71 struct au0828_dev *dev = i2c_adap->algo_data;
9beb0de9
DH
72 return au0828_read(dev, AU0828_I2C_STATUS_201) &
73 AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
265a6510
ST
74}
75
76static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
77{
78 int count;
79
80 for (count = 0; count < I2C_WAIT_RETRY; count++) {
81 if (!i2c_is_read_busy(i2c_adap))
82 break;
83 udelay(I2C_WAIT_DELAY);
84 }
85
86 if (I2C_WAIT_RETRY == count)
87 return 0;
88
89 return 1;
90}
91
92static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
93{
94 struct au0828_dev *dev = i2c_adap->algo_data;
9beb0de9
DH
95 return au0828_read(dev, AU0828_I2C_STATUS_201) &
96 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
265a6510
ST
97}
98
99static 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
115static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
116{
117 struct au0828_dev *dev = i2c_adap->algo_data;
9beb0de9
DH
118 return au0828_read(dev, AU0828_I2C_STATUS_201) &
119 AU0828_I2C_STATUS_BUSY ? 1 : 0;
265a6510
ST
120}
121
122static int i2c_wait_done(struct i2c_adapter *i2c_adap)
123{
124 int count;
125
126 for (count = 0; count < I2C_WAIT_RETRY; count++) {
127 if (!i2c_is_busy(i2c_adap))
128 break;
129 udelay(I2C_WAIT_DELAY);
130 }
131
132 if (I2C_WAIT_RETRY == count)
133 return 0;
134
135 return 1;
136}
137
138/* FIXME: Implement join handling correctly */
139static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
140 const struct i2c_msg *msg, int joined_rlen)
141{
142 int i, strobe = 0;
143 struct au0828_dev *dev = i2c_adap->algo_data;
cfd0c77d 144 u8 i2c_speed = dev->board.i2c_clk_divider;
265a6510 145
f07e8e4b 146 dprintk(4, "%s()\n", __func__);
265a6510 147
9beb0de9 148 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
32c000ad 149
ad03d300
DH
150 if (((dev->board.tuner_type == TUNER_XC5000) ||
151 (dev->board.tuner_type == TUNER_XC5000C)) &&
cfd0c77d
MCC
152 (dev->board.tuner_addr == msg->addr)) {
153 /*
154 * Due to I2C clock stretch, we need to use a lower speed
155 * on xc5000 for commands. However, firmware transfer can
156 * speed up to 400 KHz.
157 */
158 if (msg->len == 64)
159 i2c_speed = AU0828_I2C_CLK_250KHZ;
160 else
161 i2c_speed = AU0828_I2C_CLK_20KHZ;
faaf01b2 162 }
cfd0c77d
MCC
163 /* Set the I2C clock */
164 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
265a6510
ST
165
166 /* Hardware needs 8 bit addresses */
9beb0de9 167 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
265a6510 168
bc3c613c 169 dprintk(4, "SEND: %02x\n", msg->addr);
265a6510 170
dc8685b5
DH
171 /* Deal with i2c_scan */
172 if (msg->len == 0) {
173 /* The analog tuner detection code makes use of the SMBUS_QUICK
174 message (which involves a zero length i2c write). To avoid
175 checking the status register when we didn't strobe out any
176 actual bytes to the bus, just do a read check. This is
177 consistent with how I saw i2c device checking done in the
178 USB trace of the Windows driver */
9beb0de9
DH
179 au0828_write(dev, AU0828_I2C_TRIGGER_200,
180 AU0828_I2C_TRIGGER_READ);
181
dc8685b5
DH
182 if (!i2c_wait_done(i2c_adap))
183 return -EIO;
184
185 if (i2c_wait_read_ack(i2c_adap))
186 return -EIO;
187
188 return 0;
189 }
190
a9c36aad 191 for (i = 0; i < msg->len;) {
265a6510 192
bc3c613c 193 dprintk(4, " %02x\n", msg->buf[i]);
265a6510 194
9beb0de9 195 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
265a6510
ST
196
197 strobe++;
198 i++;
199
200 if ((strobe >= 4) || (i >= msg->len)) {
201
202 /* Strobe the byte into the bus */
203 if (i < msg->len)
9beb0de9
DH
204 au0828_write(dev, AU0828_I2C_TRIGGER_200,
205 AU0828_I2C_TRIGGER_WRITE |
206 AU0828_I2C_TRIGGER_HOLD);
265a6510 207 else
9beb0de9
DH
208 au0828_write(dev, AU0828_I2C_TRIGGER_200,
209 AU0828_I2C_TRIGGER_WRITE);
265a6510
ST
210
211 /* Reset strobe trigger */
212 strobe = 0;
213
214 if (!i2c_wait_write_done(i2c_adap))
215 return -EIO;
216
217 }
218
219 }
220 if (!i2c_wait_done(i2c_adap))
221 return -EIO;
222
bc3c613c 223 dprintk(4, "\n");
265a6510
ST
224
225 return msg->len;
226}
227
228/* FIXME: Implement join handling correctly */
229static int i2c_readbytes(struct i2c_adapter *i2c_adap,
230 const struct i2c_msg *msg, int joined)
231{
232 struct au0828_dev *dev = i2c_adap->algo_data;
cfd0c77d 233 u8 i2c_speed = dev->board.i2c_clk_divider;
265a6510
ST
234 int i;
235
f07e8e4b 236 dprintk(4, "%s()\n", __func__);
265a6510 237
9beb0de9 238 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
32c000ad 239
cfd0c77d
MCC
240 /*
241 * Due to xc5000c clock stretch, we cannot use full speed at
242 * readings from xc5000, as otherwise they'll fail.
243 */
244 if (((dev->board.tuner_type == TUNER_XC5000) ||
245 (dev->board.tuner_type == TUNER_XC5000C)) &&
246 (dev->board.tuner_addr == msg->addr))
247 i2c_speed = AU0828_I2C_CLK_20KHZ;
248
16af6f5a 249 /* Set the I2C clock */
cfd0c77d 250 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
265a6510
ST
251
252 /* Hardware needs 8 bit addresses */
9beb0de9 253 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
265a6510 254
bc3c613c 255 dprintk(4, " RECV:\n");
265a6510
ST
256
257 /* Deal with i2c_scan */
258 if (msg->len == 0) {
9beb0de9
DH
259 au0828_write(dev, AU0828_I2C_TRIGGER_200,
260 AU0828_I2C_TRIGGER_READ);
261
265a6510
ST
262 if (i2c_wait_read_ack(i2c_adap))
263 return -EIO;
264 return 0;
265 }
266
a9c36aad 267 for (i = 0; i < msg->len;) {
265a6510
ST
268
269 i++;
270
271 if (i < msg->len)
9beb0de9
DH
272 au0828_write(dev, AU0828_I2C_TRIGGER_200,
273 AU0828_I2C_TRIGGER_READ |
274 AU0828_I2C_TRIGGER_HOLD);
265a6510 275 else
9beb0de9
DH
276 au0828_write(dev, AU0828_I2C_TRIGGER_200,
277 AU0828_I2C_TRIGGER_READ);
265a6510
ST
278
279 if (!i2c_wait_read_done(i2c_adap))
280 return -EIO;
281
9beb0de9
DH
282 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
283 0xff;
265a6510 284
bc3c613c 285 dprintk(4, " %02x\n", msg->buf[i-1]);
265a6510
ST
286 }
287 if (!i2c_wait_done(i2c_adap))
288 return -EIO;
289
bc3c613c 290 dprintk(4, "\n");
265a6510
ST
291
292 return msg->len;
293}
294
295static int i2c_xfer(struct i2c_adapter *i2c_adap,
296 struct i2c_msg *msgs, int num)
297{
298 int i, retval = 0;
299
f07e8e4b 300 dprintk(4, "%s(num = %d)\n", __func__, num);
265a6510 301
a9c36aad 302 for (i = 0; i < num; i++) {
bc3c613c 303 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
f07e8e4b 304 __func__, num, msgs[i].addr, msgs[i].len);
265a6510
ST
305 if (msgs[i].flags & I2C_M_RD) {
306 /* read */
307 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
308 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
309 msgs[i].addr == msgs[i + 1].addr) {
310 /* write then read from same address */
311 retval = i2c_sendbytes(i2c_adap, &msgs[i],
312 msgs[i + 1].len);
313 if (retval < 0)
314 goto err;
315 i++;
316 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
317 } else {
318 /* write */
319 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
320 }
321 if (retval < 0)
322 goto err;
323 }
324 return num;
325
326err:
327 return retval;
328}
329
265a6510
ST
330static u32 au0828_functionality(struct i2c_adapter *adap)
331{
332 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
333}
334
335static struct i2c_algorithm au0828_i2c_algo_template = {
336 .master_xfer = i2c_xfer,
337 .functionality = au0828_functionality,
338};
339
340/* ----------------------------------------------------------------------- */
341
342static struct i2c_adapter au0828_i2c_adap_template = {
343 .name = DRIVER_NAME,
344 .owner = THIS_MODULE,
265a6510 345 .algo = &au0828_i2c_algo_template,
265a6510
ST
346};
347
348static struct i2c_client au0828_i2c_client_template = {
349 .name = "au0828 internal",
350};
351
352static char *i2c_devs[128] = {
18d73c58
MCC
353 [0x8e >> 1] = "au8522",
354 [0xa0 >> 1] = "eeprom",
355 [0xc2 >> 1] = "tuner/xc5000",
265a6510
ST
356};
357
358static void do_i2c_scan(char *name, struct i2c_client *c)
359{
360 unsigned char buf;
361 int i, rc;
362
363 for (i = 0; i < 128; i++) {
364 c->addr = i;
365 rc = i2c_master_recv(c, &buf, 0);
366 if (rc < 0)
367 continue;
18d73c58 368 printk(KERN_INFO "%s: i2c scan: found device @ 0x%x [%s]\n",
265a6510
ST
369 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
370 }
371}
372
a824f0f4 373/* init + register i2c adapter */
265a6510
ST
374int au0828_i2c_register(struct au0828_dev *dev)
375{
f07e8e4b 376 dprintk(1, "%s()\n", __func__);
265a6510 377
f01e0ffd
EG
378 dev->i2c_adap = au0828_i2c_adap_template;
379 dev->i2c_algo = au0828_i2c_algo_template;
380 dev->i2c_client = au0828_i2c_client_template;
265a6510
ST
381
382 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
383
384 strlcpy(dev->i2c_adap.name, DRIVER_NAME,
385 sizeof(dev->i2c_adap.name));
386
b14667f3 387 dev->i2c_adap.algo = &dev->i2c_algo;
265a6510 388 dev->i2c_adap.algo_data = dev;
8a4e7866 389#ifdef CONFIG_VIDEO_AU0828_V4L2
b14667f3 390 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
8a4e7866
MK
391#else
392 i2c_set_adapdata(&dev->i2c_adap, dev);
393#endif
265a6510
ST
394 i2c_add_adapter(&dev->i2c_adap);
395
396 dev->i2c_client.adapter = &dev->i2c_adap;
397
398 if (0 == dev->i2c_rc) {
18d73c58 399 printk(KERN_INFO "%s: i2c bus registered\n", DRIVER_NAME);
265a6510
ST
400 if (i2c_scan)
401 do_i2c_scan(DRIVER_NAME, &dev->i2c_client);
402 } else
18d73c58 403 printk(KERN_INFO "%s: i2c bus register FAILED\n", DRIVER_NAME);
bc3c613c 404
265a6510
ST
405 return dev->i2c_rc;
406}
407
408int au0828_i2c_unregister(struct au0828_dev *dev)
409{
410 i2c_del_adapter(&dev->i2c_adap);
411 return 0;
412}
413