perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / au0828 / au0828-i2c.c
blob1b8ec5d9e7ab8aca9f9dff2cdbe25fdf7fdee130
1 /*
2 * Driver for the Auvitek AU0828 USB bridge
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
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.
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
15 * GNU General Public License for more details.
18 #include "au0828.h"
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/io.h>
26 #include "media/tuner.h"
27 #include <media/v4l2-common.h>
29 static int i2c_scan;
30 module_param(i2c_scan, int, 0444);
31 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
33 #define I2C_WAIT_DELAY 25
34 #define I2C_WAIT_RETRY 1000
36 static inline int i2c_slave_did_write_ack(struct i2c_adapter *i2c_adap)
38 struct au0828_dev *dev = i2c_adap->algo_data;
39 return au0828_read(dev, AU0828_I2C_STATUS_201) &
40 AU0828_I2C_STATUS_NO_WRITE_ACK ? 0 : 1;
43 static inline int i2c_slave_did_read_ack(struct i2c_adapter *i2c_adap)
45 struct au0828_dev *dev = i2c_adap->algo_data;
46 return au0828_read(dev, AU0828_I2C_STATUS_201) &
47 AU0828_I2C_STATUS_NO_READ_ACK ? 0 : 1;
50 static int i2c_wait_read_ack(struct i2c_adapter *i2c_adap)
52 int count;
54 for (count = 0; count < I2C_WAIT_RETRY; count++) {
55 if (!i2c_slave_did_read_ack(i2c_adap))
56 break;
57 udelay(I2C_WAIT_DELAY);
60 if (I2C_WAIT_RETRY == count)
61 return 0;
63 return 1;
66 static inline int i2c_is_read_busy(struct i2c_adapter *i2c_adap)
68 struct au0828_dev *dev = i2c_adap->algo_data;
69 return au0828_read(dev, AU0828_I2C_STATUS_201) &
70 AU0828_I2C_STATUS_READ_DONE ? 0 : 1;
73 static int i2c_wait_read_done(struct i2c_adapter *i2c_adap)
75 int count;
77 for (count = 0; count < I2C_WAIT_RETRY; count++) {
78 if (!i2c_is_read_busy(i2c_adap))
79 break;
80 udelay(I2C_WAIT_DELAY);
83 if (I2C_WAIT_RETRY == count)
84 return 0;
86 return 1;
89 static inline int i2c_is_write_done(struct i2c_adapter *i2c_adap)
91 struct au0828_dev *dev = i2c_adap->algo_data;
92 return au0828_read(dev, AU0828_I2C_STATUS_201) &
93 AU0828_I2C_STATUS_WRITE_DONE ? 1 : 0;
96 static int i2c_wait_write_done(struct i2c_adapter *i2c_adap)
98 int count;
100 for (count = 0; count < I2C_WAIT_RETRY; count++) {
101 if (i2c_is_write_done(i2c_adap))
102 break;
103 udelay(I2C_WAIT_DELAY);
106 if (I2C_WAIT_RETRY == count)
107 return 0;
109 return 1;
112 static inline int i2c_is_busy(struct i2c_adapter *i2c_adap)
114 struct au0828_dev *dev = i2c_adap->algo_data;
115 return au0828_read(dev, AU0828_I2C_STATUS_201) &
116 AU0828_I2C_STATUS_BUSY ? 1 : 0;
119 static int i2c_wait_done(struct i2c_adapter *i2c_adap)
121 int count;
123 for (count = 0; count < I2C_WAIT_RETRY; count++) {
124 if (!i2c_is_busy(i2c_adap))
125 break;
126 udelay(I2C_WAIT_DELAY);
129 if (I2C_WAIT_RETRY == count)
130 return 0;
132 return 1;
135 /* FIXME: Implement join handling correctly */
136 static int i2c_sendbytes(struct i2c_adapter *i2c_adap,
137 const struct i2c_msg *msg, int joined_rlen)
139 int i, strobe = 0;
140 struct au0828_dev *dev = i2c_adap->algo_data;
141 u8 i2c_speed = dev->board.i2c_clk_divider;
143 dprintk(4, "%s()\n", __func__);
145 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
147 if (((dev->board.tuner_type == TUNER_XC5000) ||
148 (dev->board.tuner_type == TUNER_XC5000C)) &&
149 (dev->board.tuner_addr == msg->addr)) {
151 * Due to I2C clock stretch, we need to use a lower speed
152 * on xc5000 for commands. However, firmware transfer can
153 * speed up to 400 KHz.
155 if (msg->len == 64)
156 i2c_speed = AU0828_I2C_CLK_250KHZ;
157 else
158 i2c_speed = AU0828_I2C_CLK_20KHZ;
160 /* Set the I2C clock */
161 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
163 /* Hardware needs 8 bit addresses */
164 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
166 dprintk(4, "SEND: %02x\n", msg->addr);
168 /* Deal with i2c_scan */
169 if (msg->len == 0) {
170 /* The analog tuner detection code makes use of the SMBUS_QUICK
171 message (which involves a zero length i2c write). To avoid
172 checking the status register when we didn't strobe out any
173 actual bytes to the bus, just do a read check. This is
174 consistent with how I saw i2c device checking done in the
175 USB trace of the Windows driver */
176 au0828_write(dev, AU0828_I2C_TRIGGER_200,
177 AU0828_I2C_TRIGGER_READ);
179 if (!i2c_wait_done(i2c_adap))
180 return -EIO;
182 if (i2c_wait_read_ack(i2c_adap))
183 return -EIO;
185 return 0;
188 for (i = 0; i < msg->len;) {
190 dprintk(4, " %02x\n", msg->buf[i]);
192 au0828_write(dev, AU0828_I2C_WRITE_FIFO_205, msg->buf[i]);
194 strobe++;
195 i++;
197 if ((strobe >= 4) || (i >= msg->len)) {
199 /* Strobe the byte into the bus */
200 if (i < msg->len)
201 au0828_write(dev, AU0828_I2C_TRIGGER_200,
202 AU0828_I2C_TRIGGER_WRITE |
203 AU0828_I2C_TRIGGER_HOLD);
204 else
205 au0828_write(dev, AU0828_I2C_TRIGGER_200,
206 AU0828_I2C_TRIGGER_WRITE);
208 /* Reset strobe trigger */
209 strobe = 0;
211 if (!i2c_wait_write_done(i2c_adap))
212 return -EIO;
217 if (!i2c_wait_done(i2c_adap))
218 return -EIO;
220 dprintk(4, "\n");
222 return msg->len;
225 /* FIXME: Implement join handling correctly */
226 static int i2c_readbytes(struct i2c_adapter *i2c_adap,
227 const struct i2c_msg *msg, int joined)
229 struct au0828_dev *dev = i2c_adap->algo_data;
230 u8 i2c_speed = dev->board.i2c_clk_divider;
231 int i;
233 dprintk(4, "%s()\n", __func__);
235 au0828_write(dev, AU0828_I2C_MULTIBYTE_MODE_2FF, 0x01);
238 * Due to xc5000c clock stretch, we cannot use full speed at
239 * readings from xc5000, as otherwise they'll fail.
241 if (((dev->board.tuner_type == TUNER_XC5000) ||
242 (dev->board.tuner_type == TUNER_XC5000C)) &&
243 (dev->board.tuner_addr == msg->addr))
244 i2c_speed = AU0828_I2C_CLK_20KHZ;
246 /* Set the I2C clock */
247 au0828_write(dev, AU0828_I2C_CLK_DIVIDER_202, i2c_speed);
249 /* Hardware needs 8 bit addresses */
250 au0828_write(dev, AU0828_I2C_DEST_ADDR_203, msg->addr << 1);
252 dprintk(4, " RECV:\n");
254 /* Deal with i2c_scan */
255 if (msg->len == 0) {
256 au0828_write(dev, AU0828_I2C_TRIGGER_200,
257 AU0828_I2C_TRIGGER_READ);
259 if (i2c_wait_read_ack(i2c_adap))
260 return -EIO;
261 return 0;
264 for (i = 0; i < msg->len;) {
266 i++;
268 if (i < msg->len)
269 au0828_write(dev, AU0828_I2C_TRIGGER_200,
270 AU0828_I2C_TRIGGER_READ |
271 AU0828_I2C_TRIGGER_HOLD);
272 else
273 au0828_write(dev, AU0828_I2C_TRIGGER_200,
274 AU0828_I2C_TRIGGER_READ);
276 if (!i2c_wait_read_done(i2c_adap))
277 return -EIO;
279 msg->buf[i-1] = au0828_read(dev, AU0828_I2C_READ_FIFO_209) &
280 0xff;
282 dprintk(4, " %02x\n", msg->buf[i-1]);
284 if (!i2c_wait_done(i2c_adap))
285 return -EIO;
287 dprintk(4, "\n");
289 return msg->len;
292 static int i2c_xfer(struct i2c_adapter *i2c_adap,
293 struct i2c_msg *msgs, int num)
295 int i, retval = 0;
297 dprintk(4, "%s(num = %d)\n", __func__, num);
299 for (i = 0; i < num; i++) {
300 dprintk(4, "%s(num = %d) addr = 0x%02x len = 0x%x\n",
301 __func__, num, msgs[i].addr, msgs[i].len);
302 if (msgs[i].flags & I2C_M_RD) {
303 /* read */
304 retval = i2c_readbytes(i2c_adap, &msgs[i], 0);
305 } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) &&
306 msgs[i].addr == msgs[i + 1].addr) {
307 /* write then read from same address */
308 retval = i2c_sendbytes(i2c_adap, &msgs[i],
309 msgs[i + 1].len);
310 if (retval < 0)
311 goto err;
312 i++;
313 retval = i2c_readbytes(i2c_adap, &msgs[i], 1);
314 } else {
315 /* write */
316 retval = i2c_sendbytes(i2c_adap, &msgs[i], 0);
318 if (retval < 0)
319 goto err;
321 return num;
323 err:
324 return retval;
327 static u32 au0828_functionality(struct i2c_adapter *adap)
329 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C;
332 static const struct i2c_algorithm au0828_i2c_algo_template = {
333 .master_xfer = i2c_xfer,
334 .functionality = au0828_functionality,
337 /* ----------------------------------------------------------------------- */
339 static const struct i2c_adapter au0828_i2c_adap_template = {
340 .name = KBUILD_MODNAME,
341 .owner = THIS_MODULE,
342 .algo = &au0828_i2c_algo_template,
345 static const struct i2c_client au0828_i2c_client_template = {
346 .name = "au0828 internal",
349 static char *i2c_devs[128] = {
350 [0x8e >> 1] = "au8522",
351 [0xa0 >> 1] = "eeprom",
352 [0xc2 >> 1] = "tuner/xc5000",
355 static void do_i2c_scan(char *name, struct i2c_client *c)
357 unsigned char buf;
358 int i, rc;
360 for (i = 0; i < 128; i++) {
361 c->addr = i;
362 rc = i2c_master_recv(c, &buf, 0);
363 if (rc < 0)
364 continue;
365 pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
366 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
370 /* init + register i2c adapter */
371 int au0828_i2c_register(struct au0828_dev *dev)
373 dprintk(1, "%s()\n", __func__);
375 dev->i2c_adap = au0828_i2c_adap_template;
376 dev->i2c_algo = au0828_i2c_algo_template;
377 dev->i2c_client = au0828_i2c_client_template;
379 dev->i2c_adap.dev.parent = &dev->usbdev->dev;
381 strlcpy(dev->i2c_adap.name, KBUILD_MODNAME,
382 sizeof(dev->i2c_adap.name));
384 dev->i2c_adap.algo = &dev->i2c_algo;
385 dev->i2c_adap.algo_data = dev;
386 #ifdef CONFIG_VIDEO_AU0828_V4L2
387 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
388 #else
389 i2c_set_adapdata(&dev->i2c_adap, dev);
390 #endif
391 i2c_add_adapter(&dev->i2c_adap);
393 dev->i2c_client.adapter = &dev->i2c_adap;
395 if (0 == dev->i2c_rc) {
396 pr_info("i2c bus registered\n");
397 if (i2c_scan)
398 do_i2c_scan(KBUILD_MODNAME, &dev->i2c_client);
399 } else
400 pr_info("i2c bus register FAILED\n");
402 return dev->i2c_rc;
405 int au0828_i2c_unregister(struct au0828_dev *dev)
407 i2c_del_adapter(&dev->i2c_adap);
408 return 0;